cpuctl.c revision 1.8 1 /* $NetBSD: cpuctl.c,v 1.8 2008/06/16 01:41:21 rmind Exp $ */
2
3 /*-
4 * Copyright (c) 2007, 2008 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.8 2008/06/16 01:41:21 rmind 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 u_int getcpuid(char **);
57 int main(int, char **);
58 void usage(void);
59
60 void cpu_identify(char **);
61 void cpu_list(char **);
62 void cpu_offline(char **);
63 void cpu_online(char **);
64
65 struct cmdtab {
66 const char *label;
67 int takesargs;
68 void (*func)(char **);
69 } const cpu_cmdtab[] = {
70 { "identify", 1, cpu_identify },
71 { "list", 0, cpu_list },
72 { "offline", 1, cpu_offline },
73 { "online", 1, cpu_online },
74 { NULL, 0, NULL },
75 };
76
77 int fd;
78
79 int
80 main(int argc, char **argv)
81 {
82 const struct cmdtab *ct;
83
84 if (argc < 2)
85 usage();
86
87 if ((fd = open("/dev/cpuctl", O_RDWR)) < 0)
88 err(EXIT_FAILURE, "/dev/cpuctl");
89
90 for (ct = cpu_cmdtab; ct->label != NULL; ct++) {
91 if (strcmp(argv[1], ct->label) == 0) {
92 if ((ct->takesargs == 0) ^ (argv[2] == NULL))
93 usage();
94 (*ct->func)(argv + 2);
95 break;
96 }
97 }
98
99 if (ct->label == NULL)
100 errx(EXIT_FAILURE, "unknown command ``%s''", argv[optind]);
101
102 close(fd);
103 exit(EXIT_SUCCESS);
104 /* NOTREACHED */
105 }
106
107 void
108 usage(void)
109 {
110 const char *progname = getprogname();
111
112 fprintf(stderr, "usage: %s identify cpuno\n", progname);
113 fprintf(stderr, " %s list\n", progname);
114 fprintf(stderr, " %s offline cpuno\n", progname);
115 fprintf(stderr, " %s online cpuno\n", progname);
116 exit(EXIT_FAILURE);
117 /* NOTREACHED */
118 }
119
120 void
121 cpu_online(char **argv)
122 {
123 cpustate_t cs;
124
125 cs.cs_id = getcpuid(argv);
126 if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
127 err(EXIT_FAILURE, "IOC_CPU_GETSTATE");
128 cs.cs_online = true;
129 if (ioctl(fd, IOC_CPU_SETSTATE, &cs) < 0)
130 err(EXIT_FAILURE, "IOC_CPU_SETSTATE");
131 }
132
133 void
134 cpu_offline(char **argv)
135 {
136 cpustate_t cs;
137
138 cs.cs_id = getcpuid(argv);
139 if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
140 err(EXIT_FAILURE, "IOC_CPU_GETSTATE");
141 cs.cs_online = false;
142 if (ioctl(fd, IOC_CPU_SETSTATE, &cs) < 0)
143 err(EXIT_FAILURE, "IOC_CPU_SETSTATE");
144 }
145
146 void
147 cpu_identify(char **argv)
148 {
149 char name[32];
150 int id, np;
151 cpuset_t *cpuset;
152
153 id = getcpuid(argv);
154 snprintf(name, sizeof(name), "cpu%d", id);
155
156 np = sysconf(_SC_NPROCESSORS_CONF);
157 if (np != 0) {
158 cpuset = cpuset_create();
159 if (cpuset == NULL)
160 err(EXIT_FAILURE, "cpuset_create");
161 cpuset_zero(cpuset);
162 cpuset_set(id, cpuset);
163 if (_sched_setaffinity(0, 0, cpuset_size(cpuset), cpuset) < 0) {
164 if (errno == EPERM) {
165 printf("Cannot bind to target CPU. Output "
166 "may not accurately describe the target.\n"
167 "Run as root to allow binding.\n\n");
168 } else {
169 err(EXIT_FAILURE, "_sched_setaffinity");
170 }
171 }
172 cpuset_destroy(cpuset);
173 }
174 identifycpu(name);
175 }
176
177 u_int
178 getcpuid(char **argv)
179 {
180 char *argp;
181 cpustate_t cs;
182
183 cs.cs_id = (int)strtoul(argv[0], &argp, 0);
184 if (*argp != '\0')
185 usage();
186 if (ioctl(fd, IOC_CPU_MAPID, &cs.cs_id) < 0)
187 err(EXIT_FAILURE, "IOC_CPU_MAPID");
188
189 return cs.cs_id;
190 }
191
192 void
193 cpu_list(char **argv)
194 {
195 const char *state, *intr;
196 cpustate_t cs;
197 u_int cnt, i;
198
199 if (ioctl(fd, IOC_CPU_GETCOUNT, &cnt) < 0)
200 err(EXIT_FAILURE, "IOC_CPU_GETCOUNT");
201
202 printf("Num HwId Unbound LWPs Interrupts Last change\n");
203 printf("---- ---- ------------ -------------- ----------------------------\n");
204
205 for (i = 0; i < cnt; i++) {
206 cs.cs_id = i;
207 if (ioctl(fd, IOC_CPU_MAPID, &cs.cs_id) < 0)
208 err(EXIT_FAILURE, "IOC_CPU_MAPID");
209 if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
210 err(EXIT_FAILURE, "IOC_CPU_GETINFO");
211 if (cs.cs_online)
212 state = "online";
213 else
214 state = "offline";
215 if (cs.cs_intr)
216 intr = "intr";
217 else
218 intr = "nointr";
219 printf("%-4d %-4x %-12s %-12s %s", i, cs.cs_id, state,
220 intr, asctime(localtime(&cs.cs_lastmod)));
221 }
222 }
223
224 int
225 aprint_normal(const char *fmt, ...)
226 {
227 va_list ap;
228 int rv;
229
230 va_start(ap, fmt);
231 rv = vfprintf(stdout, fmt, ap);
232 va_end(ap);
233
234 return rv;
235 }
236 __strong_alias(aprint_verbose,aprint_normal)
237 __strong_alias(aprint_error,aprint_normal)
238
239 int
240 aprint_normal_dev(const char *dev, const char *fmt, ...)
241 {
242 va_list ap;
243 int rv;
244
245 printf("%s: ", dev);
246 va_start(ap, fmt);
247 rv = vfprintf(stdout, fmt, ap);
248 va_end(ap);
249
250 return rv;
251 }
252 __strong_alias(aprint_verbose_dev,aprint_normal_dev)
253 __strong_alias(aprint_error_dev,aprint_normal_dev)
254