perf_stats.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // SPDX-FileCopyrightText: 2017 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <atomic>
  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. /// Average game FPS (GPU frame renders) in Hz
  15. double average_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::steady_clock;
  30. void BeginSystemFrame();
  31. void EndSystemFrame();
  32. void EndGameFrame();
  33. PerfStatsResults GetAndResetStats(std::chrono::microseconds current_system_time_us);
  34. /**
  35. * Returns the arithmetic mean of all frametime values stored in the performance history.
  36. */
  37. double GetMeanFrametime() const;
  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() const;
  43. private:
  44. mutable 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. std::atomic<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. /// Previously computed fps
  69. double previous_fps = 0;
  70. };
  71. class SpeedLimiter {
  72. public:
  73. using Clock = std::chrono::steady_clock;
  74. void DoSpeedLimiting(std::chrono::microseconds current_system_time_us);
  75. private:
  76. /// Emulated system time (in microseconds) at the last limiter invocation
  77. std::chrono::microseconds previous_system_time_us{0};
  78. /// Walltime at the last limiter invocation
  79. Clock::time_point previous_walltime = Clock::now();
  80. /// Accumulated difference between walltime and emulated time
  81. std::chrono::microseconds speed_limiting_delta_err{0};
  82. };
  83. } // namespace Core