]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/cpuset/cpuset.c
Implement several enhancements to NUMA policies.
[FreeBSD/FreeBSD.git] / usr.bin / cpuset / cpuset.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2007, 2008     Jeffrey Roberson <jeff@freebsd.org>
5  * All rights reserved.
6  *
7  * Copyright (c) 2008 Nokia Corporation
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/time.h>
38 #include <sys/resource.h>
39 #include <sys/cpuset.h>
40 #include <sys/domainset.h>
41
42 #include <ctype.h>
43 #include <err.h>
44 #include <errno.h>
45 #include <limits.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <stdint.h>
49 #include <unistd.h>
50 #include <string.h>
51
52 static int Cflag;
53 static int cflag;
54 static int dflag;
55 static int gflag;
56 static int iflag;
57 static int jflag;
58 static int lflag;
59 static int nflag;
60 static int pflag;
61 static int rflag;
62 static int sflag;
63 static int tflag;
64 static int xflag;
65 static id_t id;
66 static cpulevel_t level;
67 static cpuwhich_t which;
68
69 static void usage(void);
70
71 struct numa_policy {
72         const char      *name;
73         int             policy;
74 };
75
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         { "interleave", DOMAINSET_POLICY_INTERLEAVE},
83         { "il", DOMAINSET_POLICY_INTERLEAVE},
84         { NULL, DOMAINSET_POLICY_INVALID }
85 };
86
87 static void printset(struct bitset *mask, int size);
88
89 static void
90 parselist(char *list, struct bitset *mask, int size)
91 {
92         enum { NONE, NUM, DASH } state;
93         int lastnum;
94         int curnum;
95         char *l;
96
97         state = NONE;
98         curnum = lastnum = 0;
99         for (l = list; *l != '\0';) {
100                 if (isdigit(*l)) {
101                         curnum = atoi(l);
102                         if (curnum > size)
103                                 errx(EXIT_FAILURE,
104                                     "List entry %d exceeds maximum of %d",
105                                     curnum, size);
106                         while (isdigit(*l))
107                                 l++;
108                         switch (state) {
109                         case NONE:
110                                 lastnum = curnum;
111                                 state = NUM;
112                                 break;
113                         case DASH:
114                                 for (; lastnum <= curnum; lastnum++)
115                                         BIT_SET(size, lastnum, mask);
116                                 state = NONE;
117                                 break;
118                         case NUM:
119                         default:
120                                 goto parserr;
121                         }
122                         continue;
123                 }
124                 switch (*l) {
125                 case ',':
126                         switch (state) {
127                         case NONE:
128                                 break;
129                         case NUM:
130                                 BIT_SET(size, curnum, mask);
131                                 state = NONE;
132                                 break;
133                         case DASH:
134                                 goto parserr;
135                                 break;
136                         }
137                         break;
138                 case '-':
139                         if (state != NUM)
140                                 goto parserr;
141                         state = DASH;
142                         break;
143                 default:
144                         goto parserr;
145                 }
146                 l++;
147         }
148         switch (state) {
149                 case NONE:
150                         break;
151                 case NUM:
152                         BIT_SET(size, curnum, mask);
153                         break;
154                 case DASH:
155                         goto parserr;
156         }
157         return;
158 parserr:
159         errx(EXIT_FAILURE, "Malformed list %s", list);
160 }
161
162 static void
163 parsecpulist(char *list, cpuset_t *mask)
164 {
165
166         if (strcasecmp(list, "all") == 0) {
167                 if (cpuset_getaffinity(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
168                     sizeof(*mask), mask) != 0)
169                         err(EXIT_FAILURE, "getaffinity");
170                 return;
171         }
172         parselist(list, (struct bitset *)mask, CPU_SETSIZE);
173 }
174
175 /*
176  * permissively parse policy:domain list
177  * allow:
178  *      round-robin:0-4         explicit
179  *      round-robin:all         explicit root domains
180  *      0-4                     implicit root policy
181  *      round-robin             implicit root domains
182  *      all                     explicit root domains and implicit policy
183  */
184 static void
185 parsedomainlist(char *list, domainset_t *mask, int *policyp)
186 {
187         domainset_t rootmask;
188         struct numa_policy *policy;
189         char *l;
190         int p;
191
192         /*
193          * Use the rootset's policy as the default for unspecified policies.
194          */
195         if (cpuset_getdomain(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
196             sizeof(rootmask), &rootmask, &p) != 0)
197                 err(EXIT_FAILURE, "getdomain");
198
199         l = list;
200         for (policy = &policies[0]; policy->name != NULL; policy++) {
201                 if (strncasecmp(l, policy->name, strlen(policy->name)) == 0) {
202                         p = policy->policy;
203                         l += strlen(policy->name);
204                         if (*l != ':' && *l != '\0')
205                                 errx(EXIT_FAILURE, "Malformed list %s", list);
206                         if (*l == ':')
207                                 l++;
208                         break;
209                 }
210         }
211         *policyp = p;
212         if (strcasecmp(l, "all") == 0 || *l == '\0') {
213                 DOMAINSET_COPY(&rootmask, mask);
214                 return;
215         }
216         parselist(l, (struct bitset *)mask, DOMAINSET_SETSIZE);
217 }
218
219 static void
220 printset(struct bitset *mask, int size)
221 {
222         int once;
223         int bit;
224
225         for (once = 0, bit = 0; bit < size; bit++) {
226                 if (BIT_ISSET(size, bit, mask)) {
227                         if (once == 0) {
228                                 printf("%d", bit);
229                                 once = 1;
230                         } else
231                                 printf(", %d", bit);
232                 }
233         }
234         printf("\n");
235 }
236
237 static const char *whichnames[] = { NULL, "tid", "pid", "cpuset", "irq", "jail",
238                                     "domain" };
239 static const char *levelnames[] = { NULL, " root", " cpuset", "" };
240 static const char *policynames[] = { "invalid", "round-robin", "first-touch",
241                                     "prefer", "interleave" };
242
243 static void
244 printaffinity(void)
245 {
246         domainset_t domain;
247         cpuset_t mask;
248         int policy;
249
250         if (cpuset_getaffinity(level, which, id, sizeof(mask), &mask) != 0)
251                 err(EXIT_FAILURE, "getaffinity");
252         printf("%s %jd%s mask: ", whichnames[which], (intmax_t)id,
253             levelnames[level]);
254         printset((struct bitset *)&mask, CPU_SETSIZE);
255         if (dflag)
256                 goto out;
257         if (cpuset_getdomain(level, which, id, sizeof(domain), &domain,
258             &policy) != 0)
259                 err(EXIT_FAILURE, "getdomain");
260         printf("%s %jd%s domain policy: %s mask: ", whichnames[which],
261             (intmax_t)id, levelnames[level], policynames[policy]);
262         printset((struct bitset *)&domain, DOMAINSET_SETSIZE);
263 out:
264         exit(EXIT_SUCCESS);
265 }
266
267 static void
268 printsetid(void)
269 {
270         cpusetid_t setid;
271
272         /*
273          * Only LEVEL_WHICH && WHICH_CPUSET has a numbered id.
274          */
275         if (level == CPU_LEVEL_WHICH && !sflag)
276                 level = CPU_LEVEL_CPUSET;
277         if (cpuset_getid(level, which, id, &setid))
278                 err(errno, "getid");
279         printf("%s %jd%s id: %d\n", whichnames[which], (intmax_t)id,
280             levelnames[level], setid);
281 }
282
283 int
284 main(int argc, char *argv[])
285 {
286         domainset_t domains;
287         cpusetid_t setid;
288         cpuset_t mask;
289         int policy;
290         lwpid_t tid;
291         pid_t pid;
292         int ch;
293
294         CPU_ZERO(&mask);
295         DOMAINSET_ZERO(&domains);
296         policy = DOMAINSET_POLICY_INVALID;
297         level = CPU_LEVEL_WHICH;
298         which = CPU_WHICH_PID;
299         id = pid = tid = setid = -1;
300         while ((ch = getopt(argc, argv, "Ccd:gij:l:n:p:rs:t:x:")) != -1) {
301                 switch (ch) {
302                 case 'C':
303                         Cflag = 1;
304                         break;
305                 case 'c':
306                         cflag = 1;
307                         level = CPU_LEVEL_CPUSET;
308                         break;
309                 case 'd':
310                         dflag = 1;
311                         which = CPU_WHICH_DOMAIN;
312                         id = atoi(optarg);
313                         break;
314                 case 'g':
315                         gflag = 1;
316                         break;
317                 case 'i':
318                         iflag = 1;
319                         break;
320                 case 'j':
321                         jflag = 1;
322                         which = CPU_WHICH_JAIL;
323                         id = atoi(optarg);
324                         break;
325                 case 'l':
326                         lflag = 1;
327                         parsecpulist(optarg, &mask);
328                         break;
329                 case 'n':
330                         nflag = 1;
331                         parsedomainlist(optarg, &domains, &policy);
332                         break;
333                 case 'p':
334                         pflag = 1;
335                         which = CPU_WHICH_PID;
336                         id = pid = atoi(optarg);
337                         break;
338                 case 'r':
339                         level = CPU_LEVEL_ROOT;
340                         rflag = 1;
341                         break;
342                 case 's':
343                         sflag = 1;
344                         which = CPU_WHICH_CPUSET;
345                         id = setid = atoi(optarg);
346                         break;
347                 case 't':
348                         tflag = 1;
349                         which = CPU_WHICH_TID;
350                         id = tid = atoi(optarg);
351                         break;
352                 case 'x':
353                         xflag = 1;
354                         which = CPU_WHICH_IRQ;
355                         id = atoi(optarg);
356                         break;
357                 default:
358                         usage();
359                 }
360         }
361         argc -= optind;
362         argv += optind;
363         if (gflag) {
364                 if (argc || Cflag || lflag || nflag)
365                         usage();
366                 /* Only one identity specifier. */
367                 if (dflag + jflag + xflag + sflag + pflag + tflag > 1)
368                         usage();
369                 if (iflag)
370                         printsetid();
371                 else
372                         printaffinity();
373                 exit(EXIT_SUCCESS);
374         }
375
376         if (dflag || iflag || rflag)
377                 usage();
378         /*
379          * The user wants to run a command with a set and possibly cpumask.
380          */
381         if (argc) {
382                 if (Cflag || pflag || tflag || xflag || jflag)
383                         usage();
384                 if (sflag) {
385                         if (cpuset_setid(CPU_WHICH_PID, -1, setid))
386                                 err(argc, "setid");
387                 } else {
388                         if (cpuset(&setid))
389                                 err(argc, "newid");
390                 }
391                 if (lflag) {
392                         if (cpuset_setaffinity(level, CPU_WHICH_PID,
393                             -1, sizeof(mask), &mask) != 0)
394                                 err(EXIT_FAILURE, "setaffinity");
395                 }
396                 if (nflag) {
397                         if (cpuset_setdomain(level, CPU_WHICH_PID,
398                             -1, sizeof(domains), &domains, policy) != 0)
399                                 err(EXIT_FAILURE, "setdomain");
400                 }
401                 errno = 0;
402                 execvp(*argv, argv);
403                 err(errno == ENOENT ? 127 : 126, "%s", *argv);
404         }
405         /*
406          * We're modifying something that presently exists.
407          */
408         if (Cflag && (jflag || !pflag || sflag || tflag || xflag))
409                 usage();
410         if ((!lflag && !nflag) && cflag)
411                 usage();
412         if ((!lflag && !nflag) && !(Cflag || sflag))
413                 usage();
414         /* You can only set a mask on a thread. */
415         if (tflag && (sflag | pflag | xflag | jflag))
416                 usage();
417         /* You can only set a mask on an irq. */
418         if (xflag && (jflag | pflag | sflag | tflag))
419                 usage();
420         if (Cflag) {
421                 /*
422                  * Create a new cpuset and move the specified process
423                  * into the set.
424                  */
425                 if (cpuset(&setid) < 0)
426                         err(EXIT_FAILURE, "newid");
427                 sflag = 1;
428         }
429         if (pflag && sflag) {
430                 if (cpuset_setid(CPU_WHICH_PID, pid, setid))
431                         err(EXIT_FAILURE, "setid");
432                 /*
433                  * If the user specifies a set and a list we want the mask
434                  * to effect the pid and not the set.
435                  */
436                 which = CPU_WHICH_PID;
437                 id = pid;
438         }
439         if (lflag) {
440                 if (cpuset_setaffinity(level, which, id, sizeof(mask),
441                     &mask) != 0)
442                         err(EXIT_FAILURE, "setaffinity");
443         }
444         if (nflag) {
445                 if (cpuset_setdomain(level, which, id, sizeof(domains),
446                     &domains, policy) != 0)
447                         err(EXIT_FAILURE, "setdomain");
448         }
449
450         exit(EXIT_SUCCESS);
451 }
452
453 static void
454 usage(void)
455 {
456
457         fprintf(stderr,
458             "usage: cpuset [-l cpu-list] [-s setid] cmd ...\n");
459         fprintf(stderr,
460             "       cpuset [-l cpu-list] [-s setid] -p pid\n");
461         fprintf(stderr,
462             "       cpuset [-c] [-l cpu-list] -C -p pid\n");
463         fprintf(stderr,
464             "       cpuset [-c] [-l cpu-list] [-j jailid | -p pid | -t tid | -s setid | -x irq]\n");
465         fprintf(stderr,
466             "       cpuset -g [-cir] [-d domain | -j jailid | -p pid | -t tid | -s setid |\n"
467             "              -x irq]\n");
468         exit(1);
469 }