perf_stats.h 3.3 KB

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