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 | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 9 | //config: bool "taskset (4.1 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 | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 23 | |
Denys Vlasenko | 5c527dc | 2017-08-04 19:55:01 +0200 | [diff] [blame] | 24 | //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] | 25 | |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 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 |
Denys Vlasenko | d6ace66 | 2017-01-30 22:20:06 +0100 | [diff] [blame] | 29 | //usage: "[-p] [HEXMASK] PID | PROG ARGS" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 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 | /* |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 45 | * Not yet implemented: |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 46 | * -a/--all-tasks (affect all threads) |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 47 | * 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] | 48 | * -c/--cpu-list (specify CPUs via "1,3,5-7") |
| 49 | */ |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 50 | |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 51 | #include <sched.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 52 | #include "libbb.h" |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 53 | |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 54 | typedef unsigned long ul; |
| 55 | #define SZOF_UL (unsigned)(sizeof(ul)) |
| 56 | #define BITS_UL (unsigned)(sizeof(ul)*8) |
| 57 | #define MASK_UL (unsigned)(sizeof(ul)*8 - 1) |
| 58 | |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 59 | #if ENABLE_FEATURE_TASKSET_FANCY |
| 60 | #define TASKSET_PRINTF_MASK "%s" |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 61 | /* craft a string from the mask */ |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 62 | static char *from_mask(const ul *mask, unsigned sz_in_bytes) |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 63 | { |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 64 | char *str = xzalloc((sz_in_bytes+1) * 2); /* we will leak it */ |
| 65 | char *p = str; |
| 66 | for (;;) { |
| 67 | ul v = *mask++; |
| 68 | if (SZOF_UL == 4) |
| 69 | p += sprintf(p, "%08lx", v); |
| 70 | if (SZOF_UL == 8) |
| 71 | p += sprintf(p, "%016lx", v); |
| 72 | if (SZOF_UL == 16) |
| 73 | p += sprintf(p, "%032lx", v); /* :) */ |
| 74 | sz_in_bytes -= SZOF_UL; |
| 75 | if ((int)sz_in_bytes <= 0) |
| 76 | break; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 77 | } |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 78 | while (str[0] == '0' && str[1]) |
| 79 | str++; |
| 80 | return str; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 81 | } |
| 82 | #else |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 83 | #define TASKSET_PRINTF_MASK "%lx" |
| 84 | 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] | 85 | { |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 86 | return *mask; |
Denis Vlasenko | 0e52541 | 2008-07-11 13:57:08 +0000 | [diff] [blame] | 87 | } |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 88 | #endif |
| 89 | |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 90 | static unsigned long *get_aff(int pid, unsigned *sz) |
| 91 | { |
| 92 | int r; |
| 93 | unsigned long *mask = NULL; |
| 94 | unsigned sz_in_bytes = *sz; |
| 95 | |
| 96 | for (;;) { |
| 97 | mask = xrealloc(mask, sz_in_bytes); |
| 98 | r = sched_getaffinity(pid, sz_in_bytes, (void*)mask); |
| 99 | if (r == 0) |
| 100 | break; |
| 101 | sz_in_bytes *= 2; |
| 102 | if (errno == EINVAL && (int)sz_in_bytes > 0) |
| 103 | continue; |
| 104 | bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid); |
| 105 | } |
| 106 | //bb_error_msg("get mask[0]:%lx sz_in_bytes:%d", mask[0], sz_in_bytes); |
| 107 | *sz = sz_in_bytes; |
| 108 | return mask; |
| 109 | } |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 110 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 111 | int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 112 | int taskset_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 113 | { |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 114 | ul *mask; |
| 115 | unsigned mask_size_in_bytes; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 116 | pid_t pid = 0; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 117 | unsigned opt_p; |
| 118 | const char *current_new; |
Denys Vlasenko | d6ace66 | 2017-01-30 22:20:06 +0100 | [diff] [blame] | 119 | char *aff; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 120 | |
Denis Vlasenko | 45ecfc2 | 2008-03-22 17:46:16 +0000 | [diff] [blame] | 121 | /* NB: we mimic util-linux's taskset: -p does not take |
| 122 | * an argument, i.e., "-pN" is NOT valid, only "-p N"! |
| 123 | * Indeed, util-linux-2.13-pre7 uses: |
| 124 | * getopt_long(argc, argv, "+pchV", ...), not "...p:..." */ |
| 125 | |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 126 | opt_p = getopt32(argv, "^+" "p" "\0" "-1" /* at least 1 arg */); |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 127 | argv += optind; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 128 | |
Denys Vlasenko | d6ace66 | 2017-01-30 22:20:06 +0100 | [diff] [blame] | 129 | aff = *argv++; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 130 | if (opt_p) { |
Denys Vlasenko | d6ace66 | 2017-01-30 22:20:06 +0100 | [diff] [blame] | 131 | char *pid_str = aff; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 132 | if (*argv) { /* "-p <aff> <pid> ...rest.is.ignored..." */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 133 | pid_str = *argv; /* NB: *argv != NULL in this case */ |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 134 | } |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 135 | /* else it was just "-p <pid>", and *argv == NULL */ |
| 136 | pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1); |
| 137 | } else { |
Denys Vlasenko | d6ace66 | 2017-01-30 22:20:06 +0100 | [diff] [blame] | 138 | /* <aff> <cmd...> */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 139 | if (!*argv) |
| 140 | bb_show_usage(); |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 143 | mask_size_in_bytes = SZOF_UL; |
Denys Vlasenko | 8666391 | 2017-01-29 18:59:38 +0100 | [diff] [blame] | 144 | current_new = "current"; |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 145 | print_aff: |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 146 | mask = get_aff(pid, &mask_size_in_bytes); |
| 147 | if (opt_p) { |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 148 | printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n", |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 149 | pid, current_new, from_mask(mask, mask_size_in_bytes)); |
Denys Vlasenko | d6ace66 | 2017-01-30 22:20:06 +0100 | [diff] [blame] | 150 | if (*argv == NULL) { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 151 | /* Either it was just "-p <pid>", |
| 152 | * or it was "-p <aff> <pid>" and we came here |
| 153 | * for the second time (see goto below) */ |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 154 | return EXIT_SUCCESS; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 155 | } |
| 156 | *argv = NULL; |
Denys Vlasenko | 8666391 | 2017-01-29 18:59:38 +0100 | [diff] [blame] | 157 | current_new = "new"; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 158 | } |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 159 | memset(mask, 0, mask_size_in_bytes); |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 160 | |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 161 | /* Affinity was specified, translate it into mask */ |
| 162 | /* it is always in hex, skip "0x" if it exists */ |
| 163 | if (aff[0] == '0' && (aff[1]|0x20) == 'x') |
| 164 | aff += 2; |
| 165 | |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 166 | if (!ENABLE_FEATURE_TASKSET_FANCY) { |
Denys Vlasenko | d6ace66 | 2017-01-30 22:20:06 +0100 | [diff] [blame] | 167 | mask[0] = xstrtoul(aff, 16); |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 168 | } else { |
| 169 | unsigned i; |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 170 | char *last_char; |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 171 | |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 172 | i = 0; /* bit pos in mask[] */ |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 173 | |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 174 | /* aff is ASCII hex string, accept very long masks in this form. |
| 175 | * Process hex string AABBCCDD... to ulong mask[] |
| 176 | * from the rightmost nibble, which is least-significant. |
| 177 | * Bits not fitting into mask[] are ignored: (example: 1234 |
| 178 | * in 12340000000000000000000000000000000000000ff) |
| 179 | */ |
| 180 | last_char = strchrnul(aff, '\0'); |
| 181 | while (last_char > aff) { |
| 182 | char c; |
| 183 | ul val; |
| 184 | |
| 185 | last_char--; |
| 186 | c = *last_char; |
| 187 | if (isdigit(c)) |
| 188 | val = c - '0'; |
| 189 | else if ((c|0x20) >= 'a' && (c|0x20) <= 'f') |
| 190 | val = (c|0x20) - ('a' - 10); |
| 191 | else |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 192 | bb_error_msg_and_die("bad affinity '%s'", aff); |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 193 | |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 194 | if (i < mask_size_in_bytes * 8) { |
| 195 | mask[i / BITS_UL] |= val << (i & MASK_UL); |
Denys Vlasenko | 962c4e8 | 2014-08-17 19:36:22 +0200 | [diff] [blame] | 196 | //bb_error_msg("bit %d set", i); |
| 197 | } |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 198 | /* else: |
| 199 | * We can error out here, but we don't. |
| 200 | * For one, kernel itself ignores bits in mask[] |
Denys Vlasenko | d6ace66 | 2017-01-30 22:20:06 +0100 | [diff] [blame] | 201 | * which do not map to any CPUs: |
| 202 | * if mask[] has one 32-bit long element, |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 203 | * but you have only 8 CPUs, all bits beyond first 8 |
| 204 | * are ignored, silently. |
| 205 | * No point in making bits past 31th to be errors. |
| 206 | */ |
| 207 | i += 4; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 208 | } |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 209 | } |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 210 | |
| 211 | /* Set pid's or our own (pid==0) affinity */ |
Denys Vlasenko | ef0e76c | 2017-01-29 18:19:29 +0100 | [diff] [blame] | 212 | if (sched_setaffinity(pid, mask_size_in_bytes, (void*)mask)) |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 213 | 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] | 214 | //bb_error_msg("set mask[0]:%lx", mask[0]); |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 215 | |
Denys Vlasenko | 41ddd9f | 2010-06-25 01:46:53 +0200 | [diff] [blame] | 216 | if (!argv[0]) /* "-p <aff> <pid> [...ignored...]" */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 217 | goto print_aff; /* print new affinity and exit */ |
| 218 | |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 219 | BB_EXECVP_or_die(argv); |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 220 | } |