From 3f48188a45874e65d0fa3dc1d7021c5e78ef2a08 Mon Sep 17 00:00:00 2001 From: ckolivas Date: Thu, 12 Feb 2026 20:48:54 +1100 Subject: [PATCH] Deal with number of available CPUs rather than number of online CPUs to cope with CPU affinity being set. --- lrzip.c | 23 ++++++++++++++++++++++- lrzip_private.h | 6 ------ main.c | 2 +- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/lrzip.c b/lrzip.c index e004cf9..ac3c923 100644 --- a/lrzip.c +++ b/lrzip.c @@ -1464,6 +1464,27 @@ error: return false; } +static int get_available_cpus(void) +{ + long sys; +#ifdef __linux__ + cpu_set_t mask; + CPU_ZERO(&mask); + + if (sched_getaffinity(0, sizeof(mask), &mask) == 0) { + int count = CPU_COUNT(&mask); + if (count > 0) + return count; + } +#endif + /* Fallback to system-wide online CPUs */ + sys = sysconf(_SC_NPROCESSORS_ONLN); + if (sys > 0) + return (int)sys; + + return 1; /* Absolute minimum */ +} + bool initialise_control(rzip_control *control) { time_t now_t, tdiff; @@ -1481,7 +1502,7 @@ bool initialise_control(rzip_control *control) if (unlikely(control->ramsize == -1)) return false; /* for testing single CPU */ - control->threads = PROCESSORS; /* get CPUs for LZMA */ + control->threads = get_available_cpus(); /* get CPUs for LZMA */ control->page_size = PAGE_SIZE; control->nice_val = 19; diff --git a/lrzip_private.h b/lrzip_private.h index 689025d..622ffee 100644 --- a/lrzip_private.h +++ b/lrzip_private.h @@ -249,12 +249,6 @@ typedef sem_t cksem_t; #define one_g (1000 * 1024 * 1024) -#if defined(NOTHREAD) || !defined(_SC_NPROCESSORS_ONLN) -# define PROCESSORS (1) -#else -# define PROCESSORS (sysconf(_SC_NPROCESSORS_ONLN)) -#endif - #ifndef PAGE_SIZE # ifdef _SC_PAGE_SIZE # define PAGE_SIZE (sysconf(_SC_PAGE_SIZE)) diff --git a/main.c b/main.c index 8e0309b..796916b 100644 --- a/main.c +++ b/main.c @@ -163,7 +163,7 @@ static void show_summary(void) if (!TEST_ONLY) print_verbose("The following options are in effect for this %s.\n", DECOMPRESS ? "DECOMPRESSION" : "COMPRESSION"); - print_verbose("Threading is %s. Number of CPUs detected: %d\n", control->threads > 1? "ENABLED" : "DISABLED", + print_verbose("Threading is %s. Number of CPUs available detected: %d\n", control->threads > 1? "ENABLED" : "DISABLED", control->threads); print_verbose("Detected %"PRId64" bytes ram\n", control->ramsize); print_verbose("Compression level %d\n", control->compression_level);