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 | * -c/--cpu-list (specify CPUs via "1,3,5-7") |
| 59 | */ |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 60 | |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 61 | #include <sched.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 62 | #include "libbb.h" |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 63 | |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 64 | typedef unsigned long ul; |
| 65 | #define SZOF_UL (unsigned)(sizeof(ul)) |
| 66 | #define BITS_UL (unsigned)(sizeof(ul)*8) |
| 67 | #define MASK_UL (unsigned)(sizeof(ul)*8 - 1) |
| 68 | |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 69 | #if ENABLE_FEATURE_TASKSET_FANCY |
| 70 | #define TASKSET_PRINTF_MASK "%s" |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 71 | /* craft a string from the mask */ |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 72 | static char *from_mask(const ul *mask, unsigned sz_in_bytes) |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 73 | { |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 74 | char *str = xzalloc((sz_in_bytes+1) * 2); /* we will leak it */ |
| 75 | char *p = str; |
| 76 | for (;;) { |
| 77 | ul v = *mask++; |
| 78 | if (SZOF_UL == 4) |
| 79 | p += sprintf(p, "%08lx", v); |
| 80 | if (SZOF_UL == 8) |
| 81 | p += sprintf(p, "%016lx", v); |
| 82 | if (SZOF_UL == 16) |
| 83 | p += sprintf(p, "%032lx", v); /* :) */ |
| 84 | sz_in_bytes -= SZOF_UL; |
| 85 | if ((int)sz_in_bytes <= 0) |
| 86 | break; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 87 | } |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 88 | while (str[0] == '0' && str[1]) |
| 89 | str++; |
| 90 | return str; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 91 | } |
| 92 | #else |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 93 | #define TASKSET_PRINTF_MASK "%lx" |
| 94 | static unsigned long long from_mask(ul *mask, unsigned sz_in_bytes UNUSED_PARAM) |
Denis Vlasenko | 0e52541 | 2008-07-11 13:57:08 +0000 | [diff] [blame] | 95 | { |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 96 | return *mask; |
Denis Vlasenko | 0e52541 | 2008-07-11 13:57:08 +0000 | [diff] [blame] | 97 | } |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 98 | #endif |
| 99 | |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 100 | static unsigned long *get_aff(int pid, unsigned *sz) |
| 101 | { |
| 102 | int r; |
| 103 | unsigned long *mask = NULL; |
| 104 | unsigned sz_in_bytes = *sz; |
| 105 | |
| 106 | for (;;) { |
| 107 | mask = xrealloc(mask, sz_in_bytes); |
| 108 | r = sched_getaffinity(pid, sz_in_bytes, (void*)mask); |
| 109 | if (r == 0) |
| 110 | break; |
| 111 | sz_in_bytes *= 2; |
| 112 | if (errno == EINVAL && (int)sz_in_bytes > 0) |
| 113 | continue; |
| 114 | bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid); |
| 115 | } |
| 116 | //bb_error_msg("get mask[0]:%lx sz_in_bytes:%d", mask[0], sz_in_bytes); |
| 117 | *sz = sz_in_bytes; |
| 118 | return mask; |
| 119 | } |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 120 | |
Denys Vlasenko | 162ac7f | 2019-11-01 15:44:49 +0100 | [diff] [blame] | 121 | #if ENABLE_FEATURE_TASKSET_CPULIST |
| 122 | /* |
| 123 | * Parse the CPU list and set the mask accordingly. |
| 124 | * |
| 125 | * 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] | 126 | * Example: "1,3,5-7". Stride can be specified: "0-7:2" is "0,2,4,6". |
| 127 | * Note: leading and trailing whitespace is not allowed. |
Denys Vlasenko | e558660 | 2019-11-08 16:32:37 +0100 | [diff] [blame] | 128 | * util-linux 2.31 allows leading and sometimes trailing whitespace: |
| 129 | * ok: taskset -c ' 1, 2' |
| 130 | * ok: taskset -c ' 1 , 2' |
| 131 | * ok: taskset -c ' 1-7: 2 ,8' |
| 132 | * not ok: taskset -c ' 1 ' |
| 133 | * not ok: taskset -c ' 1-7: 2 ' |
Denys Vlasenko | 162ac7f | 2019-11-01 15:44:49 +0100 | [diff] [blame] | 134 | */ |
| 135 | static void parse_cpulist(ul *mask, unsigned max, char *s) |
| 136 | { |
| 137 | char *aff = s; |
| 138 | for (;;) { |
| 139 | unsigned bit, end; |
Denys Vlasenko | a82fb1b | 2019-11-09 17:05:14 +0100 | [diff] [blame] | 140 | unsigned stride = 1; |
Denys Vlasenko | 162ac7f | 2019-11-01 15:44:49 +0100 | [diff] [blame] | 141 | |
| 142 | bit = end = bb_strtou(s, &s, 10); |
| 143 | if (*s == '-') { |
| 144 | s++; |
| 145 | end = bb_strtou(s, &s, 10); |
Denys Vlasenko | a82fb1b | 2019-11-09 17:05:14 +0100 | [diff] [blame] | 146 | if (*s == ':') { |
| 147 | s++; |
| 148 | stride = bb_strtou(s, &s, 10); |
| 149 | } |
Denys Vlasenko | 162ac7f | 2019-11-01 15:44:49 +0100 | [diff] [blame] | 150 | } |
| 151 | if ((*s != ',' && *s != '\0') |
| 152 | || bit > end |
| 153 | || end == UINT_MAX /* bb_strtou returns this on malformed / ERANGE numbers */ |
Denys Vlasenko | b230fdf | 2019-11-09 17:32:43 +0100 | [diff] [blame] | 154 | || (stride - 1) > (UINT_MAX / 4) |
| 155 | /* disallow 0, malformed input, and too large stride prone to overflows */ |
Denys Vlasenko | 162ac7f | 2019-11-01 15:44:49 +0100 | [diff] [blame] | 156 | ) { |
| 157 | bb_error_msg_and_die("bad affinity '%s'", aff); |
| 158 | } |
| 159 | while (bit <= end && bit < max) { |
| 160 | mask[bit / BITS_UL] |= (1UL << (bit & MASK_UL)); |
Denys Vlasenko | a82fb1b | 2019-11-09 17:05:14 +0100 | [diff] [blame] | 161 | bit += stride; |
Denys Vlasenko | 162ac7f | 2019-11-01 15:44:49 +0100 | [diff] [blame] | 162 | } |
| 163 | if (*s == '\0') |
| 164 | break; |
| 165 | s++; |
| 166 | } |
| 167 | } |
| 168 | static void print_cpulist(const ul *mask, unsigned mask_size_in_bytes) |
| 169 | { |
| 170 | const ul *mask_end; |
| 171 | const char *delim; |
| 172 | unsigned pos; |
| 173 | ul bit; |
| 174 | |
| 175 | mask_end = mask + mask_size_in_bytes / sizeof(mask[0]); |
| 176 | delim = ""; |
| 177 | pos = 0; |
| 178 | bit = 1; |
| 179 | for (;;) { |
| 180 | if (*mask & bit) { |
| 181 | unsigned onebit = pos + 1; |
| 182 | printf("%s%u", delim, pos); |
| 183 | do { |
| 184 | pos++; |
| 185 | bit <<= 1; |
| 186 | if (bit == 0) { |
| 187 | mask++; |
| 188 | if (mask >= mask_end) |
| 189 | break; |
| 190 | bit = 1; |
| 191 | } |
| 192 | } while (*mask & bit); |
| 193 | if (onebit != pos) |
| 194 | printf("-%u", pos - 1); |
| 195 | delim = ","; |
| 196 | } |
| 197 | pos++; |
| 198 | bit <<= 1; |
| 199 | if (bit == 0) { |
| 200 | mask++; |
| 201 | if (mask >= mask_end) |
| 202 | break; |
| 203 | bit = 1; |
| 204 | } |
| 205 | } |
| 206 | bb_putchar('\n'); |
| 207 | } |
| 208 | #endif |
| 209 | |
Denys Vlasenko | 2512817 | 2021-06-19 17:42:35 +0200 | [diff] [blame] | 210 | enum { |
| 211 | OPT_p = 1 << 0, |
| 212 | OPT_a = 1 << 1, |
| 213 | OPT_c = (1 << 2) * ENABLE_FEATURE_TASKSET_CPULIST, |
| 214 | }; |
| 215 | |
| 216 | static void process_pid_str(const char *pid_str, unsigned opts, char *aff) |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 217 | { |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 218 | ul *mask; |
| 219 | unsigned mask_size_in_bytes; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 220 | const char *current_new; |
Denys Vlasenko | 2512817 | 2021-06-19 17:42:35 +0200 | [diff] [blame] | 221 | pid_t pid = xatoi_positive(pid_str); |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 222 | |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 223 | mask_size_in_bytes = SZOF_UL; |
Denys Vlasenko | 8666391 | 2017-01-29 18:59:38 +0100 | [diff] [blame] | 224 | current_new = "current"; |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 225 | print_aff: |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 226 | mask = get_aff(pid, &mask_size_in_bytes); |
Denys Vlasenko | 162ac7f | 2019-11-01 15:44:49 +0100 | [diff] [blame] | 227 | if (opts & OPT_p) { |
| 228 | #if ENABLE_FEATURE_TASKSET_CPULIST |
| 229 | if (opts & OPT_c) { |
| 230 | printf("pid %d's %s affinity list: ", pid, current_new); |
| 231 | print_cpulist(mask, mask_size_in_bytes); |
| 232 | } else |
| 233 | #endif |
| 234 | printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n", |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 235 | pid, current_new, from_mask(mask, mask_size_in_bytes)); |
Denys Vlasenko | 2512817 | 2021-06-19 17:42:35 +0200 | [diff] [blame] | 236 | if (!aff) { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 237 | /* Either it was just "-p <pid>", |
| 238 | * or it was "-p <aff> <pid>" and we came here |
| 239 | * for the second time (see goto below) */ |
Denys Vlasenko | 2512817 | 2021-06-19 17:42:35 +0200 | [diff] [blame] | 240 | return; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 241 | } |
Denys Vlasenko | 8666391 | 2017-01-29 18:59:38 +0100 | [diff] [blame] | 242 | current_new = "new"; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 243 | } |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 244 | memset(mask, 0, mask_size_in_bytes); |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 245 | |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 246 | if (!ENABLE_FEATURE_TASKSET_FANCY) { |
Denys Vlasenko | 162ac7f | 2019-11-01 15:44:49 +0100 | [diff] [blame] | 247 | /* Affinity was specified, translate it into mask */ |
| 248 | /* it is always in hex, skip "0x" if it exists */ |
| 249 | if (aff[0] == '0' && (aff[1]|0x20) == 'x') |
| 250 | aff += 2; |
Denys Vlasenko | d6ace66 | 2017-01-30 22:20:06 +0100 | [diff] [blame] | 251 | mask[0] = xstrtoul(aff, 16); |
Denys Vlasenko | 162ac7f | 2019-11-01 15:44:49 +0100 | [diff] [blame] | 252 | } |
| 253 | #if ENABLE_FEATURE_TASKSET_CPULIST |
| 254 | else if (opts & OPT_c) { |
| 255 | parse_cpulist(mask, mask_size_in_bytes * 8, aff); |
| 256 | } |
| 257 | #endif |
| 258 | else { |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 259 | unsigned i; |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 260 | char *last_char; |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 261 | |
Denys Vlasenko | 162ac7f | 2019-11-01 15:44:49 +0100 | [diff] [blame] | 262 | /* Affinity was specified, translate it into mask */ |
| 263 | /* it is always in hex, skip "0x" if it exists */ |
| 264 | if (aff[0] == '0' && (aff[1]|0x20) == 'x') |
| 265 | aff += 2; |
| 266 | |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 267 | i = 0; /* bit pos in mask[] */ |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 268 | |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 269 | /* aff is ASCII hex string, accept very long masks in this form. |
| 270 | * Process hex string AABBCCDD... to ulong mask[] |
| 271 | * from the rightmost nibble, which is least-significant. |
| 272 | * Bits not fitting into mask[] are ignored: (example: 1234 |
| 273 | * in 12340000000000000000000000000000000000000ff) |
| 274 | */ |
| 275 | last_char = strchrnul(aff, '\0'); |
| 276 | while (last_char > aff) { |
| 277 | char c; |
| 278 | ul val; |
| 279 | |
| 280 | last_char--; |
| 281 | c = *last_char; |
| 282 | if (isdigit(c)) |
| 283 | val = c - '0'; |
| 284 | else if ((c|0x20) >= 'a' && (c|0x20) <= 'f') |
| 285 | val = (c|0x20) - ('a' - 10); |
| 286 | else |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 287 | bb_error_msg_and_die("bad affinity '%s'", aff); |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 288 | |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 289 | if (i < mask_size_in_bytes * 8) { |
| 290 | mask[i / BITS_UL] |= val << (i & MASK_UL); |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 291 | //bb_error_msg("bit %d set", i); |
| 292 | } |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 293 | /* else: |
| 294 | * We can error out here, but we don't. |
| 295 | * For one, kernel itself ignores bits in mask[] |
Denys Vlasenko | d6ace66 | 2017-01-30 22:20:06 +0100 | [diff] [blame] | 296 | * which do not map to any CPUs: |
| 297 | * if mask[] has one 32-bit long element, |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 298 | * but you have only 8 CPUs, all bits beyond first 8 |
| 299 | * are ignored, silently. |
| 300 | * No point in making bits past 31th to be errors. |
| 301 | */ |
| 302 | i += 4; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 303 | } |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 304 | } |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 305 | |
| 306 | /* Set pid's or our own (pid==0) affinity */ |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 307 | if (sched_setaffinity(pid, mask_size_in_bytes, (void*)mask)) |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 308 | 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] | 309 | //bb_error_msg("set mask[0]:%lx", mask[0]); |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 310 | |
Denys Vlasenko | 2512817 | 2021-06-19 17:42:35 +0200 | [diff] [blame] | 311 | if ((opts & OPT_p) && aff) { /* "-p <aff> <pid> [...ignored...]" */ |
| 312 | aff = NULL; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 313 | goto print_aff; /* print new affinity and exit */ |
Denys Vlasenko | 2512817 | 2021-06-19 17:42:35 +0200 | [diff] [blame] | 314 | } |
| 315 | } |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 316 | |
Denys Vlasenko | 2512817 | 2021-06-19 17:42:35 +0200 | [diff] [blame] | 317 | int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 318 | int taskset_main(int argc UNUSED_PARAM, char **argv) |
| 319 | { |
| 320 | const char *pid_str; |
| 321 | char *aff; |
| 322 | unsigned opts; |
| 323 | |
| 324 | /* NB: we mimic util-linux's taskset: -p does not take |
| 325 | * an argument, i.e., "-pN" is NOT valid, only "-p N"! |
| 326 | * Indeed, util-linux-2.13-pre7 uses: |
| 327 | * getopt_long(argc, argv, "+pchV", ...), not "...p:..." */ |
| 328 | |
| 329 | opts = getopt32(argv, "^+" "pa"IF_FEATURE_TASKSET_CPULIST("c") |
| 330 | "\0" "-1" /* at least 1 arg */); |
| 331 | argv += optind; |
| 332 | |
| 333 | aff = *argv++; |
| 334 | if (!(opts & OPT_p)) { |
| 335 | /* <aff> <cmd...> */ |
| 336 | if (!*argv) |
| 337 | bb_show_usage(); |
| 338 | process_pid_str("0", opts, aff); |
| 339 | BB_EXECVP_or_die(argv); |
| 340 | } |
| 341 | |
| 342 | pid_str = aff; |
| 343 | if (*argv) /* "-p <aff> <pid> ...rest.is.ignored..." */ |
| 344 | pid_str = *argv; /* NB: *argv != NULL in this case */ |
| 345 | else |
| 346 | aff = NULL; |
| 347 | |
| 348 | if (opts & OPT_a) { |
Denys Vlasenko | 9c291f2 | 2021-06-20 09:02:03 +0200 | [diff] [blame^] | 349 | char *dn; |
Denys Vlasenko | 2512817 | 2021-06-19 17:42:35 +0200 | [diff] [blame] | 350 | DIR *dir; |
| 351 | struct dirent *ent; |
| 352 | |
Denys Vlasenko | 9c291f2 | 2021-06-20 09:02:03 +0200 | [diff] [blame^] | 353 | dn = xasprintf("/proc/%s/task", pid_str); |
Denys Vlasenko | 2512817 | 2021-06-19 17:42:35 +0200 | [diff] [blame] | 354 | dir = opendir(dn); |
Denys Vlasenko | 9c291f2 | 2021-06-20 09:02:03 +0200 | [diff] [blame^] | 355 | IF_FEATURE_CLEAN_UP(free(dn);) |
Denys Vlasenko | 2512817 | 2021-06-19 17:42:35 +0200 | [diff] [blame] | 356 | if (!dir) { |
| 357 | goto no_threads; |
| 358 | } |
| 359 | while ((ent = readdir(dir)) != NULL) { |
| 360 | if (isdigit(ent->d_name[0])) |
| 361 | process_pid_str(ent->d_name, opts, aff); |
| 362 | } |
| 363 | IF_FEATURE_CLEAN_UP(closedir(dir);) |
| 364 | } else { |
| 365 | no_threads: |
| 366 | process_pid_str(pid_str, opts, aff); |
| 367 | } |
| 368 | return EXIT_SUCCESS; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 369 | } |