[llvm] cd53ded - [Support] Fix computeHostNumPhysicalCores() to respect affinity

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 31 11:20:35 PDT 2020


Author: Fangrui Song
Date: 2020-07-31T11:20:15-07:00
New Revision: cd53ded557c3487b8dae2f9de894fdb5b75cb8c8

URL: https://github.com/llvm/llvm-project/commit/cd53ded557c3487b8dae2f9de894fdb5b75cb8c8
DIFF: https://github.com/llvm/llvm-project/commit/cd53ded557c3487b8dae2f9de894fdb5b75cb8c8.diff

LOG: [Support] Fix computeHostNumPhysicalCores() to respect affinity

computeHostNumPhysicalCores() is designed to respect CPU affinity.
D84764 used sysconf(_SC_NPROCESSORS_ONLN) which does not respect
affinity.
SupportTests Threading.PhysicalConcurrency may fail if taskset -c is specified.

Added: 
    

Modified: 
    llvm/lib/Support/Host.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/Host.cpp b/llvm/lib/Support/Host.cpp
index 01ede62c755a..d3b255ae0f2e 100644
--- a/llvm/lib/Support/Host.cpp
+++ b/llvm/lib/Support/Host.cpp
@@ -1271,11 +1271,14 @@ int computeHostNumPhysicalCores() {
   }
   return CPU_COUNT(&Enabled);
 }
-#elif (defined(__linux__) &&                                                   \
-       (defined(__ppc__) || defined(__powerpc__) || defined(__s390x__)))
-#include <unistd.h>
-
-// Gets the number of *physical cores* on the machine.
+#elif defined(__linux__) && defined(__powerpc__)
+int computeHostNumPhysicalCores() {
+  cpu_set_t Affinity;
+  if (sched_getaffinity(0, sizeof(Affinity), &Affinity) != 0)
+    return -1;
+  return CPU_COUNT(&Affinity);
+}
+#elif defined(__linux__) && defined(__s390x__)
 int computeHostNumPhysicalCores() { return sysconf(_SC_NPROCESSORS_ONLN); }
 #elif defined(__APPLE__) && defined(__x86_64__)
 #include <sys/param.h>


        


More information about the llvm-commits mailing list