Tracer.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Copyright (C) 2009-present, 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/Util/Tracer.h>
  6. #include <AnKi/Util/HighRezTimer.h>
  7. #include <AnKi/Util/HashMap.h>
  8. #include <AnKi/Util/List.h>
  9. #if ANKI_OS_ANDROID
  10. # include <ThirdParty/StreamlineAnnotate/streamline_annotate.h>
  11. #endif
  12. #if ANKI_GR_BACKEND_DIRECT3D && ANKI_TRACING_ENABLED
  13. # if !defined(WIN32_LEAN_AND_MEAN)
  14. # define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers.
  15. # endif
  16. # include <windows.h>
  17. # define USE_PIX ANKI_TRACING_ENABLED
  18. # include <pix3.h>
  19. #endif
  20. namespace anki {
  21. #if ANKI_TRACING_ENABLED
  22. class Tracer::Chunk : public IntrusiveListEnabled<Chunk>
  23. {
  24. public:
  25. Array<TracerEvent, kEventsPerChunk> m_events;
  26. U32 m_eventCount = 0;
  27. Array<TracerCounter, kCountersPerChunk> m_counters;
  28. U32 m_counterCount = 0;
  29. };
  30. /// Thread local storage.
  31. class alignas(ANKI_CACHE_LINE_SIZE) Tracer::ThreadLocal
  32. {
  33. public:
  34. ThreadId m_tid = 0;
  35. Chunk* m_currentChunk = nullptr;
  36. IntrusiveList<Chunk> m_allChunks;
  37. SpinLock m_currentChunkLock;
  38. };
  39. thread_local Tracer::ThreadLocal* Tracer::m_threadLocal = nullptr;
  40. Tracer::Tracer()
  41. {
  42. setEnabled(false);
  43. # if ANKI_OS_ANDROID
  44. ANNOTATE_SETUP;
  45. setStreamlineEnabled(false);
  46. # endif
  47. }
  48. Tracer::~Tracer()
  49. {
  50. LockGuard<Mutex> lock(m_allThreadLocalMtx);
  51. for(ThreadLocal* tlocal : m_allThreadLocal)
  52. {
  53. deleteInstance(DefaultMemoryPool::getSingleton(), tlocal);
  54. }
  55. }
  56. Tracer::ThreadLocal& Tracer::getThreadLocal()
  57. {
  58. ThreadLocal* out = m_threadLocal;
  59. if(out == nullptr) [[unlikely]]
  60. {
  61. out = newInstance<ThreadLocal>(DefaultMemoryPool::getSingleton());
  62. out->m_tid = Thread::getCurrentThreadId();
  63. m_threadLocal = out;
  64. // Store it
  65. LockGuard<Mutex> lock(m_allThreadLocalMtx);
  66. m_allThreadLocal.emplaceBack(out);
  67. }
  68. return *out;
  69. }
  70. Tracer::Chunk& Tracer::getOrCreateChunk(ThreadLocal& tlocal)
  71. {
  72. Chunk* out;
  73. if(tlocal.m_currentChunk && tlocal.m_currentChunk->m_eventCount < kEventsPerChunk && tlocal.m_currentChunk->m_counterCount < kCountersPerChunk)
  74. {
  75. // There is a chunk and it has enough space
  76. out = tlocal.m_currentChunk;
  77. }
  78. else
  79. {
  80. // Create a new
  81. out = newInstance<Chunk>(DefaultMemoryPool::getSingleton());
  82. tlocal.m_currentChunk = out;
  83. tlocal.m_allChunks.pushBack(out);
  84. }
  85. return *out;
  86. }
  87. TracerEventHandle Tracer::beginEvent([[maybe_unused]] const char* eventName)
  88. {
  89. TracerEventHandle out;
  90. if(m_enabled)
  91. {
  92. out.m_start = HighRezTimer::getCurrentTime();
  93. # if ANKI_OS_ANDROID
  94. if(m_streamlineEnabled)
  95. {
  96. ANNOTATE_COLOR(ANNOTATE_RED, eventName);
  97. }
  98. # endif
  99. # if ANKI_GR_BACKEND_DIRECT3D
  100. if(m_pixEnabled)
  101. {
  102. const U32 green = PIX_COLOR(0, 255, 0);
  103. PIXBeginEvent(green, "%s", eventName);
  104. }
  105. # endif
  106. }
  107. else
  108. {
  109. out.m_start = 0.0;
  110. }
  111. return out;
  112. }
  113. void Tracer::endEvent(const char* eventName, TracerEventHandle event)
  114. {
  115. if(!m_enabled)
  116. {
  117. return;
  118. }
  119. # if ANKI_OS_ANDROID
  120. if(m_streamlineEnabled)
  121. {
  122. ANNOTATE_END();
  123. }
  124. # endif
  125. # if ANKI_GR_BACKEND_DIRECT3D
  126. if(m_pixEnabled)
  127. {
  128. PIXEndEvent();
  129. }
  130. # endif
  131. if(event.m_start == 0.0)
  132. {
  133. return;
  134. }
  135. // Get the time before the lock and everything
  136. const Second duration = HighRezTimer::getCurrentTime() - event.m_start;
  137. if(duration == 0.0)
  138. {
  139. return;
  140. }
  141. ThreadLocal& tlocal = getThreadLocal();
  142. // Write the event
  143. LockGuard<SpinLock> lock(tlocal.m_currentChunkLock);
  144. Chunk& chunk = getOrCreateChunk(tlocal);
  145. TracerEvent& writeEvent = chunk.m_events[chunk.m_eventCount++];
  146. writeEvent.m_name = eventName;
  147. writeEvent.m_start = event.m_start;
  148. writeEvent.m_duration = duration;
  149. // Write counter as well. In ns
  150. TracerCounter& writeCounter = chunk.m_counters[chunk.m_counterCount++];
  151. writeCounter.m_name = eventName;
  152. writeCounter.m_value = U64(writeEvent.m_duration * 1000000000.0);
  153. }
  154. void Tracer::addCustomEvent(const char* eventName, Second start, Second duration)
  155. {
  156. ANKI_ASSERT(eventName && start >= 0.0 && duration >= 0.0);
  157. if(!m_enabled || duration == 0.0)
  158. {
  159. return;
  160. }
  161. ThreadLocal& tlocal = getThreadLocal();
  162. // Write the event
  163. LockGuard<SpinLock> lock(tlocal.m_currentChunkLock);
  164. Chunk& chunk = getOrCreateChunk(tlocal);
  165. TracerEvent& writeEvent = chunk.m_events[chunk.m_eventCount++];
  166. writeEvent.m_name = eventName;
  167. writeEvent.m_start = start;
  168. writeEvent.m_duration = duration;
  169. // Write counter as well. In ns
  170. TracerCounter& writeCounter = chunk.m_counters[chunk.m_counterCount++];
  171. writeCounter.m_name = eventName;
  172. writeCounter.m_value = U64(duration * 1000000000.0);
  173. }
  174. void Tracer::incrementCounter(const char* counterName, U64 value)
  175. {
  176. if(!m_enabled)
  177. {
  178. return;
  179. }
  180. ThreadLocal& tlocal = getThreadLocal();
  181. LockGuard<SpinLock> lock(tlocal.m_currentChunkLock);
  182. Chunk& chunk = getOrCreateChunk(tlocal);
  183. TracerCounter& writeTo = chunk.m_counters[chunk.m_counterCount++];
  184. writeTo.m_name = counterName;
  185. writeTo.m_value = value;
  186. }
  187. void Tracer::flush(TracerFlushCallback callback, void* callbackUserData)
  188. {
  189. ANKI_ASSERT(callback);
  190. LockGuard<Mutex> lock(m_allThreadLocalMtx);
  191. for(ThreadLocal* tlocal : m_allThreadLocal)
  192. {
  193. LockGuard<SpinLock> lock2(tlocal->m_currentChunkLock);
  194. while(!tlocal->m_allChunks.isEmpty())
  195. {
  196. Chunk* chunk = tlocal->m_allChunks.popFront();
  197. callback(callbackUserData, tlocal->m_tid, WeakArray<TracerEvent>(&chunk->m_events[0], chunk->m_eventCount),
  198. WeakArray<TracerCounter>(&chunk->m_counters[0], chunk->m_counterCount));
  199. deleteInstance(DefaultMemoryPool::getSingleton(), chunk);
  200. }
  201. tlocal->m_currentChunk = nullptr;
  202. }
  203. }
  204. #endif
  205. } // end namespace anki