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