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>
64 static cpulevel_t level;
65 static cpuwhich_t which;
67 static void usage(void);
69 static void printset(cpuset_t *mask);
72 parselist(char *list, cpuset_t *mask)
74 enum { NONE, NUM, DASH } state;
79 if (strcasecmp(list, "all") == 0) {
80 if (cpuset_getaffinity(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
81 sizeof(*mask), mask) != 0)
82 err(EXIT_FAILURE, "getaffinity");
87 for (l = list; *l != '\0';) {
90 if (curnum > CPU_SETSIZE)
92 "Only %d cpus supported", CPU_SETSIZE);
101 for (; lastnum <= curnum; lastnum++)
102 CPU_SET(lastnum, mask);
117 CPU_SET(curnum, mask);
139 CPU_SET(curnum, mask);
146 errx(EXIT_FAILURE, "Malformed cpu-list %s", list);
150 printset(cpuset_t *mask)
155 for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) {
156 if (CPU_ISSET(cpu, mask)) {
167 static const char *whichnames[] = { NULL, "tid", "pid", "cpuset", "irq", "jail",
169 static const char *levelnames[] = { NULL, " root", " cpuset", "" };
176 if (cpuset_getaffinity(level, which, id, sizeof(mask), &mask) != 0)
177 err(EXIT_FAILURE, "getaffinity");
178 printf("%s %jd%s mask: ", whichnames[which], (intmax_t)id,
190 * Only LEVEL_WHICH && WHICH_CPUSET has a numbered id.
192 if (level == CPU_LEVEL_WHICH && !sflag)
193 level = CPU_LEVEL_CPUSET;
194 if (cpuset_getid(level, which, id, &setid))
196 printf("%s %jd%s id: %d\n", whichnames[which], (intmax_t)id,
197 levelnames[level], setid);
201 main(int argc, char *argv[])
210 level = CPU_LEVEL_WHICH;
211 which = CPU_WHICH_PID;
212 id = pid = tid = setid = -1;
213 while ((ch = getopt(argc, argv, "Ccd:gij:l:p:rs:t:x:")) != -1) {
220 level = CPU_LEVEL_CPUSET;
224 which = CPU_WHICH_DOMAIN;
235 which = CPU_WHICH_JAIL;
240 parselist(optarg, &mask);
244 which = CPU_WHICH_PID;
245 id = pid = atoi(optarg);
248 level = CPU_LEVEL_ROOT;
253 which = CPU_WHICH_CPUSET;
254 id = setid = atoi(optarg);
258 which = CPU_WHICH_TID;
259 id = tid = atoi(optarg);
263 which = CPU_WHICH_IRQ;
273 if (argc || Cflag || lflag)
275 /* Only one identity specifier. */
276 if (dflag + jflag + xflag + sflag + pflag + tflag > 1)
284 if (dflag || iflag || rflag)
287 * The user wants to run a command with a set and possibly cpumask.
290 if (Cflag || pflag || tflag || xflag || jflag)
293 if (cpuset_setid(CPU_WHICH_PID, -1, setid))
300 if (cpuset_setaffinity(level, CPU_WHICH_PID,
301 -1, sizeof(mask), &mask) != 0)
302 err(EXIT_FAILURE, "setaffinity");
306 err(errno == ENOENT ? 127 : 126, "%s", *argv);
309 * We're modifying something that presently exists.
311 if (Cflag && (jflag || !pflag || sflag || tflag || xflag))
315 if (!lflag && !(Cflag || sflag))
317 /* You can only set a mask on a thread. */
318 if (tflag && (sflag | pflag | xflag | jflag))
320 /* You can only set a mask on an irq. */
321 if (xflag && (jflag | pflag | sflag | tflag))
325 * Create a new cpuset and move the specified process
328 if (cpuset(&setid) < 0)
329 err(EXIT_FAILURE, "newid");
332 if (pflag && sflag) {
333 if (cpuset_setid(CPU_WHICH_PID, pid, setid))
334 err(EXIT_FAILURE, "setid");
336 * If the user specifies a set and a list we want the mask
337 * to effect the pid and not the set.
339 which = CPU_WHICH_PID;
343 if (cpuset_setaffinity(level, which, id, sizeof(mask),
345 err(EXIT_FAILURE, "setaffinity");
356 "usage: cpuset [-l cpu-list] [-s setid] cmd ...\n");
358 " cpuset [-l cpu-list] [-s setid] -p pid\n");
360 " cpuset [-c] [-l cpu-list] -C -p pid\n");
362 " cpuset [-c] [-l cpu-list] [-j jailid | -p pid | -t tid | -s setid | -x irq]\n");
364 " cpuset -g [-cir] [-d domain | -j jailid | -p pid | -t tid | -s setid |\n"