Deal with number of available CPUs rather than number of online CPUs to cope with CPU affinity being set.

This commit is contained in:
ckolivas 2026-02-12 20:48:54 +11:00
parent 7534c29ff8
commit 3f48188a45
3 changed files with 23 additions and 8 deletions

23
lrzip.c
View file

@ -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;

View file

@ -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))

2
main.c
View file

@ -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);