Skip to content

Commit 7c2288d

Browse files
authored
Detect number of cores better on multi-socket systems (#6108)
While the currently used way to detect the number of CPU cores ond Windows is nice and straight-forward, GetSystemInfo() only [gives us access to the number of processors within the current group.](https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-system_info#members) While that is usually fine for systems with a single physical CPU, separate physical sockets are typically separate groups. Switch to using GetLogicalProcessorInformationEx() to handle multi-socket systems better. I've tested this on a physical single-socket x86-64 and a physical dual-socket x86-64 system, and on a virtual single-socket ARM64 system. Physical [multi-socket ARM64 systems seem to exist](https://cloudbase.it/ampere-altra-industry-leading-arm64-server/), but I don't have access to such hardware and the hypervisor I use apparently can't emulate that either.
2 parents d14b572 + 5ce2b45 commit 7c2288d

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

thread-utils.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,28 @@ int online_cpus(void)
2828
#endif
2929

3030
#ifdef GIT_WINDOWS_NATIVE
31-
SYSTEM_INFO info;
32-
GetSystemInfo(&info);
33-
34-
if ((int)info.dwNumberOfProcessors > 0)
35-
return (int)info.dwNumberOfProcessors;
31+
DWORD len = 0;
32+
if (!GetLogicalProcessorInformationEx(RelationProcessorCore, NULL, &len) && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
33+
uint8_t *buf = malloc(len);
34+
if (buf) {
35+
if (GetLogicalProcessorInformationEx(RelationProcessorCore, (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX) buf, &len)) {
36+
DWORD offset = 0;
37+
int n_cores = 0;
38+
while (offset < len) {
39+
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX info = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX) (buf + offset);
40+
offset += info->Size;
41+
/* The threads within a core always share a single group. We need to count the bits in the mask to get a thread count. */
42+
for (KAFFINITY mask = info->Processor.GroupMask[0].Mask; mask; mask >>= 1)
43+
n_cores += mask &1;
44+
}
45+
if (n_cores) {
46+
free(buf);
47+
return n_cores;
48+
}
49+
}
50+
free(buf);
51+
}
52+
}
3653
#elif defined(hpux) || defined(__hpux) || defined(_hpux)
3754
struct pst_dynamic psd;
3855

0 commit comments

Comments
 (0)