]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/cpuset/cpuset.c
Update to bmake-201802222
[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         { NULL, DOMAINSET_POLICY_INVALID }
83 };
84
85 BITSET_DEFINE(bitset, 1);
86 static void printset(struct bitset *mask, int size);
87
88 static void
89 parselist(char *list, struct bitset *mask, int size)
90 {
91         enum { NONE, NUM, DASH } state;
92         int lastnum;
93         int curnum;
94         char *l;
95
96         state = NONE;
97         curnum = lastnum = 0;
98         for (l = list; *l != '\0';) {
99                 if (isdigit(*l)) {
100                         curnum = atoi(l);
101                         if (curnum > size)
102                                 errx(EXIT_FAILURE,
103                                     "List entry %d exceeds maximum of %d",
104                                     curnum, size);
105                         while (isdigit(*l))
106                                 l++;
107                         switch (state) {
108                         case NONE:
109                                 lastnum = curnum;
110                                 state = NUM;
111                                 break;
112                         case DASH:
113                                 for (; lastnum <= curnum; lastnum++)
114                                         BIT_SET(size, lastnum, mask);
115                                 state = NONE;
116                                 break;
117                         case NUM:
118                         default:
119                                 goto parserr;
120                         }
121                         continue;
122                 }
123                 switch (*l) {
124                 case ',':
125                         switch (state) {
126                         case NONE:
127                                 break;
128                         case NUM:
129                                 BIT_SET(size, curnum, mask);
130                                 state = NONE;
131                                 break;
132                         case DASH:
133                                 goto parserr;
134                                 break;
135                         }
136                         break;
137                 case '-':
138                         if (state != NUM)
139                                 goto parserr;
140                         state = DASH;
141                         break;
142                 default:
143                         goto parserr;
144                 }
145                 l++;
146         }
147         switch (state) {
148                 case NONE:
149                         break;
150                 case NUM:
151                         BIT_SET(size, curnum, mask);
152                         break;
153                 case DASH:
154                         goto parserr;
155         }
156         return;
157 parserr:
158         errx(EXIT_FAILURE, "Malformed list %s", list);
159 }
160
161 static void
162 parsecpulist(char *list, cpuset_t *mask)
163 {
164
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");
169                 return;
170         }
171         parselist(list, (struct bitset *)mask, CPU_SETSIZE);
172 }
173
174 /*
175  * permissively parse policy:domain list
176  * allow:
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
182  */
183 static void
184 parsedomainlist(char *list, domainset_t *mask, int *policyp)
185 {
186         domainset_t rootmask;
187         struct numa_policy *policy;
188         char *l;
189         int p;
190
191         /*
192          * Use the rootset's policy as the default for unspecified policies.
193          */
194         if (cpuset_getdomain(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
195             sizeof(rootmask), &rootmask, &p) != 0)
196                 err(EXIT_FAILURE, "getdomain");
197
198         l = list;
199         for (policy = &policies[0]; policy->name != NULL; policy++) {
200                 if (strncasecmp(l, policy->name, strlen(policy->name)) == 0) {
201                         p = policy->policy;
202                         l += strlen(policy->name);
203                         if (*l != ':' && *l != '\0')
204                                 errx(EXIT_FAILURE, "Malformed list %s", list);
205                         if (*l == ':')
206                                 l++;
207                         break;
208                 }
209         }
210         *policyp = p;
211         if (strcasecmp(l, "all") == 0 || *l == '\0') {
212                 DOMAINSET_COPY(&rootmask, mask);
213                 return;
214         }
215         parselist(l, (struct bitset *)mask, DOMAINSET_SETSIZE);
216 }
217
218 static void
219 printset(struct bitset *mask, int size)
220 {
221         int once;
222         int bit;
223
224         for (once = 0, bit = 0; bit < size; bit++) {
225                 if (BIT_ISSET(size, bit, mask)) {
226                         if (once == 0) {
227                                 printf("%d", bit);
228                                 once = 1;
229                         } else
230                                 printf(", %d", bit);
231                 }
232         }
233         printf("\n");
234 }
235
236 static const char *whichnames[] = { NULL, "tid", "pid", "cpuset", "irq", "jail",
237                                     "domain" };
238 static const char *levelnames[] = { NULL, " root", " cpuset", "" };
239 static const char *policynames[] = { "invalid", "round-robin", "first-touch",
240                                     "prefer" };
241
242 static void
243 printaffinity(void)
244 {
245         domainset_t domain;
246         cpuset_t mask;
247         int policy;
248
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,
252             levelnames[level]);
253         printset((struct bitset *)&mask, CPU_SETSIZE);
254         if (dflag)
255                 goto out;
256         if (cpuset_getdomain(level, which, id, sizeof(domain), &domain,
257             &policy) != 0)
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);
262 out:
263         exit(EXIT_SUCCESS);
264 }
265
266 static void
267 printsetid(void)
268 {
269         cpusetid_t setid;
270
271         /*
272          * Only LEVEL_WHICH && WHICH_CPUSET has a numbered id.
273          */
274         if (level == CPU_LEVEL_WHICH && !sflag)
275                 level = CPU_LEVEL_CPUSET;
276         if (cpuset_getid(level, which, id, &setid))
277                 err(errno, "getid");
278         printf("%s %jd%s id: %d\n", whichnames[which], (intmax_t)id,
279             levelnames[level], setid);
280 }
281
282 int
283 main(int argc, char *argv[])
284 {
285         domainset_t domains;
286         cpusetid_t setid;
287         cpuset_t mask;
288         int policy;
289         lwpid_t tid;
290         pid_t pid;
291         int ch;
292
293         CPU_ZERO(&mask);
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) {
300                 switch (ch) {
301                 case 'C':
302                         Cflag = 1;
303                         break;
304                 case 'c':
305                         cflag = 1;
306                         level = CPU_LEVEL_CPUSET;
307                         break;
308                 case 'd':
309                         dflag = 1;
310                         which = CPU_WHICH_DOMAIN;
311                         id = atoi(optarg);
312                         break;
313                 case 'g':
314                         gflag = 1;
315                         break;
316                 case 'i':
317                         iflag = 1;
318                         break;
319                 case 'j':
320                         jflag = 1;
321                         which = CPU_WHICH_JAIL;
322                         id = atoi(optarg);
323                         break;
324                 case 'l':
325                         lflag = 1;
326                         parsecpulist(optarg, &mask);
327                         break;
328                 case 'n':
329                         nflag = 1;
330                         parsedomainlist(optarg, &domains, &policy);
331                         break;
332                 case 'p':
333                         pflag = 1;
334                         which = CPU_WHICH_PID;
335                         id = pid = atoi(optarg);
336                         break;
337                 case 'r':
338                         level = CPU_LEVEL_ROOT;
339                         rflag = 1;
340                         break;
341                 case 's':
342                         sflag = 1;
343                         which = CPU_WHICH_CPUSET;
344                         id = setid = atoi(optarg);
345                         break;
346                 case 't':
347                         tflag = 1;
348                         which = CPU_WHICH_TID;
349                         id = tid = atoi(optarg);
350                         break;
351                 case 'x':
352                         xflag = 1;
353                         which = CPU_WHICH_IRQ;
354                         id = atoi(optarg);
355                         break;
356                 default:
357                         usage();
358                 }
359         }
360         argc -= optind;
361         argv += optind;
362         if (gflag) {
363                 if (argc || Cflag || lflag || nflag)
364                         usage();
365                 /* Only one identity specifier. */
366                 if (dflag + jflag + xflag + sflag + pflag + tflag > 1)
367                         usage();
368                 if (iflag)
369                         printsetid();
370                 else
371                         printaffinity();
372                 exit(EXIT_SUCCESS);
373         }
374
375         if (dflag || iflag || rflag)
376                 usage();
377         /*
378          * The user wants to run a command with a set and possibly cpumask.
379          */
380         if (argc) {
381                 if (Cflag || pflag || tflag || xflag || jflag)
382                         usage();
383                 if (sflag) {
384                         if (cpuset_setid(CPU_WHICH_PID, -1, setid))
385                                 err(argc, "setid");
386                 } else {
387                         if (cpuset(&setid))
388                                 err(argc, "newid");
389                 }
390                 if (lflag) {
391                         if (cpuset_setaffinity(level, CPU_WHICH_PID,
392                             -1, sizeof(mask), &mask) != 0)
393                                 err(EXIT_FAILURE, "setaffinity");
394                 }
395                 if (nflag) {
396                         if (cpuset_setdomain(level, CPU_WHICH_PID,
397                             -1, sizeof(domains), &domains, policy) != 0)
398                                 err(EXIT_FAILURE, "setdomain");
399                 }
400                 errno = 0;
401                 execvp(*argv, argv);
402                 err(errno == ENOENT ? 127 : 126, "%s", *argv);
403         }
404         /*
405          * We're modifying something that presently exists.
406          */
407         if (Cflag && (jflag || !pflag || sflag || tflag || xflag))
408                 usage();
409         if ((!lflag && !nflag) && cflag)
410                 usage();
411         if ((!lflag && !nflag) && !(Cflag || sflag))
412                 usage();
413         /* You can only set a mask on a thread. */
414         if (tflag && (sflag | pflag | xflag | jflag))
415                 usage();
416         /* You can only set a mask on an irq. */
417         if (xflag && (jflag | pflag | sflag | tflag))
418                 usage();
419         if (Cflag) {
420                 /*
421                  * Create a new cpuset and move the specified process
422                  * into the set.
423                  */
424                 if (cpuset(&setid) < 0)
425                         err(EXIT_FAILURE, "newid");
426                 sflag = 1;
427         }
428         if (pflag && sflag) {
429                 if (cpuset_setid(CPU_WHICH_PID, pid, setid))
430                         err(EXIT_FAILURE, "setid");
431                 /*
432                  * If the user specifies a set and a list we want the mask
433                  * to effect the pid and not the set.
434                  */
435                 which = CPU_WHICH_PID;
436                 id = pid;
437         }
438         if (lflag) {
439                 if (cpuset_setaffinity(level, which, id, sizeof(mask),
440                     &mask) != 0)
441                         err(EXIT_FAILURE, "setaffinity");
442         }
443         if (nflag) {
444                 if (cpuset_setdomain(level, which, id, sizeof(domains),
445                     &domains, policy) != 0)
446                         err(EXIT_FAILURE, "setdomain");
447         }
448
449         exit(EXIT_SUCCESS);
450 }
451
452 static void
453 usage(void)
454 {
455
456         fprintf(stderr,
457             "usage: cpuset [-l cpu-list] [-s setid] cmd ...\n");
458         fprintf(stderr,
459             "       cpuset [-l cpu-list] [-s setid] -p pid\n");
460         fprintf(stderr,
461             "       cpuset [-c] [-l cpu-list] -C -p pid\n");
462         fprintf(stderr,
463             "       cpuset [-c] [-l cpu-list] [-j jailid | -p pid | -t tid | -s setid | -x irq]\n");
464         fprintf(stderr,
465             "       cpuset -g [-cir] [-d domain | -j jailid | -p pid | -t tid | -s setid |\n"
466             "              -x irq]\n");
467         exit(1);
468 }