Skip to content

Commit 5ce2b45

Browse files
rimrulGermanAizek
andcommitted
win32: thread-utils: handle multi-socket systems
While the currently used way to detect the number of CPU cores on Windows is nice and straight-forward, GetSystemInfo() only gives us access to the number of processors within the current group. [1] 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. [1] https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-system_info#members This fixes #4766 Co-Authored-by: Herman Semenov <GermanAizek@yandex.ru> Signed-off-by: Matthias Aßhauer <mha1993@live.de>
1 parent 3b2e009 commit 5ce2b45

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)