From e59638d961cb93373d0ca4c65bfdbc1dbc408a04 Mon Sep 17 00:00:00 2001 From: David Grove Date: Thu, 12 Jan 2017 10:48:27 -0500 Subject: [PATCH] consider affinity when computing active cpus on Linux If pthread_getaffinity_np is available, then use the cpuset of the current thread to determine the number of active cpus. --- src/shims/hw_config.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/shims/hw_config.h b/src/shims/hw_config.h index cad211d21..6b5a06929 100644 --- a/src/shims/hw_config.h +++ b/src/shims/hw_config.h @@ -87,7 +87,17 @@ _dispatch_hw_get_config(_dispatch_hw_config_t c) case _dispatch_hw_config_physical_cpus: return sysconf(_SC_NPROCESSORS_CONF); case _dispatch_hw_config_active_cpus: - return sysconf(_SC_NPROCESSORS_ONLN); + { +#ifdef __USE_GNU + // Prefer pthread_getaffinity_np because it considers + // scheduler cpu affinity. This matters if the program + // is restricted to a subset of the online cpus (eg via numactl). + cpu_set_t cpuset; + if (pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset) == 0) + return CPU_COUNT(&cpuset); +#endif + return sysconf(_SC_NPROCESSORS_ONLN); + } } #else const char *name = NULL;