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

Fixed get count threads for multi-cpu system with NUMA architecture #84842

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions core/os/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@

#include <stdarg.h>
#include <thread>
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#endif

OS *OS::singleton = nullptr;
uint64_t OS::target_ticks = 0;
Expand Down Expand Up @@ -359,7 +362,46 @@ String OS::get_unique_id() const {
}

int OS::get_processor_count() const {
#if defined(_WIN32) || defined(_WIN64)
Copy link
Member

Choose a reason for hiding this comment

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

Windows specific code should be override in OS_Widnows.

DWORD length = 0;
int concurrency = 0;
const auto validConcurrency = [&concurrency]() noexcept -> int {
Copy link
Member

Choose a reason for hiding this comment

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

We avoid lambdas unless necessary

Copy link
Member

Choose a reason for hiding this comment

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

We do not use auto, and lamdas only used if absolutely necessary.

if (concurrency == 0)
return std::thread::hardware_concurrency();
else
return concurrency;
};
if (GetLogicalProcessorInformationEx(RelationAll, nullptr, &length) != FALSE) {
return validConcurrency();
}
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
return validConcurrency();
}
std::unique_ptr<void, void (*)(void *)> buffer(std::malloc(length), std::free);
Copy link
Member

Choose a reason for hiding this comment

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

Same for std::unique_ptr, use memalloc / memfree instead of custom std::malloc. Or Vector<uint8_t>.

if (!buffer) {
return validConcurrency();
}
auto *mem = reinterpret_cast<unsigned char *>(buffer.get());
if (GetLogicalProcessorInformationEx(
RelationAll, reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(mem), &length) == false) {
return validConcurrency();
}
DWORD i = 0;
while (i < length) {
const auto *proc = reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(mem + i);
Copy link
Member

Choose a reason for hiding this comment

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

auto is not allowed

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 validConcurrency();
#else
return std::thread::hardware_concurrency();
#endif
}

String OS::get_processor_name() const {
Expand Down