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>
40 #include <sys/domainset.h>
67 static cpulevel_t level;
68 static cpuwhich_t which;
70 static void usage(void);
77 static struct numa_policy policies[] = {
78 { "round-robin", DOMAINSET_POLICY_ROUNDROBIN },
79 { "rr", DOMAINSET_POLICY_ROUNDROBIN },
80 { "first-touch", DOMAINSET_POLICY_FIRSTTOUCH },
81 { "ft", DOMAINSET_POLICY_FIRSTTOUCH },
82 { "prefer", DOMAINSET_POLICY_PREFER },
83 { "interleave", DOMAINSET_POLICY_INTERLEAVE},
84 { "il", DOMAINSET_POLICY_INTERLEAVE},
85 { NULL, DOMAINSET_POLICY_INVALID }
88 static void printset(struct bitset *mask, int size);
91 parselist(char *list, struct bitset *mask, int size)
93 enum { NONE, NUM, DASH } state;
100 for (l = list; *l != '\0';) {
105 "List entry %d exceeds maximum of %d",
115 for (; lastnum <= curnum; lastnum++)
116 BIT_SET(size, lastnum, mask);
131 BIT_SET(size, curnum, mask);
153 BIT_SET(size, curnum, mask);
160 errx(EXIT_FAILURE, "Malformed list %s", list);
164 parsecpulist(char *list, cpuset_t *mask)
167 if (strcasecmp(list, "all") == 0) {
168 if (cpuset_getaffinity(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
169 sizeof(*mask), mask) != 0)
170 err(EXIT_FAILURE, "getaffinity");
173 parselist(list, (struct bitset *)mask, CPU_SETSIZE);
177 * permissively parse policy:domain list
179 * round-robin:0-4 explicit
180 * round-robin:all explicit root domains
181 * 0-4 implicit root policy
182 * round-robin implicit root domains
183 * all explicit root domains and implicit policy
186 parsedomainlist(char *list, domainset_t *mask, int *policyp)
188 domainset_t rootmask;
189 struct numa_policy *policy;
194 * Use the rootset's policy as the default for unspecified policies.
196 if (cpuset_getdomain(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
197 sizeof(rootmask), &rootmask, &p) != 0)
198 err(EXIT_FAILURE, "getdomain");
201 for (policy = &policies[0]; policy->name != NULL; policy++) {
202 if (strncasecmp(l, policy->name, strlen(policy->name)) == 0) {
204 l += strlen(policy->name);
205 if (*l != ':' && *l != '\0')
206 errx(EXIT_FAILURE, "Malformed list %s", list);
213 if (strcasecmp(l, "all") == 0 || *l == '\0') {
214 DOMAINSET_COPY(&rootmask, mask);
217 parselist(l, (struct bitset *)mask, DOMAINSET_SETSIZE);
221 printset(struct bitset *mask, int size)
226 for (once = 0, bit = 0; bit < size; bit++) {
227 if (BIT_ISSET(size, bit, mask)) {
238 static const char *whichnames[] = { NULL, "tid", "pid", "cpuset", "irq", "jail",
240 static const char *levelnames[] = { NULL, " root", " cpuset", "" };
241 static const char *policynames[] = { "invalid", "round-robin", "first-touch",
242 "prefer", "interleave" };
251 if (cpuset_getaffinity(level, which, id, sizeof(mask), &mask) != 0)
252 err(EXIT_FAILURE, "getaffinity");
253 printf("%s %jd%s mask: ", whichnames[which], (intmax_t)id,
255 printset((struct bitset *)&mask, CPU_SETSIZE);
258 if (cpuset_getdomain(level, which, id, sizeof(domain), &domain,
260 err(EXIT_FAILURE, "getdomain");
261 printf("%s %jd%s domain policy: %s mask: ", whichnames[which],
262 (intmax_t)id, levelnames[level], policynames[policy]);
263 printset((struct bitset *)&domain, DOMAINSET_SETSIZE);
274 * Only LEVEL_WHICH && WHICH_CPUSET has a numbered id.
276 if (level == CPU_LEVEL_WHICH && !sflag)
277 level = CPU_LEVEL_CPUSET;
278 if (cpuset_getid(level, which, id, &setid))
280 printf("%s %jd%s id: %d\n", whichnames[which], (intmax_t)id,
281 levelnames[level], setid);
285 main(int argc, char *argv[])
296 DOMAINSET_ZERO(&domains);
297 policy = DOMAINSET_POLICY_INVALID;
298 level = CPU_LEVEL_WHICH;
299 which = CPU_WHICH_PID;
300 id = pid = tid = setid = -1;
301 while ((ch = getopt(argc, argv, "Ccd:gij:l:n:p:rs:t:x:")) != -1) {
308 level = CPU_LEVEL_CPUSET;
312 which = CPU_WHICH_DOMAIN;
323 which = CPU_WHICH_JAIL;
324 id = jail_getid(optarg);
326 errx(EXIT_FAILURE, "%s", jail_errmsg);
330 parsecpulist(optarg, &mask);
334 parsedomainlist(optarg, &domains, &policy);
338 which = CPU_WHICH_PID;
339 id = pid = atoi(optarg);
342 level = CPU_LEVEL_ROOT;
347 which = CPU_WHICH_CPUSET;
348 id = setid = atoi(optarg);
352 which = CPU_WHICH_TID;
353 id = tid = atoi(optarg);
357 which = CPU_WHICH_IRQ;
367 if (argc || Cflag || lflag || nflag)
369 /* Only one identity specifier. */
370 if (dflag + jflag + xflag + sflag + pflag + tflag > 1)
379 if (dflag || iflag || rflag)
382 * The user wants to run a command with a set and possibly cpumask.
385 if (Cflag || pflag || tflag || xflag || jflag)
388 if (cpuset_setid(CPU_WHICH_PID, -1, setid))
395 if (cpuset_setaffinity(level, CPU_WHICH_PID,
396 -1, sizeof(mask), &mask) != 0)
397 err(EXIT_FAILURE, "setaffinity");
400 if (cpuset_setdomain(level, CPU_WHICH_PID,
401 -1, sizeof(domains), &domains, policy) != 0)
402 err(EXIT_FAILURE, "setdomain");
406 err(errno == ENOENT ? 127 : 126, "%s", *argv);
409 * We're modifying something that presently exists.
411 if (Cflag && (jflag || !pflag || sflag || tflag || xflag))
413 if ((!lflag && !nflag) && cflag)
415 if ((!lflag && !nflag) && !(Cflag || sflag))
417 /* You can only set a mask on a thread. */
418 if (tflag && (sflag | pflag | xflag | jflag))
420 /* You can only set a mask on an irq. */
421 if (xflag && (jflag | pflag | sflag | tflag))
425 * Create a new cpuset and move the specified process
428 if (cpuset(&setid) < 0)
429 err(EXIT_FAILURE, "newid");
432 if (pflag && sflag) {
433 if (cpuset_setid(CPU_WHICH_PID, pid, setid))
434 err(EXIT_FAILURE, "setid");
436 * If the user specifies a set and a list we want the mask
437 * to effect the pid and not the set.
439 which = CPU_WHICH_PID;
443 if (cpuset_setaffinity(level, which, id, sizeof(mask),
445 err(EXIT_FAILURE, "setaffinity");
448 if (cpuset_setdomain(level, which, id, sizeof(domains),
449 &domains, policy) != 0)
450 err(EXIT_FAILURE, "setdomain");
461 "usage: cpuset [-l cpu-list] [-s setid] cmd ...\n");
463 " cpuset [-l cpu-list] [-s setid] -p pid\n");
465 " cpuset [-c] [-l cpu-list] -C -p pid\n");
467 " cpuset [-c] [-l cpu-list] [-j jailid | -p pid | -t tid | -s setid | -x irq]\n");
469 " cpuset -g [-cir] [-d domain | -j jailid | -p pid | -t tid | -s setid |\n"