From 81485baadbaba2db248fbaa5df3072f5eed23baa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sun, 19 May 2024 18:23:18 +0800 Subject: [PATCH] CPU: better compatibility for Intel CPUs --- src/detection/cpu/cpu.h | 2 +- src/detection/cpu/cpu_bsd.c | 9 ++++----- src/detection/cpu/cpu_linux.c | 17 ++++++++--------- src/detection/cpu/cpu_windows.c | 16 +++++++--------- 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/detection/cpu/cpu.h b/src/detection/cpu/cpu.h index 982310cdb..2c69487bb 100644 --- a/src/detection/cpu/cpu.h +++ b/src/detection/cpu/cpu.h @@ -23,7 +23,7 @@ typedef struct FFCPUResult double frequencyMax; // GHz double frequencyMin; // GHz - FFCPUCore coreTypes[8]; // number of P cores, E cores, etc. + FFCPUCore coreTypes[16]; // number of P cores, E cores, etc. double temperature; } FFCPUResult; diff --git a/src/detection/cpu/cpu_bsd.c b/src/detection/cpu/cpu_bsd.c index 2310ac432..80f767ca8 100644 --- a/src/detection/cpu/cpu_bsd.c +++ b/src/detection/cpu/cpu_bsd.c @@ -56,12 +56,11 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu) if (!(cpu->frequencyMin <= fmin)) cpu->frequencyMin = fmin; // Counting for NaN if (!(cpu->frequencyMax >= fmax)) cpu->frequencyMax = fmax; - if (cpu->coreTypes[ifreq].freq != fmax) - { - if (cpu->coreTypes[ifreq].count && ifreq < sizeof(cpu->coreTypes) / sizeof(cpu->coreTypes[0])) - ++ifreq; + uint32_t ifreq = 0; + while (cpu->coreTypes[ifreq].freq != fmax && cpu->coreTypes[ifreq].freq > 0) + ++ifreq; + if (cpu->coreTypes[ifreq].freq == 0) cpu->coreTypes[ifreq].freq = fmax; - } cpu->coreTypes[ifreq].count++; } } diff --git a/src/detection/cpu/cpu_linux.c b/src/detection/cpu/cpu_linux.c index fdcd2e3e3..6034bd5de 100644 --- a/src/detection/cpu/cpu_linux.c +++ b/src/detection/cpu/cpu_linux.c @@ -136,7 +136,6 @@ static bool detectFrequency(FFCPUResult* cpu) FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate(); uint32_t baseLen = path.length; - uint32_t ifreq = 0; struct dirent* entry; while ((entry = readdir(dir)) != NULL) { @@ -158,14 +157,6 @@ static bool detectFrequency(FFCPUResult* cpu) cpu->frequencyMax = cpu->frequencyMax > fmax ? cpu->frequencyMax : fmax; else cpu->frequencyMax = fmax; - - if (cpu->coreTypes[ifreq].freq != fmax) - { - if (cpu->coreTypes[ifreq].count && ifreq < sizeof(cpu->coreTypes) / sizeof(cpu->coreTypes[0])) - ++ifreq; - cpu->coreTypes[ifreq].freq = fmax; - } - cpu->coreTypes[ifreq].count += getNumCores(&path, &buffer); } uint32_t fmin = getFrequency(&path, "/cpuinfo_min_freq", "/scaling_min_freq", &buffer); if (fmin > 0) @@ -175,6 +166,14 @@ static bool detectFrequency(FFCPUResult* cpu) else cpu->frequencyMin = fmin; } + + uint32_t freq = fbase <= 0 ? fmax : fbase; // seems base frequencies are more stable + uint32_t ifreq = 0; + while (cpu->coreTypes[ifreq].freq != freq && cpu->coreTypes[ifreq].freq > 0) + ++ifreq; + if (cpu->coreTypes[ifreq].freq == 0) + cpu->coreTypes[ifreq].freq = freq; + cpu->coreTypes[ifreq].count += getNumCores(&path, &buffer); ffStrbufSubstrBefore(&path, baseLen); } } diff --git a/src/detection/cpu/cpu_windows.c b/src/detection/cpu/cpu_windows.c index 56cd5c839..9e27c7eba 100644 --- a/src/detection/cpu/cpu_windows.c +++ b/src/detection/cpu/cpu_windows.c @@ -166,16 +166,14 @@ static const char* detectCoreTypes(FFCPUResult* cpu) if (!NT_SUCCESS(NtPowerInformation(ProcessorInformation, NULL, 0, pinfo, (ULONG) sizeof(PROCESSOR_POWER_INFORMATION) * cpu->coresLogical))) return "NtPowerInformation(ProcessorInformation, NULL, 0, pinfo, size) failed"; - uint32_t ifreq = 0; - for (uint32_t i = 0; i < cpu->coresLogical && pinfo[i].MhzLimit; ++i) + for (uint32_t icore = 0; icore < cpu->coresLogical && pinfo[icore].MhzLimit; ++icore) { - if (cpu->coreTypes[ifreq].freq != pinfo[i].MhzLimit) - { - if (cpu->coreTypes[ifreq].count && ifreq < sizeof(cpu->coreTypes) / sizeof(cpu->coreTypes[0])) - ++ifreq; - cpu->coreTypes[ifreq].freq = pinfo[i].MhzLimit; - } - cpu->coreTypes[ifreq].count++; + uint32_t ifreq = 0; + while (cpu->coreTypes[ifreq].freq != pinfo[icore].MhzLimit && cpu->coreTypes[ifreq].freq > 0) + ++ifreq; + if (cpu->coreTypes[ifreq].freq == 0) + cpu->coreTypes[ifreq].freq = pinfo[icore].MhzLimit; + ++cpu->coreTypes[ifreq].count; } if (cpu->frequencyBase != cpu->frequencyBase)