Profiler.cpp 4.6 KB

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