profiler.cpp 5.8 KB

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