aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--util-linux/taskset.c129
1 files changed, 118 insertions, 11 deletions
diff --git a/util-linux/taskset.c b/util-linux/taskset.c
index ed8878ad4..cebc20fea 100644
--- a/util-linux/taskset.c
+++ b/util-linux/taskset.c
@@ -20,6 +20,14 @@
20//config: Needed for machines with more than 32-64 CPUs: 20//config: Needed for machines with more than 32-64 CPUs:
21//config: affinity parameter 0xHHHHHHHHHHHHHHHHHHHH can be arbitrarily long 21//config: affinity parameter 0xHHHHHHHHHHHHHHHHHHHH can be arbitrarily long
22//config: in this case. Otherwise, it is limited to sizeof(long). 22//config: in this case. Otherwise, it is limited to sizeof(long).
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'.
23 31
24//applet:IF_TASKSET(APPLET_NOEXEC(taskset, taskset, BB_DIR_USR_BIN, BB_SUID_DROP, taskset)) 32//applet:IF_TASKSET(APPLET_NOEXEC(taskset, taskset, BB_DIR_USR_BIN, BB_SUID_DROP, taskset))
25 33
@@ -108,26 +116,109 @@ static unsigned long *get_aff(int pid, unsigned *sz)
108 return mask; 116 return mask;
109} 117}
110 118
119#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.
124 * Example: "1,3,5-7".
125 *
126 * note1: pattern specifiers after a range (e.g. 0-255:2/64) are not supported
127 * note2: leading/trailing white-spaces are not allowed
128 */
129static void parse_cpulist(ul *mask, unsigned max, char *s)
130{
131 char *aff = s;
132 for (;;) {
133 unsigned bit, end;
134
135 bit = end = bb_strtou(s, &s, 10);
136 if (*s == '-') {
137 s++;
138 end = bb_strtou(s, &s, 10);
139 }
140 if ((*s != ',' && *s != '\0')
141 || bit > end
142 || end == UINT_MAX /* bb_strtou returns this on malformed / ERANGE numbers */
143 ) {
144 bb_error_msg_and_die("bad affinity '%s'", aff);
145 }
146 while (bit <= end && bit < max) {
147 mask[bit / BITS_UL] |= (1UL << (bit & MASK_UL));
148 bit++;
149 }
150 if (*s == '\0')
151 break;
152 s++;
153 }
154}
155static void print_cpulist(const ul *mask, unsigned mask_size_in_bytes)
156{
157 const ul *mask_end;
158 const char *delim;
159 unsigned pos;
160 ul bit;
161
162 mask_end = mask + mask_size_in_bytes / sizeof(mask[0]);
163 delim = "";
164 pos = 0;
165 bit = 1;
166 for (;;) {
167 if (*mask & bit) {
168 unsigned onebit = pos + 1;
169 printf("%s%u", delim, pos);
170 do {
171 pos++;
172 bit <<= 1;
173 if (bit == 0) {
174 mask++;
175 if (mask >= mask_end)
176 break;
177 bit = 1;
178 }
179 } while (*mask & bit);
180 if (onebit != pos)
181 printf("-%u", pos - 1);
182 delim = ",";
183 }
184 pos++;
185 bit <<= 1;
186 if (bit == 0) {
187 mask++;
188 if (mask >= mask_end)
189 break;
190 bit = 1;
191 }
192 }
193 bb_putchar('\n');
194}
195#endif
196
111int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 197int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
112int taskset_main(int argc UNUSED_PARAM, char **argv) 198int taskset_main(int argc UNUSED_PARAM, char **argv)
113{ 199{
114 ul *mask; 200 ul *mask;
115 unsigned mask_size_in_bytes; 201 unsigned mask_size_in_bytes;
116 pid_t pid = 0; 202 pid_t pid = 0;
117 unsigned opt_p;
118 const char *current_new; 203 const char *current_new;
119 char *aff; 204 char *aff;
205 unsigned opts;
206 enum {
207 OPT_p = 1 << 0,
208 OPT_c = (1 << 1) * ENABLE_FEATURE_TASKSET_CPULIST,
209 };
120 210
121 /* NB: we mimic util-linux's taskset: -p does not take 211 /* NB: we mimic util-linux's taskset: -p does not take
122 * an argument, i.e., "-pN" is NOT valid, only "-p N"! 212 * an argument, i.e., "-pN" is NOT valid, only "-p N"!
123 * Indeed, util-linux-2.13-pre7 uses: 213 * Indeed, util-linux-2.13-pre7 uses:
124 * getopt_long(argc, argv, "+pchV", ...), not "...p:..." */ 214 * getopt_long(argc, argv, "+pchV", ...), not "...p:..." */
125 215
126 opt_p = getopt32(argv, "^+" "p" "\0" "-1" /* at least 1 arg */); 216 opts = getopt32(argv, "^+" "p"IF_FEATURE_TASKSET_CPULIST("c")
217 "\0" "-1" /* at least 1 arg */);
127 argv += optind; 218 argv += optind;
128 219
129 aff = *argv++; 220 aff = *argv++;
130 if (opt_p) { 221 if (opts & OPT_p) {
131 char *pid_str = aff; 222 char *pid_str = aff;
132 if (*argv) { /* "-p <aff> <pid> ...rest.is.ignored..." */ 223 if (*argv) { /* "-p <aff> <pid> ...rest.is.ignored..." */
133 pid_str = *argv; /* NB: *argv != NULL in this case */ 224 pid_str = *argv; /* NB: *argv != NULL in this case */
@@ -144,8 +235,14 @@ int taskset_main(int argc UNUSED_PARAM, char **argv)
144 current_new = "current"; 235 current_new = "current";
145 print_aff: 236 print_aff:
146 mask = get_aff(pid, &mask_size_in_bytes); 237 mask = get_aff(pid, &mask_size_in_bytes);
147 if (opt_p) { 238 if (opts & OPT_p) {
148 printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n", 239#if ENABLE_FEATURE_TASKSET_CPULIST
240 if (opts & OPT_c) {
241 printf("pid %d's %s affinity list: ", pid, current_new);
242 print_cpulist(mask, mask_size_in_bytes);
243 } else
244#endif
245 printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n",
149 pid, current_new, from_mask(mask, mask_size_in_bytes)); 246 pid, current_new, from_mask(mask, mask_size_in_bytes));
150 if (*argv == NULL) { 247 if (*argv == NULL) {
151 /* Either it was just "-p <pid>", 248 /* Either it was just "-p <pid>",
@@ -158,17 +255,27 @@ int taskset_main(int argc UNUSED_PARAM, char **argv)
158 } 255 }
159 memset(mask, 0, mask_size_in_bytes); 256 memset(mask, 0, mask_size_in_bytes);
160 257
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
166 if (!ENABLE_FEATURE_TASKSET_FANCY) { 258 if (!ENABLE_FEATURE_TASKSET_FANCY) {
259 /* Affinity was specified, translate it into mask */
260 /* it is always in hex, skip "0x" if it exists */
261 if (aff[0] == '0' && (aff[1]|0x20) == 'x')
262 aff += 2;
167 mask[0] = xstrtoul(aff, 16); 263 mask[0] = xstrtoul(aff, 16);
168 } else { 264 }
265#if ENABLE_FEATURE_TASKSET_CPULIST
266 else if (opts & OPT_c) {
267 parse_cpulist(mask, mask_size_in_bytes * 8, aff);
268 }
269#endif
270 else {
169 unsigned i; 271 unsigned i;
170 char *last_char; 272 char *last_char;
171 273
274 /* Affinity was specified, translate it into mask */
275 /* it is always in hex, skip "0x" if it exists */
276 if (aff[0] == '0' && (aff[1]|0x20) == 'x')
277 aff += 2;
278
172 i = 0; /* bit pos in mask[] */ 279 i = 0; /* bit pos in mask[] */
173 280
174 /* aff is ASCII hex string, accept very long masks in this form. 281 /* aff is ASCII hex string, accept very long masks in this form.