profiler.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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_reporting.h"
  9. #include "common/synchronized_wrapper.h"
  10. namespace Common {
  11. namespace Profiling {
  12. ProfilingManager::ProfilingManager()
  13. : last_frame_end(Clock::now()), this_frame_start(Clock::now()) {
  14. }
  15. void ProfilingManager::BeginFrame() {
  16. this_frame_start = Clock::now();
  17. }
  18. void ProfilingManager::FinishFrame() {
  19. Clock::time_point now = Clock::now();
  20. results.interframe_time = now - last_frame_end;
  21. results.frame_time = now - this_frame_start;
  22. last_frame_end = now;
  23. }
  24. TimingResultsAggregator::TimingResultsAggregator(size_t window_size)
  25. : max_window_size(window_size), window_size(0) {
  26. interframe_times.resize(window_size, Duration::zero());
  27. frame_times.resize(window_size, Duration::zero());
  28. }
  29. void TimingResultsAggregator::Clear() {
  30. window_size = cursor = 0;
  31. }
  32. void TimingResultsAggregator::AddFrame(const ProfilingFrameResult& frame_result) {
  33. interframe_times[cursor] = frame_result.interframe_time;
  34. frame_times[cursor] = frame_result.frame_time;
  35. ++cursor;
  36. if (cursor == max_window_size)
  37. cursor = 0;
  38. if (window_size < max_window_size)
  39. ++window_size;
  40. }
  41. static AggregatedDuration AggregateField(const std::vector<Duration>& v, size_t len) {
  42. AggregatedDuration result;
  43. result.avg = Duration::zero();
  44. result.min = result.max = (len == 0 ? Duration::zero() : v[0]);
  45. for (size_t i = 0; i < len; ++i) {
  46. Duration value = v[i];
  47. result.avg += value;
  48. result.min = std::min(result.min, value);
  49. result.max = std::max(result.max, value);
  50. }
  51. if (len != 0)
  52. result.avg /= len;
  53. return result;
  54. }
  55. static float tof(Common::Profiling::Duration dur) {
  56. using FloatMs = std::chrono::duration<float, std::chrono::milliseconds::period>;
  57. return std::chrono::duration_cast<FloatMs>(dur).count();
  58. }
  59. AggregatedFrameResult TimingResultsAggregator::GetAggregatedResults() const {
  60. AggregatedFrameResult result;
  61. result.interframe_time = AggregateField(interframe_times, window_size);
  62. result.frame_time = AggregateField(frame_times, window_size);
  63. if (result.interframe_time.avg != Duration::zero()) {
  64. result.fps = 1000.0f / tof(result.interframe_time.avg);
  65. } else {
  66. result.fps = 0.0f;
  67. }
  68. return result;
  69. }
  70. ProfilingManager& GetProfilingManager() {
  71. // Takes advantage of "magic" static initialization for race-free initialization.
  72. static ProfilingManager manager;
  73. return manager;
  74. }
  75. SynchronizedRef<TimingResultsAggregator> GetTimingResultsAggregator() {
  76. static SynchronizedWrapper<TimingResultsAggregator> aggregator(30);
  77. return SynchronizedRef<TimingResultsAggregator>(aggregator);
  78. }
  79. } // namespace Profiling
  80. } // namespace Common