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>
65 static void printset(cpuset_t *mask);
68 parselist(char *list, cpuset_t *mask)
70 enum { NONE, NUM, DASH } state;
77 for (l = list; *l != '\0';) {
80 if (curnum > CPU_SETSIZE)
82 "Only %d cpus supported", CPU_SETSIZE);
91 for (; lastnum <= curnum; lastnum++)
92 CPU_SET(lastnum, mask);
107 CPU_SET(curnum, mask);
129 CPU_SET(curnum, mask);
136 errx(EXIT_FAILURE, "Malformed cpu-list %s", list);
140 printset(cpuset_t *mask)
145 for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) {
146 if (CPU_ISSET(cpu, mask)) {
157 const char *whichnames[] = { NULL, "tid", "pid", "cpuset", "irq", "jail" };
158 const char *levelnames[] = { NULL, " root", " cpuset", "" };
165 if (cpuset_getaffinity(level, which, id, sizeof(mask), &mask) != 0)
166 err(EXIT_FAILURE, "getaffinity");
167 printf("%s %jd%s mask: ", whichnames[which], (intmax_t)id,
179 * Only LEVEL_WHICH && WHICH_CPUSET has a numbered id.
181 if (level == CPU_LEVEL_WHICH && !sflag)
182 level = CPU_LEVEL_CPUSET;
183 if (cpuset_getid(level, which, id, &setid))
185 printf("%s %jd%s id: %d\n", whichnames[which], (intmax_t)id,
186 levelnames[level], setid);
190 main(int argc, char *argv[])
199 level = CPU_LEVEL_WHICH;
200 which = CPU_WHICH_PID;
201 id = pid = tid = setid = -1;
202 while ((ch = getopt(argc, argv, "cgij:l:p:rs:t:x:")) != -1) {
208 level = CPU_LEVEL_CPUSET;
218 which = CPU_WHICH_JAIL;
223 parselist(optarg, &mask);
227 which = CPU_WHICH_PID;
228 id = pid = atoi(optarg);
233 level = CPU_LEVEL_ROOT;
238 which = CPU_WHICH_CPUSET;
239 id = setid = atoi(optarg);
243 which = CPU_WHICH_TID;
244 id = tid = atoi(optarg);
248 which = CPU_WHICH_IRQ;
260 /* Only one identity specifier. */
261 if (jflag + xflag + sflag + pflag + tflag > 1)
272 * The user wants to run a command with a set and possibly cpumask.
275 if (pflag | rflag | tflag | xflag | jflag)
278 if (cpuset_setid(CPU_WHICH_PID, -1, setid))
285 if (cpuset_setaffinity(level, CPU_WHICH_PID,
286 -1, sizeof(mask), &mask) != 0)
287 err(EXIT_FAILURE, "setaffinity");
291 err(errno == ENOENT ? 127 : 126, "%s", *argv);
294 * We're modifying something that presently exists.
296 if (!lflag && (cflag || rflag))
298 if (!lflag && !sflag)
300 /* You can only set a mask on a thread. */
301 if (tflag && (sflag | pflag | xflag | jflag))
303 /* You can only set a mask on an irq. */
304 if (xflag && (jflag | pflag | sflag | tflag))
306 if (pflag && sflag) {
307 if (cpuset_setid(CPU_WHICH_PID, pid, setid))
308 err(EXIT_FAILURE, "setid");
310 * If the user specifies a set and a list we want the mask
311 * to effect the pid and not the set.
313 which = CPU_WHICH_PID;
317 if (cpuset_setaffinity(level, which, id, sizeof(mask),
319 err(EXIT_FAILURE, "setaffinity");
330 "usage: cpuset [-l cpu-list] [-s setid] cmd ...\n");
332 " cpuset [-l cpu-list] [-s setid] -p pid\n");
334 " cpuset [-cr] [-l cpu-list] [-j jailid | -p pid | -t tid | -s setid | -x irq]\n");
336 " cpuset [-cgir] [-j jailid | -p pid | -t tid | -s setid | -x irq]\n");