Skip to content

Commit 7aeabe5

Browse files
committed
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.
1 parent ac16fbb commit 7aeabe5

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/shims/hw_config.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,14 @@ _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+
cpu_set_t cpuset;
93+
if (pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset) == 0)
94+
return CPU_COUNT(&cpuset);
95+
#endif
96+
return sysconf(_SC_NPROCESSORS_ONLN);
97+
}
9198
}
9299
#else
93100
const char *name = NULL;

0 commit comments

Comments
 (0)