Skip to content

Commit 3ec7e94

Browse files
committed
CPU (Windows): uses a better way to fetch core types
1 parent a720e1f commit 3ec7e94

1 file changed

Lines changed: 30 additions & 29 deletions

File tree

src/detection/cpu/cpu_windows.c

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,21 @@ static const char* detectMaxSpeedBySmbios(FFCPUResult* cpu) {
216216
return NULL;
217217
}
218218

219-
static const char* detectNCores(FFCPUResult* cpu) {
219+
static uint32_t getNumLogicalCores(const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* ptr) {
220+
uint32_t num = 0;
221+
for (uint32_t i = 0; i < ptr->Processor.GroupCount; ++i) {
222+
num += (uint32_t)
223+
#if _WIN64
224+
__builtin_popcountll
225+
#else
226+
__builtin_popcountl
227+
#endif
228+
(ptr->Processor.GroupMask[i].Mask);
229+
}
230+
return num;
231+
}
232+
233+
static const char* detectNCores(const FFCPUOptions* options, FFCPUResult* cpu) {
220234
LOGICAL_PROCESSOR_RELATIONSHIP lpr = RelationAll;
221235
ULONG length = 0;
222236
NtQuerySystemInformationEx(SystemLogicalProcessorAndGroupInformation, &lpr, sizeof(lpr), NULL, 0, &length);
@@ -242,6 +256,19 @@ static const char* detectNCores(FFCPUResult* cpu) {
242256
}
243257
} else if (ptr->Relationship == RelationProcessorCore) {
244258
++cpu->coresPhysical;
259+
260+
if (options->showPeCoreCount) {
261+
for (uint32_t i = 0; i < ARRAY_SIZE(cpu->coreTypes); ++i) {
262+
if (ptr->Processor.EfficiencyClass + 1 == cpu->coreTypes[i].freq) {
263+
cpu->coreTypes[i].count += getNumLogicalCores(ptr);
264+
break;
265+
} else if (cpu->coreTypes[i].freq == 0) {
266+
cpu->coreTypes[i].freq = ptr->Processor.EfficiencyClass + 1;
267+
cpu->coreTypes[i].count += getNumLogicalCores(ptr);
268+
break;
269+
}
270+
}
271+
}
245272
} else if (ptr->Relationship == RelationProcessorPackage) {
246273
++cpu->packages;
247274
} else if (ptr->Relationship == RelationNumaNode) {
@@ -258,7 +285,7 @@ static const char* detectByRegistry(FFCPUResult* cpu) {
258285
return "ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, L\"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\", &hKey, NULL) failed";
259286
}
260287

261-
if (ffRegReadValues(hKey, 3, (FFRegValueArg[]) {
288+
if (ffRegReadValues(hKey, 3, (FFRegValueArg[]){
262289
FF_ARG(cpu->name, L"ProcessorNameString"),
263290
FF_ARG(cpu->vendor, L"VendorIdentifier"),
264291
FF_ARG(cpu->frequencyBase, L"~MHz"),
@@ -272,41 +299,15 @@ static const char* detectByRegistry(FFCPUResult* cpu) {
272299
return NULL;
273300
}
274301

275-
static const char* detectCoreTypes(FFCPUResult* cpu) {
276-
FF_AUTO_FREE PROCESSOR_POWER_INFORMATION* pinfo = calloc(cpu->coresLogical, sizeof(PROCESSOR_POWER_INFORMATION));
277-
if (!NT_SUCCESS(NtPowerInformation(ProcessorInformation, NULL, 0, pinfo, (ULONG) sizeof(PROCESSOR_POWER_INFORMATION) * cpu->coresLogical))) {
278-
return "NtPowerInformation(ProcessorInformation, NULL, 0, pinfo, size) failed";
279-
}
280-
281-
for (uint32_t icore = 0; icore < cpu->coresLogical && pinfo[icore].MhzLimit; ++icore) {
282-
uint32_t ifreq = 0;
283-
while (cpu->coreTypes[ifreq].freq != pinfo[icore].MhzLimit && cpu->coreTypes[ifreq].freq > 0) {
284-
++ifreq;
285-
}
286-
if (cpu->coreTypes[ifreq].freq == 0) {
287-
cpu->coreTypes[ifreq].freq = pinfo[icore].MhzLimit;
288-
}
289-
++cpu->coreTypes[ifreq].count;
290-
}
291-
292-
if (cpu->frequencyBase == 0) {
293-
cpu->frequencyBase = pinfo->MaxMhz;
294-
}
295-
return NULL;
296-
}
297-
298302
const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu) {
299-
detectNCores(cpu);
303+
detectNCores(options, cpu);
300304

301305
const char* error = detectByRegistry(cpu);
302306
if (error) {
303307
return error;
304308
}
305309

306310
ffCPUDetectByCpuid(cpu);
307-
if (options->showPeCoreCount) {
308-
detectCoreTypes(cpu);
309-
}
310311

311312
if (cpu->frequencyMax == 0) {
312313
detectMaxSpeedBySmbios(cpu);

0 commit comments

Comments
 (0)