1 From bc8e655a420d2f62bb0597947e96dce7b4d3fb36 Mon Sep 17 00:00:00 2001
2 From: Wessel Dankers <wsl@fruit.je>
3 Date: Sun, 30 Oct 2022 19:29:28 +0100
4 Subject: [PATCH] Only use available CPUs
6 Not all online CPUs may be available for the current process,
7 especially when CPU affinity is involved. In such cases too many
8 threads will be created, which will then compete unnecessarily
11 Use sched_getaffinity() to determine the correct number of threads
14 squashfs-tools/mksquashfs.c | 16 ++++++++++++----
15 squashfs-tools/unsquashfs.c | 13 ++++++++++---
16 2 files changed, 22 insertions(+), 7 deletions(-)
18 --- a/squashfs-tools/mksquashfs.c
19 +++ b/squashfs-tools/mksquashfs.c
22 #include <sys/sysinfo.h>
28 #include <sys/sysctl.h>
31 @@ -5079,7 +5081,15 @@ static void initialise_threads(int readq
32 BAD_ERROR("Failed to set signal mask in intialise_threads\n");
34 if(processors == -1) {
40 + if(sched_getaffinity(0, sizeof cpu_set, &cpu_set) == -1)
41 + processors = sysconf(_SC_NPROCESSORS_ONLN);
43 + processors = CPU_COUNT(&cpu_set);
46 size_t len = sizeof(processors);
48 @@ -5096,8 +5106,6 @@ static void initialise_threads(int readq
49 ERROR_EXIT(" Defaulting to 1\n");
53 - processors = sysconf(_SC_NPROCESSORS_ONLN);
57 --- a/squashfs-tools/unsquashfs.c
58 +++ b/squashfs-tools/unsquashfs.c
60 #include "fnmatch_compat.h"
64 #include <sys/sysinfo.h>
65 #include <sys/sysmacros.h>
66 #elif defined __FreeBSD__
67 @@ -2719,7 +2720,15 @@ void initialise_threads(int fragment_buf
70 if(processors == -1) {
76 + if(sched_getaffinity(0, sizeof cpu_set, &cpu_set) == -1)
77 + processors = sysconf(_SC_NPROCESSORS_ONLN);
79 + processors = CPU_COUNT(&cpu_set);
82 size_t len = sizeof(processors);
84 @@ -2735,8 +2744,6 @@ void initialise_threads(int fragment_buf
89 - processors = sysconf(_SC_NPROCESSORS_ONLN);