bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] nproc: Use affinity mask even on systems with more than 1024 CPU


From: Florian Weimer
Subject: [PATCH] nproc: Use affinity mask even on systems with more than 1024 CPUs
Date: Mon, 11 Nov 2024 12:37:30 +0100
User-agent: Gnus/5.13 (Gnus v5.13)

* lib/nproc.c (num_processors_via_affinity_mask): Retry
with larger affinity masks if CPU_ALLOC_SIZE is available.

---
 lib/nproc.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/lib/nproc.c b/lib/nproc.c
index 92a07e8289..14da5f6a9b 100644
--- a/lib/nproc.c
+++ b/lib/nproc.c
@@ -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,31 @@ 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);
+        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;

base-commit: 6b5077524be35e89d004d2d49965aba0dc5b79d6




reply via email to

[Prev in Thread] Current Thread [Next in Thread]