]> Savannah Git Hosting - gnulib.git/commitdiff
nproc: Use affinity mask even on systems with more than 1024 CPUs.
authorFlorian Weimer <fweimer@redhat.com>
Mon, 11 Nov 2024 13:05:53 +0000 (14:05 +0100)
committerBruno Haible <bruno@clisp.org>
Mon, 11 Nov 2024 14:24:23 +0000 (15:24 +0100)
* lib/nproc.c (num_processors_via_affinity_mask): Retry
with larger affinity masks if CPU_ALLOC_SIZE is available.

ChangeLog
lib/nproc.c

index 0401c700f78591aa5503d518db6ee0879cab5a45..c95151da24259cf0ce24dec18b71134d58f509ff 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2024-11-11  Florian Weimer  <fweimer@redhat.com>
+
+       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  <bruno@clisp.org>
 
        acl-permissions: Define the inline functions in this module.
index 92a07e8289007937420208f4038e1dbde40a2801..48bc3d06fa90ad2f88dd8a70e7810642dfc8a396 100644 (file)
@@ -20,6 +20,7 @@
 #include <config.h>
 #include "nproc.h"
 
+#include <errno.h>
 #include <limits.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -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;