Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

88 linhas
2.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* func, const int line, char* custom_name) {
  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. is_initialized = true;
  54. }
  55. QueryPerformanceCounter(&tmp_time);
  56. fprintf(out_file, "->,%lld,%s,%s,%d\n",
  57. tmp_time.QuadPart,
  58. (custom_name ?
  59. custom_name :
  60. func), file, line);
  61. };
  62. ~Profiler() {
  63. call_depth -= 1;
  64. QueryPerformanceCounter(&tmp_time);
  65. fprintf(out_file, "<-,%lld,,,\n", tmp_time.QuadPart);
  66. if (call_depth == 0)
  67. fflush(out_file);
  68. };
  69. };
  70. # define profile_this() Profiler profiler(__FILE__, __FUNCTION__, __LINE__, 0)
  71. # define profile_with_name(name) Profiler profiler(__FILE__, __FUNCTION__, __LINE__, name)
  72. #else
  73. # define profile_this() do {} while(0)
  74. # define profile_with_name() do {} while(0)
  75. #endif