StatsSet.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. #pragma once
  6. #include <AnKi/Core/Common.h>
  7. #include <AnKi/Util/Singleton.h>
  8. #include <AnKi/Util/Enum.h>
  9. #include <AnKi/Util/Thread.h>
  10. #include <AnKi/Util/String.h>
  11. namespace anki {
  12. /// @addtogroup core
  13. /// @{
  14. /// Define a global stat variable.
  15. #define ANKI_SVAR(name, category, descr, ...) inline StatCounter g_svar##name(category, descr, StatFlag::kNone | __VA_ARGS__);
  16. enum class StatFlag : U16
  17. {
  18. kNone = 0,
  19. kZeroEveryFrame = 1 << 0,
  20. kMainThreadUpdates = 1 << 1, ///< Can only be updated from the main thread.
  21. kFloat = 1 << 2,
  22. kShowAverage = 1 << 3,
  23. kSecond = (1 << 4) | kFloat,
  24. kMilisecond = (1 << 5) | kFloat,
  25. kNanoSeconds = 1 << 6,
  26. kBytes = 1 << 7,
  27. };
  28. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(StatFlag)
  29. enum class StatCategory : U8
  30. {
  31. kTime,
  32. kCpuMem,
  33. kGpuMem,
  34. kGpuMisc,
  35. kRenderer,
  36. kGr,
  37. kScene,
  38. kMisc,
  39. kCount,
  40. };
  41. inline constexpr Array<CString, U32(StatCategory::kCount)> kStatCategoryTexts = {"Time", "CPU memory", "GPU memory", "GPU misc",
  42. "Renderer", "GFX API", "Scene", "Misc"};
  43. /// A stats counter.
  44. class StatCounter
  45. {
  46. friend class StatsSet;
  47. public:
  48. /// Construct.
  49. /// @param name Name of the counter. The object will share ownership of the pointer.
  50. StatCounter(StatCategory category, const Char* name, StatFlag flags = StatFlag::kNone);
  51. template<std::integral T>
  52. U64 increment(T value)
  53. {
  54. #if ANKI_STATS_ENABLED
  55. ANKI_ASSERT(!(m_flags & StatFlag::kFloat));
  56. checkThread();
  57. U64 orig;
  58. if(!!(m_flags & StatFlag::kMainThreadUpdates))
  59. {
  60. orig = m_u;
  61. m_u += value;
  62. }
  63. else
  64. {
  65. orig = m_atomic.fetchAdd(value);
  66. }
  67. return orig;
  68. #else
  69. (void)value;
  70. return 0;
  71. #endif
  72. }
  73. template<std::floating_point T>
  74. F64 increment(T value)
  75. {
  76. #if ANKI_STATS_ENABLED
  77. ANKI_ASSERT(!!(m_flags & StatFlag::kFloat));
  78. checkThread();
  79. F64 orig;
  80. if(!!(m_flags & StatFlag::kMainThreadUpdates))
  81. {
  82. orig = m_f;
  83. m_f += value;
  84. }
  85. else
  86. {
  87. LockGuard lock(m_floatLock);
  88. orig = m_f;
  89. m_f += value;
  90. }
  91. return orig;
  92. #else
  93. (void)value;
  94. return 0.0;
  95. #endif
  96. }
  97. template<std::integral T>
  98. U64 decrement(T value)
  99. {
  100. #if ANKI_STATS_ENABLED
  101. ANKI_ASSERT(!(m_flags & StatFlag::kFloat));
  102. checkThread();
  103. U64 orig;
  104. if(!!(m_flags & StatFlag::kMainThreadUpdates))
  105. {
  106. orig = m_u;
  107. m_u -= value;
  108. }
  109. else
  110. {
  111. orig = m_atomic.fetchSub(value);
  112. }
  113. ANKI_ASSERT(orig >= value);
  114. return orig;
  115. #else
  116. (void)value;
  117. return 0;
  118. #endif
  119. }
  120. template<std::integral T>
  121. U64 set(T value)
  122. {
  123. #if ANKI_STATS_ENABLED
  124. ANKI_ASSERT(!(m_flags & StatFlag::kFloat));
  125. checkThread();
  126. U64 orig;
  127. if(!!(m_flags & StatFlag::kMainThreadUpdates))
  128. {
  129. orig = m_u;
  130. m_u = value;
  131. }
  132. else
  133. {
  134. orig = m_atomic.exchange(value);
  135. }
  136. return orig;
  137. #else
  138. (void)value;
  139. return 0;
  140. #endif
  141. }
  142. template<std::floating_point T>
  143. F64 set(T value)
  144. {
  145. #if ANKI_STATS_ENABLED
  146. ANKI_ASSERT(!!(m_flags & StatFlag::kFloat));
  147. checkThread();
  148. F64 orig;
  149. if(!!(m_flags & StatFlag::kMainThreadUpdates))
  150. {
  151. orig = m_f;
  152. m_f = value;
  153. }
  154. else
  155. {
  156. LockGuard lock(m_floatLock);
  157. orig = m_f;
  158. m_f = value;
  159. }
  160. return orig;
  161. #else
  162. (void)value;
  163. return 0.0;
  164. #endif
  165. }
  166. template<std::integral T>
  167. U64 max(T value)
  168. {
  169. #if ANKI_STATS_ENABLED
  170. ANKI_ASSERT(!(m_flags & StatFlag::kFloat));
  171. checkThread();
  172. U64 orig;
  173. if(!!(m_flags & StatFlag::kMainThreadUpdates))
  174. {
  175. orig = m_u;
  176. m_u = value;
  177. }
  178. else
  179. {
  180. orig = m_atomic.max(value);
  181. }
  182. return orig;
  183. #else
  184. (void)value;
  185. return 0;
  186. #endif
  187. }
  188. template<std::floating_point T>
  189. F64 max([[maybe_unused]] T value)
  190. {
  191. #if ANKI_STATS_ENABLED
  192. ANKI_ASSERT("Not supported for floats");
  193. return 0.0;
  194. #else
  195. (void)value;
  196. return 0.0;
  197. #endif
  198. }
  199. template<std::integral T>
  200. U64 getValue() const
  201. {
  202. #if ANKI_STATS_ENABLED
  203. ANKI_ASSERT(!(m_flags & StatFlag::kFloat));
  204. checkThread();
  205. return !!(m_flags & StatFlag::kMainThreadUpdates) ? m_u : m_atomic.load();
  206. #else
  207. return 0;
  208. #endif
  209. }
  210. template<std::floating_point T>
  211. F64 getValue() const
  212. {
  213. #if ANKI_STATS_ENABLED
  214. ANKI_ASSERT(!!(m_flags & StatFlag::kFloat));
  215. checkThread();
  216. if(!!(m_flags & StatFlag::kMainThreadUpdates))
  217. {
  218. return m_f;
  219. }
  220. else
  221. {
  222. LockGuard lock(m_floatLock);
  223. return m_f;
  224. }
  225. #else
  226. return -1.0;
  227. #endif
  228. }
  229. private:
  230. #if ANKI_STATS_ENABLED
  231. union
  232. {
  233. Atomic<U64> m_atomic = {0};
  234. U64 m_u;
  235. F64 m_f;
  236. };
  237. union
  238. {
  239. U64 m_prevValueu = 0;
  240. F64 m_prevValuef;
  241. };
  242. const Char* m_name = nullptr;
  243. mutable SpinLock m_floatLock;
  244. StatFlag m_flags = StatFlag::kNone;
  245. StatCategory m_category = StatCategory::kCount;
  246. void checkThread() const;
  247. #endif
  248. };
  249. /// A collection of stat counters.
  250. class StatsSet : public MakeSingletonSimple<StatsSet>
  251. {
  252. friend class StatCounter;
  253. template<typename>
  254. friend class MakeSingletonSimple;
  255. public:
  256. void initFromMainThread()
  257. {
  258. #if ANKI_STATS_ENABLED
  259. ANKI_ASSERT(m_mainThreadId == kMaxU64);
  260. m_mainThreadId = Thread::getCurrentThreadId();
  261. #endif
  262. }
  263. /// @note Not thread-safe.
  264. template<typename TFuncUint, typename TFuncFloat>
  265. void iterateStats(TFuncUint funcUint, TFuncFloat funcFloat)
  266. {
  267. #if ANKI_STATS_ENABLED
  268. for(U32 i = 0; i < m_statCounterArrSize; ++i)
  269. {
  270. const StatCounter& counter = *m_statCounterArr[i];
  271. if(!!(counter.m_flags & StatFlag::kFloat))
  272. {
  273. funcFloat(counter.m_category, counter.m_name, counter.m_prevValuef, counter.m_flags);
  274. }
  275. else
  276. {
  277. funcUint(counter.m_category, counter.m_name, counter.m_prevValueu, counter.m_flags);
  278. }
  279. }
  280. #else
  281. (void)funcUint;
  282. (void)funcFloat;
  283. #endif
  284. }
  285. /// @note Not thread-safe.
  286. void endFrame()
  287. #if ANKI_STATS_ENABLED
  288. ;
  289. #else
  290. {
  291. }
  292. #endif
  293. /// @note Thread-safe.
  294. U32 getCounterCount() const
  295. {
  296. #if ANKI_STATS_ENABLED
  297. return m_statCounterArrSize;
  298. #else
  299. return 0;
  300. #endif
  301. }
  302. private:
  303. #if ANKI_STATS_ENABLED
  304. StatCounter** m_statCounterArr = nullptr;
  305. U32 m_statCounterArrSize = 0;
  306. U32 m_statCounterArrStorageSize = 0;
  307. U64 m_mainThreadId = kMaxU64;
  308. #endif
  309. StatsSet() = default;
  310. #if ANKI_STATS_ENABLED
  311. ~StatsSet();
  312. void registerCounter(StatCounter* counter);
  313. #endif
  314. };
  315. inline StatCounter::StatCounter(StatCategory category, const Char* name, StatFlag flags)
  316. #if ANKI_STATS_ENABLED
  317. : m_name(name)
  318. , m_flags(flags)
  319. , m_category(category)
  320. {
  321. ANKI_ASSERT(name);
  322. ANKI_ASSERT(m_category < StatCategory::kCount);
  323. StatsSet::getSingleton().registerCounter(this);
  324. }
  325. #else
  326. {
  327. (void)category;
  328. (void)name;
  329. (void)flags;
  330. }
  331. #endif
  332. #if ANKI_STATS_ENABLED
  333. inline void StatCounter::checkThread() const
  334. {
  335. if(!!(m_flags & StatFlag::kMainThreadUpdates))
  336. {
  337. ANKI_ASSERT(StatsSet::getSingleton().m_mainThreadId == Thread::getCurrentThreadId() && "Counter can only be updated from the main thread");
  338. }
  339. }
  340. #endif
  341. /// @}
  342. } // end namespace anki