perf_stats.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <chrono>
  6. #include <mutex>
  7. #include <thread>
  8. #include "common/math_util.h"
  9. #include "core/perf_stats.h"
  10. #include "core/settings.h"
  11. using namespace std::chrono_literals;
  12. using DoubleSecs = std::chrono::duration<double, std::chrono::seconds::period>;
  13. using std::chrono::duration_cast;
  14. using std::chrono::microseconds;
  15. namespace Core {
  16. void PerfStats::BeginSystemFrame() {
  17. std::lock_guard<std::mutex> lock(object_mutex);
  18. frame_begin = Clock::now();
  19. }
  20. void PerfStats::EndSystemFrame() {
  21. std::lock_guard<std::mutex> lock(object_mutex);
  22. auto frame_end = Clock::now();
  23. accumulated_frametime += frame_end - frame_begin;
  24. system_frames += 1;
  25. previous_frame_length = frame_end - previous_frame_end;
  26. previous_frame_end = frame_end;
  27. }
  28. void PerfStats::EndGameFrame() {
  29. std::lock_guard<std::mutex> lock(object_mutex);
  30. game_frames += 1;
  31. }
  32. PerfStats::Results PerfStats::GetAndResetStats(microseconds current_system_time_us) {
  33. std::lock_guard<std::mutex> lock(object_mutex);
  34. const auto now = Clock::now();
  35. // Walltime elapsed since stats were reset
  36. const auto interval = duration_cast<DoubleSecs>(now - reset_point).count();
  37. const auto system_us_per_second = (current_system_time_us - reset_point_system_us) / interval;
  38. Results results{};
  39. results.system_fps = static_cast<double>(system_frames) / interval;
  40. results.game_fps = static_cast<double>(game_frames) / interval;
  41. results.frametime = duration_cast<DoubleSecs>(accumulated_frametime).count() /
  42. static_cast<double>(system_frames);
  43. results.emulation_speed = system_us_per_second.count() / 1'000'000.0;
  44. // Reset counters
  45. reset_point = now;
  46. reset_point_system_us = current_system_time_us;
  47. accumulated_frametime = Clock::duration::zero();
  48. system_frames = 0;
  49. game_frames = 0;
  50. return results;
  51. }
  52. double PerfStats::GetLastFrameTimeScale() {
  53. std::lock_guard<std::mutex> lock(object_mutex);
  54. constexpr double FRAME_LENGTH = 1.0 / 60;
  55. return duration_cast<DoubleSecs>(previous_frame_length).count() / FRAME_LENGTH;
  56. }
  57. void FrameLimiter::DoFrameLimiting(microseconds current_system_time_us) {
  58. // Max lag caused by slow frames. Can be adjusted to compensate for too many slow frames. Higher
  59. // values increase the time needed to recover and limit framerate again after spikes.
  60. constexpr microseconds MAX_LAG_TIME_US = 25000us;
  61. if (!Settings::values.use_frame_limit) {
  62. return;
  63. }
  64. auto now = Clock::now();
  65. const double sleep_scale = Settings::values.frame_limit / 100.0;
  66. // Max lag caused by slow frames. Shouldn't be more than the length of a frame at the current
  67. // speed percent or it will clamp too much and prevent this from properly limiting to that
  68. // percent. High values means it'll take longer after a slow frame to recover and start
  69. // limiting
  70. const microseconds max_lag_time_us = duration_cast<microseconds>(
  71. std::chrono::duration<double, std::chrono::microseconds::period>(25ms / sleep_scale));
  72. frame_limiting_delta_err += duration_cast<microseconds>(
  73. std::chrono::duration<double, std::chrono::microseconds::period>(
  74. (current_system_time_us - previous_system_time_us) / sleep_scale));
  75. frame_limiting_delta_err -= duration_cast<microseconds>(now - previous_walltime);
  76. frame_limiting_delta_err =
  77. std::clamp(frame_limiting_delta_err, -max_lag_time_us, max_lag_time_us);
  78. if (frame_limiting_delta_err > microseconds::zero()) {
  79. std::this_thread::sleep_for(frame_limiting_delta_err);
  80. auto now_after_sleep = Clock::now();
  81. frame_limiting_delta_err -= duration_cast<microseconds>(now_after_sleep - now);
  82. now = now_after_sleep;
  83. }
  84. previous_system_time_us = current_system_time_us;
  85. previous_walltime = now;
  86. }
  87. } // namespace Core