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 | */ |
| 8 | |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 9 | //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 Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 28 | //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 Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 32 | //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 Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 44 | /* |
| 45 | Not yet implemented: |
| 46 | * -a/--all-tasks (affect all threads) |
| 47 | * -c/--cpu-list (specify CPUs via "1,3,5-7") |
| 48 | */ |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 49 | |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 50 | #include <sched.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 51 | #include "libbb.h" |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 52 | |
| 53 | #if ENABLE_FEATURE_TASKSET_FANCY |
| 54 | #define TASKSET_PRINTF_MASK "%s" |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 55 | /* craft a string from the mask */ |
Denis Vlasenko | 0e52541 | 2008-07-11 13:57:08 +0000 | [diff] [blame] | 56 | static char *from_cpuset(cpu_set_t *mask) |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 57 | { |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 58 | int i; |
Denis Vlasenko | 0e52541 | 2008-07-11 13:57:08 +0000 | [diff] [blame] | 59 | char *ret = NULL; |
| 60 | char *str = xzalloc((CPU_SETSIZE / 4) + 1); /* we will leak it */ |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 61 | |
| 62 | for (i = CPU_SETSIZE - 4; i >= 0; i -= 4) { |
Denis Vlasenko | 0e52541 | 2008-07-11 13:57:08 +0000 | [diff] [blame] | 63 | int val = 0; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 64 | int off; |
| 65 | for (off = 0; off <= 3; ++off) |
Denis Vlasenko | 0e52541 | 2008-07-11 13:57:08 +0000 | [diff] [blame] | 66 | if (CPU_ISSET(i + off, mask)) |
| 67 | val |= 1 << off; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 68 | if (!ret && val) |
| 69 | ret = str; |
Denis Vlasenko | 0e52541 | 2008-07-11 13:57:08 +0000 | [diff] [blame] | 70 | *str++ = bb_hexdigits_upcase[val] | 0x20; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 71 | } |
| 72 | return ret; |
| 73 | } |
| 74 | #else |
Denis Vlasenko | 0e52541 | 2008-07-11 13:57:08 +0000 | [diff] [blame] | 75 | #define TASKSET_PRINTF_MASK "%llx" |
| 76 | static unsigned long long from_cpuset(cpu_set_t *mask) |
| 77 | { |
Denis Vlasenko | 0e52541 | 2008-07-11 13:57:08 +0000 | [diff] [blame] | 78 | char *p = (void*)mask; |
| 79 | |
Denys Vlasenko | 7b85ec3 | 2015-10-13 17:17:34 +0200 | [diff] [blame] | 80 | BUILD_BUG_ON(CPU_SETSIZE < sizeof(int)); |
| 81 | |
Denis Vlasenko | 0e52541 | 2008-07-11 13:57:08 +0000 | [diff] [blame] | 82 | /* Take the least significant bits. Careful! |
| 83 | * Consider both CPU_SETSIZE=4 and CPU_SETSIZE=1024 cases |
| 84 | */ |
| 85 | #if BB_BIG_ENDIAN |
| 86 | /* For big endian, it means LAST bits */ |
| 87 | if (CPU_SETSIZE < sizeof(long)) |
| 88 | p += CPU_SETSIZE - sizeof(int); |
| 89 | else if (CPU_SETSIZE < sizeof(long long)) |
| 90 | p += CPU_SETSIZE - sizeof(long); |
| 91 | else |
| 92 | p += CPU_SETSIZE - sizeof(long long); |
| 93 | #endif |
| 94 | if (CPU_SETSIZE < sizeof(long)) |
| 95 | return *(unsigned*)p; |
| 96 | if (CPU_SETSIZE < sizeof(long long)) |
| 97 | return *(unsigned long*)p; |
| 98 | return *(unsigned long long*)p; |
| 99 | } |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 100 | #endif |
| 101 | |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 102 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 103 | int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 104 | int taskset_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 105 | { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 106 | cpu_set_t mask; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 107 | pid_t pid = 0; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 108 | unsigned opt_p; |
| 109 | const char *current_new; |
| 110 | char *pid_str; |
| 111 | char *aff = aff; /* for compiler */ |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 112 | |
Denis Vlasenko | 45ecfc2 | 2008-03-22 17:46:16 +0000 | [diff] [blame] | 113 | /* NB: we mimic util-linux's taskset: -p does not take |
| 114 | * an argument, i.e., "-pN" is NOT valid, only "-p N"! |
| 115 | * Indeed, util-linux-2.13-pre7 uses: |
| 116 | * getopt_long(argc, argv, "+pchV", ...), not "...p:..." */ |
| 117 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 118 | opt_complementary = "-1"; /* at least 1 arg */ |
| 119 | opt_p = getopt32(argv, "+p"); |
| 120 | argv += optind; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 121 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 122 | if (opt_p) { |
| 123 | pid_str = *argv++; |
| 124 | if (*argv) { /* "-p <aff> <pid> ...rest.is.ignored..." */ |
| 125 | aff = pid_str; |
| 126 | pid_str = *argv; /* NB: *argv != NULL in this case */ |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 127 | } |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 128 | /* else it was just "-p <pid>", and *argv == NULL */ |
| 129 | pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1); |
| 130 | } else { |
| 131 | aff = *argv++; /* <aff> <cmd...> */ |
| 132 | if (!*argv) |
| 133 | bb_show_usage(); |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 136 | current_new = "current\0new"; |
| 137 | if (opt_p) { |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 138 | print_aff: |
| 139 | if (sched_getaffinity(pid, sizeof(mask), &mask) < 0) |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 140 | bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid); |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 141 | printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n", |
Denis Vlasenko | 0e52541 | 2008-07-11 13:57:08 +0000 | [diff] [blame] | 142 | pid, current_new, from_cpuset(&mask)); |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 143 | if (!*argv) { |
| 144 | /* Either it was just "-p <pid>", |
| 145 | * or it was "-p <aff> <pid>" and we came here |
| 146 | * for the second time (see goto below) */ |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 147 | return EXIT_SUCCESS; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 148 | } |
| 149 | *argv = NULL; |
| 150 | current_new += 8; /* "new" */ |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 153 | /* Affinity was specified, translate it into cpu_set_t */ |
| 154 | CPU_ZERO(&mask); |
| 155 | if (!ENABLE_FEATURE_TASKSET_FANCY) { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 156 | unsigned i; |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 157 | unsigned long long m; |
Denis Vlasenko | 44f0821 | 2008-04-25 17:01:06 +0000 | [diff] [blame] | 158 | |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 159 | /* Do not allow zero mask: */ |
| 160 | m = xstrtoull_range(aff, 0, 1, ULLONG_MAX); |
| 161 | i = 0; |
| 162 | do { |
| 163 | if (m & 1) |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 164 | CPU_SET(i, &mask); |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 165 | i++; |
| 166 | m >>= 1; |
| 167 | } while (m != 0); |
| 168 | } else { |
| 169 | unsigned i; |
| 170 | char *last_byte; |
| 171 | char *bin; |
| 172 | uint8_t bit_in_byte; |
| 173 | |
| 174 | /* Cheap way to get "long enough" buffer */ |
| 175 | bin = xstrdup(aff); |
| 176 | |
Denys Vlasenko | f02c52b | 2014-08-31 01:35:45 +0200 | [diff] [blame] | 177 | if (aff[0] != '0' || (aff[1]|0x20) != 'x') { |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 178 | /* TODO: decimal/octal masks are still limited to 2^64 */ |
| 179 | unsigned long long m = xstrtoull_range(aff, 0, 1, ULLONG_MAX); |
| 180 | bin += strlen(bin); |
| 181 | last_byte = bin - 1; |
| 182 | while (m) { |
| 183 | *--bin = m & 0xff; |
| 184 | m >>= 8; |
| 185 | } |
| 186 | } else { |
| 187 | /* aff is "0x.....", we accept very long masks in this form */ |
| 188 | last_byte = hex2bin(bin, aff + 2, INT_MAX); |
| 189 | if (!last_byte) { |
| 190 | bad_aff: |
| 191 | bb_error_msg_and_die("bad affinity '%s'", aff); |
| 192 | } |
| 193 | last_byte--; /* now points to the last byte */ |
| 194 | } |
| 195 | |
| 196 | i = 0; |
| 197 | bit_in_byte = 1; |
| 198 | while (last_byte >= bin) { |
| 199 | if (bit_in_byte & *last_byte) { |
| 200 | if (i >= CPU_SETSIZE) |
| 201 | goto bad_aff; |
| 202 | CPU_SET(i, &mask); |
| 203 | //bb_error_msg("bit %d set", i); |
| 204 | } |
| 205 | i++; |
| 206 | /* bit_in_byte is uint8_t! & 0xff is implied */ |
| 207 | bit_in_byte = (bit_in_byte << 1); |
| 208 | if (!bit_in_byte) { |
| 209 | bit_in_byte = 1; |
| 210 | last_byte--; |
| 211 | } |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 212 | } |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 213 | } |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 214 | |
| 215 | /* Set pid's or our own (pid==0) affinity */ |
| 216 | if (sched_setaffinity(pid, sizeof(mask), &mask)) |
| 217 | bb_perror_msg_and_die("can't %cet pid %d's affinity", 's', pid); |
| 218 | |
Denys Vlasenko | 41ddd9f | 2010-06-25 01:46:53 +0200 | [diff] [blame] | 219 | if (!argv[0]) /* "-p <aff> <pid> [...ignored...]" */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 220 | goto print_aff; /* print new affinity and exit */ |
| 221 | |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 222 | BB_EXECVP_or_die(argv); |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 223 | } |