[PATCH] D78324: [Support][X86] Change getHostNumPhsicalCores() to return number of physical cores enabled by affinity

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 16 13:23:51 PDT 2020


MaskRay created this revision.
MaskRay added reviewers: aganea, mehdi_amini, rnk, tejohnson.
Herald added subscribers: llvm-commits, hiraditya, krytarowski.
Herald added a project: LLVM.

While here, make the x86-64 code available for x86-32.

The output has been available and stable since before Linux 2.6.12-rc2 (`arch/i386/kernel/cpu/proc.c` when Linux started to use git)

  physical id:
  siblings:
  core id:

Don't check HAVE_SCHED_GETAFFINITY/HAVE_CPU_COUNT. The interface is
simply available in every libc which can build LLVM.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D78324

Files:
  llvm/lib/Support/Host.cpp


Index: llvm/lib/Support/Host.cpp
===================================================================
--- llvm/lib/Support/Host.cpp
+++ llvm/lib/Support/Host.cpp
@@ -1277,13 +1277,16 @@
 StringRef sys::getHostCPUName() { return "generic"; }
 #endif
 
-#if defined(__linux__) && defined(__x86_64__)
+#if defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
 // On Linux, the number of physical cores can be computed from /proc/cpuinfo,
 // using the number of unique physical/core id pairs. The following
 // implementation reads the /proc/cpuinfo format on an x86_64 system.
 int computeHostNumPhysicalCores() {
   // Read /proc/cpuinfo as a stream (until EOF reached). It cannot be
   // mmapped because it appears to have 0 size.
+  cpu_set_t Set;
+  if (sched_getaffinity(0, sizeof(Set), &Set) != 0)
+    return -1;
   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
       llvm::MemoryBuffer::getFileAsStream("/proc/cpuinfo");
   if (std::error_code EC = Text.getError()) {
@@ -1294,28 +1297,26 @@
   SmallVector<StringRef, 8> strs;
   (*Text)->getBuffer().split(strs, "\n", /*MaxSplit=*/-1,
                              /*KeepEmpty=*/false);
+  int CurProcessor = -1;
   int CurPhysicalId = -1;
   int CurCoreId = -1;
   SmallSet<std::pair<int, int>, 32> UniqueItems;
-  for (auto &Line : strs) {
-    Line = Line.trim();
-    if (!Line.startswith("physical id") && !Line.startswith("core id"))
-      continue;
+  for (StringRef Line : strs) {
     std::pair<StringRef, StringRef> Data = Line.split(':');
     auto Name = Data.first.trim();
     auto Val = Data.second.trim();
-    if (Name == "physical id") {
-      assert(CurPhysicalId == -1 &&
-             "Expected a core id before seeing another physical id");
+    if (Name == "processor")
+      Val.getAsInteger(10, CurProcessor);
+    else if (Name == "physical id")
       Val.getAsInteger(10, CurPhysicalId);
-    }
-    if (Name == "core id") {
-      assert(CurCoreId == -1 &&
+    else if (Name == "core id") {
+      assert((CurProcessor != -1 && CurPhysicalId != -1 && CurCoreId == -1) &&
              "Expected a physical id before seeing another core id");
       Val.getAsInteger(10, CurCoreId);
-    }
-    if (CurPhysicalId != -1 && CurCoreId != -1) {
-      UniqueItems.insert(std::make_pair(CurPhysicalId, CurCoreId));
+      // The processor id corresponds to an index into cpu_set_t.
+      if (CPU_ISSET(CurProcessor, &Set))
+        UniqueItems.insert(std::make_pair(CurPhysicalId, CurCoreId));
+      CurProcessor = -1;
       CurPhysicalId = -1;
       CurCoreId = -1;
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D78324.258156.patch
Type: text/x-patch
Size: 2587 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200416/710c2f3b/attachment.bin>


More information about the llvm-commits mailing list