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>
61 static cpulevel_t level;
62 static cpuwhich_t which;
64 static void usage(void);
66 static void printset(cpuset_t *mask);
69 parselist(char *list, cpuset_t *mask)
71 enum { NONE, NUM, DASH } state;
76 if (strcasecmp(list, "all") == 0) {
77 if (cpuset_getaffinity(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
78 sizeof(*mask), mask) != 0)
79 err(EXIT_FAILURE, "getaffinity");
84 for (l = list; *l != '\0';) {
87 if (curnum > CPU_SETSIZE)
89 "Only %d cpus supported", CPU_SETSIZE);
98 for (; lastnum <= curnum; lastnum++)
99 CPU_SET(lastnum, mask);
114 CPU_SET(curnum, mask);
136 CPU_SET(curnum, mask);
143 errx(EXIT_FAILURE, "Malformed cpu-list %s", list);
147 printset(cpuset_t *mask)
152 for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) {
153 if (CPU_ISSET(cpu, mask)) {
164 static const char *whichnames[] = { NULL, "tid", "pid", "cpuset", "irq", "jail" };
165 static const char *levelnames[] = { NULL, " root", " cpuset", "" };
172 if (cpuset_getaffinity(level, which, id, sizeof(mask), &mask) != 0)
173 err(EXIT_FAILURE, "getaffinity");
174 printf("%s %jd%s mask: ", whichnames[which], (intmax_t)id,
186 * Only LEVEL_WHICH && WHICH_CPUSET has a numbered id.
188 if (level == CPU_LEVEL_WHICH && !sflag)
189 level = CPU_LEVEL_CPUSET;
190 if (cpuset_getid(level, which, id, &setid))
192 printf("%s %jd%s id: %d\n", whichnames[which], (intmax_t)id,
193 levelnames[level], setid);
197 main(int argc, char *argv[])
206 level = CPU_LEVEL_WHICH;
207 which = CPU_WHICH_PID;
208 id = pid = tid = setid = -1;
209 while ((ch = getopt(argc, argv, "Ccgij:l:p:rs:t:x:")) != -1) {
218 level = CPU_LEVEL_CPUSET;
228 which = CPU_WHICH_JAIL;
233 parselist(optarg, &mask);
237 which = CPU_WHICH_PID;
238 id = pid = atoi(optarg);
243 level = CPU_LEVEL_ROOT;
248 which = CPU_WHICH_CPUSET;
249 id = setid = atoi(optarg);
253 which = CPU_WHICH_TID;
254 id = tid = atoi(optarg);
258 which = CPU_WHICH_IRQ;
268 if (argc || Cflag || lflag)
270 /* Only one identity specifier. */
271 if (jflag + xflag + sflag + pflag + tflag > 1)
282 * The user wants to run a command with a set and possibly cpumask.
285 if (Cflag | pflag | rflag | tflag | xflag | jflag)
288 if (cpuset_setid(CPU_WHICH_PID, -1, setid))
295 if (cpuset_setaffinity(level, CPU_WHICH_PID,
296 -1, sizeof(mask), &mask) != 0)
297 err(EXIT_FAILURE, "setaffinity");
301 err(errno == ENOENT ? 127 : 126, "%s", *argv);
304 * We're modifying something that presently exists.
306 if (Cflag && (sflag || rflag || !pflag || tflag || xflag || jflag))
308 if (!lflag && (cflag || rflag))
310 if (!lflag && !(Cflag || sflag))
312 /* You can only set a mask on a thread. */
313 if (tflag && (sflag | pflag | xflag | jflag))
315 /* You can only set a mask on an irq. */
316 if (xflag && (jflag | pflag | sflag | tflag))
320 * Create a new cpuset and move the specified process
323 if (cpuset(&setid) < 0)
324 err(EXIT_FAILURE, "newid");
327 if (pflag && sflag) {
328 if (cpuset_setid(CPU_WHICH_PID, pid, setid))
329 err(EXIT_FAILURE, "setid");
331 * If the user specifies a set and a list we want the mask
332 * to effect the pid and not the set.
334 which = CPU_WHICH_PID;
338 if (cpuset_setaffinity(level, which, id, sizeof(mask),
340 err(EXIT_FAILURE, "setaffinity");
351 "usage: cpuset [-l cpu-list] [-s setid] cmd ...\n");
353 " cpuset [-l cpu-list] [-s setid] -p pid\n");
355 " cpuset [-c] [-l cpu-list] -C -p pid\n");
357 " cpuset [-cr] [-l cpu-list] [-j jailid | -p pid | -t tid | -s setid | -x irq]\n");
359 " cpuset [-cgir] [-j jailid | -p pid | -t tid | -s setid | -x irq]\n");