1
0
mirror of https://github.com/RPCS3/rpcs3.git synced 2024-11-22 02:32:36 +01:00

Move CPUStats.h -> util/cpu_stats.cpp

This commit is contained in:
Nekotekina 2021-02-23 13:52:07 +03:00
parent 090a769bf6
commit cd01a1eb09
7 changed files with 57 additions and 43 deletions

View File

@ -40,6 +40,7 @@ target_sources(rpcs3_emu PRIVATE
../util/vm_native.cpp
../util/dyn_lib.cpp
../util/sysinfo.cpp
../util/cpu_stats.cpp
../../Utilities/bin_patch.cpp
../../Utilities/cheat_info.cpp
../../Utilities/cond.cpp

View File

@ -10,6 +10,7 @@
#include <charconv>
#include "util/sysinfo.hpp"
#include "util/cpu_stats.hpp"
namespace rsx
{
@ -423,7 +424,7 @@ namespace rsx
m_rsx_load = rsx_thread->get_load();
m_total_threads = CPUStats::get_thread_count();
m_total_threads = utils::cpu_stats::get_thread_count();
[[fallthrough]];
}

View File

@ -1,7 +1,7 @@
#pragma once
#include "overlays.h"
#include "Utilities/CPUStats.h"
#include "util/cpu_stats.hpp"
#include "Emu/system_config_types.h"
namespace rsx
@ -28,7 +28,7 @@ namespace rsx
graph m_fps_graph;
graph m_frametime_graph;
CPUStats m_cpu_stats{};
utils::cpu_stats m_cpu_stats{};
Timer m_update_timer{};
Timer m_frametime_timer{};
u32 m_update_interval{}; // in ms

View File

@ -149,6 +149,9 @@
<ClCompile Include="util\sysinfo.cpp">
<PrecompiledHeader>NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="util\cpu_stats.cpp">
<PrecompiledHeader>NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="..\Utilities\Thread.cpp" />
<ClCompile Include="..\Utilities\version.cpp" />
<ClCompile Include="util\vm_native.cpp" />
@ -508,6 +511,7 @@
<ClInclude Include="util\slow_mutex.hpp" />
<ClInclude Include="util\fifo_mutex.hpp" />
<ClInclude Include="util\logs.hpp" />
<ClInclude Include="util\cpu_stats.hpp" />
<ClInclude Include="..\Utilities\dyn_lib.hpp" />
<ClInclude Include="..\Utilities\File.h" />
<ClInclude Include="..\Utilities\Config.h" />

View File

@ -884,6 +884,9 @@
<ClCompile Include="util\fixed_typemap.cpp">
<Filter>Utilities</Filter>
</ClCompile>
<ClCompile Include="util\cpu_stats.cpp">
<Filter>Utilities</Filter>
</ClCompile>
<ClCompile Include="Crypto\aesni.cpp">
<Filter>Crypto</Filter>
</ClCompile>
@ -1855,6 +1858,9 @@
<ClInclude Include="util\typeindices.hpp">
<Filter>Utilities</Filter>
</ClInclude>
<ClInclude Include="util\cpu_stats.hpp">
<Filter>Utilities</Filter>
</ClInclude>
<ClInclude Include="Emu\RSX\Common\ShaderInterpreter.h">
<Filter>Emu\GPU\RSX\Common</Filter>
</ClInclude>

View File

@ -1,6 +1,7 @@
#pragma once
#include "util/types.hpp"
#include "util/cpu_stats.hpp"
#include "util/sysinfo.hpp"
#include <algorithm>
#ifdef _WIN32
#include "windows.h"
@ -43,34 +44,18 @@
# endif
#endif
class CPUStats
namespace utils
{
#ifdef _WIN32
HANDLE m_self;
using time_type = ULARGE_INTEGER;
#else
using time_type = clock_t;
#endif
private:
s32 m_num_processors;
time_type m_last_cpu, m_sys_cpu, m_usr_cpu;
public:
CPUStats()
cpu_stats::cpu_stats()
{
#ifdef _WIN32
SYSTEM_INFO sysInfo;
FILETIME ftime, fsys, fuser;
GetSystemInfo(&sysInfo);
m_num_processors = sysInfo.dwNumberOfProcessors;
GetSystemTimeAsFileTime(&ftime);
memcpy(&m_last_cpu, &ftime, sizeof(FILETIME));
m_self = GetCurrentProcess();
GetProcessTimes(m_self, &ftime, &ftime, &fsys, &fuser);
GetProcessTimes(GetCurrentProcess(), &ftime, &ftime, &fsys, &fuser);
memcpy(&m_sys_cpu, &fsys, sizeof(FILETIME));
memcpy(&m_usr_cpu, &fuser, sizeof(FILETIME));
#else
@ -79,11 +64,10 @@ public:
m_last_cpu = times(&timeSample);
m_sys_cpu = timeSample.tms_stime;
m_usr_cpu = timeSample.tms_utime;
m_num_processors = sysconf(_SC_NPROCESSORS_ONLN);
#endif
}
double get_usage()
double cpu_stats::get_usage()
{
#ifdef _WIN32
FILETIME ftime, fsys, fusr;
@ -92,16 +76,16 @@ public:
GetSystemTimeAsFileTime(&ftime);
memcpy(&now, &ftime, sizeof(FILETIME));
GetProcessTimes(m_self, &ftime, &ftime, &fsys, &fusr);
GetProcessTimes(GetCurrentProcess(), &ftime, &ftime, &fsys, &fusr);
memcpy(&sys, &fsys, sizeof(FILETIME));
memcpy(&usr, &fusr, sizeof(FILETIME));
double percent = double(sys.QuadPart - m_sys_cpu.QuadPart) + (usr.QuadPart - m_usr_cpu.QuadPart);
percent /= (now.QuadPart - m_last_cpu.QuadPart);
percent /= m_num_processors;
double percent = 1. * (sys.QuadPart - m_sys_cpu) + (usr.QuadPart - m_usr_cpu);
percent /= (now.QuadPart - m_last_cpu);
percent /= utils::get_thread_count();
m_last_cpu = now;
m_usr_cpu = usr;
m_sys_cpu = sys;
m_last_cpu = now.QuadPart;
m_usr_cpu = usr.QuadPart;
m_sys_cpu = sys.QuadPart;
return std::clamp(percent * 100, 0.0, 100.0);
#else
@ -110,7 +94,7 @@ public:
double percent;
now = times(&timeSample);
if (now <= m_last_cpu || timeSample.tms_stime < m_sys_cpu || timeSample.tms_utime < m_usr_cpu)
if (now <= static_cast<clock_t>(m_last_cpu) || timeSample.tms_stime < static_cast<clock_t>(m_sys_cpu) || timeSample.tms_utime < static_cast<clock_t>(m_usr_cpu))
{
// Overflow detection. Just skip this value.
percent = -1.0;
@ -119,7 +103,7 @@ public:
{
percent = (timeSample.tms_stime - m_sys_cpu) + (timeSample.tms_utime - m_usr_cpu);
percent /= (now - m_last_cpu);
percent /= m_num_processors;
percent /= utils::get_thread_count();
percent *= 100;
}
m_last_cpu = now;
@ -130,7 +114,7 @@ public:
#endif
}
static u32 get_thread_count()
u32 cpu_stats::get_thread_count() // static
{
#ifdef _WIN32
// first determine the id of the current process
@ -240,4 +224,4 @@ public:
return 0;
#endif
}
};
}

18
rpcs3/util/cpu_stats.hpp Normal file
View File

@ -0,0 +1,18 @@
#pragma once
#include "util/types.hpp"
namespace utils
{
class cpu_stats
{
u64 m_last_cpu, m_sys_cpu, m_usr_cpu;
public:
cpu_stats();
double get_usage();
static u32 get_thread_count();
};
}