Skip to content

Commit 480fde3

Browse files
committed
Use sysctl instead of the cpuid method to detect the physical core count
1 parent 5556a69 commit 480fde3

File tree

1 file changed

+4
-35
lines changed

1 file changed

+4
-35
lines changed

smc.c

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -206,41 +206,10 @@ unsigned long getCoreArgCount(const char *arg) {
206206
}
207207

208208
int getPhysicalCoreCount() {
209-
// https://stackoverflow.com/a/150971
210-
int mib[4];
211-
int numCPU;
212-
size_t len = sizeof(numCPU);
213-
214-
/* set the mib for hw.ncpu */
215-
mib[0] = CTL_HW;
216-
mib[1] = HW_NCPU; // alternatively, try HW_NCPU;
217-
218-
/* get the number of CPUs from the system */
219-
sysctl(mib, 2, &numCPU, &len, NULL, 0);
220-
221-
if (numCPU < 1) {
222-
mib[1] = HW_NCPU;
223-
sysctl(mib, 2, &numCPU, &len, NULL, 0);
224-
if (numCPU < 1)
225-
numCPU = 1;
226-
}
227-
228-
// https://stackoverflow.com/a/29761237
229-
uint32_t registers[4];
230-
__asm__ __volatile__("cpuid "
231-
: "=a"(registers[0]),
232-
"=b"(registers[1]),
233-
"=c"(registers[2]),
234-
"=d"(registers[3])
235-
: "a"(1), "c"(0));
236-
237-
unsigned cpuFeatureSet = registers[3];
238-
bool hyperthreading = cpuFeatureSet & (1 << 28);
239-
240-
if (hyperthreading)
241-
return numCPU / 2;
242-
else
243-
return numCPU;
209+
int coreCount;
210+
size_t len = sizeof(coreCount);
211+
sysctlbyname("hw.physicalcpu_max", &coreCount, &len, NULL, 0);
212+
return coreCount;
244213
}
245214

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

0 commit comments

Comments
 (0)