[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.
This commit is contained in:
Fangrui Song 2020-07-31 11:20:15 -07:00
parent 8830f1170d
commit cd53ded557
1 changed files with 8 additions and 5 deletions

View File

@ -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>