EventProfiler.cpp 5.4 KB

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