Skip to content

Commit 47e7fe0

Browse files
committed
SR-10611: ProcessInfo.activeProcessorCount too high in containerized Linux
- On Linux, use sched_getaffinity() falling back to sysconf(_SC_NPROCESSORS_ONLN) as this should provide a better value for ProcessInfo.activeProcessorCount when running under cgroups (eg Docker).
1 parent 9ba604d commit 47e7fe0

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

CoreFoundation/Base.subproj/CFUtilities.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@
7171
#include <pthread.h>
7272
#endif
7373

74+
#if DEPLOYMENT_TARGET_LINUX && __has_include(<sched.h>)
75+
#include <sched.h>
76+
#endif
7477

7578

7679
#if !defined(CF_HAVE_HW_CONFIG_COMMPAGE) && defined(_COMM_PAGE_LOGICAL_CPUS) && defined(_COMM_PAGE_PHYSICAL_CPUS) && defined(_COMM_PAGE_ACTIVE_CPUS) && !__has_feature(address_sanitizer)
@@ -451,6 +454,14 @@ CF_PRIVATE CFIndex __CFActiveProcessorCount() {
451454
pcnt = 0;
452455
}
453456
#elif DEPLOYMENT_TARGET_LINUX
457+
458+
#ifdef _SCHED_H
459+
cpu_set_t set;
460+
if (sched_getaffinity (getpid(), sizeof(set), &set) == 0) {
461+
return CPU_COUNT (&set);
462+
}
463+
#endif
464+
454465
pcnt = sysconf(_SC_NPROCESSORS_ONLN);
455466
#else
456467
// Assume the worst

0 commit comments

Comments
 (0)