0ad/source/lib/sysdep/linux/lcpu.cpp
janwas c0ed950657 had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).

it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.

after several hours, the code now requires fewer casts and less
guesswork.

other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.

This was SVN commit r5942.
2008-05-11 18:48:32 +00:00

77 lines
1.5 KiB
C++

#include "precompiled.h"
#include "../cpu.h"
#if OS_LINUX
#include "valgrind.h"
#endif
double cpu_ClockFrequency()
{
return -1; // don't know
}
size_t cpu_NumProcessors()
{
long res = sysconf(_SC_NPROCESSORS_CONF);
if (res == -1)
return 0;
else
return (size_t)res;
}
size_t cpu_PageSize()
{
return (size_t)sysconf(_SC_PAGESIZE);
}
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 cpu_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 cpu_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;
}