xref: /freebsd/lib/libutil/cpuset.c (revision e7be843b4a162e68651d3911f0357ed464915629)
1 /*
2  * Copyright (c) 2007, 2008 	Jeffrey Roberson <jeff@freebsd.org>
3  * All rights reserved.
4  *
5  * Copyright (c) 2008 Nokia Corporation
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #define _WANT_FREEBSD_BITSET
32 
33 #include <sys/types.h>
34 #include <sys/cpuset.h>
35 #include <sys/domainset.h>
36 
37 #include <stdlib.h>
38 #include <string.h>
39 #include <libutil.h>
40 #include <ctype.h>
41 
42 struct numa_policy {
43 	const char 	*name;
44 	int		policy;
45 };
46 
47 static const struct numa_policy policies[] = {
48 	{ "round-robin", DOMAINSET_POLICY_ROUNDROBIN },
49 	{ "rr", DOMAINSET_POLICY_ROUNDROBIN },
50 	{ "first-touch", DOMAINSET_POLICY_FIRSTTOUCH },
51 	{ "ft", DOMAINSET_POLICY_FIRSTTOUCH },
52 	{ "prefer", DOMAINSET_POLICY_PREFER },
53 	{ "interleave", DOMAINSET_POLICY_INTERLEAVE},
54 	{ "il", DOMAINSET_POLICY_INTERLEAVE},
55 	{ NULL, DOMAINSET_POLICY_INVALID }
56 };
57 
58 static int
59 parselist(const char *list, struct bitset *mask, int size)
60 {
61 	enum { NONE, NUM, DASH } state;
62 	int lastnum;
63 	int curnum;
64 	const char *l;
65 
66 	state = NONE;
67 	curnum = lastnum = 0;
68 	for (l = list; *l != '\0';) {
69 		if (isdigit(*l)) {
70 			curnum = atoi(l);
71 			if (curnum >= size)
72 				return (CPUSET_PARSE_INVALID_CPU);
73 			while (isdigit(*l))
74 				l++;
75 			switch (state) {
76 			case NONE:
77 				lastnum = curnum;
78 				state = NUM;
79 				break;
80 			case DASH:
81 				for (; lastnum <= curnum; lastnum++)
82 					BIT_SET(size, lastnum, mask);
83 				state = NONE;
84 				break;
85 			case NUM:
86 			default:
87 				goto parserr;
88 			}
89 			continue;
90 		}
91 		switch (*l) {
92 		case ',':
93 			switch (state) {
94 			case NONE:
95 				break;
96 			case NUM:
97 				BIT_SET(size, curnum, mask);
98 				state = NONE;
99 				break;
100 			case DASH:
101 				goto parserr;
102 				break;
103 			}
104 			break;
105 		case '-':
106 			if (state != NUM)
107 				goto parserr;
108 			state = DASH;
109 			break;
110 		default:
111 			goto parserr;
112 		}
113 		l++;
114 	}
115 	switch (state) {
116 		case NONE:
117 			break;
118 		case NUM:
119 			BIT_SET(size, curnum, mask);
120 			break;
121 		case DASH:
122 			goto parserr;
123 	}
124 	return (CPUSET_PARSE_OK);
125 parserr:
126 	return (CPUSET_PARSE_ERROR);
127 }
128 
129 /*
130  * permissively parse policy:domain list
131  * allow:
132  *	round-robin:0-4		explicit
133  *	round-robin:all		explicit root domains
134  *	0-4			implicit root policy
135  *	round-robin		implicit root domains
136  *	all			explicit root domains and implicit policy
137  */
138 int
139 domainset_parselist(const char *list, domainset_t *mask, int *policyp)
140 {
141 	domainset_t rootmask;
142 	const struct numa_policy *policy;
143 	const char *l;
144 	int p;
145 
146 	/*
147 	 * Use the rootset's policy as the default for unspecified policies.
148 	 */
149 	if (cpuset_getdomain(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
150 	    sizeof(rootmask), &rootmask, &p) != 0)
151 		return (CPUSET_PARSE_GETDOMAIN);
152 
153 	if (list == NULL || strcasecmp(list, "all") == 0 || *list == '\0') {
154 		*policyp = p;
155 		DOMAINSET_COPY(&rootmask, mask);
156 		return (CPUSET_PARSE_OK);
157 	}
158 
159 	l = list;
160 	for (policy = &policies[0]; policy->name != NULL; policy++) {
161 		if (strncasecmp(l, policy->name, strlen(policy->name)) == 0) {
162 			p = policy->policy;
163 			l += strlen(policy->name);
164 			if (*l != ':' && *l != '\0')
165 				return (CPUSET_PARSE_ERROR);
166 			if (*l == ':')
167 				l++;
168 			break;
169 		}
170 	}
171 	*policyp = p;
172 
173 	return (parselist(l, (struct bitset *)mask, DOMAINSET_SETSIZE));
174 }
175 
176 int
177 cpuset_parselist(const char *list, cpuset_t *mask)
178 {
179 	if (strcasecmp(list, "all") == 0) {
180 		if (cpuset_getaffinity(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
181 		    sizeof(*mask), mask) != 0)
182 			return (CPUSET_PARSE_GETAFFINITY);
183 		return (CPUSET_PARSE_OK);
184 	}
185 
186 	return (parselist(list, (struct bitset *)mask, CPU_SETSIZE));
187 }
188