2 * Copyright (c) 2007, 2008 Jeffrey Roberson <jeff@freebsd.org>
5 * Copyright (c) 2008 Nokia Corporation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/types.h>
36 #include <sys/resource.h>
37 #include <sys/cpuset.h>
62 static cpulevel_t level;
63 static cpuwhich_t which;
65 static void usage(void);
67 static void printset(cpuset_t *mask);
70 parselist(char *list, cpuset_t *mask)
72 enum { NONE, NUM, DASH } state;
77 if (strcasecmp(list, "all") == 0) {
78 if (cpuset_getaffinity(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
79 sizeof(*mask), mask) != 0)
80 err(EXIT_FAILURE, "getaffinity");
85 for (l = list; *l != '\0';) {
88 if (curnum > CPU_SETSIZE)
90 "Only %d cpus supported", CPU_SETSIZE);
99 for (; lastnum <= curnum; lastnum++)
100 CPU_SET(lastnum, mask);
115 CPU_SET(curnum, mask);
137 CPU_SET(curnum, mask);
144 errx(EXIT_FAILURE, "Malformed cpu-list %s", list);
148 printset(cpuset_t *mask)
153 for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) {
154 if (CPU_ISSET(cpu, mask)) {
165 static const char *whichnames[] = { NULL, "tid", "pid", "cpuset", "irq", "jail",
167 static const char *levelnames[] = { NULL, " root", " cpuset", "" };
174 if (cpuset_getaffinity(level, which, id, sizeof(mask), &mask) != 0)
175 err(EXIT_FAILURE, "getaffinity");
176 printf("%s %jd%s mask: ", whichnames[which], (intmax_t)id,
188 * Only LEVEL_WHICH && WHICH_CPUSET has a numbered id.
190 if (level == CPU_LEVEL_WHICH && !sflag)
191 level = CPU_LEVEL_CPUSET;
192 if (cpuset_getid(level, which, id, &setid))
194 printf("%s %jd%s id: %d\n", whichnames[which], (intmax_t)id,
195 levelnames[level], setid);
199 main(int argc, char *argv[])
208 level = CPU_LEVEL_WHICH;
209 which = CPU_WHICH_PID;
210 id = pid = tid = setid = -1;
211 while ((ch = getopt(argc, argv, "Ccd:gij:l:p:rs:t:x:")) != -1) {
218 level = CPU_LEVEL_CPUSET;
222 which = CPU_WHICH_DOMAIN;
233 which = CPU_WHICH_JAIL;
238 parselist(optarg, &mask);
242 which = CPU_WHICH_PID;
243 id = pid = atoi(optarg);
246 level = CPU_LEVEL_ROOT;
251 which = CPU_WHICH_CPUSET;
252 id = setid = atoi(optarg);
256 which = CPU_WHICH_TID;
257 id = tid = atoi(optarg);
261 which = CPU_WHICH_IRQ;
271 if (argc || Cflag || lflag)
273 /* Only one identity specifier. */
274 if (dflag + jflag + xflag + sflag + pflag + tflag > 1)
282 if (dflag || iflag || rflag)
285 * The user wants to run a command with a set and possibly cpumask.
288 if (Cflag || pflag || tflag || xflag || jflag)
291 if (cpuset_setid(CPU_WHICH_PID, -1, setid))
298 if (cpuset_setaffinity(level, CPU_WHICH_PID,
299 -1, sizeof(mask), &mask) != 0)
300 err(EXIT_FAILURE, "setaffinity");
304 err(errno == ENOENT ? 127 : 126, "%s", *argv);
307 * We're modifying something that presently exists.
309 if (Cflag && (jflag || !pflag || sflag || tflag || xflag))
313 if (!lflag && !(Cflag || sflag))
315 /* You can only set a mask on a thread. */
316 if (tflag && (sflag | pflag | xflag | jflag))
318 /* You can only set a mask on an irq. */
319 if (xflag && (jflag | pflag | sflag | tflag))
323 * Create a new cpuset and move the specified process
326 if (cpuset(&setid) < 0)
327 err(EXIT_FAILURE, "newid");
330 if (pflag && sflag) {
331 if (cpuset_setid(CPU_WHICH_PID, pid, setid))
332 err(EXIT_FAILURE, "setid");
334 * If the user specifies a set and a list we want the mask
335 * to effect the pid and not the set.
337 which = CPU_WHICH_PID;
341 if (cpuset_setaffinity(level, which, id, sizeof(mask),
343 err(EXIT_FAILURE, "setaffinity");
354 "usage: cpuset [-l cpu-list] [-s setid] cmd ...\n");
356 " cpuset [-l cpu-list] [-s setid] -p pid\n");
358 " cpuset [-c] [-l cpu-list] -C -p pid\n");
360 " cpuset [-c] [-l cpu-list] [-j jailid | -p pid | -t tid | -s setid | -x irq]\n");
362 " cpuset -g [-cir] [-d domain | -j jailid | -p pid | -t tid | -s setid |\n"