Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#4766] Win32: Added support NUMA configuration with multiple count CPU sockets #4896

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 66 additions & 5 deletions thread-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,67 @@
# endif
#endif

#ifdef GIT_WINDOWS_NATIVE
/// defines the GetLogicalProcessorInformationEx function
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid C++-style comments. The existing code does not use them.

typedef BOOL (*WINAPI glpie_t)(
LOGICAL_PROCESSOR_RELATIONSHIP,
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX,
PDWORD);
Comment on lines +23 to +26
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this code to compat/win32/ and use lazyload.h (imitate existing code that declares and initializes the function pointer).


// Classic way to get number of cores in windows.
static size_t get_windows_compatible_n_cores() {

Check failure on line 29 in thread-utils.c

View workflow job for this annotation

GitHub Actions / win build

thread-utils.c:29:15: function declaration isn't a prototype [-Werror=strict-prototypes]

Check failure on line 29 in thread-utils.c

View workflow job for this annotation

GitHub Actions / win build

thread-utils.c:29:15: old-style function definition [-Werror=old-style-definition]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not add an extra function here. Instead, let's declare a single win32_online_cpus(void) function in a header file in compat/win32/ and implement it in a .c file. Or put them into compat/mingw.h/compat/mingw.c.

SYSTEM_INFO info;
GetSystemInfo(&info);
if ((int)info.dwNumberOfProcessors > 0)
return (int)info.dwNumberOfProcessors;
}

Check failure on line 34 in thread-utils.c

View workflow job for this annotation

GitHub Actions / win build

thread-utils.c:34:1: control reaches end of non-void function [-Werror=return-type]

/*
* Windows NUMA support is particular
* https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getlogicalprocessorinformationex#remarks
*
* New api here:
* https://learn.microsoft.com/en-us/windows/win32/procthread/numa-support
*/
static size_t get_windows_numa_n_cores() {

Check failure on line 43 in thread-utils.c

View workflow job for this annotation

GitHub Actions / win build

thread-utils.c:43:15: function declaration isn't a prototype [-Werror=strict-prototypes]

Check failure on line 43 in thread-utils.c

View workflow job for this annotation

GitHub Actions / win build

thread-utils.c:43:15: old-style function definition [-Werror=old-style-definition]
uint8_t *buffer = NULL;
size_t n_cores = 0;
DWORD length = 0;
HMODULE kernel32 = GetModuleHandleW(L"kernel32.dll");

glpie_t GetLogicalProcessorInformationEx = (glpie_t)GetProcAddress(kernel32, "GetLogicalProcessorInformationEx");

Check failure on line 49 in thread-utils.c

View workflow job for this annotation

GitHub Actions / win build

thread-utils.c:49:52: cast between incompatible function types from 'FARPROC' {aka 'long long int (*)()'} to 'BOOL (*)(LOGICAL_PROCESSOR_RELATIONSHIP, struct _SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *, DWORD *)' {aka 'int (*)(LOGICAL_PROCESSOR_RELATIONSHIP, struct _SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *, long unsigned int *)'} [-Werror=cast-function-type]
if (!GetLogicalProcessorInformationEx) {
return 0;
}

GetLogicalProcessorInformationEx(RelationAll, NULL, &length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not forget to check the return value.

if (length < 1 || GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
return 0;
}

buffer = malloc(length);
if (!buffer || !GetLogicalProcessorInformationEx(RelationAll, (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)buffer, &length)) {
free(buffer);
return 0;
}

for (DWORD offset = 0; offset < length;) {
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX info = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)(buffer + offset);
offset += info->Size;
if (info->Relationship != RelationProcessorCore) {
continue;
}
for (WORD group = 0; group < info->Processor.GroupCount; ++group) {
for (KAFFINITY mask = info->Processor.GroupMask[group].Mask; mask != 0; mask >>= 1) {
n_cores += mask & 1;
}
}
}
free(buffer);
return n_cores;
}
#endif

int online_cpus(void)
{
#ifdef NO_PTHREADS
Expand All @@ -28,11 +89,11 @@
#endif

#ifdef GIT_WINDOWS_NATIVE
SYSTEM_INFO info;
GetSystemInfo(&info);

if ((int)info.dwNumberOfProcessors > 0)
return (int)info.dwNumberOfProcessors;
size_t n_cores = get_windows_numa_n_cores();
if (n_cores > 0) {
return n_cores;
}
return get_windows_compatible_n_cores();
#elif defined(hpux) || defined(__hpux) || defined(_hpux)
struct pst_dynamic psd;

Expand Down
Loading