2015-12-15 01:59:19 +01:00
|
|
|
//========- unittests/Support/ThreadPools.cpp - ThreadPools.h tests --========//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-12-15 01:59:19 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/ThreadPool.h"
|
|
|
|
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2021-01-14 23:03:13 +01:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2015-12-15 10:10:25 +01:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Triple.h"
|
2021-01-14 23:03:13 +01:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2015-12-15 10:10:25 +01:00
|
|
|
#include "llvm/Support/Host.h"
|
2021-01-14 23:03:13 +01:00
|
|
|
#include "llvm/Support/Program.h"
|
2015-12-15 10:10:25 +01:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
[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::thread::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 04:49:57 +01:00
|
|
|
#include "llvm/Support/Threading.h"
|
2015-12-15 01:59:19 +01:00
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-12-15 10:10:25 +01:00
|
|
|
// Fixture for the unittests, allowing to *temporarily* disable the unittests
|
|
|
|
// on a particular platform
|
|
|
|
class ThreadPoolTest : public testing::Test {
|
|
|
|
Triple Host;
|
|
|
|
SmallVector<Triple::ArchType, 4> UnsupportedArchs;
|
|
|
|
SmallVector<Triple::OSType, 4> UnsupportedOSs;
|
|
|
|
SmallVector<Triple::EnvironmentType, 1> UnsupportedEnvironments;
|
|
|
|
protected:
|
|
|
|
// This is intended for platform as a temporary "XFAIL"
|
|
|
|
bool isUnsupportedOSOrEnvironment() {
|
|
|
|
Triple Host(Triple::normalize(sys::getProcessTriple()));
|
|
|
|
|
2016-08-12 00:21:41 +02:00
|
|
|
if (find(UnsupportedEnvironments, Host.getEnvironment()) !=
|
|
|
|
UnsupportedEnvironments.end())
|
2015-12-15 10:10:25 +01:00
|
|
|
return true;
|
|
|
|
|
2016-08-12 00:21:41 +02:00
|
|
|
if (is_contained(UnsupportedOSs, Host.getOS()))
|
2015-12-15 10:10:25 +01:00
|
|
|
return true;
|
|
|
|
|
2016-08-12 00:21:41 +02:00
|
|
|
if (is_contained(UnsupportedArchs, Host.getArch()))
|
2015-12-15 10:10:25 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadPoolTest() {
|
|
|
|
// Add unsupported configuration here, example:
|
|
|
|
// UnsupportedArchs.push_back(Triple::x86_64);
|
2015-12-15 10:10:28 +01:00
|
|
|
|
|
|
|
// See https://llvm.org/bugs/show_bug.cgi?id=25829
|
|
|
|
UnsupportedArchs.push_back(Triple::ppc64le);
|
|
|
|
UnsupportedArchs.push_back(Triple::ppc64);
|
2015-12-15 10:10:25 +01:00
|
|
|
}
|
2015-12-19 06:12:07 +01:00
|
|
|
|
|
|
|
/// Make sure this thread not progress faster than the main thread.
|
|
|
|
void waitForMainThread() {
|
2015-12-19 10:54:27 +01:00
|
|
|
std::unique_lock<std::mutex> LockGuard(WaitMainThreadMutex);
|
|
|
|
WaitMainThread.wait(LockGuard, [&] { return MainThreadReady; });
|
2015-12-19 06:12:07 +01:00
|
|
|
}
|
2015-12-19 10:49:09 +01:00
|
|
|
|
|
|
|
/// Set the readiness of the main thread.
|
2015-12-19 23:56:24 +01:00
|
|
|
void setMainThreadReady() {
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> LockGuard(WaitMainThreadMutex);
|
|
|
|
MainThreadReady = true;
|
|
|
|
}
|
2015-12-19 10:49:09 +01:00
|
|
|
WaitMainThread.notify_all();
|
|
|
|
}
|
|
|
|
|
2015-12-19 23:56:24 +01:00
|
|
|
void SetUp() override { MainThreadReady = false; }
|
|
|
|
|
2021-01-14 23:03:13 +01:00
|
|
|
std::vector<llvm::BitVector> RunOnAllSockets(ThreadPoolStrategy S);
|
[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::thread::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 04:49:57 +01:00
|
|
|
|
2015-12-19 06:12:07 +01:00
|
|
|
std::condition_variable WaitMainThread;
|
|
|
|
std::mutex WaitMainThreadMutex;
|
2019-11-18 13:05:40 +01:00
|
|
|
bool MainThreadReady = false;
|
2015-12-15 10:10:25 +01:00
|
|
|
};
|
|
|
|
|
2021-01-14 23:03:13 +01:00
|
|
|
#define CHECK_UNSUPPORTED() \
|
|
|
|
do { \
|
|
|
|
if (isUnsupportedOSOrEnvironment()) \
|
|
|
|
return; \
|
|
|
|
} while (0);
|
|
|
|
|
2015-12-15 10:10:25 +01:00
|
|
|
TEST_F(ThreadPoolTest, AsyncBarrier) {
|
|
|
|
CHECK_UNSUPPORTED();
|
2015-12-15 01:59:19 +01:00
|
|
|
// test that async & barrier work together properly.
|
|
|
|
|
|
|
|
std::atomic_int checked_in{0};
|
|
|
|
|
|
|
|
ThreadPool Pool;
|
|
|
|
for (size_t i = 0; i < 5; ++i) {
|
2017-01-13 18:12:16 +01:00
|
|
|
Pool.async([this, &checked_in] {
|
2015-12-19 06:12:07 +01:00
|
|
|
waitForMainThread();
|
2015-12-15 01:59:19 +01:00
|
|
|
++checked_in;
|
|
|
|
});
|
|
|
|
}
|
2015-12-19 06:12:07 +01:00
|
|
|
ASSERT_EQ(0, checked_in);
|
2015-12-19 23:56:24 +01:00
|
|
|
setMainThreadReady();
|
2015-12-15 01:59:19 +01:00
|
|
|
Pool.wait();
|
|
|
|
ASSERT_EQ(5, checked_in);
|
|
|
|
}
|
|
|
|
|
2015-12-15 05:44:02 +01:00
|
|
|
static void TestFunc(std::atomic_int &checked_in, int i) { checked_in += i; }
|
|
|
|
|
2015-12-15 10:10:25 +01:00
|
|
|
TEST_F(ThreadPoolTest, AsyncBarrierArgs) {
|
|
|
|
CHECK_UNSUPPORTED();
|
2015-12-15 05:44:02 +01:00
|
|
|
// Test that async works with a function requiring multiple parameters.
|
|
|
|
std::atomic_int checked_in{0};
|
|
|
|
|
|
|
|
ThreadPool Pool;
|
|
|
|
for (size_t i = 0; i < 5; ++i) {
|
|
|
|
Pool.async(TestFunc, std::ref(checked_in), i);
|
|
|
|
}
|
|
|
|
Pool.wait();
|
|
|
|
ASSERT_EQ(10, checked_in);
|
|
|
|
}
|
|
|
|
|
2015-12-15 10:10:25 +01:00
|
|
|
TEST_F(ThreadPoolTest, Async) {
|
|
|
|
CHECK_UNSUPPORTED();
|
2015-12-15 01:59:19 +01:00
|
|
|
ThreadPool Pool;
|
|
|
|
std::atomic_int i{0};
|
2015-12-19 06:12:07 +01:00
|
|
|
Pool.async([this, &i] {
|
|
|
|
waitForMainThread();
|
2015-12-15 01:59:19 +01:00
|
|
|
++i;
|
|
|
|
});
|
|
|
|
Pool.async([&i] { ++i; });
|
2015-12-19 06:12:07 +01:00
|
|
|
ASSERT_NE(2, i.load());
|
2015-12-19 23:56:24 +01:00
|
|
|
setMainThreadReady();
|
2015-12-15 01:59:19 +01:00
|
|
|
Pool.wait();
|
|
|
|
ASSERT_EQ(2, i.load());
|
|
|
|
}
|
|
|
|
|
2016-11-28 10:17:12 +01:00
|
|
|
TEST_F(ThreadPoolTest, GetFuture) {
|
|
|
|
CHECK_UNSUPPORTED();
|
[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::thread::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 04:49:57 +01:00
|
|
|
ThreadPool Pool(hardware_concurrency(2));
|
2016-11-28 10:17:12 +01:00
|
|
|
std::atomic_int i{0};
|
|
|
|
Pool.async([this, &i] {
|
|
|
|
waitForMainThread();
|
|
|
|
++i;
|
|
|
|
});
|
|
|
|
// Force the future using get()
|
|
|
|
Pool.async([&i] { ++i; }).get();
|
|
|
|
ASSERT_NE(2, i.load());
|
|
|
|
setMainThreadReady();
|
|
|
|
Pool.wait();
|
|
|
|
ASSERT_EQ(2, i.load());
|
|
|
|
}
|
|
|
|
|
2015-12-15 10:10:25 +01:00
|
|
|
TEST_F(ThreadPoolTest, PoolDestruction) {
|
|
|
|
CHECK_UNSUPPORTED();
|
2015-12-15 01:59:19 +01:00
|
|
|
// Test that we are waiting on destruction
|
|
|
|
std::atomic_int checked_in{0};
|
|
|
|
{
|
|
|
|
ThreadPool Pool;
|
|
|
|
for (size_t i = 0; i < 5; ++i) {
|
2017-01-13 18:12:16 +01:00
|
|
|
Pool.async([this, &checked_in] {
|
2015-12-19 06:12:07 +01:00
|
|
|
waitForMainThread();
|
2015-12-15 01:59:19 +01:00
|
|
|
++checked_in;
|
|
|
|
});
|
|
|
|
}
|
2015-12-19 06:12:07 +01:00
|
|
|
ASSERT_EQ(0, checked_in);
|
2015-12-19 23:56:24 +01:00
|
|
|
setMainThreadReady();
|
2015-12-15 01:59:19 +01:00
|
|
|
}
|
|
|
|
ASSERT_EQ(5, checked_in);
|
|
|
|
}
|
[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::thread::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 04:49:57 +01:00
|
|
|
|
|
|
|
#if LLVM_ENABLE_THREADS == 1
|
|
|
|
|
2021-04-09 18:40:00 +02:00
|
|
|
// FIXME: Skip some tests below on non-Windows because multi-socket systems
|
|
|
|
// were not fully tested on Unix yet, and llvm::get_thread_affinity_mask()
|
|
|
|
// isn't implemented for Unix (need AffinityMask in Support/Unix/Program.inc).
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
2021-01-14 23:03:13 +01:00
|
|
|
std::vector<llvm::BitVector>
|
|
|
|
ThreadPoolTest::RunOnAllSockets(ThreadPoolStrategy S) {
|
|
|
|
llvm::SetVector<llvm::BitVector> ThreadsUsed;
|
[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::thread::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 04:49:57 +01:00
|
|
|
std::mutex Lock;
|
|
|
|
{
|
2020-04-25 21:05:53 +02:00
|
|
|
std::condition_variable AllThreads;
|
|
|
|
std::mutex AllThreadsLock;
|
|
|
|
unsigned Active = 0;
|
|
|
|
|
[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::thread::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 04:49:57 +01:00
|
|
|
ThreadPool Pool(S);
|
2020-04-25 21:05:53 +02:00
|
|
|
for (size_t I = 0; I < S.compute_thread_count(); ++I) {
|
[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::thread::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 04:49:57 +01:00
|
|
|
Pool.async([&] {
|
2020-04-25 21:05:53 +02:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Guard(AllThreadsLock);
|
|
|
|
++Active;
|
|
|
|
AllThreads.notify_one();
|
|
|
|
}
|
[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::thread::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 04:49:57 +01:00
|
|
|
waitForMainThread();
|
|
|
|
std::lock_guard<std::mutex> Guard(Lock);
|
|
|
|
auto Mask = llvm::get_thread_affinity_mask();
|
|
|
|
ThreadsUsed.insert(Mask);
|
|
|
|
});
|
|
|
|
}
|
2021-01-14 23:03:13 +01:00
|
|
|
EXPECT_EQ(true, ThreadsUsed.empty());
|
2020-04-25 21:05:53 +02:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> Guard(AllThreadsLock);
|
|
|
|
AllThreads.wait(Guard,
|
|
|
|
[&]() { return Active == S.compute_thread_count(); });
|
|
|
|
}
|
[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::thread::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 04:49:57 +01:00
|
|
|
setMainThreadReady();
|
|
|
|
}
|
2021-01-14 23:03:13 +01:00
|
|
|
return ThreadsUsed.takeVector();
|
[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::thread::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 04:49:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ThreadPoolTest, AllThreads_UseAllRessources) {
|
|
|
|
CHECK_UNSUPPORTED();
|
2021-01-14 23:03:13 +01:00
|
|
|
std::vector<llvm::BitVector> ThreadsUsed = RunOnAllSockets({});
|
|
|
|
ASSERT_EQ(llvm::get_cpus(), ThreadsUsed.size());
|
[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::thread::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 04:49:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ThreadPoolTest, AllThreads_OneThreadPerCore) {
|
|
|
|
CHECK_UNSUPPORTED();
|
2021-01-14 23:03:13 +01:00
|
|
|
std::vector<llvm::BitVector> ThreadsUsed =
|
|
|
|
RunOnAllSockets(llvm::heavyweight_hardware_concurrency());
|
|
|
|
ASSERT_EQ(llvm::get_cpus(), ThreadsUsed.size());
|
[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::thread::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 04:49:57 +01:00
|
|
|
}
|
|
|
|
|
2021-01-14 23:03:13 +01:00
|
|
|
// From TestMain.cpp.
|
|
|
|
extern const char *TestMainArgv0;
|
|
|
|
|
|
|
|
// Just a reachable symbol to ease resolving of the executable's path.
|
|
|
|
static cl::opt<std::string> ThreadPoolTestStringArg1("thread-pool-string-arg1");
|
|
|
|
|
2021-03-09 19:52:24 +01:00
|
|
|
#ifdef _WIN32
|
2021-01-14 23:03:13 +01:00
|
|
|
#define setenv(name, var, ignore) _putenv_s(name, var)
|
[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::thread::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 04:49:57 +01:00
|
|
|
#endif
|
2021-01-14 23:03:13 +01:00
|
|
|
|
|
|
|
TEST_F(ThreadPoolTest, AffinityMask) {
|
|
|
|
CHECK_UNSUPPORTED();
|
|
|
|
|
|
|
|
// Skip this test if less than 4 threads are available.
|
|
|
|
if (llvm::hardware_concurrency().compute_thread_count() < 4)
|
|
|
|
return;
|
|
|
|
|
|
|
|
using namespace llvm::sys;
|
|
|
|
if (getenv("LLVM_THREADPOOL_AFFINITYMASK")) {
|
|
|
|
std::vector<llvm::BitVector> ThreadsUsed = RunOnAllSockets({});
|
|
|
|
// Ensure the threads only ran on CPUs 0-3.
|
2021-04-09 18:40:00 +02:00
|
|
|
// NOTE: Don't use ASSERT* here because this runs in a subprocess,
|
|
|
|
// and will show up as un-executed in the parent.
|
2021-04-17 19:56:23 +02:00
|
|
|
assert(llvm::all_of(ThreadsUsed,
|
|
|
|
[](auto &T) { return T.getData().front() < 16UL; }) &&
|
|
|
|
"Threads ran on more CPUs than expected! The affinity mask does not "
|
|
|
|
"seem to work.");
|
2021-01-14 23:03:13 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::string Executable =
|
|
|
|
sys::fs::getMainExecutable(TestMainArgv0, &ThreadPoolTestStringArg1);
|
|
|
|
StringRef argv[] = {Executable, "--gtest_filter=ThreadPoolTest.AffinityMask"};
|
|
|
|
|
|
|
|
// Add environment variable to the environment of the child process.
|
|
|
|
int Res = setenv("LLVM_THREADPOOL_AFFINITYMASK", "1", false);
|
|
|
|
ASSERT_EQ(Res, 0);
|
|
|
|
|
|
|
|
std::string Error;
|
|
|
|
bool ExecutionFailed;
|
|
|
|
BitVector Affinity;
|
|
|
|
Affinity.resize(4);
|
|
|
|
Affinity.set(0, 4); // Use CPUs 0,1,2,3.
|
|
|
|
int Ret = sys::ExecuteAndWait(Executable, argv, {}, {}, 0, 0, &Error,
|
|
|
|
&ExecutionFailed, nullptr, &Affinity);
|
|
|
|
ASSERT_EQ(0, Ret);
|
|
|
|
}
|
|
|
|
|
2021-04-09 18:40:00 +02:00
|
|
|
#endif // #ifdef _WIN32
|
2021-01-14 23:03:13 +01:00
|
|
|
#endif // #if LLVM_ENABLE_THREADS == 1
|