]>
Commit | Line | Data |
---|---|---|
48d7b13a | 1 | /* |
de878776 KZ |
2 | * taskset.c - command-line utility for setting and retrieving |
3 | * a task's CPU affinity | |
48d7b13a KZ |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License, v2, as | |
7 | * published by the Free Software Foundation | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
17 | * | |
18 | * Copyright (C) 2004 Robert Love | |
de878776 | 19 | * Copyright (C) 2010 Karel Zak <kzak@redhat.com> |
48d7b13a KZ |
20 | */ |
21 | ||
48d7b13a KZ |
22 | #include <stdio.h> |
23 | #include <stdlib.h> | |
24 | #include <unistd.h> | |
25 | #include <getopt.h> | |
48d7b13a | 26 | #include <errno.h> |
6f45c0e9 SK |
27 | #include <sched.h> |
28 | #include <stddef.h> | |
29 | #include <string.h> | |
f7e3dc05 | 30 | |
efcb71f8 | 31 | #include "cpuset.h" |
de878776 | 32 | #include "nls.h" |
8abcf290 | 33 | #include "strutils.h" |
dd29a762 | 34 | #include "xalloc.h" |
42708f12 | 35 | #include "procutils.h" |
eb76ca98 | 36 | #include "c.h" |
24295096 | 37 | |
42708f12 DB |
38 | struct taskset { |
39 | pid_t pid; /* task PID */ | |
42708f12 DB |
40 | cpu_set_t *set; /* task CPU mask */ |
41 | size_t setsize; | |
42708f12 DB |
42 | char *buf; /* buffer for conversion from mask to string */ |
43 | size_t buflen; | |
42708f12 DB |
44 | int use_list:1, /* use list rather than masks */ |
45 | get_only:1; /* print the mask, but not modify */ | |
46 | }; | |
47 | ||
de878776 | 48 | static void __attribute__((__noreturn__)) usage(FILE *out) |
48d7b13a | 49 | { |
de878776 KZ |
50 | fprintf(out, |
51 | _("Usage: %s [options] [mask | cpu-list] [pid|cmd [args...]]\n\n"), | |
52 | program_invocation_short_name); | |
53 | ||
54 | fprintf(out, _( | |
55 | "Options:\n" | |
42708f12 | 56 | " -a, --all-tasks operate on all the tasks (threads) for a given pid\n" |
de878776 KZ |
57 | " -p, --pid operate on existing given pid\n" |
58 | " -c, --cpu-list display and specify cpus in list format\n" | |
59 | " -h, --help display this help\n" | |
60 | " -V, --version output version information\n\n")); | |
61 | ||
62 | fprintf(out, _( | |
63 | "The default behavior is to run a new command:\n" | |
64 | " %1$s 03 sshd -b 1024\n" | |
65 | "You can retrieve the mask of an existing task:\n" | |
66 | " %1$s -p 700\n" | |
67 | "Or set it:\n" | |
68 | " %1$s -p 03 700\n" | |
69 | "List format uses a comma-separated list instead of a mask:\n" | |
70 | " %1$s -pc 0,3,7-11 700\n" | |
71 | "Ranges in list format can take a stride argument:\n" | |
72 | " e.g. 0-31:2 is equivalent to mask 0x55555555\n"), | |
73 | program_invocation_short_name); | |
74 | ||
75 | fprintf(out, _("\nFor more information see taskset(1).\n")); | |
76 | ||
77 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
48d7b13a KZ |
78 | } |
79 | ||
42708f12 DB |
80 | static void print_affinity(struct taskset *ts, int isnew) |
81 | { | |
82 | char *str, *msg; | |
83 | ||
84 | if (ts->use_list) { | |
85 | str = cpulist_create(ts->buf, ts->buflen, ts->set, ts->setsize); | |
86 | msg = isnew ? _("pid %d's new affinity list: %s\n") : | |
87 | _("pid %d's current affinity list: %s\n"); | |
88 | } else { | |
89 | str = cpumask_create(ts->buf, ts->buflen, ts->set, ts->setsize); | |
90 | msg = isnew ? _("pid %d's new affinity mask: %s\n") : | |
91 | _("pid %d's current affinity mask: %s\n"); | |
92 | } | |
93 | ||
94 | if (!str) | |
95 | /* this is internal error... */ | |
96 | errx(EXIT_FAILURE, _("conversion from cpuset to string failed")); | |
97 | ||
98 | printf(msg, ts->pid, str); | |
99 | } | |
100 | ||
101 | static void do_taskset(struct taskset *ts, size_t setsize, cpu_set_t *set) | |
102 | { | |
103 | /* read the current mask */ | |
104 | if (ts->pid) { | |
105 | if (sched_getaffinity(ts->pid, ts->setsize, ts->set) < 0) | |
106 | err(EXIT_FAILURE, _("failed to get pid %d's affinity"), | |
02ad0100 | 107 | ts->pid); |
42708f12 DB |
108 | print_affinity(ts, FALSE); |
109 | } | |
110 | ||
111 | if (ts->get_only) | |
112 | return; | |
113 | ||
114 | /* set new mask */ | |
115 | if (sched_setaffinity(ts->pid, setsize, set) < 0) | |
02ad0100 SK |
116 | err(EXIT_FAILURE, _("failed to set pid %d's affinity"), |
117 | ts->pid); | |
42708f12 DB |
118 | |
119 | /* re-read the current mask */ | |
120 | if (ts->pid) { | |
121 | if (sched_getaffinity(ts->pid, ts->setsize, ts->set) < 0) | |
122 | err(EXIT_FAILURE, _("failed to get pid %d's affinity"), | |
02ad0100 | 123 | ts->pid); |
42708f12 DB |
124 | print_affinity(ts, TRUE); |
125 | } | |
126 | } | |
127 | ||
02ad0100 | 128 | int main(int argc, char **argv) |
48d7b13a | 129 | { |
42708f12 | 130 | cpu_set_t *new_set; |
48d7b13a | 131 | pid_t pid = 0; |
42708f12 | 132 | int c, all_tasks = 0; |
02ad0100 | 133 | unsigned int ncpus; |
42708f12 DB |
134 | size_t new_setsize, nbits; |
135 | struct taskset ts; | |
48d7b13a | 136 | |
6c7d5ae9 | 137 | static const struct option longopts[] = { |
02ad0100 | 138 | { "all-tasks", 0, NULL, 'a' }, |
48d7b13a KZ |
139 | { "pid", 0, NULL, 'p' }, |
140 | { "cpu-list", 0, NULL, 'c' }, | |
141 | { "help", 0, NULL, 'h' }, | |
142 | { "version", 0, NULL, 'V' }, | |
02ad0100 | 143 | { NULL, 0, NULL, 0 } |
48d7b13a KZ |
144 | }; |
145 | ||
de878776 KZ |
146 | setlocale(LC_ALL, ""); |
147 | bindtextdomain(PACKAGE, LOCALEDIR); | |
148 | textdomain(PACKAGE); | |
48d7b13a | 149 | |
42708f12 DB |
150 | memset(&ts, 0, sizeof(ts)); |
151 | ||
152 | while ((c = getopt_long(argc, argv, "+apchV", longopts, NULL)) != -1) { | |
153 | switch (c) { | |
154 | case 'a': | |
155 | all_tasks = 1; | |
156 | break; | |
48d7b13a | 157 | case 'p': |
02ad0100 SK |
158 | pid = strtol_or_err(argv[argc - 1], |
159 | _("failed to parse pid")); | |
48d7b13a KZ |
160 | break; |
161 | case 'c': | |
42708f12 | 162 | ts.use_list = 1; |
48d7b13a KZ |
163 | break; |
164 | case 'V': | |
02ad0100 SK |
165 | printf("%s from %s\n", program_invocation_short_name, |
166 | PACKAGE_STRING); | |
de878776 | 167 | return EXIT_SUCCESS; |
48d7b13a | 168 | case 'h': |
de878776 KZ |
169 | usage(stdout); |
170 | break; | |
48d7b13a | 171 | default: |
de878776 KZ |
172 | usage(stderr); |
173 | break; | |
48d7b13a KZ |
174 | } |
175 | } | |
176 | ||
177 | if ((!pid && argc - optind < 2) | |
02ad0100 | 178 | || (pid && (argc - optind < 1 || argc - optind > 2))) |
de878776 | 179 | usage(stderr); |
48d7b13a | 180 | |
bae91ecf KZ |
181 | ncpus = get_max_number_of_cpus(); |
182 | if (ncpus <= 0) | |
183 | errx(EXIT_FAILURE, _("cannot determine NR_CPUS; aborting")); | |
f7e3dc05 CW |
184 | |
185 | /* | |
42708f12 | 186 | * the ts->set is always used for the sched_getaffinity call |
f7e3dc05 CW |
187 | * On the sched_getaffinity the kernel demands a user mask of |
188 | * at least the size of its own cpumask_t. | |
189 | */ | |
42708f12 DB |
190 | ts.set = cpuset_alloc(ncpus, &ts.setsize, &nbits); |
191 | if (!ts.set) | |
de878776 KZ |
192 | err(EXIT_FAILURE, _("cpuset_alloc failed")); |
193 | ||
42708f12 DB |
194 | /* buffer for conversion from mask to string */ |
195 | ts.buflen = 7 * nbits; | |
196 | ts.buf = xmalloc(ts.buflen); | |
f7e3dc05 CW |
197 | |
198 | /* | |
de878776 | 199 | * new_set is always used for the sched_setaffinity call |
f7e3dc05 CW |
200 | * On the sched_setaffinity the kernel will zero-fill its |
201 | * cpumask_t if the user's mask is shorter. | |
202 | */ | |
ff5a6d20 | 203 | new_set = cpuset_alloc(ncpus, &new_setsize, NULL); |
de878776 KZ |
204 | if (!new_set) |
205 | err(EXIT_FAILURE, _("cpuset_alloc failed")); | |
f7e3dc05 | 206 | |
42708f12 DB |
207 | if (argc - optind == 1) |
208 | ts.get_only = 1; | |
48d7b13a | 209 | |
42708f12 DB |
210 | else if (ts.use_list) { |
211 | if (cpulist_parse(argv[optind], new_set, new_setsize)) | |
212 | errx(EXIT_FAILURE, _("failed to parse CPU list: %s"), | |
02ad0100 | 213 | argv[optind]); |
42708f12 DB |
214 | } else if (cpumask_parse(argv[optind], new_set, new_setsize)) { |
215 | errx(EXIT_FAILURE, _("failed to parse CPU mask: %s"), | |
02ad0100 | 216 | argv[optind]); |
42708f12 | 217 | } |
48d7b13a | 218 | |
42708f12 DB |
219 | if (all_tasks) { |
220 | struct proc_tasks *tasks = proc_open_tasks(pid); | |
02ad0100 | 221 | while (!proc_next_tid(tasks, &ts.pid)) |
42708f12 DB |
222 | do_taskset(&ts, new_setsize, new_set); |
223 | proc_close_tasks(tasks); | |
02ad0100 | 224 | } else { |
42708f12 DB |
225 | ts.pid = pid; |
226 | do_taskset(&ts, new_setsize, new_set); | |
ff5a6d20 KZ |
227 | } |
228 | ||
42708f12 DB |
229 | free(ts.buf); |
230 | cpuset_free(ts.set); | |
ff5a6d20 KZ |
231 | cpuset_free(new_set); |
232 | ||
233 | if (!pid) { | |
48d7b13a KZ |
234 | argv += optind + 1; |
235 | execvp(argv[0], argv); | |
de878776 | 236 | err(EXIT_FAILURE, _("executing %s failed"), argv[0]); |
48d7b13a KZ |
237 | } |
238 | ||
de878776 | 239 | return EXIT_SUCCESS; |
48d7b13a | 240 | } |