perf_stats.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <iterator>
  7. #include <mutex>
  8. #include <numeric>
  9. #include <sstream>
  10. #include <thread>
  11. #include <fmt/chrono.h>
  12. #include <fmt/format.h>
  13. #include "common/file_util.h"
  14. #include "common/math_util.h"
  15. #include "core/perf_stats.h"
  16. #include "core/settings.h"
  17. using namespace std::chrono_literals;
  18. using DoubleSecs = std::chrono::duration<double, std::chrono::seconds::period>;
  19. using std::chrono::duration_cast;
  20. using std::chrono::microseconds;
  21. // Purposefully ignore the first five frames, as there's a significant amount of overhead in
  22. // booting that we shouldn't account for
  23. constexpr std::size_t IgnoreFrames = 5;
  24. namespace Core {
  25. PerfStats::PerfStats(u64 title_id) : title_id(title_id) {}
  26. PerfStats::~PerfStats() {
  27. if (!Settings::values.record_frame_times || title_id == 0) {
  28. return;
  29. }
  30. const std::time_t t = std::time(nullptr);
  31. std::ostringstream stream;
  32. std::copy(perf_history.begin() + IgnoreFrames, perf_history.begin() + current_index,
  33. std::ostream_iterator<double>(stream, "\n"));
  34. const std::string& path = FileUtil::GetUserPath(FileUtil::UserPath::LogDir);
  35. // %F Date format expanded is "%Y-%m-%d"
  36. const std::string filename =
  37. fmt::format("{}/{:%F-%H-%M}_{:016X}.csv", path, *std::localtime(&t), title_id);
  38. FileUtil::IOFile file(filename, "w");
  39. file.WriteString(stream.str());
  40. }
  41. void PerfStats::BeginSystemFrame() {
  42. std::lock_guard lock{object_mutex};
  43. frame_begin = Clock::now();
  44. }
  45. void PerfStats::EndSystemFrame() {
  46. std::lock_guard lock{object_mutex};
  47. auto frame_end = Clock::now();
  48. const auto frame_time = frame_end - frame_begin;
  49. if (current_index < perf_history.size()) {
  50. perf_history[current_index++] =
  51. std::chrono::duration<double, std::milli>(frame_time).count();
  52. }
  53. accumulated_frametime += frame_time;
  54. system_frames += 1;
  55. previous_frame_length = frame_end - previous_frame_end;
  56. previous_frame_end = frame_end;
  57. }
  58. void PerfStats::EndGameFrame() {
  59. std::lock_guard lock{object_mutex};
  60. game_frames += 1;
  61. }
  62. double PerfStats::GetMeanFrametime() {
  63. std::lock_guard lock{object_mutex};
  64. if (current_index <= IgnoreFrames) {
  65. return 0;
  66. }
  67. const double sum = std::accumulate(perf_history.begin() + IgnoreFrames,
  68. perf_history.begin() + current_index, 0.0);
  69. return sum / (current_index - IgnoreFrames);
  70. }
  71. PerfStatsResults PerfStats::GetAndResetStats(microseconds current_system_time_us) {
  72. std::lock_guard lock{object_mutex};
  73. const auto now = Clock::now();
  74. // Walltime elapsed since stats were reset
  75. const auto interval = duration_cast<DoubleSecs>(now - reset_point).count();
  76. const auto system_us_per_second = (current_system_time_us - reset_point_system_us) / interval;
  77. PerfStatsResults results{};
  78. results.system_fps = static_cast<double>(system_frames) / interval;
  79. results.game_fps = static_cast<double>(game_frames) / interval;
  80. results.frametime = duration_cast<DoubleSecs>(accumulated_frametime).count() /
  81. static_cast<double>(system_frames);
  82. results.emulation_speed = system_us_per_second.count() / 1'000'000.0;
  83. // Reset counters
  84. reset_point = now;
  85. reset_point_system_us = current_system_time_us;
  86. accumulated_frametime = Clock::duration::zero();
  87. system_frames = 0;
  88. game_frames = 0;
  89. return results;
  90. }
  91. double PerfStats::GetLastFrameTimeScale() {
  92. std::lock_guard lock{object_mutex};
  93. constexpr double FRAME_LENGTH = 1.0 / 60;
  94. return duration_cast<DoubleSecs>(previous_frame_length).count() / FRAME_LENGTH;
  95. }
  96. void FrameLimiter::DoFrameLimiting(microseconds current_system_time_us) {
  97. if (!Settings::values.use_frame_limit) {
  98. return;
  99. }
  100. auto now = Clock::now();
  101. const double sleep_scale = Settings::values.frame_limit / 100.0;
  102. // Max lag caused by slow frames. Shouldn't be more than the length of a frame at the current
  103. // speed percent or it will clamp too much and prevent this from properly limiting to that
  104. // percent. High values means it'll take longer after a slow frame to recover and start
  105. // limiting
  106. const microseconds max_lag_time_us = duration_cast<microseconds>(
  107. std::chrono::duration<double, std::chrono::microseconds::period>(25ms / sleep_scale));
  108. frame_limiting_delta_err += duration_cast<microseconds>(
  109. std::chrono::duration<double, std::chrono::microseconds::period>(
  110. (current_system_time_us - previous_system_time_us) / sleep_scale));
  111. frame_limiting_delta_err -= duration_cast<microseconds>(now - previous_walltime);
  112. frame_limiting_delta_err =
  113. std::clamp(frame_limiting_delta_err, -max_lag_time_us, max_lag_time_us);
  114. if (frame_limiting_delta_err > microseconds::zero()) {
  115. std::this_thread::sleep_for(frame_limiting_delta_err);
  116. auto now_after_sleep = Clock::now();
  117. frame_limiting_delta_err -= duration_cast<microseconds>(now_after_sleep - now);
  118. now = now_after_sleep;
  119. }
  120. previous_system_time_us = current_system_time_us;
  121. previous_walltime = now;
  122. }
  123. } // namespace Core