-
-
Notifications
You must be signed in to change notification settings - Fork 22k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -359,7 +362,46 @@ String OS::get_unique_id() const { | |
} | ||
|
||
int OS::get_processor_count() const { | ||
#if defined(_WIN32) || defined(_WIN64) | ||
DWORD length = 0; | ||
int concurrency = 0; | ||
const auto validConcurrency = [&concurrency]() noexcept -> int { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We avoid lambdas unless necessary There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do not use |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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 { | ||
|
There was a problem hiding this comment.
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
.