xref: /netbsd-src/usr.sbin/schedctl/schedctl.c (revision 56a34939419542e88b386b2229be7565f4f45461)
1 /*	$NetBSD: schedctl.c,v 1.12 2008/10/18 03:40:18 rmind Exp $	*/
2 
3 /*
4  * Copyright (c) 2008, Mindaugas Rasiukevicius <rmind at NetBSD org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * schedctl(8) - a program to control scheduling of processes and threads.
31  */
32 
33 #include <sys/cdefs.h>
34 
35 #ifndef lint
36 __RCSID("$NetBSD: schedctl.c,v 1.12 2008/10/18 03:40:18 rmind Exp $");
37 #endif
38 
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 
43 #include <err.h>
44 #include <fcntl.h>
45 #include <kvm.h>
46 #include <unistd.h>
47 
48 #include <sys/pset.h>
49 #include <sys/sched.h>
50 #include <sys/sysctl.h>
51 #include <sys/types.h>
52 
53 static const char *class_str[] = {
54 	"SCHED_OTHER",
55 	"SCHED_FIFO",
56 	"SCHED_RR",
57 	NULL
58 };
59 
60 static void	sched_set(pid_t, lwpid_t, int, struct sched_param *, cpuset_t *);
61 static void	thread_info(pid_t, lwpid_t);
62 static cpuset_t	*makecpuset(char *);
63 static char	*showcpuset(cpuset_t *);
64 static void	usage(void);
65 
66 static u_int	ncpu;
67 
68 int
69 main(int argc, char **argv)
70 {
71 	kvm_t *kd;
72 	struct kinfo_lwp *lwp_list, *lwp;
73 	struct sched_param *sp;
74 	cpuset_t *cpuset;
75 	int i, count, ch, policy;
76 	pid_t pid;
77 	lwpid_t lid;
78 	bool set;
79 
80 	ncpu = sysconf(_SC_NPROCESSORS_CONF);
81 
82 	pid = lid = 0;
83 	cpuset = NULL;
84 	set = false;
85 
86 	sp = calloc(1, sizeof(struct sched_param));
87 	if (sp == NULL)
88 		err(EXIT_FAILURE, "calloc");
89 
90 	sp->sched_priority = PRI_NONE;
91 	policy = SCHED_NONE;
92 
93 	while ((ch = getopt(argc, argv, "A:C:P:p:t:")) != -1) {
94 		switch (ch) {
95 		case 'p':
96 			/* PID */
97 			pid = atoi(optarg);
98 			break;
99 		case 't':
100 			/* Thread (LWP) ID */
101 			lid = atoi(optarg);
102 			break;
103 		case 'A':
104 			/* Affinity */
105 			cpuset = makecpuset(optarg);
106 			if (cpuset == NULL) {
107 				fprintf(stderr, "%s: invalid CPU value\n",
108 				    getprogname());
109 				exit(EXIT_FAILURE);
110 			}
111 			break;
112 		case 'C':
113 			/* Scheduling class */
114 			for (policy = 0; class_str[policy] != NULL; policy++) {
115 				if (strcasecmp(optarg, class_str[policy]) == 0)
116 					break;
117 			}
118 			if (class_str[policy] == NULL)
119 				policy = atoi(optarg);
120 			if (policy < SCHED_OTHER || policy > SCHED_RR) {
121 				fprintf(stderr,
122 				    "%s: invalid scheduling class\n",
123 				    getprogname());
124 				exit(EXIT_FAILURE);
125 			}
126 			set = true;
127 			break;
128 		case 'P':
129 			/* Priority */
130 			sp->sched_priority = atoi(optarg);
131 			if (sp->sched_priority < sysconf(_SC_SCHED_PRI_MIN) ||
132 			    sp->sched_priority > sysconf(_SC_SCHED_PRI_MAX)) {
133 				fprintf(stderr, "%s: invalid priority\n",
134 				    getprogname());
135 				exit(EXIT_FAILURE);
136 			}
137 			set = true;
138 			break;
139 		default:
140 			usage();
141 		}
142 	}
143 
144 	/* At least PID must be specified */
145 	if (pid == 0) {
146 		if (argv[optind] == NULL || lid != 0)
147 			usage();
148 		pid = getpid();
149 	} else {
150 		if (argv[optind] != NULL)
151 			usage();
152 	}
153 
154 	/* Set the scheduling information for thread/process */
155 	sched_set(pid, lid, policy, set ? sp : NULL, cpuset);
156 
157 	/* Show information about each thread */
158 	if (pid != getpid()) {
159 		kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "kvm_open");
160 		if (kd == NULL)
161 			err(EXIT_FAILURE, "kvm_open");
162 		lwp_list = kvm_getlwps(kd, pid, 0, sizeof(struct kinfo_lwp), &count);
163 		if (lwp_list == NULL)
164 			err(EXIT_FAILURE, "kvm_getlwps");
165 		for (lwp = lwp_list, i = 0; i < count; lwp++, i++) {
166 			if (lid && lid != lwp->l_lid)
167 				continue;
168 			thread_info(pid, lwp->l_lid);
169 		}
170 		kvm_close(kd);
171 		free(sp);
172 		cpuset_destroy(cpuset);
173 		return 0;
174 	}
175 
176 	(void)execvp(argv[optind], argv + optind);
177 	err(EXIT_FAILURE, "execvp");
178 }
179 
180 static void
181 sched_set(pid_t pid, lwpid_t lid, int policy,
182     struct sched_param *sp, cpuset_t *cpuset)
183 {
184 	int error;
185 
186 	if (sp) {
187 		/* Set the scheduling parameters for the thread */
188 		error = _sched_setparam(pid, lid, policy, sp);
189 		if (error < 0)
190 			err(EXIT_FAILURE, "_sched_setparam");
191 	}
192 	if (cpuset) {
193 		/* Set the CPU-set for affinity */
194 		error = _sched_setaffinity(pid, lid,
195 		    cpuset_size(cpuset), cpuset);
196 		if (error < 0)
197 			err(EXIT_FAILURE, "_sched_setaffinity");
198 	}
199 }
200 
201 static void
202 thread_info(pid_t pid, lwpid_t lid)
203 {
204 	struct sched_param sp;
205 	cpuset_t *cpuset;
206 	char *cpus;
207 	int error, policy;
208 
209 	cpuset = cpuset_create();
210 	if (cpuset == NULL)
211 		err(EXIT_FAILURE, "cpuset_create");
212 
213 	error = _sched_getparam(pid, lid, &policy, &sp);
214 	if (error < 0)
215 		err(EXIT_FAILURE, "_sched_getparam");
216 
217 	error = _sched_getaffinity(pid, lid, cpuset_size(cpuset), cpuset);
218 	if (error < 0)
219 		err(EXIT_FAILURE, "_sched_getaffinity");
220 
221 	printf("  LID:              %d\n", lid);
222 	printf("  Priority:         %d\n", sp.sched_priority);
223 	printf("  Class:            %s\n", class_str[policy]);
224 
225 	cpus = showcpuset(cpuset);
226 	printf("  Affinity (CPUs):  %s\n", cpus);
227 	free(cpus);
228 
229 	cpuset_destroy(cpuset);
230 }
231 
232 static cpuset_t *
233 makecpuset(char *str)
234 {
235 	cpuset_t *cpuset;
236 	char *cpustr, *s;
237 
238 	if (str == NULL)
239 		return NULL;
240 
241 	cpuset = cpuset_create();
242 	if (cpuset == NULL)
243 		err(EXIT_FAILURE, "cpuset_create");
244 	cpuset_zero(cpuset);
245 
246 	cpustr = strdup(str);
247 	if (cpustr == NULL)
248 		err(EXIT_FAILURE, "strdup");
249 	s = cpustr;
250 
251 	while (s != NULL) {
252 		char *p;
253 		int i;
254 
255 		/* Get the CPU number and validate the range */
256 		p = strsep(&s, ",");
257 		if (p == NULL) {
258 			cpuset_destroy(cpuset);
259 			cpuset = NULL;
260 			break;
261 		}
262 		i = atoi(p);
263 		if (i == -1) {
264 			cpuset_zero(cpuset);
265 			break;
266 		}
267 		if ((unsigned int)i >= ncpu) {
268 			cpuset_destroy(cpuset);
269 			cpuset = NULL;
270 			break;
271 		}
272 
273 		/* Set the bit */
274 		cpuset_set(i, cpuset);
275 	}
276 
277 	free(cpustr);
278 	return cpuset;
279 }
280 
281 static char *
282 showcpuset(cpuset_t *cpuset)
283 {
284 	char *buf;
285 	size_t size;
286 	int i;
287 
288 	size = 3 * ncpu;	/* XXX */
289 	buf = malloc(size + 1);
290 	if (buf == NULL)
291 		err(EXIT_FAILURE, "malloc");
292 	memset(buf, '\0', size + 1);
293 
294 	for (i = 0; i < ncpu; i++)
295 		if (cpuset_isset(i, cpuset))
296 			snprintf(buf, size, "%s%d,", buf, i);
297 
298 	i = strlen(buf);
299 	if (i != 0) {
300 		buf[i - 1] = '\0';
301 	} else {
302 		strncpy(buf, "<none>", size);
303 	}
304 
305 	return buf;
306 }
307 
308 static void
309 usage(void)
310 {
311 
312 	fprintf(stderr, "usage: %s [-A processor] [-C class] "
313 	    "[-P priority] [-t lid] {-p pid|command}\n", getprogname());
314 	exit(EXIT_FAILURE);
315 }
316