diff options
author | Bernhard Reutner-Fischer <rep.dot.nop@gmail.com> | 2006-06-07 15:44:59 +0000 |
---|---|---|
committer | Bernhard Reutner-Fischer <rep.dot.nop@gmail.com> | 2006-06-07 15:44:59 +0000 |
commit | 16d3e4e0aff2371d0bcfd7b2c19d5bc253302f73 (patch) | |
tree | 3945b0f4e0293206ac4969a79b73112b9ce29e85 /miscutils/taskset.c | |
parent | ea93f8a397772e515fb54f6051836218112abf3f (diff) | |
download | busybox-w32-16d3e4e0aff2371d0bcfd7b2c19d5bc253302f73.tar.gz busybox-w32-16d3e4e0aff2371d0bcfd7b2c19d5bc253302f73.tar.bz2 busybox-w32-16d3e4e0aff2371d0bcfd7b2c19d5bc253302f73.zip |
- add applet taskset to set/retrieve the CPU affinity of a process
text data bss dec hex filename
584 0 0 584 248 taskset.o.gcc-2.95
509 0 0 509 1fd taskset.o.gcc-3.3
505 0 0 505 1f9 taskset.o.gcc-3.4
506 0 0 506 1fa taskset.o.gcc-4.0
498 0 0 498 1f2 taskset.o.gcc-4.1
495 0 0 495 1ef taskset.o.gcc-4.2-HEAD
Diffstat (limited to 'miscutils/taskset.c')
-rw-r--r-- | miscutils/taskset.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/miscutils/taskset.c b/miscutils/taskset.c new file mode 100644 index 000000000..a72f3ff53 --- /dev/null +++ b/miscutils/taskset.c | |||
@@ -0,0 +1,67 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * taskset - retrieve or set a processes's 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 | int taskset_main(int argc, char** argv) | ||
15 | { | ||
16 | cpu_set_t mask, new_mask; | ||
17 | pid_t pid = 0; | ||
18 | unsigned long ul; | ||
19 | const char *state = "current\0new"; | ||
20 | char *p_opt = NULL, *aff = NULL; | ||
21 | |||
22 | ul = bb_getopt_ulflags(argc, argv, "+p:", &p_opt); | ||
23 | #define TASKSET_OPT_p (1) | ||
24 | |||
25 | if (ul & TASKSET_OPT_p) { | ||
26 | if (argc == optind+1) { /* -p <aff> <pid> */ | ||
27 | aff = p_opt; | ||
28 | p_opt = argv[optind]; | ||
29 | } | ||
30 | argv += optind; /* me -p <arg> */ | ||
31 | pid = bb_xgetularg10_bnd(p_opt, 1, ULONG_MAX); /* -p <pid> */ | ||
32 | } else | ||
33 | aff = *++argv; /* <aff> <cmd...> */ | ||
34 | if (aff) { | ||
35 | /* to_cpuset(bb_xgetularg_bnd(aff, 16, 1, ULONG_MAX), &new_mask); */ | ||
36 | unsigned i = 0; | ||
37 | unsigned long l = bb_xgetularg_bnd(aff, 16, 1, ULONG_MAX); | ||
38 | |||
39 | CPU_ZERO(&new_mask); | ||
40 | while (i < CPU_SETSIZE && l >= (1<<i)) { | ||
41 | if ((1<<i) & l) | ||
42 | CPU_SET(i, &new_mask); | ||
43 | ++i; | ||
44 | } | ||
45 | } | ||
46 | |||
47 | if (ul & TASKSET_OPT_p) { | ||
48 | print_aff: | ||
49 | if (sched_getaffinity(pid, sizeof (mask), &mask) < 0) | ||
50 | bb_perror_msg_and_die("Failed to %cet pid %d's affinity", 'g', pid); | ||
51 | bb_printf("pid %d's %s affinity mask: %x\n", /* %x .. perhaps _FANCY */ | ||
52 | pid, state, mask); | ||
53 | if (!*argv) /* no new affinity given or we did print already, done. */ | ||
54 | return EXIT_SUCCESS; | ||
55 | } | ||
56 | |||
57 | if (sched_setaffinity(pid, sizeof (new_mask), &new_mask)) | ||
58 | bb_perror_msg_and_die("Failed to %cet pid %d's affinity", 's', pid); | ||
59 | if (ul & TASKSET_OPT_p) { | ||
60 | state += 8; | ||
61 | ++argv; | ||
62 | goto print_aff; | ||
63 | } | ||
64 | ++argv; | ||
65 | execvp(*argv, argv); | ||
66 | bb_perror_msg_and_die("%s", *argv); | ||
67 | } | ||