Skip to content

Commit

Permalink
Reorganise way profiling stats are collected.
Browse files Browse the repository at this point in the history
Info needs to be updated when VM thread completes a frame.
  • Loading branch information
jpd002 committed Jan 12, 2024
1 parent 0249ff8 commit 17ea036
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 48 deletions.
13 changes: 6 additions & 7 deletions Source/PS2VM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -902,13 +902,12 @@ void CPS2VM::EmuThread()
m_pad->Update(m_ee->m_ram);
}
#ifdef PROFILE
{
CProfiler::GetInstance().CountCurrentZone();
auto stats = CProfiler::GetInstance().GetStats();
ProfileFrameDone(stats);
CProfiler::GetInstance().Reset();
}

//Finish up profile
CProfiler::GetInstance().CountCurrentZone();
#endif
OnNewFrame();
#ifdef PROFILE
CProfiler::GetInstance().Reset();
#endif
m_cpuUtilisation = CPU_UTILISATION_INFO();
}
Expand Down
4 changes: 2 additions & 2 deletions Source/PS2VM.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CPS2VM : public CVirtualMachine
typedef std::unique_ptr<COpticalMedia> OpticalMediaPtr;
typedef std::unique_ptr<Ee::CSubSystem> EeSubSystemPtr;
typedef std::unique_ptr<Iop::CSubSystem> IopSubSystemPtr;
typedef Framework::CSignal<void(const CProfiler::ZoneArray&)> ProfileFrameDoneSignal;
typedef Framework::CSignal<void()> NewFrameEvent;
typedef std::function<void(CPS2VM*)> ExecutableReloadedHandler;

CPS2VM();
Expand Down Expand Up @@ -95,7 +95,7 @@ class CPS2VM : public CVirtualMachine
EeSubSystemPtr m_ee;
IopSubSystemPtr m_iop;

ProfileFrameDoneSignal ProfileFrameDone;
NewFrameEvent OnNewFrame;

ExecutableReloadedHandler BeforeExecutableReloaded;
ExecutableReloadedHandler AfterExecutableReloaded;
Expand Down
7 changes: 2 additions & 5 deletions Source/ui_qt/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,6 @@ void MainWindow::InitVirtualMachine()
}
}

#ifdef PROFILE
m_profileFrameDoneConnection = m_virtualMachine->ProfileFrameDone.Connect(std::bind(&CStatsManager::OnProfileFrameDone, &CStatsManager::GetInstance(), std::placeholders::_1));
#endif

//OnExecutableChange might be called from another thread, we need to wrap it around a Qt signal
m_OnExecutableChangeConnection = m_virtualMachine->m_ee->m_os->OnExecutableChange.Connect(std::bind(&MainWindow::EmitOnExecutableChange, this));
connect(this, SIGNAL(onExecutableChange()), this, SLOT(HandleOnExecutableChange()));
Expand Down Expand Up @@ -258,7 +254,8 @@ void MainWindow::SetupGsHandler()
connect(m_outputwindow, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(outputWindow_mousePressEvent(QMouseEvent*)));
connect(m_outputwindow, SIGNAL(mouseRelease(QMouseEvent*)), this, SLOT(outputWindow_mouseReleaseEvent(QMouseEvent*)));

m_OnNewFrameConnection = m_virtualMachine->m_ee->m_gs->OnNewFrame.Connect(std::bind(&CStatsManager::OnNewFrame, &CStatsManager::GetInstance(), m_virtualMachine, std::placeholders::_1));
m_OnNewFrameConnection = m_virtualMachine->OnNewFrame.Connect(std::bind(&CStatsManager::OnNewFrame, &CStatsManager::GetInstance(), m_virtualMachine));
m_OnGsNewFrameConnection = m_virtualMachine->m_ee->m_gs->OnNewFrame.Connect(std::bind(&CStatsManager::OnGsNewFrame, &CStatsManager::GetInstance(), std::placeholders::_1));
}

void MainWindow::SetupSoundHandler()
Expand Down
4 changes: 2 additions & 2 deletions Source/ui_qt/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ class MainWindow : public QMainWindow
QLabel* m_gsLabel = nullptr;
#ifdef PROFILE
QLabel* m_profileStatsLabel = nullptr;
CPS2VM::ProfileFrameDoneSignal::Connection m_profileFrameDoneConnection;
#endif
ElidedLabel* m_msgLabel = nullptr;
QTimer* m_fpsTimer = nullptr;
Expand All @@ -117,7 +116,8 @@ class MainWindow : public QMainWindow
fs::path m_lastPath;

Framework::CSignal<void()>::Connection m_OnExecutableChangeConnection;
CGSHandler::NewFrameEvent::Connection m_OnNewFrameConnection;
CPS2VM::NewFrameEvent::Connection m_OnNewFrameConnection;
CGSHandler::NewFrameEvent::Connection m_OnGsNewFrameConnection;
CScreenShotUtils::Connection m_screenShotCompleteConnection;
CVirtualMachine::RunningStateChangeEvent::Connection m_onRunningStateChangeConnection;

Expand Down
56 changes: 29 additions & 27 deletions Source/ui_shared/StatsManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,39 @@
#include "string_format.h"
#include "PS2VM.h"

void CStatsManager::OnNewFrame(CPS2VM* virtualMachine, uint32 drawCalls)
void CStatsManager::OnNewFrame(CPS2VM* virtualMachine)
{
{
std::lock_guard<std::mutex> statsLock(m_statsMutex);
auto cpuUtilisation = virtualMachine->GetCpuUtilisationInfo();
m_cpuUtilisation.eeTotalTicks += cpuUtilisation.eeTotalTicks;
m_cpuUtilisation.eeIdleTicks += cpuUtilisation.eeIdleTicks;
m_cpuUtilisation.iopTotalTicks += cpuUtilisation.iopTotalTicks;
m_cpuUtilisation.iopIdleTicks += cpuUtilisation.iopIdleTicks;
}

#ifdef PROFILE
std::lock_guard<std::mutex> profileZonesLock(m_profilerZonesMutex);

auto zones = CProfiler::GetInstance().GetStats();
for(auto& zone : zones)
{
auto& zoneInfo = m_profilerZones[zone.name];
zoneInfo.currentValue += zone.totalTime;
if(zone.totalTime != 0)
{
zoneInfo.minValue = std::min<uint64>(zoneInfo.minValue, zone.totalTime);
}
zoneInfo.maxValue = std::max<uint64>(zoneInfo.maxValue, zone.totalTime);
}
#endif
}

void CStatsManager::OnGsNewFrame(uint32 drawCalls)
{
std::lock_guard<std::mutex> statsLock(m_statsMutex);
m_frames++;
m_drawCalls += drawCalls;

auto cpuUtilisation = virtualMachine->GetCpuUtilisationInfo();
m_cpuUtilisation.eeTotalTicks += cpuUtilisation.eeTotalTicks;
m_cpuUtilisation.eeIdleTicks += cpuUtilisation.eeIdleTicks;
m_cpuUtilisation.iopTotalTicks += cpuUtilisation.iopTotalTicks;
m_cpuUtilisation.iopIdleTicks += cpuUtilisation.iopIdleTicks;
}

float CStatsManager::ComputeCpuUsageRatio(int32 idleTicks, int32 totalTicks)
Expand Down Expand Up @@ -103,23 +125,3 @@ void CStatsManager::ClearStats()
}
#endif
}

#ifdef PROFILE

void CStatsManager::OnProfileFrameDone(const CProfiler::ZoneArray& zones)
{
std::lock_guard<std::mutex> profileZonesLock(m_profilerZonesMutex);

for(auto& zone : zones)
{
auto& zoneInfo = m_profilerZones[zone.name];
zoneInfo.currentValue += zone.totalTime;
if(zone.totalTime != 0)
{
zoneInfo.minValue = std::min<uint64>(zoneInfo.minValue, zone.totalTime);
}
zoneInfo.maxValue = std::max<uint64>(zoneInfo.maxValue, zone.totalTime);
}
}

#endif
7 changes: 2 additions & 5 deletions Source/ui_shared/StatsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
class CStatsManager : public CSingleton<CStatsManager>
{
public:
void OnNewFrame(CPS2VM*, uint32);
void OnNewFrame(CPS2VM*);
void OnGsNewFrame(uint32);

static float ComputeCpuUsageRatio(int32 idleTicks, int32 totalTicks);

Expand All @@ -23,10 +24,6 @@ class CStatsManager : public CSingleton<CStatsManager>

void ClearStats();

#ifdef PROFILE
void OnProfileFrameDone(const CProfiler::ZoneArray&);
#endif

private:
std::mutex m_statsMutex;

Expand Down

0 comments on commit 17ea036

Please sign in to comment.