Magellan Linux

Contents of /trunk/mkinitrd-magellan/busybox/miscutils/taskset.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 532 - (show annotations) (download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File MIME type: text/plain
File size: 2372 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd

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 <getopt.h> /* optind */
12
13 #if ENABLE_FEATURE_TASKSET_FANCY
14 #define TASKSET_PRINTF_MASK "%s"
15 #define from_cpuset(x) __from_cpuset(&x)
16 /* craft a string from the mask */
17 static char *__from_cpuset(cpu_set_t *mask)
18 {
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 /* (void*) cast is for battling gcc: */
38 /* "dereferencing type-punned pointer will break strict-aliasing rules" */
39 #define from_cpuset(mask) (*(unsigned*)(void*)&(mask))
40 #endif
41
42 #define OPT_p 1
43
44 int taskset_main(int argc, char** argv)
45 {
46 cpu_set_t mask, new_mask;
47 pid_t pid = 0;
48 unsigned opt;
49 const char *state = "current\0new";
50 char *p_opt = NULL, *aff = NULL;
51
52 opt = getopt32(argc, argv, "+p:", &p_opt);
53
54 if (opt & OPT_p) {
55 if (argc == optind+1) { /* -p <aff> <pid> */
56 aff = p_opt;
57 p_opt = argv[optind];
58 }
59 argv += optind; /* me -p <arg> */
60 pid = xatoul_range(p_opt, 1, ULONG_MAX); /* -p <pid> */
61 } else
62 aff = *++argv; /* <aff> <cmd...> */
63 if (aff) {
64 unsigned i = 0;
65 unsigned long l = xstrtol_range(aff, 16, 1, ULONG_MAX);
66
67 CPU_ZERO(&new_mask);
68 while (i < CPU_SETSIZE && l >= (1<<i)) {
69 if ((1<<i) & l)
70 CPU_SET(i, &new_mask);
71 ++i;
72 }
73 }
74
75 if (opt & OPT_p) {
76 print_aff:
77 if (sched_getaffinity(pid, sizeof(mask), &mask) < 0)
78 bb_perror_msg_and_die("failed to %cet pid %d's affinity", 'g', pid);
79 printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n",
80 pid, state, from_cpuset(mask));
81 if (!*argv) /* no new affinity given or we did print already, done. */
82 return EXIT_SUCCESS;
83 }
84
85 if (sched_setaffinity(pid, sizeof(new_mask), &new_mask))
86 bb_perror_msg_and_die("failed to %cet pid %d's affinity", 's', pid);
87 if (opt & OPT_p) {
88 state += 8;
89 ++argv;
90 goto print_aff;
91 }
92 ++argv;
93 execvp(*argv, argv);
94 bb_perror_msg_and_die("%s", *argv);
95 }
96 #undef OPT_p
97 #undef TASKSET_PRINTF_MASK
98 #undef from_cpuset