[PATCH] D92271: Implement computeHostNumHardwareThreads() for FreeBSD

Dimitry Andric via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 28 12:05:14 PST 2020

dim created this revision.
dim added reviewers: aganea, cem, chandlerc, emaste, MaskRay, sammccall.
Herald added subscribers: dexonsmith, hiraditya, krytarowski, arichardson.
Herald added a project: LLVM.
dim requested review of this revision.

This retrieves CPU affinity via FreeBSD's cpuset(2) API, and makes LLVM
respect affinity settings configured by the user via the cpuset(1)

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.

  rG LLVM Github Monorepo



Index: llvm/lib/Support/Unix/Threading.inc
--- llvm/lib/Support/Unix/Threading.inc
+++ llvm/lib/Support/Unix/Threading.inc
@@ -26,6 +26,10 @@
 #include <pthread_np.h> // For pthread_getthreadid_np() / pthread_set_name_np()
+#if defined(__FreeBSD__)
+#include <sys/cpuset.h>
 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
 #include <errno.h>
 #include <sys/sysctl.h>
@@ -282,6 +286,13 @@
 #include <thread>
 int computeHostNumHardwareThreads() {
+#ifdef __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);
 #ifdef __linux__
   cpu_set_t Set;
   if (sched_getaffinity(0, sizeof(Set), &Set) == 0)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92271.308185.patch
Type: text/x-patch
Size: 845 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201128/866c6419/attachment.bin>

More information about the llvm-commits mailing list