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