minor cpuid cleanup

This commit is contained in:
Doug MacEachern 2008-04-10 17:15:42 +00:00
parent a16f868a0b
commit da647fe47e
1 changed files with 26 additions and 34 deletions

View File

@ -485,71 +485,63 @@ double sigar_file_system_usage_calc_used(sigar_t *sigar,
#if defined(__linux__) || defined(DARWIN) #if defined(__linux__) || defined(DARWIN)
#define INTEL_ID 0x756e6547 typedef struct {
#define AMD_ID 0x68747541 sigar_uint32_t eax;
sigar_uint32_t ebx;
sigar_uint32_t ecx;
sigar_uint32_t edx;
} sigar_cpuid_t;
#if defined(__i386__) #if defined(__i386__)
#define SIGAR_HAS_CPUID #define SIGAR_HAS_CPUID
static void sigar_cpuid(sigar_uint32_t request, static void sigar_cpuid(sigar_uint32_t request, sigar_cpuid_t *id)
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: */ /* derived from: */
/* http://svn.red-bean.com/repos/minor/trunk/gc/barriers-ia-32.c */ /* http://svn.red-bean.com/repos/minor/trunk/gc/barriers-ia-32.c */
asm volatile ("mov %%ebx, %%esi\n\t" asm volatile ("mov %%ebx, %%esi\n\t"
"cpuid\n\t" "cpuid\n\t"
"xchgl %%ebx, %%esi" "xchgl %%ebx, %%esi"
: "=a" (*eax), : "=a" (id->eax),
"=S" (*ebx), "=S" (id->ebx),
"=c" (*ecx), "=c" (id->ecx),
"=d" (*edx) "=d" (id->edx)
: "0" (request) : "0" (request)
: "memory"); : "memory");
#endif
} }
#elif defined(__amd64__) #elif defined(__amd64__)
#define SIGAR_HAS_CPUID #define SIGAR_HAS_CPUID
static void sigar_cpuid(unsigned int request, static void sigar_cpuid(sigar_uint32_t request,
unsigned int *eax, sigar_cpuid_t *id)
unsigned int *ebx,
unsigned int *ecx,
unsigned int *edx)
{ {
/* http://svn.red-bean.com/repos/minor/trunk/gc/barriers-amd64.c */ /* http://svn.red-bean.com/repos/minor/trunk/gc/barriers-amd64.c */
asm volatile ("cpuid\n\t" asm volatile ("cpuid\n\t"
: "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) : "=a" (id->eax),
"=b" (id->ebx),
"=c" (id->ecx),
"=d" (id->edx)
: "0" (request) : "0" (request)
: "memory"); : "memory");
} }
#endif #endif
#define INTEL_ID 0x756e6547
#define AMD_ID 0x68747541
int sigar_cpu_core_count(sigar_t *sigar) int sigar_cpu_core_count(sigar_t *sigar)
{ {
#ifdef SIGAR_HAS_CPUID #ifdef SIGAR_HAS_CPUID
sigar_uint32_t eax, ebx, ecx, edx; sigar_cpuid_t id;
if (sigar->lcpu == -1) { if (sigar->lcpu == -1) {
sigar->lcpu = 1; sigar->lcpu = 1;
sigar_cpuid(0, &eax, &ebx, &ecx, &edx); sigar_cpuid(0, &id);
if ((ebx == INTEL_ID) || (ebx == AMD_ID)) { if ((id.ebx == INTEL_ID) || (id.ebx == AMD_ID)) {
sigar_cpuid(1, &eax, &ebx, &ecx, &edx); sigar_cpuid(1, &id);
if (edx & (1<<28)) { if (id.edx & (1<<28)) {
sigar->lcpu = (ebx & 0x00FF0000) >> 16; sigar->lcpu = (id.ebx & 0x00FF0000) >> 16;
} }
} }