Profiler.h 7.5 KB

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