From: Florian Weimer Date: Mon, 11 Nov 2024 13:05:53 +0000 (+0100) Subject: nproc: Use affinity mask even on systems with more than 1024 CPUs. X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=45c2456a56337ebcafe0dd9faa2bd995ccbc3357;p=gnulib.git nproc: Use affinity mask even on systems with more than 1024 CPUs. * lib/nproc.c (num_processors_via_affinity_mask): Retry with larger affinity masks if CPU_ALLOC_SIZE is available. --- diff --git a/ChangeLog b/ChangeLog index 0401c700f7..c95151da24 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2024-11-11 Florian Weimer + + nproc: Use affinity mask even on systems with more than 1024 CPUs. + * lib/nproc.c (num_processors_via_affinity_mask): Retry + with larger affinity masks if CPU_ALLOC_SIZE is available. + 2024-11-11 Bruno Haible acl-permissions: Define the inline functions in this module. diff --git a/lib/nproc.c b/lib/nproc.c index 92a07e8289..48bc3d06fa 100644 --- a/lib/nproc.c +++ b/lib/nproc.c @@ -20,6 +20,7 @@ #include #include "nproc.h" +#include #include #include #include @@ -124,6 +125,33 @@ num_processors_via_affinity_mask (void) return count; } } +#elif HAVE_SCHED_GETAFFINITY_LIKE_GLIBC \ + && defined CPU_ALLOC_SIZE /* glibc >= 2.6 */ + { + unsigned int alloc_count = 1024; + while (1) + { + cpu_set_t *set = CPU_ALLOC (alloc_count); + if (set == NULL) + return 0; + unsigned int size = CPU_ALLOC_SIZE (alloc_count); + if (sched_getaffinity (0, size, set) == 0) + { + unsigned int count = CPU_COUNT_S (size, set); + CPU_FREE (set); + return count; + } + if (errno != EINVAL) + { + CPU_FREE (set); + return 0; + } + CPU_FREE (set); + alloc_count *= 2; + if (alloc_count == 0) + return 0; + } + } #elif HAVE_SCHED_GETAFFINITY_LIKE_GLIBC /* glibc >= 2.3.4 */ { cpu_set_t set;