Profiler.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //
  2. // Copyright (c) 2008-2017 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 "../Container/Str.h"
  24. #include "../Core/Thread.h"
  25. #include "../Core/Timer.h"
  26. namespace Urho3D
  27. {
  28. /// Profiling data for one block in the profiling tree.
  29. class URHO3D_API ProfilerBlock
  30. {
  31. public:
  32. /// Construct with the specified parent block and name.
  33. ProfilerBlock(ProfilerBlock* parent, const char* name) :
  34. name_(0),
  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. if (name)
  50. {
  51. unsigned nameLength = String::CStringLength(name);
  52. name_ = new char[nameLength + 1];
  53. memcpy(name_, name, nameLength + 1);
  54. }
  55. }
  56. /// Destruct. Free the child blocks.
  57. virtual ~ProfilerBlock()
  58. {
  59. for (PODVector<ProfilerBlock*>::Iterator i = children_.Begin(); i != children_.End(); ++i)
  60. {
  61. delete *i;
  62. *i = 0;
  63. }
  64. delete [] name_;
  65. }
  66. /// Begin timing.
  67. void Begin()
  68. {
  69. timer_.Reset();
  70. ++count_;
  71. }
  72. /// End timing.
  73. void End()
  74. {
  75. long long time = timer_.GetUSec(false);
  76. if (time > maxTime_)
  77. maxTime_ = time;
  78. time_ += time;
  79. }
  80. /// End profiling frame and update interval and total values.
  81. void EndFrame()
  82. {
  83. frameTime_ = time_;
  84. frameMaxTime_ = maxTime_;
  85. frameCount_ = count_;
  86. intervalTime_ += time_;
  87. if (maxTime_ > intervalMaxTime_)
  88. intervalMaxTime_ = maxTime_;
  89. intervalCount_ += count_;
  90. totalTime_ += time_;
  91. if (maxTime_ > totalMaxTime_)
  92. totalMaxTime_ = maxTime_;
  93. totalCount_ += count_;
  94. time_ = 0;
  95. maxTime_ = 0;
  96. count_ = 0;
  97. for (PODVector<ProfilerBlock*>::Iterator i = children_.Begin(); i != children_.End(); ++i)
  98. (*i)->EndFrame();
  99. }
  100. /// Begin new profiling interval.
  101. void BeginInterval()
  102. {
  103. intervalTime_ = 0;
  104. intervalMaxTime_ = 0;
  105. intervalCount_ = 0;
  106. for (PODVector<ProfilerBlock*>::Iterator i = children_.Begin(); i != children_.End(); ++i)
  107. (*i)->BeginInterval();
  108. }
  109. /// Return child block with the specified name.
  110. ProfilerBlock* GetChild(const char* name)
  111. {
  112. for (PODVector<ProfilerBlock*>::Iterator i = children_.Begin(); i != children_.End(); ++i)
  113. {
  114. if (!String::Compare((*i)->name_, name, true))
  115. return *i;
  116. }
  117. ProfilerBlock* newBlock = new ProfilerBlock(this, name);
  118. children_.Push(newBlock);
  119. return newBlock;
  120. }
  121. /// Block name.
  122. char* name_;
  123. /// High-resolution timer for measuring the block duration.
  124. HiresTimer timer_;
  125. /// Time on current frame.
  126. long long time_;
  127. /// Maximum time on current frame.
  128. long long maxTime_;
  129. /// Calls on current frame.
  130. unsigned count_;
  131. /// Parent block.
  132. ProfilerBlock* parent_;
  133. /// Child blocks.
  134. PODVector<ProfilerBlock*> children_;
  135. /// Time on the previous frame.
  136. long long frameTime_;
  137. /// Maximum time on the previous frame.
  138. long long frameMaxTime_;
  139. /// Calls on the previous frame.
  140. unsigned frameCount_;
  141. /// Time during current profiler interval.
  142. long long intervalTime_;
  143. /// Maximum time during current profiler interval.
  144. long long intervalMaxTime_;
  145. /// Calls during current profiler interval.
  146. unsigned intervalCount_;
  147. /// Total accumulated time.
  148. long long totalTime_;
  149. /// All-time maximum time.
  150. long long totalMaxTime_;
  151. /// Total accumulated calls.
  152. unsigned totalCount_;
  153. };
  154. /// Hierarchical performance profiler subsystem.
  155. class URHO3D_API Profiler : public Object
  156. {
  157. URHO3D_OBJECT(Profiler, Object);
  158. public:
  159. /// Construct.
  160. Profiler(Context* context);
  161. /// Destruct.
  162. virtual ~Profiler();
  163. /// Begin timing a profiling block.
  164. void BeginBlock(const char* name)
  165. {
  166. // Profiler supports only the main thread currently
  167. if (!Thread::IsMainThread())
  168. return;
  169. current_ = current_->GetChild(name);
  170. current_->Begin();
  171. }
  172. /// End timing the current profiling block.
  173. void EndBlock()
  174. {
  175. if (!Thread::IsMainThread())
  176. return;
  177. current_->End();
  178. if (current_->parent_)
  179. current_ = current_->parent_;
  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. This method is not thread-safe.
  188. const String& PrintData(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. protected:
  194. /// Return profiling data as text output for a specified profiling block.
  195. void PrintData(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. };
  203. /// Helper class for automatically beginning and ending a profiling block
  204. class URHO3D_API AutoProfileBlock
  205. {
  206. public:
  207. /// Construct. Begin a profiling block with the specified name and optional call count.
  208. AutoProfileBlock(Profiler* profiler, const char* name) :
  209. profiler_(profiler)
  210. {
  211. if (profiler_)
  212. profiler_->BeginBlock(name);
  213. }
  214. /// Destruct. End the profiling block.
  215. ~AutoProfileBlock()
  216. {
  217. if (profiler_)
  218. profiler_->EndBlock();
  219. }
  220. private:
  221. /// Profiler.
  222. Profiler* profiler_;
  223. };
  224. #ifdef URHO3D_PROFILING
  225. #define URHO3D_PROFILE(name) Urho3D::AutoProfileBlock profile_ ## name (GetSubsystem<Urho3D::Profiler>(), #name)
  226. #else
  227. #define URHO3D_PROFILE(name)
  228. #endif
  229. }