Home | History | Annotate | Line # | Download | only in cpuctl
cpuctl.c revision 1.11
      1 /*	$NetBSD: cpuctl.c,v 1.11 2008/11/19 11:05:40 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.11 2008/11/19 11:05:40 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 	unsigned int id, np;
    151 	cpuset_t *cpuset;
    152 
    153 	np = sysconf(_SC_NPROCESSORS_CONF);
    154 	id = getcpuid(argv);
    155 
    156 	if (id >= np)
    157 		errx(EXIT_FAILURE, "invalid CPU number");
    158 
    159 	snprintf(name, sizeof(name), "cpu%d", id);
    160 
    161 	if (np != 0) {
    162 		cpuset = cpuset_create();
    163 		if (cpuset == NULL)
    164 			err(EXIT_FAILURE, "cpuset_create");
    165 		cpuset_zero(cpuset);
    166 		cpuset_set(id, cpuset);
    167 		if (_sched_setaffinity(0, 0, cpuset_size(cpuset), cpuset) < 0) {
    168 			if (errno == EPERM) {
    169 				printf("Cannot bind to target CPU.  Output "
    170 				    "may not accurately describe the target.\n"
    171 				    "Run as root to allow binding.\n\n");
    172 			} else {
    173 				err(EXIT_FAILURE, "_sched_setaffinity");
    174 			}
    175 		}
    176 		cpuset_destroy(cpuset);
    177 	}
    178 	identifycpu(name);
    179 }
    180 
    181 u_int
    182 getcpuid(char **argv)
    183 {
    184 	char *argp;
    185 	u_int id;
    186 
    187 	id = (int)strtoul(argv[0], &argp, 0);
    188 	if (*argp != '\0')
    189 		usage();
    190 	return id;
    191 }
    192 
    193 void
    194 cpu_list(char **argv)
    195 {
    196 	const char *state, *intr;
    197 	cpustate_t cs;
    198 	u_int cnt, i;
    199 
    200 	if (ioctl(fd, IOC_CPU_GETCOUNT, &cnt) < 0)
    201 		err(EXIT_FAILURE, "IOC_CPU_GETCOUNT");
    202 
    203 	printf("Num  HwId Unbound LWPs Interrupts     Last change\n");
    204  	printf("---- ---- ------------ -------------- ----------------------------\n");
    205 
    206 	for (i = 0; i < cnt; i++) {
    207 		cs.cs_id = i;
    208 		if (ioctl(fd, IOC_CPU_MAPID, &cs.cs_id) < 0)
    209 			err(EXIT_FAILURE, "IOC_CPU_MAPID");
    210 		if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
    211 			err(EXIT_FAILURE, "IOC_CPU_GETINFO");
    212 		if (cs.cs_online)
    213 			state = "online";
    214 		else
    215 			state = "offline";
    216 		if (cs.cs_intr)
    217 			intr = "intr";
    218 		else
    219 			intr = "nointr";
    220 		printf("%-4d %-4x %-12s %-12s   %s", i, cs.cs_id, state,
    221 		   intr, asctime(localtime(&cs.cs_lastmod)));
    222 	}
    223 }
    224 
    225 int
    226 aprint_normal(const char *fmt, ...)
    227 {
    228 	va_list ap;
    229 	int rv;
    230 
    231 	va_start(ap, fmt);
    232 	rv = vfprintf(stdout, fmt, ap);
    233 	va_end(ap);
    234 
    235 	return rv;
    236 }
    237 __strong_alias(aprint_verbose,aprint_normal)
    238 __strong_alias(aprint_error,aprint_normal)
    239 
    240 int
    241 aprint_normal_dev(const char *dev, const char *fmt, ...)
    242 {
    243 	va_list ap;
    244 	int rv;
    245 
    246 	printf("%s: ", dev);
    247 	va_start(ap, fmt);
    248 	rv = vfprintf(stdout, fmt, ap);
    249 	va_end(ap);
    250 
    251 	return rv;
    252 }
    253 __strong_alias(aprint_verbose_dev,aprint_normal_dev)
    254 __strong_alias(aprint_error_dev,aprint_normal_dev)
    255