0ad/source/lib/sysdep/linux/lcpu.cpp
janwas b776e58c67 use (newly added) cpu_PageSize instead of sysconf (avoids a critical order dependency on windows)
refactored sysdep code:
- avoid many #if OS_xxx.
- remove bsd.cpp, merge its contents with osx
- move linux-specific parts of unix/ into linux/

This was SVN commit r5363.
2007-09-23 08:05:38 +00:00

55 lines
1.2 KiB
C++

#include "precompiled.h"
#include "lcpu.h"
#if OS_LINUX
#include "valgrind.h"
#endif
static int SysconfFromMemType(CpuMemoryIndicators mem_type)
{
switch(mem_type)
{
case CPU_MEM_TOTAL:
return _SC_PHYS_PAGES;
case CPU_MEM_AVAILABLE:
return _SC_AVPHYS_PAGES;
}
UNREACHABLE;
}
size_t lcpu_MemorySize(CpuMemoryIndicators mem_type)
{
const int sc_name = SysconfFromMemType(mem_type);
const size_t pageSize = sysconf(_SC_PAGESIZE);
const size_t memory_size = sysconf(sc_name) * pageSize;
return memory_size;
}
LibError lcpu_CallByEachCPU(CpuCallback cb, void* param)
{
long ncpus = sysconf(_SC_NPROCESSORS_CONF);
// 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)
ncpus = 1;
cpu_set_t set;
for (long i = 0; i < ncpus && i < CPU_SETSIZE; ++i)
{
CPU_ZERO(&set);
CPU_SET(i, &set);
int ret = sched_setaffinity(0, sizeof(set), &set);
if (ret)
WARN_RETURN(ERR::FAIL);
// (The process gets migrated immediately by the setaffinity call)
cb(param);
}
return INFO::OK;
}