profiler.cpp 2.9 KB

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