您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

100 行
3.6 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. #ifdef _MSC_VER
  12. // if windows
  13. # include <Windows.h>
  14. #else
  15. # include <sys/time.h>
  16. #endif
  17. struct Profiler {
  18. LARGE_INTEGER tmp_time;
  19. // same for all threads
  20. inline static char file_template[40] = "\0";
  21. // thread local
  22. inline thread_local static bool is_initialized = false;
  23. inline thread_local static size_t thread_id = -1;
  24. inline thread_local static int call_depth = 0;
  25. inline thread_local static FILE* out_file = nullptr;
  26. Profiler(const char* file, const char* name, const int line, const char* comment1, const char* comment2) {
  27. call_depth += 1;
  28. // if we never used this thread before
  29. if (!is_initialized) {
  30. thread_id = (size_t)&thread_id;
  31. time_t t = time(NULL);
  32. tm* tm_i = localtime(&t);
  33. // create folder
  34. #ifdef _MSC_VER
  35. _mkdir("./profiler_reports/");
  36. #else
  37. mkdir("./profiler_reports/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  38. #endif
  39. // if we never even created the shared file name template
  40. if (!file_template[0]) {
  41. // make sure folder exists
  42. sprintf(file_template, "./profiler_reports/%02d.%02d.%04d-%02d.%02d.%02d-%%zd-profiler.report",
  43. tm_i->tm_mday, tm_i->tm_mon+1, tm_i->tm_year+1900,
  44. tm_i->tm_hour, tm_i->tm_min, tm_i->tm_sec);
  45. }
  46. char file_name[100];
  47. sprintf(file_name, file_template, thread_id);
  48. // printf("Hello I am %zd\n", thread_id);
  49. out_file = fopen(file_name, "w");
  50. if (!out_file) {
  51. printf("could not open %s\n", file_name);
  52. }
  53. // initially write the performance frequency
  54. LARGE_INTEGER pf;
  55. QueryPerformanceFrequency(&pf);
  56. fprintf(out_file, "%lld,,,,\n", pf.QuadPart);
  57. is_initialized = true;
  58. }
  59. QueryPerformanceCounter(&tmp_time);
  60. fprintf(out_file, "->,%lld,%s,%s,%d,%s,%s\n",
  61. tmp_time.QuadPart, name, file,
  62. line, comment1, comment2);
  63. };
  64. ~Profiler() {
  65. call_depth -= 1;
  66. QueryPerformanceCounter(&tmp_time);
  67. fprintf(out_file, "<-,%lld,,,,,\n", tmp_time.QuadPart);
  68. if (call_depth == 0)
  69. fflush(out_file);
  70. };
  71. };
  72. # define profile_this() Profiler profiler(__FILE__, __FUNCTION__, __LINE__, "", "")
  73. # define profile_with_name(name) Profiler profiler(__FILE__, name, __LINE__, "", "")
  74. # define profile_with_comment(c1) Profiler profiler(__FILE__, __FUNCTION__, __LINE__, c1, "")
  75. # define profile_with_comments(c1,c2) Profiler profiler(__FILE__, __FUNCTION__, __LINE__, c1, c2)
  76. # define profile_with_name_and_comment(name,c1) Profiler profiler(__FILE__, name, __LINE__, c1, "")
  77. # define profile_with_name_and_comments(name,c1,c2) Profiler profiler(__FILE__, name, __LINE__, c1, c2)
  78. #else
  79. # define profile_this() do {} while(0)
  80. # define profile_with_name(name) do {} while(0)
  81. # define profile_with_comment(c1) do {} while(0)
  82. # define profile_with_comments(c1,c2) do {} while(0)
  83. # define profile_with_name_and_comment(name,c1) do {} while(0)
  84. # define profile_with_name_and_comments(name,c1,c2) do {} while(0)
  85. #endif