cpuctl.c revision 1.2 1 /* $NetBSD: cpuctl.c,v 1.2 2008/01/09 00:01:33 tnn Exp $ */
2
3 /*-
4 * Copyright (c) 2007 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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #ifndef lint
40 #include <sys/cdefs.h>
41 __RCSID("$NetBSD: cpuctl.c,v 1.2 2008/01/09 00:01:33 tnn Exp $");
42 #endif /* not lint */
43
44 #include <sys/param.h>
45 #include <sys/ioctl.h>
46 #include <sys/uio.h>
47 #include <sys/cpuio.h>
48
49 #include <err.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <stdarg.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <util.h>
58 #include <time.h>
59
60 u_int getcpuid(char **);
61 int main(int, char **);
62 void usage(void);
63
64 void cpu_list(char **);
65 void cpu_offline(char **);
66 void cpu_online(char **);
67
68 struct cmdtab {
69 const char *label;
70 int takesargs;
71 void (*func)(char **);
72 } const cpu_cmdtab[] = {
73 { "list", 0, cpu_list },
74 { "offline", 1, cpu_offline },
75 { "online", 1, cpu_online },
76 { NULL, 0, NULL },
77 };
78
79 int fd;
80
81 int
82 main(int argc, char **argv)
83 {
84 const struct cmdtab *ct;
85
86 if (argc < 2)
87 usage();
88
89 if ((fd = open("/dev/cpuctl", O_RDWR)) < 0)
90 err(EXIT_FAILURE, "/dev/cpuctl");
91
92 for (ct = cpu_cmdtab; ct->label != NULL; ct++) {
93 if (strcmp(argv[1], ct->label) == 0) {
94 if ((ct->takesargs == 0) ^ (argv[2] == NULL))
95 usage();
96 (*ct->func)(argv + 2);
97 break;
98 }
99 }
100
101 if (ct->label == NULL)
102 errx(EXIT_FAILURE, "unknown command ``%s''", argv[optind]);
103
104 close(fd);
105 exit(EXIT_SUCCESS);
106 /* NOTREACHED */
107 }
108
109 void
110 usage(void)
111 {
112 const char *progname = getprogname();
113
114 fprintf(stderr, "usage: %s list\n", progname);
115 fprintf(stderr, " %s offline cpuid\n", progname);
116 fprintf(stderr, " %s online cpuid\n", progname);
117 exit(EXIT_FAILURE);
118 /* NOTREACHED */
119 }
120
121 void
122 cpu_online(char **argv)
123 {
124 cpustate_t cs;
125
126 cs.cs_id = getcpuid(argv);
127 if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
128 err(EXIT_FAILURE, "IOC_CPU_GETSTATE");
129 cs.cs_online = true;
130 if (ioctl(fd, IOC_CPU_SETSTATE, &cs) < 0)
131 err(EXIT_FAILURE, "IOC_CPU_SETSTATE");
132 }
133
134 void
135 cpu_offline(char **argv)
136 {
137 cpustate_t cs;
138
139 cs.cs_id = getcpuid(argv);
140 if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
141 err(EXIT_FAILURE, "IOC_CPU_GETSTATE");
142 cs.cs_online = false;
143 if (ioctl(fd, IOC_CPU_SETSTATE, &cs) < 0)
144 err(EXIT_FAILURE, "IOC_CPU_SETSTATE");
145 }
146
147 u_int
148 getcpuid(char **argv)
149 {
150 char *argp;
151 u_int id;
152
153 id = (int)strtoul(argv[0], &argp, 0);
154 if (*argp != '\0')
155 usage();
156
157 return id;
158 }
159
160 void
161 cpu_list(char **argv)
162 {
163 const char *state, *intr;
164 cpustate_t cs;
165 u_int cnt, i;
166
167 if (ioctl(fd, IOC_CPU_GETCOUNT, &cnt) < 0)
168 err(EXIT_FAILURE, "IOC_CPU_GETCOUNT");
169
170 printf("ID Unbound LWPs Interrupts Last change\n");
171 printf("---- ------------ -------------- ----------------------------\n");
172
173 for (i = 0; i < cnt; i++) {
174 cs.cs_id = i;
175 if (ioctl(fd, IOC_CPU_MAPID, &cs.cs_id) < 0)
176 err(EXIT_FAILURE, "IOC_CPU_MAPID");
177 if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
178 err(EXIT_FAILURE, "IOC_CPU_GETINFO");
179 if (cs.cs_online)
180 state = "online";
181 else
182 state = "offline";
183 if (cs.cs_intr)
184 intr = "intr";
185 else
186 intr = "nointr";
187 printf("%-4d %-12s %-12s %s", cs.cs_id, state,
188 intr, asctime(localtime(&cs.cs_lastmod)));
189 }
190 }
191