2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2007, 2008 Jeffrey Roberson <jeff@freebsd.org>
7 * Copyright (c) 2008 Nokia Corporation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/types.h>
38 #include <sys/resource.h>
39 #include <sys/cpuset.h>
65 static cpulevel_t level;
66 static cpuwhich_t which;
68 static void usage(void);
70 static void printset(cpuset_t *mask);
73 parselist(char *list, cpuset_t *mask)
75 enum { NONE, NUM, DASH } state;
80 if (strcasecmp(list, "all") == 0) {
81 if (cpuset_getaffinity(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
82 sizeof(*mask), mask) != 0)
83 err(EXIT_FAILURE, "getaffinity");
88 for (l = list; *l != '\0';) {
91 if (curnum > CPU_SETSIZE)
93 "Only %d cpus supported", CPU_SETSIZE);
102 for (; lastnum <= curnum; lastnum++)
103 CPU_SET(lastnum, mask);
118 CPU_SET(curnum, mask);
140 CPU_SET(curnum, mask);
147 errx(EXIT_FAILURE, "Malformed cpu-list %s", list);
151 printset(cpuset_t *mask)
156 for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) {
157 if (CPU_ISSET(cpu, mask)) {
168 static const char *whichnames[] = { NULL, "tid", "pid", "cpuset", "irq", "jail",
170 static const char *levelnames[] = { NULL, " root", " cpuset", "" };
177 if (cpuset_getaffinity(level, which, id, sizeof(mask), &mask) != 0)
178 err(EXIT_FAILURE, "getaffinity");
179 printf("%s %jd%s mask: ", whichnames[which], (intmax_t)id,
191 * Only LEVEL_WHICH && WHICH_CPUSET has a numbered id.
193 if (level == CPU_LEVEL_WHICH && !sflag)
194 level = CPU_LEVEL_CPUSET;
195 if (cpuset_getid(level, which, id, &setid))
197 printf("%s %jd%s id: %d\n", whichnames[which], (intmax_t)id,
198 levelnames[level], setid);
202 main(int argc, char *argv[])
211 level = CPU_LEVEL_WHICH;
212 which = CPU_WHICH_PID;
213 id = pid = tid = setid = -1;
214 while ((ch = getopt(argc, argv, "Ccd:gij:l:p:rs:t:x:")) != -1) {
221 level = CPU_LEVEL_CPUSET;
225 which = CPU_WHICH_DOMAIN;
236 which = CPU_WHICH_JAIL;
237 id = jail_getid(optarg);
239 errx(EXIT_FAILURE, "%s", jail_errmsg);
243 parselist(optarg, &mask);
247 which = CPU_WHICH_PID;
248 id = pid = atoi(optarg);
251 level = CPU_LEVEL_ROOT;
256 which = CPU_WHICH_CPUSET;
257 id = setid = atoi(optarg);
261 which = CPU_WHICH_TID;
262 id = tid = atoi(optarg);
266 which = CPU_WHICH_IRQ;
276 if (argc || Cflag || lflag)
278 /* Only one identity specifier. */
279 if (dflag + jflag + xflag + sflag + pflag + tflag > 1)
287 if (dflag || iflag || rflag)
290 * The user wants to run a command with a set and possibly cpumask.
293 if (Cflag || pflag || tflag || xflag || jflag)
296 if (cpuset_setid(CPU_WHICH_PID, -1, setid))
303 if (cpuset_setaffinity(level, CPU_WHICH_PID,
304 -1, sizeof(mask), &mask) != 0)
305 err(EXIT_FAILURE, "setaffinity");
309 err(errno == ENOENT ? 127 : 126, "%s", *argv);
312 * We're modifying something that presently exists.
314 if (Cflag && (jflag || !pflag || sflag || tflag || xflag))
318 if (!lflag && !(Cflag || sflag))
320 /* You can only set a mask on a thread. */
321 if (tflag && (sflag | pflag | xflag | jflag))
323 /* You can only set a mask on an irq. */
324 if (xflag && (jflag | pflag | sflag | tflag))
328 * Create a new cpuset and move the specified process
331 if (cpuset(&setid) < 0)
332 err(EXIT_FAILURE, "newid");
335 if (pflag && sflag) {
336 if (cpuset_setid(CPU_WHICH_PID, pid, setid))
337 err(EXIT_FAILURE, "setid");
339 * If the user specifies a set and a list we want the mask
340 * to effect the pid and not the set.
342 which = CPU_WHICH_PID;
346 if (cpuset_setaffinity(level, which, id, sizeof(mask),
348 err(EXIT_FAILURE, "setaffinity");
359 "usage: cpuset [-l cpu-list] [-s setid] cmd ...\n");
361 " cpuset [-l cpu-list] [-s setid] -p pid\n");
363 " cpuset [-c] [-l cpu-list] -C -p pid\n");
365 " cpuset [-c] [-l cpu-list] [-j jailid | -p pid | -t tid | -s setid | -x irq]\n");
367 " cpuset -g [-cir] [-d domain | -j jailid | -p pid | -t tid | -s setid |\n"