xref: /onnv-gate/usr/src/cmd/trapstat/sun4/trapstat.c (revision 12719:bd9fb35d09c2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52179Sahl  * Common Development and Distribution License (the "License").
62179Sahl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
212179Sahl 
220Sstevel@tonic-gate /*
23*12719SRod.Evans@Sun.COM  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <stdio.h>
270Sstevel@tonic-gate #include <stdlib.h>
280Sstevel@tonic-gate #include <string.h>
290Sstevel@tonic-gate #include <fcntl.h>
300Sstevel@tonic-gate #include <errno.h>
310Sstevel@tonic-gate #include <unistd.h>
320Sstevel@tonic-gate #include <signal.h>
330Sstevel@tonic-gate #include <strings.h>
340Sstevel@tonic-gate #include <limits.h>
350Sstevel@tonic-gate #include <sys/mman.h>
360Sstevel@tonic-gate #include <sys/pset.h>
370Sstevel@tonic-gate #include <sys/varargs.h>
380Sstevel@tonic-gate #include <sys/trapstat.h>
390Sstevel@tonic-gate #include <sys/wait.h>
400Sstevel@tonic-gate #include <stddef.h>
410Sstevel@tonic-gate #include <termio.h>
42*12719SRod.Evans@Sun.COM #include "_trapstat.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #define	TSTAT_DEVICE	"/dev/trapstat"
450Sstevel@tonic-gate #define	TSTAT_COMMAND	"trapstat"
460Sstevel@tonic-gate #define	TSTAT_DELTA(data, old, member) g_absolute ? (data)->member : \
470Sstevel@tonic-gate 	(uint64_t)(0.5 + (g_interval / (double)((data)->tdata_snapts - \
480Sstevel@tonic-gate 	(old)->tdata_snapts)) * (double)((data)->member - (old)->member))
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #define	TSTAT_PRINT_MISSDATA(diff, time) \
510Sstevel@tonic-gate 	(void) printf(" %9lld %4.1f", (diff), (time));
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #define	TSTAT_PAGESIZE_MODIFIERS	" kmgtp"
540Sstevel@tonic-gate #define	TSTAT_PAGESIZE_STRLEN		10
550Sstevel@tonic-gate #define	TSTAT_MAX_RATE			5000
560Sstevel@tonic-gate #define	TSTAT_COLUMN_OFFS	26
570Sstevel@tonic-gate #define	TSTAT_COLUMNS_PER_CPU	9
580Sstevel@tonic-gate 
590Sstevel@tonic-gate static tstat_data_t *g_data[2];
600Sstevel@tonic-gate static tstat_data_t *g_ndata, *g_odata;
610Sstevel@tonic-gate static processorid_t g_max_cpus;
620Sstevel@tonic-gate static int8_t *g_selected;
630Sstevel@tonic-gate static timer_t g_tid;
640Sstevel@tonic-gate static int g_interval = NANOSEC;
650Sstevel@tonic-gate static int g_peffect = 1;
660Sstevel@tonic-gate static int g_absolute = 0;
670Sstevel@tonic-gate static sigset_t g_oset;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate static psetid_t g_pset = PS_NONE;
700Sstevel@tonic-gate static processorid_t *g_pset_cpus;
710Sstevel@tonic-gate static uint_t g_pset_ncpus;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate static int g_cpus_per_line = (80 - TSTAT_COLUMN_OFFS) / TSTAT_COLUMNS_PER_CPU;
740Sstevel@tonic-gate static int g_winch;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate static int g_pgsizes;
770Sstevel@tonic-gate static size_t *g_pgsize;
780Sstevel@tonic-gate static char **g_pgnames;
790Sstevel@tonic-gate static size_t g_datasize;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate static int g_gen;
820Sstevel@tonic-gate static int g_fd;
830Sstevel@tonic-gate static uint8_t g_active[TSTAT_NENT];
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static hrtime_t g_start;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate static int g_exec_errno;
880Sstevel@tonic-gate static int g_child_exited;
890Sstevel@tonic-gate static int g_child_status;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate static void (*g_process)(void *, uint64_t, double);
920Sstevel@tonic-gate static void *g_arg;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate typedef struct tstat_sum {
950Sstevel@tonic-gate 	uint64_t	tsum_diff;
960Sstevel@tonic-gate 	double		tsum_time;
970Sstevel@tonic-gate } tstat_sum_t;
980Sstevel@tonic-gate 
99*12719SRod.Evans@Sun.COM /*
100*12719SRod.Evans@Sun.COM  * Define a dummy g_traps reader to establish a symbol capabilities lead.
101*12719SRod.Evans@Sun.COM  * This routine should never be called, as the sun4u and sun4v variants
102*12719SRod.Evans@Sun.COM  * will be used as appropriate.
103*12719SRod.Evans@Sun.COM  */
104*12719SRod.Evans@Sun.COM /* ARGSUSED0 */
105*12719SRod.Evans@Sun.COM tstat_ent_t *
get_trap_ent(int ndx)106*12719SRod.Evans@Sun.COM get_trap_ent(int ndx)
107*12719SRod.Evans@Sun.COM {
108*12719SRod.Evans@Sun.COM 	return (NULL);
109*12719SRod.Evans@Sun.COM }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate static void
usage(void)1120Sstevel@tonic-gate usage(void)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate 	(void) fprintf(stderr,
1150Sstevel@tonic-gate 	    "\nusage:  trapstat [ -t | -T | -e entrylist ]\n"
1160Sstevel@tonic-gate 	    "   [ -C psrset | -c cpulist ]\n"
1170Sstevel@tonic-gate 	    "   [ -P ] [ -a ] [ -r rate ] [[ interval [ count ] ] | "
1180Sstevel@tonic-gate 	    "command [ args ] ]\n\n"
1190Sstevel@tonic-gate 	    "Trap selection options:\n\n"
1200Sstevel@tonic-gate 	    " -t             TLB statistics\n"
1210Sstevel@tonic-gate 	    " -T             TLB statistics, with pagesize information\n"
1220Sstevel@tonic-gate 	    " -e entrylist   Enable statistics only for entries specified "
1230Sstevel@tonic-gate 	    "by entrylist\n\n"
1240Sstevel@tonic-gate 	    "CPU selection options:\n\n"
1250Sstevel@tonic-gate 	    " -c cpulist     Enable statistics only for specified CPU list\n"
1260Sstevel@tonic-gate 	    " -C psrset      Enable statistics only for specified processor "
1270Sstevel@tonic-gate 	    "set\n\n"
1280Sstevel@tonic-gate 	    "Other options:\n\n"
1290Sstevel@tonic-gate 	    " -a             Display trap values as accumulating values "
1300Sstevel@tonic-gate 	    "instead of rates\n"
1310Sstevel@tonic-gate 	    " -l             List trap table entries and exit\n"
1320Sstevel@tonic-gate 	    " -P             Display output in parsable format\n"
1330Sstevel@tonic-gate 	    " -r hz          Set sampling rate to be hz samples "
1340Sstevel@tonic-gate 	    "per second\n\n");
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	exit(EXIT_FAILURE);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate static void
fatal(char * fmt,...)1400Sstevel@tonic-gate fatal(char *fmt, ...)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	va_list ap;
1430Sstevel@tonic-gate 	int error = errno;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	va_start(ap, fmt);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	(void) fprintf(stderr, TSTAT_COMMAND ": ");
1480Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	if (fmt[strlen(fmt) - 1] != '\n')
1510Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s\n", strerror(error));
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	exit(EXIT_FAILURE);
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate static void
set_width(void)1570Sstevel@tonic-gate set_width(void)
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate 	struct winsize win;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	if (!isatty(fileno(stdout)))
1620Sstevel@tonic-gate 		return;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (ioctl(fileno(stdout), TIOCGWINSZ, &win) == -1)
1650Sstevel@tonic-gate 		return;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if (win.ws_col == 0) {
1680Sstevel@tonic-gate 		/*
1690Sstevel@tonic-gate 		 * If TIOCGWINSZ returned 0 for the columns, just return --
1700Sstevel@tonic-gate 		 * thereby using the default value of g_cpus_per_line.  (This
1710Sstevel@tonic-gate 		 * happens, e.g., when running over a tip line.)
1720Sstevel@tonic-gate 		 */
1730Sstevel@tonic-gate 		return;
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	g_cpus_per_line = (win.ws_col - TSTAT_COLUMN_OFFS) /
1770Sstevel@tonic-gate 	    TSTAT_COLUMNS_PER_CPU;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	if (g_cpus_per_line < 1)
1800Sstevel@tonic-gate 		g_cpus_per_line = 1;
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate static void
intr(int signo)1840Sstevel@tonic-gate intr(int signo)
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate 	int error = errno;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	switch (signo) {
1890Sstevel@tonic-gate 	case SIGWINCH:
1900Sstevel@tonic-gate 		g_winch = 1;
1910Sstevel@tonic-gate 		set_width();
1920Sstevel@tonic-gate 		break;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	case SIGCHLD:
1950Sstevel@tonic-gate 		g_child_exited = 1;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 		while (wait(&g_child_status) == -1 && errno == EINTR)
1980Sstevel@tonic-gate 			continue;
1990Sstevel@tonic-gate 		break;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	default:
2020Sstevel@tonic-gate 		break;
2030Sstevel@tonic-gate 	}
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	errno = error;
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate static void
setup(void)2090Sstevel@tonic-gate setup(void)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate 	struct sigaction act;
2120Sstevel@tonic-gate 	struct sigevent ev;
2130Sstevel@tonic-gate 	sigset_t set;
2140Sstevel@tonic-gate 	int i;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	for (i = 0; i < TSTAT_NENT; i++) {
217*12719SRod.Evans@Sun.COM 		tstat_ent_t	*gtp;
218*12719SRod.Evans@Sun.COM 
219*12719SRod.Evans@Sun.COM 		if ((gtp = get_trap_ent(i)) == NULL)
220*12719SRod.Evans@Sun.COM 			continue;
2210Sstevel@tonic-gate 
222*12719SRod.Evans@Sun.COM 		if (gtp->tent_type == TSTAT_ENT_RESERVED)
223*12719SRod.Evans@Sun.COM 			gtp->tent_name = "reserved";
224*12719SRod.Evans@Sun.COM 
225*12719SRod.Evans@Sun.COM 		if (gtp->tent_type == TSTAT_ENT_UNUSED)
226*12719SRod.Evans@Sun.COM 			gtp->tent_name = "unused";
2270Sstevel@tonic-gate 	}
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	g_max_cpus = (processorid_t)sysconf(_SC_CPUID_MAX) + 1;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	if ((g_selected = malloc(sizeof (int8_t) * g_max_cpus)) == NULL)
2320Sstevel@tonic-gate 		fatal("could not allocate g_selected");
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	bzero(g_selected, sizeof (int8_t) * g_max_cpus);
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	g_pset_cpus = malloc(sizeof (processorid_t) * g_max_cpus);
2370Sstevel@tonic-gate 	if (g_pset_cpus == NULL)
2380Sstevel@tonic-gate 		fatal("could not allocate g_pset_cpus");
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	bzero(g_pset_cpus, sizeof (processorid_t) * g_max_cpus);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	if ((g_pgsizes = getpagesizes(NULL, 0)) == -1)
2430Sstevel@tonic-gate 		fatal("getpagesizes()");
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	if ((g_pgsize = malloc(sizeof (size_t) * g_pgsizes)) == NULL)
2460Sstevel@tonic-gate 		fatal("could not allocate g_pgsize array");
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	if (getpagesizes(g_pgsize, g_pgsizes) == -1)
2490Sstevel@tonic-gate 		fatal("getpagesizes(%d)", g_pgsizes);
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	if ((g_pgnames = malloc(sizeof (char *) * g_pgsizes)) == NULL)
2520Sstevel@tonic-gate 		fatal("could not allocate g_pgnames");
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	for (i = 0; i < g_pgsizes; i++) {
2550Sstevel@tonic-gate 		size_t j, mul;
2560Sstevel@tonic-gate 		size_t sz = g_pgsize[i];
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 		if ((g_pgnames[i] = malloc(TSTAT_PAGESIZE_STRLEN)) == NULL)
2590Sstevel@tonic-gate 			fatal("could not allocate g_pgnames[%d]", i);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 		for (j = 0, mul = 10; (1 << mul) <= sz; j++, mul += 10)
2620Sstevel@tonic-gate 			continue;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 		(void) snprintf(g_pgnames[i], TSTAT_PAGESIZE_STRLEN,
2650Sstevel@tonic-gate 		    "%d%c", sz >> (mul - 10), " kmgtpe"[j]);
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	g_datasize =
2690Sstevel@tonic-gate 	    sizeof (tstat_data_t) + (g_pgsizes - 1) * sizeof (tstat_pgszdata_t);
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	if ((g_data[0] = malloc(g_datasize * g_max_cpus)) == NULL)
2720Sstevel@tonic-gate 		fatal("could not allocate data buffer 0");
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	if ((g_data[1] = malloc(g_datasize * g_max_cpus)) == NULL)
2750Sstevel@tonic-gate 		fatal("could not allocate data buffer 1");
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
2780Sstevel@tonic-gate 	act.sa_flags = 0;
2790Sstevel@tonic-gate 	act.sa_handler = intr;
2800Sstevel@tonic-gate 	(void) sigaction(SIGUSR1, &act, NULL);
2810Sstevel@tonic-gate 	(void) sigaction(SIGCHLD, &act, NULL);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	(void) sigaddset(&act.sa_mask, SIGCHLD);
2840Sstevel@tonic-gate 	(void) sigaddset(&act.sa_mask, SIGUSR1);
2850Sstevel@tonic-gate 	(void) sigaction(SIGWINCH, &act, NULL);
2860Sstevel@tonic-gate 	set_width();
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	(void) sigemptyset(&set);
2890Sstevel@tonic-gate 	(void) sigaddset(&set, SIGCHLD);
2900Sstevel@tonic-gate 	(void) sigaddset(&set, SIGUSR1);
2910Sstevel@tonic-gate 	(void) sigaddset(&set, SIGWINCH);
2920Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &set, &g_oset);
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	ev.sigev_notify = SIGEV_SIGNAL;
2950Sstevel@tonic-gate 	ev.sigev_signo = SIGUSR1;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	if (timer_create(CLOCK_HIGHRES, &ev, &g_tid) == -1)
2980Sstevel@tonic-gate 		fatal("cannot create CLOCK_HIGHRES timer");
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate static void
set_interval(hrtime_t nsec)3020Sstevel@tonic-gate set_interval(hrtime_t nsec)
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate 	struct itimerspec ts;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	/*
3070Sstevel@tonic-gate 	 * If the interval is less than one second, we'll report the
3080Sstevel@tonic-gate 	 * numbers in terms of rate-per-interval.  If the interval is
3090Sstevel@tonic-gate 	 * greater than one second, we'll report numbers in terms of
3100Sstevel@tonic-gate 	 * rate-per-second.
3110Sstevel@tonic-gate 	 */
3120Sstevel@tonic-gate 	g_interval = nsec < NANOSEC ? nsec : NANOSEC;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	ts.it_value.tv_sec = nsec / NANOSEC;
3150Sstevel@tonic-gate 	ts.it_value.tv_nsec = nsec % NANOSEC;
3160Sstevel@tonic-gate 	ts.it_interval.tv_sec = nsec / NANOSEC;
3170Sstevel@tonic-gate 	ts.it_interval.tv_nsec = nsec % NANOSEC;
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	if (timer_settime(g_tid, TIMER_RELTIME, &ts, NULL) == -1)
3200Sstevel@tonic-gate 		fatal("cannot set time on CLOCK_HIGHRES timer");
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate static void
print_entries(FILE * stream,int parsable)3240Sstevel@tonic-gate print_entries(FILE *stream, int parsable)
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate 	int entno;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	if (!parsable) {
3290Sstevel@tonic-gate 		(void) fprintf(stream, "  %3s %3s | %-20s | %s\n", "hex",
3300Sstevel@tonic-gate 		    "dec", "entry name", "description");
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 		(void) fprintf(stream, "----------+----------------------"
3330Sstevel@tonic-gate 		    "+-----------------------\n");
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	for (entno = 0; entno < TSTAT_NENT; entno++) {
337*12719SRod.Evans@Sun.COM 		tstat_ent_t	*gtp;
338*12719SRod.Evans@Sun.COM 
339*12719SRod.Evans@Sun.COM 		if ((gtp = get_trap_ent(entno)) == NULL)
340*12719SRod.Evans@Sun.COM 			continue;
341*12719SRod.Evans@Sun.COM 
342*12719SRod.Evans@Sun.COM 		if (gtp->tent_type != TSTAT_ENT_USED)
3430Sstevel@tonic-gate 			continue;
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 		(void) fprintf(stream, "0x%03x %3d %s%-20s %s%s\n",
3460Sstevel@tonic-gate 		    entno, entno,
347*12719SRod.Evans@Sun.COM 		    parsable ? "" : "| ", gtp->tent_name,
348*12719SRod.Evans@Sun.COM 		    parsable ? "" : "| ", gtp->tent_descr);
3490Sstevel@tonic-gate 	}
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate static void
select_entry(char * entry)3530Sstevel@tonic-gate select_entry(char *entry)
3540Sstevel@tonic-gate {
3550Sstevel@tonic-gate 	ulong_t entno;
3560Sstevel@tonic-gate 	char *end;
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	/*
3590Sstevel@tonic-gate 	 * The entry may be specified as a number (e.g., "0x68", "104") or
3600Sstevel@tonic-gate 	 * as a name ("dtlb-miss").
3610Sstevel@tonic-gate 	 */
3620Sstevel@tonic-gate 	entno = strtoul(entry, &end, 0);
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	if (*end == '\0') {
3650Sstevel@tonic-gate 		if (entno >= TSTAT_NENT)
3660Sstevel@tonic-gate 			goto bad_entry;
3670Sstevel@tonic-gate 	} else {
3680Sstevel@tonic-gate 		for (entno = 0; entno < TSTAT_NENT; entno++) {
369*12719SRod.Evans@Sun.COM 			tstat_ent_t	*gtp;
370*12719SRod.Evans@Sun.COM 
371*12719SRod.Evans@Sun.COM 			if ((gtp = get_trap_ent(entno)) == NULL)
3720Sstevel@tonic-gate 				continue;
3730Sstevel@tonic-gate 
374*12719SRod.Evans@Sun.COM 			if (gtp->tent_type != TSTAT_ENT_USED)
375*12719SRod.Evans@Sun.COM 				continue;
376*12719SRod.Evans@Sun.COM 
377*12719SRod.Evans@Sun.COM 			if (strcmp(entry, gtp->tent_name) == 0)
3780Sstevel@tonic-gate 				break;
3790Sstevel@tonic-gate 		}
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 		if (entno == TSTAT_NENT)
3820Sstevel@tonic-gate 			goto bad_entry;
3830Sstevel@tonic-gate 	}
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	if (ioctl(g_fd, TSTATIOC_ENTRY, entno) == -1)
3860Sstevel@tonic-gate 		fatal("TSTATIOC_ENTRY failed for entry 0x%x", entno);
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	g_active[entno] = 1;
3890Sstevel@tonic-gate 	return;
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate bad_entry:
3920Sstevel@tonic-gate 	(void) fprintf(stderr, TSTAT_COMMAND ": invalid entry '%s'", entry);
3930Sstevel@tonic-gate 	(void) fprintf(stderr, "; valid entries:\n\n");
3940Sstevel@tonic-gate 	print_entries(stderr, 0);
3950Sstevel@tonic-gate 	exit(EXIT_FAILURE);
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate static void
select_cpu(processorid_t cpu)3990Sstevel@tonic-gate select_cpu(processorid_t cpu)
4000Sstevel@tonic-gate {
4010Sstevel@tonic-gate 	if (g_pset != PS_NONE)
4020Sstevel@tonic-gate 		fatal("cannot specify both a processor set and a processor\n");
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	if (cpu < 0 || cpu >= g_max_cpus)
4050Sstevel@tonic-gate 		fatal("cpu %d out of range\n", cpu);
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	if (p_online(cpu, P_STATUS) == -1) {
4080Sstevel@tonic-gate 		if (errno != EINVAL)
4090Sstevel@tonic-gate 			fatal("could not get status for cpu %d", cpu);
4100Sstevel@tonic-gate 		fatal("cpu %d not present\n", cpu);
4110Sstevel@tonic-gate 	}
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	g_selected[cpu] = 1;
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate static void
select_cpus(processorid_t low,processorid_t high)4170Sstevel@tonic-gate select_cpus(processorid_t low, processorid_t high)
4180Sstevel@tonic-gate {
4190Sstevel@tonic-gate 	if (g_pset != PS_NONE)
4200Sstevel@tonic-gate 		fatal("cannot specify both a processor set and processors\n");
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	if (low < 0 || low >= g_max_cpus)
4230Sstevel@tonic-gate 		fatal("invalid cpu '%d'\n", low);
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	if (high < 0 || high >= g_max_cpus)
4260Sstevel@tonic-gate 		fatal("invalid cpu '%d'\n", high);
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	if (low >= high)
4290Sstevel@tonic-gate 		fatal("invalid range '%d' to '%d'\n", low, high);
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	do {
4320Sstevel@tonic-gate 		if (p_online(low, P_STATUS) != -1)
4330Sstevel@tonic-gate 			g_selected[low] = 1;
4340Sstevel@tonic-gate 	} while (++low <= high);
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate static void
select_pset(psetid_t pset)4380Sstevel@tonic-gate select_pset(psetid_t pset)
4390Sstevel@tonic-gate {
4400Sstevel@tonic-gate 	processorid_t i;
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	if (pset < 0)
4430Sstevel@tonic-gate 		fatal("processor set %d is out of range\n", pset);
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	/*
4460Sstevel@tonic-gate 	 * Only one processor set can be specified.
4470Sstevel@tonic-gate 	 */
4480Sstevel@tonic-gate 	if (g_pset != PS_NONE)
4490Sstevel@tonic-gate 		fatal("at most one processor set may be specified\n");
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	/*
4520Sstevel@tonic-gate 	 * One cannot select processors _and_ a processor set.
4530Sstevel@tonic-gate 	 */
4540Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus; i++)
4550Sstevel@tonic-gate 		if (g_selected[i])
4560Sstevel@tonic-gate 			break;
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	if (i != g_max_cpus)
4590Sstevel@tonic-gate 		fatal("cannot specify both a processor and a processor set\n");
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	g_pset = pset;
4620Sstevel@tonic-gate 	g_pset_ncpus = g_max_cpus;
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	if (pset_info(g_pset, NULL, &g_pset_ncpus, g_pset_cpus) == -1)
4650Sstevel@tonic-gate 		fatal("invalid processor set: %d\n", g_pset);
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	if (g_pset_ncpus == 0)
4680Sstevel@tonic-gate 		fatal("processor set %d empty\n", g_pset);
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	if (ioctl(g_fd, TSTATIOC_NOCPU) == -1)
4710Sstevel@tonic-gate 		fatal("TSTATIOC_NOCPU failed");
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	for (i = 0; i < g_pset_ncpus; i++)
4740Sstevel@tonic-gate 		g_selected[g_pset_cpus[i]] = 1;
4750Sstevel@tonic-gate }
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate static void
check_pset(void)4780Sstevel@tonic-gate check_pset(void)
4790Sstevel@tonic-gate {
4800Sstevel@tonic-gate 	uint_t ncpus = g_max_cpus;
4810Sstevel@tonic-gate 	processorid_t i;
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	if (g_pset == PS_NONE)
4840Sstevel@tonic-gate 		return;
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	if (pset_info(g_pset, NULL, &ncpus, g_pset_cpus) == -1) {
4870Sstevel@tonic-gate 		if (errno == EINVAL)
4880Sstevel@tonic-gate 			fatal("processor set %d destroyed\n", g_pset);
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 		fatal("couldn't get info for processor set %d", g_pset);
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	if (ncpus == 0)
4940Sstevel@tonic-gate 		fatal("processor set %d empty\n", g_pset);
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	if (ncpus == g_pset_ncpus) {
4970Sstevel@tonic-gate 		for (i = 0; i < g_pset_ncpus; i++) {
4980Sstevel@tonic-gate 			if (!g_selected[g_pset_cpus[i]])
4990Sstevel@tonic-gate 				break;
5000Sstevel@tonic-gate 		}
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 		/*
5030Sstevel@tonic-gate 		 * If the number of CPUs hasn't changed, and every CPU
5040Sstevel@tonic-gate 		 * in the processor set is also selected, we know that the
5050Sstevel@tonic-gate 		 * processor set itself hasn't changed.
5060Sstevel@tonic-gate 		 */
5070Sstevel@tonic-gate 		if (i == g_pset_ncpus)
5080Sstevel@tonic-gate 			return;
5090Sstevel@tonic-gate 	}
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	/*
5120Sstevel@tonic-gate 	 * If we're here, we have a new processor set.  First, we need
5130Sstevel@tonic-gate 	 * to zero out the selection array.
5140Sstevel@tonic-gate 	 */
5150Sstevel@tonic-gate 	bzero(g_selected, sizeof (int8_t) * g_max_cpus);
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	g_pset_ncpus = ncpus;
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	if (ioctl(g_fd, TSTATIOC_STOP) == -1)
5200Sstevel@tonic-gate 		fatal("TSTATIOC_STOP failed");
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	if (ioctl(g_fd, TSTATIOC_NOCPU) == -1)
5230Sstevel@tonic-gate 		fatal("TSATIOC_NOCPU failed");
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	for (i = 0; i < g_pset_ncpus; i++) {
5260Sstevel@tonic-gate 		g_selected[g_pset_cpus[i]] = 1;
5270Sstevel@tonic-gate 		if (ioctl(g_fd, TSTATIOC_CPU, g_pset_cpus[i]) == -1)
5280Sstevel@tonic-gate 			fatal("TSTATIOC_CPU failed for cpu %d", i);
5290Sstevel@tonic-gate 	}
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	/*
5320Sstevel@tonic-gate 	 * Now that we have selected the CPUs, we're going to reenable
5330Sstevel@tonic-gate 	 * trapstat, and reread the data for the current generation.
5340Sstevel@tonic-gate 	 */
5350Sstevel@tonic-gate 	if (ioctl(g_fd, TSTATIOC_GO) == -1)
5360Sstevel@tonic-gate 		fatal("TSTATIOC_GO failed");
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	if (ioctl(g_fd, TSTATIOC_READ, g_data[g_gen]) == -1)
5390Sstevel@tonic-gate 		fatal("TSTATIOC_READ failed");
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate static void
missdata(tstat_missdata_t * miss,tstat_missdata_t * omiss)5430Sstevel@tonic-gate missdata(tstat_missdata_t *miss, tstat_missdata_t *omiss)
5440Sstevel@tonic-gate {
5450Sstevel@tonic-gate 	hrtime_t ts = g_ndata->tdata_snapts - g_odata->tdata_snapts;
5460Sstevel@tonic-gate 	hrtime_t tick = g_ndata->tdata_snaptick - g_odata->tdata_snaptick;
5470Sstevel@tonic-gate 	uint64_t raw = miss->tmiss_count - omiss->tmiss_count;
5480Sstevel@tonic-gate 	uint64_t diff = g_absolute ? miss->tmiss_count :
5490Sstevel@tonic-gate 	    (uint64_t)(0.5 + g_interval /
5500Sstevel@tonic-gate 	    (double)ts * (double)(miss->tmiss_count - omiss->tmiss_count));
5510Sstevel@tonic-gate 	hrtime_t peffect = raw * g_ndata->tdata_peffect * g_peffect, time;
5520Sstevel@tonic-gate 	double p;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	/*
5550Sstevel@tonic-gate 	 * Now we need to account for the trapstat probe effect.  Take
5560Sstevel@tonic-gate 	 * the amount of time spent in the handler, and add the
5570Sstevel@tonic-gate 	 * amount of time known to be due to the trapstat probe effect.
5580Sstevel@tonic-gate 	 */
5590Sstevel@tonic-gate 	time = miss->tmiss_time - omiss->tmiss_time + peffect;
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	if (time >= tick) {
5620Sstevel@tonic-gate 		/*
5630Sstevel@tonic-gate 		 * This really shouldn't happen unless our calculation of
5640Sstevel@tonic-gate 		 * the probe effect was vastly incorrect.  In any case,
5650Sstevel@tonic-gate 		 * print 99.9 for the time instead of printing negative
5660Sstevel@tonic-gate 		 * values...
5670Sstevel@tonic-gate 		 */
5680Sstevel@tonic-gate 		time = tick / 1000 * 999;
5690Sstevel@tonic-gate 	}
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	p = (double)time / (double)tick * (double)100.0;
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	(*g_process)(g_arg, diff, p);
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate static void
tlbdata(tstat_tlbdata_t * tlb,tstat_tlbdata_t * otlb)5770Sstevel@tonic-gate tlbdata(tstat_tlbdata_t *tlb, tstat_tlbdata_t *otlb)
5780Sstevel@tonic-gate {
5790Sstevel@tonic-gate 	missdata(&tlb->ttlb_tlb, &otlb->ttlb_tlb);
5800Sstevel@tonic-gate 	missdata(&tlb->ttlb_tsb, &otlb->ttlb_tsb);
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate static void
print_missdata(double * ttl,uint64_t diff,double p)5840Sstevel@tonic-gate print_missdata(double *ttl, uint64_t diff, double p)
5850Sstevel@tonic-gate {
5860Sstevel@tonic-gate 	TSTAT_PRINT_MISSDATA(diff, p);
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	if (ttl != NULL)
5890Sstevel@tonic-gate 		*ttl += p;
5900Sstevel@tonic-gate }
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate static void
print_modepgsz(char * prefix,tstat_modedata_t * data,tstat_modedata_t * odata)5930Sstevel@tonic-gate print_modepgsz(char *prefix, tstat_modedata_t *data, tstat_modedata_t *odata)
5940Sstevel@tonic-gate {
5950Sstevel@tonic-gate 	int ps;
5960Sstevel@tonic-gate 	size_t incr = sizeof (tstat_pgszdata_t);
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	for (ps = 0; ps < g_pgsizes; ps++) {
5990Sstevel@tonic-gate 		double ttl = 0.0;
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 		g_process = (void(*)(void *, uint64_t, double))print_missdata;
6020Sstevel@tonic-gate 		g_arg = &ttl;
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 		(void) printf("%s %4s|", prefix, g_pgnames[ps]);
6050Sstevel@tonic-gate 		tlbdata(&data->tmode_itlb, &odata->tmode_itlb);
6060Sstevel@tonic-gate 		(void) printf(" |");
6070Sstevel@tonic-gate 		tlbdata(&data->tmode_dtlb, &odata->tmode_dtlb);
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 		(void) printf(" |%4.1f\n", ttl);
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 		data = (tstat_modedata_t *)((uintptr_t)data + incr);
6120Sstevel@tonic-gate 		odata = (tstat_modedata_t *)((uintptr_t)odata + incr);
6130Sstevel@tonic-gate 	}
6140Sstevel@tonic-gate }
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate static void
parsable_modepgsz(char * prefix,tstat_modedata_t * data,tstat_modedata_t * odata)6170Sstevel@tonic-gate parsable_modepgsz(char *prefix, tstat_modedata_t *data, tstat_modedata_t *odata)
6180Sstevel@tonic-gate {
6190Sstevel@tonic-gate 	int ps;
6200Sstevel@tonic-gate 	size_t incr = sizeof (tstat_pgszdata_t);
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	g_process = (void(*)(void *, uint64_t, double))print_missdata;
6230Sstevel@tonic-gate 	g_arg = NULL;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	for (ps = 0; ps < g_pgsizes; ps++) {
6260Sstevel@tonic-gate 		(void) printf("%s %7d", prefix, g_pgsize[ps]);
6270Sstevel@tonic-gate 		tlbdata(&data->tmode_itlb, &odata->tmode_itlb);
6280Sstevel@tonic-gate 		tlbdata(&data->tmode_dtlb, &odata->tmode_dtlb);
6290Sstevel@tonic-gate 		(void) printf("\n");
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 		data = (tstat_modedata_t *)((uintptr_t)data + incr);
6320Sstevel@tonic-gate 		odata = (tstat_modedata_t *)((uintptr_t)odata + incr);
6330Sstevel@tonic-gate 	}
6340Sstevel@tonic-gate }
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate static void
sum_missdata(void * sump,uint64_t diff,double p)6370Sstevel@tonic-gate sum_missdata(void *sump, uint64_t diff, double p)
6380Sstevel@tonic-gate {
6390Sstevel@tonic-gate 	tstat_sum_t *sum = *((tstat_sum_t **)sump);
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	sum->tsum_diff += diff;
6420Sstevel@tonic-gate 	sum->tsum_time += p;
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	(*(tstat_sum_t **)sump)++;
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate static void
sum_modedata(tstat_modedata_t * data,tstat_modedata_t * odata,tstat_sum_t * sum)6480Sstevel@tonic-gate sum_modedata(tstat_modedata_t *data, tstat_modedata_t *odata, tstat_sum_t *sum)
6490Sstevel@tonic-gate {
6500Sstevel@tonic-gate 	int ps, incr = sizeof (tstat_pgszdata_t);
6510Sstevel@tonic-gate 	tstat_sum_t *sump;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	for (ps = 0; ps < g_pgsizes; ps++) {
6540Sstevel@tonic-gate 		sump = sum;
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 		g_process = sum_missdata;
6570Sstevel@tonic-gate 		g_arg = &sump;
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 		tlbdata(&data->tmode_itlb, &odata->tmode_itlb);
6600Sstevel@tonic-gate 		tlbdata(&data->tmode_dtlb, &odata->tmode_dtlb);
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 		data = (tstat_modedata_t *)((uintptr_t)data + incr);
6630Sstevel@tonic-gate 		odata = (tstat_modedata_t *)((uintptr_t)odata + incr);
6640Sstevel@tonic-gate 	}
6650Sstevel@tonic-gate }
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate static void
print_sum(tstat_sum_t * sum,int divisor)6680Sstevel@tonic-gate print_sum(tstat_sum_t *sum, int divisor)
6690Sstevel@tonic-gate {
6700Sstevel@tonic-gate 	int i;
6710Sstevel@tonic-gate 	double ttl = 0.0;
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	for (i = 0; i < 4; i++) {
6740Sstevel@tonic-gate 		if (i == 2)
6750Sstevel@tonic-gate 			(void) printf(" |");
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 		sum[i].tsum_time /= divisor;
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 		TSTAT_PRINT_MISSDATA(sum[i].tsum_diff, sum[i].tsum_time);
6800Sstevel@tonic-gate 		ttl += sum[i].tsum_time;
6810Sstevel@tonic-gate 	}
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	(void) printf(" |%4.1f\n", ttl);
6840Sstevel@tonic-gate }
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate static void
print_tlbpgsz(tstat_data_t * data,tstat_data_t * odata)6870Sstevel@tonic-gate print_tlbpgsz(tstat_data_t *data, tstat_data_t *odata)
6880Sstevel@tonic-gate {
6890Sstevel@tonic-gate 	int i, cpu, ncpus = 0;
6900Sstevel@tonic-gate 	char pre[12];
6910Sstevel@tonic-gate 	tstat_sum_t sum[4];
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	(void) printf("cpu m size| %9s %4s %9s %4s | %9s %4s %9s %4s |%4s\n"
6940Sstevel@tonic-gate 	    "----------+-------------------------------+-----------------------"
6950Sstevel@tonic-gate 	    "--------+----\n", "itlb-miss", "%tim", "itsb-miss", "%tim",
6960Sstevel@tonic-gate 	    "dtlb-miss", "%tim", "dtsb-miss", "%tim", "%tim");
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	bzero(sum, sizeof (sum));
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus; i++) {
7010Sstevel@tonic-gate 		tstat_pgszdata_t *pgsz = data->tdata_pgsz;
7020Sstevel@tonic-gate 		tstat_pgszdata_t *opgsz = odata->tdata_pgsz;
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 		if ((cpu = data->tdata_cpuid) == -1)
7050Sstevel@tonic-gate 			break;
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 		if (i != 0)
7080Sstevel@tonic-gate 			(void) printf("----------+-----------------------------"
7090Sstevel@tonic-gate 			    "--+-------------------------------+----\n");
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 		g_ndata = data;
7120Sstevel@tonic-gate 		g_odata = odata;
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 		(void) sprintf(pre, "%3d u", cpu);
7150Sstevel@tonic-gate 		print_modepgsz(pre, &pgsz->tpgsz_user, &opgsz->tpgsz_user);
7160Sstevel@tonic-gate 		sum_modedata(&pgsz->tpgsz_user, &opgsz->tpgsz_user, sum);
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 		(void) printf("- - - - - + - - - - - - - - - - - - - -"
7190Sstevel@tonic-gate 		    " - + - - - - - - - - - - - - - - - + - -\n");
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 		(void) sprintf(pre, "%3d k", cpu);
7220Sstevel@tonic-gate 		print_modepgsz(pre, &pgsz->tpgsz_kernel, &opgsz->tpgsz_kernel);
7230Sstevel@tonic-gate 		sum_modedata(&pgsz->tpgsz_kernel, &opgsz->tpgsz_kernel, sum);
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 		data = (tstat_data_t *)((uintptr_t)data + g_datasize);
7260Sstevel@tonic-gate 		odata = (tstat_data_t *)((uintptr_t)odata + g_datasize);
7270Sstevel@tonic-gate 		ncpus++;
7280Sstevel@tonic-gate 	}
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	(void) printf("==========+===============================+========="
7310Sstevel@tonic-gate 	    "======================+====\n");
7320Sstevel@tonic-gate 	(void) printf("      ttl |");
7330Sstevel@tonic-gate 	print_sum(sum, ncpus);
7340Sstevel@tonic-gate 	(void) printf("\n");
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate static void
parsable_tlbpgsz(tstat_data_t * data,tstat_data_t * odata)7380Sstevel@tonic-gate parsable_tlbpgsz(tstat_data_t *data, tstat_data_t *odata)
7390Sstevel@tonic-gate {
7400Sstevel@tonic-gate 	int i, cpu;
7410Sstevel@tonic-gate 	char pre[30];
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus; i++) {
7440Sstevel@tonic-gate 		tstat_pgszdata_t *pgsz = data->tdata_pgsz;
7450Sstevel@tonic-gate 		tstat_pgszdata_t *opgsz = odata->tdata_pgsz;
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 		if ((cpu = data->tdata_cpuid) == -1)
7480Sstevel@tonic-gate 			break;
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 		g_ndata = data;
7510Sstevel@tonic-gate 		g_odata = odata;
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 		(void) sprintf(pre, "%lld %3d u",
7540Sstevel@tonic-gate 		    data->tdata_snapts - g_start, cpu);
7550Sstevel@tonic-gate 		parsable_modepgsz(pre, &pgsz->tpgsz_user, &opgsz->tpgsz_user);
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 		pre[strlen(pre) - 1] = 'k';
7580Sstevel@tonic-gate 		parsable_modepgsz(pre, &pgsz->tpgsz_kernel,
7590Sstevel@tonic-gate 		    &opgsz->tpgsz_kernel);
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 		data = (tstat_data_t *)((uintptr_t)data + g_datasize);
7620Sstevel@tonic-gate 		odata = (tstat_data_t *)((uintptr_t)odata + g_datasize);
7630Sstevel@tonic-gate 	}
7640Sstevel@tonic-gate }
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate static void
print_modedata(tstat_modedata_t * data,tstat_modedata_t * odata,int parsable)7670Sstevel@tonic-gate print_modedata(tstat_modedata_t *data, tstat_modedata_t *odata, int parsable)
7680Sstevel@tonic-gate {
7690Sstevel@tonic-gate 	int ps, i;
7700Sstevel@tonic-gate 	size_t incr = sizeof (tstat_pgszdata_t);
7710Sstevel@tonic-gate 	tstat_sum_t sum[4], *sump = sum;
7720Sstevel@tonic-gate 	double ttl = 0.0;
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	bzero(sum, sizeof (sum));
7750Sstevel@tonic-gate 	g_process = sum_missdata;
7760Sstevel@tonic-gate 	g_arg = &sump;
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	for (ps = 0; ps < g_pgsizes; ps++) {
7790Sstevel@tonic-gate 		tlbdata(&data->tmode_itlb, &odata->tmode_itlb);
7800Sstevel@tonic-gate 		tlbdata(&data->tmode_dtlb, &odata->tmode_dtlb);
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 		data = (tstat_modedata_t *)((uintptr_t)data + incr);
7830Sstevel@tonic-gate 		odata = (tstat_modedata_t *)((uintptr_t)odata + incr);
7840Sstevel@tonic-gate 		sump = sum;
7850Sstevel@tonic-gate 	}
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	for (i = 0; i < 4; i++) {
7880Sstevel@tonic-gate 		if (i == 2 && !parsable)
7890Sstevel@tonic-gate 			(void) printf(" |");
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 		TSTAT_PRINT_MISSDATA(sum[i].tsum_diff, sum[i].tsum_time);
7920Sstevel@tonic-gate 		ttl += sum[i].tsum_time;
7930Sstevel@tonic-gate 	}
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	if (parsable) {
7960Sstevel@tonic-gate 		(void) printf("\n");
7970Sstevel@tonic-gate 		return;
7980Sstevel@tonic-gate 	}
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	(void) printf(" |%4.1f\n", ttl);
8010Sstevel@tonic-gate }
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate static void
print_tlb(tstat_data_t * data,tstat_data_t * odata)8040Sstevel@tonic-gate print_tlb(tstat_data_t *data, tstat_data_t *odata)
8050Sstevel@tonic-gate {
8060Sstevel@tonic-gate 	int i, cpu, ncpus = 0;
8070Sstevel@tonic-gate 	tstat_sum_t sum[4];
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	(void) printf("cpu m| %9s %4s %9s %4s | %9s %4s %9s %4s |%4s\n"
8100Sstevel@tonic-gate 	    "-----+-------------------------------+-----------------------"
8110Sstevel@tonic-gate 	    "--------+----\n", "itlb-miss", "%tim", "itsb-miss", "%tim",
8120Sstevel@tonic-gate 	    "dtlb-miss", "%tim", "dtsb-miss", "%tim", "%tim");
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	bzero(sum, sizeof (sum));
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus; i++) {
8170Sstevel@tonic-gate 		tstat_pgszdata_t *pgsz = data->tdata_pgsz;
8180Sstevel@tonic-gate 		tstat_pgszdata_t *opgsz = odata->tdata_pgsz;
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 		if ((cpu = data->tdata_cpuid) == -1)
8210Sstevel@tonic-gate 			break;
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 		if (i != 0)
8240Sstevel@tonic-gate 			(void) printf("-----+-------------------------------+-"
8250Sstevel@tonic-gate 			    "------------------------------+----\n");
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 		g_ndata = data;
8280Sstevel@tonic-gate 		g_odata = odata;
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 		(void) printf("%3d u|", cpu);
8310Sstevel@tonic-gate 		print_modedata(&pgsz->tpgsz_user, &opgsz->tpgsz_user, 0);
8320Sstevel@tonic-gate 		sum_modedata(&pgsz->tpgsz_user, &opgsz->tpgsz_user, sum);
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 		(void) printf("%3d k|", cpu);
8350Sstevel@tonic-gate 		print_modedata(&pgsz->tpgsz_kernel, &opgsz->tpgsz_kernel, 0);
8360Sstevel@tonic-gate 		sum_modedata(&pgsz->tpgsz_kernel, &opgsz->tpgsz_kernel, sum);
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 		data = (tstat_data_t *)((uintptr_t)data + g_datasize);
8390Sstevel@tonic-gate 		odata = (tstat_data_t *)((uintptr_t)odata + g_datasize);
8400Sstevel@tonic-gate 		ncpus++;
8410Sstevel@tonic-gate 	}
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	(void) printf("=====+===============================+========="
8440Sstevel@tonic-gate 	    "======================+====\n");
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	(void) printf(" ttl |");
8470Sstevel@tonic-gate 	print_sum(sum, ncpus);
8480Sstevel@tonic-gate 	(void) printf("\n");
8490Sstevel@tonic-gate }
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate static void
parsable_tlb(tstat_data_t * data,tstat_data_t * odata)8520Sstevel@tonic-gate parsable_tlb(tstat_data_t *data, tstat_data_t *odata)
8530Sstevel@tonic-gate {
8540Sstevel@tonic-gate 	int i, cpu;
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus; i++) {
8570Sstevel@tonic-gate 		tstat_pgszdata_t *pgsz = data->tdata_pgsz;
8580Sstevel@tonic-gate 		tstat_pgszdata_t *opgsz = odata->tdata_pgsz;
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 		if ((cpu = data->tdata_cpuid) == -1)
8610Sstevel@tonic-gate 			break;
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 		g_ndata = data;
8640Sstevel@tonic-gate 		g_odata = odata;
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 		(void) printf("%lld %3d u ", data->tdata_snapts - g_start, cpu);
8670Sstevel@tonic-gate 		print_modedata(&pgsz->tpgsz_user, &opgsz->tpgsz_user, 1);
8680Sstevel@tonic-gate 		(void) printf("%lld %3d k ", data->tdata_snapts - g_start, cpu);
8690Sstevel@tonic-gate 		print_modedata(&pgsz->tpgsz_kernel, &opgsz->tpgsz_kernel, 1);
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 		data = (tstat_data_t *)((uintptr_t)data + g_datasize);
8720Sstevel@tonic-gate 		odata = (tstat_data_t *)((uintptr_t)odata + g_datasize);
8730Sstevel@tonic-gate 	}
8740Sstevel@tonic-gate }
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate static void
print_stats(tstat_data_t * data,tstat_data_t * odata)8770Sstevel@tonic-gate print_stats(tstat_data_t *data, tstat_data_t *odata)
8780Sstevel@tonic-gate {
8790Sstevel@tonic-gate 	int i, j, k, done;
8800Sstevel@tonic-gate 	processorid_t id;
8810Sstevel@tonic-gate 	tstat_data_t *base = data;
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	/*
8840Sstevel@tonic-gate 	 * First, blast through all of the data updating our array
8850Sstevel@tonic-gate 	 * of active traps.  We keep an array of active traps to prevent
8860Sstevel@tonic-gate 	 * printing lines for traps that are never seen -- while still printing
8870Sstevel@tonic-gate 	 * lines for traps that have been seen only once on some CPU.
8880Sstevel@tonic-gate 	 */
8890Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus; i++) {
8900Sstevel@tonic-gate 		if (data[i].tdata_cpuid == -1)
8910Sstevel@tonic-gate 			break;
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 		for (j = 0; j < TSTAT_NENT; j++) {
8940Sstevel@tonic-gate 			if (!data[i].tdata_traps[j] || g_active[j])
8950Sstevel@tonic-gate 				continue;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 			g_active[j] = 1;
8980Sstevel@tonic-gate 		}
8990Sstevel@tonic-gate 	}
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	data = base;
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	for (done = 0; !done; data += g_cpus_per_line) {
9040Sstevel@tonic-gate 		for (i = 0; i < g_cpus_per_line; i++) {
9050Sstevel@tonic-gate 			if (&data[i] - base >= g_max_cpus)
9060Sstevel@tonic-gate 				break;
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 			if ((id = data[i].tdata_cpuid) == -1)
9090Sstevel@tonic-gate 				break;
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 			if (i == 0)
9120Sstevel@tonic-gate 				(void) printf("vct name                |");
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 			(void) printf("   %scpu%d", id >= 100 ? "" :
9150Sstevel@tonic-gate 			    id >= 10 ? " " : "  ", id);
9160Sstevel@tonic-gate 		}
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 		if (i == 0)
9190Sstevel@tonic-gate 			break;
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 		if (i != g_cpus_per_line)
9220Sstevel@tonic-gate 			done = 1;
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 		(void) printf("\n------------------------+");
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 		for (j = 0; j < i; j++)
9270Sstevel@tonic-gate 			(void) printf("---------");
9280Sstevel@tonic-gate 		(void) printf("\n");
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 		for (j = 0; j < TSTAT_NENT; j++) {
931*12719SRod.Evans@Sun.COM 			tstat_ent_t	*gtp;
932*12719SRod.Evans@Sun.COM 
933*12719SRod.Evans@Sun.COM 			if ((!g_active[j]) || ((gtp = get_trap_ent(j)) == NULL))
9340Sstevel@tonic-gate 				continue;
9350Sstevel@tonic-gate 
936*12719SRod.Evans@Sun.COM 			(void) printf("%3x %-20s|", j, gtp->tent_name);
9370Sstevel@tonic-gate 			for (k = 0; k < i; k++) {
9380Sstevel@tonic-gate 				(void) printf(" %8lld", TSTAT_DELTA(&data[k],
9390Sstevel@tonic-gate 				    &odata[data - base + k], tdata_traps[j]));
9400Sstevel@tonic-gate 			}
9410Sstevel@tonic-gate 			(void) printf("\n");
9420Sstevel@tonic-gate 		}
9430Sstevel@tonic-gate 		(void) printf("\n");
9440Sstevel@tonic-gate 	}
9450Sstevel@tonic-gate }
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate static void
parsable_stats(tstat_data_t * data,tstat_data_t * odata)9480Sstevel@tonic-gate parsable_stats(tstat_data_t *data, tstat_data_t *odata)
9490Sstevel@tonic-gate {
9500Sstevel@tonic-gate 	tstat_data_t *base;
9510Sstevel@tonic-gate 	int i;
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	for (base = data; data - base < g_max_cpus; data++, odata++) {
9540Sstevel@tonic-gate 		if (data->tdata_cpuid == -1)
9550Sstevel@tonic-gate 			break;
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 		for (i = 0; i < TSTAT_NENT; i++) {
958*12719SRod.Evans@Sun.COM 			tstat_ent_t	*gtp;
959*12719SRod.Evans@Sun.COM 
960*12719SRod.Evans@Sun.COM 			if ((!data->tdata_traps[i] && !g_active[i]) ||
961*12719SRod.Evans@Sun.COM 			    ((gtp = get_trap_ent(i)) == NULL))
9620Sstevel@tonic-gate 				continue;
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 			(void) printf("%lld %d %x %s ",
9650Sstevel@tonic-gate 			    data->tdata_snapts - g_start, data->tdata_cpuid, i,
966*12719SRod.Evans@Sun.COM 			    gtp->tent_name);
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 			(void) printf("%lld\n", TSTAT_DELTA(data, odata,
9690Sstevel@tonic-gate 			    tdata_traps[i]));
9700Sstevel@tonic-gate 		}
9710Sstevel@tonic-gate 	}
9720Sstevel@tonic-gate }
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate static void
check_data(tstat_data_t * data,tstat_data_t * odata)9750Sstevel@tonic-gate check_data(tstat_data_t *data, tstat_data_t *odata)
9760Sstevel@tonic-gate {
9770Sstevel@tonic-gate 	tstat_data_t *ndata;
9780Sstevel@tonic-gate 	int i;
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	if (data->tdata_cpuid == -1) {
9810Sstevel@tonic-gate 		/*
9820Sstevel@tonic-gate 		 * The last CPU we were watching must have been DR'd out
9830Sstevel@tonic-gate 		 * of the system.  Print a vaguely useful message and exit.
9840Sstevel@tonic-gate 		 */
9850Sstevel@tonic-gate 		fatal("all initially selected CPUs have been unconfigured\n");
9860Sstevel@tonic-gate 	}
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 	/*
9890Sstevel@tonic-gate 	 * If a CPU is DR'd out of the system, we'll stop receiving data
9900Sstevel@tonic-gate 	 * for it.  CPUs are never added, however (that is, if a CPU is
9910Sstevel@tonic-gate 	 * DR'd into the system, we won't automatically start receiving
9920Sstevel@tonic-gate 	 * data for it).  We check for this by making sure that all of
9930Sstevel@tonic-gate 	 * the CPUs present in the old data are present in the new data.
9940Sstevel@tonic-gate 	 * If we find one missing in the new data, we correct the old data
9950Sstevel@tonic-gate 	 * by removing the old CPU.  This assures that delta are printed
9960Sstevel@tonic-gate 	 * correctly.
9970Sstevel@tonic-gate 	 */
9980Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus; i++) {
9990Sstevel@tonic-gate 		if (odata->tdata_cpuid == -1)
10000Sstevel@tonic-gate 			return;
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 		if (data->tdata_cpuid != odata->tdata_cpuid)
10030Sstevel@tonic-gate 			break;
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 		data = (tstat_data_t *)((uintptr_t)data + g_datasize);
10060Sstevel@tonic-gate 		odata = (tstat_data_t *)((uintptr_t)odata + g_datasize);
10070Sstevel@tonic-gate 	}
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	if (i == g_max_cpus)
10100Sstevel@tonic-gate 		return;
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 	/*
10130Sstevel@tonic-gate 	 * If we're here, we know that the odata is a CPU which has been
10140Sstevel@tonic-gate 	 * DR'd out.  We'll now smoosh it out of the old data.
10150Sstevel@tonic-gate 	 */
10160Sstevel@tonic-gate 	for (odata->tdata_cpuid = -1; i < g_max_cpus - 1; i++) {
10170Sstevel@tonic-gate 		ndata = (tstat_data_t *)((uintptr_t)odata + g_datasize);
10180Sstevel@tonic-gate 		bcopy(ndata, odata, g_datasize);
10190Sstevel@tonic-gate 		ndata->tdata_cpuid = -1;
10200Sstevel@tonic-gate 	}
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	/*
10230Sstevel@tonic-gate 	 * There may be other CPUs DR'd out; tail-call recurse.
10240Sstevel@tonic-gate 	 */
10250Sstevel@tonic-gate 	check_data(data, odata);
10260Sstevel@tonic-gate }
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate int
main(int argc,char ** argv)10290Sstevel@tonic-gate main(int argc, char **argv)
10300Sstevel@tonic-gate {
10310Sstevel@tonic-gate 	processorid_t id;
10320Sstevel@tonic-gate 	char c, *end;
10330Sstevel@tonic-gate 	ulong_t indefinite;
10340Sstevel@tonic-gate 	long count = 0, rate = 0;
10350Sstevel@tonic-gate 	int list = 0, parsable = 0;
10360Sstevel@tonic-gate 	void (*print)(tstat_data_t *, tstat_data_t *);
10370Sstevel@tonic-gate 	sigset_t set;
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	struct {
10400Sstevel@tonic-gate 		char opt;
10410Sstevel@tonic-gate 		void (*print)(tstat_data_t *, tstat_data_t *);
10420Sstevel@tonic-gate 		void (*parsable)(tstat_data_t *, tstat_data_t *);
10430Sstevel@tonic-gate 		int repeat;
10440Sstevel@tonic-gate 	} tab[] = {
10450Sstevel@tonic-gate 		{ '\0',	print_stats,	parsable_stats,		0 },
10460Sstevel@tonic-gate 		{ 'e',	print_stats,	parsable_stats,		1 },
10470Sstevel@tonic-gate 		{ 't',	print_tlb,	parsable_tlb,		0 },
10480Sstevel@tonic-gate 		{ 'T',	print_tlbpgsz,	parsable_tlbpgsz,	0 },
10490Sstevel@tonic-gate 		{ -1,	NULL,		NULL,			0 }
10500Sstevel@tonic-gate 	}, *tabent = NULL, *iter;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	uintptr_t offs = (uintptr_t)&tab->print - (uintptr_t)tab;
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	/*
10550Sstevel@tonic-gate 	 * If argv[0] is non-NULL, set argv[0] to keep any getopt(3C) output
10560Sstevel@tonic-gate 	 * consistent with other error output.
10570Sstevel@tonic-gate 	 */
10580Sstevel@tonic-gate 	if (argv[0] != NULL)
10590Sstevel@tonic-gate 		argv[0] = TSTAT_COMMAND;
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	if ((g_fd = open(TSTAT_DEVICE, O_RDWR)) == -1)
10620Sstevel@tonic-gate 		fatal("couldn't open " TSTAT_DEVICE);
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 	setup();
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "alnNtTc:C:r:e:P")) != EOF) {
10670Sstevel@tonic-gate 		/*
10680Sstevel@tonic-gate 		 * First, check to see if this option changes our printing
10690Sstevel@tonic-gate 		 * function.
10700Sstevel@tonic-gate 		 */
10710Sstevel@tonic-gate 		for (iter = tab; iter->opt >= 0; iter++) {
10720Sstevel@tonic-gate 			if (c != iter->opt)
10730Sstevel@tonic-gate 				continue;
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 			if (tabent != NULL) {
10760Sstevel@tonic-gate 				if (tabent == iter) {
10770Sstevel@tonic-gate 					if (tabent->repeat) {
10780Sstevel@tonic-gate 						/*
10790Sstevel@tonic-gate 						 * This option is allowed to
10800Sstevel@tonic-gate 						 * have repeats; break out.
10810Sstevel@tonic-gate 						 */
10820Sstevel@tonic-gate 						break;
10830Sstevel@tonic-gate 					}
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 					fatal("expected -%c at most once\n", c);
10860Sstevel@tonic-gate 				}
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 				fatal("only one of -%c, -%c expected\n",
10890Sstevel@tonic-gate 				    tabent->opt, c);
10900Sstevel@tonic-gate 			}
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 			tabent = iter;
10930Sstevel@tonic-gate 			break;
10940Sstevel@tonic-gate 		}
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 		switch (c) {
10970Sstevel@tonic-gate 		case 'a':
10980Sstevel@tonic-gate 			g_absolute = 1;
10990Sstevel@tonic-gate 			break;
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 		case 'e': {
11020Sstevel@tonic-gate 			char *s = strtok(optarg, ",");
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate 			while (s != NULL) {
11050Sstevel@tonic-gate 				select_entry(s);
11060Sstevel@tonic-gate 				s = strtok(NULL, ",");
11070Sstevel@tonic-gate 			}
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 			break;
11100Sstevel@tonic-gate 		}
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 		case 'l':
11130Sstevel@tonic-gate 			list = 1;
11140Sstevel@tonic-gate 			break;
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 		case 'n':
11170Sstevel@tonic-gate 			/*
11180Sstevel@tonic-gate 			 * This undocumented option prevents trapstat from
11190Sstevel@tonic-gate 			 * actually switching the %tba to point to the
11200Sstevel@tonic-gate 			 * interposing trap table.  It's very useful when
11210Sstevel@tonic-gate 			 * debugging trapstat bugs:  one can specify "-n"
11220Sstevel@tonic-gate 			 * and then examine the would-be interposing trap
11230Sstevel@tonic-gate 			 * table without running the risk of RED stating.
11240Sstevel@tonic-gate 			 */
11250Sstevel@tonic-gate 			if (ioctl(g_fd, TSTATIOC_NOGO) == -1)
11260Sstevel@tonic-gate 				fatal("TSTATIOC_NOGO");
11270Sstevel@tonic-gate 			break;
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 		case 'N':
11300Sstevel@tonic-gate 			/*
11310Sstevel@tonic-gate 			 * This undocumented option forces trapstat to ignore
11320Sstevel@tonic-gate 			 * its determined probe effect.  This may be useful
11330Sstevel@tonic-gate 			 * if it is believed that the probe effect has been
11340Sstevel@tonic-gate 			 * grossly overestimated.
11350Sstevel@tonic-gate 			 */
11360Sstevel@tonic-gate 			g_peffect = 0;
11370Sstevel@tonic-gate 			break;
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 		case 't':
11400Sstevel@tonic-gate 		case 'T':
11410Sstevel@tonic-gate 			/*
11420Sstevel@tonic-gate 			 * When running with TLB statistics, we want to
11430Sstevel@tonic-gate 			 * minimize probe effect by running with all other
11440Sstevel@tonic-gate 			 * entries explicitly disabled.
11450Sstevel@tonic-gate 			 */
11460Sstevel@tonic-gate 			if (ioctl(g_fd, TSTATIOC_NOENTRY) == -1)
11470Sstevel@tonic-gate 				fatal("TSTATIOC_NOENTRY");
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 			if (ioctl(g_fd, TSTATIOC_TLBDATA) == -1)
11500Sstevel@tonic-gate 				fatal("TSTATIOC_TLBDATA");
11510Sstevel@tonic-gate 			break;
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 		case 'c': {
11540Sstevel@tonic-gate 			/*
11550Sstevel@tonic-gate 			 * We allow CPUs to be specified as an optionally
11560Sstevel@tonic-gate 			 * comma separated list of either CPU IDs or ranges
11570Sstevel@tonic-gate 			 * of CPU IDs.
11580Sstevel@tonic-gate 			 */
11590Sstevel@tonic-gate 			char *s = strtok(optarg, ",");
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 			while (s != NULL) {
11620Sstevel@tonic-gate 				id = strtoul(s, &end, 0);
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 				if (id == ULONG_MAX && errno == ERANGE) {
11650Sstevel@tonic-gate 					*end = '\0';
11660Sstevel@tonic-gate 					fatal("invalid cpu '%s'\n", s);
11670Sstevel@tonic-gate 				}
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 				if (*(s = end) != '\0') {
11700Sstevel@tonic-gate 					processorid_t p;
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 					if (*s != '-')
11730Sstevel@tonic-gate 						fatal("invalid cpu '%s'\n", s);
11740Sstevel@tonic-gate 					p = strtoul(++s, &end, 0);
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 					if (*end != '\0' ||
11770Sstevel@tonic-gate 					    (p == ULONG_MAX && errno == ERANGE))
11780Sstevel@tonic-gate 						fatal("invalid cpu '%s'\n", s);
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 					select_cpus(id, p);
11810Sstevel@tonic-gate 				} else {
11820Sstevel@tonic-gate 					select_cpu(id);
11830Sstevel@tonic-gate 				}
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 				s = strtok(NULL, ",");
11860Sstevel@tonic-gate 			}
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 			break;
11890Sstevel@tonic-gate 		}
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 		case 'C': {
11920Sstevel@tonic-gate 			psetid_t pset = strtoul(optarg, &end, 0);
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 			if (*end != '\0' ||
11950Sstevel@tonic-gate 			    (pset == ULONG_MAX && errno == ERANGE))
11960Sstevel@tonic-gate 				fatal("invalid processor set '%s'\n", optarg);
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 			select_pset(pset);
11990Sstevel@tonic-gate 			break;
12000Sstevel@tonic-gate 		}
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 		case 'r': {
12030Sstevel@tonic-gate 			rate = strtol(optarg, &end, 0);
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 			if (*end != '\0' ||
12060Sstevel@tonic-gate 			    (rate == LONG_MAX && errno == ERANGE))
12070Sstevel@tonic-gate 				fatal("invalid rate '%s'\n", optarg);
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 			if (rate <= 0)
12100Sstevel@tonic-gate 				fatal("rate must be greater than zero\n");
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 			if (rate > TSTAT_MAX_RATE)
12130Sstevel@tonic-gate 				fatal("rate may not exceed %d\n",
12140Sstevel@tonic-gate 				    TSTAT_MAX_RATE);
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 			set_interval(NANOSEC / rate);
12170Sstevel@tonic-gate 			break;
12180Sstevel@tonic-gate 		}
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 		case 'P':
12210Sstevel@tonic-gate 			offs = (uintptr_t)&tab->parsable - (uintptr_t)tab;
12220Sstevel@tonic-gate 			parsable = 1;
12230Sstevel@tonic-gate 			break;
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 		default:
12260Sstevel@tonic-gate 			usage();
12270Sstevel@tonic-gate 		}
12280Sstevel@tonic-gate 	}
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 	if (list) {
12310Sstevel@tonic-gate 		print_entries(stdout, parsable);
12320Sstevel@tonic-gate 		exit(EXIT_SUCCESS);
12330Sstevel@tonic-gate 	}
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	if (optind != argc) {
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 		int interval = strtol(argv[optind], &end, 0);
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 		if (*end != '\0') {
12400Sstevel@tonic-gate 			/*
12410Sstevel@tonic-gate 			 * That wasn't a valid number.  It must be that we're
12420Sstevel@tonic-gate 			 * to execute this command.
12430Sstevel@tonic-gate 			 */
12440Sstevel@tonic-gate 			switch (vfork()) {
12450Sstevel@tonic-gate 			case 0:
12460Sstevel@tonic-gate 				(void) close(g_fd);
12470Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, &g_oset, NULL);
12480Sstevel@tonic-gate 				(void) execvp(argv[optind], &argv[optind]);
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate 				/*
12510Sstevel@tonic-gate 				 * No luck.  Set errno.
12520Sstevel@tonic-gate 				 */
12530Sstevel@tonic-gate 				g_exec_errno = errno;
12540Sstevel@tonic-gate 				_exit(EXIT_FAILURE);
12550Sstevel@tonic-gate 				/*NOTREACHED*/
12560Sstevel@tonic-gate 			case -1:
12570Sstevel@tonic-gate 				fatal("cannot fork");
12580Sstevel@tonic-gate 				/*NOTREACHED*/
12590Sstevel@tonic-gate 			default:
12600Sstevel@tonic-gate 				break;
12610Sstevel@tonic-gate 			}
12620Sstevel@tonic-gate 		} else {
12630Sstevel@tonic-gate 			if (interval <= 0)
12640Sstevel@tonic-gate 				fatal("interval must be greater than zero.\n");
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 			if (interval == LONG_MAX && errno == ERANGE)
12670Sstevel@tonic-gate 				fatal("invalid interval '%s'\n", argv[optind]);
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 			set_interval(NANOSEC * (hrtime_t)interval);
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 			if (++optind != argc) {
12720Sstevel@tonic-gate 				char *s = argv[optind];
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 				count = strtol(s, &end, 0);
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 				if (*end != '\0' || count <= 0 ||
12770Sstevel@tonic-gate 				    (count == LONG_MAX && errno == ERANGE))
12780Sstevel@tonic-gate 					fatal("invalid count '%s'\n", s);
12790Sstevel@tonic-gate 			}
12800Sstevel@tonic-gate 		}
12810Sstevel@tonic-gate 	} else {
12820Sstevel@tonic-gate 		if (!rate)
12830Sstevel@tonic-gate 			set_interval(NANOSEC);
12840Sstevel@tonic-gate 	}
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	if (tabent == NULL)
12870Sstevel@tonic-gate 		tabent = tab;
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 	print = *(void(**)(tstat_data_t *, tstat_data_t *))
12900Sstevel@tonic-gate 	    ((uintptr_t)tabent + offs);
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 	for (id = 0; id < g_max_cpus; id++) {
12930Sstevel@tonic-gate 		if (!g_selected[id])
12940Sstevel@tonic-gate 			continue;
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 		if (ioctl(g_fd, TSTATIOC_CPU, id) == -1)
12970Sstevel@tonic-gate 			fatal("TSTATIOC_CPU failed for cpu %d", id);
12980Sstevel@tonic-gate 	}
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 	g_start = gethrtime();
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	if (ioctl(g_fd, TSTATIOC_GO) == -1)
13030Sstevel@tonic-gate 		fatal("TSTATIOC_GO failed");
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 	if (ioctl(g_fd, TSTATIOC_READ, g_data[g_gen ^ 1]) == -1)
13060Sstevel@tonic-gate 		fatal("initial TSTATIOC_READ failed");
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 	(void) sigemptyset(&set);
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	for (indefinite = (count == 0); indefinite || count; count--) {
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 		(void) sigsuspend(&set);
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 		if (g_winch) {
13150Sstevel@tonic-gate 			g_winch = 0;
13160Sstevel@tonic-gate 			continue;
13170Sstevel@tonic-gate 		}
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate 		if (g_child_exited && g_exec_errno != 0) {
13200Sstevel@tonic-gate 			errno = g_exec_errno;
13210Sstevel@tonic-gate 			fatal("could not execute %s", argv[optind]);
13220Sstevel@tonic-gate 		}
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 		if (ioctl(g_fd, TSTATIOC_READ, g_data[g_gen]) == -1)
13250Sstevel@tonic-gate 			fatal("TSTATIOC_READ failed");
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 		/*
13280Sstevel@tonic-gate 		 * Before we blithely print the data, we need to
13290Sstevel@tonic-gate 		 * make sure that we haven't lost a CPU.
13300Sstevel@tonic-gate 		 */
13310Sstevel@tonic-gate 		check_data(g_data[g_gen], g_data[g_gen ^ 1]);
13320Sstevel@tonic-gate 		(*print)(g_data[g_gen], g_data[g_gen ^ 1]);
13330Sstevel@tonic-gate 		(void) fflush(stdout);
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 		if (g_child_exited) {
13360Sstevel@tonic-gate 			if (WIFEXITED(g_child_status)) {
13370Sstevel@tonic-gate 				if (WEXITSTATUS(g_child_status) == 0)
13380Sstevel@tonic-gate 					break;
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 				(void) fprintf(stderr, TSTAT_COMMAND ": "
13410Sstevel@tonic-gate 				    "warning: %s exited with code %d\n",
13420Sstevel@tonic-gate 				    argv[optind], WEXITSTATUS(g_child_status));
13430Sstevel@tonic-gate 			} else {
13440Sstevel@tonic-gate 				(void) fprintf(stderr, TSTAT_COMMAND ": "
13450Sstevel@tonic-gate 				    "warning: %s died on signal %d\n",
13460Sstevel@tonic-gate 				    argv[optind], WTERMSIG(g_child_status));
13470Sstevel@tonic-gate 			}
13480Sstevel@tonic-gate 			break;
13490Sstevel@tonic-gate 		}
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 		check_pset();
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 		g_gen ^= 1;
13540Sstevel@tonic-gate 	}
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	return (0);
13570Sstevel@tonic-gate }
1358