llvm-project/clang-tools-extra/clangd
Alexandre Ganea 8404aeb56a [Support] On Windows, ensure hardware_concurrency() extends to all CPU sockets and all NUMA groups
The goal of this patch is to maximize CPU utilization on multi-socket or high core count systems, so that parallel computations such as LLD/ThinLTO can use all hardware threads in the system. Before this patch, on Windows, a maximum of 64 hardware threads could be used at most, in some cases dispatched only on one CPU socket.

== Background ==
Windows doesn't have a flat cpu_set_t like Linux. Instead, it projects hardware CPUs (or NUMA nodes) to applications through a concept of "processor groups". A "processor" is the smallest unit of execution on a CPU, that is, an hyper-thread if SMT is active; a core otherwise. There's a limit of 32-bit processors on older 32-bit versions of Windows, which later was raised to 64-processors with 64-bit versions of Windows. This limit comes from the affinity mask, which historically is represented by the sizeof(void*). Consequently, the concept of "processor groups" was introduced for dealing with systems with more than 64 hyper-threads.

By default, the Windows OS assigns only one "processor group" to each starting application, in a round-robin manner. If the application wants to use more processors, it needs to programmatically enable it, by assigning threads to other "processor groups". This also means that affinity cannot cross "processor group" boundaries; one can only specify a "preferred" group on start-up, but the application is free to allocate more groups if it wants to.

This creates a peculiar situation, where newer CPUs like the AMD EPYC 7702P (64-cores, 128-hyperthreads) are projected by the OS as two (2) "processor groups". This means that by default, an application can only use half of the cores. This situation could only get worse in the years to come, as dies with more cores will appear on the market.

== The problem ==
The heavyweight_hardware_concurrency() API was introduced so that only *one hardware thread per core* was used. Once that API returns, that original intention is lost, only the number of threads is retained. Consider a situation, on Windows, where the system has 2 CPU sockets, 18 cores each, each core having 2 hyper-threads, for a total of 72 hyper-threads. Both heavyweight_hardware_concurrency() and hardware_concurrency() currently return 36, because on Windows they are simply wrappers over std:🧵:hardware_concurrency() -- which can only return processors from the current "processor group".

== The changes in this patch ==
To solve this situation, we capture (and retain) the initial intention until the point of usage, through a new ThreadPoolStrategy class. The number of threads to use is deferred as late as possible, until the moment where the std::threads are created (ThreadPool in the case of ThinLTO).

When using hardware_concurrency(), setting ThreadCount to 0 now means to use all the possible hardware CPU (SMT) threads. Providing a ThreadCount above to the maximum number of threads will have no effect, the maximum will be used instead.
The heavyweight_hardware_concurrency() is similar to hardware_concurrency(), except that only one thread per hardware *core* will be used.

When LLVM_ENABLE_THREADS is OFF, the threading APIs will always return 1, to ensure any caller loops will be exercised at least once.

Differential Revision: https://reviews.llvm.org/D71775
2020-02-14 10:24:22 -05:00
..
benchmarks Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
clients/clangd-vscode [clangd] Bump vscode-clangd v0.0.20 2020-01-30 11:45:54 +01:00
fuzzer [clang-tools-extra] [cmake] Link against libclang-cpp whenever possible 2019-10-04 20:30:02 +00:00
include-mapping [clangd] Add include-mapping for C symbols. 2019-06-21 13:32:18 +00:00
index [Support] On Windows, ensure hardware_concurrency() extends to all CPU sockets and all NUMA groups 2020-02-14 10:24:22 -05:00
indexer Revert "Use InitLLVM to setup a pretty stack printer" 2019-11-25 21:06:56 -05:00
refactor [clangd] Add tracer to the rename workflow, NFC 2020-02-14 15:39:29 +01:00
test [clangd] Expose Code Completion score to the client 2020-02-13 15:05:18 +01:00
tool Make llvm::StringRef to std::string conversions explicit. 2020-01-28 23:25:25 +01:00
unittests [clangd] Update the CompletionItemKind. 2020-02-14 15:48:30 +01:00
xpc Fix clangd-xpc-test-client build after 777180a32b 2020-01-28 19:48:31 -05:00
AST.cpp [clangd] Don't assert when completing a lambda variable inside the lambda. 2020-02-04 17:24:26 +01:00
AST.h [clangd] Don't assert when completing a lambda variable inside the lambda. 2020-02-04 17:24:26 +01:00
CMakeLists.txt Revert "[clang-tools-extra] fix the check for if '-latomic' is necessary" 2020-02-14 15:01:52 +00:00
CSymbolMap.inc [clangd] Add include-mapping for C symbols. 2019-06-21 13:32:18 +00:00
Cancellation.cpp Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
Cancellation.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
ClangdLSPServer.cpp [clang] Add `forceReload` clangd extension to 'textDocument/didChange' 2020-02-10 14:02:02 -05:00
ClangdLSPServer.h [clangd] Only re-open files if their flags changed 2020-01-27 10:58:20 -05:00
ClangdServer.cpp [clang] Add `forceReload` clangd extension to 'textDocument/didChange' 2020-02-10 14:02:02 -05:00
ClangdServer.h [clang] Add `forceReload` clangd extension to 'textDocument/didChange' 2020-02-10 14:02:02 -05:00
CodeComplete.cpp [clangd] Update the CompletionItemKind. 2020-02-14 15:48:30 +01:00
CodeComplete.h [clangd] Expose completion range in code completion results (C++ API) 2020-02-11 15:25:03 +01:00
CodeCompletionStrings.cpp Code completion should not ignore default parameters in functions. 2019-08-07 16:52:21 +00:00
CodeCompletionStrings.h [clangd] Place cursor better after completing patterns 2019-05-28 15:33:37 +00:00
CollectMacros.h [clangd] Inactive regions support as an extension to semantic highlighting 2019-11-21 19:40:55 -05:00
CompileCommands.cpp Make llvm::StringRef to std::string conversions explicit. 2020-01-28 23:25:25 +01:00
CompileCommands.h [clangd] Try to fix CompileCommandsTests on windows. NFC 2019-12-06 12:28:01 +01:00
Compiler.cpp [clangd] Log cc1 args at verbose level. 2019-11-29 11:00:01 +01:00
Compiler.h [clang] Add `forceReload` clangd extension to 'textDocument/didChange' 2020-02-10 14:02:02 -05:00
Context.cpp Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
Context.h NFC: Fix trivial typos in comments 2020-01-04 10:28:41 -05:00
Diagnostics.cpp Make llvm::StringRef to std::string conversions explicit. 2020-01-28 23:25:25 +01:00
Diagnostics.h [NFC] Fix typos in Clangd and Clang 2019-12-16 10:54:40 +01:00
DraftStore.cpp Make llvm::StringRef to std::string conversions explicit. 2020-01-28 23:25:25 +01:00
DraftStore.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
ExpectedTypes.cpp Make llvm::StringRef to std::string conversions explicit. 2020-01-28 23:25:25 +01:00
ExpectedTypes.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
FS.cpp Make llvm::StringRef to std::string conversions explicit. 2020-01-28 23:25:25 +01:00
FS.h [clangd] Get rid of dots and dotsdots within GlobalCompilationDatabase 2019-07-18 16:13:23 +00:00
FSProvider.cpp [clangd] Don't mmap source files on all platforms --> don't crash on git checkout 2020-01-29 19:44:14 +01:00
FSProvider.h [clangd] Don't mmap source files on all platforms --> don't crash on git checkout 2020-01-29 19:44:14 +01:00
Features.inc.in [clangd] XPC transport layer 2019-01-16 00:24:22 +00:00
FileDistance.cpp [clangd] Save an unnecessary copy, NFC. 2019-09-24 13:25:38 +00:00
FileDistance.h Move endian constant from Host.h to SwapByteOrder.h, prune include 2019-10-19 00:48:11 +00:00
FindSymbols.cpp Make llvm::StringRef to std::string conversions explicit. 2020-01-28 23:25:25 +01:00
FindSymbols.h [clangd] Type hierarchy subtypes 2019-06-16 02:31:37 +00:00
FindTarget.cpp [clangd] Support renaming designated initializers 2020-02-10 11:53:17 +01:00
FindTarget.h [clangd] targetDecl() returns only NamedDecls. 2020-01-03 18:18:40 +01:00
Format.cpp Make llvm::StringRef to std::string conversions explicit. 2020-01-28 23:25:25 +01:00
Format.h [clangd] Revamp textDocument/onTypeFormatting. 2019-06-10 14:26:21 +00:00
FormattedString.cpp [clangd] Add a ruler after header in hover 2020-01-15 15:54:38 +01:00
FormattedString.h [clangd] Add a ruler after header in hover 2020-01-15 15:54:38 +01:00
Function.h [clangd] Remove Bind, use C++14 lambda captures instead. NFC 2019-08-16 09:20:01 +00:00
FuzzyMatch.cpp [clangd] Tune the fuzzy-matching algorithm 2019-03-15 14:00:49 +00:00
FuzzyMatch.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
GlobalCompilationDatabase.cpp [clangd] Log directory when a CDB is loaded 2020-01-30 12:15:04 +01:00
GlobalCompilationDatabase.h [clangd] (take 2) Try harder to find a plausible `clang` as argv0, particularly on Mac. 2019-12-06 09:47:03 +01:00
HeaderSourceSwitch.cpp Make llvm::StringRef to std::string conversions explicit. 2020-01-28 23:25:25 +01:00
HeaderSourceSwitch.h [clangd] Implement a smart version of HeaderSource switch. 2019-09-30 10:48:02 +00:00
Headers.cpp Make llvm::StringRef to std::string conversions explicit. 2020-01-28 23:25:25 +01:00
Headers.h [clangd][NFC] Typo in comment 2019-07-30 20:39:39 +00:00
Hover.cpp [clang][Index] Introduce a TemplateParm SymbolKind 2020-02-14 13:20:34 +01:00
Hover.h [clangd] Introduce paragraph, the first part of new rendering structs 2019-12-13 09:58:55 +01:00
IncludeFixer.cpp Make llvm::StringRef to std::string conversions explicit. 2020-01-28 23:25:25 +01:00
IncludeFixer.h [clangd] Compute scopes eagerly in IncludeFixer 2019-08-06 11:37:50 +00:00
JSONTransport.cpp Make llvm::StringRef to std::string conversions explicit. 2020-01-28 23:25:25 +01:00
Logger.cpp Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
Logger.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
ParsedAST.cpp Make llvm::StringRef to std::string conversions explicit. 2020-01-28 23:25:25 +01:00
ParsedAST.h NFC: Fix trivial typos in comments 2020-01-04 10:28:41 -05:00
Path.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
PathMapping.cpp Make llvm::StringRef to std::string conversions explicit. 2020-01-28 23:25:25 +01:00
PathMapping.h [clangd] Add path mappings functionality 2020-01-07 12:40:51 +01:00
Preamble.cpp [clangd] Improve long and confusing preamble log message. 2019-11-16 17:13:02 +01:00
Preamble.h [clangd] Collect macros in the preamble region of the main file 2019-09-24 11:14:06 +00:00
Protocol.cpp [clang][Index] Introduce a TemplateParm SymbolKind 2020-02-14 13:20:34 +01:00
Protocol.h [clangd] Expose Code Completion score to the client 2020-02-13 15:05:18 +01:00
Quality.cpp [clang][Index] Introduce a TemplateParm SymbolKind 2020-02-14 13:20:34 +01:00
Quality.h [clangd] Remove unused signature help quality signal. NFC 2019-06-06 08:32:25 +00:00
QueryDriverDatabase.cpp [clangd] Preserve -nostdinc and --sysroot when calling query driver 2020-02-05 09:58:06 +01:00
RIFF.cpp Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
RIFF.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
Selection.cpp [clangd] Improve ObjC property handling in SelectionTree. 2020-01-28 11:07:19 +01:00
Selection.h Revert "[clangd] Reapply b60896fad9 Fall back to selecting token-before-cursor if token-after-cursor fails." 2019-12-16 19:07:49 +01:00
SemanticHighlighting.cpp [clangd] Add C++20 concepts support to findExplicitReferences() and semantic highlighting 2020-01-23 15:11:46 -05:00
SemanticHighlighting.h [clangd] Add C++20 concepts support to findExplicitReferences() and semantic highlighting 2020-01-23 15:11:46 -05:00
SemanticSelection.cpp Revert "[clangd] Reapply b60896fad9 Fall back to selecting token-before-cursor if token-after-cursor fails." 2019-12-16 19:07:49 +01:00
SemanticSelection.h Implement semantic selections. 2019-09-16 11:29:35 +00:00
Shutdown.cpp Include <cstdlib> for std::abort() in clangd 2020-01-24 20:52:37 +01:00
Shutdown.h Use std::foo_t rather than std::foo in LLVM. 2020-02-11 15:12:51 -08:00
SourceCode.cpp [clangd] Pull out a isProtoFile function. 2020-02-05 12:04:03 +01:00
SourceCode.h [clangd] Pull out a isProtoFile function. 2020-02-05 12:04:03 +01:00
StdSymbolMap.inc [clangd] Add include-mapping for C symbols. 2019-06-21 13:32:18 +00:00
TUScheduler.cpp [Support] On Windows, ensure hardware_concurrency() extends to all CPU sockets and all NUMA groups 2020-02-14 10:24:22 -05:00
TUScheduler.h Reland "[clangd] Mechanism to make update debounce responsive to rebuild speed." 2020-02-04 17:17:53 +01:00
Threading.cpp [clangd] Increase stack size of the new threads on macOS 2020-02-04 15:21:27 +01:00
Threading.h [clangd] Remove obsolete includes. NFC 2019-09-04 10:01:05 +00:00
Trace.cpp [clangd] Remove obsolete includes. NFC 2019-09-04 10:01:05 +00:00
Trace.h NFC: Fix trivial typos in comments 2020-01-04 10:28:41 -05:00
Transport.h [NFC] Fix typos in Clangd and Clang 2019-12-16 10:54:40 +01:00
URI.cpp Make llvm::StringRef to std::string conversions explicit. 2020-01-28 23:25:25 +01:00
URI.h [clangd] Simplify the callside of URI::resolve, NFC. 2019-09-23 14:39:37 +00:00
XRefs.cpp [clangd] Make go-to-def jumps to overriden methods on `final` specifier. 2020-01-30 12:49:30 +01:00
XRefs.h Revert "Revert "[clangd] Implement "textDocument/documentLink" protocol support"" 2020-01-02 16:36:21 +01:00