cpuctl.c revision 1.17 1 /* $NetBSD: cpuctl.c,v 1.17 2011/09/11 14:54:49 jdc Exp $ */
2
3 /*-
4 * Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef lint
33 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: cpuctl.c,v 1.17 2011/09/11 14:54:49 jdc Exp $");
35 #endif /* not lint */
36
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
39 #include <sys/uio.h>
40 #include <sys/cpuio.h>
41
42 #include <err.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <stdarg.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <util.h>
51 #include <time.h>
52 #include <sched.h>
53
54 #include "cpuctl.h"
55
56 static u_int getcpuid(char **);
57 __dead static void usage(void);
58
59 static void cpu_identify(char **);
60 static void cpu_list(char **);
61 static void cpu_offline(char **);
62 static void cpu_online(char **);
63 static void cpu_intr(char **);
64 static void cpu_nointr(char **);
65
66 static struct cmdtab {
67 const char *label;
68 int takesargs;
69 void (*func)(char **);
70 } const cpu_cmdtab[] = {
71 { "identify", 1, cpu_identify },
72 { "list", 0, cpu_list },
73 { "offline", 1, cpu_offline },
74 { "online", 1, cpu_online },
75 { "intr", 1, cpu_intr },
76 { "nointr", 1, cpu_nointr },
77 { NULL, 0, NULL },
78 };
79
80 static int fd;
81
82 int
83 main(int argc, char **argv)
84 {
85 const struct cmdtab *ct;
86
87 if (argc < 2)
88 usage();
89
90 if ((fd = open("/dev/cpuctl", O_RDWR)) < 0)
91 err(EXIT_FAILURE, "/dev/cpuctl");
92
93 for (ct = cpu_cmdtab; ct->label != NULL; ct++) {
94 if (strcmp(argv[1], ct->label) == 0) {
95 if ((ct->takesargs == 0) ^ (argv[2] == NULL))
96 usage();
97 (*ct->func)(argv + 2);
98 break;
99 }
100 }
101
102 if (ct->label == NULL)
103 errx(EXIT_FAILURE, "unknown command ``%s''", argv[optind]);
104
105 close(fd);
106 exit(EXIT_SUCCESS);
107 /* NOTREACHED */
108 }
109
110 static void
111 usage(void)
112 {
113 const char *progname = getprogname();
114
115 fprintf(stderr, "usage: %s identify cpuno\n", progname);
116 fprintf(stderr, " %s list\n", progname);
117 fprintf(stderr, " %s offline cpuno\n", progname);
118 fprintf(stderr, " %s online cpuno\n", progname);
119 fprintf(stderr, " %s intr cpuno\n", progname);
120 fprintf(stderr, " %s nointr cpuno\n", progname);
121 exit(EXIT_FAILURE);
122 /* NOTREACHED */
123 }
124
125 static void
126 cpu_online(char **argv)
127 {
128 cpustate_t cs;
129
130 cs.cs_id = getcpuid(argv);
131 if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
132 err(EXIT_FAILURE, "IOC_CPU_GETSTATE");
133 cs.cs_online = true;
134 if (ioctl(fd, IOC_CPU_SETSTATE, &cs) < 0)
135 err(EXIT_FAILURE, "IOC_CPU_SETSTATE");
136 }
137
138 static void
139 cpu_offline(char **argv)
140 {
141 cpustate_t cs;
142
143 cs.cs_id = getcpuid(argv);
144 if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
145 err(EXIT_FAILURE, "IOC_CPU_GETSTATE");
146 cs.cs_online = false;
147 if (ioctl(fd, IOC_CPU_SETSTATE, &cs) < 0)
148 err(EXIT_FAILURE, "IOC_CPU_SETSTATE");
149 }
150
151 static void
152 cpu_intr(char **argv)
153 {
154 cpustate_t cs;
155
156 cs.cs_id = getcpuid(argv);
157 if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
158 err(EXIT_FAILURE, "IOC_CPU_GETSTATE");
159 cs.cs_intr = true;
160 if (ioctl(fd, IOC_CPU_SETSTATE, &cs) < 0)
161 err(EXIT_FAILURE, "IOC_CPU_SETSTATE");
162 }
163
164 static void
165 cpu_nointr(char **argv)
166 {
167 cpustate_t cs;
168
169 cs.cs_id = getcpuid(argv);
170 if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
171 err(EXIT_FAILURE, "IOC_CPU_GETSTATE");
172 cs.cs_intr = false;
173 if (ioctl(fd, IOC_CPU_SETSTATE, &cs) < 0) {
174 if (errno == EOPNOTSUPP) {
175 warnx("interrupt control not supported on "
176 "this platform");
177 } else
178 err(EXIT_FAILURE, "IOC_CPU_SETSTATE");
179 }
180 }
181
182 static void
183 cpu_identify(char **argv)
184 {
185 char name[32];
186 unsigned int id, np;
187 cpuset_t *cpuset;
188
189 np = sysconf(_SC_NPROCESSORS_CONF);
190 id = getcpuid(argv);
191 snprintf(name, sizeof(name), "cpu%u", id);
192
193 if (np != 0) {
194 cpuset = cpuset_create();
195 if (cpuset == NULL)
196 err(EXIT_FAILURE, "cpuset_create");
197 cpuset_zero(cpuset);
198 cpuset_set(id, cpuset);
199 if (_sched_setaffinity(0, 0, cpuset_size(cpuset), cpuset) < 0) {
200 if (errno == EPERM) {
201 printf("Cannot bind to target CPU. Output "
202 "may not accurately describe the target.\n"
203 "Run as root to allow binding.\n\n");
204 } else {
205 err(EXIT_FAILURE, "_sched_setaffinity");
206 }
207 }
208 cpuset_destroy(cpuset);
209 }
210 identifycpu(name);
211 }
212
213 static u_int
214 getcpuid(char **argv)
215 {
216 char *argp;
217 u_int id;
218 long np;
219
220 id = (u_int)strtoul(argv[0], &argp, 0);
221 if (*argp != '\0')
222 usage();
223
224 np = sysconf(_SC_NPROCESSORS_CONF);
225 if (id >= (u_long)np)
226 errx(EXIT_FAILURE, "Invalid CPU number");
227
228 return id;
229 }
230
231 static void
232 cpu_list(char **argv)
233 {
234 const char *state, *intr;
235 cpustate_t cs;
236 u_int cnt, i;
237 time_t lastmod;
238 char ibuf[16], *ts;
239
240 if (ioctl(fd, IOC_CPU_GETCOUNT, &cnt) < 0)
241 err(EXIT_FAILURE, "IOC_CPU_GETCOUNT");
242
243 printf(
244 "Num HwId Unbound LWPs Interrupts Last change #Intr\n"
245 "---- ---- ------------ ---------- ------------------------ -----\n");
246
247 for (i = 0; i < cnt; i++) {
248 cs.cs_id = i;
249 if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
250 err(EXIT_FAILURE, "IOC_CPU_GETINFO");
251 if (ioctl(fd, IOC_CPU_MAPID, &cs.cs_id) < 0)
252 err(EXIT_FAILURE, "IOC_CPU_MAPID");
253 if (cs.cs_online)
254 state = "online";
255 else
256 state = "offline";
257 if (cs.cs_intr)
258 intr = "intr";
259 else
260 intr = "nointr";
261 if (cs.cs_intrcnt == 0)
262 strcpy(ibuf, "?");
263 else
264 snprintf(ibuf, sizeof(ibuf), "%d", cs.cs_intrcnt - 1);
265 lastmod = (time_t)cs.cs_lastmod |
266 ((time_t)cs.cs_lastmodhi << 32);
267 ts = asctime(localtime(&lastmod));
268 ts[strlen(ts) - 1] = '\0';
269 printf("%-4d %-4x %-12s %-10s %s %s\n", i, cs.cs_hwid, state,
270 intr, ts, ibuf);
271 }
272 }
273
274 int
275 aprint_normal(const char *fmt, ...)
276 {
277 va_list ap;
278 int rv;
279
280 va_start(ap, fmt);
281 rv = vfprintf(stdout, fmt, ap);
282 va_end(ap);
283
284 return rv;
285 }
286 __strong_alias(aprint_verbose,aprint_normal)
287 __strong_alias(aprint_error,aprint_normal)
288
289 int
290 aprint_normal_dev(const char *dev, const char *fmt, ...)
291 {
292 va_list ap;
293 int rv;
294
295 printf("%s: ", dev);
296 va_start(ap, fmt);
297 rv = vfprintf(stdout, fmt, ap);
298 va_end(ap);
299
300 return rv;
301 }
302 __strong_alias(aprint_verbose_dev,aprint_normal_dev)
303 __strong_alias(aprint_error_dev,aprint_normal_dev)
304