perf_stats.cpp 5.7 KB

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