summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support/Threading.cpp
diff options
context:
space:
mode:
authorRafael Espindola <rafael.espindola@gmail.com>2017-10-03 16:25:15 +0000
committerRafael Espindola <rafael.espindola@gmail.com>2017-10-03 16:25:15 +0000
commit6e182fbab46d61ea6bef31719fb4524dac85ae9f (patch)
tree127149647aa8306e3bbca82fc319407732a2191b /llvm/lib/Support/Threading.cpp
parentc1f906c134feab3d5602e869f0983b8b35f18394 (diff)
downloadbcm5719-llvm-6e182fbab46d61ea6bef31719fb4524dac85ae9f.tar.gz
bcm5719-llvm-6e182fbab46d61ea6bef31719fb4524dac85ae9f.zip
Use sched_getaffinity instead of std::thread::hardware_concurrency.
The issue with std::thread::hardware_concurrency is that it forwards to libc and some implementations (like glibc) don't take thread affinity into consideration. With this change a llvm program that can execute in only 2 cores will use 2 threads, even if the machine has 32 cores. This makes benchmarking a lot easier, but should also help if someone doesn't want to use all cores for compilation for example. llvm-svn: 314809
Diffstat (limited to 'llvm/lib/Support/Threading.cpp')
-rw-r--r--llvm/lib/Support/Threading.cpp14
1 files changed, 14 insertions, 0 deletions
diff --git a/llvm/lib/Support/Threading.cpp b/llvm/lib/Support/Threading.cpp
index 6a10b988d46..b3579b57548 100644
--- a/llvm/lib/Support/Threading.cpp
+++ b/llvm/lib/Support/Threading.cpp
@@ -47,6 +47,8 @@ void llvm::llvm_execute_on_thread(void (*Fn)(void *), void *UserData,
unsigned llvm::heavyweight_hardware_concurrency() { return 1; }
+unsigned llvm::hardware_concurrency() { return 1; }
+
uint64_t llvm::get_threadid() { return 0; }
uint32_t llvm::get_max_thread_name_length() { return 0; }
@@ -71,6 +73,18 @@ unsigned llvm::heavyweight_hardware_concurrency() {
return NumPhysical;
}
+unsigned llvm::hardware_concurrency() {
+#ifdef HAVE_SCHED_GETAFFINITY
+ cpu_set_t Set;
+ if (sched_getaffinity(0, sizeof(Set), &Set))
+ return CPU_COUNT(&Set);
+#endif
+ // Guard against std::thread::hardware_concurrency() returning 0.
+ if (unsigned Val = std::thread::hardware_concurrency())
+ return Val;
+ return 1;
+}
+
// Include the platform-specific parts of this class.
#ifdef LLVM_ON_UNIX
#include "Unix/Threading.inc"
OpenPOWER on IntegriCloud