Home | History | Annotate | Line # | Download | only in cpuctl
cpuctl.c revision 1.3
      1 /*	$NetBSD: cpuctl.c,v 1.3 2008/03/25 15:06:02 martin 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.3 2008/03/25 15:06:02 martin 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 cpuno\n", progname);
    116 	fprintf(stderr, "       %s online cpuno\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 	cpustate_t cs;
    152 
    153 	cs.cs_id = (int)strtoul(argv[0], &argp, 0);
    154 	if (*argp != '\0')
    155 		usage();
    156 	if (ioctl(fd, IOC_CPU_MAPID, &cs.cs_id) < 0)
    157 		err(EXIT_FAILURE, "IOC_CPU_MAPID");
    158 
    159 	return cs.cs_id;
    160 }
    161 
    162 void
    163 cpu_list(char **argv)
    164 {
    165 	const char *state, *intr;
    166 	cpustate_t cs;
    167 	u_int cnt, i;
    168 
    169 	if (ioctl(fd, IOC_CPU_GETCOUNT, &cnt) < 0)
    170 		err(EXIT_FAILURE, "IOC_CPU_GETCOUNT");
    171 
    172 	printf("No   ID     Unbound LWPs Interrupts     Last change\n");
    173  	printf("---- ------ ------------ -------------- ----------------------------\n");
    174 
    175 	for (i = 0; i < cnt; i++) {
    176 		cs.cs_id = i;
    177 		if (ioctl(fd, IOC_CPU_MAPID, &cs.cs_id) < 0)
    178 			err(EXIT_FAILURE, "IOC_CPU_MAPID");
    179 		if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
    180 			err(EXIT_FAILURE, "IOC_CPU_GETINFO");
    181 		if (cs.cs_online)
    182 			state = "online";
    183 		else
    184 			state = "offline";
    185 		if (cs.cs_intr)
    186 			intr = "intr";
    187 		else
    188 			intr = "nointr";
    189 		printf("%-4d %-7x %-12s %-12s   %s", i, cs.cs_id, state,
    190 		   intr, asctime(localtime(&cs.cs_lastmod)));
    191 	}
    192 }
    193