diff options
author | Rafael Espindola <rafael.espindola@gmail.com> | 2017-10-04 20:27:01 +0000 |
---|---|---|
committer | Rafael Espindola <rafael.espindola@gmail.com> | 2017-10-04 20:27:01 +0000 |
commit | 8c0ff9508da5f02e8ce6580a126a2018c9bf702a (patch) | |
tree | 684e2b65a792c355a8c8659a94b735e82237976d /llvm/lib/Support/Threading.cpp | |
parent | 4c33d5213b91b367a8392c19b4a110f62243a91d (diff) | |
download | bcm5719-llvm-8c0ff9508da5f02e8ce6580a126a2018c9bf702a.tar.gz bcm5719-llvm-8c0ff9508da5f02e8ce6580a126a2018c9bf702a.zip |
Bring r314809 back.
But now include a check for CPU_COUNT so we still build on 10 year old
versions of glibc.
Original message:
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: 314931
Diffstat (limited to 'llvm/lib/Support/Threading.cpp')
-rw-r--r-- | llvm/lib/Support/Threading.cpp | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/llvm/lib/Support/Threading.cpp b/llvm/lib/Support/Threading.cpp index 6a10b988d46..473c84808af 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() { +#if defined(HAVE_SCHED_GETAFFINITY) && defined(HAVE_CPU_COUNT) + 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" |