tools/squashfs4: add new tool for squashfs4 images
[openwrt/staging/aparcar.git] / tools / squashfs4 / patches / 003-Only-use-available-CPUs.patch
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
5
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
9 for CPU time.
10
11 Use sched_getaffinity() to determine the correct number of threads
12 to create.
13 ---
14 squashfs-tools/mksquashfs.c | 16 ++++++++++++----
15 squashfs-tools/unsquashfs.c | 13 ++++++++++---
16 2 files changed, 22 insertions(+), 7 deletions(-)
17
18 --- a/squashfs-tools/mksquashfs.c
19 +++ b/squashfs-tools/mksquashfs.c
20 @@ -52,7 +52,9 @@
21 #include <ctype.h>
22 #include <sys/sysinfo.h>
23
24 -#ifndef linux
25 +#ifdef linux
26 +#include <sched.h>
27 +#else
28 #include <sys/sysctl.h>
29 #endif
30
31 @@ -5079,7 +5081,15 @@ static void initialise_threads(int readq
32 BAD_ERROR("Failed to set signal mask in intialise_threads\n");
33
34 if(processors == -1) {
35 -#ifndef linux
36 +#ifdef linux
37 + cpu_set_t cpu_set;
38 + CPU_ZERO(&cpu_set);
39 +
40 + if(sched_getaffinity(0, sizeof cpu_set, &cpu_set) == -1)
41 + processors = sysconf(_SC_NPROCESSORS_ONLN);
42 + else
43 + processors = CPU_COUNT(&cpu_set);
44 +#else
45 int mib[2];
46 size_t len = sizeof(processors);
47
48 @@ -5096,8 +5106,6 @@ static void initialise_threads(int readq
49 ERROR_EXIT(" Defaulting to 1\n");
50 processors = 1;
51 }
52 -#else
53 - processors = sysconf(_SC_NPROCESSORS_ONLN);
54 #endif
55 }
56
57 --- a/squashfs-tools/unsquashfs.c
58 +++ b/squashfs-tools/unsquashfs.c
59 @@ -33,6 +33,7 @@
60 #include "fnmatch_compat.h"
61
62 #ifdef __linux__
63 +#include <sched.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
68 }
69
70 if(processors == -1) {
71 -#ifndef linux
72 +#ifdef linux
73 + cpu_set_t cpu_set;
74 + CPU_ZERO(&cpu_set);
75 +
76 + if(sched_getaffinity(0, sizeof cpu_set, &cpu_set) == -1)
77 + processors = sysconf(_SC_NPROCESSORS_ONLN);
78 + else
79 + processors = CPU_COUNT(&cpu_set);
80 +#else
81 int mib[2];
82 size_t len = sizeof(processors);
83
84 @@ -2735,8 +2744,6 @@ void initialise_threads(int fragment_buf
85 "Defaulting to 1\n");
86 processors = 1;
87 }
88 -#else
89 - processors = sysconf(_SC_NPROCESSORS_ONLN);
90 #endif
91 }
92