perf_stats.cpp 5.7 KB

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