2
0

Profiler.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "Str.h"
  24. #include "Timer.h"
  25. namespace Urho3D
  26. {
  27. /// Profiling data for one block in the profiling tree.
  28. class URHO3D_API ProfilerBlock
  29. {
  30. public:
  31. /// Construct with the specified parent block and name.
  32. ProfilerBlock(ProfilerBlock* parent, const char* name) :
  33. name_(name),
  34. time_(0),
  35. maxTime_(0),
  36. count_(0),
  37. parent_(parent),
  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. // First check using string pointers only, then resort to actual strcmp
  105. for (PODVector<ProfilerBlock*>::Iterator i = children_.Begin(); i != children_.End(); ++i)
  106. {
  107. if ((*i)->name_ == name)
  108. return *i;
  109. }
  110. for (PODVector<ProfilerBlock*>::Iterator i = children_.Begin(); i != children_.End(); ++i)
  111. {
  112. if (!String::Compare((*i)->name_, name, true))
  113. return *i;
  114. }
  115. ProfilerBlock* newBlock = new ProfilerBlock(this, name);
  116. children_.Push(newBlock);
  117. return newBlock;
  118. }
  119. /// Block name.
  120. const char* name_;
  121. /// High-resolution timer for measuring the block duration.
  122. HiresTimer timer_;
  123. /// Time on current frame.
  124. long long time_;
  125. /// Maximum time on current frame.
  126. long long maxTime_;
  127. /// Calls on current frame.
  128. unsigned count_;
  129. /// Parent block.
  130. ProfilerBlock* parent_;
  131. /// Child blocks.
  132. PODVector<ProfilerBlock*> children_;
  133. /// Time on the previous frame.
  134. long long frameTime_;
  135. /// Maximum time on the previous frame.
  136. long long frameMaxTime_;
  137. /// Calls on the previous frame.
  138. unsigned frameCount_;
  139. /// Time during current profiler interval.
  140. long long intervalTime_;
  141. /// Maximum time during current profiler interval.
  142. long long intervalMaxTime_;
  143. /// Calls during current profiler interval.
  144. unsigned intervalCount_;
  145. /// Total accumulated time.
  146. long long totalTime_;
  147. /// All-time maximum time.
  148. long long totalMaxTime_;
  149. /// Total accumulated calls.
  150. unsigned totalCount_;
  151. };
  152. /// Hierarchical performance profiler subsystem.
  153. class URHO3D_API Profiler : public Object
  154. {
  155. OBJECT(Profiler);
  156. public:
  157. /// Construct.
  158. Profiler(Context* context);
  159. /// Destruct.
  160. virtual ~Profiler();
  161. /// Begin timing a profiling block.
  162. void BeginBlock(const char* name)
  163. {
  164. current_ = current_->GetChild(name);
  165. current_->Begin();
  166. }
  167. /// End timing the current profiling block.
  168. void EndBlock()
  169. {
  170. if (current_ != root_)
  171. {
  172. current_->End();
  173. current_ = current_->parent_;
  174. }
  175. }
  176. /// Begin the profiling frame. Called by HandleBeginFrame().
  177. void BeginFrame();
  178. /// End the profiling frame. Called by HandleEndFrame().
  179. void EndFrame();
  180. /// Begin a new interval.
  181. void BeginInterval();
  182. /// Return profiling data as text output.
  183. String GetData(bool showUnused = false, bool showTotal = false, unsigned maxDepth = M_MAX_UNSIGNED) const;
  184. /// Return the current profiling block.
  185. const ProfilerBlock* GetCurrentBlock() { return current_; }
  186. /// Return the root profiling block.
  187. const ProfilerBlock* GetRootBlock() { return root_; }
  188. private:
  189. /// Return profiling data as text output for a specified profiling block.
  190. void GetData(ProfilerBlock* block, String& output, unsigned depth, unsigned maxDepth, bool showUnused, bool showTotal) const;
  191. /// Current profiling block.
  192. ProfilerBlock* current_;
  193. /// Root profiling block.
  194. ProfilerBlock* root_;
  195. /// Frames in the current interval.
  196. unsigned intervalFrames_;
  197. /// Total frames.
  198. unsigned totalFrames_;
  199. };
  200. /// Helper class for automatically beginning and ending a profiling block
  201. class URHO3D_API AutoProfileBlock
  202. {
  203. public:
  204. /// Construct. Begin a profiling block with the specified name and optional call count.
  205. AutoProfileBlock(Profiler* profiler, const char* name) :
  206. profiler_(profiler)
  207. {
  208. if (profiler_)
  209. profiler_->BeginBlock(name);
  210. }
  211. /// Destruct. End the profiling block.
  212. ~AutoProfileBlock()
  213. {
  214. if (profiler_)
  215. profiler_->EndBlock();
  216. }
  217. private:
  218. /// Profiler.
  219. Profiler* profiler_;
  220. };
  221. #ifdef ENABLE_PROFILING
  222. #define PROFILE(name) AutoProfileBlock profile_ ## name (GetSubsystem<Profiler>(), #name)
  223. #else
  224. #define PROFILE(name)
  225. #endif
  226. }