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