perf_stats.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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(u64 current_system_time_us) {
  33. std::lock_guard<std::mutex> lock(object_mutex);
  34. auto now = Clock::now();
  35. // Walltime elapsed since stats were reset
  36. auto interval = duration_cast<DoubleSecs>(now - reset_point).count();
  37. auto system_us_per_second =
  38. static_cast<double>(current_system_time_us - reset_point_system_us) / interval;
  39. Results results{};
  40. results.system_fps = static_cast<double>(system_frames) / interval;
  41. results.game_fps = static_cast<double>(game_frames) / interval;
  42. results.frametime = duration_cast<DoubleSecs>(accumulated_frametime).count() /
  43. static_cast<double>(system_frames);
  44. results.emulation_speed = system_us_per_second / 1'000'000.0;
  45. // Reset counters
  46. reset_point = now;
  47. reset_point_system_us = current_system_time_us;
  48. accumulated_frametime = Clock::duration::zero();
  49. system_frames = 0;
  50. game_frames = 0;
  51. return results;
  52. }
  53. double PerfStats::GetLastFrameTimeScale() {
  54. std::lock_guard<std::mutex> lock(object_mutex);
  55. constexpr double FRAME_LENGTH = 1.0 / 60;
  56. return duration_cast<DoubleSecs>(previous_frame_length).count() / FRAME_LENGTH;
  57. }
  58. void FrameLimiter::DoFrameLimiting(u64 current_system_time_us) {
  59. // Max lag caused by slow frames. Can be adjusted to compensate for too many slow frames. Higher
  60. // values increase the time needed to recover and limit framerate again after spikes.
  61. constexpr microseconds MAX_LAG_TIME_US = 25ms;
  62. if (!Settings::values.toggle_framelimit) {
  63. return;
  64. }
  65. auto now = Clock::now();
  66. frame_limiting_delta_err += microseconds(current_system_time_us - previous_system_time_us);
  67. frame_limiting_delta_err -= duration_cast<microseconds>(now - previous_walltime);
  68. frame_limiting_delta_err =
  69. std::clamp(frame_limiting_delta_err, -MAX_LAG_TIME_US, MAX_LAG_TIME_US);
  70. if (frame_limiting_delta_err > microseconds::zero()) {
  71. std::this_thread::sleep_for(frame_limiting_delta_err);
  72. auto now_after_sleep = Clock::now();
  73. frame_limiting_delta_err -= duration_cast<microseconds>(now_after_sleep - now);
  74. now = now_after_sleep;
  75. }
  76. previous_system_time_us = current_system_time_us;
  77. previous_walltime = now;
  78. }
  79. } // namespace Core