Profiler.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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. namespace Urho3D
  31. {
  32. static const int LINE_MAX_LENGTH = 256;
  33. static const int NAME_MAX_LENGTH = 30;
  34. OBJECTTYPESTATIC(Profiler);
  35. Profiler::Profiler(Context* context) :
  36. Object(context),
  37. current_(0),
  38. root_(0),
  39. intervalFrames_(0),
  40. totalFrames_(0)
  41. {
  42. root_ = new ProfilerBlock(0, "Root");
  43. current_ = root_;
  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. ++intervalFrames_;
  62. ++totalFrames_;
  63. if (!totalFrames_)
  64. ++totalFrames_;
  65. root_->EndFrame();
  66. current_ = root_;
  67. }
  68. }
  69. void Profiler::BeginInterval()
  70. {
  71. root_->BeginInterval();
  72. intervalFrames_ = 0;
  73. }
  74. String Profiler::GetData(bool showUnused, bool showTotal, unsigned maxDepth) const
  75. {
  76. String output;
  77. if (!showTotal)
  78. output += String("Block Cnt Avg Max Frame Total\n\n");
  79. else
  80. {
  81. output += String("Block Last frame Whole execution time\n\n");
  82. output += String(" Cnt Avg Max Total Cnt Avg Max Total\n\n");
  83. }
  84. if (!maxDepth)
  85. maxDepth = 1;
  86. GetData(root_, output, 0, maxDepth, showUnused, showTotal);
  87. return output;
  88. }
  89. void Profiler::GetData(ProfilerBlock* block, String& output, unsigned depth, unsigned maxDepth, bool showUnused, bool showTotal) const
  90. {
  91. char line[LINE_MAX_LENGTH];
  92. char indentedName[LINE_MAX_LENGTH];
  93. unsigned intervalFrames = Max(intervalFrames_, 1);
  94. if (depth >= maxDepth)
  95. return;
  96. // Do not print the root block as it does not collect any actual data
  97. if (block != root_)
  98. {
  99. if (showUnused || block->intervalCount_ || (showTotal && block->totalCount_))
  100. {
  101. memset(indentedName, ' ', NAME_MAX_LENGTH);
  102. indentedName[depth] = 0;
  103. strcat(indentedName, block->name_);
  104. indentedName[strlen(indentedName)] = ' ';
  105. indentedName[NAME_MAX_LENGTH] = 0;
  106. if (!showTotal)
  107. {
  108. float avg = (block->intervalCount_ ? block->intervalTime_ / block->intervalCount_ : 0.0f) / 1000.0f;
  109. float max = block->intervalMaxTime_ / 1000.0f;
  110. float frame = block->intervalTime_ / intervalFrames / 1000.0f;
  111. float all = block->intervalTime_ / 1000.0f;
  112. sprintf(line, "%s %5u %8.3f %8.3f %8.3f %9.3f\n", indentedName, block->intervalCount_, avg, max, frame, all);
  113. }
  114. else
  115. {
  116. float avg = (block->frameCount_ ? block->frameTime_ / block->frameCount_ : 0.0f) / 1000.0f;
  117. float max = block->frameMaxTime_ / 1000.0f;
  118. float all = block->frameTime_ / 1000.0f;
  119. float totalAvg = (block->totalCount_ ? block->totalTime_ / block->totalCount_ : 0.0f) / 1000.0f;
  120. float totalMax = block->totalMaxTime_ / 1000.0f;
  121. float totalAll = block->totalTime_ / 1000.0f;
  122. sprintf(line, "%s %5u %8.3f %8.3f %9.3f %7u %9.3f %9.3f %11.3f\n", indentedName, block->frameCount_, avg, max,
  123. all, block->totalCount_, totalAvg, totalMax, totalAll);
  124. }
  125. output += String(line);
  126. }
  127. ++depth;
  128. }
  129. for (PODVector<ProfilerBlock*>::ConstIterator i = block->children_.Begin(); i != block->children_.End(); ++i)
  130. GetData(*i, output, depth, maxDepth, showUnused, showTotal);
  131. }
  132. }