Skip to content

Instantly share code, notes, and snippets.

@Coneko
Created December 7, 2012 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Coneko/4234770 to your computer and use it in GitHub Desktop.
Save Coneko/4234770 to your computer and use it in GitHub Desktop.
How to check what core your code is running on.
// The x86 cpuid instruction gives information about the processor
static inline void cpuid(uint32_t code, uint32_t data[4]) {
asm volatile("cpuid":"=a"(*data),"=b"(*(data+1)),
"=c"(*(data+2)),"=d"(*(data+3)):"a"(code));
}
// Gets the APIC ID of the processor. Returns a unique identifier for each core.
static inline uint8_t apic_id(void) {
uint32_t data[4];
cpuid(1, data);
uint32_t ebx = data[1];
uint8_t apic_id = *((uint8_t *)&ebx + 3);
return apic_id;
}
// More information: http://wiki.osdev.org/Detecting_CPU_Topology_(80x86)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment