From b7877c45fc7fe47709c963e15214a3dd5fc71e32 Mon Sep 17 00:00:00 2001 From: Wessel Dankers Date: Sun, 30 Oct 2022 20:04:05 +0100 Subject: Only use available CPUs Not all CPUs may be available for the current process. Some CPUs may be offline, others may not be included in the process affinity mask. In such cases too many threads will be created, which will then compete unnecessarily for CPU time. Use sched_getaffinity() to determine the correct number of threads to create. --- lib/common/writer/init.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'lib/common/writer/init.c') diff --git a/lib/common/writer/init.c b/lib/common/writer/init.c index 06726ce..7940c3f 100644 --- a/lib/common/writer/init.c +++ b/lib/common/writer/init.c @@ -10,16 +10,20 @@ #include #include +#include -#ifdef HAVE_SYS_SYSINFO_H -#include +#ifdef HAVE_SCHED_GETAFFINITY +#include static size_t os_get_num_jobs(void) { - int nprocs; + cpu_set_t cpu_set; + CPU_ZERO(&cpu_set); - nprocs = get_nprocs_conf(); - return nprocs < 1 ? 1 : nprocs; + if (sched_getaffinity(0, sizeof cpu_set, &cpu_set) == -1) + return 1; + else + return CPU_COUNT(&cpu_set); } #else static size_t os_get_num_jobs(void) -- cgit v1.2.3