Prefer getaffinity() to detect number of CPUs.

This commit is contained in:
Qi Wang 2019-11-08 13:05:43 -08:00 committed by Qi Wang
parent 04cb7d4d6b
commit a787d2f5b3

View File

@ -742,18 +742,28 @@ malloc_ncpus(void) {
SYSTEM_INFO si; SYSTEM_INFO si;
GetSystemInfo(&si); GetSystemInfo(&si);
result = si.dwNumberOfProcessors; result = si.dwNumberOfProcessors;
#elif defined(JEMALLOC_GLIBC_MALLOC_HOOK) && defined(CPU_COUNT) #elif defined(CPU_COUNT)
/* /*
* glibc >= 2.6 has the CPU_COUNT macro. * glibc >= 2.6 has the CPU_COUNT macro.
* *
* glibc's sysconf() uses isspace(). glibc allocates for the first time * glibc's sysconf() uses isspace(). glibc allocates for the first time
* *before* setting up the isspace tables. Therefore we need a * *before* setting up the isspace tables. Therefore we need a
* different method to get the number of CPUs. * different method to get the number of CPUs.
*
* The getaffinity approach is also preferred when only a subset of CPUs
* is available, to avoid using more arenas than necessary.
*/ */
{ {
# if defined(__FreeBSD__)
cpuset_t set;
# else
cpu_set_t set; cpu_set_t set;
# endif
# if defined(JEMALLOC_HAVE_SCHED_SETAFFINITY)
sched_getaffinity(0, sizeof(set), &set);
# else
pthread_getaffinity_np(pthread_self(), sizeof(set), &set); pthread_getaffinity_np(pthread_self(), sizeof(set), &set);
# endif
result = CPU_COUNT(&set); result = CPU_COUNT(&set);
} }
#else #else