xref: /netbsd-src/usr.sbin/schedctl/schedctl.c (revision cd22f25e6f6d1cc1f197fe8c5468a80f51d1c4e1)
1 /*	$NetBSD: schedctl.c,v 1.6 2008/04/28 16:52:17 ad 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 COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF 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.6 2008/04/28 16:52:17 ad 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 		usage();
147 
148 	/* Set the scheduling information for thread/process */
149 	sched_set(pid, lid, policy, set ? sp : NULL, cpuset);
150 
151 	/* Show information about each thread */
152 	kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "kvm_open");
153 	if (kd == NULL)
154 		err(EXIT_FAILURE, "kvm_open");
155 	lwp_list = kvm_getlwps(kd, pid, 0, sizeof(struct kinfo_lwp), &count);
156 	if (lwp_list == NULL)
157 		err(EXIT_FAILURE, "kvm_getlwps");
158 	for (lwp = lwp_list, i = 0; i < count; lwp++, i++) {
159 		if (lid && lid != lwp->l_lid)
160 			continue;
161 		thread_info(pid, lwp->l_lid);
162 	}
163 	kvm_close(kd);
164 
165 	free(sp);
166 	free(cpuset);
167 	return 0;
168 }
169 
170 static void
171 sched_set(pid_t pid, lwpid_t lid, int policy,
172     struct sched_param *sp, cpuset_t *cpuset)
173 {
174 	int error;
175 
176 	if (sp) {
177 		/* Set the scheduling parameters for the thread */
178 		error = _sched_setparam(pid, lid, policy, sp);
179 		if (error < 0)
180 			err(EXIT_FAILURE, "_sched_setparam");
181 	}
182 	if (cpuset) {
183 		/* Set the CPU-set for affinity */
184 		error = _sched_setaffinity(pid, lid,
185 		    sizeof(cpuset_t), cpuset);
186 		if (error < 0)
187 			err(EXIT_FAILURE, "_sched_setaffinity");
188 	}
189 }
190 
191 static void
192 thread_info(pid_t pid, lwpid_t lid)
193 {
194 	struct sched_param sp;
195 	cpuset_t *cpuset;
196 	char *cpus;
197 	int error, policy;
198 
199 	cpuset = malloc(sizeof(cpuset_t));
200 	if (cpuset == NULL)
201 		err(EXIT_FAILURE, "malloc");
202 
203 	error = _sched_getparam(pid, lid, &policy, &sp);
204 	if (error < 0)
205 		err(EXIT_FAILURE, "_sched_getparam");
206 
207 	error = _sched_getaffinity(pid, lid, sizeof(cpuset_t), cpuset);
208 	if (error < 0)
209 		err(EXIT_FAILURE, "_sched_getaffinity");
210 
211 	printf("  LID:              %d\n", lid);
212 	printf("  Priority:         %d\n", sp.sched_priority);
213 	printf("  Class:            %s\n", class_str[policy]);
214 
215 	cpus = showcpuset(cpuset);
216 	printf("  Affinity (CPUs):  %s\n", cpus);
217 	free(cpus);
218 
219 	free(cpuset);
220 }
221 
222 static cpuset_t *
223 makecpuset(char *str)
224 {
225 	cpuset_t *cpuset;
226 	char *cpustr, *s;
227 
228 	if (str == NULL)
229 		return NULL;
230 
231 	cpuset = calloc(1, sizeof(cpuset_t));
232 	if (cpuset == NULL)
233 		err(EXIT_FAILURE, "malloc");
234 
235 	cpustr = strdup(str);
236 	if (cpustr == NULL)
237 		err(EXIT_FAILURE, "strdup");
238 	s = cpustr;
239 
240 	while (s != NULL) {
241 		char *p;
242 		int i;
243 
244 		/* Get the CPU number and validate the range */
245 		p = strsep(&s, ",");
246 		if (p == NULL) {
247 			free(cpuset);
248 			cpuset = NULL;
249 			break;
250 		}
251 		i = atoi(p);
252 		if (i == -1) {
253 			memset(cpuset, 0, sizeof(cpuset_t));
254 			break;
255 		}
256 		if ((unsigned int)i >= ncpu) {
257 			free(cpuset);
258 			cpuset = NULL;
259 			break;
260 		}
261 
262 		/* Set the bit */
263 		CPU_SET(i, cpuset);
264 	}
265 
266 	free(cpustr);
267 	return cpuset;
268 }
269 
270 static char *
271 showcpuset(cpuset_t *cpuset)
272 {
273 	char *buf;
274 	size_t size;
275 	int i;
276 
277 	size = 3 * ncpu;	/* XXX */
278 	buf = malloc(size + 1);
279 	if (buf == NULL)
280 		err(EXIT_FAILURE, "malloc");
281 	memset(buf, '\0', size + 1);
282 
283 	for (i = 0; i < ncpu; i++)
284 		if (CPU_ISSET(i, cpuset))
285 			snprintf(buf, size, "%s%d,", buf, i);
286 
287 	i = strlen(buf);
288 	if (i != 0) {
289 		buf[i - 1] = '\0';
290 	} else {
291 		strncpy(buf, "<none>", size);
292 	}
293 
294 	return buf;
295 }
296 
297 static void
298 usage(void)
299 {
300 	fprintf(stderr, "usage: %s -p pid [-t lid] [-A processor] "
301 	    "[-C class] [-P priority]\n", getprogname());
302 	exit(EXIT_FAILURE);
303 }
304