Profiler.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // Copyright (c) 2008-2015 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. #include "../Precompiled.h"
  23. #include "../Core/CoreEvents.h"
  24. #include "../Core/Profiler.h"
  25. #include <cstdio>
  26. #include "../DebugNew.h"
  27. namespace Atomic
  28. {
  29. static const int LINE_MAX_LENGTH = 256;
  30. static const int NAME_MAX_LENGTH = 30;
  31. Profiler::Profiler(Context* context) :
  32. Object(context),
  33. current_(0),
  34. root_(0),
  35. intervalFrames_(0),
  36. totalFrames_(0)
  37. {
  38. root_ = new ProfilerBlock(0, "Root");
  39. current_ = root_;
  40. }
  41. Profiler::~Profiler()
  42. {
  43. delete root_;
  44. root_ = 0;
  45. }
  46. void Profiler::BeginFrame()
  47. {
  48. // End the previous frame if any
  49. EndFrame();
  50. BeginBlock("RunFrame");
  51. }
  52. void Profiler::EndFrame()
  53. {
  54. if (current_ != root_)
  55. {
  56. EndBlock();
  57. ++intervalFrames_;
  58. ++totalFrames_;
  59. if (!totalFrames_)
  60. ++totalFrames_;
  61. root_->EndFrame();
  62. current_ = root_;
  63. }
  64. }
  65. void Profiler::BeginInterval()
  66. {
  67. root_->BeginInterval();
  68. intervalFrames_ = 0;
  69. }
  70. String Profiler::GetData(bool showUnused, bool showTotal, unsigned maxDepth) const
  71. {
  72. String output;
  73. if (!showTotal)
  74. output += "Block Cnt Avg Max Frame Total\n\n";
  75. else
  76. {
  77. output += "Block Last frame Whole execution time\n\n";
  78. output += " Cnt Avg Max Total Cnt Avg Max Total\n\n";
  79. }
  80. if (!maxDepth)
  81. maxDepth = 1;
  82. GetData(root_, output, 0, maxDepth, showUnused, showTotal);
  83. return output;
  84. }
  85. void Profiler::GetData(ProfilerBlock* block, String& output, unsigned depth, unsigned maxDepth, bool showUnused,
  86. bool showTotal) const
  87. {
  88. char line[LINE_MAX_LENGTH];
  89. char indentedName[LINE_MAX_LENGTH];
  90. unsigned intervalFrames = (unsigned)Max(intervalFrames_, 1);
  91. if (depth >= maxDepth)
  92. return;
  93. // Do not print the root block as it does not collect any actual data
  94. if (block != root_)
  95. {
  96. if (showUnused || block->intervalCount_ || (showTotal && block->totalCount_))
  97. {
  98. memset(indentedName, ' ', NAME_MAX_LENGTH);
  99. indentedName[depth] = 0;
  100. strcat(indentedName, block->name_);
  101. indentedName[strlen(indentedName)] = ' ';
  102. indentedName[NAME_MAX_LENGTH] = 0;
  103. if (!showTotal)
  104. {
  105. float avg = (block->intervalCount_ ? block->intervalTime_ / block->intervalCount_ : 0.0f) / 1000.0f;
  106. float max = block->intervalMaxTime_ / 1000.0f;
  107. float frame = block->intervalTime_ / intervalFrames / 1000.0f;
  108. float all = block->intervalTime_ / 1000.0f;
  109. sprintf(line, "%s %5u %8.3f %8.3f %8.3f %9.3f\n", indentedName, Min(block->intervalCount_, 99999),
  110. avg, max, frame, all);
  111. }
  112. else
  113. {
  114. float avg = (block->frameCount_ ? block->frameTime_ / block->frameCount_ : 0.0f) / 1000.0f;
  115. float max = block->frameMaxTime_ / 1000.0f;
  116. float all = block->frameTime_ / 1000.0f;
  117. float totalAvg = (block->totalCount_ ? block->totalTime_ / block->totalCount_ : 0.0f) / 1000.0f;
  118. float totalMax = block->totalMaxTime_ / 1000.0f;
  119. float totalAll = block->totalTime_ / 1000.0f;
  120. sprintf(line, "%s %5u %8.3f %8.3f %9.3f %7u %9.3f %9.3f %11.3f\n", indentedName, Min(block->frameCount_, 99999),
  121. avg, max, all, Min(block->totalCount_, 99999), totalAvg, totalMax, totalAll);
  122. }
  123. output += String(line);
  124. }
  125. ++depth;
  126. }
  127. for (PODVector<ProfilerBlock*>::ConstIterator i = block->children_.Begin(); i != block->children_.End(); ++i)
  128. GetData(*i, output, depth, maxDepth, showUnused, showTotal);
  129. }
  130. }