Profiler.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #include "Precompiled.h"
  24. #include "CoreEvents.h"
  25. #include "Profiler.h"
  26. #include <cstdio>
  27. #include <cstring>
  28. #include <ctype.h>
  29. #include "DebugNew.h"
  30. static const int LINE_MAX_LENGTH = 256;
  31. static const int NAME_MAX_LENGTH = 30;
  32. OBJECTTYPESTATIC(Profiler);
  33. Profiler::Profiler(Context* context) :
  34. Object(context),
  35. current_(0),
  36. root_(0),
  37. accumulatedFrames_(0),
  38. totalFrames_(0)
  39. {
  40. root_ = new ProfilerBlock(0, "Root");
  41. current_ = root_;
  42. SubscribeToEvent(E_BEGINFRAME, HANDLER(Profiler, HandleBeginFrame));
  43. SubscribeToEvent(E_ENDFRAME, HANDLER(Profiler, HandleEndFrame));
  44. }
  45. Profiler::~Profiler()
  46. {
  47. delete root_;
  48. root_ = 0;
  49. }
  50. void Profiler::BeginFrame()
  51. {
  52. // End the previous frame if any
  53. EndFrame();
  54. BeginBlock("RunFrame");
  55. }
  56. void Profiler::EndFrame()
  57. {
  58. if (current_ != root_)
  59. {
  60. EndBlock();
  61. ++accumulatedFrames_;
  62. ++totalFrames_;
  63. if (!totalFrames_)
  64. ++totalFrames_;
  65. root_->EndFrame();
  66. current_ = root_;
  67. }
  68. }
  69. void Profiler::ClearAccumulated()
  70. {
  71. root_->ClearAccumulated();
  72. accumulatedFrames_ = 0;
  73. }
  74. String Profiler::GetData(bool showUnused, bool showAccumulated, bool showTotal) const
  75. {
  76. String output;
  77. if (!showTotal)
  78. output += String("Block Count Average Total\n \n");
  79. else
  80. {
  81. output += String("Block Frame average Accumulated total\n\n");
  82. output += String(" Count Average Total Count Average Total\n\n");
  83. }
  84. GetData(root_, output, 0, showUnused, showAccumulated, showTotal);
  85. return output;
  86. }
  87. void Profiler::GetData(ProfilerBlock* block, String& output, unsigned indent, bool showUnused, bool showAccumulated, bool showTotal) const
  88. {
  89. char line[LINE_MAX_LENGTH];
  90. char indentedName[LINE_MAX_LENGTH];
  91. unsigned frames = Max(totalFrames_, 1);
  92. unsigned accumulatedFrames = Max(accumulatedFrames_, 1);
  93. // Do not print the root block as it does not collect any actual data
  94. if (block != root_)
  95. {
  96. // Output data in milliseconds
  97. float frameTime = block->GetFrameTime() / 1000.0f;
  98. float accumulatedTime = block->GetAccumulatedTime() / 1000.0f;
  99. float totalTime = block->GetTotalTime() / 1000.0f;
  100. unsigned frameCount = block->GetFrameCount();
  101. unsigned accumulatedCount = block->GetAccumulatedCount();
  102. unsigned totalCount = block->GetTotalCount();
  103. float avgFrameTime = 0.0f;
  104. float avgAccumulatedTime = 0.0f;
  105. float avgTotalTime = 0.0f;
  106. if (frameCount)
  107. avgFrameTime = frameTime / frameCount;
  108. if (accumulatedCount)
  109. avgAccumulatedTime = accumulatedTime / accumulatedCount;
  110. if (totalCount)
  111. avgTotalTime = totalTime / totalCount;
  112. if (showUnused || frameCount || (showAccumulated && accumulatedCount))
  113. {
  114. memset(indentedName, ' ', NAME_MAX_LENGTH);
  115. indentedName[indent] = 0;
  116. strcat(indentedName, block->GetName());
  117. indentedName[strlen(indentedName)] = ' ';
  118. indentedName[NAME_MAX_LENGTH] = 0;
  119. if (!showTotal)
  120. {
  121. if (!showAccumulated)
  122. sprintf(line, "%s %5u %8.3f %8.3f\n", indentedName, frameCount, avgFrameTime, frameTime);
  123. else
  124. sprintf(line, "%s %5u %8.3f %8.3f\n", indentedName,
  125. (unsigned)((float)accumulatedCount / accumulatedFrames + 0.5f),
  126. avgAccumulatedTime, accumulatedTime / accumulatedFrames);
  127. }
  128. else
  129. {
  130. unsigned avgFrameCount = (unsigned)((float)totalCount / frames + 0.5f);
  131. float avgFrameTime = avgTotalTime;
  132. float avgFrameTotalTime = totalTime / frames;
  133. if (!avgFrameCount)
  134. {
  135. avgFrameTime = 0.0f;
  136. avgFrameTotalTime = 0.0f;
  137. }
  138. sprintf(line, "%s %10u %11.3f %11.3f %10u %11.3f %11.3f\n",
  139. indentedName, avgFrameCount, avgFrameTime, avgFrameTotalTime,
  140. totalCount, avgTotalTime, totalTime);
  141. }
  142. output += String(line);
  143. indent++;
  144. }
  145. }
  146. const PODVector<ProfilerBlock*>& children = block->GetChildren();
  147. for (PODVector<ProfilerBlock*>::ConstIterator i = children.Begin(); i != children.End(); ++i)
  148. GetData(*i, output, indent, showUnused, showAccumulated, showTotal);
  149. }
  150. void Profiler::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  151. {
  152. BeginFrame();
  153. }
  154. void Profiler::HandleEndFrame(StringHash eventType, VariantMap& eventData)
  155. {
  156. EndFrame();
  157. }