aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/Unix/Threading.inc
diff options
context:
space:
mode:
authorDimitry Andric <dimitry@andric.com>2020-11-28 21:42:52 +0100
committerDimitry Andric <dimitry@andric.com>2020-11-29 00:49:39 +0100
commitd989ffd109b2b5e7fd7c577ea549f4d1c5f5492c (patch)
tree346c707b3a2935aa829d2ba3c45725b59d026dd6 /llvm/lib/Support/Unix/Threading.inc
parentf502b14d40e751fe00afc493ef0d08f196524886 (diff)
downloadllvm-d989ffd109b2b5e7fd7c577ea549f4d1c5f5492c.zip
llvm-d989ffd109b2b5e7fd7c577ea549f4d1c5f5492c.tar.gz
llvm-d989ffd109b2b5e7fd7c577ea549f4d1c5f5492c.tar.bz2
Implement computeHostNumHardwareThreads() for FreeBSD
This retrieves CPU affinity via FreeBSD's cpuset(2) API, and makes LLVM respect affinity settings configured by the user via the cpuset(1) command. In particular, this allows to reduce the number of threads used on machines with high core counts, which can interact badly with parallelized build systems. This is particularly noticable with lld, which spawns lots of threads even for linking e.g. hello_world! This fix is related to PR48193, but does not adress the more fundamental problem, which is that LLVM by default grabs as many CPUs and/or threads as possible. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D92271
Diffstat (limited to 'llvm/lib/Support/Unix/Threading.inc')
-rw-r--r--llvm/lib/Support/Unix/Threading.inc9
1 files changed, 8 insertions, 1 deletions
diff --git a/llvm/lib/Support/Unix/Threading.inc b/llvm/lib/Support/Unix/Threading.inc
index 2d0aaca..667d023 100644
--- a/llvm/lib/Support/Unix/Threading.inc
+++ b/llvm/lib/Support/Unix/Threading.inc
@@ -28,6 +28,7 @@
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
#include <errno.h>
+#include <sys/cpuset.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#include <unistd.h>
@@ -282,7 +283,13 @@ SetThreadPriorityResult llvm::set_thread_priority(ThreadPriority Priority) {
#include <thread>
int computeHostNumHardwareThreads() {
-#ifdef __linux__
+#if defined(__FreeBSD__)
+ cpuset_t mask;
+ CPU_ZERO(&mask);
+ if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(mask),
+ &mask) == 0)
+ return CPU_COUNT(&mask);
+#elif defined(__linux__)
cpu_set_t Set;
if (sched_getaffinity(0, sizeof(Set), &Set) == 0)
return CPU_COUNT(&Set);