coreutils/coreutils-nproc-affinity-1.patch
Lukáš Zaoral 068a1ad058
Fix affinity mask handling in nproc for large CPU counts
Kudos to Florian Weimer for fixing this issue in Fedora!

Resolves: RHEL-68961
2024-11-26 10:19:20 +01:00

56 lines
1.5 KiB
Diff

commit 45c2456a56337ebcafe0dd9faa2bd995ccbc3357
Author: Florian Weimer <fweimer@redhat.com>
Date: Mon Nov 11 14:05:53 2024 +0100
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/lib/nproc.c b/lib/nproc.c
index 92a07e8289..48bc3d06fa 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,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;