BsProfilerCPU.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsModule.h"
  4. #include "BsFrameAlloc.h"
  5. namespace BansheeEngine
  6. {
  7. /** @addtogroup Profiling
  8. * @{
  9. */
  10. class CPUProfilerReport;
  11. /**
  12. * Provides various performance measuring methods.
  13. *
  14. * @note Thread safe. Matching begin*\end* calls must belong to the same thread though.
  15. */
  16. class BS_CORE_EXPORT ProfilerCPU : public Module<ProfilerCPU>
  17. {
  18. /** Timer class responsible for tracking elapsed time. */
  19. class Timer
  20. {
  21. public:
  22. Timer();
  23. /** Sets the start time for the timer. */
  24. void start();
  25. /** Stops the timer and calculates the elapsed time from start time to now. */
  26. void stop();
  27. /** Resets the elapsed time to zero. */
  28. void reset();
  29. double time;
  30. private:
  31. double startTime;
  32. /** Returns time elapsed since CPU was started in millseconds. */
  33. static inline double getCurrentTime();
  34. };
  35. /** Timer class responsible for tracking number of elapsed CPU cycles. */
  36. class TimerPrecise
  37. {
  38. public:
  39. TimerPrecise();
  40. /** Starts the counter marking the current number of executed CPU cycles since CPU was started. */
  41. void start();
  42. /** Ends the counter and calculates the number of CPU cycles between now and the start time. */
  43. void stop();
  44. /** Resets the cycle count to zero. */
  45. void reset();
  46. UINT64 cycles;
  47. private:
  48. UINT64 startCycles;
  49. /** Queries the CPU for the current number of CPU cycles executed since the program was started. */
  50. static inline UINT64 getNumCycles();
  51. };
  52. /**
  53. * Contains data about a single profiler sample (counting time in milliseconds).
  54. *
  55. * @note
  56. * A sample is created whenever a named profile block is entered. e.g. if you have a function you are profiling,
  57. * and it gets called 10 times, there will be 10 samples.
  58. */
  59. struct ProfileSample
  60. {
  61. ProfileSample(double _time, UINT64 _numAllocs, UINT64 _numFrees)
  62. :time(_time), numAllocs(_numAllocs), numFrees(_numFrees)
  63. { }
  64. double time;
  65. UINT64 numAllocs;
  66. UINT64 numFrees;
  67. };
  68. /**
  69. * Contains data about a single precise profiler sample (counting CPU cycles).
  70. *
  71. * @note
  72. * A sample is created whenever a named profile block is entered. e.g. if you have a function you are profiling,
  73. * and it gets called 10 times, there will be 10 samples.
  74. */
  75. struct PreciseProfileSample
  76. {
  77. PreciseProfileSample(UINT64 _cycles, UINT64 _numAllocs, UINT64 _numFrees)
  78. :cycles(_cycles), numAllocs(_numAllocs), numFrees(_numFrees)
  79. { }
  80. UINT64 cycles;
  81. UINT64 numAllocs;
  82. UINT64 numFrees;
  83. };
  84. /** Contains basic (time based) profiling data contained in a profiling block. */
  85. struct ProfileData
  86. {
  87. ProfileData(FrameAlloc* alloc);
  88. /** Begins a new sample and records current sample state. Previous sample must not be active. */
  89. void beginSample();
  90. /**
  91. * Records current sample state and creates a new sample based on start and end state. Adds the sample to the
  92. * sample list.
  93. */
  94. void endSample();
  95. /**
  96. * Removes the last added sample from the sample list and makes it active again. You must call endSample()
  97. * when done as if you called beginSample().
  98. */
  99. void resumeLastSample();
  100. Vector<ProfileSample, StdFrameAlloc<ProfileSample>> samples;
  101. Timer timer;
  102. UINT64 memAllocs;
  103. UINT64 memFrees;
  104. };
  105. /** Contains precise (CPU cycle based) profiling data contained in a profiling block. */
  106. struct PreciseProfileData
  107. {
  108. PreciseProfileData(FrameAlloc* alloc);
  109. /** Begins a new sample and records current sample state. Previous sample must not be active. */
  110. void beginSample();
  111. /**
  112. * Records current sample state and creates a new sample based on start and end state. Adds the sample to the
  113. * sample list.
  114. */
  115. void endSample();
  116. /**
  117. * Removes the last added sample from the sample list and makes it active again. You must call endSample()
  118. * when done as if you called beginSample.
  119. */
  120. void resumeLastSample();
  121. Vector<PreciseProfileSample, StdFrameAlloc<ProfileSample>> samples;
  122. TimerPrecise timer;
  123. UINT64 memAllocs;
  124. UINT64 memFrees;
  125. };
  126. /**
  127. * Contains all sampling information about a single named profiling block. Each block has its own sampling
  128. * information and optionally child blocks.
  129. */
  130. struct ProfiledBlock
  131. {
  132. ProfiledBlock(FrameAlloc* alloc);
  133. ~ProfiledBlock();
  134. /** Attempts to find a child block with the specified name. Returns null if not found. */
  135. ProfiledBlock* findChild(const char* name) const;
  136. char* name;
  137. ProfileData basic;
  138. PreciseProfileData precise;
  139. Vector<ProfiledBlock*, StdFrameAlloc<ProfiledBlock*>> children;
  140. };
  141. /** CPU sampling type. */
  142. enum class ActiveSamplingType
  143. {
  144. Basic, /**< Sample using milliseconds. */
  145. Precise /**< Sample using CPU cycles. */
  146. };
  147. /** Contains data about the currently active profiling block. */
  148. struct ActiveBlock
  149. {
  150. ActiveBlock()
  151. :type(ActiveSamplingType::Basic), block(nullptr)
  152. { }
  153. ActiveBlock(ActiveSamplingType _type, ProfiledBlock* _block)
  154. :type(_type), block(_block)
  155. { }
  156. ActiveSamplingType type;
  157. ProfiledBlock* block;
  158. };
  159. /** Contains data about an active profiling thread. */
  160. struct ThreadInfo
  161. {
  162. ThreadInfo();
  163. /**
  164. * Starts profiling on the thread. New primary profiling block is created with the given name.
  165. */
  166. void begin(const char* _name);
  167. /**
  168. * Ends profiling on the thread. You should end all samples before calling this, but if you don't they will be
  169. * terminated automatically.
  170. */
  171. void end();
  172. /**
  173. * Deletes all internal profiling data and makes the object ready for another iteration. Should be called
  174. * after end in order to delete any existing data.
  175. */
  176. void reset();
  177. /** Gets the primary profiling block used by the thread. */
  178. ProfiledBlock* getBlock(const char* name);
  179. /** Deletes the provided block. */
  180. void releaseBlock(ProfiledBlock* block);
  181. static BS_THREADLOCAL ThreadInfo* activeThread;
  182. bool isActive;
  183. ProfiledBlock* rootBlock;
  184. FrameAlloc frameAlloc;
  185. ActiveBlock activeBlock;
  186. Stack<ActiveBlock, StdFrameAlloc<ActiveBlock>>* activeBlocks;
  187. };
  188. public:
  189. ProfilerCPU();
  190. ~ProfilerCPU();
  191. /**
  192. * Registers a new thread we will be doing sampling in. This needs to be called before any beginSample*\endSample*
  193. * calls are made in that thread.
  194. *
  195. * @param[in] name Name that will allow you to more easily identify the thread.
  196. */
  197. void beginThread(const char* name);
  198. /** Ends sampling for the current thread. No beginSample*\endSample* calls after this point. */
  199. void endThread();
  200. /**
  201. * Begins sample measurement. Must be followed by endSample().
  202. *
  203. * @param[in] name Unique name for the sample you can later use to find the sampling data.
  204. */
  205. void beginSample(const char* name);
  206. /**
  207. * Ends sample measurement.
  208. *
  209. * @param[in] name Unique name for the sample.
  210. *
  211. * @note
  212. * Unique name is primarily needed to more easily identify mismatched begin/end sample pairs. Otherwise the name in
  213. * beginSample() would be enough.
  214. */
  215. void endSample(const char* name);
  216. /**
  217. * Begins precise sample measurement. Must be followed by endSamplePrecise().
  218. *
  219. * @param[in] name Unique name for the sample you can later use to find the sampling data.
  220. *
  221. * @note
  222. * This method uses very precise CPU counters to determine variety of data not provided by standard beginSample().
  223. * However due to the way these counters work you should not use this method for larger parts of code. It does not
  224. * consider context switches so if the OS decides to switch context between measurements you will get invalid data.
  225. */
  226. void beginSamplePrecise(const char* name);
  227. /**
  228. * Ends precise sample measurement.
  229. *
  230. * @param[in] name Unique name for the sample.
  231. *
  232. * @note
  233. * Unique name is primarily needed to more easily identify mismatched begin/end sample pairs. Otherwise the name
  234. * in beginSamplePrecise() would be enough.
  235. */
  236. void endSamplePrecise(const char* name);
  237. /** Clears all sampling data, and ends any unfinished sampling blocks. */
  238. void reset();
  239. /**
  240. * Generates a report from all previously sampled data.
  241. *
  242. * @note Generating a report will stop all in-progress sampling. You should make sure
  243. * you call endSample* manually beforehand so this doesn't have to happen.
  244. */
  245. CPUProfilerReport generateReport();
  246. private:
  247. /**
  248. * Calculates overhead that the timing and sampling methods themselves introduce so we might get more accurate
  249. * measurements when creating reports.
  250. */
  251. void estimateTimerOverhead();
  252. private:
  253. double mBasicTimerOverhead;
  254. UINT64 mPreciseTimerOverhead;
  255. double mBasicSamplingOverheadMs;
  256. double mPreciseSamplingOverheadMs;
  257. UINT64 mBasicSamplingOverheadCycles;
  258. UINT64 mPreciseSamplingOverheadCycles;
  259. ProfilerVector<ThreadInfo*> mActiveThreads;
  260. BS_MUTEX(mThreadSync);
  261. };
  262. /** Profiling entry containing information about a single CPU profiling block containing timing information. */
  263. struct BS_CORE_EXPORT CPUProfilerBasicSamplingEntry
  264. {
  265. struct BS_CORE_EXPORT Data
  266. {
  267. Data();
  268. String name; /**< Name of the profiling block. */
  269. UINT32 numCalls; /**< Number of times the block was entered. */
  270. UINT64 memAllocs; /**< Number of memory allocations that happened within the block. */
  271. UINT64 memFrees; /**< Number of memory deallocations that happened within the block. */
  272. double avgTimeMs; /**< Average time it took to execute the block, per call. In milliseconds. */
  273. double maxTimeMs; /**< Maximum time of a single call in the block. In milliseconds. */
  274. double totalTimeMs; /**< Total time the block took, across all calls. In milliseconds. */
  275. double avgSelfTimeMs; /**< Average time it took to execute the block, per call. Ignores time used by child blocks. In milliseconds. */
  276. double totalSelfTimeMs; /**< Total time the block took, across all calls. Ignores time used by child blocks. In milliseconds. */
  277. double estimatedSelfOverheadMs; /**< Estimated overhead of profiling methods, only for this exact block. In milliseconds. */
  278. double estimatedOverheadMs; /**< Estimated overhead of profiling methods for this block and all children. In milliseconds. */
  279. float pctOfParent; /**< Percent of parent block time this block took to execute. Ranging [0.0, 1.0]. */
  280. } data;
  281. ProfilerVector<CPUProfilerBasicSamplingEntry> childEntries;
  282. };
  283. /**
  284. * Profiling entry containing information about a single CPU profiling block containing CPU cycle count based
  285. * information.
  286. */
  287. struct BS_CORE_EXPORT CPUProfilerPreciseSamplingEntry
  288. {
  289. struct BS_CORE_EXPORT Data
  290. {
  291. Data();
  292. String name; /**< Name of the profiling block. */
  293. UINT32 numCalls; /**< Number of times the block was entered. */
  294. UINT64 memAllocs; /**< Number of memory allocations that happened within the block. */
  295. UINT64 memFrees; /**< Number of memory deallocations that happened within the block. */
  296. UINT64 avgCycles; /**< Average number of cycles it took to execute the block, per call. */
  297. UINT64 maxCycles; /**< Maximum number of cycles of a single call in the block. */
  298. UINT64 totalCycles; /**< Total number of cycles across all calls in the block. */
  299. UINT64 avgSelfCycles; /**< Average number of cycles it took to execute the block, per call. Ignores cycles used by child blocks. */
  300. UINT64 totalSelfCycles; /**< Total number of cycles across all calls in the block. Ignores time used by child blocks. */
  301. UINT64 estimatedSelfOverhead; /**< Estimated overhead of profiling methods, only for this exact block. In cycles. */
  302. UINT64 estimatedOverhead; /**< Estimated overhead of profiling methods for this block and all children. In cycles. */
  303. float pctOfParent; /**< Percent of parent block cycles used by this block. Ranging [0.0, 1.0]. */
  304. } data;
  305. ProfilerVector<CPUProfilerPreciseSamplingEntry> childEntries;
  306. };
  307. /** CPU profiling report containing all profiling information for a single profiling session. */
  308. class BS_CORE_EXPORT CPUProfilerReport
  309. {
  310. public:
  311. CPUProfilerReport();
  312. /**
  313. * Returns root entry for the basic (time based) sampling data. Root entry always contains the profiling block
  314. * associated with the entire thread.
  315. */
  316. const CPUProfilerBasicSamplingEntry& getBasicSamplingData() const { return mBasicSamplingRootEntry; }
  317. /**
  318. * Returns root entry for the precise (CPU cycle based) sampling data. Root entry always contains the profiling
  319. * block associated with the entire thread.
  320. */
  321. const CPUProfilerPreciseSamplingEntry& getPreciseSamplingData() const { return mPreciseSamplingRootEntry; }
  322. private:
  323. friend class ProfilerCPU;
  324. CPUProfilerBasicSamplingEntry mBasicSamplingRootEntry;
  325. CPUProfilerPreciseSamplingEntry mPreciseSamplingRootEntry;
  326. };
  327. /** Easier way to access ProfilerCPU. */
  328. BS_CORE_EXPORT ProfilerCPU& gProfilerCPU();
  329. /** Shortcut for profiling a single function call. */
  330. #define PROFILE_CALL(call, name) \
  331. BansheeEngine::gProfilerCPU().beginSample(##name##); \
  332. call; \
  333. BansheeEngine::gProfilerCPU().endSample(##name##);
  334. /** @} */
  335. }