From ab033d84b194e4e40b89a6fc4662880894f548a1 Mon Sep 17 00:00:00 2001 From: Quantum Date: Wed, 1 Sep 2021 06:01:28 -0400 Subject: [PATCH] [common] cpuinfo: handle more than 64 threads on Windows The old code will not correctly report the number of threads on CPUs with more than one processor group, i.e. when there are more than 64 logical processors (threads). --- common/src/platform/windows/cpuinfo.c | 33 ++++++++++++++------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/common/src/platform/windows/cpuinfo.c b/common/src/platform/windows/cpuinfo.c index e20507b4..974b4ffb 100644 --- a/common/src/platform/windows/cpuinfo.c +++ b/common/src/platform/windows/cpuinfo.c @@ -24,16 +24,6 @@ #include -static void getProcessorCount(int * procs) -{ - if (!procs) - return; - - SYSTEM_INFO si; - GetSystemInfo(&si); - *procs = si.dwNumberOfProcessors; -} - static bool getCPUModel(char * model, size_t modelSize) { if (!model) @@ -58,9 +48,9 @@ static bool getCPUModel(char * model, size_t modelSize) return true; } -static bool getCoreCount(int * cores) +static bool getCoreCount(int * cores, int * procs) { - if (!cores) + if (!cores && !procs) return true; DWORD cb = 0; @@ -79,14 +69,26 @@ static bool getCoreCount(int * cores) return false; } - *cores = 0; + if (cores) + *cores = 0; + + if (procs) + *procs = 0; + DWORD offset = 0; while (offset < cb) { PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX lpi = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX) (buffer + offset); if (lpi->Relationship == RelationProcessorCore) - ++*cores; + { + if (cores) + ++*cores; + + if (procs) + for (int i = 0; i < lpi->Processor.GroupCount; ++i) + *procs += __builtin_popcount(lpi->Processor.GroupMask[i].Mask); + } offset += lpi->Size; } @@ -95,6 +97,5 @@ static bool getCoreCount(int * cores) bool lgCPUInfo(char * model, size_t modelSize, int * procs, int * cores) { - getProcessorCount(procs); - return getCPUModel(model, modelSize) && getCoreCount(cores); + return getCPUModel(model, modelSize) && getCoreCount(cores, procs); }