From 1bcc689324bdee090eed035353724abc3fa7c909 Mon Sep 17 00:00:00 2001 From: Jan Niklas Hasse Date: Tue, 26 Feb 2019 14:37:19 +0100 Subject: Take CPU set limitations into account when calculating processor count Fixes #1278. --- src/util.cc | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/util.cc b/src/util.cc index 47a5de2..ee810d6 100644 --- a/src/util.cc +++ b/src/util.cc @@ -485,6 +485,15 @@ int GetProcessorCount() { GetNativeSystemInfo(&info); return info.dwNumberOfProcessors; #else +#ifdef CPU_COUNT + // The number of exposed processors might not represent the actual number of + // processors threads can run on. This happens when a CPU set limitation is + // active, see https://github.com/ninja-build/ninja/issues/1278 + cpu_set_t set; + if (sched_getaffinity(getpid(), sizeof(set), &set) == 0) { + return CPU_COUNT(&set); + } +#endif return sysconf(_SC_NPROCESSORS_ONLN); #endif } -- cgit v0.12