schedctl.c revision 1.1 1 /* $NetBSD: schedctl.c,v 1.1 2008/01/15 03:37:15 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 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.1 2008/01/15 03:37:15 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/param.h>
49 #include <sys/pset.h>
50 #include <sys/sched.h>
51 #include <sys/sysctl.h>
52 #include <sys/types.h>
53
54 static const char *class_str[] = {
55 "SCHED_OTHER",
56 "SCHED_FIFO",
57 "SCHED_RR"
58 };
59
60 static void sched_set(pid_t, lwpid_t, 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 int
67 main(int argc, char **argv)
68 {
69 kvm_t *kd;
70 struct kinfo_lwp *lwp_list, *lwp;
71 struct sched_param *sp;
72 cpuset_t *cpuset;
73 int i, count, ch;
74 pid_t pid;
75 lwpid_t lid;
76 bool set;
77
78 pid = lid = 0;
79 cpuset = NULL;
80 set = false;
81
82 sp = malloc(sizeof(struct sched_param));
83 if (sp == NULL)
84 err(EXIT_FAILURE, "malloc");
85
86 memset(sp, 0, sizeof(struct sched_param));
87 sp->sched_class = SCHED_NONE;
88 sp->sched_priority = PRI_NONE;
89
90 while ((ch = getopt(argc, argv, "A:C:P:p:t:")) != -1) {
91 switch (ch) {
92 case 'p':
93 /* PID */
94 pid = atoi(optarg);
95 break;
96 case 't':
97 /* Thread (LWP) ID */
98 lid = atoi(optarg);
99 break;
100 case 'A':
101 /* Affinity */
102 cpuset = makecpuset(optarg);
103 if (cpuset == NULL) {
104 fprintf(stderr, "%s: invalid CPU value\n",
105 getprogname());
106 exit(EXIT_FAILURE);
107 }
108 break;
109 case 'C':
110 /* Scheduling class */
111 sp->sched_class = atoi(optarg);
112 if (sp->sched_class < SCHED_OTHER ||
113 sp->sched_class > SCHED_RR) {
114 fprintf(stderr,
115 "%s: invalid scheduling class\n",
116 getprogname());
117 exit(EXIT_FAILURE);
118 }
119 set = true;
120 break;
121 case 'P':
122 /* Priority */
123 sp->sched_priority = atoi(optarg);
124 if (sp->sched_priority < sysconf(_SC_SCHED_PRI_MIN) ||
125 sp->sched_priority > sysconf(_SC_SCHED_PRI_MAX)) {
126 fprintf(stderr, "%s: invalid priority\n",
127 getprogname());
128 exit(EXIT_FAILURE);
129 }
130 set = true;
131 break;
132 default:
133 usage();
134 }
135 }
136
137 /* At least PID must be specified */
138 if (pid == 0)
139 usage();
140
141 /* Set the scheduling information for thread/process */
142 sched_set(pid, lid, set ? sp : NULL, cpuset);
143
144 /* Show information about each thread */
145 kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "kvm_open");
146 if (kd == NULL)
147 err(EXIT_FAILURE, "kvm_open");
148 lwp_list = kvm_getlwps(kd, pid, 0, sizeof(struct kinfo_lwp), &count);
149 if (lwp_list == NULL)
150 err(EXIT_FAILURE, "kvm_getlwps");
151 for (lwp = lwp_list, i = 0; i < count; lwp++, i++) {
152 if (lid && lid != lwp->l_lid)
153 continue;
154 thread_info(pid, lwp->l_lid);
155 }
156 kvm_close(kd);
157
158 free(sp);
159 free(cpuset);
160 return 0;
161 }
162
163 static void
164 sched_set(pid_t pid, lwpid_t lid, struct sched_param *sp, cpuset_t *cpuset)
165 {
166 int error;
167
168 if (sp) {
169 /* Set the scheduling parameters for the thread */
170 error = _sched_setparam(pid, lid, sp);
171 if (error < 0)
172 err(EXIT_FAILURE, "_sched_setparam");
173 }
174 if (cpuset) {
175 /* Set the CPU-set for affinity */
176 error = _sched_setaffinity(pid, lid,
177 sizeof(cpuset_t), cpuset);
178 if (error < 0)
179 err(EXIT_FAILURE, "_sched_setaffinity");
180 }
181 }
182
183 static void
184 thread_info(pid_t pid, lwpid_t lid)
185 {
186 struct sched_param sp;
187 cpuset_t *cpuset;
188 char *cpus;
189 int error;
190
191 cpuset = malloc(sizeof(cpuset_t));
192 if (cpuset == NULL)
193 err(EXIT_FAILURE, "malloc");
194
195 error = _sched_getparam(pid, lid, &sp);
196 if (error < 0)
197 err(EXIT_FAILURE, "_sched_getparam");
198
199 error = _sched_getaffinity(pid, lid, sizeof(cpuset_t), cpuset);
200 if (error < 0)
201 err(EXIT_FAILURE, "_sched_getaffinity");
202
203 printf(" LID: %d\n", lid);
204 printf(" Priority: %d\n", sp.sched_priority);
205 printf(" Class: %s\n", class_str[sp.sched_class]);
206
207 cpus = showcpuset(cpuset);
208 printf(" Affinity (CPUs): %s\n", cpus);
209 free(cpus);
210
211 free(cpuset);
212 }
213
214 static cpuset_t *
215 makecpuset(char *str)
216 {
217 cpuset_t *cpuset;
218 char *cpustr, *s;
219
220 if (str == NULL)
221 return NULL;
222
223 cpuset = malloc(sizeof(cpuset_t));
224 if (cpuset == NULL)
225 err(EXIT_FAILURE, "malloc");
226 memset(cpuset, 0, sizeof(cpuset_t));
227
228 cpustr = strdup(str);
229 if (cpustr == NULL)
230 err(EXIT_FAILURE, "strdup");
231 s = cpustr;
232
233 while (s != NULL) {
234 char *p;
235 int i;
236
237 /* Get the CPU number and validate the range */
238 p = strsep(&s, ",");
239 if (p == NULL) {
240 free(cpuset);
241 cpuset = NULL;
242 break;
243 }
244 i = atoi(p);
245 if (i == -1) {
246 memset(cpuset, 0, sizeof(cpuset_t));
247 break;
248 }
249 if ((unsigned int)i > MAXCPUS) {
250 free(cpuset);
251 cpuset = NULL;
252 break;
253 }
254
255 /* Set the bit */
256 CPU_SET(i, cpuset);
257 }
258
259 free(cpustr);
260 return cpuset;
261 }
262
263 static char *
264 showcpuset(cpuset_t *cpuset)
265 {
266 char *buf;
267 size_t size;
268 int i;
269
270 size = 3 * MAXCPUS; /* XXX */
271 buf = malloc(size + 1);
272 if (cpuset == NULL)
273 err(EXIT_FAILURE, "malloc");
274 memset(buf, '\0', size + 1);
275
276 for (i = 0; i < MAXCPUS; i++)
277 if (CPU_ISSET(i, cpuset))
278 snprintf(buf, size, "%s%d,", buf, i);
279
280 i = strlen(buf);
281 if (i != 0) {
282 buf[i - 1] = '\0';
283 } else {
284 strncpy(buf, "<none>", size);
285 }
286
287 return buf;
288 }
289
290 static void
291 usage(void)
292 {
293 const char *progname = getprogname();
294
295 fprintf(stderr, "usage: %s -p pid [ -t lid ] [ -A processor ]\n"
296 "\t [ -C class ] [ -P priority ]\n", progname);
297 exit(EXIT_FAILURE);
298 }
299