Profiler.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 "Exception.h"
  25. #include "MathDefs.h"
  26. #include "Profiler.h"
  27. #include <cstdio>
  28. #include <cstdlib>
  29. #include <cstring>
  30. #include <ctype.h>
  31. #include "DebugNew.h"
  32. Profiler* Profiler::sInstance = 0;
  33. Profiler::Profiler() :
  34. mRoot(0),
  35. mCurrent(0),
  36. mAccumulatedFrames(0),
  37. mTotalFrames(0)
  38. {
  39. if (sInstance)
  40. EXCEPTION("Profiler already exists");
  41. initHiresTimer();
  42. mRoot = new ProfilerBlock(0, "Root");
  43. mCurrent = mRoot;
  44. sInstance = this;
  45. }
  46. Profiler::~Profiler()
  47. {
  48. delete mRoot;
  49. mRoot = 0;
  50. mCurrent = 0;
  51. if (sInstance == this)
  52. sInstance = 0;
  53. }
  54. void Profiler::beginFrame()
  55. {
  56. mCurrent = mRoot;
  57. mAccumulatedFrames++;
  58. mTotalFrames++;
  59. }
  60. void Profiler::endFrame()
  61. {
  62. mRoot->endFrame();
  63. }
  64. void Profiler::clearAccumulated()
  65. {
  66. mRoot->clearAccumulated();
  67. mAccumulatedFrames = 0;
  68. }
  69. std::string Profiler::getData(bool showUnused, bool showAccumulated, bool showTotal) const
  70. {
  71. std::string output;
  72. if (!showTotal)
  73. output.append("Block Count Average Total\n \n");
  74. else
  75. output.append("Block Frame average (msec) Accumulated total (sec)\n \n");
  76. getData(mRoot, output, 0, showUnused, showAccumulated, showTotal);
  77. return output;
  78. }
  79. void Profiler::getData(ProfilerBlock* block, std::string& output, unsigned indent, bool showUnused, bool showAccumulated, bool showTotal) const
  80. {
  81. static const int LINE_MAX_LENGTH = 256;
  82. static const int NAME_MAX_LENGTH = 40;
  83. char line[LINE_MAX_LENGTH];
  84. char indentedName[LINE_MAX_LENGTH];
  85. unsigned frames = max(mTotalFrames, 1);
  86. unsigned accumulatedFrames = max(mAccumulatedFrames, 1);
  87. // Do not print the root block as it does not collect any actual data
  88. if (block != mRoot)
  89. {
  90. // Output data in milliseconds
  91. float frameTime = block->getFrameTime() / 1000.0f;
  92. float accumulatedTime = block->getAccumulatedTime() / 1000.0f;
  93. float totalTime = block->getTotalTime() / 1000.0f;
  94. unsigned frameCount = block->getFrameCount();
  95. unsigned accumulatedCount = block->getAccumulatedCount();
  96. unsigned totalCount = block->getTotalCount();
  97. float avgFrameTime = 0.0f;
  98. float avgAccumulatedTime = 0.0f;
  99. float avgTotalTime = 0.0f;
  100. if (frameCount)
  101. avgFrameTime = frameTime / frameCount;
  102. if (accumulatedCount)
  103. avgAccumulatedTime = accumulatedTime / accumulatedCount;
  104. if (totalCount)
  105. avgTotalTime = totalTime / totalCount;
  106. if ((showUnused) || (frameCount) || ((showAccumulated) && (accumulatedCount)))
  107. {
  108. memset(indentedName, ' ', NAME_MAX_LENGTH);
  109. indentedName[indent] = 0;
  110. strcat(indentedName, block->getName());
  111. indentedName[strlen(indentedName)] = ' ';
  112. indentedName[NAME_MAX_LENGTH] = 0;
  113. if (!showTotal)
  114. {
  115. if (!showAccumulated)
  116. sprintf(line, "%s %5u %8.3f %8.3f\n", indentedName, frameCount, avgFrameTime, frameTime);
  117. else
  118. sprintf(line, "%s %5u %8.3f %8.3f\n", indentedName,
  119. (unsigned)((float)accumulatedCount / accumulatedFrames + 0.5f),
  120. avgAccumulatedTime, accumulatedTime / accumulatedFrames);
  121. }
  122. else
  123. {
  124. unsigned avgFrameCount = (unsigned)((float)totalCount / frames + 0.5f);
  125. float avgFrameTime = avgTotalTime;
  126. float avgFrameTotalTime = totalTime / frames;
  127. if (!avgFrameCount)
  128. {
  129. avgFrameTime = 0.0f;
  130. avgFrameTotalTime = 0.0f;
  131. }
  132. sprintf(line, "%s %5u %8.3f %8.3f %10u %8.3f %8.3f\n",
  133. indentedName, avgFrameCount, avgFrameTime, avgFrameTotalTime,
  134. totalCount, avgTotalTime / 1000.0f, totalTime / 1000.0);
  135. }
  136. output.append(std::string(line));
  137. indent++;
  138. }
  139. }
  140. const std::vector<ProfilerBlock*>& children = block->getChildren();
  141. for (std::vector<ProfilerBlock*>::const_iterator i = children.begin(); i != children.end(); ++i)
  142. getData(*i, output, indent, showUnused, showAccumulated, showTotal);
  143. }