2 * Copyright (c) 2007, 2008 Jeffrey Roberson <jeff@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/types.h>
33 #include <sys/resource.h>
34 #include <sys/cpuset.h>
61 static void printset(cpuset_t *mask);
64 parselist(char *list, cpuset_t *mask)
66 enum { NONE, NUM, DASH } state;
73 for (l = list; *l != '\0';) {
76 if (curnum > CPU_SETSIZE)
78 "Only %d cpus supported", CPU_SETSIZE);
87 for (; lastnum <= curnum; lastnum++)
88 CPU_SET(lastnum, mask);
103 CPU_SET(curnum, mask);
125 CPU_SET(curnum, mask);
132 errx(EXIT_FAILURE, "Malformed cpu-list %s", list);
136 printset(cpuset_t *mask)
141 for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) {
142 if (CPU_ISSET(cpu, mask)) {
153 const char *whichnames[] = { NULL, "tid", "pid", "cpuset", "N/A", "jail" };
154 const char *levelnames[] = { NULL, " root", " cpuset", "" };
161 if (cpuset_getaffinity(level, which, id, sizeof(mask), &mask) != 0)
162 err(EXIT_FAILURE, "getaffinity");
163 printf("%s %jd%s mask: ", whichnames[which], (intmax_t)id,
175 * Only LEVEL_WHICH && WHICH_CPUSET has a numbered id.
177 if (level == CPU_LEVEL_WHICH && !sflag)
178 level = CPU_LEVEL_CPUSET;
179 if (cpuset_getid(level, which, id, &setid))
181 printf("%s %jd%s id: %d\n", whichnames[which], (intmax_t)id,
182 levelnames[level], setid);
186 main(int argc, char *argv[])
195 level = CPU_LEVEL_WHICH;
196 which = CPU_WHICH_PID;
197 id = pid = tid = setid = -1;
198 while ((ch = getopt(argc, argv, "cgij:l:p:rs:t:")) != -1) {
204 level = CPU_LEVEL_CPUSET;
214 which = CPU_WHICH_JAIL;
219 parselist(optarg, &mask);
223 which = CPU_WHICH_PID;
224 id = pid = atoi(optarg);
229 level = CPU_LEVEL_ROOT;
234 which = CPU_WHICH_CPUSET;
235 id = setid = atoi(optarg);
239 which = CPU_WHICH_TID;
240 id = tid = atoi(optarg);
251 /* Only one identity specifier. */
252 if (jflag + sflag + pflag + tflag > 1)
263 * The user wants to run a command with a set and possibly cpumask.
266 if (pflag | rflag | tflag | jflag)
269 if (cpuset_setid(CPU_WHICH_PID, -1, setid))
276 if (cpuset_setaffinity(level, CPU_WHICH_PID,
277 -1, sizeof(mask), &mask) != 0)
278 err(EXIT_FAILURE, "setaffinity");
282 err(errno == ENOENT ? 127 : 126, "%s", *argv);
285 * We're modifying something that presently exists.
287 if (!lflag && (cflag || rflag))
289 if (!lflag && !sflag)
291 /* You can only set a mask on a thread. */
292 if (tflag && (sflag | pflag | jflag))
294 if (pflag && sflag) {
295 if (cpuset_setid(CPU_WHICH_PID, pid, setid))
296 err(EXIT_FAILURE, "setid");
298 * If the user specifies a set and a list we want the mask
299 * to effect the pid and not the set.
301 which = CPU_WHICH_PID;
305 if (cpuset_setaffinity(level, which, id, sizeof(mask),
307 err(EXIT_FAILURE, "setaffinity");
318 "usage: cpuset [-l cpu-list] [-s setid] cmd ...\n");
320 " cpuset [-l cpu-list] [-s setid] -p pid\n");
322 " cpuset [-cr] [-l cpu-list] [-j jailid | -p pid | -t tid | -s setid]\n");
324 " cpuset [-cgir] [-j jailid | -p pid | -t tid | -s setid]\n");