Trace.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Copyright (C) 2009-2016, 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 <cstdlib>
  7. #if ANKI_ENABLE_TRACE
  8. namespace anki
  9. {
  10. //==============================================================================
  11. // Misc =
  12. //==============================================================================
  13. static Array<const char*, U(TraceEventType::COUNT)> eventNames = {
  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. "RENDER",
  26. "RENDER_MS",
  27. "RENDER_IS",
  28. "RENDER_SM",
  29. "RENDER_IR",
  30. "RENDER_DRAWER",
  31. "RENDERER_COMMAND_BUFFER_BUILDING",
  32. "GL_THREAD",
  33. "GL_2ND_LEVEL_CMD_BUFFER",
  34. "GL_BIND_RESOURCES",
  35. "GL_BIND_PPLINE",
  36. "GL_CMD_BUFFER_DESTROY",
  37. "SWAP_BUFFERS",
  38. "BARRIER_WAIT"}};
  39. static Array<const char*, U(TraceCounterType::COUNT)> counterNames = {
  40. {"GR_DRAWCALLS",
  41. "GR_DYNAMIC_UNIFORMS_SIZE",
  42. "GR_DYNAMIC_STORAGE_SIZE",
  43. "GR_VERTICES",
  44. "GR_PIPELINES_CREATED",
  45. "GR_PIPELINE_BINDS_SKIPPED",
  46. "GR_PIPELINE_BINDS_HAPPENED",
  47. "RENDERER_LIGHTS",
  48. "RENDERER_SHADOW_PASSES",
  49. "RENDERER_MERGED_DRAWCALLS",
  50. "RENDERER_REFLECTIONS",
  51. "SCENE_NODES_UPDATED"}};
  52. #define ANKI_TRACE_FILE_ERROR() \
  53. if(err) \
  54. { \
  55. ANKI_LOGE("Error writing the trace file"); \
  56. }
  57. const U MAX_EVENTS_DEPTH = 20;
  58. thread_local HighRezTimer::Scalar g_traceEventStartTime[MAX_EVENTS_DEPTH];
  59. thread_local I g_traceEventsInFlight = 0;
  60. //==============================================================================
  61. // TraceManager =
  62. //==============================================================================
  63. //==============================================================================
  64. TraceManager::~TraceManager()
  65. {
  66. // No need to close the json (no need to add ']'). Chrome will take care
  67. // of that
  68. }
  69. //==============================================================================
  70. Error TraceManager::create(HeapAllocator<U8> alloc, const CString& cacheDir)
  71. {
  72. if(getenv("ANKI_DISABLE_TRACE")
  73. && CString(getenv("ANKI_DISABLE_TRACE")) == "1")
  74. {
  75. m_disabled = true;
  76. return ErrorCode::NONE;
  77. }
  78. // Create trace file
  79. StringAuto fname(alloc);
  80. fname.sprintf("%s/trace.json", &cacheDir[0]);
  81. ANKI_CHECK(m_traceFile.open(fname.toCString(), File::OpenFlag::WRITE));
  82. ANKI_CHECK(m_traceFile.writeText("["));
  83. // Create per frame file
  84. StringAuto perFrameFname(alloc);
  85. perFrameFname.sprintf("%s/per_frame.csv", &cacheDir[0]);
  86. ANKI_CHECK(
  87. m_perFrameFile.open(perFrameFname.toCString(), File::OpenFlag::WRITE));
  88. ANKI_CHECK(m_perFrameFile.writeText("FPS, "));
  89. for(U i = 0; i < U(TraceCounterType::COUNT); ++i)
  90. {
  91. ANKI_CHECK(m_perFrameFile.writeText("%s, ", counterNames[i]));
  92. }
  93. for(U i = 0; i < U(TraceEventType::COUNT); ++i)
  94. {
  95. const char* fmt = (i < U(TraceEventType::COUNT) - 1) ? "%s, " : "%s\n";
  96. ANKI_CHECK(m_perFrameFile.writeText(fmt, eventNames[i]));
  97. }
  98. return ErrorCode::NONE;
  99. }
  100. //==============================================================================
  101. void TraceManager::startEvent()
  102. {
  103. if(ANKI_UNLIKELY(m_disabled))
  104. {
  105. return;
  106. }
  107. I i = ++g_traceEventsInFlight;
  108. --i;
  109. ANKI_ASSERT(i >= 0 && i <= I(MAX_EVENTS_DEPTH));
  110. g_traceEventStartTime[i] = HighRezTimer::getCurrentTime();
  111. }
  112. //==============================================================================
  113. void TraceManager::stopEvent(TraceEventType type)
  114. {
  115. if(ANKI_UNLIKELY(m_disabled))
  116. {
  117. return;
  118. }
  119. ANKI_ASSERT(g_traceEventsInFlight > 0
  120. && g_traceEventsInFlight < I(MAX_EVENTS_DEPTH));
  121. I i = --g_traceEventsInFlight;
  122. ANKI_ASSERT(i >= 0 && i < I(MAX_EVENTS_DEPTH));
  123. auto startedTime = g_traceEventStartTime[i];
  124. U id = m_count.fetchAdd(1);
  125. if(id < BUFFERED_ENTRIES)
  126. {
  127. auto now = HighRezTimer::getCurrentTime();
  128. auto dur = now - startedTime;
  129. m_entries[id] =
  130. Entry{type, startedTime, dur, Thread::getCurrentThreadId()};
  131. m_perFrameCounters[U(TraceCounterType::COUNT) + U(type)].fetchAdd(
  132. U64(dur * 1000000000.0));
  133. }
  134. else
  135. {
  136. ANKI_LOGW("Increase the buffered trace entries");
  137. m_perFrameCounters[U(TraceCounterType::COUNT) + U(type)].fetchAdd(0);
  138. }
  139. }
  140. //==============================================================================
  141. Error TraceManager::flushCounters()
  142. {
  143. if(ANKI_UNLIKELY(m_disabled))
  144. {
  145. return ErrorCode::NONE;
  146. }
  147. // Write the FPS counter
  148. HighRezTimer::Scalar now = HighRezTimer::getCurrentTime();
  149. HighRezTimer::Scalar time = now - m_startFrameTime;
  150. F32 fps = 1.0 / time;
  151. ANKI_CHECK(m_traceFile.writeText(
  152. "{\"name\": \"FPS\", \"cat\": \"PERF\", \"ph\": \"C\", "
  153. "\"pid\": 1, \"ts\": %llu, \"args\": {\"val\": %f}},\n",
  154. U64(m_startFrameTime * 1000000.0),
  155. fps));
  156. ANKI_CHECK(m_perFrameFile.writeText("%f, ", fps));
  157. for(U i = 0; i < U(TraceCounterType::COUNT); ++i)
  158. {
  159. auto count = m_perFrameCounters[i].exchange(0);
  160. ANKI_CHECK(m_traceFile.writeText(
  161. "{\"name\": \"%s\", \"cat\": \"PERF\", \"ph\": \"C\", "
  162. "\"pid\": 1, \"ts\": %llu, \"args\": {\"val\": %llu}},\n",
  163. counterNames[i],
  164. U64(m_startFrameTime * 1000000.0),
  165. count));
  166. ANKI_CHECK(m_perFrameFile.writeText("%llu, ", count));
  167. }
  168. return ErrorCode::NONE;
  169. }
  170. //==============================================================================
  171. Error TraceManager::flushEvents()
  172. {
  173. if(ANKI_UNLIKELY(m_disabled))
  174. {
  175. return ErrorCode::NONE;
  176. }
  177. // Write the events
  178. U count = m_count.exchange(0);
  179. count = min<U>(count, BUFFERED_ENTRIES);
  180. for(U i = 0; i < count; ++i)
  181. {
  182. const Entry& e = m_entries[i];
  183. U64 startMicroSec = U64(e.m_timestamp * 1000000.0);
  184. U64 durMicroSec = U64(e.m_duration * 1000000.0);
  185. if(durMicroSec == 0)
  186. {
  187. continue;
  188. }
  189. ANKI_CHECK(m_traceFile.writeText(
  190. "{\"name\": \"%s\", \"cat\": \"PERF\", \"ph\": \"X\", "
  191. "\"pid\": 1, \"tid\": %llu, \"ts\": %llu, \"dur\": %llu},\n",
  192. eventNames[e.m_event],
  193. e.m_tid,
  194. startMicroSec,
  195. durMicroSec));
  196. }
  197. for(U i = 0; i < U(TraceEventType::COUNT); ++i)
  198. {
  199. const char* fmt = (i < U(TraceEventType::COUNT) - 1) ? "%f, " : "%f\n";
  200. U64 ns = m_perFrameCounters[i + U(TraceCounterType::COUNT)].exchange(0);
  201. ANKI_CHECK(m_perFrameFile.writeText(fmt, F64(ns) / 1000000.0));
  202. }
  203. return ErrorCode::NONE;
  204. }
  205. //==============================================================================
  206. void TraceManager::startFrame()
  207. {
  208. if(ANKI_UNLIKELY(m_disabled))
  209. {
  210. return;
  211. }
  212. m_startFrameTime = HighRezTimer::getCurrentTime();
  213. }
  214. //==============================================================================
  215. void TraceManager::stopFrame()
  216. {
  217. if(ANKI_UNLIKELY(m_disabled))
  218. {
  219. return;
  220. }
  221. Error err = flushCounters();
  222. if(!err)
  223. {
  224. err = flushEvents();
  225. }
  226. if(err)
  227. {
  228. ANKI_LOGE("Error writing the trace file");
  229. }
  230. }
  231. } // end namespace anki
  232. #endif