Trace.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // Copyright (C) 2009-2018, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <anki/core/Trace.h>
  6. #include <anki/util/HighRezTimer.h>
  7. #include <cstdlib>
  8. #if ANKI_ENABLE_TRACE
  9. namespace anki
  10. {
  11. static Array<const char*, U(TraceEventType::COUNT)> eventNames = {{"RESOURCE_ALLOCATE_TRANSFER",
  12. "RESOURCE_ASYNC_TASK",
  13. "RESOURCE_FILE_READ",
  14. "SCENE_UPDATE",
  15. "SCENE_DELETE_STUFF",
  16. "SCENE_PHYSICS_UPDATE",
  17. "SCENE_NODES_UPDATE",
  18. "SCENE_VISIBILITY_TESTS",
  19. "VIS_TEST",
  20. "VIS_COMBINE_RESULTS",
  21. "VIS_ITERATE_SECTORS",
  22. "VIS_GATHER_TRIANGLES",
  23. "VIS_RASTERIZE",
  24. "VIS_RASTERIZER_TEST",
  25. "RENDERER_INIT",
  26. "RENDER",
  27. "RENDER_MS",
  28. "RENDER_IS",
  29. "RENDER_SM",
  30. "RENDER_IR",
  31. "RENDER_DRAWER",
  32. "RENDERER_COMMAND_BUFFER_BUILDING",
  33. "RENDERER_LIGHT_BINNING",
  34. "GR_RENDER_GRAPH",
  35. "GR_COMMAND_BUFFER_RESET",
  36. "GR_SHADER_COMPILE",
  37. "GL_THREAD",
  38. "GL_2ND_LEVEL_CMD_BUFFER",
  39. "GL_BIND_RESOURCES",
  40. "GL_BIND_PPLINE",
  41. "GL_CMD_BUFFER_DESTROY",
  42. "VK_ACQUIRE_IMAGE",
  43. "VK_QUEUE_SUBMIT",
  44. "VK_PIPELINE_CREATE",
  45. "VK_BIND_OBJECT",
  46. "VK_DESCRIPTOR_SET_GET_OR_CREATE",
  47. "SWAP_BUFFERS",
  48. "BARRIER_WAIT",
  49. "LUA_EXEC",
  50. "TIMER_TICK_SLEEP"}};
  51. static Array<const char*, U(TraceCounterType::COUNT)> counterNames = {{"GR_DRAWCALLS",
  52. "GR_VERTICES",
  53. "GL_PROGS_SKIPPED",
  54. "VK_PIPELINE_BARRIERS",
  55. "VK_CMD_BUFFER_CREATE",
  56. "VK_FENCE_CREATE",
  57. "VK_SEMAPHORE_CREATE",
  58. "VK_DESCRIPTOR_POOL_CREATE",
  59. "VK_DESCRIPTOR_SET_CREATE",
  60. "VK_PIPELINE_CREATE",
  61. "RENDERER_LIGHTS",
  62. "RENDERER_SHADOW_PASSES",
  63. "RENDERER_MERGED_DRAWCALLS",
  64. "RENDERER_REFLECTIONS",
  65. "RESOURCE_ASYNC_TASKS",
  66. "SCENE_NODES_UPDATED",
  67. "STAGING_UNIFORMS_SIZE",
  68. "STAGING_STORAGE_SIZE"}};
  69. # define ANKI_TRACE_FILE_ERROR() \
  70. if(err) \
  71. { \
  72. ANKI_CORE_LOGE("Error writing the trace file"); \
  73. }
  74. const U MAX_EVENTS_DEPTH = 20;
  75. thread_local Second g_traceEventStartTime[MAX_EVENTS_DEPTH];
  76. thread_local I g_traceEventsInFlight = 0;
  77. TraceManager::~TraceManager()
  78. {
  79. // No need to close the json (no need to add ']'). Chrome will take care of that
  80. }
  81. Error TraceManager::create(HeapAllocator<U8> alloc, const CString& cacheDir)
  82. {
  83. if(getenv("ANKI_DISABLE_TRACE") && CString(getenv("ANKI_DISABLE_TRACE")) == "1")
  84. {
  85. m_disabled = true;
  86. return Error::NONE;
  87. }
  88. memset(&m_perFrameCounters[0], 0, sizeof(m_perFrameCounters));
  89. memset(&m_perRunCounters[0], 0, sizeof(m_perRunCounters));
  90. // Create trace file
  91. StringAuto fname(alloc);
  92. fname.sprintf("%s/trace.json", &cacheDir[0]);
  93. ANKI_CHECK(m_traceFile.open(fname.toCString(), FileOpenFlag::WRITE));
  94. ANKI_CHECK(m_traceFile.writeText("["));
  95. // Create per frame file
  96. StringAuto perFrameFname(alloc);
  97. perFrameFname.sprintf("%s/per_frame.csv", &cacheDir[0]);
  98. ANKI_CHECK(m_perFrameFile.open(perFrameFname.toCString(), FileOpenFlag::WRITE));
  99. ANKI_CHECK(m_perFrameFile.writeText("FPS, "));
  100. for(U i = 0; i < U(TraceCounterType::COUNT); ++i)
  101. {
  102. ANKI_CHECK(m_perFrameFile.writeText("%s, ", counterNames[i]));
  103. }
  104. for(U i = 0; i < U(TraceEventType::COUNT); ++i)
  105. {
  106. const char* fmt = (i < U(TraceEventType::COUNT) - 1) ? "%s, " : "%s\n";
  107. ANKI_CHECK(m_perFrameFile.writeText(fmt, eventNames[i]));
  108. }
  109. return Error::NONE;
  110. }
  111. void TraceManager::startEvent()
  112. {
  113. if(ANKI_UNLIKELY(m_disabled))
  114. {
  115. return;
  116. }
  117. I i = ++g_traceEventsInFlight;
  118. --i;
  119. ANKI_ASSERT(i >= 0 && i <= I(MAX_EVENTS_DEPTH));
  120. g_traceEventStartTime[i] = HighRezTimer::getCurrentTime();
  121. }
  122. void TraceManager::stopEvent(TraceEventType type)
  123. {
  124. if(ANKI_UNLIKELY(m_disabled))
  125. {
  126. return;
  127. }
  128. ANKI_ASSERT(g_traceEventsInFlight > 0 && g_traceEventsInFlight < I(MAX_EVENTS_DEPTH));
  129. I i = --g_traceEventsInFlight;
  130. ANKI_ASSERT(i >= 0 && i < I(MAX_EVENTS_DEPTH));
  131. auto startedTime = g_traceEventStartTime[i];
  132. U id = m_count.fetchAdd(1);
  133. if(id < BUFFERED_ENTRIES)
  134. {
  135. auto now = HighRezTimer::getCurrentTime();
  136. auto dur = now - startedTime;
  137. m_entries[id] = Entry{type, startedTime, dur, Thread::getCurrentThreadId()};
  138. m_perFrameCounters[U(TraceCounterType::COUNT) + U(type)].fetchAdd(U64(dur * 1000000000.0));
  139. }
  140. else
  141. {
  142. ANKI_CORE_LOGW("Increase the buffered trace entries");
  143. m_perFrameCounters[U(TraceCounterType::COUNT) + U(type)].fetchAdd(0);
  144. }
  145. }
  146. Error TraceManager::flushCounters()
  147. {
  148. if(ANKI_UNLIKELY(m_disabled))
  149. {
  150. return Error::NONE;
  151. }
  152. // Write the FPS counter
  153. Second now = HighRezTimer::getCurrentTime();
  154. Second time = now - m_startFrameTime;
  155. F32 fps = 1.0 / time;
  156. ANKI_CHECK(m_traceFile.writeText("{\"name\": \"FPS\", \"cat\": \"PERF\", \"ph\": \"C\", "
  157. "\"pid\": 1, \"ts\": %llu, \"args\": {\"val\": %f}},\n",
  158. U64(m_startFrameTime * 1000000.0),
  159. fps));
  160. ANKI_CHECK(m_perFrameFile.writeText("%f, ", fps));
  161. for(U i = 0; i < U(TraceCounterType::COUNT); ++i)
  162. {
  163. auto count = m_perFrameCounters[i].exchange(0);
  164. ANKI_CHECK(m_traceFile.writeText("{\"name\": \"%s\", \"cat\": \"PERF\", \"ph\": \"C\", "
  165. "\"pid\": 1, \"ts\": %llu, \"args\": {\"val\": %llu}},\n",
  166. counterNames[i],
  167. U64(m_startFrameTime * 1000000.0),
  168. count));
  169. ANKI_CHECK(m_perFrameFile.writeText("%llu, ", count));
  170. }
  171. return Error::NONE;
  172. }
  173. Error TraceManager::flushEvents()
  174. {
  175. if(ANKI_UNLIKELY(m_disabled))
  176. {
  177. return Error::NONE;
  178. }
  179. // Write the events
  180. U count = m_count.exchange(0);
  181. count = min<U>(count, BUFFERED_ENTRIES);
  182. for(U i = 0; i < count; ++i)
  183. {
  184. const Entry& e = m_entries[i];
  185. U64 startMicroSec = U64(e.m_timestamp * 1000000.0);
  186. U64 durMicroSec = U64(e.m_duration * 1000000.0);
  187. if(durMicroSec == 0)
  188. {
  189. continue;
  190. }
  191. ANKI_CHECK(m_traceFile.writeText("{\"name\": \"%s\", \"cat\": \"PERF\", \"ph\": \"X\", "
  192. "\"pid\": 1, \"tid\": %llu, \"ts\": %llu, \"dur\": %llu},\n",
  193. eventNames[e.m_event],
  194. e.m_tid,
  195. startMicroSec,
  196. durMicroSec));
  197. }
  198. for(U i = 0; i < U(TraceEventType::COUNT); ++i)
  199. {
  200. const char* fmt = (i < U(TraceEventType::COUNT) - 1) ? "%f, " : "%f\n";
  201. U64 ns = m_perFrameCounters[i + U(TraceCounterType::COUNT)].exchange(0);
  202. ANKI_CHECK(m_perFrameFile.writeText(fmt, F64(ns) / 1000000.0)); // Time in ms
  203. }
  204. return Error::NONE;
  205. }
  206. void TraceManager::startFrame()
  207. {
  208. if(ANKI_UNLIKELY(m_disabled))
  209. {
  210. return;
  211. }
  212. m_startFrameTime = HighRezTimer::getCurrentTime();
  213. }
  214. void TraceManager::stopFrame()
  215. {
  216. if(ANKI_UNLIKELY(m_disabled))
  217. {
  218. return;
  219. }
  220. Error err = flushCounters();
  221. if(!err)
  222. {
  223. err = flushEvents();
  224. }
  225. if(err)
  226. {
  227. ANKI_CORE_LOGE("Error writing the trace file");
  228. }
  229. }
  230. } // end namespace anki
  231. #endif