From de6485646ddf3248871e1af6dcd1de180742b8e6 Mon Sep 17 00:00:00 2001 From: Cameron Desrochers Date: Tue, 12 Nov 2019 15:47:27 -0500 Subject: Fixed processor count detection on Windows when multiple processor groups (i.e. >64 processors) are present --- src/util.cc | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/util.cc b/src/util.cc index 4df2bb2..7893f61 100644 --- a/src/util.cc +++ b/src/util.cc @@ -481,6 +481,32 @@ string StripAnsiEscapeCodes(const string& in) { int GetProcessorCount() { #ifdef _WIN32 +#if _WIN32_WINNT >= 0x0601 + // Need to use GetLogicalProcessorInformationEx to get real core count on + // machines with >64 cores. See https://stackoverflow.com/a/31209344/21475 + DWORD len = 0; + if (!GetLogicalProcessorInformationEx(RelationProcessorCore, nullptr, &len) + && GetLastError() == ERROR_INSUFFICIENT_BUFFER) { + std::vector buf(len); + int cores = 0; + if (GetLogicalProcessorInformationEx(RelationProcessorCore, + (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)buf.data(), &len)) { + for (DWORD i = 0; i < len; ) { + PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX info = + (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)(buf.data() + i); + if (info->Relationship == RelationProcessorCore && + info->Processor.GroupCount == 1) { + for (KAFFINITY core_mask = info->Processor.GroupMask[0].Mask; + core_mask; core_mask >>= 1) + cores += (core_mask & 1); + } + i += info->Size; + } + if (cores) + return cores; + } + } +#endif return GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); #else #ifdef CPU_COUNT -- cgit v0.12 From 48a8a7ae95f10d70a4c2f117df820e9ce5f4a0f2 Mon Sep 17 00:00:00 2001 From: Cameron Date: Tue, 12 Nov 2019 21:13:36 -0500 Subject: Use symbolic constant as suggested in code review Co-Authored-By: Takuto Ikuta --- src/util.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util.cc b/src/util.cc index 7893f61..ba63ab3 100644 --- a/src/util.cc +++ b/src/util.cc @@ -481,7 +481,7 @@ string StripAnsiEscapeCodes(const string& in) { int GetProcessorCount() { #ifdef _WIN32 -#if _WIN32_WINNT >= 0x0601 +#if _WIN32_WINNT >= _WIN32_WINNT_WIN7 // Need to use GetLogicalProcessorInformationEx to get real core count on // machines with >64 cores. See https://stackoverflow.com/a/31209344/21475 DWORD len = 0; -- cgit v0.12 From 9d502da06d179d598c2167ca0b97756e19d687c2 Mon Sep 17 00:00:00 2001 From: Cameron Desrochers Date: Wed, 13 Nov 2019 12:17:31 -0500 Subject: Removed unnecessary #ifdef following code review --- src/util.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/util.cc b/src/util.cc index ba63ab3..ac4f247 100644 --- a/src/util.cc +++ b/src/util.cc @@ -481,7 +481,6 @@ string StripAnsiEscapeCodes(const string& in) { int GetProcessorCount() { #ifdef _WIN32 -#if _WIN32_WINNT >= _WIN32_WINNT_WIN7 // Need to use GetLogicalProcessorInformationEx to get real core count on // machines with >64 cores. See https://stackoverflow.com/a/31209344/21475 DWORD len = 0; @@ -506,7 +505,7 @@ int GetProcessorCount() { return cores; } } -#endif + // fallback just in case return GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); #else #ifdef CPU_COUNT -- cgit v0.12 From 546e27c6459b862e3394caad1c71d63da0891041 Mon Sep 17 00:00:00 2001 From: Cameron Desrochers Date: Wed, 20 Nov 2019 16:31:14 -0500 Subject: Style changes following code review --- src/util.cc | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/util.cc b/src/util.cc index ac4f247..9049f90 100644 --- a/src/util.cc +++ b/src/util.cc @@ -489,20 +489,24 @@ int GetProcessorCount() { std::vector buf(len); int cores = 0; if (GetLogicalProcessorInformationEx(RelationProcessorCore, - (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)buf.data(), &len)) { + reinterpret_cast( + buf.data()), &len)) { for (DWORD i = 0; i < len; ) { PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX info = - (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)(buf.data() + i); + reinterpret_cast( + buf.data() + i); if (info->Relationship == RelationProcessorCore && info->Processor.GroupCount == 1) { for (KAFFINITY core_mask = info->Processor.GroupMask[0].Mask; - core_mask; core_mask >>= 1) + core_mask; core_mask >>= 1) { cores += (core_mask & 1); + } } i += info->Size; } - if (cores) + if (cores) { return cores; + } } } // fallback just in case -- cgit v0.12 From 9e1e51aeb56115b94d3cf14e67934dcd51a3f5c6 Mon Sep 17 00:00:00 2001 From: Cameron Date: Wed, 10 Jun 2020 07:32:04 -0400 Subject: Tweaks following code review --- src/util.cc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/util.cc b/src/util.cc index 9049f90..e8455ec 100644 --- a/src/util.cc +++ b/src/util.cc @@ -492,9 +492,8 @@ int GetProcessorCount() { reinterpret_cast( buf.data()), &len)) { for (DWORD i = 0; i < len; ) { - PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX info = - reinterpret_cast( - buf.data() + i); + auto info = reinterpret_cast( + buf.data() + i); if (info->Relationship == RelationProcessorCore && info->Processor.GroupCount == 1) { for (KAFFINITY core_mask = info->Processor.GroupMask[0].Mask; @@ -504,7 +503,7 @@ int GetProcessorCount() { } i += info->Size; } - if (cores) { + if (cores != 0) { return cores; } } -- cgit v0.12 From 4b07ca35eb56b572720521fef7c202ec4cc1e325 Mon Sep 17 00:00:00 2001 From: Cameron Date: Thu, 31 Dec 2020 14:45:59 -0500 Subject: Added preprocessor if to clarify processor count workaround code is only needed on 32-bit builds (following code review) --- src/util.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/util.cc b/src/util.cc index e8455ec..6ea854b 100644 --- a/src/util.cc +++ b/src/util.cc @@ -481,6 +481,7 @@ string StripAnsiEscapeCodes(const string& in) { int GetProcessorCount() { #ifdef _WIN32 +#ifndef _WIN64 // Need to use GetLogicalProcessorInformationEx to get real core count on // machines with >64 cores. See https://stackoverflow.com/a/31209344/21475 DWORD len = 0; @@ -508,7 +509,7 @@ int GetProcessorCount() { } } } - // fallback just in case +#endif return GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); #else #ifdef CPU_COUNT -- cgit v0.12