Home | History | Annotate | Line # | Download | only in cpuctl
cpuctl.c revision 1.1
      1 /*	$NetBSD: cpuctl.c,v 1.1 2007/08/04 11:03:05 ad 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.1 2007/08/04 11:03:05 ad 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 
    113 	(void)fprintf(stderr, "usage: %s <command> [arguments]\n",
    114 	    getprogname());
    115 	exit(EXIT_FAILURE);
    116 	/* NOTREACHED */
    117 }
    118 
    119 void
    120 cpu_online(char **argv)
    121 {
    122 	cpustate_t cs;
    123 
    124 	cs.cs_id = getcpuid(argv);
    125 	if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
    126 		err(EXIT_FAILURE, "IOC_CPU_GETSTATE");
    127 	cs.cs_online = true;
    128 	if (ioctl(fd, IOC_CPU_SETSTATE, &cs) < 0)
    129 		err(EXIT_FAILURE, "IOC_CPU_SETSTATE");
    130 }
    131 
    132 void
    133 cpu_offline(char **argv)
    134 {
    135 	cpustate_t cs;
    136 
    137 	cs.cs_id = getcpuid(argv);
    138 	if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
    139 		err(EXIT_FAILURE, "IOC_CPU_GETSTATE");
    140 	cs.cs_online = false;
    141 	if (ioctl(fd, IOC_CPU_SETSTATE, &cs) < 0)
    142 		err(EXIT_FAILURE, "IOC_CPU_SETSTATE");
    143 }
    144 
    145 u_int
    146 getcpuid(char **argv)
    147 {
    148 	char *argp;
    149 	u_int id;
    150 
    151 	id = (int)strtoul(argv[0], &argp, 0);
    152 	if (*argp != '\0')
    153 		usage();
    154 
    155 	return id;
    156 }
    157 
    158 void
    159 cpu_list(char **argv)
    160 {
    161 	const char *state, *intr;
    162 	cpustate_t cs;
    163 	u_int cnt, i;
    164 
    165 	if (ioctl(fd, IOC_CPU_GETCOUNT, &cnt) < 0)
    166 		err(EXIT_FAILURE, "IOC_CPU_GETCOUNT");
    167 
    168 	printf("ID   Unbound LWPs Interrupts     Last change\n");
    169  	printf("---- ------------ -------------- ----------------------------\n");
    170 
    171 	for (i = 0; i < cnt; i++) {
    172 		cs.cs_id = i;
    173 		if (ioctl(fd, IOC_CPU_MAPID, &cs.cs_id) < 0)
    174 			err(EXIT_FAILURE, "IOC_CPU_MAPID");
    175 		if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
    176 			err(EXIT_FAILURE, "IOC_CPU_GETINFO");
    177 		if (cs.cs_online)
    178 			state = "online";
    179 		else
    180 			state = "offline";
    181 		if (cs.cs_intr)
    182 			intr = "intr";
    183 		else
    184 			intr = "nointr";
    185 		printf("%-4d %-12s %-12s   %s", cs.cs_id, state,
    186 		   intr, asctime(localtime(&cs.cs_lastmod)));
    187 	}
    188 }
    189