Profiler.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "Str.h"
  25. #include "Timer.h"
  26. /// Profiling data for one block in the profiling tree.
  27. class ProfilerBlock
  28. {
  29. public:
  30. /// Construct with the specified parent block and name.
  31. ProfilerBlock(ProfilerBlock* parent, const char* name) :
  32. name_(name),
  33. time_(0),
  34. maxTime_(0),
  35. count_(0),
  36. parent_(parent),
  37. frameTime_(0),
  38. frameMaxTime_(0),
  39. frameCount_(0),
  40. intervalTime_(0),
  41. intervalMaxTime_(0),
  42. intervalCount_(0),
  43. totalTime_(0),
  44. totalMaxTime_(0),
  45. totalCount_(0)
  46. {
  47. }
  48. /// Destruct. Free the child blocks.
  49. ~ProfilerBlock()
  50. {
  51. for (PODVector<ProfilerBlock*>::Iterator i = children_.Begin(); i != children_.End(); ++i)
  52. {
  53. delete *i;
  54. *i = 0;
  55. }
  56. }
  57. /// Begin timing.
  58. void Begin()
  59. {
  60. timer_.Reset();
  61. ++count_;
  62. }
  63. /// End timing.
  64. void End()
  65. {
  66. long long time = timer_.GetUSec(false);
  67. if (time > maxTime_)
  68. maxTime_ = time;
  69. time_ += time;
  70. }
  71. /// End profiling frame and update interval and total values.
  72. void EndFrame()
  73. {
  74. frameTime_ = time_;
  75. frameMaxTime_ = maxTime_;
  76. frameCount_ = count_;
  77. intervalTime_ += time_;
  78. if (maxTime_ > intervalMaxTime_)
  79. intervalMaxTime_ = maxTime_;
  80. intervalCount_ += count_;
  81. totalTime_ += time_;
  82. if (maxTime_ > totalMaxTime_)
  83. totalMaxTime_ = maxTime_;
  84. totalCount_ += count_;
  85. time_ = 0;
  86. maxTime_ = 0;
  87. count_ = 0;
  88. for (PODVector<ProfilerBlock*>::Iterator i = children_.Begin(); i != children_.End(); ++i)
  89. (*i)->EndFrame();
  90. }
  91. /// Begin new profiling interval.
  92. void BeginInterval()
  93. {
  94. intervalTime_ = 0;
  95. intervalMaxTime_ = 0;
  96. intervalCount_ = 0;
  97. for (PODVector<ProfilerBlock*>::Iterator i = children_.Begin(); i != children_.End(); ++i)
  98. (*i)->BeginInterval();
  99. }
  100. /// Return child block with the specified name.
  101. ProfilerBlock* GetChild(const char* name)
  102. {
  103. // First check using string pointers only, then resort to actual strcmp
  104. for (PODVector<ProfilerBlock*>::Iterator i = children_.Begin(); i != children_.End(); ++i)
  105. {
  106. if ((*i)->name_ == name)
  107. return *i;
  108. }
  109. for (PODVector<ProfilerBlock*>::Iterator i = children_.Begin(); i != children_.End(); ++i)
  110. {
  111. if (!String::Compare((*i)->name_, name, true))
  112. return *i;
  113. }
  114. ProfilerBlock* newBlock = new ProfilerBlock(this, name);
  115. children_.Push(newBlock);
  116. return newBlock;
  117. }
  118. /// Block name.
  119. const char* name_;
  120. /// High-resolution timer for measuring the block duration.
  121. HiresTimer timer_;
  122. /// Time on current frame.
  123. long long time_;
  124. /// Maximum time on current frame.
  125. long long maxTime_;
  126. /// Calls on current frame.
  127. unsigned count_;
  128. /// Parent block.
  129. ProfilerBlock* parent_;
  130. /// Child blocks.
  131. PODVector<ProfilerBlock*> children_;
  132. /// Time on the previous frame.
  133. long long frameTime_;
  134. /// Maximum time on the previous frame.
  135. long long frameMaxTime_;
  136. /// Calls on the previous frame.
  137. unsigned frameCount_;
  138. /// Time during current profiler interval.
  139. long long intervalTime_;
  140. /// Maximum time during current profiler interval.
  141. long long intervalMaxTime_;
  142. /// Calls during current profiler interval.
  143. unsigned intervalCount_;
  144. /// Total accumulated time.
  145. long long totalTime_;
  146. /// All-time maximum time.
  147. long long totalMaxTime_;
  148. /// Total accumulated calls.
  149. unsigned totalCount_;
  150. };
  151. /// Hierarchical performance profiler subsystem.
  152. class Profiler : public Object
  153. {
  154. OBJECT(Profiler);
  155. public:
  156. /// Construct.
  157. Profiler(Context* context);
  158. /// Destruct.
  159. virtual ~Profiler();
  160. /// Begin timing a profiling block.
  161. void BeginBlock(const char* name)
  162. {
  163. current_ = current_->GetChild(name);
  164. current_->Begin();
  165. }
  166. /// End timing the current profiling block.
  167. void EndBlock()
  168. {
  169. if (current_ != root_)
  170. {
  171. current_->End();
  172. current_ = current_->parent_;
  173. }
  174. }
  175. /// Begin the profiling frame. Called by HandleBeginFrame().
  176. void BeginFrame();
  177. /// End the profiling frame. Called by HandleEndFrame().
  178. void EndFrame();
  179. /// Begin a new interval.
  180. void BeginInterval();
  181. /// Return profiling data as text output.
  182. String GetData(bool showUnused = false, bool showTotal = false, unsigned maxDepth = M_MAX_UNSIGNED) const;
  183. /// Return the current profiling block.
  184. const ProfilerBlock* GetCurrentBlock() { return current_; }
  185. /// Return the root profiling block.
  186. const ProfilerBlock* GetRootBlock() { return root_; }
  187. private:
  188. /// Return profiling data as text output for a specified profiling block.
  189. void GetData(ProfilerBlock* block, String& output, unsigned depth, unsigned maxDepth, bool showUnused, bool showTotal) const;
  190. /// Current profiling block.
  191. ProfilerBlock* current_;
  192. /// Root profiling block.
  193. ProfilerBlock* root_;
  194. /// Frames in the current interval.
  195. unsigned intervalFrames_;
  196. /// Total frames.
  197. unsigned totalFrames_;
  198. };
  199. /// Helper class for automatically beginning and ending a profiling block
  200. class AutoProfileBlock
  201. {
  202. public:
  203. /// Construct. Begin a profiling block with the specified name and optional call count.
  204. AutoProfileBlock(Profiler* profiler, const char* name) :
  205. profiler_(profiler)
  206. {
  207. if (profiler_)
  208. profiler_->BeginBlock(name);
  209. }
  210. /// Destruct. End the profiling block.
  211. ~AutoProfileBlock()
  212. {
  213. if (profiler_)
  214. profiler_->EndBlock();
  215. }
  216. private:
  217. /// Profiler.
  218. Profiler* profiler_;
  219. };
  220. #ifdef ENABLE_PROFILING
  221. #define PROFILE(name) AutoProfileBlock profile_ ## name (GetSubsystem<Profiler>(), #name)
  222. #else
  223. #define PROFILE(name)
  224. #endif