Skip to content

Commit

Permalink
[libc++] Fixed get count threads for multi-cpu system with NUMA archi…
Browse files Browse the repository at this point in the history
…tecture (llvm#72267)
  • Loading branch information
GermanAizek committed Nov 16, 2023
1 parent 94d6699 commit d1fa25f
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions libcxx/src/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,43 @@ thread::hardware_concurrency() noexcept
return 0;
return static_cast<unsigned>(result);
#elif defined(_LIBCPP_WIN32API)
// This implementation supports both conventional single-cpu PC configurations
// and multi-cpu system on NUMA (Non-uniform_memory_access) architecture
DWORD length = 0;
const auto single_cpu_concurrency = []() noexcept -> unsigned int {
SYSTEM_INFO info;
GetSystemInfo(&info);
return info.dwNumberOfProcessors;
};
if (GetLogicalProcessorInformationEx(RelationAll, nullptr, &length) != FALSE) {
return single_cpu_concurrency();
}
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
return single_cpu_concurrency();
}
std::unique_ptr<void, void (*)(void*)> buffer(std::malloc(length), std::free);
if (!buffer) {
return single_cpu_concurrency();
}
auto* mem = reinterpret_cast<unsigned char*>(buffer.get());
if (GetLogicalProcessorInformationEx(
RelationAll, reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(mem), &length) == false) {
return single_cpu_concurrency();
}
DWORD i = 0;
unsigned int concurrency = 0;
while (i < length) {
const auto* proc = reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(mem + i);
if (proc->Relationship == RelationProcessorCore) {
for (WORD group = 0; group < proc->Processor.GroupCount; ++group) {
for (KAFFINITY mask = proc->Processor.GroupMask[group].Mask; mask != 0; mask >>= 1) {
concurrency += mask & 1;
}
}
}
i += proc->Size;
}
return concurrency;
#else // defined(CTL_HW) && defined(HW_NCPU)
// TODO: grovel through /proc or check cpuid on x86 and similar
// instructions on other architectures.
Expand Down

0 comments on commit d1fa25f

Please sign in to comment.