mirror of
https://gitea.wildfiregames.com/0ad/0ad
synced 2026-06-16 05:13:58 -07:00
fix documentation of ceil_log2 relating to input=0 remove os_cpu_SetThreadAffinity (redundant) wcpu: make max # processors available via os_cpu.h; remove warning if process affinity is restricted This was SVN commit r5951.
127 lines
2.4 KiB
C++
127 lines
2.4 KiB
C++
#include "precompiled.h"
|
|
|
|
#include "lib/sysdep/os_cpu.h
|
|
|
|
#if OS_LINUX
|
|
#include "valgrind.h"
|
|
#endif
|
|
|
|
|
|
double os_cpu_ClockFrequency()
|
|
{
|
|
return -1; // don't know
|
|
}
|
|
|
|
|
|
size_t os_cpu_NumProcessors()
|
|
{
|
|
static size_t numProcessors;
|
|
|
|
if(numProcessors == 0)
|
|
{
|
|
// Valgrind reports the number of real CPUs, but only emulates a single CPU.
|
|
// That causes problems when we expect all those CPUs to be distinct, so
|
|
// just pretend there's only one CPU
|
|
if (RUNNING_ON_VALGRIND)
|
|
numProcessors = 1;
|
|
else
|
|
{
|
|
long res = sysconf(_SC_NPROCESSORS_CONF);
|
|
debug_assert(res != -1);
|
|
numProcessors = (size_t)res;
|
|
}
|
|
}
|
|
|
|
return numProcessors;
|
|
}
|
|
|
|
|
|
uintptr_t os_cpu_ProcessorMask()
|
|
{
|
|
static uintptr_t processorMask;
|
|
|
|
if(!processorMask)
|
|
processorMask = bit_mask<uintptr_t>(os_cpu_NumProcessors());
|
|
|
|
return processorMask;
|
|
}
|
|
|
|
|
|
size_t os_cpu_PageSize()
|
|
{
|
|
static size_t pageSize;
|
|
|
|
if(!pageSize)
|
|
pageSize = (size_t)sysconf(_SC_PAGESIZE);
|
|
|
|
return pageSize;
|
|
}
|
|
|
|
|
|
size_t os_cpu_LargePageSize()
|
|
{
|
|
// assume they're unsupported.
|
|
return 0;
|
|
}
|
|
|
|
|
|
size_t os_cpu_MemorySize()
|
|
{
|
|
static size_t memorySize;
|
|
|
|
if(!memorySize)
|
|
memorySize = sysconf(_SC_PHYS_PAGES) * os_cpu_PageSize();
|
|
|
|
return memorySize;
|
|
}
|
|
|
|
|
|
size_t os_cpu_MemoryAvailable()
|
|
{
|
|
const size_t memoryAvailable = sysconf(_SC_AVPHYS_PAGES) * os_cpu_PageSize();
|
|
return memoryAvailable;
|
|
}
|
|
|
|
|
|
uintptr_t os_cpu_SetThreadAffinityMask(uintptr_t processorMask)
|
|
{
|
|
int ret;
|
|
cpu_set_t set;
|
|
|
|
uintptr_t previousProcessorMask = 0;
|
|
{
|
|
ret = sched_getaffinity(0, sizeof(set), &set);
|
|
debug_assert(ret == 0);
|
|
|
|
for(size_t processor = 0; processor < os_cpu_NumProcessors(); processor++)
|
|
{
|
|
if(CPU_ISSET(processor, &set))
|
|
previousProcessorMask |= uintptr_t(1) << processor;
|
|
}
|
|
}
|
|
|
|
CPU_ZERO(&set);
|
|
for(size_t processor = 0; processor < os_cpu_NumProcessors(); processor++)
|
|
{
|
|
if(IsBitSet(processorMask, processor))
|
|
CPU_SET(processor, &set);
|
|
}
|
|
|
|
ret = sched_setaffinity(0, sizeof(set), &set);
|
|
debug_assert(ret == 0);
|
|
// (The process gets migrated immediately by the setaffinity call)
|
|
|
|
return previousProcessorMask;
|
|
}
|
|
|
|
|
|
LibError cpu_CallByEachCPU(OsCpuCallback cb, uintptr_t cbData)
|
|
{
|
|
for(size_t processor = 0; processor < os_cpu_NumProcessors(); processor++)
|
|
{
|
|
os_cpu_SetThreadAffinity(processor);
|
|
cb(processor, cbData);
|
|
}
|
|
|
|
return INFO::OK;
|
|
}
|