xref: /netbsd-src/usr.sbin/cpuctl/cpuctl.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: cpuctl.c,v 1.12 2008/11/19 20:56:08 cegger 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.12 2008/11/19 20:56:08 cegger 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 	unsigned int id, np;
151 	cpuset_t *cpuset;
152 
153 	np = sysconf(_SC_NPROCESSORS_CONF);
154 	id = getcpuid(argv);
155 	snprintf(name, sizeof(name), "cpu%u", id);
156 
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 	u_int id;
182 	long np;
183 
184 	id = (u_int)strtoul(argv[0], &argp, 0);
185 	if (*argp != '\0')
186 		usage();
187 
188 	np = sysconf(_SC_NPROCESSORS_CONF);
189 	if (id >= np)
190 		errx(EXIT_FAILURE, "Invalid CPU number");
191 
192 	return id;
193 }
194 
195 void
196 cpu_list(char **argv)
197 {
198 	const char *state, *intr;
199 	cpustate_t cs;
200 	u_int cnt, i;
201 
202 	if (ioctl(fd, IOC_CPU_GETCOUNT, &cnt) < 0)
203 		err(EXIT_FAILURE, "IOC_CPU_GETCOUNT");
204 
205 	printf("Num  HwId Unbound LWPs Interrupts     Last change\n");
206  	printf("---- ---- ------------ -------------- ----------------------------\n");
207 
208 	for (i = 0; i < cnt; i++) {
209 		cs.cs_id = i;
210 		if (ioctl(fd, IOC_CPU_MAPID, &cs.cs_id) < 0)
211 			err(EXIT_FAILURE, "IOC_CPU_MAPID");
212 		if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
213 			err(EXIT_FAILURE, "IOC_CPU_GETINFO");
214 		if (cs.cs_online)
215 			state = "online";
216 		else
217 			state = "offline";
218 		if (cs.cs_intr)
219 			intr = "intr";
220 		else
221 			intr = "nointr";
222 		printf("%-4d %-4x %-12s %-12s   %s", i, cs.cs_id, state,
223 		   intr, asctime(localtime(&cs.cs_lastmod)));
224 	}
225 }
226 
227 int
228 aprint_normal(const char *fmt, ...)
229 {
230 	va_list ap;
231 	int rv;
232 
233 	va_start(ap, fmt);
234 	rv = vfprintf(stdout, fmt, ap);
235 	va_end(ap);
236 
237 	return rv;
238 }
239 __strong_alias(aprint_verbose,aprint_normal)
240 __strong_alias(aprint_error,aprint_normal)
241 
242 int
243 aprint_normal_dev(const char *dev, const char *fmt, ...)
244 {
245 	va_list ap;
246 	int rv;
247 
248 	printf("%s: ", dev);
249 	va_start(ap, fmt);
250 	rv = vfprintf(stdout, fmt, ap);
251 	va_end(ap);
252 
253 	return rv;
254 }
255 __strong_alias(aprint_verbose_dev,aprint_normal_dev)
256 __strong_alias(aprint_error_dev,aprint_normal_dev)
257