From 3ec2b843bb8cf66ea56018aaaf07c5d076daa76a Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Fri, 30 May 2025 10:04:29 +0100 Subject: [PATCH] [Tests] Fix Dispatch tests to work in container environments. We need to use `pthread_getaffinity_np()` to get the active core count; using `sysconf()` will tell us about the underlying machine, which may have many more cores than the container in which we are executing. rdar://152301793 --- tests/dispatch_apply.c | 9 +++++++++ tests/dispatch_workqueue.c | 14 ++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/tests/dispatch_apply.c b/tests/dispatch_apply.c index 01b3dfb65..4f80aac71 100644 --- a/tests/dispatch_apply.c +++ b/tests/dispatch_apply.c @@ -21,6 +21,7 @@ #include #include #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) +#include #include #ifdef __ANDROID__ #include @@ -83,6 +84,14 @@ static void test_apply_contended(dispatch_queue_t dq) uint32_t activecpu; #if defined(__linux__) || defined(__OpenBSD__) || defined(__FreeBSD__) activecpu = (uint32_t)sysconf(_SC_NPROCESSORS_ONLN); + +#if defined(__linux__) && __USE_GNU + cpu_set_t cpuset; + if (pthread_getaffinity_np(pthread_self(), + sizeof(cpu_set_t), + &cpuset) == 0) + activecpu = (uint32_t)CPU_COUNT(&cpuset); +#endif #elif defined(_WIN32) SYSTEM_INFO si; GetSystemInfo(&si); diff --git a/tests/dispatch_workqueue.c b/tests/dispatch_workqueue.c index 21e96e043..c12598b69 100644 --- a/tests/dispatch_workqueue.c +++ b/tests/dispatch_workqueue.c @@ -1,6 +1,11 @@ #include #include "dispatch_test.h" +#if defined(__linux__) +// For pthread_getaffinity_np() +#include +#endif + struct test_context { uint32_t ncpu; int flag; @@ -35,6 +40,15 @@ activecpu(void) uint32_t activecpu; #if defined(__linux__) || defined(__OpenBSD__) activecpu = (uint32_t)sysconf(_SC_NPROCESSORS_ONLN); + +#if defined(__linux__) && __USE_GNU + cpu_set_t cpuset; + if (pthread_getaffinity_np(pthread_self(), + sizeof(cpu_set_t), + &cpuset) == 0) + activecpu = (uint32_t)CPU_COUNT(&cpuset); +#endif + #elif defined(_WIN32) SYSTEM_INFO si; GetSystemInfo(&si);