| 1 | // Copyright 2022 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/PerformanceTracker.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <cmath> |
| 8 | #include <iomanip> |
| 9 | #include <utility> |
| 10 | |
| 11 | #include <implot.h> |
| 12 | |
| 13 | #include "Common/CommonTypes.h" |
| 14 | #include "Common/FileUtil.h" |
| 15 | #include "Common/MathUtil.h" |
| 16 | #include "VideoCommon/VideoConfig.h" |
| 17 | |
| 18 | static constexpr double SAMPLE_RC_RATIO = 0.25; |
| 19 | static constexpr u64 MAX_DT_QUEUE_SIZE = 1UL << 12; |
| 20 | static constexpr u64 MAX_QUALITY_GRAPH_SIZE = 1UL << 8; |
| 21 | |
| 22 | PerformanceTracker::PerformanceTracker(std::optional<std::string> log_name, |
| 23 | const std::optional<DT> sample_window_duration) |
| 24 | : m_log_name{std::move(log_name)}, m_sample_window_duration{sample_window_duration} |
| 25 | { |
| 26 | Reset(); |
| 27 | } |
| 28 | |
| 29 | void PerformanceTracker::Reset() |
| 30 | { |
| 31 | m_raw_dts.Clear(); |
| 32 | m_dt_queue.clear(); |
| 33 | |
| 34 | m_dt_total = DT::zero(); |
| 35 | m_last_raw_dt = DT::zero(); |
| 36 | m_last_time = Clock::now(); |
| 37 | m_hz_avg = 0.0; |
| 38 | m_dt_avg = DT::zero(); |
| 39 | m_dt_std = DT::zero(); |
| 40 | m_is_last_time_sane = false; |
| 41 | } |
| 42 | |
| 43 | void PerformanceTracker::Count() |
| 44 | { |
| 45 | const TimePoint current_time{Clock::now()}; |
| 46 | |
| 47 | const DT diff{current_time - m_last_time}; |
| 48 | m_last_time = current_time; |
| 49 | |
| 50 | if (!m_is_last_time_sane) |
| 51 | { |
| 52 | m_is_last_time_sane = true; |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | m_last_raw_dt = diff; |
| 57 | m_raw_dts.Push(diff); |
| 58 | } |
| 59 | |
| 60 | void PerformanceTracker::UpdateStats() |
| 61 | { |
| 62 | DT diff{}; |
| 63 | while (m_raw_dts.Pop(diff)) |
| 64 | HandleRawDt(diff); |
| 65 | |
| 66 | // Update Std Dev |
| 67 | MathUtil::RunningVariance<double> variance; |
| 68 | for (auto dt : m_dt_queue) |
| 69 | variance.Push(DT_s(dt).count()); |
| 70 | m_dt_std = std::chrono::duration_cast<DT>(DT_s(variance.PopulationStandardDeviation())); |
| 71 | } |
| 72 | |
| 73 | void PerformanceTracker::HandleRawDt(DT diff) |
| 74 | { |
| 75 | if (m_dt_queue.size() == MAX_DT_QUEUE_SIZE) |
| 76 | PopBack(); |
| 77 | |
| 78 | PushFront(diff); |
| 79 | |
| 80 | const DT window{GetSampleWindow()}; |
| 81 | |
| 82 | while (m_dt_total - m_dt_queue.back() >= window) |
| 83 | PopBack(); |
| 84 | |
| 85 | // Simple Moving Average Throughout the Window |
| 86 | const DT dt_avg = m_dt_total / m_dt_queue.size(); |
| 87 | const double hz = DT_s(1.0) / dt_avg; |
| 88 | m_dt_avg = dt_avg; |
| 89 | |
| 90 | // Exponential Moving Average |
| 91 | const DT_s rc = SAMPLE_RC_RATIO * std::min(window, m_dt_total); |
| 92 | const double a = 1.0 - std::exp(-(DT_s(diff) / rc)); |
| 93 | |
| 94 | // Sometimes euler averages can break when the average is inf/nan |
| 95 | const auto hz_avg = m_hz_avg.load(); |
| 96 | if (std::isfinite(hz_avg)) |
| 97 | m_hz_avg = hz_avg + a * (hz - hz_avg); |
| 98 | else |
| 99 | m_hz_avg = hz; |
| 100 | |
| 101 | LogRenderTimeToFile(diff); |
| 102 | } |
| 103 | |
| 104 | DT PerformanceTracker::GetSampleWindow() const |
| 105 | { |
| 106 | return m_sample_window_duration.value_or( |
| 107 | duration_cast<DT>(DT_us{std::max(1, g_ActiveConfig.iPerfSampleUSec)})); |
| 108 | } |
| 109 | |
| 110 | double PerformanceTracker::GetHzAvg() const |
| 111 | { |
| 112 | return m_hz_avg; |
| 113 | } |
| 114 | |
| 115 | DT PerformanceTracker::GetDtAvg() const |
| 116 | { |
| 117 | return m_dt_avg; |
| 118 | } |
| 119 | |
| 120 | DT PerformanceTracker::GetDtStd() const |
| 121 | { |
| 122 | return m_dt_std; |
| 123 | } |
| 124 | |
| 125 | DT PerformanceTracker::GetLastRawDt() const |
| 126 | { |
| 127 | return m_last_raw_dt; |
| 128 | } |
| 129 | |
| 130 | void PerformanceTracker::InvalidateLastTime() |
| 131 | { |
| 132 | m_is_last_time_sane = false; |
| 133 | } |
| 134 | |
| 135 | void PerformanceTracker::ImPlotPlotLines(const char* label) const |
| 136 | { |
| 137 | // "quality" graph uses twice as many points. |
| 138 | static_assert(MAX_QUALITY_GRAPH_SIZE * 2 <= MAX_DT_QUEUE_SIZE); |
| 139 | static std::array<float, MAX_DT_QUEUE_SIZE + 1> x, y; |
| 140 | |
| 141 | if (m_dt_queue.empty()) |
| 142 | return; |
| 143 | |
| 144 | // Decides if there are too many points to plot using rectangles |
| 145 | const bool quality = m_dt_queue.size() < MAX_QUALITY_GRAPH_SIZE; |
| 146 | |
| 147 | std::size_t point_index = 0; |
| 148 | const auto add_point = [&](DT dt, DT shift_x, float prev_ms) { |
| 149 | const float ms = DT_ms{dt}.count(); |
| 150 | |
| 151 | if (quality) |
| 152 | { |
| 153 | x[point_index] = prev_ms; |
| 154 | y[point_index] = ms; |
| 155 | ++point_index; |
| 156 | } |
| 157 | |
| 158 | x[point_index] = prev_ms + DT_ms{shift_x}.count(); |
| 159 | y[point_index] = ms; |
| 160 | ++point_index; |
| 161 | }; |
| 162 | |
| 163 | // Rightmost point. |
| 164 | const auto update_time = Clock::now() - m_last_time; |
| 165 | const auto predicted_frame_time = std::max(update_time, m_dt_queue.front()); |
| 166 | add_point(predicted_frame_time, DT{}, 0); |
| 167 | |
| 168 | // Other points, right to left. |
| 169 | for (auto dt : m_dt_queue) |
| 170 | add_point(dt, dt, x[point_index - 1]); |
| 171 | |
| 172 | ImPlot::PlotLine(label, x.data(), y.data(), static_cast<int>(point_index)); |
| 173 | } |
| 174 | |
| 175 | void PerformanceTracker::PushFront(DT value) |
| 176 | { |
| 177 | m_dt_queue.push_front(value); |
| 178 | m_dt_total += value; |
| 179 | } |
| 180 | |
| 181 | void PerformanceTracker::PopBack() |
| 182 | { |
| 183 | m_dt_total -= m_dt_queue.back(); |
| 184 | m_dt_queue.pop_back(); |
| 185 | } |
| 186 | |
| 187 | void PerformanceTracker::LogRenderTimeToFile(DT val) |
| 188 | { |
| 189 | if (!m_log_name || !g_ActiveConfig.bLogRenderTimeToFile) |
| 190 | return; |
| 191 | |
| 192 | if (!m_bench_file.is_open()) |
| 193 | { |
| 194 | File::OpenFStream(m_bench_file, File::GetUserPath(D_LOGS_IDX) + *m_log_name, |
| 195 | std::ios_base::out); |
| 196 | } |
| 197 | |
| 198 | m_bench_file << std::fixed << std::setprecision(8) << DT_ms(val).count() << std::endl; |
| 199 | } |