Skip to content

Instantly share code, notes, and snippets.

@mikeb01
Created January 10, 2012 22:22
Show Gist options
  • Save mikeb01/1591569 to your computer and use it in GitHub Desktop.
Save mikeb01/1591569 to your computer and use it in GitHub Desktop.
extern "C" {
/* from sys/osfmk/i386/mp.c */
extern void mp_rendezvous(void (*setup_func)(void *),
void (*action_func)(void *),
void (*teardown_func)(void *),
void *arg);
extern int cpu_number(void);
}
typedef struct {
int os_id;
int socket;
int core;
int apic;
} cpu_info_t;
static void topologyHelperFunction(void* data) {
int values[4];
cpu_info_t* cpus = (cpu_info_t*) data;
int cpu = cpu_number();
cpus[cpu].os_id = cpu;
cpuid(0xB, 0, values);
cpus[cpu].core = values[3] >> values[0] & 0xF
cpuid(0xB, 1, values);
cpus[cpu].socket = values[3] >> values[0] & 0xF
cpus[cpu].apic = values[3];
}
static void getCpuidData() {
int ncpu;
size_t len = sizeof(int);
sysctlbyname("hw.ncpu", &ncpu, &len, NULL, 0);
cpu_info_t* values = (cpu_info_t*) malloc(sizeof(cpu_info_t) * ncpus);
mp_rendevous(NULL, (void (*)(void *))topologyHelperFunction,
NULL, (void*) values);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment