xref: /titanic_51/usr/src/cmd/trapstat/sun4/trapstat.c (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5f498645aSahl  * Common Development and Distribution License (the "License").
6f498645aSahl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21f498645aSahl 
227c478bd9Sstevel@tonic-gate /*
23*1e49577aSRod Evans  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <string.h>
297c478bd9Sstevel@tonic-gate #include <fcntl.h>
307c478bd9Sstevel@tonic-gate #include <errno.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <signal.h>
337c478bd9Sstevel@tonic-gate #include <strings.h>
347c478bd9Sstevel@tonic-gate #include <limits.h>
357c478bd9Sstevel@tonic-gate #include <sys/mman.h>
367c478bd9Sstevel@tonic-gate #include <sys/pset.h>
377c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
387c478bd9Sstevel@tonic-gate #include <sys/trapstat.h>
397c478bd9Sstevel@tonic-gate #include <sys/wait.h>
407c478bd9Sstevel@tonic-gate #include <stddef.h>
417c478bd9Sstevel@tonic-gate #include <termio.h>
42*1e49577aSRod Evans #include "_trapstat.h"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #define	TSTAT_DEVICE	"/dev/trapstat"
457c478bd9Sstevel@tonic-gate #define	TSTAT_COMMAND	"trapstat"
467c478bd9Sstevel@tonic-gate #define	TSTAT_DELTA(data, old, member) g_absolute ? (data)->member : \
477c478bd9Sstevel@tonic-gate 	(uint64_t)(0.5 + (g_interval / (double)((data)->tdata_snapts - \
487c478bd9Sstevel@tonic-gate 	(old)->tdata_snapts)) * (double)((data)->member - (old)->member))
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #define	TSTAT_PRINT_MISSDATA(diff, time) \
517c478bd9Sstevel@tonic-gate 	(void) printf(" %9lld %4.1f", (diff), (time));
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	TSTAT_PAGESIZE_MODIFIERS	" kmgtp"
547c478bd9Sstevel@tonic-gate #define	TSTAT_PAGESIZE_STRLEN		10
557c478bd9Sstevel@tonic-gate #define	TSTAT_MAX_RATE			5000
567c478bd9Sstevel@tonic-gate #define	TSTAT_COLUMN_OFFS	26
577c478bd9Sstevel@tonic-gate #define	TSTAT_COLUMNS_PER_CPU	9
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static tstat_data_t *g_data[2];
607c478bd9Sstevel@tonic-gate static tstat_data_t *g_ndata, *g_odata;
617c478bd9Sstevel@tonic-gate static processorid_t g_max_cpus;
627c478bd9Sstevel@tonic-gate static int8_t *g_selected;
637c478bd9Sstevel@tonic-gate static timer_t g_tid;
647c478bd9Sstevel@tonic-gate static int g_interval = NANOSEC;
657c478bd9Sstevel@tonic-gate static int g_peffect = 1;
667c478bd9Sstevel@tonic-gate static int g_absolute = 0;
677c478bd9Sstevel@tonic-gate static sigset_t g_oset;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate static psetid_t g_pset = PS_NONE;
707c478bd9Sstevel@tonic-gate static processorid_t *g_pset_cpus;
717c478bd9Sstevel@tonic-gate static uint_t g_pset_ncpus;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static int g_cpus_per_line = (80 - TSTAT_COLUMN_OFFS) / TSTAT_COLUMNS_PER_CPU;
747c478bd9Sstevel@tonic-gate static int g_winch;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static int g_pgsizes;
777c478bd9Sstevel@tonic-gate static size_t *g_pgsize;
787c478bd9Sstevel@tonic-gate static char **g_pgnames;
797c478bd9Sstevel@tonic-gate static size_t g_datasize;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static int g_gen;
827c478bd9Sstevel@tonic-gate static int g_fd;
837c478bd9Sstevel@tonic-gate static uint8_t g_active[TSTAT_NENT];
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate static hrtime_t g_start;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static int g_exec_errno;
887c478bd9Sstevel@tonic-gate static int g_child_exited;
897c478bd9Sstevel@tonic-gate static int g_child_status;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static void (*g_process)(void *, uint64_t, double);
927c478bd9Sstevel@tonic-gate static void *g_arg;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate typedef struct tstat_sum {
957c478bd9Sstevel@tonic-gate 	uint64_t	tsum_diff;
967c478bd9Sstevel@tonic-gate 	double		tsum_time;
977c478bd9Sstevel@tonic-gate } tstat_sum_t;
987c478bd9Sstevel@tonic-gate 
99*1e49577aSRod Evans /*
100*1e49577aSRod Evans  * Define a dummy g_traps reader to establish a symbol capabilities lead.
101*1e49577aSRod Evans  * This routine should never be called, as the sun4u and sun4v variants
102*1e49577aSRod Evans  * will be used as appropriate.
103*1e49577aSRod Evans  */
104*1e49577aSRod Evans /* ARGSUSED0 */
105*1e49577aSRod Evans tstat_ent_t *
106*1e49577aSRod Evans get_trap_ent(int ndx)
107*1e49577aSRod Evans {
108*1e49577aSRod Evans 	return (NULL);
109*1e49577aSRod Evans }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static void
1127c478bd9Sstevel@tonic-gate usage(void)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
1157c478bd9Sstevel@tonic-gate 	    "\nusage:  trapstat [ -t | -T | -e entrylist ]\n"
1167c478bd9Sstevel@tonic-gate 	    "   [ -C psrset | -c cpulist ]\n"
1177c478bd9Sstevel@tonic-gate 	    "   [ -P ] [ -a ] [ -r rate ] [[ interval [ count ] ] | "
1187c478bd9Sstevel@tonic-gate 	    "command [ args ] ]\n\n"
1197c478bd9Sstevel@tonic-gate 	    "Trap selection options:\n\n"
1207c478bd9Sstevel@tonic-gate 	    " -t             TLB statistics\n"
1217c478bd9Sstevel@tonic-gate 	    " -T             TLB statistics, with pagesize information\n"
1227c478bd9Sstevel@tonic-gate 	    " -e entrylist   Enable statistics only for entries specified "
1237c478bd9Sstevel@tonic-gate 	    "by entrylist\n\n"
1247c478bd9Sstevel@tonic-gate 	    "CPU selection options:\n\n"
1257c478bd9Sstevel@tonic-gate 	    " -c cpulist     Enable statistics only for specified CPU list\n"
1267c478bd9Sstevel@tonic-gate 	    " -C psrset      Enable statistics only for specified processor "
1277c478bd9Sstevel@tonic-gate 	    "set\n\n"
1287c478bd9Sstevel@tonic-gate 	    "Other options:\n\n"
1297c478bd9Sstevel@tonic-gate 	    " -a             Display trap values as accumulating values "
1307c478bd9Sstevel@tonic-gate 	    "instead of rates\n"
1317c478bd9Sstevel@tonic-gate 	    " -l             List trap table entries and exit\n"
1327c478bd9Sstevel@tonic-gate 	    " -P             Display output in parsable format\n"
1337c478bd9Sstevel@tonic-gate 	    " -r hz          Set sampling rate to be hz samples "
1347c478bd9Sstevel@tonic-gate 	    "per second\n\n");
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate static void
1407c478bd9Sstevel@tonic-gate fatal(char *fmt, ...)
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate 	va_list ap;
1437c478bd9Sstevel@tonic-gate 	int error = errno;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, TSTAT_COMMAND ": ");
1487c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	if (fmt[strlen(fmt) - 1] != '\n')
1517c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s\n", strerror(error));
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate static void
1577c478bd9Sstevel@tonic-gate set_width(void)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	struct winsize win;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	if (!isatty(fileno(stdout)))
1627c478bd9Sstevel@tonic-gate 		return;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(stdout), TIOCGWINSZ, &win) == -1)
1657c478bd9Sstevel@tonic-gate 		return;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (win.ws_col == 0) {
1687c478bd9Sstevel@tonic-gate 		/*
1697c478bd9Sstevel@tonic-gate 		 * If TIOCGWINSZ returned 0 for the columns, just return --
1707c478bd9Sstevel@tonic-gate 		 * thereby using the default value of g_cpus_per_line.  (This
1717c478bd9Sstevel@tonic-gate 		 * happens, e.g., when running over a tip line.)
1727c478bd9Sstevel@tonic-gate 		 */
1737c478bd9Sstevel@tonic-gate 		return;
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	g_cpus_per_line = (win.ws_col - TSTAT_COLUMN_OFFS) /
1777c478bd9Sstevel@tonic-gate 	    TSTAT_COLUMNS_PER_CPU;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if (g_cpus_per_line < 1)
1807c478bd9Sstevel@tonic-gate 		g_cpus_per_line = 1;
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate static void
1847c478bd9Sstevel@tonic-gate intr(int signo)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	int error = errno;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	switch (signo) {
1897c478bd9Sstevel@tonic-gate 	case SIGWINCH:
1907c478bd9Sstevel@tonic-gate 		g_winch = 1;
1917c478bd9Sstevel@tonic-gate 		set_width();
1927c478bd9Sstevel@tonic-gate 		break;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	case SIGCHLD:
1957c478bd9Sstevel@tonic-gate 		g_child_exited = 1;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		while (wait(&g_child_status) == -1 && errno == EINTR)
1987c478bd9Sstevel@tonic-gate 			continue;
1997c478bd9Sstevel@tonic-gate 		break;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	default:
2027c478bd9Sstevel@tonic-gate 		break;
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	errno = error;
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate static void
2097c478bd9Sstevel@tonic-gate setup(void)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	struct sigaction act;
2127c478bd9Sstevel@tonic-gate 	struct sigevent ev;
2137c478bd9Sstevel@tonic-gate 	sigset_t set;
2147c478bd9Sstevel@tonic-gate 	int i;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	for (i = 0; i < TSTAT_NENT; i++) {
217*1e49577aSRod Evans 		tstat_ent_t	*gtp;
2187c478bd9Sstevel@tonic-gate 
219*1e49577aSRod Evans 		if ((gtp = get_trap_ent(i)) == NULL)
220*1e49577aSRod Evans 			continue;
221*1e49577aSRod Evans 
222*1e49577aSRod Evans 		if (gtp->tent_type == TSTAT_ENT_RESERVED)
223*1e49577aSRod Evans 			gtp->tent_name = "reserved";
224*1e49577aSRod Evans 
225*1e49577aSRod Evans 		if (gtp->tent_type == TSTAT_ENT_UNUSED)
226*1e49577aSRod Evans 			gtp->tent_name = "unused";
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	g_max_cpus = (processorid_t)sysconf(_SC_CPUID_MAX) + 1;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	if ((g_selected = malloc(sizeof (int8_t) * g_max_cpus)) == NULL)
2327c478bd9Sstevel@tonic-gate 		fatal("could not allocate g_selected");
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	bzero(g_selected, sizeof (int8_t) * g_max_cpus);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	g_pset_cpus = malloc(sizeof (processorid_t) * g_max_cpus);
2377c478bd9Sstevel@tonic-gate 	if (g_pset_cpus == NULL)
2387c478bd9Sstevel@tonic-gate 		fatal("could not allocate g_pset_cpus");
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	bzero(g_pset_cpus, sizeof (processorid_t) * g_max_cpus);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	if ((g_pgsizes = getpagesizes(NULL, 0)) == -1)
2437c478bd9Sstevel@tonic-gate 		fatal("getpagesizes()");
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if ((g_pgsize = malloc(sizeof (size_t) * g_pgsizes)) == NULL)
2467c478bd9Sstevel@tonic-gate 		fatal("could not allocate g_pgsize array");
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	if (getpagesizes(g_pgsize, g_pgsizes) == -1)
2497c478bd9Sstevel@tonic-gate 		fatal("getpagesizes(%d)", g_pgsizes);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	if ((g_pgnames = malloc(sizeof (char *) * g_pgsizes)) == NULL)
2527c478bd9Sstevel@tonic-gate 		fatal("could not allocate g_pgnames");
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_pgsizes; i++) {
2557c478bd9Sstevel@tonic-gate 		size_t j, mul;
2567c478bd9Sstevel@tonic-gate 		size_t sz = g_pgsize[i];
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 		if ((g_pgnames[i] = malloc(TSTAT_PAGESIZE_STRLEN)) == NULL)
2597c478bd9Sstevel@tonic-gate 			fatal("could not allocate g_pgnames[%d]", i);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 		for (j = 0, mul = 10; (1 << mul) <= sz; j++, mul += 10)
2627c478bd9Sstevel@tonic-gate 			continue;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 		(void) snprintf(g_pgnames[i], TSTAT_PAGESIZE_STRLEN,
2657c478bd9Sstevel@tonic-gate 		    "%d%c", sz >> (mul - 10), " kmgtpe"[j]);
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	g_datasize =
2697c478bd9Sstevel@tonic-gate 	    sizeof (tstat_data_t) + (g_pgsizes - 1) * sizeof (tstat_pgszdata_t);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	if ((g_data[0] = malloc(g_datasize * g_max_cpus)) == NULL)
2727c478bd9Sstevel@tonic-gate 		fatal("could not allocate data buffer 0");
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	if ((g_data[1] = malloc(g_datasize * g_max_cpus)) == NULL)
2757c478bd9Sstevel@tonic-gate 		fatal("could not allocate data buffer 1");
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
2787c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
2797c478bd9Sstevel@tonic-gate 	act.sa_handler = intr;
2807c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR1, &act, NULL);
2817c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGCHLD, &act, NULL);
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	(void) sigaddset(&act.sa_mask, SIGCHLD);
2847c478bd9Sstevel@tonic-gate 	(void) sigaddset(&act.sa_mask, SIGUSR1);
2857c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGWINCH, &act, NULL);
2867c478bd9Sstevel@tonic-gate 	set_width();
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
2897c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGCHLD);
2907c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGUSR1);
2917c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGWINCH);
2927c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &set, &g_oset);
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	ev.sigev_notify = SIGEV_SIGNAL;
2957c478bd9Sstevel@tonic-gate 	ev.sigev_signo = SIGUSR1;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	if (timer_create(CLOCK_HIGHRES, &ev, &g_tid) == -1)
2987c478bd9Sstevel@tonic-gate 		fatal("cannot create CLOCK_HIGHRES timer");
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate static void
3027c478bd9Sstevel@tonic-gate set_interval(hrtime_t nsec)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	struct itimerspec ts;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/*
3077c478bd9Sstevel@tonic-gate 	 * If the interval is less than one second, we'll report the
3087c478bd9Sstevel@tonic-gate 	 * numbers in terms of rate-per-interval.  If the interval is
3097c478bd9Sstevel@tonic-gate 	 * greater than one second, we'll report numbers in terms of
3107c478bd9Sstevel@tonic-gate 	 * rate-per-second.
3117c478bd9Sstevel@tonic-gate 	 */
3127c478bd9Sstevel@tonic-gate 	g_interval = nsec < NANOSEC ? nsec : NANOSEC;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	ts.it_value.tv_sec = nsec / NANOSEC;
3157c478bd9Sstevel@tonic-gate 	ts.it_value.tv_nsec = nsec % NANOSEC;
3167c478bd9Sstevel@tonic-gate 	ts.it_interval.tv_sec = nsec / NANOSEC;
3177c478bd9Sstevel@tonic-gate 	ts.it_interval.tv_nsec = nsec % NANOSEC;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	if (timer_settime(g_tid, TIMER_RELTIME, &ts, NULL) == -1)
3207c478bd9Sstevel@tonic-gate 		fatal("cannot set time on CLOCK_HIGHRES timer");
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate static void
3247c478bd9Sstevel@tonic-gate print_entries(FILE *stream, int parsable)
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate 	int entno;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	if (!parsable) {
3297c478bd9Sstevel@tonic-gate 		(void) fprintf(stream, "  %3s %3s | %-20s | %s\n", "hex",
3307c478bd9Sstevel@tonic-gate 		    "dec", "entry name", "description");
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 		(void) fprintf(stream, "----------+----------------------"
3337c478bd9Sstevel@tonic-gate 		    "+-----------------------\n");
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	for (entno = 0; entno < TSTAT_NENT; entno++) {
337*1e49577aSRod Evans 		tstat_ent_t	*gtp;
338*1e49577aSRod Evans 
339*1e49577aSRod Evans 		if ((gtp = get_trap_ent(entno)) == NULL)
340*1e49577aSRod Evans 			continue;
341*1e49577aSRod Evans 
342*1e49577aSRod Evans 		if (gtp->tent_type != TSTAT_ENT_USED)
3437c478bd9Sstevel@tonic-gate 			continue;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 		(void) fprintf(stream, "0x%03x %3d %s%-20s %s%s\n",
3467c478bd9Sstevel@tonic-gate 		    entno, entno,
347*1e49577aSRod Evans 		    parsable ? "" : "| ", gtp->tent_name,
348*1e49577aSRod Evans 		    parsable ? "" : "| ", gtp->tent_descr);
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate static void
3537c478bd9Sstevel@tonic-gate select_entry(char *entry)
3547c478bd9Sstevel@tonic-gate {
3557c478bd9Sstevel@tonic-gate 	ulong_t entno;
3567c478bd9Sstevel@tonic-gate 	char *end;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	/*
3597c478bd9Sstevel@tonic-gate 	 * The entry may be specified as a number (e.g., "0x68", "104") or
3607c478bd9Sstevel@tonic-gate 	 * as a name ("dtlb-miss").
3617c478bd9Sstevel@tonic-gate 	 */
3627c478bd9Sstevel@tonic-gate 	entno = strtoul(entry, &end, 0);
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	if (*end == '\0') {
3657c478bd9Sstevel@tonic-gate 		if (entno >= TSTAT_NENT)
3667c478bd9Sstevel@tonic-gate 			goto bad_entry;
3677c478bd9Sstevel@tonic-gate 	} else {
3687c478bd9Sstevel@tonic-gate 		for (entno = 0; entno < TSTAT_NENT; entno++) {
369*1e49577aSRod Evans 			tstat_ent_t	*gtp;
370*1e49577aSRod Evans 
371*1e49577aSRod Evans 			if ((gtp = get_trap_ent(entno)) == NULL)
3727c478bd9Sstevel@tonic-gate 				continue;
3737c478bd9Sstevel@tonic-gate 
374*1e49577aSRod Evans 			if (gtp->tent_type != TSTAT_ENT_USED)
375*1e49577aSRod Evans 				continue;
376*1e49577aSRod Evans 
377*1e49577aSRod Evans 			if (strcmp(entry, gtp->tent_name) == 0)
3787c478bd9Sstevel@tonic-gate 				break;
3797c478bd9Sstevel@tonic-gate 		}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 		if (entno == TSTAT_NENT)
3827c478bd9Sstevel@tonic-gate 			goto bad_entry;
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	if (ioctl(g_fd, TSTATIOC_ENTRY, entno) == -1)
3867c478bd9Sstevel@tonic-gate 		fatal("TSTATIOC_ENTRY failed for entry 0x%x", entno);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	g_active[entno] = 1;
3897c478bd9Sstevel@tonic-gate 	return;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate bad_entry:
3927c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, TSTAT_COMMAND ": invalid entry '%s'", entry);
3937c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "; valid entries:\n\n");
3947c478bd9Sstevel@tonic-gate 	print_entries(stderr, 0);
3957c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate static void
3997c478bd9Sstevel@tonic-gate select_cpu(processorid_t cpu)
4007c478bd9Sstevel@tonic-gate {
4017c478bd9Sstevel@tonic-gate 	if (g_pset != PS_NONE)
4027c478bd9Sstevel@tonic-gate 		fatal("cannot specify both a processor set and a processor\n");
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	if (cpu < 0 || cpu >= g_max_cpus)
4057c478bd9Sstevel@tonic-gate 		fatal("cpu %d out of range\n", cpu);
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	if (p_online(cpu, P_STATUS) == -1) {
4087c478bd9Sstevel@tonic-gate 		if (errno != EINVAL)
4097c478bd9Sstevel@tonic-gate 			fatal("could not get status for cpu %d", cpu);
4107c478bd9Sstevel@tonic-gate 		fatal("cpu %d not present\n", cpu);
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	g_selected[cpu] = 1;
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate static void
4177c478bd9Sstevel@tonic-gate select_cpus(processorid_t low, processorid_t high)
4187c478bd9Sstevel@tonic-gate {
4197c478bd9Sstevel@tonic-gate 	if (g_pset != PS_NONE)
4207c478bd9Sstevel@tonic-gate 		fatal("cannot specify both a processor set and processors\n");
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	if (low < 0 || low >= g_max_cpus)
4237c478bd9Sstevel@tonic-gate 		fatal("invalid cpu '%d'\n", low);
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	if (high < 0 || high >= g_max_cpus)
4267c478bd9Sstevel@tonic-gate 		fatal("invalid cpu '%d'\n", high);
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	if (low >= high)
4297c478bd9Sstevel@tonic-gate 		fatal("invalid range '%d' to '%d'\n", low, high);
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	do {
4327c478bd9Sstevel@tonic-gate 		if (p_online(low, P_STATUS) != -1)
4337c478bd9Sstevel@tonic-gate 			g_selected[low] = 1;
4347c478bd9Sstevel@tonic-gate 	} while (++low <= high);
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate static void
4387c478bd9Sstevel@tonic-gate select_pset(psetid_t pset)
4397c478bd9Sstevel@tonic-gate {
4407c478bd9Sstevel@tonic-gate 	processorid_t i;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	if (pset < 0)
4437c478bd9Sstevel@tonic-gate 		fatal("processor set %d is out of range\n", pset);
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	/*
4467c478bd9Sstevel@tonic-gate 	 * Only one processor set can be specified.
4477c478bd9Sstevel@tonic-gate 	 */
4487c478bd9Sstevel@tonic-gate 	if (g_pset != PS_NONE)
4497c478bd9Sstevel@tonic-gate 		fatal("at most one processor set may be specified\n");
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	/*
4527c478bd9Sstevel@tonic-gate 	 * One cannot select processors _and_ a processor set.
4537c478bd9Sstevel@tonic-gate 	 */
4547c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus; i++)
4557c478bd9Sstevel@tonic-gate 		if (g_selected[i])
4567c478bd9Sstevel@tonic-gate 			break;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	if (i != g_max_cpus)
4597c478bd9Sstevel@tonic-gate 		fatal("cannot specify both a processor and a processor set\n");
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	g_pset = pset;
4627c478bd9Sstevel@tonic-gate 	g_pset_ncpus = g_max_cpus;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	if (pset_info(g_pset, NULL, &g_pset_ncpus, g_pset_cpus) == -1)
4657c478bd9Sstevel@tonic-gate 		fatal("invalid processor set: %d\n", g_pset);
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	if (g_pset_ncpus == 0)
4687c478bd9Sstevel@tonic-gate 		fatal("processor set %d empty\n", g_pset);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	if (ioctl(g_fd, TSTATIOC_NOCPU) == -1)
4717c478bd9Sstevel@tonic-gate 		fatal("TSTATIOC_NOCPU failed");
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_pset_ncpus; i++)
4747c478bd9Sstevel@tonic-gate 		g_selected[g_pset_cpus[i]] = 1;
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate static void
4787c478bd9Sstevel@tonic-gate check_pset(void)
4797c478bd9Sstevel@tonic-gate {
4807c478bd9Sstevel@tonic-gate 	uint_t ncpus = g_max_cpus;
4817c478bd9Sstevel@tonic-gate 	processorid_t i;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	if (g_pset == PS_NONE)
4847c478bd9Sstevel@tonic-gate 		return;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	if (pset_info(g_pset, NULL, &ncpus, g_pset_cpus) == -1) {
4877c478bd9Sstevel@tonic-gate 		if (errno == EINVAL)
4887c478bd9Sstevel@tonic-gate 			fatal("processor set %d destroyed\n", g_pset);
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 		fatal("couldn't get info for processor set %d", g_pset);
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	if (ncpus == 0)
4947c478bd9Sstevel@tonic-gate 		fatal("processor set %d empty\n", g_pset);
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	if (ncpus == g_pset_ncpus) {
4977c478bd9Sstevel@tonic-gate 		for (i = 0; i < g_pset_ncpus; i++) {
4987c478bd9Sstevel@tonic-gate 			if (!g_selected[g_pset_cpus[i]])
4997c478bd9Sstevel@tonic-gate 				break;
5007c478bd9Sstevel@tonic-gate 		}
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 		/*
5037c478bd9Sstevel@tonic-gate 		 * If the number of CPUs hasn't changed, and every CPU
5047c478bd9Sstevel@tonic-gate 		 * in the processor set is also selected, we know that the
5057c478bd9Sstevel@tonic-gate 		 * processor set itself hasn't changed.
5067c478bd9Sstevel@tonic-gate 		 */
5077c478bd9Sstevel@tonic-gate 		if (i == g_pset_ncpus)
5087c478bd9Sstevel@tonic-gate 			return;
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	/*
5127c478bd9Sstevel@tonic-gate 	 * If we're here, we have a new processor set.  First, we need
5137c478bd9Sstevel@tonic-gate 	 * to zero out the selection array.
5147c478bd9Sstevel@tonic-gate 	 */
5157c478bd9Sstevel@tonic-gate 	bzero(g_selected, sizeof (int8_t) * g_max_cpus);
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	g_pset_ncpus = ncpus;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	if (ioctl(g_fd, TSTATIOC_STOP) == -1)
5207c478bd9Sstevel@tonic-gate 		fatal("TSTATIOC_STOP failed");
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	if (ioctl(g_fd, TSTATIOC_NOCPU) == -1)
5237c478bd9Sstevel@tonic-gate 		fatal("TSATIOC_NOCPU failed");
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_pset_ncpus; i++) {
5267c478bd9Sstevel@tonic-gate 		g_selected[g_pset_cpus[i]] = 1;
5277c478bd9Sstevel@tonic-gate 		if (ioctl(g_fd, TSTATIOC_CPU, g_pset_cpus[i]) == -1)
5287c478bd9Sstevel@tonic-gate 			fatal("TSTATIOC_CPU failed for cpu %d", i);
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	/*
5327c478bd9Sstevel@tonic-gate 	 * Now that we have selected the CPUs, we're going to reenable
5337c478bd9Sstevel@tonic-gate 	 * trapstat, and reread the data for the current generation.
5347c478bd9Sstevel@tonic-gate 	 */
5357c478bd9Sstevel@tonic-gate 	if (ioctl(g_fd, TSTATIOC_GO) == -1)
5367c478bd9Sstevel@tonic-gate 		fatal("TSTATIOC_GO failed");
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	if (ioctl(g_fd, TSTATIOC_READ, g_data[g_gen]) == -1)
5397c478bd9Sstevel@tonic-gate 		fatal("TSTATIOC_READ failed");
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate static void
5437c478bd9Sstevel@tonic-gate missdata(tstat_missdata_t *miss, tstat_missdata_t *omiss)
5447c478bd9Sstevel@tonic-gate {
5457c478bd9Sstevel@tonic-gate 	hrtime_t ts = g_ndata->tdata_snapts - g_odata->tdata_snapts;
5467c478bd9Sstevel@tonic-gate 	hrtime_t tick = g_ndata->tdata_snaptick - g_odata->tdata_snaptick;
5477c478bd9Sstevel@tonic-gate 	uint64_t raw = miss->tmiss_count - omiss->tmiss_count;
5487c478bd9Sstevel@tonic-gate 	uint64_t diff = g_absolute ? miss->tmiss_count :
5497c478bd9Sstevel@tonic-gate 	    (uint64_t)(0.5 + g_interval /
5507c478bd9Sstevel@tonic-gate 	    (double)ts * (double)(miss->tmiss_count - omiss->tmiss_count));
5517c478bd9Sstevel@tonic-gate 	hrtime_t peffect = raw * g_ndata->tdata_peffect * g_peffect, time;
5527c478bd9Sstevel@tonic-gate 	double p;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	/*
5557c478bd9Sstevel@tonic-gate 	 * Now we need to account for the trapstat probe effect.  Take
5567c478bd9Sstevel@tonic-gate 	 * the amount of time spent in the handler, and add the
5577c478bd9Sstevel@tonic-gate 	 * amount of time known to be due to the trapstat probe effect.
5587c478bd9Sstevel@tonic-gate 	 */
5597c478bd9Sstevel@tonic-gate 	time = miss->tmiss_time - omiss->tmiss_time + peffect;
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	if (time >= tick) {
5627c478bd9Sstevel@tonic-gate 		/*
5637c478bd9Sstevel@tonic-gate 		 * This really shouldn't happen unless our calculation of
5647c478bd9Sstevel@tonic-gate 		 * the probe effect was vastly incorrect.  In any case,
5657c478bd9Sstevel@tonic-gate 		 * print 99.9 for the time instead of printing negative
5667c478bd9Sstevel@tonic-gate 		 * values...
5677c478bd9Sstevel@tonic-gate 		 */
5687c478bd9Sstevel@tonic-gate 		time = tick / 1000 * 999;
5697c478bd9Sstevel@tonic-gate 	}
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	p = (double)time / (double)tick * (double)100.0;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	(*g_process)(g_arg, diff, p);
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate static void
5777c478bd9Sstevel@tonic-gate tlbdata(tstat_tlbdata_t *tlb, tstat_tlbdata_t *otlb)
5787c478bd9Sstevel@tonic-gate {
5797c478bd9Sstevel@tonic-gate 	missdata(&tlb->ttlb_tlb, &otlb->ttlb_tlb);
5807c478bd9Sstevel@tonic-gate 	missdata(&tlb->ttlb_tsb, &otlb->ttlb_tsb);
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate static void
5847c478bd9Sstevel@tonic-gate print_missdata(double *ttl, uint64_t diff, double p)
5857c478bd9Sstevel@tonic-gate {
5867c478bd9Sstevel@tonic-gate 	TSTAT_PRINT_MISSDATA(diff, p);
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	if (ttl != NULL)
5897c478bd9Sstevel@tonic-gate 		*ttl += p;
5907c478bd9Sstevel@tonic-gate }
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate static void
5937c478bd9Sstevel@tonic-gate print_modepgsz(char *prefix, tstat_modedata_t *data, tstat_modedata_t *odata)
5947c478bd9Sstevel@tonic-gate {
5957c478bd9Sstevel@tonic-gate 	int ps;
5967c478bd9Sstevel@tonic-gate 	size_t incr = sizeof (tstat_pgszdata_t);
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	for (ps = 0; ps < g_pgsizes; ps++) {
5997c478bd9Sstevel@tonic-gate 		double ttl = 0.0;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 		g_process = (void(*)(void *, uint64_t, double))print_missdata;
6027c478bd9Sstevel@tonic-gate 		g_arg = &ttl;
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 		(void) printf("%s %4s|", prefix, g_pgnames[ps]);
6057c478bd9Sstevel@tonic-gate 		tlbdata(&data->tmode_itlb, &odata->tmode_itlb);
6067c478bd9Sstevel@tonic-gate 		(void) printf(" |");
6077c478bd9Sstevel@tonic-gate 		tlbdata(&data->tmode_dtlb, &odata->tmode_dtlb);
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 		(void) printf(" |%4.1f\n", ttl);
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 		data = (tstat_modedata_t *)((uintptr_t)data + incr);
6127c478bd9Sstevel@tonic-gate 		odata = (tstat_modedata_t *)((uintptr_t)odata + incr);
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate }
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate static void
6177c478bd9Sstevel@tonic-gate parsable_modepgsz(char *prefix, tstat_modedata_t *data, tstat_modedata_t *odata)
6187c478bd9Sstevel@tonic-gate {
6197c478bd9Sstevel@tonic-gate 	int ps;
6207c478bd9Sstevel@tonic-gate 	size_t incr = sizeof (tstat_pgszdata_t);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	g_process = (void(*)(void *, uint64_t, double))print_missdata;
6237c478bd9Sstevel@tonic-gate 	g_arg = NULL;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	for (ps = 0; ps < g_pgsizes; ps++) {
6267c478bd9Sstevel@tonic-gate 		(void) printf("%s %7d", prefix, g_pgsize[ps]);
6277c478bd9Sstevel@tonic-gate 		tlbdata(&data->tmode_itlb, &odata->tmode_itlb);
6287c478bd9Sstevel@tonic-gate 		tlbdata(&data->tmode_dtlb, &odata->tmode_dtlb);
6297c478bd9Sstevel@tonic-gate 		(void) printf("\n");
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 		data = (tstat_modedata_t *)((uintptr_t)data + incr);
6327c478bd9Sstevel@tonic-gate 		odata = (tstat_modedata_t *)((uintptr_t)odata + incr);
6337c478bd9Sstevel@tonic-gate 	}
6347c478bd9Sstevel@tonic-gate }
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate static void
6377c478bd9Sstevel@tonic-gate sum_missdata(void *sump, uint64_t diff, double p)
6387c478bd9Sstevel@tonic-gate {
6397c478bd9Sstevel@tonic-gate 	tstat_sum_t *sum = *((tstat_sum_t **)sump);
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	sum->tsum_diff += diff;
6427c478bd9Sstevel@tonic-gate 	sum->tsum_time += p;
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	(*(tstat_sum_t **)sump)++;
6457c478bd9Sstevel@tonic-gate }
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate static void
6487c478bd9Sstevel@tonic-gate sum_modedata(tstat_modedata_t *data, tstat_modedata_t *odata, tstat_sum_t *sum)
6497c478bd9Sstevel@tonic-gate {
6507c478bd9Sstevel@tonic-gate 	int ps, incr = sizeof (tstat_pgszdata_t);
6517c478bd9Sstevel@tonic-gate 	tstat_sum_t *sump;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	for (ps = 0; ps < g_pgsizes; ps++) {
6547c478bd9Sstevel@tonic-gate 		sump = sum;
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 		g_process = sum_missdata;
6577c478bd9Sstevel@tonic-gate 		g_arg = &sump;
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 		tlbdata(&data->tmode_itlb, &odata->tmode_itlb);
6607c478bd9Sstevel@tonic-gate 		tlbdata(&data->tmode_dtlb, &odata->tmode_dtlb);
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 		data = (tstat_modedata_t *)((uintptr_t)data + incr);
6637c478bd9Sstevel@tonic-gate 		odata = (tstat_modedata_t *)((uintptr_t)odata + incr);
6647c478bd9Sstevel@tonic-gate 	}
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate static void
6687c478bd9Sstevel@tonic-gate print_sum(tstat_sum_t *sum, int divisor)
6697c478bd9Sstevel@tonic-gate {
6707c478bd9Sstevel@tonic-gate 	int i;
6717c478bd9Sstevel@tonic-gate 	double ttl = 0.0;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	for (i = 0; i < 4; i++) {
6747c478bd9Sstevel@tonic-gate 		if (i == 2)
6757c478bd9Sstevel@tonic-gate 			(void) printf(" |");
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 		sum[i].tsum_time /= divisor;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 		TSTAT_PRINT_MISSDATA(sum[i].tsum_diff, sum[i].tsum_time);
6807c478bd9Sstevel@tonic-gate 		ttl += sum[i].tsum_time;
6817c478bd9Sstevel@tonic-gate 	}
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	(void) printf(" |%4.1f\n", ttl);
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate static void
6877c478bd9Sstevel@tonic-gate print_tlbpgsz(tstat_data_t *data, tstat_data_t *odata)
6887c478bd9Sstevel@tonic-gate {
6897c478bd9Sstevel@tonic-gate 	int i, cpu, ncpus = 0;
6907c478bd9Sstevel@tonic-gate 	char pre[12];
6917c478bd9Sstevel@tonic-gate 	tstat_sum_t sum[4];
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	(void) printf("cpu m size| %9s %4s %9s %4s | %9s %4s %9s %4s |%4s\n"
6947c478bd9Sstevel@tonic-gate 	    "----------+-------------------------------+-----------------------"
6957c478bd9Sstevel@tonic-gate 	    "--------+----\n", "itlb-miss", "%tim", "itsb-miss", "%tim",
6967c478bd9Sstevel@tonic-gate 	    "dtlb-miss", "%tim", "dtsb-miss", "%tim", "%tim");
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	bzero(sum, sizeof (sum));
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus; i++) {
7017c478bd9Sstevel@tonic-gate 		tstat_pgszdata_t *pgsz = data->tdata_pgsz;
7027c478bd9Sstevel@tonic-gate 		tstat_pgszdata_t *opgsz = odata->tdata_pgsz;
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 		if ((cpu = data->tdata_cpuid) == -1)
7057c478bd9Sstevel@tonic-gate 			break;
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 		if (i != 0)
7087c478bd9Sstevel@tonic-gate 			(void) printf("----------+-----------------------------"
7097c478bd9Sstevel@tonic-gate 			    "--+-------------------------------+----\n");
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 		g_ndata = data;
7127c478bd9Sstevel@tonic-gate 		g_odata = odata;
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 		(void) sprintf(pre, "%3d u", cpu);
7157c478bd9Sstevel@tonic-gate 		print_modepgsz(pre, &pgsz->tpgsz_user, &opgsz->tpgsz_user);
7167c478bd9Sstevel@tonic-gate 		sum_modedata(&pgsz->tpgsz_user, &opgsz->tpgsz_user, sum);
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 		(void) printf("- - - - - + - - - - - - - - - - - - - -"
7197c478bd9Sstevel@tonic-gate 		    " - + - - - - - - - - - - - - - - - + - -\n");
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 		(void) sprintf(pre, "%3d k", cpu);
7227c478bd9Sstevel@tonic-gate 		print_modepgsz(pre, &pgsz->tpgsz_kernel, &opgsz->tpgsz_kernel);
7237c478bd9Sstevel@tonic-gate 		sum_modedata(&pgsz->tpgsz_kernel, &opgsz->tpgsz_kernel, sum);
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 		data = (tstat_data_t *)((uintptr_t)data + g_datasize);
7267c478bd9Sstevel@tonic-gate 		odata = (tstat_data_t *)((uintptr_t)odata + g_datasize);
7277c478bd9Sstevel@tonic-gate 		ncpus++;
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	(void) printf("==========+===============================+========="
7317c478bd9Sstevel@tonic-gate 	    "======================+====\n");
7327c478bd9Sstevel@tonic-gate 	(void) printf("      ttl |");
7337c478bd9Sstevel@tonic-gate 	print_sum(sum, ncpus);
7347c478bd9Sstevel@tonic-gate 	(void) printf("\n");
7357c478bd9Sstevel@tonic-gate }
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate static void
7387c478bd9Sstevel@tonic-gate parsable_tlbpgsz(tstat_data_t *data, tstat_data_t *odata)
7397c478bd9Sstevel@tonic-gate {
7407c478bd9Sstevel@tonic-gate 	int i, cpu;
7417c478bd9Sstevel@tonic-gate 	char pre[30];
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus; i++) {
7447c478bd9Sstevel@tonic-gate 		tstat_pgszdata_t *pgsz = data->tdata_pgsz;
7457c478bd9Sstevel@tonic-gate 		tstat_pgszdata_t *opgsz = odata->tdata_pgsz;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 		if ((cpu = data->tdata_cpuid) == -1)
7487c478bd9Sstevel@tonic-gate 			break;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 		g_ndata = data;
7517c478bd9Sstevel@tonic-gate 		g_odata = odata;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 		(void) sprintf(pre, "%lld %3d u",
7547c478bd9Sstevel@tonic-gate 		    data->tdata_snapts - g_start, cpu);
7557c478bd9Sstevel@tonic-gate 		parsable_modepgsz(pre, &pgsz->tpgsz_user, &opgsz->tpgsz_user);
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 		pre[strlen(pre) - 1] = 'k';
7587c478bd9Sstevel@tonic-gate 		parsable_modepgsz(pre, &pgsz->tpgsz_kernel,
7597c478bd9Sstevel@tonic-gate 		    &opgsz->tpgsz_kernel);
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 		data = (tstat_data_t *)((uintptr_t)data + g_datasize);
7627c478bd9Sstevel@tonic-gate 		odata = (tstat_data_t *)((uintptr_t)odata + g_datasize);
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate }
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate static void
7677c478bd9Sstevel@tonic-gate print_modedata(tstat_modedata_t *data, tstat_modedata_t *odata, int parsable)
7687c478bd9Sstevel@tonic-gate {
7697c478bd9Sstevel@tonic-gate 	int ps, i;
7707c478bd9Sstevel@tonic-gate 	size_t incr = sizeof (tstat_pgszdata_t);
7717c478bd9Sstevel@tonic-gate 	tstat_sum_t sum[4], *sump = sum;
7727c478bd9Sstevel@tonic-gate 	double ttl = 0.0;
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	bzero(sum, sizeof (sum));
7757c478bd9Sstevel@tonic-gate 	g_process = sum_missdata;
7767c478bd9Sstevel@tonic-gate 	g_arg = &sump;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	for (ps = 0; ps < g_pgsizes; ps++) {
7797c478bd9Sstevel@tonic-gate 		tlbdata(&data->tmode_itlb, &odata->tmode_itlb);
7807c478bd9Sstevel@tonic-gate 		tlbdata(&data->tmode_dtlb, &odata->tmode_dtlb);
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 		data = (tstat_modedata_t *)((uintptr_t)data + incr);
7837c478bd9Sstevel@tonic-gate 		odata = (tstat_modedata_t *)((uintptr_t)odata + incr);
7847c478bd9Sstevel@tonic-gate 		sump = sum;
7857c478bd9Sstevel@tonic-gate 	}
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	for (i = 0; i < 4; i++) {
7887c478bd9Sstevel@tonic-gate 		if (i == 2 && !parsable)
7897c478bd9Sstevel@tonic-gate 			(void) printf(" |");
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 		TSTAT_PRINT_MISSDATA(sum[i].tsum_diff, sum[i].tsum_time);
7927c478bd9Sstevel@tonic-gate 		ttl += sum[i].tsum_time;
7937c478bd9Sstevel@tonic-gate 	}
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	if (parsable) {
7967c478bd9Sstevel@tonic-gate 		(void) printf("\n");
7977c478bd9Sstevel@tonic-gate 		return;
7987c478bd9Sstevel@tonic-gate 	}
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	(void) printf(" |%4.1f\n", ttl);
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate static void
8047c478bd9Sstevel@tonic-gate print_tlb(tstat_data_t *data, tstat_data_t *odata)
8057c478bd9Sstevel@tonic-gate {
8067c478bd9Sstevel@tonic-gate 	int i, cpu, ncpus = 0;
8077c478bd9Sstevel@tonic-gate 	tstat_sum_t sum[4];
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	(void) printf("cpu m| %9s %4s %9s %4s | %9s %4s %9s %4s |%4s\n"
8107c478bd9Sstevel@tonic-gate 	    "-----+-------------------------------+-----------------------"
8117c478bd9Sstevel@tonic-gate 	    "--------+----\n", "itlb-miss", "%tim", "itsb-miss", "%tim",
8127c478bd9Sstevel@tonic-gate 	    "dtlb-miss", "%tim", "dtsb-miss", "%tim", "%tim");
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	bzero(sum, sizeof (sum));
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus; i++) {
8177c478bd9Sstevel@tonic-gate 		tstat_pgszdata_t *pgsz = data->tdata_pgsz;
8187c478bd9Sstevel@tonic-gate 		tstat_pgszdata_t *opgsz = odata->tdata_pgsz;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 		if ((cpu = data->tdata_cpuid) == -1)
8217c478bd9Sstevel@tonic-gate 			break;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 		if (i != 0)
8247c478bd9Sstevel@tonic-gate 			(void) printf("-----+-------------------------------+-"
8257c478bd9Sstevel@tonic-gate 			    "------------------------------+----\n");
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 		g_ndata = data;
8287c478bd9Sstevel@tonic-gate 		g_odata = odata;
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 		(void) printf("%3d u|", cpu);
8317c478bd9Sstevel@tonic-gate 		print_modedata(&pgsz->tpgsz_user, &opgsz->tpgsz_user, 0);
8327c478bd9Sstevel@tonic-gate 		sum_modedata(&pgsz->tpgsz_user, &opgsz->tpgsz_user, sum);
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 		(void) printf("%3d k|", cpu);
8357c478bd9Sstevel@tonic-gate 		print_modedata(&pgsz->tpgsz_kernel, &opgsz->tpgsz_kernel, 0);
8367c478bd9Sstevel@tonic-gate 		sum_modedata(&pgsz->tpgsz_kernel, &opgsz->tpgsz_kernel, sum);
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 		data = (tstat_data_t *)((uintptr_t)data + g_datasize);
8397c478bd9Sstevel@tonic-gate 		odata = (tstat_data_t *)((uintptr_t)odata + g_datasize);
8407c478bd9Sstevel@tonic-gate 		ncpus++;
8417c478bd9Sstevel@tonic-gate 	}
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	(void) printf("=====+===============================+========="
8447c478bd9Sstevel@tonic-gate 	    "======================+====\n");
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	(void) printf(" ttl |");
8477c478bd9Sstevel@tonic-gate 	print_sum(sum, ncpus);
8487c478bd9Sstevel@tonic-gate 	(void) printf("\n");
8497c478bd9Sstevel@tonic-gate }
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate static void
8527c478bd9Sstevel@tonic-gate parsable_tlb(tstat_data_t *data, tstat_data_t *odata)
8537c478bd9Sstevel@tonic-gate {
8547c478bd9Sstevel@tonic-gate 	int i, cpu;
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus; i++) {
8577c478bd9Sstevel@tonic-gate 		tstat_pgszdata_t *pgsz = data->tdata_pgsz;
8587c478bd9Sstevel@tonic-gate 		tstat_pgszdata_t *opgsz = odata->tdata_pgsz;
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 		if ((cpu = data->tdata_cpuid) == -1)
8617c478bd9Sstevel@tonic-gate 			break;
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 		g_ndata = data;
8647c478bd9Sstevel@tonic-gate 		g_odata = odata;
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 		(void) printf("%lld %3d u ", data->tdata_snapts - g_start, cpu);
8677c478bd9Sstevel@tonic-gate 		print_modedata(&pgsz->tpgsz_user, &opgsz->tpgsz_user, 1);
8687c478bd9Sstevel@tonic-gate 		(void) printf("%lld %3d k ", data->tdata_snapts - g_start, cpu);
8697c478bd9Sstevel@tonic-gate 		print_modedata(&pgsz->tpgsz_kernel, &opgsz->tpgsz_kernel, 1);
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 		data = (tstat_data_t *)((uintptr_t)data + g_datasize);
8727c478bd9Sstevel@tonic-gate 		odata = (tstat_data_t *)((uintptr_t)odata + g_datasize);
8737c478bd9Sstevel@tonic-gate 	}
8747c478bd9Sstevel@tonic-gate }
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate static void
8777c478bd9Sstevel@tonic-gate print_stats(tstat_data_t *data, tstat_data_t *odata)
8787c478bd9Sstevel@tonic-gate {
8797c478bd9Sstevel@tonic-gate 	int i, j, k, done;
8807c478bd9Sstevel@tonic-gate 	processorid_t id;
8817c478bd9Sstevel@tonic-gate 	tstat_data_t *base = data;
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	/*
8847c478bd9Sstevel@tonic-gate 	 * First, blast through all of the data updating our array
8857c478bd9Sstevel@tonic-gate 	 * of active traps.  We keep an array of active traps to prevent
8867c478bd9Sstevel@tonic-gate 	 * printing lines for traps that are never seen -- while still printing
8877c478bd9Sstevel@tonic-gate 	 * lines for traps that have been seen only once on some CPU.
8887c478bd9Sstevel@tonic-gate 	 */
8897c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus; i++) {
8907c478bd9Sstevel@tonic-gate 		if (data[i].tdata_cpuid == -1)
8917c478bd9Sstevel@tonic-gate 			break;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 		for (j = 0; j < TSTAT_NENT; j++) {
8947c478bd9Sstevel@tonic-gate 			if (!data[i].tdata_traps[j] || g_active[j])
8957c478bd9Sstevel@tonic-gate 				continue;
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 			g_active[j] = 1;
8987c478bd9Sstevel@tonic-gate 		}
8997c478bd9Sstevel@tonic-gate 	}
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	data = base;
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	for (done = 0; !done; data += g_cpus_per_line) {
9047c478bd9Sstevel@tonic-gate 		for (i = 0; i < g_cpus_per_line; i++) {
9057c478bd9Sstevel@tonic-gate 			if (&data[i] - base >= g_max_cpus)
9067c478bd9Sstevel@tonic-gate 				break;
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 			if ((id = data[i].tdata_cpuid) == -1)
9097c478bd9Sstevel@tonic-gate 				break;
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 			if (i == 0)
9127c478bd9Sstevel@tonic-gate 				(void) printf("vct name                |");
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 			(void) printf("   %scpu%d", id >= 100 ? "" :
9157c478bd9Sstevel@tonic-gate 			    id >= 10 ? " " : "  ", id);
9167c478bd9Sstevel@tonic-gate 		}
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 		if (i == 0)
9197c478bd9Sstevel@tonic-gate 			break;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 		if (i != g_cpus_per_line)
9227c478bd9Sstevel@tonic-gate 			done = 1;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 		(void) printf("\n------------------------+");
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 		for (j = 0; j < i; j++)
9277c478bd9Sstevel@tonic-gate 			(void) printf("---------");
9287c478bd9Sstevel@tonic-gate 		(void) printf("\n");
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 		for (j = 0; j < TSTAT_NENT; j++) {
931*1e49577aSRod Evans 			tstat_ent_t	*gtp;
932*1e49577aSRod Evans 
933*1e49577aSRod Evans 			if ((!g_active[j]) || ((gtp = get_trap_ent(j)) == NULL))
9347c478bd9Sstevel@tonic-gate 				continue;
9357c478bd9Sstevel@tonic-gate 
936*1e49577aSRod Evans 			(void) printf("%3x %-20s|", j, gtp->tent_name);
9377c478bd9Sstevel@tonic-gate 			for (k = 0; k < i; k++) {
9387c478bd9Sstevel@tonic-gate 				(void) printf(" %8lld", TSTAT_DELTA(&data[k],
9397c478bd9Sstevel@tonic-gate 				    &odata[data - base + k], tdata_traps[j]));
9407c478bd9Sstevel@tonic-gate 			}
9417c478bd9Sstevel@tonic-gate 			(void) printf("\n");
9427c478bd9Sstevel@tonic-gate 		}
9437c478bd9Sstevel@tonic-gate 		(void) printf("\n");
9447c478bd9Sstevel@tonic-gate 	}
9457c478bd9Sstevel@tonic-gate }
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate static void
9487c478bd9Sstevel@tonic-gate parsable_stats(tstat_data_t *data, tstat_data_t *odata)
9497c478bd9Sstevel@tonic-gate {
9507c478bd9Sstevel@tonic-gate 	tstat_data_t *base;
9517c478bd9Sstevel@tonic-gate 	int i;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	for (base = data; data - base < g_max_cpus; data++, odata++) {
9547c478bd9Sstevel@tonic-gate 		if (data->tdata_cpuid == -1)
9557c478bd9Sstevel@tonic-gate 			break;
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 		for (i = 0; i < TSTAT_NENT; i++) {
958*1e49577aSRod Evans 			tstat_ent_t	*gtp;
959*1e49577aSRod Evans 
960*1e49577aSRod Evans 			if ((!data->tdata_traps[i] && !g_active[i]) ||
961*1e49577aSRod Evans 			    ((gtp = get_trap_ent(i)) == NULL))
9627c478bd9Sstevel@tonic-gate 				continue;
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 			(void) printf("%lld %d %x %s ",
9657c478bd9Sstevel@tonic-gate 			    data->tdata_snapts - g_start, data->tdata_cpuid, i,
966*1e49577aSRod Evans 			    gtp->tent_name);
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 			(void) printf("%lld\n", TSTAT_DELTA(data, odata,
9697c478bd9Sstevel@tonic-gate 			    tdata_traps[i]));
9707c478bd9Sstevel@tonic-gate 		}
9717c478bd9Sstevel@tonic-gate 	}
9727c478bd9Sstevel@tonic-gate }
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate static void
9757c478bd9Sstevel@tonic-gate check_data(tstat_data_t *data, tstat_data_t *odata)
9767c478bd9Sstevel@tonic-gate {
9777c478bd9Sstevel@tonic-gate 	tstat_data_t *ndata;
9787c478bd9Sstevel@tonic-gate 	int i;
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	if (data->tdata_cpuid == -1) {
9817c478bd9Sstevel@tonic-gate 		/*
9827c478bd9Sstevel@tonic-gate 		 * The last CPU we were watching must have been DR'd out
9837c478bd9Sstevel@tonic-gate 		 * of the system.  Print a vaguely useful message and exit.
9847c478bd9Sstevel@tonic-gate 		 */
9857c478bd9Sstevel@tonic-gate 		fatal("all initially selected CPUs have been unconfigured\n");
9867c478bd9Sstevel@tonic-gate 	}
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	/*
9897c478bd9Sstevel@tonic-gate 	 * If a CPU is DR'd out of the system, we'll stop receiving data
9907c478bd9Sstevel@tonic-gate 	 * for it.  CPUs are never added, however (that is, if a CPU is
9917c478bd9Sstevel@tonic-gate 	 * DR'd into the system, we won't automatically start receiving
9927c478bd9Sstevel@tonic-gate 	 * data for it).  We check for this by making sure that all of
9937c478bd9Sstevel@tonic-gate 	 * the CPUs present in the old data are present in the new data.
9947c478bd9Sstevel@tonic-gate 	 * If we find one missing in the new data, we correct the old data
9957c478bd9Sstevel@tonic-gate 	 * by removing the old CPU.  This assures that delta are printed
9967c478bd9Sstevel@tonic-gate 	 * correctly.
9977c478bd9Sstevel@tonic-gate 	 */
9987c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus; i++) {
9997c478bd9Sstevel@tonic-gate 		if (odata->tdata_cpuid == -1)
10007c478bd9Sstevel@tonic-gate 			return;
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 		if (data->tdata_cpuid != odata->tdata_cpuid)
10037c478bd9Sstevel@tonic-gate 			break;
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 		data = (tstat_data_t *)((uintptr_t)data + g_datasize);
10067c478bd9Sstevel@tonic-gate 		odata = (tstat_data_t *)((uintptr_t)odata + g_datasize);
10077c478bd9Sstevel@tonic-gate 	}
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	if (i == g_max_cpus)
10107c478bd9Sstevel@tonic-gate 		return;
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	/*
10137c478bd9Sstevel@tonic-gate 	 * If we're here, we know that the odata is a CPU which has been
10147c478bd9Sstevel@tonic-gate 	 * DR'd out.  We'll now smoosh it out of the old data.
10157c478bd9Sstevel@tonic-gate 	 */
10167c478bd9Sstevel@tonic-gate 	for (odata->tdata_cpuid = -1; i < g_max_cpus - 1; i++) {
10177c478bd9Sstevel@tonic-gate 		ndata = (tstat_data_t *)((uintptr_t)odata + g_datasize);
10187c478bd9Sstevel@tonic-gate 		bcopy(ndata, odata, g_datasize);
10197c478bd9Sstevel@tonic-gate 		ndata->tdata_cpuid = -1;
10207c478bd9Sstevel@tonic-gate 	}
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	/*
10237c478bd9Sstevel@tonic-gate 	 * There may be other CPUs DR'd out; tail-call recurse.
10247c478bd9Sstevel@tonic-gate 	 */
10257c478bd9Sstevel@tonic-gate 	check_data(data, odata);
10267c478bd9Sstevel@tonic-gate }
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate int
10297c478bd9Sstevel@tonic-gate main(int argc, char **argv)
10307c478bd9Sstevel@tonic-gate {
10317c478bd9Sstevel@tonic-gate 	processorid_t id;
10327c478bd9Sstevel@tonic-gate 	char c, *end;
10337c478bd9Sstevel@tonic-gate 	ulong_t indefinite;
10347c478bd9Sstevel@tonic-gate 	long count = 0, rate = 0;
10357c478bd9Sstevel@tonic-gate 	int list = 0, parsable = 0;
10367c478bd9Sstevel@tonic-gate 	void (*print)(tstat_data_t *, tstat_data_t *);
10377c478bd9Sstevel@tonic-gate 	sigset_t set;
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	struct {
10407c478bd9Sstevel@tonic-gate 		char opt;
10417c478bd9Sstevel@tonic-gate 		void (*print)(tstat_data_t *, tstat_data_t *);
10427c478bd9Sstevel@tonic-gate 		void (*parsable)(tstat_data_t *, tstat_data_t *);
10437c478bd9Sstevel@tonic-gate 		int repeat;
10447c478bd9Sstevel@tonic-gate 	} tab[] = {
10457c478bd9Sstevel@tonic-gate 		{ '\0',	print_stats,	parsable_stats,		0 },
10467c478bd9Sstevel@tonic-gate 		{ 'e',	print_stats,	parsable_stats,		1 },
10477c478bd9Sstevel@tonic-gate 		{ 't',	print_tlb,	parsable_tlb,		0 },
10487c478bd9Sstevel@tonic-gate 		{ 'T',	print_tlbpgsz,	parsable_tlbpgsz,	0 },
10497c478bd9Sstevel@tonic-gate 		{ -1,	NULL,		NULL,			0 }
10507c478bd9Sstevel@tonic-gate 	}, *tabent = NULL, *iter;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	uintptr_t offs = (uintptr_t)&tab->print - (uintptr_t)tab;
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	/*
10557c478bd9Sstevel@tonic-gate 	 * If argv[0] is non-NULL, set argv[0] to keep any getopt(3C) output
10567c478bd9Sstevel@tonic-gate 	 * consistent with other error output.
10577c478bd9Sstevel@tonic-gate 	 */
10587c478bd9Sstevel@tonic-gate 	if (argv[0] != NULL)
10597c478bd9Sstevel@tonic-gate 		argv[0] = TSTAT_COMMAND;
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	if ((g_fd = open(TSTAT_DEVICE, O_RDWR)) == -1)
10627c478bd9Sstevel@tonic-gate 		fatal("couldn't open " TSTAT_DEVICE);
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	setup();
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "alnNtTc:C:r:e:P")) != EOF) {
10677c478bd9Sstevel@tonic-gate 		/*
10687c478bd9Sstevel@tonic-gate 		 * First, check to see if this option changes our printing
10697c478bd9Sstevel@tonic-gate 		 * function.
10707c478bd9Sstevel@tonic-gate 		 */
10717c478bd9Sstevel@tonic-gate 		for (iter = tab; iter->opt >= 0; iter++) {
10727c478bd9Sstevel@tonic-gate 			if (c != iter->opt)
10737c478bd9Sstevel@tonic-gate 				continue;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 			if (tabent != NULL) {
10767c478bd9Sstevel@tonic-gate 				if (tabent == iter) {
10777c478bd9Sstevel@tonic-gate 					if (tabent->repeat) {
10787c478bd9Sstevel@tonic-gate 						/*
10797c478bd9Sstevel@tonic-gate 						 * This option is allowed to
10807c478bd9Sstevel@tonic-gate 						 * have repeats; break out.
10817c478bd9Sstevel@tonic-gate 						 */
10827c478bd9Sstevel@tonic-gate 						break;
10837c478bd9Sstevel@tonic-gate 					}
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 					fatal("expected -%c at most once\n", c);
10867c478bd9Sstevel@tonic-gate 				}
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 				fatal("only one of -%c, -%c expected\n",
10897c478bd9Sstevel@tonic-gate 				    tabent->opt, c);
10907c478bd9Sstevel@tonic-gate 			}
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 			tabent = iter;
10937c478bd9Sstevel@tonic-gate 			break;
10947c478bd9Sstevel@tonic-gate 		}
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 		switch (c) {
10977c478bd9Sstevel@tonic-gate 		case 'a':
10987c478bd9Sstevel@tonic-gate 			g_absolute = 1;
10997c478bd9Sstevel@tonic-gate 			break;
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 		case 'e': {
11027c478bd9Sstevel@tonic-gate 			char *s = strtok(optarg, ",");
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 			while (s != NULL) {
11057c478bd9Sstevel@tonic-gate 				select_entry(s);
11067c478bd9Sstevel@tonic-gate 				s = strtok(NULL, ",");
11077c478bd9Sstevel@tonic-gate 			}
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 			break;
11107c478bd9Sstevel@tonic-gate 		}
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 		case 'l':
11137c478bd9Sstevel@tonic-gate 			list = 1;
11147c478bd9Sstevel@tonic-gate 			break;
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 		case 'n':
11177c478bd9Sstevel@tonic-gate 			/*
11187c478bd9Sstevel@tonic-gate 			 * This undocumented option prevents trapstat from
11197c478bd9Sstevel@tonic-gate 			 * actually switching the %tba to point to the
11207c478bd9Sstevel@tonic-gate 			 * interposing trap table.  It's very useful when
11217c478bd9Sstevel@tonic-gate 			 * debugging trapstat bugs:  one can specify "-n"
11227c478bd9Sstevel@tonic-gate 			 * and then examine the would-be interposing trap
11237c478bd9Sstevel@tonic-gate 			 * table without running the risk of RED stating.
11247c478bd9Sstevel@tonic-gate 			 */
11257c478bd9Sstevel@tonic-gate 			if (ioctl(g_fd, TSTATIOC_NOGO) == -1)
11267c478bd9Sstevel@tonic-gate 				fatal("TSTATIOC_NOGO");
11277c478bd9Sstevel@tonic-gate 			break;
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 		case 'N':
11307c478bd9Sstevel@tonic-gate 			/*
11317c478bd9Sstevel@tonic-gate 			 * This undocumented option forces trapstat to ignore
11327c478bd9Sstevel@tonic-gate 			 * its determined probe effect.  This may be useful
11337c478bd9Sstevel@tonic-gate 			 * if it is believed that the probe effect has been
11347c478bd9Sstevel@tonic-gate 			 * grossly overestimated.
11357c478bd9Sstevel@tonic-gate 			 */
11367c478bd9Sstevel@tonic-gate 			g_peffect = 0;
11377c478bd9Sstevel@tonic-gate 			break;
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 		case 't':
11407c478bd9Sstevel@tonic-gate 		case 'T':
11417c478bd9Sstevel@tonic-gate 			/*
11427c478bd9Sstevel@tonic-gate 			 * When running with TLB statistics, we want to
11437c478bd9Sstevel@tonic-gate 			 * minimize probe effect by running with all other
11447c478bd9Sstevel@tonic-gate 			 * entries explicitly disabled.
11457c478bd9Sstevel@tonic-gate 			 */
11467c478bd9Sstevel@tonic-gate 			if (ioctl(g_fd, TSTATIOC_NOENTRY) == -1)
11477c478bd9Sstevel@tonic-gate 				fatal("TSTATIOC_NOENTRY");
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 			if (ioctl(g_fd, TSTATIOC_TLBDATA) == -1)
11507c478bd9Sstevel@tonic-gate 				fatal("TSTATIOC_TLBDATA");
11517c478bd9Sstevel@tonic-gate 			break;
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 		case 'c': {
11547c478bd9Sstevel@tonic-gate 			/*
11557c478bd9Sstevel@tonic-gate 			 * We allow CPUs to be specified as an optionally
11567c478bd9Sstevel@tonic-gate 			 * comma separated list of either CPU IDs or ranges
11577c478bd9Sstevel@tonic-gate 			 * of CPU IDs.
11587c478bd9Sstevel@tonic-gate 			 */
11597c478bd9Sstevel@tonic-gate 			char *s = strtok(optarg, ",");
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 			while (s != NULL) {
11627c478bd9Sstevel@tonic-gate 				id = strtoul(s, &end, 0);
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 				if (id == ULONG_MAX && errno == ERANGE) {
11657c478bd9Sstevel@tonic-gate 					*end = '\0';
11667c478bd9Sstevel@tonic-gate 					fatal("invalid cpu '%s'\n", s);
11677c478bd9Sstevel@tonic-gate 				}
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 				if (*(s = end) != '\0') {
11707c478bd9Sstevel@tonic-gate 					processorid_t p;
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 					if (*s != '-')
11737c478bd9Sstevel@tonic-gate 						fatal("invalid cpu '%s'\n", s);
11747c478bd9Sstevel@tonic-gate 					p = strtoul(++s, &end, 0);
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 					if (*end != '\0' ||
11777c478bd9Sstevel@tonic-gate 					    (p == ULONG_MAX && errno == ERANGE))
11787c478bd9Sstevel@tonic-gate 						fatal("invalid cpu '%s'\n", s);
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 					select_cpus(id, p);
11817c478bd9Sstevel@tonic-gate 				} else {
11827c478bd9Sstevel@tonic-gate 					select_cpu(id);
11837c478bd9Sstevel@tonic-gate 				}
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 				s = strtok(NULL, ",");
11867c478bd9Sstevel@tonic-gate 			}
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 			break;
11897c478bd9Sstevel@tonic-gate 		}
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 		case 'C': {
11927c478bd9Sstevel@tonic-gate 			psetid_t pset = strtoul(optarg, &end, 0);
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 			if (*end != '\0' ||
11957c478bd9Sstevel@tonic-gate 			    (pset == ULONG_MAX && errno == ERANGE))
11967c478bd9Sstevel@tonic-gate 				fatal("invalid processor set '%s'\n", optarg);
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 			select_pset(pset);
11997c478bd9Sstevel@tonic-gate 			break;
12007c478bd9Sstevel@tonic-gate 		}
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 		case 'r': {
12037c478bd9Sstevel@tonic-gate 			rate = strtol(optarg, &end, 0);
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 			if (*end != '\0' ||
12067c478bd9Sstevel@tonic-gate 			    (rate == LONG_MAX && errno == ERANGE))
12077c478bd9Sstevel@tonic-gate 				fatal("invalid rate '%s'\n", optarg);
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 			if (rate <= 0)
12107c478bd9Sstevel@tonic-gate 				fatal("rate must be greater than zero\n");
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 			if (rate > TSTAT_MAX_RATE)
12137c478bd9Sstevel@tonic-gate 				fatal("rate may not exceed %d\n",
12147c478bd9Sstevel@tonic-gate 				    TSTAT_MAX_RATE);
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 			set_interval(NANOSEC / rate);
12177c478bd9Sstevel@tonic-gate 			break;
12187c478bd9Sstevel@tonic-gate 		}
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 		case 'P':
12217c478bd9Sstevel@tonic-gate 			offs = (uintptr_t)&tab->parsable - (uintptr_t)tab;
12227c478bd9Sstevel@tonic-gate 			parsable = 1;
12237c478bd9Sstevel@tonic-gate 			break;
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 		default:
12267c478bd9Sstevel@tonic-gate 			usage();
12277c478bd9Sstevel@tonic-gate 		}
12287c478bd9Sstevel@tonic-gate 	}
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 	if (list) {
12317c478bd9Sstevel@tonic-gate 		print_entries(stdout, parsable);
12327c478bd9Sstevel@tonic-gate 		exit(EXIT_SUCCESS);
12337c478bd9Sstevel@tonic-gate 	}
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	if (optind != argc) {
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 		int interval = strtol(argv[optind], &end, 0);
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 		if (*end != '\0') {
12407c478bd9Sstevel@tonic-gate 			/*
12417c478bd9Sstevel@tonic-gate 			 * That wasn't a valid number.  It must be that we're
12427c478bd9Sstevel@tonic-gate 			 * to execute this command.
12437c478bd9Sstevel@tonic-gate 			 */
12447c478bd9Sstevel@tonic-gate 			switch (vfork()) {
12457c478bd9Sstevel@tonic-gate 			case 0:
12467c478bd9Sstevel@tonic-gate 				(void) close(g_fd);
12477c478bd9Sstevel@tonic-gate 				(void) sigprocmask(SIG_SETMASK, &g_oset, NULL);
12487c478bd9Sstevel@tonic-gate 				(void) execvp(argv[optind], &argv[optind]);
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 				/*
12517c478bd9Sstevel@tonic-gate 				 * No luck.  Set errno.
12527c478bd9Sstevel@tonic-gate 				 */
12537c478bd9Sstevel@tonic-gate 				g_exec_errno = errno;
12547c478bd9Sstevel@tonic-gate 				_exit(EXIT_FAILURE);
12557c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
12567c478bd9Sstevel@tonic-gate 			case -1:
12577c478bd9Sstevel@tonic-gate 				fatal("cannot fork");
12587c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
12597c478bd9Sstevel@tonic-gate 			default:
12607c478bd9Sstevel@tonic-gate 				break;
12617c478bd9Sstevel@tonic-gate 			}
12627c478bd9Sstevel@tonic-gate 		} else {
12637c478bd9Sstevel@tonic-gate 			if (interval <= 0)
12647c478bd9Sstevel@tonic-gate 				fatal("interval must be greater than zero.\n");
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 			if (interval == LONG_MAX && errno == ERANGE)
12677c478bd9Sstevel@tonic-gate 				fatal("invalid interval '%s'\n", argv[optind]);
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 			set_interval(NANOSEC * (hrtime_t)interval);
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 			if (++optind != argc) {
12727c478bd9Sstevel@tonic-gate 				char *s = argv[optind];
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 				count = strtol(s, &end, 0);
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 				if (*end != '\0' || count <= 0 ||
12777c478bd9Sstevel@tonic-gate 				    (count == LONG_MAX && errno == ERANGE))
12787c478bd9Sstevel@tonic-gate 					fatal("invalid count '%s'\n", s);
12797c478bd9Sstevel@tonic-gate 			}
12807c478bd9Sstevel@tonic-gate 		}
12817c478bd9Sstevel@tonic-gate 	} else {
12827c478bd9Sstevel@tonic-gate 		if (!rate)
12837c478bd9Sstevel@tonic-gate 			set_interval(NANOSEC);
12847c478bd9Sstevel@tonic-gate 	}
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	if (tabent == NULL)
12877c478bd9Sstevel@tonic-gate 		tabent = tab;
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	print = *(void(**)(tstat_data_t *, tstat_data_t *))
12907c478bd9Sstevel@tonic-gate 	    ((uintptr_t)tabent + offs);
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate 	for (id = 0; id < g_max_cpus; id++) {
12937c478bd9Sstevel@tonic-gate 		if (!g_selected[id])
12947c478bd9Sstevel@tonic-gate 			continue;
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 		if (ioctl(g_fd, TSTATIOC_CPU, id) == -1)
12977c478bd9Sstevel@tonic-gate 			fatal("TSTATIOC_CPU failed for cpu %d", id);
12987c478bd9Sstevel@tonic-gate 	}
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	g_start = gethrtime();
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 	if (ioctl(g_fd, TSTATIOC_GO) == -1)
13037c478bd9Sstevel@tonic-gate 		fatal("TSTATIOC_GO failed");
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 	if (ioctl(g_fd, TSTATIOC_READ, g_data[g_gen ^ 1]) == -1)
13067c478bd9Sstevel@tonic-gate 		fatal("initial TSTATIOC_READ failed");
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 	for (indefinite = (count == 0); indefinite || count; count--) {
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 		(void) sigsuspend(&set);
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 		if (g_winch) {
13157c478bd9Sstevel@tonic-gate 			g_winch = 0;
13167c478bd9Sstevel@tonic-gate 			continue;
13177c478bd9Sstevel@tonic-gate 		}
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 		if (g_child_exited && g_exec_errno != 0) {
13207c478bd9Sstevel@tonic-gate 			errno = g_exec_errno;
13217c478bd9Sstevel@tonic-gate 			fatal("could not execute %s", argv[optind]);
13227c478bd9Sstevel@tonic-gate 		}
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 		if (ioctl(g_fd, TSTATIOC_READ, g_data[g_gen]) == -1)
13257c478bd9Sstevel@tonic-gate 			fatal("TSTATIOC_READ failed");
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 		/*
13287c478bd9Sstevel@tonic-gate 		 * Before we blithely print the data, we need to
13297c478bd9Sstevel@tonic-gate 		 * make sure that we haven't lost a CPU.
13307c478bd9Sstevel@tonic-gate 		 */
13317c478bd9Sstevel@tonic-gate 		check_data(g_data[g_gen], g_data[g_gen ^ 1]);
13327c478bd9Sstevel@tonic-gate 		(*print)(g_data[g_gen], g_data[g_gen ^ 1]);
13337c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 		if (g_child_exited) {
13367c478bd9Sstevel@tonic-gate 			if (WIFEXITED(g_child_status)) {
13377c478bd9Sstevel@tonic-gate 				if (WEXITSTATUS(g_child_status) == 0)
13387c478bd9Sstevel@tonic-gate 					break;
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, TSTAT_COMMAND ": "
13417c478bd9Sstevel@tonic-gate 				    "warning: %s exited with code %d\n",
13427c478bd9Sstevel@tonic-gate 				    argv[optind], WEXITSTATUS(g_child_status));
13437c478bd9Sstevel@tonic-gate 			} else {
13447c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, TSTAT_COMMAND ": "
13457c478bd9Sstevel@tonic-gate 				    "warning: %s died on signal %d\n",
13467c478bd9Sstevel@tonic-gate 				    argv[optind], WTERMSIG(g_child_status));
13477c478bd9Sstevel@tonic-gate 			}
13487c478bd9Sstevel@tonic-gate 			break;
13497c478bd9Sstevel@tonic-gate 		}
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 		check_pset();
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 		g_gen ^= 1;
13547c478bd9Sstevel@tonic-gate 	}
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 	return (0);
13577c478bd9Sstevel@tonic-gate }
1358