perf_stats.cpp 5.8 KB

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