Merge pull request #199 from dgrove-oss/hwconfig-respect-affinity
consider affinity when computing active cpus on Linux
diff --git a/src/shims/hw_config.h b/src/shims/hw_config.h
index cad211d..6b5a069 100644
--- a/src/shims/hw_config.h
+++ b/src/shims/hw_config.h
@@ -87,7 +87,17 @@
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;