From dcc66fdc5743b9a8d384d62e5054fe74f725abd2 Mon Sep 17 00:00:00 2001 From: Vladislav Belov Date: Sat, 17 Oct 2026 15:53:14 +0200 Subject: [PATCH] Removes CPU frequency calculating functionality --- source/lib/sysdep/arch/x86_x64/x86_x64.cpp | 111 +-------------------- source/lib/sysdep/arch/x86_x64/x86_x64.h | 8 -- source/lib/sysdep/os/win/wcpu.cpp | 20 ---- source/lib/sysdep/os/win/wcpu.h | 4 +- source/lib/sysdep/os/win/wvm.cpp | 1 + source/lib/sysdep/os_cpu.cpp | 20 ---- source/lib/sysdep/os_cpu.h | 6 -- source/lib/timer.h | 5 - 8 files changed, 3 insertions(+), 172 deletions(-) diff --git a/source/lib/sysdep/arch/x86_x64/x86_x64.cpp b/source/lib/sysdep/arch/x86_x64/x86_x64.cpp index 3b0d8e6961..1ae9cf7050 100644 --- a/source/lib/sysdep/arch/x86_x64/x86_x64.cpp +++ b/source/lib/sysdep/arch/x86_x64/x86_x64.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -397,115 +397,6 @@ void DebugBreak() #endif } - -//----------------------------------------------------------------------------- -// CPU frequency - -// set scheduling priority and restore when going out of scope. -class ScopedSetPriority -{ -public: - ScopedSetPriority(int newPriority) - { - // get current scheduling policy and priority - pthread_getschedparam(pthread_self(), &m_oldPolicy, &m_oldParam); - - // set new priority - sched_param newParam = {0}; - newParam.sched_priority = newPriority; - pthread_setschedparam(pthread_self(), SCHED_FIFO, &newParam); - } - - ~ScopedSetPriority() - { - // restore previous policy and priority. - pthread_setschedparam(pthread_self(), m_oldPolicy, &m_oldParam); - } - -private: - int m_oldPolicy; - sched_param m_oldParam; -}; - -// note: this function uses timer.cpp!timer_Time, which is implemented via -// whrt.cpp on Windows. -double ClockFrequency() -{ - // if the TSC isn't available, there's really no good way to count the - // actual CPU clocks per known time interval, so bail. - // note: loop iterations ("bogomips") are not a reliable measure due - // to differing IPC and compiler optimizations. - if(!Cap(x86_x64::CAP_TSC)) - return -1.0; // impossible value - - // increase priority to reduce interference while measuring. - const int priority = sched_get_priority_max(SCHED_FIFO)-1; - ScopedSetPriority ssp(priority); - - // note: no need to "warm up" cpuid - it will already have been - // called several times by the time this code is reached. - // (background: it's used in rdtsc() to serialize instruction flow; - // the first call is documented to be slower on Intel CPUs) - - size_t numSamples = 16; - // if clock is low-res, do less samples so it doesn't take too long. - // balance measuring time (~ 10 ms) and accuracy (< 0.1% error - - // ok for using the TSC as a time reference) - if(timer_Resolution() >= 1e-3) - numSamples = 8; - std::vector samples(numSamples); - - for(size_t i = 0; i < numSamples; i++) - { - double dt; - std::int64_t dc; // (std::int64_t instead of std::uint64_t for faster conversion to double) - - // count # of clocks in max{1 tick, 1 ms}: - // .. wait for start of tick. - const double t0 = timer_Time(); - std::uint64_t c1; double t1; - do - { - // note: timer_Time effectively has a long delay (up to 5 us) - // before returning the time. we call it before rdtsc to - // minimize the delay between actually sampling time / TSC, - // thus decreasing the chance for interference. - // (if unavoidable background activity, e.g. interrupts, - // delays the second reading, inaccuracy is introduced). - t1 = timer_Time(); - c1 = rdtsc(); - } - while(t1 == t0); - // .. wait until start of next tick and at least 1 ms elapsed. - do - { - const double t2 = timer_Time(); - const std::uint64_t c2 = rdtsc(); - dc = static_cast(c2 - c1); - dt = t2 - t1; - } - while(dt < 1e-3); - - // .. freq = (delta_clocks) / (delta_seconds); - // rdtsc/timer overhead is negligible. - const double freq = dc / dt; - samples[i] = freq; - } - - std::sort(samples.begin(), samples.end()); - - // median filter (remove upper and lower 25% and average the rest). - // note: don't just take the lowest value! it could conceivably be - // too low, if background processing delays reading c1 (see above). - double sum = 0.0; - const size_t lo = numSamples/4, hi = 3*numSamples/4; - for(size_t i = lo; i < hi; i++) - sum += samples[i]; - - const double clockFrequency = sum / (hi-lo); - return clockFrequency; -} - } // namespace x86_x64 diff --git a/source/lib/sysdep/arch/x86_x64/x86_x64.h b/source/lib/sysdep/arch/x86_x64/x86_x64.h index 5a58824cf5..bf29405cd0 100644 --- a/source/lib/sysdep/arch/x86_x64/x86_x64.h +++ b/source/lib/sysdep/arch/x86_x64/x86_x64.h @@ -172,14 +172,6 @@ std::uint64_t rdtsc(); **/ void DebugBreak(); -/** - * measure the CPU clock frequency via rdtsc and timer_Time. - * (it follows that this must not be called from WHRT init.) - * this takes several milliseconds (i.e. much longer than - * os_cpu_ClockFrequency) but delivers accurate measurements. - **/ -double ClockFrequency(); - } // namespace x86_x64 #endif // #ifndef INCLUDED_X86_X64 diff --git a/source/lib/sysdep/os/win/wcpu.cpp b/source/lib/sysdep/os/win/wcpu.cpp index 5c5e16c419..5f1f83c946 100644 --- a/source/lib/sysdep/os/win/wcpu.cpp +++ b/source/lib/sysdep/os/win/wcpu.cpp @@ -74,26 +74,6 @@ size_t os_cpu_NumProcessors() } -//----------------------------------------------------------------------------- - -Status wcpu_ReadFrequencyFromRegistry(std::uint32_t& freqMhz) -{ - HKEY hKey; - if(RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS) - return ERR::NOT_SUPPORTED; - - DWORD size = sizeof(freqMhz); - LONG ret = RegQueryValueExW(hKey, L"~MHz", 0, 0, (LPBYTE)&freqMhz, &size); - - RegCloseKey(hKey); - - if(ret != ERROR_SUCCESS) - WARN_RETURN(ERR::FAIL); - - return INFO::OK; -} - - size_t os_cpu_PageSize() { static size_t systemPageSize; diff --git a/source/lib/sysdep/os/win/wcpu.h b/source/lib/sysdep/os/win/wcpu.h index 8660772a93..33569e4c1c 100644 --- a/source/lib/sysdep/os/win/wcpu.h +++ b/source/lib/sysdep/os/win/wcpu.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2010 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -29,8 +29,6 @@ #include "lib/sysdep/os/win/win.h" -extern Status wcpu_ReadFrequencyFromRegistry(std::uint32_t& freqMhz); - // "affinity" and "processorNumber" are what Windows sees. // "processorMask" and "processor" are the idealized representation we expose // to users. the latter insulates them from process affinity restrictions by diff --git a/source/lib/sysdep/os/win/wvm.cpp b/source/lib/sysdep/os/win/wvm.cpp index 074d67ba94..a7de1c7628 100644 --- a/source/lib/sysdep/os/win/wvm.cpp +++ b/source/lib/sysdep/os/win/wvm.cpp @@ -31,6 +31,7 @@ #include "lib/alignment.h" // CACHE_ALIGNED #include "lib/bits.h" // round_down #include "lib/module_init.h" +#include "lib/sysdep/os_cpu.h" #include "lib/sysdep/os/win/wutil.h" #include "lib/timer.h" diff --git a/source/lib/sysdep/os_cpu.cpp b/source/lib/sysdep/os_cpu.cpp index e84e8c65ee..238c05aabc 100644 --- a/source/lib/sysdep/os_cpu.cpp +++ b/source/lib/sysdep/os_cpu.cpp @@ -32,32 +32,12 @@ #include "lib/code_annotation.h" #include "lib/sysdep/os.h" -#if OS_WIN -# include "lib/sysdep/os/win/wcpu.h" -#endif - static const StatusDefinition osCpuStatusDefinitions[] = { { ERR::OS_CPU_RESTRICTED_AFFINITY, L"Cannot set desired CPU affinity" } }; STATUS_ADD_DEFINITIONS(osCpuStatusDefinitions); -double os_cpu_ClockFrequency() -{ - static double clockFrequency; - if(clockFrequency != 0.0) // already initialized - return clockFrequency; - -#if OS_WIN - std::uint32_t freqMhz; - if(wcpu_ReadFrequencyFromRegistry(freqMhz) == INFO::OK) - return clockFrequency = freqMhz * 1e6; -#endif - - return clockFrequency = -1.0; // unknown -} - - size_t os_cpu_MemorySize() { static size_t memorySize; diff --git a/source/lib/sysdep/os_cpu.h b/source/lib/sysdep/os_cpu.h index a932681a91..75a082ade7 100644 --- a/source/lib/sysdep/os_cpu.h +++ b/source/lib/sysdep/os_cpu.h @@ -74,12 +74,6 @@ size_t os_cpu_NumProcessors(); //----------------------------------------------------------------------------- // CPU and memory characteristics -/** - * @return a rough estimate of the CPU clock frequency. - * this is usually accurate to a few MHz and is faster than measurement loops. - **/ -double os_cpu_ClockFrequency(); - /** * @return the size [bytes] of a MMU page (4096 on most IA-32 systems) **/ diff --git a/source/lib/timer.h b/source/lib/timer.h index 3594510783..98009c0c16 100644 --- a/source/lib/timer.h +++ b/source/lib/timer.h @@ -35,11 +35,6 @@ #include #include -#if ARCH_X86_X64 && CONFIG2_TIMER_ALLOW_RDTSC -# include "lib/sysdep/os_cpu.h" // os_cpu_ClockFrequency -# include "lib/sysdep/arch/x86_x64/x86_x64.h" // x86_x64::rdtsc -#endif - /** * timer_Time will subsequently return values relative to the current time. **/