1 /* $NetBSD: cpuctl.c,v 1.14 2009/04/19 14:11:38 ad Exp $ */
4 * Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
33 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: cpuctl.c,v 1.14 2009/04/19 14:11:38 ad Exp $");
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
40 #include <sys/cpuio.h>
56 u_int
getcpuid(char **);
57 int main(int, char **);
60 void cpu_identify(char **);
61 void cpu_list(char **);
62 void cpu_offline(char **);
63 void cpu_online(char **);
64 void cpu_intr(char **);
65 void cpu_nointr(char **);
70 void (*func
)(char **);
71 } const cpu_cmdtab
[] = {
72 { "identify", 1, cpu_identify
},
73 { "list", 0, cpu_list
},
74 { "offline", 1, cpu_offline
},
75 { "online", 1, cpu_online
},
76 { "intr", 1, cpu_intr
},
77 { "nointr", 1, cpu_nointr
},
84 main(int argc
, char **argv
)
86 const struct cmdtab
*ct
;
91 if ((fd
= open("/dev/cpuctl", O_RDWR
)) < 0)
92 err(EXIT_FAILURE
, "/dev/cpuctl");
94 for (ct
= cpu_cmdtab
; ct
->label
!= NULL
; ct
++) {
95 if (strcmp(argv
[1], ct
->label
) == 0) {
96 if ((ct
->takesargs
== 0) ^ (argv
[2] == NULL
))
98 (*ct
->func
)(argv
+ 2);
103 if (ct
->label
== NULL
)
104 errx(EXIT_FAILURE
, "unknown command ``%s''", argv
[optind
]);
114 const char *progname
= getprogname();
116 fprintf(stderr
, "usage: %s identify cpuno\n", progname
);
117 fprintf(stderr
, " %s list\n", progname
);
118 fprintf(stderr
, " %s offline cpuno\n", progname
);
119 fprintf(stderr
, " %s online cpuno\n", progname
);
120 fprintf(stderr
, " %s intr cpuno\n", progname
);
121 fprintf(stderr
, " %s nointr cpuno\n", progname
);
127 cpu_online(char **argv
)
131 cs
.cs_id
= getcpuid(argv
);
132 if (ioctl(fd
, IOC_CPU_GETSTATE
, &cs
) < 0)
133 err(EXIT_FAILURE
, "IOC_CPU_GETSTATE");
135 if (ioctl(fd
, IOC_CPU_SETSTATE
, &cs
) < 0)
136 err(EXIT_FAILURE
, "IOC_CPU_SETSTATE");
140 cpu_offline(char **argv
)
144 cs
.cs_id
= getcpuid(argv
);
145 if (ioctl(fd
, IOC_CPU_GETSTATE
, &cs
) < 0)
146 err(EXIT_FAILURE
, "IOC_CPU_GETSTATE");
147 cs
.cs_online
= false;
148 if (ioctl(fd
, IOC_CPU_SETSTATE
, &cs
) < 0)
149 err(EXIT_FAILURE
, "IOC_CPU_SETSTATE");
153 cpu_intr(char **argv
)
157 cs
.cs_id
= getcpuid(argv
);
158 if (ioctl(fd
, IOC_CPU_GETSTATE
, &cs
) < 0)
159 err(EXIT_FAILURE
, "IOC_CPU_GETSTATE");
161 if (ioctl(fd
, IOC_CPU_SETSTATE
, &cs
) < 0)
162 err(EXIT_FAILURE
, "IOC_CPU_SETSTATE");
166 cpu_nointr(char **argv
)
170 cs
.cs_id
= getcpuid(argv
);
171 if (ioctl(fd
, IOC_CPU_GETSTATE
, &cs
) < 0)
172 err(EXIT_FAILURE
, "IOC_CPU_GETSTATE");
174 if (ioctl(fd
, IOC_CPU_SETSTATE
, &cs
) < 0) {
175 if (errno
== EOPNOTSUPP
) {
176 warnx("interrupt control not supported on "
179 err(EXIT_FAILURE
, "IOC_CPU_SETSTATE");
184 cpu_identify(char **argv
)
190 np
= sysconf(_SC_NPROCESSORS_CONF
);
192 snprintf(name
, sizeof(name
), "cpu%u", id
);
195 cpuset
= cpuset_create();
197 err(EXIT_FAILURE
, "cpuset_create");
199 cpuset_set(id
, cpuset
);
200 if (_sched_setaffinity(0, 0, cpuset_size(cpuset
), cpuset
) < 0) {
201 if (errno
== EPERM
) {
202 printf("Cannot bind to target CPU. Output "
203 "may not accurately describe the target.\n"
204 "Run as root to allow binding.\n\n");
206 err(EXIT_FAILURE
, "_sched_setaffinity");
209 cpuset_destroy(cpuset
);
215 getcpuid(char **argv
)
221 id
= (u_int
)strtoul(argv
[0], &argp
, 0);
225 np
= sysconf(_SC_NPROCESSORS_CONF
);
226 if (id
>= (u_long
)np
)
227 errx(EXIT_FAILURE
, "Invalid CPU number");
233 cpu_list(char **argv
)
235 const char *state
, *intr
;
241 if (ioctl(fd
, IOC_CPU_GETCOUNT
, &cnt
) < 0)
242 err(EXIT_FAILURE
, "IOC_CPU_GETCOUNT");
245 "Num HwId Unbound LWPs Interrupts Last change #Intr\n"
246 "---- ---- ------------ ---------- ------------------------ -----\n");
248 for (i
= 0; i
< cnt
; i
++) {
250 if (ioctl(fd
, IOC_CPU_GETSTATE
, &cs
) < 0)
251 err(EXIT_FAILURE
, "IOC_CPU_GETINFO");
252 if (ioctl(fd
, IOC_CPU_MAPID
, &cs
.cs_id
) < 0)
253 err(EXIT_FAILURE
, "IOC_CPU_MAPID");
262 if (cs
.cs_intrcnt
== 0)
265 snprintf(ibuf
, sizeof(ibuf
), "%d", cs
.cs_intrcnt
- 1);
266 lastmod
= (time_t)cs
.cs_lastmod
|
267 ((time_t)cs
.cs_lastmodhi
<< 32);
268 ts
= asctime(localtime(&lastmod
));
269 ts
[strlen(ts
) - 1] = '\0';
270 printf("%-4d %-4x %-12s %-10s %s %s\n", i
, cs
.cs_id
, state
,
276 aprint_normal(const char *fmt
, ...)
282 rv
= vfprintf(stdout
, fmt
, ap
);
287 __strong_alias(aprint_verbose
,aprint_normal
)
288 __strong_alias(aprint_error
,aprint_normal
)
291 aprint_normal_dev(const char *dev
, const char *fmt
, ...)
298 rv
= vfprintf(stdout
, fmt
, ap
);
303 __strong_alias(aprint_verbose_dev
,aprint_normal_dev
)
304 __strong_alias(aprint_error_dev
,aprint_normal_dev
)