diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2024-05-31 11:56:40 +0200 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2024-05-31 11:58:48 +0200 |
| commit | 5a68a246e750359819d63bcff5ef97dd9c7788fc (patch) | |
| tree | 2a9af97b38d5a9780bd63e03403d0b613194a0f4 | |
| parent | fd47f056765aed515f4c71118813f07be1402bee (diff) | |
| download | busybox-w32-5a68a246e750359819d63bcff5ef97dd9c7788fc.tar.gz busybox-w32-5a68a246e750359819d63bcff5ef97dd9c7788fc.tar.bz2 busybox-w32-5a68a246e750359819d63bcff5ef97dd9c7788fc.zip | |
nproc: prepare for arbitrarily large CPU masks
function old new delta
get_malloc_cpu_affinity - 76 +76
nproc_main 216 206 -10
process_pid_str 250 206 -44
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 0/2 up/down: 76/-54) Total: 22 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
| -rw-r--r-- | coreutils/nproc.c | 9 | ||||
| -rw-r--r-- | include/libbb.h | 2 | ||||
| -rw-r--r-- | libbb/Kbuild.src | 1 | ||||
| -rw-r--r-- | libbb/alloc_affinity.c | 29 | ||||
| -rw-r--r-- | util-linux/taskset.c | 24 |
5 files changed, 38 insertions, 27 deletions
diff --git a/coreutils/nproc.c b/coreutils/nproc.c index f1d11fa5a..a0d818c59 100644 --- a/coreutils/nproc.c +++ b/coreutils/nproc.c | |||
| @@ -23,13 +23,11 @@ | |||
| 23 | //usage: "\n --ignore=N Exclude N CPUs" | 23 | //usage: "\n --ignore=N Exclude N CPUs" |
| 24 | //usage: ) | 24 | //usage: ) |
| 25 | 25 | ||
| 26 | #include <sched.h> | ||
| 27 | #include "libbb.h" | 26 | #include "libbb.h" |
| 28 | 27 | ||
| 29 | int nproc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 28 | int nproc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 30 | int nproc_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) | 29 | int nproc_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
| 31 | { | 30 | { |
| 32 | unsigned long mask[1024]; | ||
| 33 | int count = 0; | 31 | int count = 0; |
| 34 | #if ENABLE_LONG_OPTS | 32 | #if ENABLE_LONG_OPTS |
| 35 | int ignore = 0; | 33 | int ignore = 0; |
| @@ -52,9 +50,12 @@ int nproc_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) | |||
| 52 | } | 50 | } |
| 53 | } else | 51 | } else |
| 54 | #endif | 52 | #endif |
| 55 | if (sched_getaffinity(0, sizeof(mask), (void*)mask) == 0) { | 53 | { |
| 56 | int i; | 54 | int i; |
| 57 | for (i = 0; i < ARRAY_SIZE(mask); i++) { | 55 | unsigned sz = 2 * 1024; |
| 56 | unsigned long *mask = get_malloc_cpu_affinity(0, &sz); | ||
| 57 | sz /= sizeof(long); | ||
| 58 | for (i = 0; i < sz; i++) { | ||
| 58 | unsigned long m = mask[i]; | 59 | unsigned long m = mask[i]; |
| 59 | while (m) { | 60 | while (m) { |
| 60 | if (m & 1) | 61 | if (m & 1) |
diff --git a/include/libbb.h b/include/libbb.h index ef5d04713..67d29f843 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
| @@ -2015,6 +2015,8 @@ int read_line_input(const char* prompt, char* command, int maxsize) FAST_FUNC; | |||
| 2015 | read_line_input(prompt, command, maxsize) | 2015 | read_line_input(prompt, command, maxsize) |
| 2016 | #endif | 2016 | #endif |
| 2017 | 2017 | ||
| 2018 | unsigned long* FAST_FUNC get_malloc_cpu_affinity(int pid, unsigned *sz); | ||
| 2019 | |||
| 2018 | #ifndef COMM_LEN | 2020 | #ifndef COMM_LEN |
| 2019 | # ifdef TASK_COMM_LEN | 2021 | # ifdef TASK_COMM_LEN |
| 2020 | enum { COMM_LEN = TASK_COMM_LEN }; | 2022 | enum { COMM_LEN = TASK_COMM_LEN }; |
diff --git a/libbb/Kbuild.src b/libbb/Kbuild.src index c3b30003f..a0e2a6da7 100644 --- a/libbb/Kbuild.src +++ b/libbb/Kbuild.src | |||
| @@ -10,6 +10,7 @@ lib-y:= | |||
| 10 | 10 | ||
| 11 | INSERT | 11 | INSERT |
| 12 | 12 | ||
| 13 | lib-y += alloc_affinity.o | ||
| 13 | lib-y += appletlib.o | 14 | lib-y += appletlib.o |
| 14 | lib-y += ask_confirmation.o | 15 | lib-y += ask_confirmation.o |
| 15 | lib-y += bb_askpass.o | 16 | lib-y += bb_askpass.o |
diff --git a/libbb/alloc_affinity.c b/libbb/alloc_affinity.c new file mode 100644 index 000000000..b6d9f649a --- /dev/null +++ b/libbb/alloc_affinity.c | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | /* vi: set sw=4 ts=4: */ | ||
| 2 | /* | ||
| 3 | * Utility routines. | ||
| 4 | * | ||
| 5 | * Copyright (C) 2024 Denys Vlasenko | ||
| 6 | * | ||
| 7 | * Licensed under GPLv2, see file LICENSE in this source tree. | ||
| 8 | */ | ||
| 9 | #include <sched.h> | ||
| 10 | #include "libbb.h" | ||
| 11 | |||
| 12 | unsigned long* FAST_FUNC get_malloc_cpu_affinity(int pid, unsigned *sz) | ||
| 13 | { | ||
| 14 | unsigned long *mask = NULL; | ||
| 15 | unsigned sz_in_bytes = *sz; | ||
| 16 | |||
| 17 | for (;;) { | ||
| 18 | mask = xrealloc(mask, sz_in_bytes); | ||
| 19 | if (sched_getaffinity(pid, sz_in_bytes, (void*)mask) == 0) | ||
| 20 | break; /* got it */ | ||
| 21 | sz_in_bytes *= 2; | ||
| 22 | if (errno == EINVAL && (int)sz_in_bytes > 0) | ||
| 23 | continue; | ||
| 24 | bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid); | ||
| 25 | } | ||
| 26 | //bb_error_msg("get mask[0]:%lx sz_in_bytes:%d", mask[0], sz_in_bytes); | ||
| 27 | *sz = sz_in_bytes; | ||
| 28 | return mask; | ||
| 29 | } | ||
diff --git a/util-linux/taskset.c b/util-linux/taskset.c index 55c915e8d..a3aa06119 100644 --- a/util-linux/taskset.c +++ b/util-linux/taskset.c | |||
| @@ -56,7 +56,6 @@ | |||
| 56 | * -a/--all-tasks (affect all threads) | 56 | * -a/--all-tasks (affect all threads) |
| 57 | * needs to get TIDs from /proc/PID/task/ and use _them_ as "pid" in sched_setaffinity(pid) | 57 | * needs to get TIDs from /proc/PID/task/ and use _them_ as "pid" in sched_setaffinity(pid) |
| 58 | */ | 58 | */ |
| 59 | |||
| 60 | #include <sched.h> | 59 | #include <sched.h> |
| 61 | #include "libbb.h" | 60 | #include "libbb.h" |
| 62 | 61 | ||
| @@ -96,27 +95,6 @@ static unsigned long from_mask(ul *mask, unsigned sz_in_bytes UNUSED_PARAM) | |||
| 96 | } | 95 | } |
| 97 | #endif | 96 | #endif |
| 98 | 97 | ||
| 99 | static unsigned long *get_aff(int pid, unsigned *sz) | ||
| 100 | { | ||
| 101 | int r; | ||
| 102 | unsigned long *mask = NULL; | ||
| 103 | unsigned sz_in_bytes = *sz; | ||
| 104 | |||
| 105 | for (;;) { | ||
| 106 | mask = xrealloc(mask, sz_in_bytes); | ||
| 107 | r = sched_getaffinity(pid, sz_in_bytes, (void*)mask); | ||
| 108 | if (r == 0) | ||
| 109 | break; | ||
| 110 | sz_in_bytes *= 2; | ||
| 111 | if (errno == EINVAL && (int)sz_in_bytes > 0) | ||
| 112 | continue; | ||
| 113 | bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid); | ||
| 114 | } | ||
| 115 | //bb_error_msg("get mask[0]:%lx sz_in_bytes:%d", mask[0], sz_in_bytes); | ||
| 116 | *sz = sz_in_bytes; | ||
| 117 | return mask; | ||
| 118 | } | ||
| 119 | |||
| 120 | #if ENABLE_FEATURE_TASKSET_CPULIST | 98 | #if ENABLE_FEATURE_TASKSET_CPULIST |
| 121 | /* | 99 | /* |
| 122 | * Parse the CPU list and set the mask accordingly. | 100 | * Parse the CPU list and set the mask accordingly. |
| @@ -222,7 +200,7 @@ static int process_pid_str(const char *pid_str, unsigned opts, char *aff) | |||
| 222 | mask_size_in_bytes = SZOF_UL; | 200 | mask_size_in_bytes = SZOF_UL; |
| 223 | current_new = "current"; | 201 | current_new = "current"; |
| 224 | print_aff: | 202 | print_aff: |
| 225 | mask = get_aff(pid, &mask_size_in_bytes); | 203 | mask = get_malloc_cpu_affinity(pid, &mask_size_in_bytes); |
| 226 | if (opts & OPT_p) { | 204 | if (opts & OPT_p) { |
| 227 | #if ENABLE_FEATURE_TASKSET_CPULIST | 205 | #if ENABLE_FEATURE_TASKSET_CPULIST |
| 228 | if (opts & OPT_c) { | 206 | if (opts & OPT_c) { |
