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>
66 static cpulevel_t level;
67 static cpuwhich_t which;
69 static void usage(void);
76 static struct numa_policy policies[] = {
77 { "round-robin", DOMAINSET_POLICY_ROUNDROBIN },
78 { "rr", DOMAINSET_POLICY_ROUNDROBIN },
79 { "first-touch", DOMAINSET_POLICY_FIRSTTOUCH },
80 { "ft", DOMAINSET_POLICY_FIRSTTOUCH },
81 { "prefer", DOMAINSET_POLICY_PREFER },
82 { NULL, DOMAINSET_POLICY_INVALID }
85 BITSET_DEFINE(bitset, 1);
86 static void printset(struct bitset *mask, int size);
89 parselist(char *list, struct bitset *mask, int size)
91 enum { NONE, NUM, DASH } state;
98 for (l = list; *l != '\0';) {
103 "List entry %d exceeds maximum of %d",
113 for (; lastnum <= curnum; lastnum++)
114 BIT_SET(size, lastnum, mask);
129 BIT_SET(size, curnum, mask);
151 BIT_SET(size, curnum, mask);
158 errx(EXIT_FAILURE, "Malformed list %s", list);
162 parsecpulist(char *list, cpuset_t *mask)
165 if (strcasecmp(list, "all") == 0) {
166 if (cpuset_getaffinity(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
167 sizeof(*mask), mask) != 0)
168 err(EXIT_FAILURE, "getaffinity");
171 parselist(list, (struct bitset *)mask, CPU_SETSIZE);
175 * permissively parse policy:domain list
177 * round-robin:0-4 explicit
178 * round-robin:all explicit root domains
179 * 0-4 implicit root policy
180 * round-robin implicit root domains
181 * all explicit root domains and implicit policy
184 parsedomainlist(char *list, domainset_t *mask, int *policyp)
186 domainset_t rootmask;
187 struct numa_policy *policy;
192 * Use the rootset's policy as the default for unspecified policies.
194 if (cpuset_getdomain(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
195 sizeof(rootmask), &rootmask, &p) != 0)
196 err(EXIT_FAILURE, "getdomain");
199 for (policy = &policies[0]; policy->name != NULL; policy++) {
200 if (strncasecmp(l, policy->name, strlen(policy->name)) == 0) {
202 l += strlen(policy->name);
203 if (*l != ':' && *l != '\0')
204 errx(EXIT_FAILURE, "Malformed list %s", list);
211 if (strcasecmp(l, "all") == 0 || *l == '\0') {
212 DOMAINSET_COPY(&rootmask, mask);
215 parselist(l, (struct bitset *)mask, DOMAINSET_SETSIZE);
219 printset(struct bitset *mask, int size)
224 for (once = 0, bit = 0; bit < size; bit++) {
225 if (BIT_ISSET(size, bit, mask)) {
236 static const char *whichnames[] = { NULL, "tid", "pid", "cpuset", "irq", "jail",
238 static const char *levelnames[] = { NULL, " root", " cpuset", "" };
239 static const char *policynames[] = { "invalid", "round-robin", "first-touch",
249 if (cpuset_getaffinity(level, which, id, sizeof(mask), &mask) != 0)
250 err(EXIT_FAILURE, "getaffinity");
251 printf("%s %jd%s mask: ", whichnames[which], (intmax_t)id,
253 printset((struct bitset *)&mask, CPU_SETSIZE);
256 if (cpuset_getdomain(level, which, id, sizeof(domain), &domain,
258 err(EXIT_FAILURE, "getdomain");
259 printf("%s %jd%s domain policy: %s mask: ", whichnames[which],
260 (intmax_t)id, levelnames[level], policynames[policy]);
261 printset((struct bitset *)&domain, DOMAINSET_SETSIZE);
272 * Only LEVEL_WHICH && WHICH_CPUSET has a numbered id.
274 if (level == CPU_LEVEL_WHICH && !sflag)
275 level = CPU_LEVEL_CPUSET;
276 if (cpuset_getid(level, which, id, &setid))
278 printf("%s %jd%s id: %d\n", whichnames[which], (intmax_t)id,
279 levelnames[level], setid);
283 main(int argc, char *argv[])
294 DOMAINSET_ZERO(&domains);
295 policy = DOMAINSET_POLICY_INVALID;
296 level = CPU_LEVEL_WHICH;
297 which = CPU_WHICH_PID;
298 id = pid = tid = setid = -1;
299 while ((ch = getopt(argc, argv, "Ccd:gij:l:n:p:rs:t:x:")) != -1) {
306 level = CPU_LEVEL_CPUSET;
310 which = CPU_WHICH_DOMAIN;
321 which = CPU_WHICH_JAIL;
326 parsecpulist(optarg, &mask);
330 parsedomainlist(optarg, &domains, &policy);
334 which = CPU_WHICH_PID;
335 id = pid = atoi(optarg);
338 level = CPU_LEVEL_ROOT;
343 which = CPU_WHICH_CPUSET;
344 id = setid = atoi(optarg);
348 which = CPU_WHICH_TID;
349 id = tid = atoi(optarg);
353 which = CPU_WHICH_IRQ;
363 if (argc || Cflag || lflag || nflag)
365 /* Only one identity specifier. */
366 if (dflag + jflag + xflag + sflag + pflag + tflag > 1)
375 if (dflag || iflag || rflag)
378 * The user wants to run a command with a set and possibly cpumask.
381 if (Cflag || pflag || tflag || xflag || jflag)
384 if (cpuset_setid(CPU_WHICH_PID, -1, setid))
391 if (cpuset_setaffinity(level, CPU_WHICH_PID,
392 -1, sizeof(mask), &mask) != 0)
393 err(EXIT_FAILURE, "setaffinity");
396 if (cpuset_setdomain(level, CPU_WHICH_PID,
397 -1, sizeof(domains), &domains, policy) != 0)
398 err(EXIT_FAILURE, "setdomain");
402 err(errno == ENOENT ? 127 : 126, "%s", *argv);
405 * We're modifying something that presently exists.
407 if (Cflag && (jflag || !pflag || sflag || tflag || xflag))
409 if ((!lflag && !nflag) && cflag)
411 if ((!lflag && !nflag) && !(Cflag || sflag))
413 /* You can only set a mask on a thread. */
414 if (tflag && (sflag | pflag | xflag | jflag))
416 /* You can only set a mask on an irq. */
417 if (xflag && (jflag | pflag | sflag | tflag))
421 * Create a new cpuset and move the specified process
424 if (cpuset(&setid) < 0)
425 err(EXIT_FAILURE, "newid");
428 if (pflag && sflag) {
429 if (cpuset_setid(CPU_WHICH_PID, pid, setid))
430 err(EXIT_FAILURE, "setid");
432 * If the user specifies a set and a list we want the mask
433 * to effect the pid and not the set.
435 which = CPU_WHICH_PID;
439 if (cpuset_setaffinity(level, which, id, sizeof(mask),
441 err(EXIT_FAILURE, "setaffinity");
444 if (cpuset_setdomain(level, which, id, sizeof(domains),
445 &domains, policy) != 0)
446 err(EXIT_FAILURE, "setdomain");
457 "usage: cpuset [-l cpu-list] [-s setid] cmd ...\n");
459 " cpuset [-l cpu-list] [-s setid] -p pid\n");
461 " cpuset [-c] [-l cpu-list] -C -p pid\n");
463 " cpuset [-c] [-l cpu-list] [-j jailid | -p pid | -t tid | -s setid | -x irq]\n");
465 " cpuset -g [-cir] [-d domain | -j jailid | -p pid | -t tid | -s setid |\n"