Core: Make PerfStats internally locked

More ergonomic to use and will be required for upcoming changes.
This commit is contained in:
Yuri Kunde Schlesner 2017-02-20 13:56:58 -08:00
parent f273959205
commit b285c2a4ed
7 changed files with 25 additions and 16 deletions

View File

@ -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 frame
GetAndResetPerfStats();
perf_stats.Lock()->BeginSystemFrame();
perf_stats.BeginSystemFrame();
return ResultStatus::Success;
}

View File

@ -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:
/**

View File

@ -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);

View File

@ -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;

View File

@ -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 reset
auto 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;
}

View File

@ -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;

View File

@ -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 buffers
render_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();