aboutsummaryrefslogtreecommitdiff
path: root/bin/cpuset/cpuset.c
blob: 43aadb33f1e06ffcf05adbe7f9713ad54152a58c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2007, 2008 	Jeffrey Roberson <jeff@freebsd.org>
 * All rights reserved.
 *
 * Copyright (c) 2008 Nokia Corporation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#define _WANT_FREEBSD_BITSET

#include <sys/param.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/cpuset.h>
#include <sys/domainset.h>

#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <jail.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>

static int Cflag;
static int cflag;
static int dflag;
static int gflag;
static int iflag;
static int jflag;
static int lflag;
static int nflag;
static int pflag;
static int rflag;
static int sflag;
static int tflag;
static int xflag;
static id_t id;
static cpulevel_t level;
static cpuwhich_t which;

static void usage(void) __dead2;

struct numa_policy {
	const char 	*name;
	int		policy;
};

static struct numa_policy policies[] = {
	{ "round-robin", DOMAINSET_POLICY_ROUNDROBIN },
	{ "rr", DOMAINSET_POLICY_ROUNDROBIN },
	{ "first-touch", DOMAINSET_POLICY_FIRSTTOUCH },
	{ "ft", DOMAINSET_POLICY_FIRSTTOUCH },
	{ "prefer", DOMAINSET_POLICY_PREFER },
	{ "interleave", DOMAINSET_POLICY_INTERLEAVE},
	{ "il", DOMAINSET_POLICY_INTERLEAVE},
	{ NULL, DOMAINSET_POLICY_INVALID }
};

static void printset(struct bitset *mask, int size);

static void
parselist(char *list, struct bitset *mask, int size)
{
	enum { NONE, NUM, DASH } state;
	int lastnum;
	int curnum;
	char *l;

	state = NONE;
	curnum = lastnum = 0;
	for (l = list; *l != '\0';) {
		if (isdigit(*l)) {
			curnum = atoi(l);
			if (curnum >= size)
				errx(EXIT_FAILURE,
				    "List entry %d exceeds maximum of %d",
				    curnum, size - 1);
			while (isdigit(*l))
				l++;
			switch (state) {
			case NONE:
				lastnum = curnum;
				state = NUM;
				break;
			case DASH:
				for (; lastnum <= curnum; lastnum++)
					BIT_SET(size, lastnum, mask);
				state = NONE;
				break;
			case NUM:
			default:
				goto parserr;
			}
			continue;
		}
		switch (*l) {
		case ',':
			switch (state) {
			case NONE:
				break;
			case NUM:
				BIT_SET(size, curnum, mask);
				state = NONE;
				break;
			case DASH:
				goto parserr;
				break;
			}
			break;
		case '-':
			if (state != NUM)
				goto parserr;
			state = DASH;
			break;
		default:
			goto parserr;
		}
		l++;
	}
	switch (state) {
		case NONE:
			break;
		case NUM:
			BIT_SET(size, curnum, mask);
			break;
		case DASH:
			goto parserr;
	}
	return;
parserr:
	errx(EXIT_FAILURE, "Malformed list %s", list);
}

static void
parsecpulist(char *list, cpuset_t *mask)
{

	if (strcasecmp(list, "all") == 0) {
		if (cpuset_getaffinity(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
		    sizeof(*mask), mask) != 0)
			err(EXIT_FAILURE, "getaffinity");
		return;
	}
	parselist(list, (struct bitset *)mask, CPU_SETSIZE);
}

/*
 * permissively parse policy:domain list
 * allow:
 *	round-robin:0-4		explicit
 *	round-robin:all		explicit root domains
 *	0-4			implicit root policy
 *	round-robin		implicit root domains
 *	all			explicit root domains and implicit policy
 */
static void
parsedomainlist(char *list, domainset_t *mask, int *policyp)
{
	domainset_t rootmask;
	struct numa_policy *policy;
	char *l;
	int p;

	/*
	 * Use the rootset's policy as the default for unspecified policies.
	 */
	if (cpuset_getdomain(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
	    sizeof(rootmask), &rootmask, &p) != 0)
		err(EXIT_FAILURE, "getdomain");

	l = list;
	for (policy = &policies[0]; policy->name != NULL; policy++) {
		if (strncasecmp(l, policy->name, strlen(policy->name)) == 0) {
			p = policy->policy;
			l += strlen(policy->name);
			if (*l != ':' && *l != '\0')
				errx(EXIT_FAILURE, "Malformed list %s", list);
			if (*l == ':')
				l++;
			break;
		}
	}
	*policyp = p;
	if (strcasecmp(l, "all") == 0 || *l == '\0') {
		DOMAINSET_COPY(&rootmask, mask);
		return;
	}
	parselist(l, (struct bitset *)mask, DOMAINSET_SETSIZE);
}

static void
printset(struct bitset *mask, int size)
{
	int once;
	int bit;

	for (once = 0, bit = 0; bit < size; bit++) {
		if (BIT_ISSET(size, bit, mask)) {
			if (once == 0) {
				printf("%d", bit);
				once = 1;
			} else
				printf(", %d", bit);
		}
	}
	printf("\n");
}

static const char *whichnames[] = { NULL, "tid", "pid", "cpuset", "irq", "jail",
				    "domain" };
static const char *levelnames[] = { NULL, " root", " cpuset", "" };
static const char *policynames[] = { "invalid", "round-robin", "first-touch",
				    "prefer", "interleave" };

static void
printaffinity(void)
{
	domainset_t domain;
	cpuset_t mask;
	int policy;

	if (cpuset_getaffinity(level, which, id, sizeof(mask), &mask) != 0)
		err(EXIT_FAILURE, "getaffinity");
	printf("%s %jd%s mask: ", whichnames[which], (intmax_t)id,
	    levelnames[level]);
	printset((struct bitset *)&mask, CPU_SETSIZE);
	if (dflag || xflag)
		goto out;
	if (cpuset_getdomain(level, which, id, sizeof(domain), &domain,
	    &policy) != 0)
		err(EXIT_FAILURE, "getdomain");
	printf("%s %jd%s domain policy: %s mask: ", whichnames[which],
	    (intmax_t)id, levelnames[level], policynames[policy]);
	printset((struct bitset *)&domain, DOMAINSET_SETSIZE);
out:
	exit(EXIT_SUCCESS);
}

static void
printsetid(void)
{
	cpusetid_t setid;

	/*
	 * Only LEVEL_WHICH && WHICH_CPUSET has a numbered id.
	 */
	if (level == CPU_LEVEL_WHICH && !sflag)
		level = CPU_LEVEL_CPUSET;
	if (cpuset_getid(level, which, id, &setid))
		err(errno, "getid");
	printf("%s %jd%s id: %d\n", whichnames[which], (intmax_t)id,
	    levelnames[level], setid);
}

int
main(int argc, char *argv[])
{
	domainset_t domains;
	cpusetid_t setid;
	cpuset_t mask;
	int policy;
	lwpid_t tid;
	pid_t pid;
	int ch;

	CPU_ZERO(&mask);
	DOMAINSET_ZERO(&domains);
	policy = DOMAINSET_POLICY_INVALID;
	level = CPU_LEVEL_WHICH;
	which = CPU_WHICH_PID;
	id = pid = tid = setid = -1;
	while ((ch = getopt(argc, argv, "Ccd:gij:l:n:p:rs:t:x:")) != -1) {
		switch (ch) {
		case 'C':
			Cflag = 1;
			break;
		case 'c':
			cflag = 1;
			level = CPU_LEVEL_CPUSET;
			break;
		case 'd':
			dflag = 1;
			which = CPU_WHICH_DOMAIN;
			id = atoi(optarg);
			break;
		case 'g':
			gflag = 1;
			break;
		case 'i':
			iflag = 1;
			break;
		case 'j':
			jflag = 1;
			which = CPU_WHICH_JAIL;
			id = jail_getid(optarg);
			if (id < 0)
				errx(EXIT_FAILURE, "%s", jail_errmsg);
			break;
		case 'l':
			lflag = 1;
			parsecpulist(optarg, &mask);
			break;
		case 'n':
			nflag = 1;
			parsedomainlist(optarg, &domains, &policy);
			break;
		case 'p':
			pflag = 1;
			which = CPU_WHICH_PID;
			id = pid = atoi(optarg);
			break;
		case 'r':
			level = CPU_LEVEL_ROOT;
			rflag = 1;
			break;
		case 's':
			sflag = 1;
			which = CPU_WHICH_CPUSET;
			id = setid = atoi(optarg);
			break;
		case 't':
			tflag = 1;
			which = CPU_WHICH_TID;
			id = tid = atoi(optarg);
			break;
		case 'x':
			xflag = 1;
			which = CPU_WHICH_IRQ;
			id = atoi(optarg);
			break;
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;
	if (gflag) {
		if (argc || Cflag || lflag || nflag)
			usage();
		/* Only one identity specifier. */
		if (dflag + jflag + xflag + sflag + pflag + tflag > 1)
			usage();
		if (iflag)
			printsetid();
		else
			printaffinity();
		exit(EXIT_SUCCESS);
	}

	if (dflag || iflag || rflag)
		usage();
	/*
	 * The user wants to run a command with a set and possibly cpumask.
	 */
	if (argc) {
		if (Cflag || pflag || tflag || xflag || jflag)
			usage();
		if (sflag) {
			if (cpuset_setid(CPU_WHICH_PID, -1, setid))
				err(argc, "setid");
		} else {
			if (cpuset(&setid))
				err(argc, "newid");
		}
		if (lflag) {
			if (cpuset_setaffinity(level, CPU_WHICH_PID,
			    -1, sizeof(mask), &mask) != 0)
				err(EXIT_FAILURE, "setaffinity");
		}
		if (nflag) {
			if (cpuset_setdomain(level, CPU_WHICH_PID,
			    -1, sizeof(domains), &domains, policy) != 0)
				err(EXIT_FAILURE, "setdomain");
		}
		errno = 0;
		execvp(*argv, argv);
		err(errno == ENOENT ? 127 : 126, "%s", *argv);
	}
	/*
	 * We're modifying something that presently exists.
	 */
	if (Cflag && (jflag || !pflag || sflag || tflag || xflag))
		usage();
	if ((!lflag && !nflag) && cflag)
		usage();
	if ((!lflag && !nflag) && !(Cflag || sflag))
		usage();
	/* You can only set a mask on a thread. */
	if (tflag && (sflag | pflag | xflag | jflag))
		usage();
	/* You can only set a mask on an irq. */
	if (xflag && (jflag | pflag | sflag | tflag))
		usage();
	if (Cflag) {
		/*
		 * Create a new cpuset and move the specified process
		 * into the set.
		 */
		if (cpuset(&setid) < 0)
			err(EXIT_FAILURE, "newid");
		sflag = 1;
	}
	if (pflag && sflag) {
		if (cpuset_setid(CPU_WHICH_PID, pid, setid))
			err(EXIT_FAILURE, "setid");
		/*
		 * If the user specifies a set and a list we want the mask
		 * to effect the pid and not the set.
		 */
		which = CPU_WHICH_PID;
		id = pid;
	}
	if (lflag) {
		if (cpuset_setaffinity(level, which, id, sizeof(mask),
		    &mask) != 0)
			err(EXIT_FAILURE, "setaffinity");
	}
	if (nflag) {
		if (cpuset_setdomain(level, which, id, sizeof(domains),
		    &domains, policy) != 0)
			err(EXIT_FAILURE, "setdomain");
	}

	exit(EXIT_SUCCESS);
}

static void
usage(void)
{

	fprintf(stderr,
    "usage: cpuset [-l cpu-list] [-n policy:domain-list] [-s setid] cmd ...\n");
	fprintf(stderr,
    "       cpuset [-l cpu-list] [-n policy:domain-list] [-s setid] -p pid\n");
	fprintf(stderr,
    "       cpuset [-c] [-l cpu-list] [-n policy:domain-list] -C -p pid\n");
	fprintf(stderr,
    "       cpuset [-c] [-l cpu-list] [-n policy:domain-list]\n"
    "              [-j jailid | -p pid | -t tid | -s setid | -x irq]\n");
	fprintf(stderr,
    "       cpuset -g [-cir]\n"
    "              [-d domain | -j jailid | -p pid | -t tid | -s setid | -x irq]\n");
	exit(1);
}