diff --git a/rpcs3/Emu/CMakeLists.txt b/rpcs3/Emu/CMakeLists.txt index d0833f24eb..2f9aef197c 100644 --- a/rpcs3/Emu/CMakeLists.txt +++ b/rpcs3/Emu/CMakeLists.txt @@ -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 diff --git a/rpcs3/Emu/RSX/Overlays/overlay_perf_metrics.cpp b/rpcs3/Emu/RSX/Overlays/overlay_perf_metrics.cpp index 1703776faa..5646bc9fd4 100644 --- a/rpcs3/Emu/RSX/Overlays/overlay_perf_metrics.cpp +++ b/rpcs3/Emu/RSX/Overlays/overlay_perf_metrics.cpp @@ -10,6 +10,7 @@ #include #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]]; } diff --git a/rpcs3/Emu/RSX/Overlays/overlay_perf_metrics.h b/rpcs3/Emu/RSX/Overlays/overlay_perf_metrics.h index 233bbfa0f6..49abecea0a 100644 --- a/rpcs3/Emu/RSX/Overlays/overlay_perf_metrics.h +++ b/rpcs3/Emu/RSX/Overlays/overlay_perf_metrics.h @@ -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 diff --git a/rpcs3/emucore.vcxproj b/rpcs3/emucore.vcxproj index 794dbb98fd..5ece3eea46 100644 --- a/rpcs3/emucore.vcxproj +++ b/rpcs3/emucore.vcxproj @@ -149,6 +149,9 @@ NotUsing + + NotUsing + @@ -508,6 +511,7 @@ + diff --git a/rpcs3/emucore.vcxproj.filters b/rpcs3/emucore.vcxproj.filters index 387b61b776..33c1897318 100644 --- a/rpcs3/emucore.vcxproj.filters +++ b/rpcs3/emucore.vcxproj.filters @@ -884,6 +884,9 @@ Utilities + + Utilities + Crypto @@ -973,12 +976,12 @@ Emu\Io - + Emu\GPU\RSX - + - Emu\GPU\RSX\Common + Emu\GPU\RSX\Common @@ -1855,6 +1858,9 @@ Utilities + + Utilities + Emu\GPU\RSX\Common @@ -1901,14 +1907,14 @@ Emu\GPU\RSX - Header Files - + Header Files + Emu\GPU\RSX Emu\GPU\RSX\Common - + diff --git a/Utilities/CPUStats.h b/rpcs3/util/cpu_stats.cpp similarity index 82% rename from Utilities/CPUStats.h rename to rpcs3/util/cpu_stats.cpp index cb34507ec6..9b9bf25827 100644 --- a/Utilities/CPUStats.h +++ b/rpcs3/util/cpu_stats.cpp @@ -1,6 +1,7 @@ -#pragma once - #include "util/types.hpp" +#include "util/cpu_stats.hpp" +#include "util/sysinfo.hpp" +#include #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(m_last_cpu) || timeSample.tms_stime < static_cast(m_sys_cpu) || timeSample.tms_utime < static_cast(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 } -}; +} diff --git a/rpcs3/util/cpu_stats.hpp b/rpcs3/util/cpu_stats.hpp new file mode 100644 index 0000000000..744478dd09 --- /dev/null +++ b/rpcs3/util/cpu_stats.hpp @@ -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(); + }; +}