blob: b542f8c83ac7c123850479bcd073a4b64fe53109 [file] [log] [blame]
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +00001/* vi: set sw=4 ts=4: */
2/*
3 * taskset - retrieve or set a processes' CPU affinity
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00004 * Copyright (c) 2006 Bernhard Reutner-Fischer
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +00005 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +00007 */
Denys Vlasenko962c4e82014-08-17 19:36:22 +02008//config:config TASKSET
Denys Vlasenkob097a842018-12-28 03:20:17 +01009//config: bool "taskset (4.2 kb)"
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +010010//config: default y
Denys Vlasenko962c4e82014-08-17 19:36:22 +020011//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020012//config: Retrieve or set a processes's CPU affinity.
13//config: This requires sched_{g,s}etaffinity support in your libc.
Denys Vlasenko962c4e82014-08-17 19:36:22 +020014//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 Vlasenko72089cf2017-07-21 09:50:55 +020020//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 Vlasenko162ac7f2019-11-01 15:44:49 +010023//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 Vlasenko962c4e82014-08-17 19:36:22 +020031
Denys Vlasenko5c527dc2017-08-04 19:55:01 +020032//applet:IF_TASKSET(APPLET_NOEXEC(taskset, taskset, BB_DIR_USR_BIN, BB_SUID_DROP, taskset))
Denys Vlasenko0c4dbd42017-09-18 16:28:43 +020033
Denys Vlasenko962c4e82014-08-17 19:36:22 +020034//kbuild:lib-$(CONFIG_TASKSET) += taskset.o
35
Pere Orga5bc8c002011-04-11 03:29:49 +020036//usage:#define taskset_trivial_usage
Denys Vlasenkod6ace662017-01-30 22:20:06 +010037//usage: "[-p] [HEXMASK] PID | PROG ARGS"
Pere Orga5bc8c002011-04-11 03:29:49 +020038//usage:#define taskset_full_usage "\n\n"
39//usage: "Set or get CPU affinity\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020040//usage: "\n -p Operate on an existing PID"
41//usage:
42//usage:#define taskset_example_usage
43//usage: "$ taskset 0x7 ./dgemm_test&\n"
44//usage: "$ taskset -p 0x1 $!\n"
45//usage: "pid 4790's current affinity mask: 7\n"
46//usage: "pid 4790's new affinity mask: 1\n"
47//usage: "$ taskset 0x7 /bin/sh -c './taskset -p 0x1 $$'\n"
48//usage: "pid 6671's current affinity mask: 1\n"
49//usage: "pid 6671's new affinity mask: 1\n"
50//usage: "$ taskset -p 1\n"
51//usage: "pid 1's current affinity mask: 3\n"
Denys Vlasenko962c4e82014-08-17 19:36:22 +020052/*
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +010053 * Not yet implemented:
Denys Vlasenko962c4e82014-08-17 19:36:22 +020054 * -a/--all-tasks (affect all threads)
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +010055 * needs to get TIDs from /proc/PID/task/ and use _them_ as "pid" in sched_setaffinity(pid)
Denys Vlasenko962c4e82014-08-17 19:36:22 +020056 * -c/--cpu-list (specify CPUs via "1,3,5-7")
57 */
Pere Orga5bc8c002011-04-11 03:29:49 +020058
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000059#include <sched.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000060#include "libbb.h"
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000061
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +010062typedef unsigned long ul;
63#define SZOF_UL (unsigned)(sizeof(ul))
64#define BITS_UL (unsigned)(sizeof(ul)*8)
65#define MASK_UL (unsigned)(sizeof(ul)*8 - 1)
66
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000067#if ENABLE_FEATURE_TASKSET_FANCY
68#define TASKSET_PRINTF_MASK "%s"
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000069/* craft a string from the mask */
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +010070static char *from_mask(const ul *mask, unsigned sz_in_bytes)
Denis Vlasenko3bba5452006-12-30 17:57:03 +000071{
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +010072 char *str = xzalloc((sz_in_bytes+1) * 2); /* we will leak it */
73 char *p = str;
74 for (;;) {
75 ul v = *mask++;
76 if (SZOF_UL == 4)
77 p += sprintf(p, "%08lx", v);
78 if (SZOF_UL == 8)
79 p += sprintf(p, "%016lx", v);
80 if (SZOF_UL == 16)
81 p += sprintf(p, "%032lx", v); /* :) */
82 sz_in_bytes -= SZOF_UL;
83 if ((int)sz_in_bytes <= 0)
84 break;
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000085 }
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +010086 while (str[0] == '0' && str[1])
87 str++;
88 return str;
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000089}
90#else
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +010091#define TASKSET_PRINTF_MASK "%lx"
92static unsigned long long from_mask(ul *mask, unsigned sz_in_bytes UNUSED_PARAM)
Denis Vlasenko0e525412008-07-11 13:57:08 +000093{
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +010094 return *mask;
Denis Vlasenko0e525412008-07-11 13:57:08 +000095}
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000096#endif
97
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +010098static unsigned long *get_aff(int pid, unsigned *sz)
99{
100 int r;
101 unsigned long *mask = NULL;
102 unsigned sz_in_bytes = *sz;
103
104 for (;;) {
105 mask = xrealloc(mask, sz_in_bytes);
106 r = sched_getaffinity(pid, sz_in_bytes, (void*)mask);
107 if (r == 0)
108 break;
109 sz_in_bytes *= 2;
110 if (errno == EINVAL && (int)sz_in_bytes > 0)
111 continue;
112 bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid);
113 }
114 //bb_error_msg("get mask[0]:%lx sz_in_bytes:%d", mask[0], sz_in_bytes);
115 *sz = sz_in_bytes;
116 return mask;
117}
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000118
Denys Vlasenko162ac7f2019-11-01 15:44:49 +0100119#if ENABLE_FEATURE_TASKSET_CPULIST
120/*
121 * Parse the CPU list and set the mask accordingly.
122 *
123 * The list element can be either a CPU index or a range of CPU indices.
Denys Vlasenkoa82fb1b2019-11-09 17:05:14 +0100124 * Example: "1,3,5-7". Stride can be specified: "0-7:2" is "0,2,4,6".
125 * Note: leading and trailing whitespace is not allowed.
Denys Vlasenkoe5586602019-11-08 16:32:37 +0100126 * util-linux 2.31 allows leading and sometimes trailing whitespace:
127 * ok: taskset -c ' 1, 2'
128 * ok: taskset -c ' 1 , 2'
129 * ok: taskset -c ' 1-7: 2 ,8'
130 * not ok: taskset -c ' 1 '
131 * not ok: taskset -c ' 1-7: 2 '
Denys Vlasenko162ac7f2019-11-01 15:44:49 +0100132 */
133static void parse_cpulist(ul *mask, unsigned max, char *s)
134{
135 char *aff = s;
136 for (;;) {
137 unsigned bit, end;
Denys Vlasenkoa82fb1b2019-11-09 17:05:14 +0100138 unsigned stride = 1;
Denys Vlasenko162ac7f2019-11-01 15:44:49 +0100139
140 bit = end = bb_strtou(s, &s, 10);
141 if (*s == '-') {
142 s++;
143 end = bb_strtou(s, &s, 10);
Denys Vlasenkoa82fb1b2019-11-09 17:05:14 +0100144 if (*s == ':') {
145 s++;
146 stride = bb_strtou(s, &s, 10);
147 }
Denys Vlasenko162ac7f2019-11-01 15:44:49 +0100148 }
149 if ((*s != ',' && *s != '\0')
150 || bit > end
151 || end == UINT_MAX /* bb_strtou returns this on malformed / ERANGE numbers */
Denys Vlasenkob230fdf2019-11-09 17:32:43 +0100152 || (stride - 1) > (UINT_MAX / 4)
153 /* disallow 0, malformed input, and too large stride prone to overflows */
Denys Vlasenko162ac7f2019-11-01 15:44:49 +0100154 ) {
155 bb_error_msg_and_die("bad affinity '%s'", aff);
156 }
157 while (bit <= end && bit < max) {
158 mask[bit / BITS_UL] |= (1UL << (bit & MASK_UL));
Denys Vlasenkoa82fb1b2019-11-09 17:05:14 +0100159 bit += stride;
Denys Vlasenko162ac7f2019-11-01 15:44:49 +0100160 }
161 if (*s == '\0')
162 break;
163 s++;
164 }
165}
166static void print_cpulist(const ul *mask, unsigned mask_size_in_bytes)
167{
168 const ul *mask_end;
169 const char *delim;
170 unsigned pos;
171 ul bit;
172
173 mask_end = mask + mask_size_in_bytes / sizeof(mask[0]);
174 delim = "";
175 pos = 0;
176 bit = 1;
177 for (;;) {
178 if (*mask & bit) {
179 unsigned onebit = pos + 1;
180 printf("%s%u", delim, pos);
181 do {
182 pos++;
183 bit <<= 1;
184 if (bit == 0) {
185 mask++;
186 if (mask >= mask_end)
187 break;
188 bit = 1;
189 }
190 } while (*mask & bit);
191 if (onebit != pos)
192 printf("-%u", pos - 1);
193 delim = ",";
194 }
195 pos++;
196 bit <<= 1;
197 if (bit == 0) {
198 mask++;
199 if (mask >= mask_end)
200 break;
201 bit = 1;
202 }
203 }
204 bb_putchar('\n');
205}
206#endif
207
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000208int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000209int taskset_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000210{
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +0100211 ul *mask;
212 unsigned mask_size_in_bytes;
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000213 pid_t pid = 0;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000214 const char *current_new;
Denys Vlasenkod6ace662017-01-30 22:20:06 +0100215 char *aff;
Denys Vlasenko162ac7f2019-11-01 15:44:49 +0100216 unsigned opts;
217 enum {
218 OPT_p = 1 << 0,
219 OPT_c = (1 << 1) * ENABLE_FEATURE_TASKSET_CPULIST,
220 };
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000221
Denis Vlasenko45ecfc22008-03-22 17:46:16 +0000222 /* NB: we mimic util-linux's taskset: -p does not take
223 * an argument, i.e., "-pN" is NOT valid, only "-p N"!
224 * Indeed, util-linux-2.13-pre7 uses:
225 * getopt_long(argc, argv, "+pchV", ...), not "...p:..." */
226
Denys Vlasenko162ac7f2019-11-01 15:44:49 +0100227 opts = getopt32(argv, "^+" "p"IF_FEATURE_TASKSET_CPULIST("c")
228 "\0" "-1" /* at least 1 arg */);
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000229 argv += optind;
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000230
Denys Vlasenkod6ace662017-01-30 22:20:06 +0100231 aff = *argv++;
Denys Vlasenko162ac7f2019-11-01 15:44:49 +0100232 if (opts & OPT_p) {
Denys Vlasenkod6ace662017-01-30 22:20:06 +0100233 char *pid_str = aff;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000234 if (*argv) { /* "-p <aff> <pid> ...rest.is.ignored..." */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000235 pid_str = *argv; /* NB: *argv != NULL in this case */
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000236 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000237 /* else it was just "-p <pid>", and *argv == NULL */
238 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
239 } else {
Denys Vlasenkod6ace662017-01-30 22:20:06 +0100240 /* <aff> <cmd...> */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000241 if (!*argv)
242 bb_show_usage();
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000243 }
244
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +0100245 mask_size_in_bytes = SZOF_UL;
Denys Vlasenko86663912017-01-29 18:59:38 +0100246 current_new = "current";
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000247 print_aff:
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +0100248 mask = get_aff(pid, &mask_size_in_bytes);
Denys Vlasenko162ac7f2019-11-01 15:44:49 +0100249 if (opts & OPT_p) {
250#if ENABLE_FEATURE_TASKSET_CPULIST
251 if (opts & OPT_c) {
252 printf("pid %d's %s affinity list: ", pid, current_new);
253 print_cpulist(mask, mask_size_in_bytes);
254 } else
255#endif
256 printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n",
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +0100257 pid, current_new, from_mask(mask, mask_size_in_bytes));
Denys Vlasenkod6ace662017-01-30 22:20:06 +0100258 if (*argv == NULL) {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000259 /* Either it was just "-p <pid>",
260 * or it was "-p <aff> <pid>" and we came here
261 * for the second time (see goto below) */
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000262 return EXIT_SUCCESS;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000263 }
264 *argv = NULL;
Denys Vlasenko86663912017-01-29 18:59:38 +0100265 current_new = "new";
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000266 }
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +0100267 memset(mask, 0, mask_size_in_bytes);
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000268
Denys Vlasenko962c4e82014-08-17 19:36:22 +0200269 if (!ENABLE_FEATURE_TASKSET_FANCY) {
Denys Vlasenko162ac7f2019-11-01 15:44:49 +0100270 /* Affinity was specified, translate it into mask */
271 /* it is always in hex, skip "0x" if it exists */
272 if (aff[0] == '0' && (aff[1]|0x20) == 'x')
273 aff += 2;
Denys Vlasenkod6ace662017-01-30 22:20:06 +0100274 mask[0] = xstrtoul(aff, 16);
Denys Vlasenko162ac7f2019-11-01 15:44:49 +0100275 }
276#if ENABLE_FEATURE_TASKSET_CPULIST
277 else if (opts & OPT_c) {
278 parse_cpulist(mask, mask_size_in_bytes * 8, aff);
279 }
280#endif
281 else {
Denys Vlasenko962c4e82014-08-17 19:36:22 +0200282 unsigned i;
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +0100283 char *last_char;
Denys Vlasenko962c4e82014-08-17 19:36:22 +0200284
Denys Vlasenko162ac7f2019-11-01 15:44:49 +0100285 /* Affinity was specified, translate it into mask */
286 /* it is always in hex, skip "0x" if it exists */
287 if (aff[0] == '0' && (aff[1]|0x20) == 'x')
288 aff += 2;
289
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +0100290 i = 0; /* bit pos in mask[] */
Denys Vlasenko962c4e82014-08-17 19:36:22 +0200291
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +0100292 /* aff is ASCII hex string, accept very long masks in this form.
293 * Process hex string AABBCCDD... to ulong mask[]
294 * from the rightmost nibble, which is least-significant.
295 * Bits not fitting into mask[] are ignored: (example: 1234
296 * in 12340000000000000000000000000000000000000ff)
297 */
298 last_char = strchrnul(aff, '\0');
299 while (last_char > aff) {
300 char c;
301 ul val;
302
303 last_char--;
304 c = *last_char;
305 if (isdigit(c))
306 val = c - '0';
307 else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
308 val = (c|0x20) - ('a' - 10);
309 else
Denys Vlasenko962c4e82014-08-17 19:36:22 +0200310 bb_error_msg_and_die("bad affinity '%s'", aff);
Denys Vlasenko962c4e82014-08-17 19:36:22 +0200311
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +0100312 if (i < mask_size_in_bytes * 8) {
313 mask[i / BITS_UL] |= val << (i & MASK_UL);
Denys Vlasenko962c4e82014-08-17 19:36:22 +0200314 //bb_error_msg("bit %d set", i);
315 }
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +0100316 /* else:
317 * We can error out here, but we don't.
318 * For one, kernel itself ignores bits in mask[]
Denys Vlasenkod6ace662017-01-30 22:20:06 +0100319 * which do not map to any CPUs:
320 * if mask[] has one 32-bit long element,
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +0100321 * but you have only 8 CPUs, all bits beyond first 8
322 * are ignored, silently.
323 * No point in making bits past 31th to be errors.
324 */
325 i += 4;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000326 }
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000327 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000328
329 /* Set pid's or our own (pid==0) affinity */
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +0100330 if (sched_setaffinity(pid, mask_size_in_bytes, (void*)mask))
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000331 bb_perror_msg_and_die("can't %cet pid %d's affinity", 's', pid);
Denys Vlasenkoef0e76c2017-01-29 18:19:29 +0100332 //bb_error_msg("set mask[0]:%lx", mask[0]);
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000333
Denys Vlasenko41ddd9f2010-06-25 01:46:53 +0200334 if (!argv[0]) /* "-p <aff> <pid> [...ignored...]" */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000335 goto print_aff; /* print new affinity and exit */
336
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200337 BB_EXECVP_or_die(argv);
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000338}