ChromeTraceUtil.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include "ChromeTraceUtil.h"
  2. #include "b3Clock.h"
  3. #include "LinearMath/btQuickprof.h"
  4. #include "LinearMath/btAlignedObjectArray.h"
  5. #include "Bullet3Common/b3Logging.h"
  6. #include <stdio.h>
  7. #include <climits>
  8. struct btTiming
  9. {
  10. const char* m_name;
  11. int m_threadId;
  12. unsigned long long int m_usStartTime;
  13. unsigned long long int m_usEndTime;
  14. };
  15. FILE* gTimingFile = 0;
  16. #ifndef __STDC_FORMAT_MACROS
  17. #define __STDC_FORMAT_MACROS
  18. #endif //__STDC_FORMAT_MACROS
  19. //see http://stackoverflow.com/questions/18107426/printf-format-for-unsigned-int64-on-windows
  20. #ifndef _WIN32
  21. #include <inttypes.h>
  22. #endif
  23. #define BT_TIMING_CAPACITY 16 * 65536
  24. static bool m_firstTiming = true;
  25. struct btTimings
  26. {
  27. btTimings()
  28. : m_numTimings(0),
  29. m_activeBuffer(0)
  30. {
  31. }
  32. void flush()
  33. {
  34. for (int i = 0; i < m_numTimings; i++)
  35. {
  36. const char* name = m_timings[m_activeBuffer][i].m_name;
  37. int threadId = m_timings[m_activeBuffer][i].m_threadId;
  38. unsigned long long int startTime = m_timings[m_activeBuffer][i].m_usStartTime;
  39. unsigned long long int endTime = m_timings[m_activeBuffer][i].m_usEndTime;
  40. if (!m_firstTiming)
  41. {
  42. fprintf(gTimingFile, ",\n");
  43. }
  44. m_firstTiming = false;
  45. if (startTime > endTime)
  46. {
  47. endTime = startTime;
  48. }
  49. unsigned long long int startTimeDiv1000 = startTime / 1000;
  50. unsigned long long int endTimeDiv1000 = endTime / 1000;
  51. #if 0
  52. fprintf(gTimingFile, "{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%" PRIu64 ".123 ,\"ph\":\"B\",\"name\":\"%s\",\"args\":{}},\n",
  53. threadId, startTimeDiv1000, name);
  54. fprintf(gTimingFile, "{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%" PRIu64 ".234 ,\"ph\":\"E\",\"name\":\"%s\",\"args\":{}}",
  55. threadId, endTimeDiv1000, name);
  56. #else
  57. unsigned int startTimeRem1000 = startTime % 1000;
  58. unsigned int endTimeRem1000 = endTime % 1000;
  59. char startTimeRem1000Str[16];
  60. char endTimeRem1000Str[16];
  61. if (startTimeRem1000 < 10)
  62. {
  63. sprintf(startTimeRem1000Str, "00%d", startTimeRem1000);
  64. }
  65. else
  66. {
  67. if (startTimeRem1000 < 100)
  68. {
  69. sprintf(startTimeRem1000Str, "0%d", startTimeRem1000);
  70. }
  71. else
  72. {
  73. sprintf(startTimeRem1000Str, "%d", startTimeRem1000);
  74. }
  75. }
  76. if (endTimeRem1000 < 10)
  77. {
  78. sprintf(endTimeRem1000Str, "00%d", endTimeRem1000);
  79. }
  80. else
  81. {
  82. if (endTimeRem1000 < 100)
  83. {
  84. sprintf(endTimeRem1000Str, "0%d", endTimeRem1000);
  85. }
  86. else
  87. {
  88. sprintf(endTimeRem1000Str, "%d", endTimeRem1000);
  89. }
  90. }
  91. char newname[1024];
  92. static int counter2 = 0;
  93. sprintf(newname, "%s%d", name, counter2++);
  94. #ifdef _WIN32
  95. fprintf(gTimingFile, "{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%I64d.%s ,\"ph\":\"B\",\"name\":\"%s\",\"args\":{}},\n",
  96. threadId, startTimeDiv1000, startTimeRem1000Str, newname);
  97. fprintf(gTimingFile, "{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%I64d.%s ,\"ph\":\"E\",\"name\":\"%s\",\"args\":{}}",
  98. threadId, endTimeDiv1000, endTimeRem1000Str, newname);
  99. #else
  100. // Note: on 64b build, PRIu64 resolves in 'lu' whereas timings ('ts') have to be printed as 'llu'.
  101. fprintf(gTimingFile, "{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%llu.%s ,\"ph\":\"B\",\"name\":\"%s\",\"args\":{}},\n",
  102. threadId, startTimeDiv1000, startTimeRem1000Str, newname);
  103. fprintf(gTimingFile, "{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%llu.%s ,\"ph\":\"E\",\"name\":\"%s\",\"args\":{}}",
  104. threadId, endTimeDiv1000, endTimeRem1000Str, newname);
  105. #endif
  106. #endif
  107. }
  108. m_numTimings = 0;
  109. }
  110. void addTiming(const char* name, int threadId, unsigned long long int startTime, unsigned long long int endTime)
  111. {
  112. if (m_numTimings >= BT_TIMING_CAPACITY)
  113. {
  114. return;
  115. }
  116. if (m_timings[0].size() == 0)
  117. {
  118. m_timings[0].resize(BT_TIMING_CAPACITY);
  119. }
  120. int slot = m_numTimings++;
  121. m_timings[m_activeBuffer][slot].m_name = name;
  122. m_timings[m_activeBuffer][slot].m_threadId = threadId;
  123. m_timings[m_activeBuffer][slot].m_usStartTime = startTime;
  124. m_timings[m_activeBuffer][slot].m_usEndTime = endTime;
  125. }
  126. int m_numTimings;
  127. int m_activeBuffer;
  128. btAlignedObjectArray<btTiming> m_timings[1];
  129. };
  130. //#ifndef BT_NO_PROFILE
  131. btTimings gTimings[BT_QUICKPROF_MAX_THREAD_COUNT];
  132. #define MAX_NESTING 1024
  133. int gStackDepths[BT_QUICKPROF_MAX_THREAD_COUNT] = {0};
  134. const char* gFuncNames[BT_QUICKPROF_MAX_THREAD_COUNT][MAX_NESTING];
  135. unsigned long long int gStartTimes[BT_QUICKPROF_MAX_THREAD_COUNT][MAX_NESTING];
  136. //#endif
  137. btClock clk;
  138. bool gProfileDisabled = true;
  139. void MyDummyEnterProfileZoneFunc(const char* msg)
  140. {
  141. }
  142. void MyDummyLeaveProfileZoneFunc()
  143. {
  144. }
  145. void MyEnterProfileZoneFunc(const char* msg)
  146. {
  147. if (gProfileDisabled)
  148. return;
  149. int threadId = btQuickprofGetCurrentThreadIndex2();
  150. if (threadId < 0 || threadId >= BT_QUICKPROF_MAX_THREAD_COUNT)
  151. return;
  152. if (gStackDepths[threadId] >= MAX_NESTING)
  153. {
  154. btAssert(0);
  155. return;
  156. }
  157. gFuncNames[threadId][gStackDepths[threadId]] = msg;
  158. gStartTimes[threadId][gStackDepths[threadId]] = clk.getTimeNanoseconds();
  159. if (gStartTimes[threadId][gStackDepths[threadId]] <= gStartTimes[threadId][gStackDepths[threadId] - 1])
  160. {
  161. gStartTimes[threadId][gStackDepths[threadId]] = 1 + gStartTimes[threadId][gStackDepths[threadId] - 1];
  162. }
  163. gStackDepths[threadId]++;
  164. }
  165. void MyLeaveProfileZoneFunc()
  166. {
  167. if (gProfileDisabled)
  168. return;
  169. int threadId = btQuickprofGetCurrentThreadIndex2();
  170. if (threadId < 0 || threadId >= BT_QUICKPROF_MAX_THREAD_COUNT)
  171. return;
  172. if (gStackDepths[threadId] <= 0)
  173. {
  174. return;
  175. }
  176. gStackDepths[threadId]--;
  177. const char* name = gFuncNames[threadId][gStackDepths[threadId]];
  178. unsigned long long int startTime = gStartTimes[threadId][gStackDepths[threadId]];
  179. unsigned long long int endTime = clk.getTimeNanoseconds();
  180. gTimings[threadId].addTiming(name, threadId, startTime, endTime);
  181. }
  182. void b3ChromeUtilsStartTimings()
  183. {
  184. m_firstTiming = true;
  185. gProfileDisabled = false; //true;
  186. b3SetCustomEnterProfileZoneFunc(MyEnterProfileZoneFunc);
  187. b3SetCustomLeaveProfileZoneFunc(MyLeaveProfileZoneFunc);
  188. //also for Bullet 2.x API
  189. btSetCustomEnterProfileZoneFunc(MyEnterProfileZoneFunc);
  190. btSetCustomLeaveProfileZoneFunc(MyLeaveProfileZoneFunc);
  191. }
  192. void b3ChromeUtilsStopTimingsAndWriteJsonFile(const char* fileNamePrefix)
  193. {
  194. b3SetCustomEnterProfileZoneFunc(MyDummyEnterProfileZoneFunc);
  195. b3SetCustomLeaveProfileZoneFunc(MyDummyLeaveProfileZoneFunc);
  196. //also for Bullet 2.x API
  197. btSetCustomEnterProfileZoneFunc(MyDummyEnterProfileZoneFunc);
  198. btSetCustomLeaveProfileZoneFunc(MyDummyLeaveProfileZoneFunc);
  199. char fileName[1024];
  200. static int fileCounter = 0;
  201. sprintf(fileName, "%s_%d.json", fileNamePrefix, fileCounter++);
  202. gTimingFile = fopen(fileName, "w");
  203. if (gTimingFile)
  204. {
  205. fprintf(gTimingFile, "{\"traceEvents\":[\n");
  206. //dump the content to file
  207. for (int i = 0; i < BT_QUICKPROF_MAX_THREAD_COUNT; i++)
  208. {
  209. if (gTimings[i].m_numTimings)
  210. {
  211. printf("Writing %d timings for thread %d\n", gTimings[i].m_numTimings, i);
  212. gTimings[i].flush();
  213. }
  214. }
  215. fprintf(gTimingFile, "\n],\n\"displayTimeUnit\": \"ns\"}");
  216. fclose(gTimingFile);
  217. }
  218. else
  219. {
  220. b3Printf("Error opening file");
  221. b3Printf(fileName);
  222. }
  223. gTimingFile = 0;
  224. }
  225. void b3ChromeUtilsEnableProfiling()
  226. {
  227. gProfileDisabled = false;
  228. }