diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2025-03-10 16:36:51 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2025-03-10 16:36:51 -0400 |
commit | 05a3361c60ba7dd8217ac1109e8fff090be4b0ef (patch) | |
tree | 7f524dba7bc7689aae5260dab16f7ed82f9ac175 | |
parent | 9e3224a0ab7257ad4705d6218f42fc1d71b327ec (diff) | |
download | bfs-05a3361c60ba7dd8217ac1109e8fff090be4b0ef.tar.xz |
bfstd: Fix nproc() on systems without dynamically sized CPU masks
Notes
Fixes: a36774b ("bfstd: Take sched_getaffinity() into account in nproc()")
-rw-r--r-- | src/bfstd.c | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/src/bfstd.c b/src/bfstd.c index 5d60199..82663eb 100644 --- a/src/bfstd.c +++ b/src/bfstd.c @@ -17,6 +17,7 @@ #include <locale.h> #include <nl_types.h> #include <pthread.h> +#include <sched.h> #include <stddef.h> #include <stdint.h> #include <stdio.h> @@ -789,7 +790,12 @@ static long bfs_sched_getaffinity(size_t size) { long ret = -1; if (sched_getaffinity(0, size, pset) == 0) { +# ifdef CPU_COUNT_S ret = CPU_COUNT_S(size, pset); +# else + bfs_assert(size <= sizeof(set)); + ret = CPU_COUNT(pset); +# endif } if (pset != &set) { @@ -805,15 +811,21 @@ long nproc(void) { #if BFS_HAS_SCHED_GETAFFINITY size_t size = sizeof(cpu_set_t); do { - // sched_getaffinity(2) says: + ret = bfs_sched_getaffinity(size); + +# ifdef CPU_COUNT_S + // On Linux, sched_getaffinity(2) says: // // When working on systems with large kernel CPU affinity masks, one must // dynamically allocate the mask argument (see CPU_ALLOC(3)). Currently, // the only way to do this is by probing for the size of the required mask // using sched_getaffinity() calls with increasing mask sizes (until the // call does not fail with the error EINVAL). - ret = bfs_sched_getaffinity(size); size *= 2; +# else + // No support for dynamically-sized CPU masks + break; +# endif } while (ret < 0 && errno == EINVAL); #endif |