diff --git a/llvm/lib/Support/Host.cpp b/llvm/lib/Support/Host.cpp --- a/llvm/lib/Support/Host.cpp +++ b/llvm/lib/Support/Host.cpp @@ -1277,11 +1277,17 @@ 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() { + // Count physical id/core id pairs with at least one processor id enabled by + // the CPU affinity mask. + cpu_set_t Affinity; + if (sched_getaffinity(0, sizeof(Affinity), &Affinity) != 0) + return -1; + // Read /proc/cpuinfo as a stream (until EOF reached). It cannot be // mmapped because it appears to have 0 size. llvm::ErrorOr> Text = @@ -1294,28 +1300,26 @@ SmallVector strs; (*Text)->getBuffer().split(strs, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); + int CurProcessor = -1; int CurPhysicalId = -1; int CurCoreId = -1; SmallSet, 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 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, &Affinity)) + UniqueItems.insert(std::make_pair(CurPhysicalId, CurCoreId)); + CurProcessor = -1; CurPhysicalId = -1; CurCoreId = -1; }