On OS X, Is there a way to find out which CPU a thread is running on? An equivalent function for Linux is sched_getcpu
GetCurrentProcessorNumber example shows code that implements this functionality using the CPUID instruction. I have tried it myself and can confirm it works on Mac OS X.
Here is my version which I used on Mac OS X
#include <cpuid.h>
#define CPUID(INFO, LEAF, SUBLEAF) __cpuid_count(LEAF, SUBLEAF, INFO[0], INFO[1], INFO[2], INFO[3])
#define GETCPU(CPU) { \
uint32_t CPUInfo[4]; \
CPUID(CPUInfo, 1, 0); \
/* CPUInfo[1] is EBX, bits 24-31 are APIC ID */ \
if ( (CPUInfo[3] & (1 << 9)) == 0) { \
CPU = -1; /* no APIC on chip */ \
} \
else { \
CPU = (unsigned)CPUInfo[1] >> 24; \
} \
if (CPU < 0) CPU = 0; \
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With