Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

71 righe
2.1 KiB

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