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