Skip to content

Commit

Permalink
Use sysctl instead of the cpuid method to detect the physical core count
Browse files Browse the repository at this point in the history
  • Loading branch information
hacker1024 committed May 9, 2021
1 parent 5556a69 commit 480fde3
Showing 1 changed file with 4 additions and 35 deletions.
39 changes: 4 additions & 35 deletions smc.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,41 +206,10 @@ unsigned long getCoreArgCount(const char *arg) {
}

int getPhysicalCoreCount() {
// https://stackoverflow.com/a/150971
int mib[4];
int numCPU;
size_t len = sizeof(numCPU);

/* set the mib for hw.ncpu */
mib[0] = CTL_HW;
mib[1] = HW_NCPU; // alternatively, try HW_NCPU;

/* get the number of CPUs from the system */
sysctl(mib, 2, &numCPU, &len, NULL, 0);

if (numCPU < 1) {
mib[1] = HW_NCPU;
sysctl(mib, 2, &numCPU, &len, NULL, 0);
if (numCPU < 1)
numCPU = 1;
}

// https://stackoverflow.com/a/29761237
uint32_t registers[4];
__asm__ __volatile__("cpuid "
: "=a"(registers[0]),
"=b"(registers[1]),
"=c"(registers[2]),
"=d"(registers[3])
: "a"(1), "c"(0));

unsigned cpuFeatureSet = registers[3];
bool hyperthreading = cpuFeatureSet & (1 << 28);

if (hyperthreading)
return numCPU / 2;
else
return numCPU;
int coreCount;
size_t len = sizeof(coreCount);
sysctlbyname("hw.physicalcpu_max", &coreCount, &len, NULL, 0);
return coreCount;
}

void getCoreNumbers(char *arg, unsigned long *cores, char *errorMsg) {
Expand Down

0 comments on commit 480fde3

Please sign in to comment.