perf_stats.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 = Common::FS::GetUserPath(Common::FS::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. Common::FS::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() const {
  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 / static_cast<double>(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. const PerfStatsResults results{
  78. .system_fps = static_cast<double>(system_frames) / interval,
  79. .game_fps = static_cast<double>(game_frames) / interval,
  80. .frametime = duration_cast<DoubleSecs>(accumulated_frametime).count() /
  81. static_cast<double>(system_frames),
  82. .emulation_speed = system_us_per_second.count() / 1'000'000.0,
  83. };
  84. // Reset counters
  85. reset_point = now;
  86. reset_point_system_us = current_system_time_us;
  87. accumulated_frametime = Clock::duration::zero();
  88. system_frames = 0;
  89. game_frames = 0;
  90. return results;
  91. }
  92. double PerfStats::GetLastFrameTimeScale() const {
  93. std::lock_guard lock{object_mutex};
  94. constexpr double FRAME_LENGTH = 1.0 / 60;
  95. return duration_cast<DoubleSecs>(previous_frame_length).count() / FRAME_LENGTH;
  96. }
  97. void FrameLimiter::DoFrameLimiting(microseconds current_system_time_us) {
  98. if (!Settings::values.use_frame_limit.GetValue() ||
  99. Settings::values.use_multi_core.GetValue()) {
  100. return;
  101. }
  102. auto now = Clock::now();
  103. const double sleep_scale = Settings::values.frame_limit.GetValue() / 100.0;
  104. // Max lag caused by slow frames. Shouldn't be more than the length of a frame at the current
  105. // speed percent or it will clamp too much and prevent this from properly limiting to that
  106. // percent. High values means it'll take longer after a slow frame to recover and start
  107. // limiting
  108. const microseconds max_lag_time_us = duration_cast<microseconds>(
  109. std::chrono::duration<double, std::chrono::microseconds::period>(25ms / sleep_scale));
  110. frame_limiting_delta_err += duration_cast<microseconds>(
  111. std::chrono::duration<double, std::chrono::microseconds::period>(
  112. (current_system_time_us - previous_system_time_us) / sleep_scale));
  113. frame_limiting_delta_err -= duration_cast<microseconds>(now - previous_walltime);
  114. frame_limiting_delta_err =
  115. std::clamp(frame_limiting_delta_err, -max_lag_time_us, max_lag_time_us);
  116. if (frame_limiting_delta_err > microseconds::zero()) {
  117. std::this_thread::sleep_for(frame_limiting_delta_err);
  118. auto now_after_sleep = Clock::now();
  119. frame_limiting_delta_err -= duration_cast<microseconds>(now_after_sleep - now);
  120. now = now_after_sleep;
  121. }
  122. previous_system_time_us = current_system_time_us;
  123. previous_walltime = now;
  124. }
  125. } // namespace Core