Skip to content

Commit 382f25a

Browse files
dgrove-ossdas
authored andcommitted
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. Signed-off-by: Daniel A. Steffen <[email protected]>
1 parent 6874715 commit 382f25a

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/shims/hw_config.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,17 @@ _dispatch_hw_get_config(_dispatch_hw_config_t c)
8787
case _dispatch_hw_config_physical_cpus:
8888
return sysconf(_SC_NPROCESSORS_CONF);
8989
case _dispatch_hw_config_active_cpus:
90-
return sysconf(_SC_NPROCESSORS_ONLN);
90+
{
91+
#ifdef __USE_GNU
92+
// Prefer pthread_getaffinity_np because it considers
93+
// scheduler cpu affinity. This matters if the program
94+
// is restricted to a subset of the online cpus (eg via numactl).
95+
cpu_set_t cpuset;
96+
if (pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset) == 0)
97+
return CPU_COUNT(&cpuset);
98+
#endif
99+
return sysconf(_SC_NPROCESSORS_ONLN);
100+
}
91101
}
92102
#else
93103
const char *name = NULL;

0 commit comments

Comments
 (0)