[llvm] r284656 - Add computeHostNumPhysicalCores() implementation for Darwin

Mehdi Amini via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 19 15:36:08 PDT 2016


Author: mehdi_amini
Date: Wed Oct 19 17:36:07 2016
New Revision: 284656

URL: http://llvm.org/viewvc/llvm-project?rev=284656&view=rev
Log:
Add computeHostNumPhysicalCores() implementation for Darwin

Differential Revision: https://reviews.llvm.org/D25800

Modified:
    llvm/trunk/lib/Support/Host.cpp
    llvm/trunk/unittests/Support/Host.cpp

Modified: llvm/trunk/lib/Support/Host.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Host.cpp?rev=284656&r1=284655&r2=284656&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Host.cpp (original)
+++ llvm/trunk/lib/Support/Host.cpp Wed Oct 19 17:36:07 2016
@@ -1234,6 +1234,25 @@ static int computeHostNumPhysicalCores()
   }
   return UniqueItems.size();
 }
+#elif defined(__APPLE__) && defined(__x86_64__)
+#include <sys/param.h>
+#include <sys/sysctl.h>
+
+// Gets the number of *physical cores* on the machine.
+static int computeHostNumPhysicalCores() {
+  uint32_t count;
+  size_t len = sizeof(count);
+  sysctlbyname("hw.physicalcpu", &count, &len, NULL, 0);
+  if (count < 1) {
+    int nm[2];
+    nm[0] = CTL_HW;
+    nm[1] = HW_AVAILCPU;
+    sysctl(nm, 2, &count, &len, NULL, 0);
+    if (count < 1)
+      return -1;
+  }
+  return count;
+}
 #else
 // On other systems, return -1 to indicate unknown.
 static int computeHostNumPhysicalCores() { return -1; }

Modified: llvm/trunk/unittests/Support/Host.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/Host.cpp?rev=284656&r1=284655&r2=284656&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/Host.cpp (original)
+++ llvm/trunk/unittests/Support/Host.cpp Wed Oct 19 17:36:07 2016
@@ -31,9 +31,10 @@ protected:
     Host.setTriple(Triple::normalize(sys::getProcessTriple()));
 
     // Initially this is only testing detection of the number of
-    // physical cores, which is currently only supported for
-    // x86_64 Linux.
+    // physical cores, which is currently only supported/tested for
+    // x86_64 Linux and Darwin.
     SupportedArchAndOSs.push_back(std::make_pair(Triple::x86_64, Triple::Linux));
+    SupportedArchAndOSs.push_back(std::make_pair(Triple::x86_64, Triple::Darwin));
   }
 };
 




More information about the llvm-commits mailing list