This commit is contained in:
Jordan Woyak 2025-05-27 18:37:58 -05:00 committed by GitHub
commit f963c18e78
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 41 deletions

View File

@ -12,16 +12,14 @@
#include "Common/CommonTypes.h"
#include "Common/FileUtil.h"
#include "Common/MathUtil.h"
#include "Core/Core.h"
#include "VideoCommon/VideoConfig.h"
static constexpr double SAMPLE_RC_RATIO = 0.25;
static constexpr u64 MAX_DT_QUEUE_SIZE = 1UL << 12;
static constexpr u64 MAX_QUALITY_GRAPH_SIZE = 1UL << 8;
static constexpr u64 MAX_DT_QUEUE_SIZE = 1UL << 12u;
static constexpr u64 MAX_QUALITY_GRAPH_SIZE = 1UL << 8u;
PerformanceTracker::PerformanceTracker(const std::optional<std::string> log_name,
const std::optional<DT> sample_window_duration)
: m_log_name{log_name}, m_sample_window_duration{sample_window_duration}
PerformanceTracker::PerformanceTracker(std::optional<std::string> log_name)
: m_log_name{std::move(log_name)}
{
Reset();
}
@ -44,17 +42,16 @@ void PerformanceTracker::Count()
{
const TimePoint current_time{Clock::now()};
const DT diff{current_time - m_last_time};
m_last_time = current_time;
if (!m_is_last_time_sane)
if (m_is_last_time_sane.exchange(true, std::memory_order_relaxed))
{
m_is_last_time_sane = true;
return;
const DT diff{current_time - std::exchange(m_last_time, current_time)};
m_last_raw_dt.store(diff, std::memory_order_relaxed);
m_raw_dts.Push(diff);
}
else
{
m_last_time = current_time;
}
m_last_raw_dt = diff;
m_raw_dts.Push(diff);
}
void PerformanceTracker::UpdateStats()
@ -67,7 +64,9 @@ void PerformanceTracker::UpdateStats()
MathUtil::RunningVariance<double> variance;
for (auto dt : m_dt_queue)
variance.Push(DT_s(dt).count());
m_dt_std = std::chrono::duration_cast<DT>(DT_s(variance.PopulationStandardDeviation()));
const auto dt_std = std::chrono::duration_cast<DT>(DT_s(variance.PopulationStandardDeviation()));
m_dt_std.store(dt_std, std::memory_order_relaxed);
}
void PerformanceTracker::HandleRawDt(DT diff)
@ -85,51 +84,51 @@ void PerformanceTracker::HandleRawDt(DT diff)
// Simple Moving Average Throughout the Window
const DT dt_avg = m_dt_total / m_dt_queue.size();
const double hz = DT_s(1.0) / dt_avg;
m_dt_avg = dt_avg;
m_dt_avg.store(dt_avg, std::memory_order_relaxed);
// Exponential Moving Average
const DT_s rc = SAMPLE_RC_RATIO * std::min(window, m_dt_total);
const double a = 1.0 - std::exp(-(DT_s(diff) / rc));
// Sometimes euler averages can break when the average is inf/nan
const auto hz_avg = m_hz_avg.load();
auto hz_avg = m_hz_avg.load(std::memory_order_relaxed);
if (std::isfinite(hz_avg))
m_hz_avg = hz_avg + a * (hz - hz_avg);
hz_avg = hz_avg + a * (hz - hz_avg);
else
m_hz_avg = hz;
hz_avg = hz;
m_hz_avg.store(hz_avg, std::memory_order_relaxed);
LogRenderTimeToFile(diff);
}
DT PerformanceTracker::GetSampleWindow() const
DT PerformanceTracker::GetSampleWindow()
{
return m_sample_window_duration.value_or(
duration_cast<DT>(DT_us{std::max(1, g_ActiveConfig.iPerfSampleUSec)}));
return duration_cast<DT>(DT_us{std::max(1, g_ActiveConfig.iPerfSampleUSec)});
}
double PerformanceTracker::GetHzAvg() const
{
return m_hz_avg;
return m_hz_avg.load(std::memory_order_relaxed);
}
DT PerformanceTracker::GetDtAvg() const
{
return m_dt_avg;
return m_dt_avg.load(std::memory_order_relaxed);
}
DT PerformanceTracker::GetDtStd() const
{
return m_dt_std;
return m_dt_std.load(std::memory_order_relaxed);
}
DT PerformanceTracker::GetLastRawDt() const
{
return m_last_raw_dt;
return m_last_raw_dt.load(std::memory_order_relaxed);
}
void PerformanceTracker::InvalidateLastTime()
{
m_is_last_time_sane = false;
m_is_last_time_sane.store(false, std::memory_order_relaxed);
}
void PerformanceTracker::ImPlotPlotLines(const char* label) const
@ -160,12 +159,7 @@ void PerformanceTracker::ImPlotPlotLines(const char* label) const
++point_index;
};
// Rightmost point.
const auto update_time = Clock::now() - m_last_time;
const auto predicted_frame_time = std::max(update_time, m_dt_queue.front());
add_point(predicted_frame_time, DT{}, 0);
// Other points, right to left.
// add points, right to left.
for (auto dt : m_dt_queue)
add_point(dt, dt, x[point_index - 1]);

View File

@ -14,8 +14,7 @@
class PerformanceTracker
{
public:
PerformanceTracker(const std::optional<std::string> log_name = std::nullopt,
const std::optional<DT> sample_window_duration = std::nullopt);
explicit PerformanceTracker(std::optional<std::string> log_name = std::nullopt);
~PerformanceTracker() = default;
PerformanceTracker(const PerformanceTracker&) = delete;
@ -34,7 +33,7 @@ public:
void Count();
// May call from any thread.
DT GetSampleWindow() const;
static DT GetSampleWindow();
double GetHzAvg() const;
DT GetDtAvg() const;
DT GetDtStd() const;
@ -44,7 +43,7 @@ public:
private:
void LogRenderTimeToFile(DT val);
void HandleRawDt(DT value);
void HandleRawDt(DT diff);
void PushFront(DT value);
void PopBack();
@ -61,9 +60,6 @@ private:
Common::SPSCQueue<DT> m_raw_dts;
std::atomic<DT> m_last_raw_dt = DT::zero();
// Amount of time to sample dt's over (defaults to config)
const std::optional<DT> m_sample_window_duration;
// Queue + Running Total used to calculate average dt
DT m_dt_total = DT::zero();
std::deque<DT> m_dt_queue;