Profiler.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Profiler.h"
  5. #include <cstdio>
  6. #include "../DebugNew.h"
  7. namespace Urho3D
  8. {
  9. Profiler::Profiler(Context* context) :
  10. Object(context),
  11. current_(nullptr),
  12. root_(nullptr),
  13. intervalFrames_(0)
  14. {
  15. current_ = root_ = new ProfilerBlock(nullptr, "RunFrame");
  16. }
  17. Profiler::~Profiler()
  18. {
  19. delete root_;
  20. root_ = nullptr;
  21. }
  22. void Profiler::BeginFrame()
  23. {
  24. // End the previous frame if any
  25. if (root_->count_)
  26. EndFrame();
  27. root_->Begin();
  28. }
  29. void Profiler::EndFrame()
  30. {
  31. EndBlock();
  32. ++intervalFrames_;
  33. root_->EndFrame();
  34. current_ = root_;
  35. }
  36. void Profiler::BeginInterval()
  37. {
  38. root_->BeginInterval();
  39. intervalFrames_ = 0;
  40. }
  41. const String& Profiler::PrintData(bool showUnused, bool showTotal, unsigned maxDepth) const
  42. {
  43. static String output;
  44. if (!showTotal)
  45. output = "Block Cnt Avg Max Frame Total\n\n";
  46. else
  47. {
  48. output = "Block Last frame Whole execution time\n\n";
  49. output += " Cnt Avg Max Total Cnt Avg Max Total\n\n";
  50. }
  51. if (!maxDepth)
  52. maxDepth = 1;
  53. PrintData(root_, output, 0, maxDepth, showUnused, showTotal);
  54. return output;
  55. }
  56. void Profiler::PrintData(ProfilerBlock* block, String& output, unsigned depth, unsigned maxDepth, bool showUnused,
  57. bool showTotal) const
  58. {
  59. static const int LINE_MAX_LENGTH = 256;
  60. static const int NAME_MAX_LENGTH = 30;
  61. char line[LINE_MAX_LENGTH];
  62. char indentedName[LINE_MAX_LENGTH];
  63. if (depth >= maxDepth)
  64. return;
  65. // Do not print any block that does not collect critical data
  66. if (showUnused || block->intervalCount_ || (showTotal && block->totalCount_))
  67. {
  68. memset(indentedName, ' ', NAME_MAX_LENGTH);
  69. indentedName[depth++] = 0;
  70. strncat(indentedName, block->name_, NAME_MAX_LENGTH - depth);
  71. indentedName[strlen(indentedName)] = ' ';
  72. indentedName[NAME_MAX_LENGTH] = 0;
  73. if (!showTotal)
  74. {
  75. float avg = (float)block->intervalTime_ / block->intervalCount_ / 1000.0f;
  76. float max = block->intervalMaxTime_ / 1000.0f;
  77. float frame = (float)block->intervalTime_ / (intervalFrames_ ? intervalFrames_ : 1) / 1000.0f;
  78. float all = block->intervalTime_ / 1000.0f;
  79. sprintf(line, "%s %5u %8.3f %8.3f %8.3f %9.3f\n", indentedName, Min(block->intervalCount_, 99999U),
  80. avg, max, frame, all);
  81. }
  82. else
  83. {
  84. float avg = (block->frameCount_ ? (float)block->frameTime_ / block->frameCount_ : 0.0f) / 1000.0f;
  85. float max = block->frameMaxTime_ / 1000.0f;
  86. float all = block->frameTime_ / 1000.0f;
  87. float totalAvg = (float)block->totalTime_ / block->totalCount_ / 1000.0f;
  88. float totalMax = block->totalMaxTime_ / 1000.0f;
  89. float totalAll = block->totalTime_ / 1000.0f;
  90. sprintf(line, "%s %5u %8.3f %8.3f %9.3f %7u %9.3f %9.3f %11.3f\n", indentedName, Min(block->frameCount_, 99999U),
  91. avg, max, all, Min(block->totalCount_, 99999U), totalAvg, totalMax, totalAll);
  92. }
  93. output += String(line);
  94. }
  95. for (Vector<ProfilerBlock*>::ConstIterator i = block->children_.Begin(); i != block->children_.End(); ++i)
  96. PrintData(*i, output, depth, maxDepth, showUnused, showTotal);
  97. }
  98. }