Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * taskset - retrieve or set a processes' CPU affinity |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 4 | * Copyright (c) 2006 Bernhard Reutner-Fischer |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 5 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 7 | */ |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 8 | //config:config TASKSET |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 9 | //config: bool "taskset (4.2 kb)" |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 10 | //config: default y |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 11 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 12 | //config: Retrieve or set a processes's CPU affinity. |
| 13 | //config: This requires sched_{g,s}etaffinity support in your libc. |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 14 | //config: |
| 15 | //config:config FEATURE_TASKSET_FANCY |
| 16 | //config: bool "Fancy output" |
| 17 | //config: default y |
| 18 | //config: depends on TASKSET |
| 19 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 20 | //config: Needed for machines with more than 32-64 CPUs: |
| 21 | //config: affinity parameter 0xHHHHHHHHHHHHHHHHHHHH can be arbitrarily long |
| 22 | //config: in this case. Otherwise, it is limited to sizeof(long). |
Denys Vlasenko | 162ac7f | 2019-11-01 15:44:49 +0100 | [diff] [blame] | 23 | //config: |
| 24 | //config:config FEATURE_TASKSET_CPULIST |
| 25 | //config: bool "CPU list support (-c option)" |
| 26 | //config: default y |
| 27 | //config: depends on FEATURE_TASKSET_FANCY |
| 28 | //config: help |
| 29 | //config: Add support for taking/printing affinity as CPU list when '-c' |
| 30 | //config: option is used. For example, it prints '0-3,7' instead of mask '8f'. |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 31 | |
Denys Vlasenko | 5c527dc | 2017-08-04 19:55:01 +0200 | [diff] [blame] | 32 | //applet:IF_TASKSET(APPLET_NOEXEC(taskset, taskset, BB_DIR_USR_BIN, BB_SUID_DROP, taskset)) |
Denys Vlasenko | 0c4dbd4 | 2017-09-18 16:28:43 +0200 | [diff] [blame] | 33 | |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 34 | //kbuild:lib-$(CONFIG_TASKSET) += taskset.o |
| 35 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 36 | //usage:#define taskset_trivial_usage |
Denys Vlasenko | 2512817 | 2021-06-19 17:42:35 +0200 | [diff] [blame] | 37 | //usage: "[-ap] [HEXMASK"IF_FEATURE_TASKSET_CPULIST(" | -c LIST")"] { PID | PROG ARGS }" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 38 | //usage:#define taskset_full_usage "\n\n" |
| 39 | //usage: "Set or get CPU affinity\n" |
Denys Vlasenko | 2512817 | 2021-06-19 17:42:35 +0200 | [diff] [blame] | 40 | //usage: "\n -p Operate on PID" |
| 41 | //usage: "\n -a Operate on all threads" |
| 42 | //usage: "\n -c Affinity is a list, not mask" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 43 | //usage: |
| 44 | //usage:#define taskset_example_usage |
| 45 | //usage: "$ taskset 0x7 ./dgemm_test&\n" |
| 46 | //usage: "$ taskset -p 0x1 $!\n" |
| 47 | //usage: "pid 4790's current affinity mask: 7\n" |
| 48 | //usage: "pid 4790's new affinity mask: 1\n" |
| 49 | //usage: "$ taskset 0x7 /bin/sh -c './taskset -p 0x1 $$'\n" |
| 50 | //usage: "pid 6671's current affinity mask: 1\n" |
| 51 | //usage: "pid 6671's new affinity mask: 1\n" |
| 52 | //usage: "$ taskset -p 1\n" |
| 53 | //usage: "pid 1's current affinity mask: 3\n" |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 54 | /* |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 55 | * Not yet implemented: |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 56 | * -a/--all-tasks (affect all threads) |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 57 | * needs to get TIDs from /proc/PID/task/ and use _them_ as "pid" in sched_setaffinity(pid) |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 58 | */ |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 59 | |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 60 | #include <sched.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 61 | #include "libbb.h" |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 62 | |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 63 | typedef unsigned long ul; |
| 64 | #define SZOF_UL (unsigned)(sizeof(ul)) |
| 65 | #define BITS_UL (unsigned)(sizeof(ul)*8) |
| 66 | #define MASK_UL (unsigned)(sizeof(ul)*8 - 1) |
| 67 | |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 68 | #if ENABLE_FEATURE_TASKSET_FANCY |
| 69 | #define TASKSET_PRINTF_MASK "%s" |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 70 | /* craft a string from the mask */ |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 71 | static char *from_mask(const ul *mask, unsigned sz_in_bytes) |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 72 | { |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 73 | char *str = xzalloc((sz_in_bytes+1) * 2); /* we will leak it */ |
| 74 | char *p = str; |
| 75 | for (;;) { |
| 76 | ul v = *mask++; |
| 77 | if (SZOF_UL == 4) |
| 78 | p += sprintf(p, "%08lx", v); |
| 79 | if (SZOF_UL == 8) |
| 80 | p += sprintf(p, "%016lx", v); |
| 81 | if (SZOF_UL == 16) |
| 82 | p += sprintf(p, "%032lx", v); /* :) */ |
| 83 | sz_in_bytes -= SZOF_UL; |
| 84 | if ((int)sz_in_bytes <= 0) |
| 85 | break; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 86 | } |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 87 | while (str[0] == '0' && str[1]) |
| 88 | str++; |
| 89 | return str; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 90 | } |
| 91 | #else |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 92 | #define TASKSET_PRINTF_MASK "%lx" |
Denys Vlasenko | 55f969a | 2022-03-01 10:46:49 +0100 | [diff] [blame] | 93 | static unsigned long from_mask(ul *mask, unsigned sz_in_bytes UNUSED_PARAM) |
Denis Vlasenko | 0e52541 | 2008-07-11 13:57:08 +0000 | [diff] [blame] | 94 | { |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 95 | return *mask; |
Denis Vlasenko | 0e52541 | 2008-07-11 13:57:08 +0000 | [diff] [blame] | 96 | } |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 97 | #endif |
| 98 | |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 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 | } |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 119 | |
Denys Vlasenko | 162ac7f | 2019-11-01 15:44:49 +0100 | [diff] [blame] | 120 | #if ENABLE_FEATURE_TASKSET_CPULIST |
| 121 | /* |
| 122 | * Parse the CPU list and set the mask accordingly. |
| 123 | * |
| 124 | * The list element can be either a CPU index or a range of CPU indices. |
Denys Vlasenko | a82fb1b | 2019-11-09 17:05:14 +0100 | [diff] [blame] | 125 | * Example: "1,3,5-7". Stride can be specified: "0-7:2" is "0,2,4,6". |
| 126 | * Note: leading and trailing whitespace is not allowed. |
Denys Vlasenko | e558660 | 2019-11-08 16:32:37 +0100 | [diff] [blame] | 127 | * util-linux 2.31 allows leading and sometimes trailing whitespace: |
| 128 | * ok: taskset -c ' 1, 2' |
| 129 | * ok: taskset -c ' 1 , 2' |
| 130 | * ok: taskset -c ' 1-7: 2 ,8' |
| 131 | * not ok: taskset -c ' 1 ' |
| 132 | * not ok: taskset -c ' 1-7: 2 ' |
Denys Vlasenko | 162ac7f | 2019-11-01 15:44:49 +0100 | [diff] [blame] | 133 | */ |
| 134 | static void parse_cpulist(ul *mask, unsigned max, char *s) |
| 135 | { |
| 136 | char *aff = s; |
| 137 | for (;;) { |
| 138 | unsigned bit, end; |
Denys Vlasenko | a82fb1b | 2019-11-09 17:05:14 +0100 | [diff] [blame] | 139 | unsigned stride = 1; |
Denys Vlasenko | 162ac7f | 2019-11-01 15:44:49 +0100 | [diff] [blame] | 140 | |
| 141 | bit = end = bb_strtou(s, &s, 10); |
| 142 | if (*s == '-') { |
| 143 | s++; |
| 144 | end = bb_strtou(s, &s, 10); |
Denys Vlasenko | a82fb1b | 2019-11-09 17:05:14 +0100 | [diff] [blame] | 145 | if (*s == ':') { |
| 146 | s++; |
| 147 | stride = bb_strtou(s, &s, 10); |
| 148 | } |
Denys Vlasenko | 162ac7f | 2019-11-01 15:44:49 +0100 | [diff] [blame] | 149 | } |
| 150 | if ((*s != ',' && *s != '\0') |
| 151 | || bit > end |
| 152 | || end == UINT_MAX /* bb_strtou returns this on malformed / ERANGE numbers */ |
Denys Vlasenko | b230fdf | 2019-11-09 17:32:43 +0100 | [diff] [blame] | 153 | || (stride - 1) > (UINT_MAX / 4) |
| 154 | /* disallow 0, malformed input, and too large stride prone to overflows */ |
Denys Vlasenko | 162ac7f | 2019-11-01 15:44:49 +0100 | [diff] [blame] | 155 | ) { |
| 156 | bb_error_msg_and_die("bad affinity '%s'", aff); |
| 157 | } |
| 158 | while (bit <= end && bit < max) { |
| 159 | mask[bit / BITS_UL] |= (1UL << (bit & MASK_UL)); |
Denys Vlasenko | a82fb1b | 2019-11-09 17:05:14 +0100 | [diff] [blame] | 160 | bit += stride; |
Denys Vlasenko | 162ac7f | 2019-11-01 15:44:49 +0100 | [diff] [blame] | 161 | } |
| 162 | if (*s == '\0') |
| 163 | break; |
| 164 | s++; |
| 165 | } |
| 166 | } |
| 167 | static void print_cpulist(const ul *mask, unsigned mask_size_in_bytes) |
| 168 | { |
| 169 | const ul *mask_end; |
| 170 | const char *delim; |
| 171 | unsigned pos; |
| 172 | ul bit; |
| 173 | |
| 174 | mask_end = mask + mask_size_in_bytes / sizeof(mask[0]); |
| 175 | delim = ""; |
| 176 | pos = 0; |
| 177 | bit = 1; |
| 178 | for (;;) { |
| 179 | if (*mask & bit) { |
| 180 | unsigned onebit = pos + 1; |
| 181 | printf("%s%u", delim, pos); |
| 182 | do { |
| 183 | pos++; |
| 184 | bit <<= 1; |
| 185 | if (bit == 0) { |
| 186 | mask++; |
| 187 | if (mask >= mask_end) |
| 188 | break; |
| 189 | bit = 1; |
| 190 | } |
| 191 | } while (*mask & bit); |
| 192 | if (onebit != pos) |
| 193 | printf("-%u", pos - 1); |
| 194 | delim = ","; |
| 195 | } |
| 196 | pos++; |
| 197 | bit <<= 1; |
| 198 | if (bit == 0) { |
| 199 | mask++; |
| 200 | if (mask >= mask_end) |
| 201 | break; |
| 202 | bit = 1; |
| 203 | } |
| 204 | } |
| 205 | bb_putchar('\n'); |
| 206 | } |
| 207 | #endif |
| 208 | |
Denys Vlasenko | 2512817 | 2021-06-19 17:42:35 +0200 | [diff] [blame] | 209 | enum { |
| 210 | OPT_p = 1 << 0, |
| 211 | OPT_a = 1 << 1, |
| 212 | OPT_c = (1 << 2) * ENABLE_FEATURE_TASKSET_CPULIST, |
| 213 | }; |
| 214 | |
Denys Vlasenko | 6d61eb1 | 2021-06-24 12:02:17 +0200 | [diff] [blame] | 215 | static int process_pid_str(const char *pid_str, unsigned opts, char *aff) |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 216 | { |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 217 | ul *mask; |
| 218 | unsigned mask_size_in_bytes; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 219 | const char *current_new; |
Denys Vlasenko | 319e20b | 2021-08-15 20:41:18 +0200 | [diff] [blame] | 220 | pid_t pid = !pid_str ? 0 : xatou_range(pid_str, 1, INT_MAX); /* disallow "0": "taskset -p 0" should fail */ |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 221 | |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 222 | mask_size_in_bytes = SZOF_UL; |
Denys Vlasenko | 8666391 | 2017-01-29 18:59:38 +0100 | [diff] [blame] | 223 | current_new = "current"; |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 224 | print_aff: |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 225 | mask = get_aff(pid, &mask_size_in_bytes); |
Denys Vlasenko | 162ac7f | 2019-11-01 15:44:49 +0100 | [diff] [blame] | 226 | if (opts & OPT_p) { |
| 227 | #if ENABLE_FEATURE_TASKSET_CPULIST |
| 228 | if (opts & OPT_c) { |
| 229 | printf("pid %d's %s affinity list: ", pid, current_new); |
| 230 | print_cpulist(mask, mask_size_in_bytes); |
| 231 | } else |
| 232 | #endif |
| 233 | printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n", |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 234 | pid, current_new, from_mask(mask, mask_size_in_bytes)); |
Denys Vlasenko | 2512817 | 2021-06-19 17:42:35 +0200 | [diff] [blame] | 235 | if (!aff) { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 236 | /* Either it was just "-p <pid>", |
| 237 | * or it was "-p <aff> <pid>" and we came here |
| 238 | * for the second time (see goto below) */ |
Denys Vlasenko | 6d61eb1 | 2021-06-24 12:02:17 +0200 | [diff] [blame] | 239 | return 0; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 240 | } |
Denys Vlasenko | 8666391 | 2017-01-29 18:59:38 +0100 | [diff] [blame] | 241 | current_new = "new"; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 242 | } |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 243 | memset(mask, 0, mask_size_in_bytes); |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 244 | |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 245 | if (!ENABLE_FEATURE_TASKSET_FANCY) { |
Denys Vlasenko | 162ac7f | 2019-11-01 15:44:49 +0100 | [diff] [blame] | 246 | /* Affinity was specified, translate it into mask */ |
| 247 | /* it is always in hex, skip "0x" if it exists */ |
| 248 | if (aff[0] == '0' && (aff[1]|0x20) == 'x') |
| 249 | aff += 2; |
Denys Vlasenko | d6ace66 | 2017-01-30 22:20:06 +0100 | [diff] [blame] | 250 | mask[0] = xstrtoul(aff, 16); |
Denys Vlasenko | 162ac7f | 2019-11-01 15:44:49 +0100 | [diff] [blame] | 251 | } |
| 252 | #if ENABLE_FEATURE_TASKSET_CPULIST |
| 253 | else if (opts & OPT_c) { |
| 254 | parse_cpulist(mask, mask_size_in_bytes * 8, aff); |
| 255 | } |
| 256 | #endif |
| 257 | else { |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 258 | unsigned i; |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 259 | char *last_char; |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 260 | |
Denys Vlasenko | 162ac7f | 2019-11-01 15:44:49 +0100 | [diff] [blame] | 261 | /* Affinity was specified, translate it into mask */ |
| 262 | /* it is always in hex, skip "0x" if it exists */ |
| 263 | if (aff[0] == '0' && (aff[1]|0x20) == 'x') |
| 264 | aff += 2; |
| 265 | |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 266 | i = 0; /* bit pos in mask[] */ |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 267 | |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 268 | /* aff is ASCII hex string, accept very long masks in this form. |
| 269 | * Process hex string AABBCCDD... to ulong mask[] |
| 270 | * from the rightmost nibble, which is least-significant. |
| 271 | * Bits not fitting into mask[] are ignored: (example: 1234 |
| 272 | * in 12340000000000000000000000000000000000000ff) |
| 273 | */ |
| 274 | last_char = strchrnul(aff, '\0'); |
| 275 | while (last_char > aff) { |
| 276 | char c; |
| 277 | ul val; |
| 278 | |
| 279 | last_char--; |
| 280 | c = *last_char; |
| 281 | if (isdigit(c)) |
| 282 | val = c - '0'; |
| 283 | else if ((c|0x20) >= 'a' && (c|0x20) <= 'f') |
| 284 | val = (c|0x20) - ('a' - 10); |
| 285 | else |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 286 | bb_error_msg_and_die("bad affinity '%s'", aff); |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 287 | |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 288 | if (i < mask_size_in_bytes * 8) { |
| 289 | mask[i / BITS_UL] |= val << (i & MASK_UL); |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 290 | //bb_error_msg("bit %d set", i); |
| 291 | } |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 292 | /* else: |
| 293 | * We can error out here, but we don't. |
| 294 | * For one, kernel itself ignores bits in mask[] |
Denys Vlasenko | d6ace66 | 2017-01-30 22:20:06 +0100 | [diff] [blame] | 295 | * which do not map to any CPUs: |
| 296 | * if mask[] has one 32-bit long element, |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 297 | * but you have only 8 CPUs, all bits beyond first 8 |
| 298 | * are ignored, silently. |
| 299 | * No point in making bits past 31th to be errors. |
| 300 | */ |
| 301 | i += 4; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 302 | } |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 303 | } |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 304 | |
| 305 | /* Set pid's or our own (pid==0) affinity */ |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 306 | if (sched_setaffinity(pid, mask_size_in_bytes, (void*)mask)) |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 307 | bb_perror_msg_and_die("can't %cet pid %d's affinity", 's', pid); |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 308 | //bb_error_msg("set mask[0]:%lx", mask[0]); |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 309 | |
Denys Vlasenko | 2512817 | 2021-06-19 17:42:35 +0200 | [diff] [blame] | 310 | if ((opts & OPT_p) && aff) { /* "-p <aff> <pid> [...ignored...]" */ |
| 311 | aff = NULL; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 312 | goto print_aff; /* print new affinity and exit */ |
Denys Vlasenko | 2512817 | 2021-06-19 17:42:35 +0200 | [diff] [blame] | 313 | } |
Denys Vlasenko | 6d61eb1 | 2021-06-24 12:02:17 +0200 | [diff] [blame] | 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static int FAST_FUNC iter(const char *dn UNUSED_PARAM, struct dirent *ent, void *aff) |
| 318 | { |
| 319 | if (isdigit(ent->d_name[0])) |
| 320 | return process_pid_str(ent->d_name, option_mask32, aff); |
| 321 | return 0; |
Denys Vlasenko | 2512817 | 2021-06-19 17:42:35 +0200 | [diff] [blame] | 322 | } |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 323 | |
Denys Vlasenko | 2512817 | 2021-06-19 17:42:35 +0200 | [diff] [blame] | 324 | int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 325 | int taskset_main(int argc UNUSED_PARAM, char **argv) |
| 326 | { |
| 327 | const char *pid_str; |
| 328 | char *aff; |
| 329 | unsigned opts; |
| 330 | |
| 331 | /* NB: we mimic util-linux's taskset: -p does not take |
| 332 | * an argument, i.e., "-pN" is NOT valid, only "-p N"! |
| 333 | * Indeed, util-linux-2.13-pre7 uses: |
| 334 | * getopt_long(argc, argv, "+pchV", ...), not "...p:..." */ |
| 335 | |
| 336 | opts = getopt32(argv, "^+" "pa"IF_FEATURE_TASKSET_CPULIST("c") |
| 337 | "\0" "-1" /* at least 1 arg */); |
| 338 | argv += optind; |
| 339 | |
| 340 | aff = *argv++; |
| 341 | if (!(opts & OPT_p)) { |
| 342 | /* <aff> <cmd...> */ |
| 343 | if (!*argv) |
| 344 | bb_show_usage(); |
Denys Vlasenko | 319e20b | 2021-08-15 20:41:18 +0200 | [diff] [blame] | 345 | process_pid_str(NULL, opts, aff); |
Denys Vlasenko | 2512817 | 2021-06-19 17:42:35 +0200 | [diff] [blame] | 346 | BB_EXECVP_or_die(argv); |
| 347 | } |
| 348 | |
| 349 | pid_str = aff; |
| 350 | if (*argv) /* "-p <aff> <pid> ...rest.is.ignored..." */ |
Denys Vlasenko | 6d61eb1 | 2021-06-24 12:02:17 +0200 | [diff] [blame] | 351 | pid_str = *argv; |
Denys Vlasenko | 2512817 | 2021-06-19 17:42:35 +0200 | [diff] [blame] | 352 | else |
| 353 | aff = NULL; |
| 354 | |
| 355 | if (opts & OPT_a) { |
Denys Vlasenko | 9c291f2 | 2021-06-20 09:02:03 +0200 | [diff] [blame] | 356 | char *dn; |
Denys Vlasenko | 6d61eb1 | 2021-06-24 12:02:17 +0200 | [diff] [blame] | 357 | int r; |
Denys Vlasenko | 2512817 | 2021-06-19 17:42:35 +0200 | [diff] [blame] | 358 | |
Denys Vlasenko | 9c291f2 | 2021-06-20 09:02:03 +0200 | [diff] [blame] | 359 | dn = xasprintf("/proc/%s/task", pid_str); |
Denys Vlasenko | 6d61eb1 | 2021-06-24 12:02:17 +0200 | [diff] [blame] | 360 | r = iterate_on_dir(dn, iter, aff); |
Denys Vlasenko | 9c291f2 | 2021-06-20 09:02:03 +0200 | [diff] [blame] | 361 | IF_FEATURE_CLEAN_UP(free(dn);) |
Denys Vlasenko | 6d61eb1 | 2021-06-24 12:02:17 +0200 | [diff] [blame] | 362 | if (r == 0) |
| 363 | return r; /* EXIT_SUCCESS */ |
| 364 | /* else: no /proc/PID/task, act as if no -a was given */ |
Denys Vlasenko | 2512817 | 2021-06-19 17:42:35 +0200 | [diff] [blame] | 365 | } |
Denys Vlasenko | 6d61eb1 | 2021-06-24 12:02:17 +0200 | [diff] [blame] | 366 | return process_pid_str(pid_str, opts, aff); |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 367 | } |