From fd16e69464c274682ecc4c7c7c1311fe82d25b38 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Sat, 18 May 2024 01:46:56 +0800 Subject: [PATCH] CPU (macOS): fix core count detection --- src/detection/cpu/cpu_apple.c | 38 ++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/detection/cpu/cpu_apple.c b/src/detection/cpu/cpu_apple.c index ff7a5d304..8c0f72827 100644 --- a/src/detection/cpu/cpu_apple.c +++ b/src/detection/cpu/cpu_apple.c @@ -55,19 +55,6 @@ static const char* detectFrequency(FFCPUResult* cpu) cpu->frequencyMin = (pMin < eMin ? pMin : eMin) / (1000.0 * 1000 * 1000); - int nPerfLevels = ffSysctlGetInt("hw.nperflevels", 0); - if (nPerfLevels > 0) - { - if (nPerfLevels > (int) sizeof(cpu->coreCounts)) - nPerfLevels = (int) sizeof(cpu->coreCounts); - char sysctlKey[] = "hw.perflevelN.logicalcpu"; - for (int i = 0; i < nPerfLevels; ++i) - { - sysctlKey[strlen("hw.perflevel")] = (char) ('0' + i); - cpu->coreCounts[i] = (uint8_t) ffSysctlGetInt(sysctlKey, 0); - } - } - if (pCoreLength >= 8) { ffCfDictGetData(properties, CFSTR("voltage-states5-sram"), pCoreLength - 8, 4, (uint8_t*) &aMax, NULL); @@ -81,20 +68,34 @@ static const char* detectFrequency(FFCPUResult* cpu) { cpu->frequencyBase = ffSysctlGetInt64("hw.cpufrequency", 0) / 1000.0 / 1000.0 / 1000.0; cpu->frequencyMin = ffSysctlGetInt64("hw.cpufrequency_min", 0) / 1000.0 / 1000.0 / 1000.0; - cpu->frequencyMax = ffSysctlGetInt64("hw.cpufrequency_max", 0); - if(cpu->frequencyMax > 0.0) - cpu->frequencyMax /= 1000.0 * 1000.0 * 1000.0; - else + cpu->frequencyMax = ffSysctlGetInt64("hw.cpufrequency_max", 0) / 1000.0 / 1000.0 / 1000.0; + if(cpu->frequencyBase != cpu->frequencyBase) { unsigned current = 0; size_t size = sizeof(current); if (sysctl((int[]){ CTL_HW, HW_CPU_FREQ }, 2, ¤t, &size, NULL, 0) == 0) - cpu->frequencyMax = (double) current / 1000.0 / 1000.0 / 1000.0; + cpu->frequencyBase = (double) current / 1000.0 / 1000.0 / 1000.0; } return NULL; } #endif +static const char* detectCoreCount(FFCPUResult* cpu) +{ + int nPerfLevels = ffSysctlGetInt("hw.nperflevels", 0); + if (nPerfLevels <= 0) return "sysctl(hw.nperflevels) failed"; + + if (nPerfLevels > (int) sizeof(cpu->coreCounts)) + nPerfLevels = (int) sizeof(cpu->coreCounts); + char sysctlKey[] = "hw.perflevelN.logicalcpu"; + for (int i = 0; i < nPerfLevels; ++i) + { + sysctlKey[strlen("hw.perflevel")] = (char) ('0' + i); + cpu->coreCounts[i] = (uint8_t) ffSysctlGetInt(sysctlKey, 0); + } + return NULL; +} + const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu) { if (ffSysctlGetString("machdep.cpu.brand_string", &cpu->name) != NULL) @@ -117,6 +118,7 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu) cpu->coresOnline = (uint16_t) ffSysctlGetInt("hw.activecpu", 1); detectFrequency(cpu); + detectCoreCount(cpu); cpu->temperature = options->temp ? detectCpuTemp(&cpu->name) : FF_CPU_TEMP_UNSET;