Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

62 строки
1.8 KiB

  1. #pragma once
  2. #ifdef _PROFILING
  3. # include <stdlib.h>
  4. # include <stdio.h>
  5. # include <string.h>
  6. # include <time.h>
  7. // for syscall:
  8. # include <unistd.h>
  9. # include <sys/syscall.h>
  10. struct Profiler {
  11. // same for all threads
  12. inline static char file_template[40] = "\0";
  13. // thread local
  14. inline thread_local static bool is_initialized = false;
  15. inline thread_local static int thread_id = -1;
  16. inline thread_local static int call_depth = 0;
  17. inline thread_local static FILE* out_file = nullptr;
  18. Profiler(const char* file, const char* func, const int line) {
  19. call_depth += 1;
  20. // if we never used this thread before
  21. if (!is_initialized) {
  22. thread_id = syscall(__NR_gettid);
  23. printf("Hello I am %d\n", thread_id);
  24. time_t t = time(NULL);
  25. tm* tm_i = localtime(&t);
  26. // if we never even created the shared file name template
  27. if (!file_template[0]) {
  28. sprintf(file_template, "%02d.%02d.%04d-%02d.%02d.%02d-%%02d-profiler.report",
  29. tm_i->tm_mday, tm_i->tm_mon+1, tm_i->tm_year+1900,
  30. tm_i->tm_hour, tm_i->tm_min, tm_i->tm_sec);
  31. }
  32. char file_name[38];
  33. sprintf(file_name, file_template, thread_id);
  34. out_file = fopen(file_name, "w");
  35. if (!out_file) {
  36. printf("could not open %s\n", file_name);
  37. }
  38. is_initialized = true;
  39. }
  40. fprintf(out_file, "-> %s %s %d\n", func, file, line);
  41. };
  42. ~Profiler() {
  43. call_depth -= 1;
  44. fprintf(out_file, "<-\n");
  45. if (call_depth == 0)
  46. fflush(out_file);
  47. };
  48. };
  49. # define profile_this Profiler profiler(__FILE__, __FUNCTION__, __LINE__)
  50. #else
  51. # define profile_this enum {}
  52. #endif