Home | History | Annotate | Line # | Download | only in cpuctl
cpuctl.c revision 1.4
      1 /*	$NetBSD: cpuctl.c,v 1.4 2008/04/28 20:24:15 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  *
     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.4 2008/04/28 20:24:15 martin 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 
     53 u_int	getcpuid(char **);
     54 int	main(int, char **);
     55 void	usage(void);
     56 
     57 void	cpu_list(char **);
     58 void	cpu_offline(char **);
     59 void	cpu_online(char **);
     60 
     61 struct cmdtab {
     62 	const char	*label;
     63 	int	takesargs;
     64 	void	(*func)(char **);
     65 } const cpu_cmdtab[] = {
     66 	{ "list", 0, cpu_list },
     67 	{ "offline", 1, cpu_offline },
     68 	{ "online", 1, cpu_online },
     69 	{ NULL, 0, NULL },
     70 };
     71 
     72 int	fd;
     73 
     74 int
     75 main(int argc, char **argv)
     76 {
     77 	const struct cmdtab *ct;
     78 
     79 	if (argc < 2)
     80 		usage();
     81 
     82 	if ((fd = open("/dev/cpuctl", O_RDWR)) < 0)
     83 		err(EXIT_FAILURE, "/dev/cpuctl");
     84 
     85 	for (ct = cpu_cmdtab; ct->label != NULL; ct++) {
     86 		if (strcmp(argv[1], ct->label) == 0) {
     87 			if ((ct->takesargs == 0) ^ (argv[2] == NULL))
     88 			    	usage();
     89 			(*ct->func)(argv + 2);
     90 			break;
     91 		}
     92 	}
     93 
     94 	if (ct->label == NULL)
     95 		errx(EXIT_FAILURE, "unknown command ``%s''", argv[optind]);
     96 
     97 	close(fd);
     98 	exit(EXIT_SUCCESS);
     99 	/* NOTREACHED */
    100 }
    101 
    102 void
    103 usage(void)
    104 {
    105 	const char *progname = getprogname();
    106 
    107 	fprintf(stderr, "usage: %s list\n", progname);
    108 	fprintf(stderr, "       %s offline cpuno\n", progname);
    109 	fprintf(stderr, "       %s online cpuno\n", progname);
    110 	exit(EXIT_FAILURE);
    111 	/* NOTREACHED */
    112 }
    113 
    114 void
    115 cpu_online(char **argv)
    116 {
    117 	cpustate_t cs;
    118 
    119 	cs.cs_id = getcpuid(argv);
    120 	if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
    121 		err(EXIT_FAILURE, "IOC_CPU_GETSTATE");
    122 	cs.cs_online = true;
    123 	if (ioctl(fd, IOC_CPU_SETSTATE, &cs) < 0)
    124 		err(EXIT_FAILURE, "IOC_CPU_SETSTATE");
    125 }
    126 
    127 void
    128 cpu_offline(char **argv)
    129 {
    130 	cpustate_t cs;
    131 
    132 	cs.cs_id = getcpuid(argv);
    133 	if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
    134 		err(EXIT_FAILURE, "IOC_CPU_GETSTATE");
    135 	cs.cs_online = false;
    136 	if (ioctl(fd, IOC_CPU_SETSTATE, &cs) < 0)
    137 		err(EXIT_FAILURE, "IOC_CPU_SETSTATE");
    138 }
    139 
    140 u_int
    141 getcpuid(char **argv)
    142 {
    143 	char *argp;
    144 	cpustate_t cs;
    145 
    146 	cs.cs_id = (int)strtoul(argv[0], &argp, 0);
    147 	if (*argp != '\0')
    148 		usage();
    149 	if (ioctl(fd, IOC_CPU_MAPID, &cs.cs_id) < 0)
    150 		err(EXIT_FAILURE, "IOC_CPU_MAPID");
    151 
    152 	return cs.cs_id;
    153 }
    154 
    155 void
    156 cpu_list(char **argv)
    157 {
    158 	const char *state, *intr;
    159 	cpustate_t cs;
    160 	u_int cnt, i;
    161 
    162 	if (ioctl(fd, IOC_CPU_GETCOUNT, &cnt) < 0)
    163 		err(EXIT_FAILURE, "IOC_CPU_GETCOUNT");
    164 
    165 	printf("No   ID     Unbound LWPs Interrupts     Last change\n");
    166  	printf("---- ------ ------------ -------------- ----------------------------\n");
    167 
    168 	for (i = 0; i < cnt; i++) {
    169 		cs.cs_id = i;
    170 		if (ioctl(fd, IOC_CPU_MAPID, &cs.cs_id) < 0)
    171 			err(EXIT_FAILURE, "IOC_CPU_MAPID");
    172 		if (ioctl(fd, IOC_CPU_GETSTATE, &cs) < 0)
    173 			err(EXIT_FAILURE, "IOC_CPU_GETINFO");
    174 		if (cs.cs_online)
    175 			state = "online";
    176 		else
    177 			state = "offline";
    178 		if (cs.cs_intr)
    179 			intr = "intr";
    180 		else
    181 			intr = "nointr";
    182 		printf("%-4d %-7x %-12s %-12s   %s", i, cs.cs_id, state,
    183 		   intr, asctime(localtime(&cs.cs_lastmod)));
    184 	}
    185 }
    186