Profiler.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 "Thread.h"
  25. #include "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. ~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. OBJECT(Profiler);
  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. if (current_ != root_)
  178. {
  179. current_->End();
  180. current_ = current_->parent_;
  181. }
  182. }
  183. /// Begin the profiling frame. Called by HandleBeginFrame().
  184. void BeginFrame();
  185. /// End the profiling frame. Called by HandleEndFrame().
  186. void EndFrame();
  187. /// Begin a new interval.
  188. void BeginInterval();
  189. /// Return profiling data as text output.
  190. String GetData(bool showUnused = false, bool showTotal = false, unsigned maxDepth = M_MAX_UNSIGNED) const;
  191. /// Return the current profiling block.
  192. const ProfilerBlock* GetCurrentBlock() { return current_; }
  193. /// Return the root profiling block.
  194. const ProfilerBlock* GetRootBlock() { return root_; }
  195. private:
  196. /// Return profiling data as text output for a specified profiling block.
  197. void GetData(ProfilerBlock* block, String& output, unsigned depth, unsigned maxDepth, bool showUnused, bool showTotal) const;
  198. /// Current profiling block.
  199. ProfilerBlock* current_;
  200. /// Root profiling block.
  201. ProfilerBlock* root_;
  202. /// Frames in the current interval.
  203. unsigned intervalFrames_;
  204. /// Total frames.
  205. unsigned totalFrames_;
  206. };
  207. /// Helper class for automatically beginning and ending a profiling block
  208. class URHO3D_API AutoProfileBlock
  209. {
  210. public:
  211. /// Construct. Begin a profiling block with the specified name and optional call count.
  212. AutoProfileBlock(Profiler* profiler, const char* name) :
  213. profiler_(profiler)
  214. {
  215. if (profiler_)
  216. profiler_->BeginBlock(name);
  217. }
  218. /// Destruct. End the profiling block.
  219. ~AutoProfileBlock()
  220. {
  221. if (profiler_)
  222. profiler_->EndBlock();
  223. }
  224. private:
  225. /// Profiler.
  226. Profiler* profiler_;
  227. };
  228. #ifdef URHO3D_PROFILING
  229. #define PROFILE(name) Urho3D::AutoProfileBlock profile_ ## name (GetSubsystem<Urho3D::Profiler>(), #name)
  230. #else
  231. #define PROFILE(name)
  232. #endif
  233. }