Skip to content
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
Copy Markdown
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
Copy Markdown
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() {

Copy link
Copy Markdown
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;
}

/*
* 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() {
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");
if (!GetLogicalProcessorInformationEx) {
return 0;
}

GetLogicalProcessorInformationEx(RelationAll, NULL, &length);

Copy link
Copy Markdown
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 @@ int online_cpus(void)
#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