Merge pull request #13397 from jordan-woyak/perf-metrics

PerformanceMetrics: Eliminated a mutex. Code cleanups.
This commit is contained in:
JosJuice 2025-03-17 23:20:41 +01:00 committed by GitHub
commit 0299540209
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 31 additions and 63 deletions

View File

@ -29,21 +29,17 @@
#include "Common/FPURoundMode.h"
#include "Common/FatFsUtil.h"
#include "Common/FileUtil.h"
#include "Common/Flag.h"
#include "Common/Logging/Log.h"
#include "Common/MemoryUtil.h"
#include "Common/MsgHandler.h"
#include "Common/ScopeGuard.h"
#include "Common/StringUtil.h"
#include "Common/Thread.h"
#include "Common/Timer.h"
#include "Common/Version.h"
#include "Core/AchievementManager.h"
#include "Core/Boot/Boot.h"
#include "Core/BootManager.h"
#include "Core/CPUThreadConfigCallback.h"
#include "Core/Config/AchievementSettings.h"
#include "Core/Config/MainSettings.h"
#include "Core/ConfigManager.h"
#include "Core/CoreTiming.h"
@ -92,7 +88,6 @@
#include "VideoCommon/FrameDumper.h"
#include "VideoCommon/OnScreenDisplay.h"
#include "VideoCommon/PerformanceMetrics.h"
#include "VideoCommon/Present.h"
#include "VideoCommon/VideoBackendBase.h"
#include "VideoCommon/VideoEvents.h"
@ -106,7 +101,6 @@ static std::vector<StateChangedCallbackFunc> s_on_state_changed_callbacks;
static std::thread s_cpu_thread;
static bool s_is_throttler_temp_disabled = false;
static std::atomic<double> s_last_actual_emulation_speed{1.0};
static bool s_frame_step = false;
static std::atomic<bool> s_stop_frame_step;
@ -118,6 +112,8 @@ static std::atomic<State> s_state = State::Uninitialized;
static std::unique_ptr<MemoryWatcher> s_memory_watcher;
#endif
void Callback_FramePresented(const PresentInfo& present_info);
struct HostJob
{
std::function<void(Core::System&)> job;
@ -134,16 +130,8 @@ static thread_local bool tls_is_host_thread = false;
static void EmuThread(Core::System& system, std::unique_ptr<BootParameters> boot,
WindowSystemInfo wsi);
static Common::EventHook s_frame_presented = AfterPresentEvent::Register(
[](auto& present_info) {
const double last_speed_denominator = g_perf_metrics.GetLastSpeedDenominator();
// The denominator should always be > 0 but if it's not, just return 1
const double last_speed = last_speed_denominator > 0.0 ? (1.0 / last_speed_denominator) : 1.0;
if (present_info.reason != PresentInfo::PresentReason::VideoInterfaceDuplicate)
Core::Callback_FramePresented(last_speed);
},
"Core Frame Presented");
static Common::EventHook s_frame_presented =
AfterPresentEvent::Register(&Core::Callback_FramePresented, "Core Frame Presented");
bool GetIsThrottlerTempDisabled()
{
@ -155,11 +143,6 @@ void SetIsThrottlerTempDisabled(bool disable)
s_is_throttler_temp_disabled = disable;
}
double GetActualEmulationSpeed()
{
return s_last_actual_emulation_speed;
}
void FrameUpdateOnCPUThread()
{
if (NetPlay::IsNetPlayRunning())
@ -317,8 +300,6 @@ void Stop(Core::System& system) // - Hammertime!
system.GetFifo().ExitGpuLoop();
}
s_last_actual_emulation_speed = 1.0;
}
void DeclareAsCPUThread()
@ -873,13 +854,13 @@ void RunOnCPUThread(Core::System& system, std::function<void()> function, bool w
// --- Callbacks for backends / engine ---
// Called from Renderer::Swap (GPU thread) when a new (non-duplicate)
// frame is presented to the host screen
void Callback_FramePresented(double actual_emulation_speed)
// Called from Renderer::Swap (GPU thread) when a frame is presented to the host screen.
void Callback_FramePresented(const PresentInfo& present_info)
{
g_perf_metrics.CountFrame();
if (present_info.reason == PresentInfo::PresentReason::VideoInterfaceDuplicate)
return;
s_last_actual_emulation_speed = actual_emulation_speed;
g_perf_metrics.CountFrame();
s_stop_frame_step.store(true);
}

View File

@ -26,10 +26,6 @@ class System;
bool GetIsThrottlerTempDisabled();
void SetIsThrottlerTempDisabled(bool disable);
// Returns the latest emulation speed (1 is full speed) (swings a lot)
double GetActualEmulationSpeed();
void Callback_FramePresented(double actual_emulation_speed = 1.0);
void Callback_NewField(Core::System& system);
enum class State

View File

@ -433,12 +433,6 @@ void CoreTimingManager::ResetThrottle(s64 cycle)
m_throttle_deadline = Clock::now();
}
TimePoint CoreTimingManager::GetCPUTimePoint(s64 cyclesLate) const
{
return TimePoint(std::chrono::duration_cast<DT>(DT_s(m_globals.global_timer - cyclesLate) /
m_throttle_clock_per_sec));
}
bool CoreTimingManager::GetVISkip() const
{
return m_throttle_disable_vi_int && g_ActiveConfig.bVISkip && !Core::WantsDeterminism();

View File

@ -161,8 +161,8 @@ public:
// May be used from any thread.
void SleepUntil(TimePoint time_point);
TimePoint GetCPUTimePoint(s64 cyclesLate) const; // Used by Dolphin Analytics
bool GetVISkip() const; // Used By VideoInterface
// Used by VideoInterface
bool GetVISkip() const;
bool UseSyncOnSkipIdle() const;

View File

@ -4,13 +4,13 @@
#include "VideoCommon/PerformanceMetrics.h"
#include <algorithm>
#include <mutex>
#include <imgui.h>
#include <implot.h>
#include "Core/Config/GraphicsSettings.h"
#include "Core/CoreTiming.h"
#include "Core/HW/SystemTimers.h"
#include "Core/HW/VideoInterface.h"
#include "Core/System.h"
#include "VideoCommon/VideoConfig.h"
@ -25,7 +25,8 @@ void PerformanceMetrics::Reset()
m_time_sleeping = DT::zero();
m_real_times.fill(Clock::now());
m_cpu_times.fill(Core::System::GetInstance().GetCoreTiming().GetCPUTimePoint(0));
m_core_ticks.fill(0);
m_max_speed = 0;
}
void PerformanceMetrics::CountFrame()
@ -40,18 +41,25 @@ void PerformanceMetrics::CountVBlank()
void PerformanceMetrics::CountThrottleSleep(DT sleep)
{
std::unique_lock lock(m_time_lock);
m_time_sleeping += sleep;
}
void PerformanceMetrics::CountPerformanceMarker(Core::System& system, s64 cyclesLate)
void PerformanceMetrics::CountPerformanceMarker(Core::System& system, s64 cycles_late)
{
std::unique_lock lock(m_time_lock);
m_speed_counter.Count();
m_real_times[m_time_index] = Clock::now() - m_time_sleeping;
m_cpu_times[m_time_index] = system.GetCoreTiming().GetCPUTimePoint(cyclesLate);
m_time_index += 1;
const auto ticks = system.GetCoreTiming().GetTicks() - cycles_late;
const auto real_time = Clock::now() - m_time_sleeping;
auto& oldest_ticks = m_core_ticks[m_time_index];
auto& oldest_time = m_real_times[m_time_index];
m_max_speed = DT_s(ticks - oldest_ticks) / system.GetSystemTimers().GetTicksPerSecond() /
(real_time - oldest_time);
oldest_ticks = ticks;
oldest_time = real_time;
++m_time_index;
}
double PerformanceMetrics::GetFPS() const
@ -71,15 +79,7 @@ double PerformanceMetrics::GetSpeed() const
double PerformanceMetrics::GetMaxSpeed() const
{
std::shared_lock lock(m_time_lock);
return DT_s(m_cpu_times[u8(m_time_index - 1)] - m_cpu_times[m_time_index]) /
DT_s(m_real_times[u8(m_time_index - 1)] - m_real_times[m_time_index]);
}
double PerformanceMetrics::GetLastSpeedDenominator() const
{
return DT_s(m_speed_counter.GetLastRawDt()).count() *
Core::System::GetInstance().GetVideoInterface().GetTargetRefreshRate();
return m_max_speed;
}
void PerformanceMetrics::DrawImGuiStats(const float backbuffer_scale)

View File

@ -4,7 +4,7 @@
#pragma once
#include <array>
#include <shared_mutex>
#include <atomic>
#include "Common/CommonTypes.h"
#include "VideoCommon/PerformanceTracker.h"
@ -39,8 +39,6 @@ public:
double GetSpeed() const;
double GetMaxSpeed() const;
double GetLastSpeedDenominator() const;
// ImGui Functions
void DrawImGuiStats(const float backbuffer_scale);
@ -51,11 +49,10 @@ private:
double m_graph_max_time = 0.0;
mutable std::shared_mutex m_time_lock;
std::atomic<double> m_max_speed{};
u8 m_time_index = 0;
std::array<TimePoint, 256> m_real_times{};
std::array<TimePoint, 256> m_cpu_times{};
std::array<u64, 256> m_core_ticks{};
DT m_time_sleeping{};
};