| 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 |
| 14 |
* along with this program. If not, see <https://gnu.org/licenses/>. |
| 15 |
* |
| 16 |
* Copyright (C) 2004 Robert Love |
| 17 |
* Copyright (C) 2010 Karel Zak <[email protected]> |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <stdbool.h> |
| 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 "procfs.h" |
| 35 |
#include "c.h" |
| 36 |
#include "closestream.h" |
| 37 |
|
| 38 |
#ifndef PF_NO_SETAFFINITY |
| 39 |
# define PF_NO_SETAFFINITY 0x04000000 |
| 40 |
#endif |
| 41 |
|
| 42 |
struct taskset { |
| 43 |
pid_t pid; /* task PID */ |
| 44 |
cpu_set_t *set; /* task CPU mask */ |
| 45 |
size_t setsize; |
| 46 |
char *buf; /* buffer for conversion from mask to string */ |
| 47 |
size_t buflen; |
| 48 |
bool use_list, /* use list rather than masks */ |
| 49 |
get_only; /* print the mask, but not modify */ |
| 50 |
}; |
| 51 |
|
| 52 |
static void __attribute__((__noreturn__)) usage(void) |
| 53 |
{ |
| 54 |
FILE *out = stdout; |
| 55 |
fprintf(out, |
| 56 |
_("Usage: %s [options] [mask | cpu-list] [pid|cmd [args...]]\n\n"), |
| 57 |
program_invocation_short_name); |
| 58 |
|
| 59 |
fputs(USAGE_SEPARATOR, out); |
| 60 |
fputs(_("Show or change the CPU affinity of a process.\n"), out); |
| 61 |
fputs(USAGE_SEPARATOR, out); |
| 62 |
|
| 63 |
fprintf(out, _( |
| 64 |
"Options:\n" |
| 65 |
" -a, --all-tasks operate on all the tasks (threads) for a given pid\n" |
| 66 |
" -p, --pid operate on existing given pid\n" |
| 67 |
" -c, --cpu-list display and specify cpus in list format\n" |
| 68 |
)); |
| 69 |
fprintf(out, USAGE_HELP_OPTIONS(25)); |
| 70 |
|
| 71 |
fputs(USAGE_SEPARATOR, out); |
| 72 |
fprintf(out, _( |
| 73 |
"The default behavior is to run a new command:\n" |
| 74 |
" %1$s 03 sshd -b 1024\n" |
| 75 |
"You can retrieve the mask of an existing task:\n" |
| 76 |
" %1$s -p 700\n" |
| 77 |
"Or set it:\n" |
| 78 |
" %1$s -p 03 700\n" |
| 79 |
"List format uses a comma-separated list instead of a mask:\n" |
| 80 |
" %1$s -pc 0,3,7-11 700\n" |
| 81 |
"Ranges in list format can take a stride argument:\n" |
| 82 |
" e.g. 0-31:2 is equivalent to mask 0x55555555\n"), |
| 83 |
program_invocation_short_name); |
| 84 |
|
| 85 |
fprintf(out, USAGE_MAN_TAIL("taskset(1)")); |
| 86 |
exit(EXIT_SUCCESS); |
| 87 |
} |
| 88 |
|
| 89 |
static void print_affinity(struct taskset *ts, int isnew) |
| 90 |
{ |
| 91 |
char *str, *msg; |
| 92 |
|
| 93 |
if (ts->use_list) { |
| 94 |
str = cpulist_create(ts->buf, ts->buflen, ts->set, ts->setsize); |
| 95 |
msg = isnew ? _("pid %d's new affinity list: %s\n") : |
| 96 |
_("pid %d's current affinity list: %s\n"); |
| 97 |
} else { |
| 98 |
str = cpumask_create(ts->buf, ts->buflen, ts->set, ts->setsize); |
| 99 |
msg = isnew ? _("pid %d's new affinity mask: %s\n") : |
| 100 |
_("pid %d's current affinity mask: %s\n"); |
| 101 |
} |
| 102 |
|
| 103 |
if (!str) |
| 104 |
errx(EXIT_FAILURE, _("internal error: conversion from cpuset to string failed")); |
| 105 |
|
| 106 |
printf(msg, ts->pid ? ts->pid : getpid(), str); |
| 107 |
} |
| 108 |
|
| 109 |
static void __attribute__((__noreturn__)) err_affinity(pid_t pid, int set) |
| 110 |
{ |
| 111 |
char *msg; |
| 112 |
|
| 113 |
msg = set ? _("failed to set pid %d's affinity") : |
| 114 |
_("failed to get pid %d's affinity"); |
| 115 |
|
| 116 |
err(EXIT_FAILURE, msg, pid ? pid : getpid()); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
static void do_taskset(struct taskset *ts, size_t setsize, cpu_set_t *set) |
| 121 |
{ |
| 122 |
/* read the current mask */ |
| 123 |
if (ts->pid) { |
| 124 |
if (sched_getaffinity(ts->pid, ts->setsize, ts->set) < 0) |
| 125 |
err_affinity(ts->pid, 0); |
| 126 |
print_affinity(ts, FALSE); |
| 127 |
} |
| 128 |
|
| 129 |
if (ts->get_only) |
| 130 |
return; |
| 131 |
|
| 132 |
/* set new mask */ |
| 133 |
if (sched_setaffinity(ts->pid, setsize, set) < 0) { |
| 134 |
uintmax_t flags = 0; |
| 135 |
struct path_cxt *pc; |
| 136 |
int errsv = errno; |
| 137 |
|
| 138 |
if (errno != EPERM |
| 139 |
&& (pc = ul_new_procfs_path(ts->pid, NULL)) |
| 140 |
&& procfs_process_get_stat_nth(pc, 9, &flags) == 0 |
| 141 |
&& (flags & PF_NO_SETAFFINITY)) { |
| 142 |
warnx(_("affinity cannot be set due to PF_NO_SETAFFINITY flag set")); |
| 143 |
errno = EINVAL; |
| 144 |
} else |
| 145 |
errno = errsv; |
| 146 |
|
| 147 |
err_affinity(ts->pid, 1); |
| 148 |
} |
| 149 |
|
| 150 |
/* re-read the current mask */ |
| 151 |
if (ts->pid) { |
| 152 |
if (sched_getaffinity(ts->pid, ts->setsize, ts->set) < 0) |
| 153 |
err_affinity(ts->pid, 0); |
| 154 |
print_affinity(ts, TRUE); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
int main(int argc, char **argv) |
| 159 |
{ |
| 160 |
cpu_set_t *new_set; |
| 161 |
pid_t pid = 0; |
| 162 |
int c, all_tasks = 0; |
| 163 |
int ncpus; |
| 164 |
size_t new_setsize, nbits; |
| 165 |
struct taskset ts; |
| 166 |
|
| 167 |
static const struct option longopts[] = { |
| 168 |
{ "all-tasks", 0, NULL, 'a' }, |
| 169 |
{ "pid", 0, NULL, 'p' }, |
| 170 |
{ "cpu-list", 0, NULL, 'c' }, |
| 171 |
{ "help", 0, NULL, 'h' }, |
| 172 |
{ "version", 0, NULL, 'V' }, |
| 173 |
{ NULL, 0, NULL, 0 } |
| 174 |
}; |
| 175 |
|
| 176 |
setlocale(LC_ALL, ""); |
| 177 |
bindtextdomain(PACKAGE, LOCALEDIR); |
| 178 |
textdomain(PACKAGE); |
| 179 |
close_stdout_atexit(); |
| 180 |
|
| 181 |
memset(&ts, 0, sizeof(ts)); |
| 182 |
|
| 183 |
while ((c = getopt_long(argc, argv, "+apchV", longopts, NULL)) != -1) { |
| 184 |
switch (c) { |
| 185 |
case 'a': |
| 186 |
all_tasks = 1; |
| 187 |
break; |
| 188 |
case 'p': |
| 189 |
/* |
| 190 |
* strtopid_or_err() is not suitable here; 0 can be |
| 191 |
* passed. |
| 192 |
*/ |
| 193 |
pid = strtos32_or_err(argv[argc - 1], |
| 194 |
_("invalid PID argument")); |
| 195 |
if (pid == 0) |
| 196 |
pid = getpid(); |
| 197 |
/* |
| 198 |
* After this point, pid == 0 means "no pid" and that |
| 199 |
* we will exec a command. |
| 200 |
*/ |
| 201 |
break; |
| 202 |
case 'c': |
| 203 |
ts.use_list = 1; |
| 204 |
break; |
| 205 |
|
| 206 |
case 'V': |
| 207 |
print_version(EXIT_SUCCESS); |
| 208 |
case 'h': |
| 209 |
usage(); |
| 210 |
default: |
| 211 |
errtryhelp(EXIT_FAILURE); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
if ((!pid && argc - optind < 2) |
| 216 |
|| (pid && (argc - optind < 1 || argc - optind > 2))) { |
| 217 |
warnx(_("bad usage")); |
| 218 |
errtryhelp(EXIT_FAILURE); |
| 219 |
} |
| 220 |
|
| 221 |
ncpus = get_max_number_of_cpus(); |
| 222 |
if (ncpus <= 0) |
| 223 |
errx(EXIT_FAILURE, _("cannot determine NR_CPUS; aborting")); |
| 224 |
|
| 225 |
/* |
| 226 |
* the ts->set is always used for the sched_getaffinity call |
| 227 |
* On the sched_getaffinity the kernel demands a user mask of |
| 228 |
* at least the size of its own cpumask_t. |
| 229 |
*/ |
| 230 |
ts.set = cpuset_alloc(ncpus, &ts.setsize, &nbits); |
| 231 |
if (!ts.set) |
| 232 |
err(EXIT_FAILURE, _("cpuset_alloc failed")); |
| 233 |
|
| 234 |
/* buffer for conversion from mask to string */ |
| 235 |
ts.buflen = 7 * nbits; |
| 236 |
ts.buf = xmalloc(ts.buflen); |
| 237 |
|
| 238 |
/* |
| 239 |
* new_set is always used for the sched_setaffinity call |
| 240 |
* On the sched_setaffinity the kernel will zero-fill its |
| 241 |
* cpumask_t if the user's mask is shorter. |
| 242 |
*/ |
| 243 |
new_set = cpuset_alloc(ncpus, &new_setsize, NULL); |
| 244 |
if (!new_set) |
| 245 |
err(EXIT_FAILURE, _("cpuset_alloc failed")); |
| 246 |
|
| 247 |
if (argc - optind == 1) |
| 248 |
ts.get_only = 1; |
| 249 |
|
| 250 |
else if (ts.use_list) { |
| 251 |
if (cpulist_parse(argv[optind], new_set, new_setsize, 0)) |
| 252 |
errx(EXIT_FAILURE, _("failed to parse CPU list: %s"), |
| 253 |
argv[optind]); |
| 254 |
} else if (cpumask_parse(argv[optind], new_set, new_setsize)) { |
| 255 |
errx(EXIT_FAILURE, _("failed to parse CPU mask: %s"), |
| 256 |
argv[optind]); |
| 257 |
} |
| 258 |
|
| 259 |
if (all_tasks && pid) { |
| 260 |
DIR *sub = NULL; |
| 261 |
struct path_cxt *pc = ul_new_procfs_path(pid, NULL); |
| 262 |
|
| 263 |
while (pc && procfs_process_next_tid(pc, &sub, &ts.pid) == 0) |
| 264 |
do_taskset(&ts, new_setsize, new_set); |
| 265 |
|
| 266 |
ul_unref_path(pc); |
| 267 |
} else { |
| 268 |
ts.pid = pid; |
| 269 |
do_taskset(&ts, new_setsize, new_set); |
| 270 |
} |
| 271 |
|
| 272 |
free(ts.buf); |
| 273 |
cpuset_free(ts.set); |
| 274 |
cpuset_free(new_set); |
| 275 |
|
| 276 |
if (!pid) { |
| 277 |
argv += optind + 1; |
| 278 |
execvp(argv[0], argv); |
| 279 |
errexec(argv[0]); |
| 280 |
} |
| 281 |
|
| 282 |
return EXIT_SUCCESS; |
| 283 |
} |