move cpuid from os/linux to sigar_util for use on other platforms

This commit is contained in:
Doug MacEachern committed 2008-04-09 06:29:08 +00:00
1 parent 277cf2e68e
commit 2b9c8971e0
5 files changed
+118 -104

No files matched your search

-102
View File
@@ -210,108 +210,6 @@ char *sigar_os_error_string(sigar_t *sigar, int err)
return NULL;
}
#define INTEL_ID 0x756e6547
#define AMD_ID 0x68747541
#if defined(__i386__)
#define SIGAR_HAS_CPUID
static void sigar_cpuid(sigar_uint32_t request,
sigar_uint32_t *eax,
sigar_uint32_t *ebx,
sigar_uint32_t *ecx,
sigar_uint32_t *edx)
{
#if 0
/* does not compile w/ -fPIC */
/* can't find a register in class `BREG' while reloading `asm' */
asm volatile ("cpuid" :
"=a" (*eax),
"=b" (*ebx),
"=c" (*ecx),
"=d" (*edx)
: "a" (request));
#else
/* derived from: */
/* http://svn.red-bean.com/repos/minor/trunk/gc/barriers-ia-32.c */
asm volatile ("mov %%ebx, %%esi\n\t"
"cpuid\n\t"
"xchgl %%ebx, %%esi"
: "=a" (*eax),
"=S" (*ebx),
"=c" (*ecx),
"=d" (*edx)
: "0" (request)
: "memory");
#endif
}
#elif defined(__amd64__)
#define SIGAR_HAS_CPUID
static void sigar_cpuid(unsigned int request,
unsigned int *eax,
unsigned int *ebx,
unsigned int *ecx,
unsigned int *edx)
{
/* http://svn.red-bean.com/repos/minor/trunk/gc/barriers-amd64.c */
asm volatile ("cpuid\n\t"
: "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
: "0" (request)
: "memory");
}
#endif
#ifdef SIGAR_HAS_CPUID
static int sigar_cpu_core_count(sigar_t *sigar)
{
sigar_uint32_t eax, ebx, ecx, edx;
if (sigar->lcpu == -1) {
sigar->lcpu = 1;
sigar_cpuid(0, &eax, &ebx, &ecx, &edx);
if ((ebx == INTEL_ID) || (ebx == AMD_ID)) {
sigar_cpuid(1, &eax, &ebx, &ecx, &edx);
if (edx & (1<<28)) {
sigar->lcpu = (ebx & 0x00FF0000) >> 16;
}
}
sigar_log_printf(sigar, SIGAR_LOG_DEBUG,
"[cpu] %d cores per socket", sigar->lcpu);
}
return sigar->lcpu;
}
static int sigar_cpu_core_rollup(sigar_t *sigar)
{
(void)sigar_cpu_core_count(sigar);
if (sigar->cpu_list_cores) {
if (sigar->lcpu > 1) {
sigar_log_printf(sigar, SIGAR_LOG_DEBUG,
"[cpu] treating cores as-is");
}
}
else {
if (sigar->lcpu > 1) {
sigar_log_printf(sigar, SIGAR_LOG_DEBUG,
"[cpu] rolling up cores to sockets");
return 1;
}
}
return 0;
}
#else
#define sigar_cpu_core_rollup(sigar) 0
#define sigar_cpu_core_count(sigar) 1
#endif
static int sigar_cpu_total_count(sigar_t *sigar)
{
return (int)sysconf(_SC_NPROCESSORS_CONF);