Removes CPU frequency calculating functionality

This commit is contained in:
Vladislav Belov committed 2026-10-17 15:53:14 +02:00
1 parent 7fcecda91b
commit dcc66fdc57
8 files changed
+3 -172

No files matched your search

+1 -110
View File
@@ -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<double> 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<std::int64_t>(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
-8
View File
@@ -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
-20
View File
@@ -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;
+1 -3
View File
@@ -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
+1
View File
@@ -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"
-20
View File
@@ -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;
-6
View File
@@ -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)
**/
-5
View File
@@ -35,11 +35,6 @@
#include <cstdint>
#include <string>
#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.
**/