diff options
| author | Bernhard Reutner-Fischer <rep.dot.nop@gmail.com> | 2006-11-22 16:39:48 +0000 |
|---|---|---|
| committer | Bernhard Reutner-Fischer <rep.dot.nop@gmail.com> | 2006-11-22 16:39:48 +0000 |
| commit | 32eddffa30538e5c4c6bd6bc580557a9ade9bac3 (patch) | |
| tree | 43fe97e0f0e315d50b08582217be33581182f8f1 /miscutils | |
| parent | c8717cd8571cd35d71696aab8aa847169756bd9f (diff) | |
| download | busybox-w32-32eddffa30538e5c4c6bd6bc580557a9ade9bac3.tar.gz busybox-w32-32eddffa30538e5c4c6bd6bc580557a9ade9bac3.tar.bz2 busybox-w32-32eddffa30538e5c4c6bd6bc580557a9ade9bac3.zip | |
- revert r15563 (pull current version of taskset off the busybox_scratch branch)
Diffstat (limited to 'miscutils')
| -rw-r--r-- | miscutils/Config.in | 10 | ||||
| -rw-r--r-- | miscutils/Kbuild | 1 | ||||
| -rw-r--r-- | miscutils/taskset.c | 96 |
3 files changed, 107 insertions, 0 deletions
diff --git a/miscutils/Config.in b/miscutils/Config.in index e093924d7..90e2b6fb1 100644 --- a/miscutils/Config.in +++ b/miscutils/Config.in | |||
| @@ -340,6 +340,16 @@ config TASKSET | |||
| 340 | default n | 340 | default n |
| 341 | help | 341 | help |
| 342 | Retrieve or set a processes's CPU affinity. | 342 | Retrieve or set a processes's CPU affinity. |
| 343 | This requires sched_{g,s}etaffinity support in your libc. | ||
| 344 | |||
| 345 | config FEATURE_TASKSET_FANCY | ||
| 346 | bool "fancy output" | ||
| 347 | default y | ||
| 348 | depends on TASKSET | ||
| 349 | help | ||
| 350 | Add code for fancy output. This merely silences a compiler-warning | ||
| 351 | and adds about 135 Bytes. May be needed for machines with alot | ||
| 352 | of CPUs. | ||
| 343 | 353 | ||
| 344 | config TIME | 354 | config TIME |
| 345 | bool "time" | 355 | bool "time" |
diff --git a/miscutils/Kbuild b/miscutils/Kbuild index 16c76fa38..8d1b9f484 100644 --- a/miscutils/Kbuild +++ b/miscutils/Kbuild | |||
| @@ -25,5 +25,6 @@ lib-$(CONFIG_RUNLEVEL) += runlevel.o | |||
| 25 | lib-$(CONFIG_RX) += rx.o | 25 | lib-$(CONFIG_RX) += rx.o |
| 26 | lib-$(CONFIG_SETSID) += setsid.o | 26 | lib-$(CONFIG_SETSID) += setsid.o |
| 27 | lib-$(CONFIG_STRINGS) += strings.o | 27 | lib-$(CONFIG_STRINGS) += strings.o |
| 28 | lib-$(CONFIG_TASKSET) += taskset.o | ||
| 28 | lib-$(CONFIG_TIME) += time.o | 29 | lib-$(CONFIG_TIME) += time.o |
| 29 | lib-$(CONFIG_WATCHDOG) += watchdog.o | 30 | lib-$(CONFIG_WATCHDOG) += watchdog.o |
diff --git a/miscutils/taskset.c b/miscutils/taskset.c new file mode 100644 index 000000000..4496aa5b4 --- /dev/null +++ b/miscutils/taskset.c | |||
| @@ -0,0 +1,96 @@ | |||
| 1 | /* vi: set sw=4 ts=4: */ | ||
| 2 | /* | ||
| 3 | * taskset - retrieve or set a processes' CPU affinity | ||
| 4 | * Copyright (c) 2006 Bernhard Fischer | ||
| 5 | * | ||
| 6 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include "busybox.h" | ||
| 10 | #include <sched.h> | ||
| 11 | #include <unistd.h> | ||
| 12 | #include <getopt.h> /* optind */ | ||
| 13 | |||
| 14 | #if ENABLE_FEATURE_TASKSET_FANCY | ||
| 15 | #define TASKSET_PRINTF_MASK "%s" | ||
| 16 | #define from_cpuset(x) __from_cpuset(&x) | ||
| 17 | /* craft a string from the mask */ | ||
| 18 | static char *__from_cpuset(cpu_set_t *mask) { | ||
| 19 | int i; | ||
| 20 | char *ret = 0, *str = xzalloc(9); | ||
| 21 | |||
| 22 | for (i = CPU_SETSIZE - 4; i >= 0; i -= 4) { | ||
| 23 | char val = 0; | ||
| 24 | int off; | ||
| 25 | for (off = 0; off <= 3; ++off) | ||
| 26 | if (CPU_ISSET(i+off, mask)) | ||
| 27 | val |= 1<<off; | ||
| 28 | |||
| 29 | if (!ret && val) | ||
| 30 | ret = str; | ||
| 31 | *str++ = (val-'0'<=9) ? (val+48) : (val+87); | ||
| 32 | } | ||
| 33 | return ret; | ||
| 34 | } | ||
| 35 | #else | ||
| 36 | #define TASKSET_PRINTF_MASK "%x" | ||
| 37 | #define from_cpuset(mask) mask | ||
| 38 | #endif | ||
| 39 | |||
| 40 | #define TASKSET_OPT_p (1) | ||
| 41 | |||
| 42 | int taskset_main(int argc, char** argv) | ||
| 43 | { | ||
| 44 | cpu_set_t mask, new_mask; | ||
| 45 | pid_t pid = 0; | ||
| 46 | unsigned long ul; | ||
| 47 | const char *state = "current\0new"; | ||
| 48 | char *p_opt = NULL, *aff = NULL; | ||
| 49 | |||
| 50 | ul = getopt32(argc, argv, "+p:", &p_opt); | ||
| 51 | |||
| 52 | if (ul & TASKSET_OPT_p) { | ||
| 53 | if (argc == optind+1) { /* -p <aff> <pid> */ | ||
| 54 | aff = p_opt; | ||
| 55 | p_opt = argv[optind]; | ||
| 56 | } | ||
| 57 | argv += optind; /* me -p <arg> */ | ||
| 58 | pid = xatoul_range(p_opt, 1, ULONG_MAX); /* -p <pid> */ | ||
| 59 | } else | ||
| 60 | aff = *++argv; /* <aff> <cmd...> */ | ||
| 61 | if (aff) { | ||
| 62 | unsigned i = 0; | ||
| 63 | unsigned long l = xstrtol_range(aff, 16, 1, ULONG_MAX); | ||
| 64 | |||
| 65 | CPU_ZERO(&new_mask); | ||
| 66 | while (i < CPU_SETSIZE && l >= (1<<i)) { | ||
| 67 | if ((1<<i) & l) | ||
| 68 | CPU_SET(i, &new_mask); | ||
| 69 | ++i; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | if (ul & TASKSET_OPT_p) { | ||
| 74 | print_aff: | ||
| 75 | if (sched_getaffinity(pid, sizeof (mask), &mask) < 0) | ||
| 76 | bb_perror_msg_and_die("Failed to %cet pid %d's affinity", 'g', pid); | ||
| 77 | printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n", | ||
| 78 | pid, state, from_cpuset(mask)); | ||
| 79 | if (!*argv) /* no new affinity given or we did print already, done. */ | ||
| 80 | return EXIT_SUCCESS; | ||
| 81 | } | ||
| 82 | |||
| 83 | if (sched_setaffinity(pid, sizeof (new_mask), &new_mask)) | ||
| 84 | bb_perror_msg_and_die("Failed to %cet pid %d's affinity", 's', pid); | ||
| 85 | if (ul & TASKSET_OPT_p) { | ||
| 86 | state += 8; | ||
| 87 | ++argv; | ||
| 88 | goto print_aff; | ||
| 89 | } | ||
| 90 | ++argv; | ||
| 91 | execvp(*argv, argv); | ||
| 92 | bb_perror_msg_and_die("%s", *argv); | ||
| 93 | } | ||
| 94 | #undef TASKSET_OPT_p | ||
| 95 | #undef TASKSET_PRINTF_MASK | ||
| 96 | #undef from_cpuset | ||
