aboutsummaryrefslogtreecommitdiff
path: root/util-linux/taskset.c
diff options
context:
space:
mode:
Diffstat (limited to 'util-linux/taskset.c')
-rw-r--r--util-linux/taskset.c108
1 files changed, 69 insertions, 39 deletions
diff --git a/util-linux/taskset.c b/util-linux/taskset.c
index b542f8c83..afe2f04d2 100644
--- a/util-linux/taskset.c
+++ b/util-linux/taskset.c
@@ -34,10 +34,12 @@
34//kbuild:lib-$(CONFIG_TASKSET) += taskset.o 34//kbuild:lib-$(CONFIG_TASKSET) += taskset.o
35 35
36//usage:#define taskset_trivial_usage 36//usage:#define taskset_trivial_usage
37//usage: "[-p] [HEXMASK] PID | PROG ARGS" 37//usage: "[-ap] [HEXMASK"IF_FEATURE_TASKSET_CPULIST(" | -c LIST")"] { PID | PROG ARGS }"
38//usage:#define taskset_full_usage "\n\n" 38//usage:#define taskset_full_usage "\n\n"
39//usage: "Set or get CPU affinity\n" 39//usage: "Set or get CPU affinity\n"
40//usage: "\n -p Operate on an existing PID" 40//usage: "\n -p Operate on PID"
41//usage: "\n -a Operate on all threads"
42//usage: "\n -c Affinity is a list, not mask"
41//usage: 43//usage:
42//usage:#define taskset_example_usage 44//usage:#define taskset_example_usage
43//usage: "$ taskset 0x7 ./dgemm_test&\n" 45//usage: "$ taskset 0x7 ./dgemm_test&\n"
@@ -205,42 +207,18 @@ static void print_cpulist(const ul *mask, unsigned mask_size_in_bytes)
205} 207}
206#endif 208#endif
207 209
208int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 210enum {
209int taskset_main(int argc UNUSED_PARAM, char **argv) 211 OPT_p = 1 << 0,
212 OPT_a = 1 << 1,
213 OPT_c = (1 << 2) * ENABLE_FEATURE_TASKSET_CPULIST,
214};
215
216static int process_pid_str(const char *pid_str, unsigned opts, char *aff)
210{ 217{
211 ul *mask; 218 ul *mask;
212 unsigned mask_size_in_bytes; 219 unsigned mask_size_in_bytes;
213 pid_t pid = 0;
214 const char *current_new; 220 const char *current_new;
215 char *aff; 221 pid_t pid = xatoi_positive(pid_str);
216 unsigned opts;
217 enum {
218 OPT_p = 1 << 0,
219 OPT_c = (1 << 1) * ENABLE_FEATURE_TASKSET_CPULIST,
220 };
221
222 /* 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
227 opts = getopt32(argv, "^+" "p"IF_FEATURE_TASKSET_CPULIST("c")
228 "\0" "-1" /* at least 1 arg */);
229 argv += optind;
230
231 aff = *argv++;
232 if (opts & OPT_p) {
233 char *pid_str = aff;
234 if (*argv) { /* "-p <aff> <pid> ...rest.is.ignored..." */
235 pid_str = *argv; /* NB: *argv != NULL in this case */
236 }
237 /* else it was just "-p <pid>", and *argv == NULL */
238 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
239 } else {
240 /* <aff> <cmd...> */
241 if (!*argv)
242 bb_show_usage();
243 }
244 222
245 mask_size_in_bytes = SZOF_UL; 223 mask_size_in_bytes = SZOF_UL;
246 current_new = "current"; 224 current_new = "current";
@@ -255,13 +233,12 @@ int taskset_main(int argc UNUSED_PARAM, char **argv)
255#endif 233#endif
256 printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n", 234 printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n",
257 pid, current_new, from_mask(mask, mask_size_in_bytes)); 235 pid, current_new, from_mask(mask, mask_size_in_bytes));
258 if (*argv == NULL) { 236 if (!aff) {
259 /* Either it was just "-p <pid>", 237 /* Either it was just "-p <pid>",
260 * or it was "-p <aff> <pid>" and we came here 238 * or it was "-p <aff> <pid>" and we came here
261 * for the second time (see goto below) */ 239 * for the second time (see goto below) */
262 return EXIT_SUCCESS; 240 return 0;
263 } 241 }
264 *argv = NULL;
265 current_new = "new"; 242 current_new = "new";
266 } 243 }
267 memset(mask, 0, mask_size_in_bytes); 244 memset(mask, 0, mask_size_in_bytes);
@@ -331,8 +308,61 @@ int taskset_main(int argc UNUSED_PARAM, char **argv)
331 bb_perror_msg_and_die("can't %cet pid %d's affinity", 's', pid); 308 bb_perror_msg_and_die("can't %cet pid %d's affinity", 's', pid);
332 //bb_error_msg("set mask[0]:%lx", mask[0]); 309 //bb_error_msg("set mask[0]:%lx", mask[0]);
333 310
334 if (!argv[0]) /* "-p <aff> <pid> [...ignored...]" */ 311 if ((opts & OPT_p) && aff) { /* "-p <aff> <pid> [...ignored...]" */
312 aff = NULL;
335 goto print_aff; /* print new affinity and exit */ 313 goto print_aff; /* print new affinity and exit */
314 }
315 return 0;
316}
317
318static int FAST_FUNC iter(const char *dn UNUSED_PARAM, struct dirent *ent, void *aff)
319{
320 if (isdigit(ent->d_name[0]))
321 return process_pid_str(ent->d_name, option_mask32, aff);
322 return 0;
323}
324
325int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
326int taskset_main(int argc UNUSED_PARAM, char **argv)
327{
328 const char *pid_str;
329 char *aff;
330 unsigned opts;
336 331
337 BB_EXECVP_or_die(argv); 332 /* NB: we mimic util-linux's taskset: -p does not take
333 * an argument, i.e., "-pN" is NOT valid, only "-p N"!
334 * Indeed, util-linux-2.13-pre7 uses:
335 * getopt_long(argc, argv, "+pchV", ...), not "...p:..." */
336
337 opts = getopt32(argv, "^+" "pa"IF_FEATURE_TASKSET_CPULIST("c")
338 "\0" "-1" /* at least 1 arg */);
339 argv += optind;
340
341 aff = *argv++;
342 if (!(opts & OPT_p)) {
343 /* <aff> <cmd...> */
344 if (!*argv)
345 bb_show_usage();
346 process_pid_str("0", opts, aff);
347 BB_EXECVP_or_die(argv);
348 }
349
350 pid_str = aff;
351 if (*argv) /* "-p <aff> <pid> ...rest.is.ignored..." */
352 pid_str = *argv;
353 else
354 aff = NULL;
355
356 if (opts & OPT_a) {
357 char *dn;
358 int r;
359
360 dn = xasprintf("/proc/%s/task", pid_str);
361 r = iterate_on_dir(dn, iter, aff);
362 IF_FEATURE_CLEAN_UP(free(dn);)
363 if (r == 0)
364 return r; /* EXIT_SUCCESS */
365 /* else: no /proc/PID/task, act as if no -a was given */
366 }
367 return process_pid_str(pid_str, opts, aff);
338} 368}