aboutsummaryrefslogtreecommitdiff
path: root/miscutils/taskset.c
diff options
context:
space:
mode:
Diffstat (limited to 'miscutils/taskset.c')
-rw-r--r--miscutils/taskset.c88
1 files changed, 49 insertions, 39 deletions
diff --git a/miscutils/taskset.c b/miscutils/taskset.c
index e64fd655b..6247aa869 100644
--- a/miscutils/taskset.c
+++ b/miscutils/taskset.c
@@ -39,61 +39,71 @@ static char *__from_cpuset(cpu_set_t *mask)
39#define from_cpuset(mask) (*(unsigned*)(void*)&(mask)) 39#define from_cpuset(mask) (*(unsigned*)(void*)&(mask))
40#endif 40#endif
41 41
42#define OPT_p 1
43 42
44int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 43int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
45int taskset_main(int argc, char **argv) 44int taskset_main(int argc ATTRIBUTE_UNUSED, char **argv)
46{ 45{
47 cpu_set_t mask, new_mask; 46 cpu_set_t mask;
48 pid_t pid = 0; 47 pid_t pid = 0;
49 unsigned opt; 48 unsigned opt_p;
50 const char *state = "current\0new"; 49 const char *current_new;
51 char *p_opt = NULL, *aff = NULL; 50 char *pid_str;
51 char *aff = aff; /* for compiler */
52 52
53 opt = getopt32(argv, "+p:", &p_opt); 53 opt_complementary = "-1"; /* at least 1 arg */
54 opt_p = getopt32(argv, "+p");
55 argv += optind;
54 56
55 if (opt & OPT_p) { 57 if (opt_p) {
56 if (argc == optind+1) { /* -p <aff> <pid> */ 58 pid_str = *argv++;
57 aff = p_opt; 59 if (*argv) { /* "-p <aff> <pid> ...rest.is.ignored..." */
58 p_opt = argv[optind]; 60 aff = pid_str;
59 } 61 pid_str = *argv; /* NB: *argv != NULL in this case */
60 argv += optind; /* me -p <arg> */
61 pid = xatoul_range(p_opt, 1, ULONG_MAX); /* -p <pid> */
62 } else
63 aff = *++argv; /* <aff> <cmd...> */
64 if (aff) {
65 unsigned i = 0;
66 unsigned long l = xstrtol_range(aff, 0, 1, LONG_MAX);
67
68 CPU_ZERO(&new_mask);
69 while (i < CPU_SETSIZE && l >= (1<<i)) {
70 if ((1<<i) & l)
71 CPU_SET(i, &new_mask);
72 ++i;
73 } 62 }
63 /* else it was just "-p <pid>", and *argv == NULL */
64 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
65 } else {
66 aff = *argv++; /* <aff> <cmd...> */
67 if (!*argv)
68 bb_show_usage();
74 } 69 }
75 70
76 if (opt & OPT_p) { 71 current_new = "current\0new";
72 if (opt_p) {
77 print_aff: 73 print_aff:
78 if (sched_getaffinity(pid, sizeof(mask), &mask) < 0) 74 if (sched_getaffinity(pid, sizeof(mask), &mask) < 0)
79 bb_perror_msg_and_die("failed to %cet pid %d's affinity", 'g', pid); 75 bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid);
80 printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n", 76 printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n",
81 pid, state, from_cpuset(mask)); 77 pid, current_new, from_cpuset(mask));
82 if (!*argv) /* no new affinity given or we did print already, done. */ 78 if (!*argv) {
79 /* Either it was just "-p <pid>",
80 * or it was "-p <aff> <pid>" and we came here
81 * for the second time (see goto below) */
83 return EXIT_SUCCESS; 82 return EXIT_SUCCESS;
83 }
84 *argv = NULL;
85 current_new += 8; /* "new" */
84 } 86 }
85 87
86 if (sched_setaffinity(pid, sizeof(new_mask), &new_mask)) 88 { /* Affinity was specified, translate it into cpu_set_t */
87 bb_perror_msg_and_die("failed to %cet pid %d's affinity", 's', pid); 89 unsigned i;
88 if (opt & OPT_p) { 90 /* Do not allow zero mask: */
89 state += 8; 91 unsigned long long m = xstrtoull_range(aff, 0, 1, ULLONG_MAX);
90 ++argv; 92 CPU_ZERO(&mask);
91 goto print_aff; 93 for (i = 0; i < CPU_SETSIZE; i++) {
94 unsigned long long bit = (1ULL << i);
95 if (bit & m)
96 CPU_SET(i, &mask);
97 }
92 } 98 }
93 ++argv; 99
100 /* Set pid's or our own (pid==0) affinity */
101 if (sched_setaffinity(pid, sizeof(mask), &mask))
102 bb_perror_msg_and_die("can't %cet pid %d's affinity", 's', pid);
103
104 if (!*argv) /* "-p <aff> <pid> [...ignored...]" */
105 goto print_aff; /* print new affinity and exit */
106
94 BB_EXECVP(*argv, argv); 107 BB_EXECVP(*argv, argv);
95 bb_simple_perror_msg_and_die(*argv); 108 bb_simple_perror_msg_and_die(*argv);
96} 109}
97#undef OPT_p
98#undef TASKSET_PRINTF_MASK
99#undef from_cpuset