Skip to content

Commit 05a3361

Browse files
committed
bfstd: Fix nproc() on systems without dynamically sized CPU masks
1 parent 9e3224a commit 05a3361

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/bfstd.c

+14-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <locale.h>
1818
#include <nl_types.h>
1919
#include <pthread.h>
20+
#include <sched.h>
2021
#include <stddef.h>
2122
#include <stdint.h>
2223
#include <stdio.h>
@@ -789,7 +790,12 @@ static long bfs_sched_getaffinity(size_t size) {
789790

790791
long ret = -1;
791792
if (sched_getaffinity(0, size, pset) == 0) {
793+
# ifdef CPU_COUNT_S
792794
ret = CPU_COUNT_S(size, pset);
795+
# else
796+
bfs_assert(size <= sizeof(set));
797+
ret = CPU_COUNT(pset);
798+
# endif
793799
}
794800

795801
if (pset != &set) {
@@ -805,15 +811,21 @@ long nproc(void) {
805811
#if BFS_HAS_SCHED_GETAFFINITY
806812
size_t size = sizeof(cpu_set_t);
807813
do {
808-
// sched_getaffinity(2) says:
814+
ret = bfs_sched_getaffinity(size);
815+
816+
# ifdef CPU_COUNT_S
817+
// On Linux, sched_getaffinity(2) says:
809818
//
810819
// When working on systems with large kernel CPU affinity masks, one must
811820
// dynamically allocate the mask argument (see CPU_ALLOC(3)). Currently,
812821
// the only way to do this is by probing for the size of the required mask
813822
// using sched_getaffinity() calls with increasing mask sizes (until the
814823
// call does not fail with the error EINVAL).
815-
ret = bfs_sched_getaffinity(size);
816824
size *= 2;
825+
# else
826+
// No support for dynamically-sized CPU masks
827+
break;
828+
# endif
817829
} while (ret < 0 && errno == EINVAL);
818830
#endif
819831

0 commit comments

Comments
 (0)