perf_stats.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <atomic>
  7. #include <chrono>
  8. #include <cstddef>
  9. #include <mutex>
  10. #include "common/common_types.h"
  11. namespace Core {
  12. struct PerfStatsResults {
  13. /// System FPS (LCD VBlanks) in Hz
  14. double system_fps;
  15. /// Average game FPS (GPU frame renders) in Hz
  16. double average_game_fps;
  17. /// Walltime per system frame, in seconds, excluding any waits
  18. double frametime;
  19. /// Ratio of walltime / emulated time elapsed
  20. double emulation_speed;
  21. };
  22. /**
  23. * Class to manage and query performance/timing statistics. All public functions of this class are
  24. * thread-safe unless stated otherwise.
  25. */
  26. class PerfStats {
  27. public:
  28. explicit PerfStats(u64 title_id_);
  29. ~PerfStats();
  30. using Clock = std::chrono::steady_clock;
  31. void BeginSystemFrame();
  32. void EndSystemFrame();
  33. void EndGameFrame();
  34. PerfStatsResults GetAndResetStats(std::chrono::microseconds current_system_time_us);
  35. /**
  36. * Returns the arithmetic mean of all frametime values stored in the performance history.
  37. */
  38. double GetMeanFrametime() const;
  39. /**
  40. * Gets the ratio between walltime and the emulated time of the previous system frame. This is
  41. * useful for scaling inputs or outputs moving between the two time domains.
  42. */
  43. double GetLastFrameTimeScale() const;
  44. private:
  45. mutable std::mutex object_mutex;
  46. /// Title ID for the game that is running. 0 if there is no game running yet
  47. u64 title_id{0};
  48. /// Current index for writing to the perf_history array
  49. std::size_t current_index{0};
  50. /// Stores an hour of historical frametime data useful for processing and tracking performance
  51. /// regressions with code changes.
  52. std::array<double, 216000> perf_history{};
  53. /// Point when the cumulative counters were reset
  54. Clock::time_point reset_point = Clock::now();
  55. /// System time when the cumulative counters were reset
  56. std::chrono::microseconds reset_point_system_us{0};
  57. /// Cumulative duration (excluding v-sync/frame-limiting) of frames since last reset
  58. Clock::duration accumulated_frametime = Clock::duration::zero();
  59. /// Cumulative number of system frames (LCD VBlanks) presented since last reset
  60. u32 system_frames = 0;
  61. /// Cumulative number of game frames (GSP frame submissions) since last reset
  62. std::atomic<u32> game_frames = 0;
  63. /// Point when the previous system frame ended
  64. Clock::time_point previous_frame_end = reset_point;
  65. /// Point when the current system frame began
  66. Clock::time_point frame_begin = reset_point;
  67. /// Total visible duration (including frame-limiting, etc.) of the previous system frame
  68. Clock::duration previous_frame_length = Clock::duration::zero();
  69. /// Previously computed fps
  70. double previous_fps = 0;
  71. };
  72. class SpeedLimiter {
  73. public:
  74. using Clock = std::chrono::steady_clock;
  75. void DoSpeedLimiting(std::chrono::microseconds current_system_time_us);
  76. private:
  77. /// Emulated system time (in microseconds) at the last limiter invocation
  78. std::chrono::microseconds previous_system_time_us{0};
  79. /// Walltime at the last limiter invocation
  80. Clock::time_point previous_walltime = Clock::now();
  81. /// Accumulated difference between walltime and emulated time
  82. std::chrono::microseconds speed_limiting_delta_err{0};
  83. };
  84. } // namespace Core