profiler.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/profiler.h"
  5. #include "common/profiler_reporting.h"
  6. #include "common/assert.h"
  7. #if defined(_MSC_VER) && _MSC_VER <= 1800 // MSVC 2013.
  8. #define WIN32_LEAN_AND_MEAN
  9. #include <Windows.h> // For QueryPerformanceCounter/Frequency
  10. #endif
  11. namespace Common {
  12. namespace Profiling {
  13. #if ENABLE_PROFILING
  14. thread_local Timer* Timer::current_timer = nullptr;
  15. #endif
  16. #if defined(_MSC_VER) && _MSC_VER <= 1800 // MSVC 2013
  17. QPCClock::time_point QPCClock::now() {
  18. static LARGE_INTEGER freq;
  19. // Use this dummy local static to ensure this gets initialized once.
  20. static BOOL dummy = QueryPerformanceFrequency(&freq);
  21. LARGE_INTEGER ticks;
  22. QueryPerformanceCounter(&ticks);
  23. // This is prone to overflow when multiplying, which is why I'm using micro instead of nano. The
  24. // correct way to approach this would be to just return ticks as a time_point and then subtract
  25. // and do this conversion when creating a duration from two time_points, however, as far as I
  26. // could tell the C++ requirements for these types are incompatible with this approach.
  27. return time_point(duration(ticks.QuadPart * std::micro::den / freq.QuadPart));
  28. }
  29. #endif
  30. TimingCategory::TimingCategory(const char* name, TimingCategory* parent)
  31. : accumulated_duration(0) {
  32. ProfilingManager& manager = GetProfilingManager();
  33. category_id = manager.RegisterTimingCategory(this, name);
  34. if (parent != nullptr)
  35. manager.SetTimingCategoryParent(category_id, parent->category_id);
  36. }
  37. ProfilingManager::ProfilingManager()
  38. : last_frame_end(Clock::now()), this_frame_start(Clock::now()) {
  39. }
  40. unsigned int ProfilingManager::RegisterTimingCategory(TimingCategory* category, const char* name) {
  41. TimingCategoryInfo info;
  42. info.category = category;
  43. info.name = name;
  44. info.parent = TimingCategoryInfo::NO_PARENT;
  45. unsigned int id = (unsigned int)timing_categories.size();
  46. timing_categories.push_back(std::move(info));
  47. return id;
  48. }
  49. void ProfilingManager::SetTimingCategoryParent(unsigned int category, unsigned int parent) {
  50. ASSERT(category < timing_categories.size());
  51. ASSERT(parent < timing_categories.size());
  52. timing_categories[category].parent = parent;
  53. }
  54. void ProfilingManager::BeginFrame() {
  55. this_frame_start = Clock::now();
  56. }
  57. void ProfilingManager::FinishFrame() {
  58. Clock::time_point now = Clock::now();
  59. results.interframe_time = now - last_frame_end;
  60. results.frame_time = now - this_frame_start;
  61. results.time_per_category.resize(timing_categories.size());
  62. for (size_t i = 0; i < timing_categories.size(); ++i) {
  63. results.time_per_category[i] = timing_categories[i].category->GetAccumulatedTime();
  64. }
  65. last_frame_end = now;
  66. }
  67. TimingResultsAggregator::TimingResultsAggregator(size_t window_size)
  68. : max_window_size(window_size), window_size(0) {
  69. interframe_times.resize(window_size, Duration::zero());
  70. frame_times.resize(window_size, Duration::zero());
  71. }
  72. void TimingResultsAggregator::Clear() {
  73. window_size = cursor = 0;
  74. }
  75. void TimingResultsAggregator::SetNumberOfCategories(size_t n) {
  76. size_t old_size = times_per_category.size();
  77. if (n == old_size)
  78. return;
  79. times_per_category.resize(n);
  80. for (size_t i = old_size; i < n; ++i) {
  81. times_per_category[i].resize(max_window_size, Duration::zero());
  82. }
  83. }
  84. void TimingResultsAggregator::AddFrame(const ProfilingFrameResult& frame_result) {
  85. SetNumberOfCategories(frame_result.time_per_category.size());
  86. interframe_times[cursor] = frame_result.interframe_time;
  87. frame_times[cursor] = frame_result.frame_time;
  88. for (size_t i = 0; i < frame_result.time_per_category.size(); ++i) {
  89. times_per_category[i][cursor] = frame_result.time_per_category[i];
  90. }
  91. ++cursor;
  92. if (cursor == max_window_size)
  93. cursor = 0;
  94. if (window_size < max_window_size)
  95. ++window_size;
  96. }
  97. static AggregatedDuration AggregateField(const std::vector<Duration>& v, size_t len) {
  98. AggregatedDuration result;
  99. result.avg = Duration::zero();
  100. result.min = result.max = (len == 0 ? Duration::zero() : v[0]);
  101. for (size_t i = 1; i < len; ++i) {
  102. Duration value = v[i];
  103. result.avg += value;
  104. result.min = std::min(result.min, value);
  105. result.max = std::max(result.max, value);
  106. }
  107. if (len != 0)
  108. result.avg /= len;
  109. return result;
  110. }
  111. static float tof(Common::Profiling::Duration dur) {
  112. using FloatMs = std::chrono::duration<float, std::chrono::milliseconds::period>;
  113. return std::chrono::duration_cast<FloatMs>(dur).count();
  114. }
  115. AggregatedFrameResult TimingResultsAggregator::GetAggregatedResults() const {
  116. AggregatedFrameResult result;
  117. result.interframe_time = AggregateField(interframe_times, window_size);
  118. result.frame_time = AggregateField(frame_times, window_size);
  119. if (result.interframe_time.avg != Duration::zero()) {
  120. result.fps = 1000.0f / tof(result.interframe_time.avg);
  121. } else {
  122. result.fps = 0.0f;
  123. }
  124. result.time_per_category.resize(times_per_category.size());
  125. for (size_t i = 0; i < times_per_category.size(); ++i) {
  126. result.time_per_category[i] = AggregateField(times_per_category[i], window_size);
  127. }
  128. return result;
  129. }
  130. ProfilingManager& GetProfilingManager() {
  131. // Takes advantage of "magic" static initialization for race-free initialization.
  132. static ProfilingManager manager;
  133. return manager;
  134. }
  135. SynchronizedRef<TimingResultsAggregator> GetTimingResultsAggregator() {
  136. static SynchronizedWrapper<TimingResultsAggregator> aggregator(30);
  137. return SynchronizedRef<TimingResultsAggregator>(aggregator);
  138. }
  139. } // namespace Profiling
  140. } // namespace Common