VERY experimental Nintendo Switch 2 emulator
C++
Core: Make PerfStats internally locked
Yuri Kunde Schlesnerea78f30e6720
diff --git a/src/core/core.cpp b/src/core/core.cppindex ca2c28ce4e..140ff64511 100644--- a/src/core/core.cpp+++ b/src/core/core.cpp@@ -110,8 +110,7 @@ void System::PrepareReschedule() {}PerfStats::Results System::GetAndResetPerfStats() {- auto perf_stats = this->perf_stats.Lock();- return perf_stats->GetAndResetStats(CoreTiming::GetGlobalTimeUs());+ return perf_stats.GetAndResetStats(CoreTiming::GetGlobalTimeUs());}void System::Reschedule() {@@ -147,7 +146,7 @@ System::ResultStatus System::Init(EmuWindow* emu_window, u32 system_mode) {// Reset counters and set time origin to current frameGetAndResetPerfStats();- perf_stats.Lock()->BeginSystemFrame();+ perf_stats.BeginSystemFrame();return ResultStatus::Success;}diff --git a/src/core/core.h b/src/core/core.hindex 3efc20c3d6..db3b98a052 100644--- a/src/core/core.h+++ b/src/core/core.h@@ -7,7 +7,6 @@#include <memory>#include <string>#include "common/common_types.h"-#include "common/synchronized_wrapper.h"#include "core/memory.h"#include "core/perf_stats.h"@@ -94,7 +93,7 @@ public:return *cpu_core;}- Common::SynchronizedWrapper<PerfStats> perf_stats;+ PerfStats perf_stats;private:/**diff --git a/src/core/frontend/emu_window.cpp b/src/core/frontend/emu_window.cppindex b65d6ff580..a155b657dd 100644--- a/src/core/frontend/emu_window.cpp+++ b/src/core/frontend/emu_window.cpp@@ -104,7 +104,7 @@ void EmuWindow::AccelerometerChanged(float x, float y, float z) {void EmuWindow::GyroscopeChanged(float x, float y, float z) {constexpr float FULL_FPS = 60;float coef = GetGyroscopeRawToDpsCoefficient();- float stretch = Core::System::GetInstance().perf_stats.Lock()->GetLastFrameTimeScale();+ float stretch = Core::System::GetInstance().perf_stats.GetLastFrameTimeScale();std::lock_guard<std::mutex> lock(gyro_mutex);gyro_x = static_cast<s16>(x * coef * stretch);gyro_y = static_cast<s16>(y * coef * stretch);diff --git a/src/core/hle/service/gsp_gpu.cpp b/src/core/hle/service/gsp_gpu.cppindex 67bab38da0..097ed87e4d 100644--- a/src/core/hle/service/gsp_gpu.cpp+++ b/src/core/hle/service/gsp_gpu.cpp@@ -281,8 +281,7 @@ ResultCode SetBufferSwap(u32 screen_id, const FrameBufferInfo& info) {if (screen_id == 0) {MicroProfileFlip();- auto perf_stats = Core::System::GetInstance().perf_stats.Lock();- perf_stats->EndGameFrame();+ Core::System::GetInstance().perf_stats.EndGameFrame();}return RESULT_SUCCESS;diff --git a/src/core/perf_stats.cpp b/src/core/perf_stats.cppindex 8d9e521a32..06bc788bd7 100644--- a/src/core/perf_stats.cpp+++ b/src/core/perf_stats.cpp@@ -3,6 +3,7 @@// Refer to the license.txt file included.#include <chrono>+#include <mutex>#include "core/hw/gpu.h"#include "core/perf_stats.h"@@ -12,10 +13,14 @@ using std::chrono::duration_cast;namespace Core {void PerfStats::BeginSystemFrame() {+ std::lock_guard<std::mutex> lock(object_mutex);+frame_begin = Clock::now();}void PerfStats::EndSystemFrame() {+ std::lock_guard<std::mutex> lock(object_mutex);+auto frame_end = Clock::now();accumulated_frametime += frame_end - frame_begin;system_frames += 1;@@ -25,10 +30,14 @@ void PerfStats::EndSystemFrame() {}void PerfStats::EndGameFrame() {+ std::lock_guard<std::mutex> lock(object_mutex);+game_frames += 1;}PerfStats::Results PerfStats::GetAndResetStats(u64 current_system_time_us) {+ std::lock_guard<std::mutex> lock(object_mutex);+auto now = Clock::now();// Walltime elapsed since stats were resetauto interval = duration_cast<DoubleSecs>(now - reset_point).count();@@ -54,6 +63,8 @@ PerfStats::Results PerfStats::GetAndResetStats(u64 current_system_time_us) {}double PerfStats::GetLastFrameTimeScale() {+ std::lock_guard<std::mutex> lock(object_mutex);+constexpr double FRAME_LENGTH = 1.0 / GPU::SCREEN_REFRESH_RATE;return duration_cast<DoubleSecs>(previous_frame_length).count() / FRAME_LENGTH;}diff --git a/src/core/perf_stats.h b/src/core/perf_stats.hindex 8a03c511a6..4098fc1f26 100644--- a/src/core/perf_stats.h+++ b/src/core/perf_stats.h@@ -5,10 +5,15 @@#pragma once#include <chrono>+#include <mutex>#include "common/common_types.h"namespace Core {+/**+ * Class to manage and query performance/timing statistics. All public functions of this class are+ * thread-safe unless stated otherwise.+ */class PerfStats {public:using Clock = std::chrono::high_resolution_clock;@@ -37,6 +42,8 @@ public:double GetLastFrameTimeScale();private:+ std::mutex object_mutex;+Clock::time_point reset_point = Clock::now();Clock::time_point frame_begin = reset_point;diff --git a/src/video_core/renderer_opengl/renderer_opengl.cpp b/src/video_core/renderer_opengl/renderer_opengl.cppindex 6bc142148b..b3604106c4 100644--- a/src/video_core/renderer_opengl/renderer_opengl.cpp+++ b/src/video_core/renderer_opengl/renderer_opengl.cpp@@ -145,10 +145,7 @@ void RendererOpenGL::SwapBuffers() {DrawScreens();- {- auto perf_stats = Core::System::GetInstance().perf_stats.Lock();- perf_stats->EndSystemFrame();- }+ Core::System::GetInstance().perf_stats.EndSystemFrame();// Swap buffersrender_window->PollEvents();@@ -156,10 +153,7 @@ void RendererOpenGL::SwapBuffers() {prev_state.Apply();- {- auto perf_stats = Core::System::GetInstance().perf_stats.Lock();- perf_stats->BeginSystemFrame();- }+ Core::System::GetInstance().perf_stats.BeginSystemFrame();RefreshRasterizerSetting();