Profiler.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Timer.h"
  25. /// Profiling data for one block in the profiling tree.
  26. class ProfilerBlock
  27. {
  28. public:
  29. /// Construct with the specified parent block and name.
  30. ProfilerBlock(ProfilerBlock* parent, const char* name) :
  31. name_(name),
  32. time_(0),
  33. maxTime_(0),
  34. count_(0),
  35. parent_(parent),
  36. lastSearchName_(0),
  37. lastSearchBlock_(0),
  38. frameTime_(0),
  39. frameMaxTime_(0),
  40. frameCount_(0),
  41. intervalTime_(0),
  42. intervalMaxTime_(0),
  43. intervalCount_(0),
  44. totalTime_(0),
  45. totalMaxTime_(0),
  46. totalCount_(0)
  47. {
  48. }
  49. /// Destruct. Free the child blocks.
  50. ~ProfilerBlock()
  51. {
  52. for (PODVector<ProfilerBlock*>::Iterator i = children_.Begin(); i != children_.End(); ++i)
  53. {
  54. delete *i;
  55. *i = 0;
  56. }
  57. }
  58. /// Begin timing.
  59. void Begin()
  60. {
  61. timer_.Reset();
  62. ++count_;
  63. }
  64. /// End timing.
  65. void End()
  66. {
  67. long long time = timer_.GetUSec(false);
  68. if (time > maxTime_)
  69. maxTime_ = time;
  70. time_ += time;
  71. }
  72. /// End profiling frame and update interval and total values.
  73. void EndFrame()
  74. {
  75. frameTime_ = time_;
  76. frameMaxTime_ = maxTime_;
  77. frameCount_ = count_;
  78. intervalTime_ += time_;
  79. if (maxTime_ > intervalMaxTime_)
  80. intervalMaxTime_ = maxTime_;
  81. intervalCount_ += count_;
  82. totalTime_ += time_;
  83. if (maxTime_ > totalMaxTime_)
  84. totalMaxTime_ = maxTime_;
  85. totalCount_ += count_;
  86. time_ = 0;
  87. maxTime_ = 0;
  88. count_ = 0;
  89. for (PODVector<ProfilerBlock*>::Iterator i = children_.Begin(); i != children_.End(); ++i)
  90. (*i)->EndFrame();
  91. }
  92. /// Begin new profiling interval.
  93. void BeginInterval()
  94. {
  95. intervalTime_ = 0;
  96. intervalMaxTime_ = 0;
  97. intervalCount_ = 0;
  98. for (PODVector<ProfilerBlock*>::Iterator i = children_.Begin(); i != children_.End(); ++i)
  99. (*i)->BeginInterval();
  100. }
  101. /// Return child block with the specified name.
  102. ProfilerBlock* GetChild(const char* name)
  103. {
  104. if (name == lastSearchName_)
  105. return lastSearchBlock_;
  106. lastSearchName_ = name;
  107. for (PODVector<ProfilerBlock*>::Iterator i = children_.Begin(); i != children_.End(); ++i)
  108. {
  109. if ((*i)->name_ == name)
  110. {
  111. lastSearchBlock_ = *i;
  112. return *i;
  113. }
  114. }
  115. ProfilerBlock* newBlock = new ProfilerBlock(this, name);
  116. children_.Push(newBlock);
  117. lastSearchBlock_ = newBlock;
  118. return newBlock;
  119. }
  120. /// Block name.
  121. const char* name_;
  122. /// High-resolution timer for measuring the block duration.
  123. HiresTimer timer_;
  124. /// Time on current frame.
  125. long long time_;
  126. /// Maximum time on current frame.
  127. long long maxTime_;
  128. /// Calls on current frame.
  129. unsigned count_;
  130. /// Parent block.
  131. ProfilerBlock* parent_;
  132. /// Last queried child block name (optimization.)
  133. const char* lastSearchName_;
  134. /// Last queried child block (optimization.)
  135. ProfilerBlock* lastSearchBlock_;
  136. /// Child blocks.
  137. PODVector<ProfilerBlock*> children_;
  138. /// Time on the previous frame.
  139. long long frameTime_;
  140. /// Maximum time on the previous frame.
  141. long long frameMaxTime_;
  142. /// Calls on the previous frame.
  143. unsigned frameCount_;
  144. /// Time during current profiler interval.
  145. long long intervalTime_;
  146. /// Maximum time during current profiler interval.
  147. long long intervalMaxTime_;
  148. /// Calls during current profiler interval.
  149. unsigned intervalCount_;
  150. /// Total accumulated time.
  151. long long totalTime_;
  152. /// All-time maximum time.
  153. long long totalMaxTime_;
  154. /// Total accumulated calls.
  155. unsigned totalCount_;
  156. };
  157. /// Hierarchical performance profiler subsystem.
  158. class Profiler : public Object
  159. {
  160. OBJECT(Profiler);
  161. public:
  162. /// Construct.
  163. Profiler(Context* context);
  164. /// Destruct.
  165. virtual ~Profiler();
  166. /// Begin timing a profiling block.
  167. void BeginBlock(const char* name)
  168. {
  169. current_ = current_->GetChild(name);
  170. current_->Begin();
  171. }
  172. /// End timing the current profiling block.
  173. void EndBlock()
  174. {
  175. if (current_ != root_)
  176. {
  177. current_->End();
  178. current_ = current_->parent_;
  179. }
  180. }
  181. /// Begin the profiling frame. Called by HandleBeginFrame().
  182. void BeginFrame();
  183. /// End the profiling frame. Called by HandleEndFrame().
  184. void EndFrame();
  185. /// Begin a new interval.
  186. void BeginInterval();
  187. /// Return profiling data as text output.
  188. String GetData(bool showUnused = false, bool showTotal = false, unsigned maxDepth = M_MAX_UNSIGNED) const;
  189. /// Return the current profiling block.
  190. const ProfilerBlock* GetCurrentBlock() { return current_; }
  191. /// Return the root profiling block.
  192. const ProfilerBlock* GetRootBlock() { return root_; }
  193. private:
  194. /// Return profiling data as text output for a specified profiling block.
  195. void GetData(ProfilerBlock* block, String& output, unsigned depth, unsigned maxDepth, bool showUnused, bool showTotal) const;
  196. /// Current profiling block.
  197. ProfilerBlock* current_;
  198. /// Root profiling block.
  199. ProfilerBlock* root_;
  200. /// Frames in the current interval.
  201. unsigned intervalFrames_;
  202. /// Total frames.
  203. unsigned totalFrames_;
  204. };
  205. /// Helper class for automatically beginning and ending a profiling block
  206. class AutoProfileBlock
  207. {
  208. public:
  209. /// Construct. Begin a profiling block with the specified name and optional call count.
  210. AutoProfileBlock(Profiler* profiler, const char* name) :
  211. profiler_(profiler)
  212. {
  213. if (profiler_)
  214. profiler_->BeginBlock(name);
  215. }
  216. /// Destruct. End the profiling block.
  217. ~AutoProfileBlock()
  218. {
  219. if (profiler_)
  220. profiler_->EndBlock();
  221. }
  222. private:
  223. /// Profiler.
  224. Profiler* profiler_;
  225. };
  226. #ifdef ENABLE_PROFILING
  227. #define PROFILE(name) AutoProfileBlock profile_ ## name (GetSubsystem<Profiler>(), #name)
  228. #else
  229. #define PROFILE(name)
  230. #endif