From 689cd31101ad4d428af4a64b72fa012f1043e4d5 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 27 Jul 2022 02:12:34 -0500 Subject: [PATCH] Minor cleanups. --- toys/other/taskset.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/toys/other/taskset.c b/toys/other/taskset.c index 2a9ae029..d501cc53 100644 --- a/toys/other/taskset.c +++ b/toys/other/taskset.c @@ -34,35 +34,32 @@ config TASKSET #define FOR_taskset #include "toys.h" +// mask is array of long which makes layout a bit weird on big endian systems #define sched_setaffinity(pid, size, cpuset) \ syscall(__NR_sched_setaffinity, (pid_t)pid, (size_t)size, (void *)cpuset) #define sched_getaffinity(pid, size, cpuset) \ syscall(__NR_sched_getaffinity, (pid_t)pid, (size_t)size, (void *)cpuset) -// mask is an array of long, which makes the layout a bit weird on big -// endian systems but as long as it's consistent... - static void do_taskset(pid_t pid, int quiet) { unsigned long *mask = (unsigned long *)toybuf; char *s = *toys.optargs, *failed = "failed to %s %d's affinity"; int i, j, k; + // loop through twice to display before/after affinity masks for (i=0; ; i++) { if (!quiet) { - int j = sizeof(toybuf), flag = 0; - if (-1 == sched_getaffinity(pid, sizeof(toybuf), (void *)mask)) perror_exit(failed, "get", pid); printf("pid %d's %s affinity mask: ", pid, i ? "new" : "current"); - while (j--) { + for (j = sizeof(toybuf), k = 0; j--;) { int x = 255 & (mask[j/sizeof(long)] >> (8*(j&(sizeof(long)-1)))); - if (flag) printf("%02x", x); + if (k) printf("%02x", x); else if (x) { - flag++; + k++; printf("%x", x); } } @@ -89,26 +86,25 @@ static void do_taskset(pid_t pid, int quiet) static int task_callback(struct dirtree *new) { - if (!new->parent) return DIRTREE_RECURSE; - if (isdigit(*new->name)) do_taskset(atoi(new->name), 0); + if (!new->parent) return DIRTREE_RECURSE|DIRTREE_SHUTUP|DIRTREE_PROC; + do_taskset(atoi(new->name), 0); return 0; } void taskset_main(void) { - if (!(toys.optflags & FLAG_p)) { + if (!FLAG(p)) { if (toys.optc < 2) error_exit("Needs 2 args"); do_taskset(getpid(), 1); xexec(toys.optargs+1); } else { - char *c; + char *c, buf[33]; pid_t pid = strtol(toys.optargs[toys.optc-1], &c, 10); if (*c) error_exit("Not int %s", toys.optargs[1]); - if (toys.optflags & FLAG_a) { - char buf[33]; + if (FLAG(a)) { sprintf(buf, "/proc/%ld/task/", (long)pid); dirtree_read(buf, task_callback); } else do_taskset(pid, 0); -- 2.39.2