blob: fb352ab8dc5d64d8e314ea8d058182d5a59386b3 [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 */
8
Denys Vlasenko962c4e82014-08-17 19:36:22 +02009//config:config TASKSET
10//config: bool "taskset"
11//config: default n # doesn't build on some non-x86 targets (m68k)
12//config: help
13//config: Retrieve or set a processes's CPU affinity.
14//config: This requires sched_{g,s}etaffinity support in your libc.
15//config:
16//config:config FEATURE_TASKSET_FANCY
17//config: bool "Fancy output"
18//config: default y
19//config: depends on TASKSET
20//config: help
21//config: Add code for fancy output. This merely silences a compiler-warning
22//config: and adds about 135 Bytes. May be needed for machines with alot
23//config: of CPUs.
24
25//applet:IF_TASKSET(APPLET(taskset, BB_DIR_USR_BIN, BB_SUID_DROP))
26//kbuild:lib-$(CONFIG_TASKSET) += taskset.o
27
Pere Orga5bc8c002011-04-11 03:29:49 +020028//usage:#define taskset_trivial_usage
29//usage: "[-p] [MASK] [PID | PROG ARGS]"
30//usage:#define taskset_full_usage "\n\n"
31//usage: "Set or get CPU affinity\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020032//usage: "\n -p Operate on an existing PID"
33//usage:
34//usage:#define taskset_example_usage
35//usage: "$ taskset 0x7 ./dgemm_test&\n"
36//usage: "$ taskset -p 0x1 $!\n"
37//usage: "pid 4790's current affinity mask: 7\n"
38//usage: "pid 4790's new affinity mask: 1\n"
39//usage: "$ taskset 0x7 /bin/sh -c './taskset -p 0x1 $$'\n"
40//usage: "pid 6671's current affinity mask: 1\n"
41//usage: "pid 6671's new affinity mask: 1\n"
42//usage: "$ taskset -p 1\n"
43//usage: "pid 1's current affinity mask: 3\n"
Denys Vlasenko962c4e82014-08-17 19:36:22 +020044/*
45 Not yet implemented:
46 * -a/--all-tasks (affect all threads)
47 * -c/--cpu-list (specify CPUs via "1,3,5-7")
48 */
Pere Orga5bc8c002011-04-11 03:29:49 +020049
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000050#include <sched.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000051#include "libbb.h"
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000052
53#if ENABLE_FEATURE_TASKSET_FANCY
54#define TASKSET_PRINTF_MASK "%s"
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000055/* craft a string from the mask */
Denis Vlasenko0e525412008-07-11 13:57:08 +000056static char *from_cpuset(cpu_set_t *mask)
Denis Vlasenko3bba5452006-12-30 17:57:03 +000057{
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000058 int i;
Denis Vlasenko0e525412008-07-11 13:57:08 +000059 char *ret = NULL;
60 char *str = xzalloc((CPU_SETSIZE / 4) + 1); /* we will leak it */
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000061
62 for (i = CPU_SETSIZE - 4; i >= 0; i -= 4) {
Denis Vlasenko0e525412008-07-11 13:57:08 +000063 int val = 0;
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000064 int off;
65 for (off = 0; off <= 3; ++off)
Denis Vlasenko0e525412008-07-11 13:57:08 +000066 if (CPU_ISSET(i + off, mask))
67 val |= 1 << off;
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000068 if (!ret && val)
69 ret = str;
Denis Vlasenko0e525412008-07-11 13:57:08 +000070 *str++ = bb_hexdigits_upcase[val] | 0x20;
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000071 }
72 return ret;
73}
74#else
Denis Vlasenko0e525412008-07-11 13:57:08 +000075#define TASKSET_PRINTF_MASK "%llx"
76static unsigned long long from_cpuset(cpu_set_t *mask)
77{
Arnout Vandecappelle (Essensium/Mind)5fa9fef2016-02-14 19:04:09 +010078 BUILD_BUG_ON(CPU_SETSIZE < 8*sizeof(int));
Denis Vlasenko0e525412008-07-11 13:57:08 +000079
Arnout Vandecappelle (Essensium/Mind)5fa9fef2016-02-14 19:04:09 +010080 /* Take the least significant bits. Assume cpu_set_t is
81 * implemented as an array of unsigned long or unsigned
82 * int.
Denis Vlasenko0e525412008-07-11 13:57:08 +000083 */
Arnout Vandecappelle (Essensium/Mind)5fa9fef2016-02-14 19:04:09 +010084 if (CPU_SETSIZE < 8*sizeof(long))
85 return *(unsigned*)mask;
86 if (CPU_SETSIZE < 8*sizeof(long long))
87 return *(unsigned long*)mask;
88# if BB_BIG_ENDIAN
89 if (sizeof(long long) > sizeof(long)) {
90 /* We can put two long in the long long, but they have to
91 * be swapped: the least significant word comes first in the
92 * array */
93 unsigned long *p = (void*)mask;
94 return p[0] + ((unsigned long long)p[1] << (8*sizeof(long)));
95 }
96# endif
97 return *(unsigned long long*)mask;
Denis Vlasenko0e525412008-07-11 13:57:08 +000098}
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000099#endif
100
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000101
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000102int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000103int taskset_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000104{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000105 cpu_set_t mask;
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000106 pid_t pid = 0;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000107 unsigned opt_p;
108 const char *current_new;
109 char *pid_str;
110 char *aff = aff; /* for compiler */
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000111
Denis Vlasenko45ecfc22008-03-22 17:46:16 +0000112 /* NB: we mimic util-linux's taskset: -p does not take
113 * an argument, i.e., "-pN" is NOT valid, only "-p N"!
114 * Indeed, util-linux-2.13-pre7 uses:
115 * getopt_long(argc, argv, "+pchV", ...), not "...p:..." */
116
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000117 opt_complementary = "-1"; /* at least 1 arg */
118 opt_p = getopt32(argv, "+p");
119 argv += optind;
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000120
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000121 if (opt_p) {
122 pid_str = *argv++;
123 if (*argv) { /* "-p <aff> <pid> ...rest.is.ignored..." */
124 aff = pid_str;
125 pid_str = *argv; /* NB: *argv != NULL in this case */
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000126 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000127 /* else it was just "-p <pid>", and *argv == NULL */
128 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
129 } else {
130 aff = *argv++; /* <aff> <cmd...> */
131 if (!*argv)
132 bb_show_usage();
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000133 }
134
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000135 current_new = "current\0new";
136 if (opt_p) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000137 print_aff:
138 if (sched_getaffinity(pid, sizeof(mask), &mask) < 0)
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000139 bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid);
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000140 printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n",
Denis Vlasenko0e525412008-07-11 13:57:08 +0000141 pid, current_new, from_cpuset(&mask));
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000142 if (!*argv) {
143 /* Either it was just "-p <pid>",
144 * or it was "-p <aff> <pid>" and we came here
145 * for the second time (see goto below) */
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000146 return EXIT_SUCCESS;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000147 }
148 *argv = NULL;
149 current_new += 8; /* "new" */
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000150 }
151
Denys Vlasenko962c4e82014-08-17 19:36:22 +0200152 /* Affinity was specified, translate it into cpu_set_t */
153 CPU_ZERO(&mask);
154 if (!ENABLE_FEATURE_TASKSET_FANCY) {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000155 unsigned i;
Denys Vlasenko962c4e82014-08-17 19:36:22 +0200156 unsigned long long m;
Denis Vlasenko44f08212008-04-25 17:01:06 +0000157
Denys Vlasenko962c4e82014-08-17 19:36:22 +0200158 /* Do not allow zero mask: */
159 m = xstrtoull_range(aff, 0, 1, ULLONG_MAX);
160 i = 0;
161 do {
162 if (m & 1)
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000163 CPU_SET(i, &mask);
Denys Vlasenko962c4e82014-08-17 19:36:22 +0200164 i++;
165 m >>= 1;
166 } while (m != 0);
167 } else {
168 unsigned i;
169 char *last_byte;
170 char *bin;
171 uint8_t bit_in_byte;
172
173 /* Cheap way to get "long enough" buffer */
174 bin = xstrdup(aff);
175
Denys Vlasenkof02c52b2014-08-31 01:35:45 +0200176 if (aff[0] != '0' || (aff[1]|0x20) != 'x') {
Denys Vlasenko962c4e82014-08-17 19:36:22 +0200177/* TODO: decimal/octal masks are still limited to 2^64 */
178 unsigned long long m = xstrtoull_range(aff, 0, 1, ULLONG_MAX);
179 bin += strlen(bin);
180 last_byte = bin - 1;
181 while (m) {
182 *--bin = m & 0xff;
183 m >>= 8;
184 }
185 } else {
186 /* aff is "0x.....", we accept very long masks in this form */
187 last_byte = hex2bin(bin, aff + 2, INT_MAX);
188 if (!last_byte) {
189 bad_aff:
190 bb_error_msg_and_die("bad affinity '%s'", aff);
191 }
192 last_byte--; /* now points to the last byte */
193 }
194
195 i = 0;
196 bit_in_byte = 1;
197 while (last_byte >= bin) {
198 if (bit_in_byte & *last_byte) {
199 if (i >= CPU_SETSIZE)
200 goto bad_aff;
201 CPU_SET(i, &mask);
202 //bb_error_msg("bit %d set", i);
203 }
204 i++;
205 /* bit_in_byte is uint8_t! & 0xff is implied */
206 bit_in_byte = (bit_in_byte << 1);
207 if (!bit_in_byte) {
208 bit_in_byte = 1;
209 last_byte--;
210 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000211 }
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000212 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000213
214 /* Set pid's or our own (pid==0) affinity */
215 if (sched_setaffinity(pid, sizeof(mask), &mask))
216 bb_perror_msg_and_die("can't %cet pid %d's affinity", 's', pid);
217
Denys Vlasenko41ddd9f2010-06-25 01:46:53 +0200218 if (!argv[0]) /* "-p <aff> <pid> [...ignored...]" */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000219 goto print_aff; /* print new affinity and exit */
220
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200221 BB_EXECVP_or_die(argv);
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000222}