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