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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23191Sahl  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <stdarg.h>
310Sstevel@tonic-gate #include <dtrace.h>
320Sstevel@tonic-gate #include <errno.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <unistd.h>
360Sstevel@tonic-gate #include <limits.h>
370Sstevel@tonic-gate #include <strings.h>
380Sstevel@tonic-gate #include <termio.h>
390Sstevel@tonic-gate #include <signal.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #define	INTRSTAT_COLUMN_OFFS		14
420Sstevel@tonic-gate #define	INTRSTAT_COLUMNS_PER_CPU	15
430Sstevel@tonic-gate #define	INTRSTAT_CPUS_PER_LINE(w)	\
440Sstevel@tonic-gate 	(((w) - INTRSTAT_COLUMN_OFFS) / INTRSTAT_COLUMNS_PER_CPU)
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static dtrace_hdl_t *g_dtp;
470Sstevel@tonic-gate static int *g_present;
480Sstevel@tonic-gate static int g_max_cpus;
490Sstevel@tonic-gate static int g_start, g_end;
500Sstevel@tonic-gate static int g_header;
510Sstevel@tonic-gate static long g_sleeptime = 1;
520Sstevel@tonic-gate static hrtime_t g_interval = NANOSEC;
530Sstevel@tonic-gate static int g_intr;
540Sstevel@tonic-gate static psetid_t g_pset = PS_NONE;
550Sstevel@tonic-gate static processorid_t *g_pset_cpus;
560Sstevel@tonic-gate static uint_t g_pset_ncpus;
570Sstevel@tonic-gate static int g_cpus_per_line = INTRSTAT_CPUS_PER_LINE(80);
580Sstevel@tonic-gate 
590Sstevel@tonic-gate static const char *g_pname = "intrstat";
600Sstevel@tonic-gate static const char *g_prog =
610Sstevel@tonic-gate "interrupt-start"
62191Sahl "/arg0 != NULL/"
630Sstevel@tonic-gate "{"
640Sstevel@tonic-gate "	self->ts = vtimestamp;"
650Sstevel@tonic-gate "}"
660Sstevel@tonic-gate ""
670Sstevel@tonic-gate "interrupt-complete"
680Sstevel@tonic-gate "/self->ts/"
690Sstevel@tonic-gate "{"
700Sstevel@tonic-gate "	this->devi = (struct dev_info *)arg0;"
710Sstevel@tonic-gate "	@counts[stringof(`devnamesp[this->devi->devi_major].dn_name),"
720Sstevel@tonic-gate "	     this->devi->devi_instance] = count();"
730Sstevel@tonic-gate "	@times[stringof(`devnamesp[this->devi->devi_major].dn_name),"
740Sstevel@tonic-gate "	     this->devi->devi_instance] = sum(vtimestamp - self->ts);"
750Sstevel@tonic-gate "	self->ts = 0;"
760Sstevel@tonic-gate "}";
770Sstevel@tonic-gate 
780Sstevel@tonic-gate static void
790Sstevel@tonic-gate usage(void)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate 	(void) fprintf(stderr,
820Sstevel@tonic-gate 	    "usage:  intrstat [ -C psrset | -c cpulist ] "
830Sstevel@tonic-gate 	    "[interval [ count]]\n");
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	exit(EXIT_FAILURE);
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
880Sstevel@tonic-gate static void
890Sstevel@tonic-gate fatal(const char *fmt, ...)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate 	va_list ap;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	va_start(ap, fmt);
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", g_pname);
960Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	if (fmt[strlen(fmt) - 1] != '\n')
990Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s\n",
1000Sstevel@tonic-gate 		    dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	exit(EXIT_FAILURE);
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*ARGSUSED*/
1060Sstevel@tonic-gate static void
1070Sstevel@tonic-gate intr(int signo)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate 	g_intr++;
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate static void
1130Sstevel@tonic-gate status(void)
1140Sstevel@tonic-gate {}
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate static void
1170Sstevel@tonic-gate set_width(void)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate 	struct winsize win;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	if (!isatty(fileno(stdout)))
1220Sstevel@tonic-gate 		return;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	if (ioctl(fileno(stdout), TIOCGWINSZ, &win) == -1)
1250Sstevel@tonic-gate 		return;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	if (win.ws_col == 0) {
1280Sstevel@tonic-gate 		/*
1290Sstevel@tonic-gate 		 * If TIOCGWINSZ returned 0 for the columns, just return --
1300Sstevel@tonic-gate 		 * thereby using the default value of g_cpus_per_line.  (This
1310Sstevel@tonic-gate 		 * happens, e.g., when running over a tip line.)
1320Sstevel@tonic-gate 		 */
1330Sstevel@tonic-gate 		return;
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	g_cpus_per_line = INTRSTAT_CPUS_PER_LINE(win.ws_col);
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	if (g_cpus_per_line < 1)
1390Sstevel@tonic-gate 		g_cpus_per_line = 1;
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate static void
1430Sstevel@tonic-gate print_header()
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate 	int i, j;
1460Sstevel@tonic-gate 	char c[256];
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	if (!g_header)
1490Sstevel@tonic-gate 		return;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	(void) printf("\n%12s |", "device");
1520Sstevel@tonic-gate 	for (i = g_start, j = 0; i < g_max_cpus; i++) {
1530Sstevel@tonic-gate 		if (!g_present[i])
1540Sstevel@tonic-gate 			continue;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 		(void) sprintf(c, "cpu%d", i);
1570Sstevel@tonic-gate 		(void) printf(" %9s %%tim", c);
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 		if (++j >= g_cpus_per_line)
1600Sstevel@tonic-gate 			break;
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	(void) printf("\n-------------+");
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	while (j--)
1660Sstevel@tonic-gate 		(void) printf("---------------");
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	(void) printf("\n");
1690Sstevel@tonic-gate 	g_header = 0;
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate /*ARGSUSED*/
1730Sstevel@tonic-gate static int
174*457Sbmc walk(const dtrace_aggdata_t *data, void *arg)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	dtrace_aggdesc_t *aggdesc = data->dtada_desc;
1770Sstevel@tonic-gate 	dtrace_recdesc_t *nrec, *irec;
1780Sstevel@tonic-gate 	char *name, c[256];
1790Sstevel@tonic-gate 	int32_t *instance;
180*457Sbmc 	static const dtrace_aggdata_t *count;
1810Sstevel@tonic-gate 	int i, j;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	if (count == NULL) {
1840Sstevel@tonic-gate 		count = data;
1850Sstevel@tonic-gate 		return (DTRACE_AGGWALK_NEXT);
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	nrec = &aggdesc->dtagd_rec[1];
1890Sstevel@tonic-gate 	irec = &aggdesc->dtagd_rec[2];
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	name = data->dtada_data + nrec->dtrd_offset;
1920Sstevel@tonic-gate 	/* LINTED - alignment */
1930Sstevel@tonic-gate 	instance = (int32_t *)(data->dtada_data + irec->dtrd_offset);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	for (i = g_start, j = 0; i < g_max_cpus && j < g_cpus_per_line; i++) {
1960Sstevel@tonic-gate 		/* LINTED - alignment */
1970Sstevel@tonic-gate 		uint64_t time = *((uint64_t *)(data->dtada_percpu[i]));
1980Sstevel@tonic-gate 		/* LINTED - alignment */
1990Sstevel@tonic-gate 		uint64_t n = *((uint64_t *)(count->dtada_percpu[i]));
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 		if (!g_present[i])
2020Sstevel@tonic-gate 			continue;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 		if (j++ == 0) {
2050Sstevel@tonic-gate 			print_header();
2060Sstevel@tonic-gate 			(void) snprintf(c, sizeof (c), "%s#%d",
2070Sstevel@tonic-gate 				name, *instance);
2080Sstevel@tonic-gate 			(void) printf("%12s |", c);
2090Sstevel@tonic-gate 		}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 		(void) printf(" %9lld %4.1f",
2120Sstevel@tonic-gate 		    (unsigned long long)((double)n /
2130Sstevel@tonic-gate 		    ((double)g_interval / (double)NANOSEC)),
2140Sstevel@tonic-gate 		    ((double)time * (double)100.0) / (double)g_interval);
2150Sstevel@tonic-gate 	}
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	(void) printf(j ? "\n" : "");
2180Sstevel@tonic-gate 	g_end = i;
2190Sstevel@tonic-gate 	count = NULL;
2200Sstevel@tonic-gate 	return (DTRACE_AGGWALK_NEXT);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate static void
2240Sstevel@tonic-gate select_cpu(processorid_t cpu)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	if (g_pset != PS_NONE)
2270Sstevel@tonic-gate 		fatal("cannot specify both a processor set and a processor\n");
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	if (cpu < 0 || cpu >= g_max_cpus)
2300Sstevel@tonic-gate 		fatal("cpu %d out of range\n", cpu);
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	if (p_online(cpu, P_STATUS) == -1) {
2330Sstevel@tonic-gate 		if (errno != EINVAL)
2340Sstevel@tonic-gate 			fatal("could not get status for cpu %d", cpu);
2350Sstevel@tonic-gate 		fatal("cpu %d not present\n", cpu);
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	g_present[cpu] = 1;
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate static void
2420Sstevel@tonic-gate select_cpus(processorid_t low, processorid_t high)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate 	if (g_pset != PS_NONE)
2450Sstevel@tonic-gate 		fatal("cannot specify both a processor set and processors\n");
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	if (low < 0 || low >= g_max_cpus)
2480Sstevel@tonic-gate 		fatal("invalid cpu '%d'\n", low);
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	if (high < 0 || high >= g_max_cpus)
2510Sstevel@tonic-gate 		fatal("invalid cpu '%d'\n", high);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	if (low >= high)
2540Sstevel@tonic-gate 		fatal("invalid range '%d' to '%d'\n", low, high);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	do {
2570Sstevel@tonic-gate 		if (p_online(low, P_STATUS) != -1)
2580Sstevel@tonic-gate 			g_present[low] = 1;
2590Sstevel@tonic-gate 	} while (++low <= high);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate static void
2630Sstevel@tonic-gate select_pset(psetid_t pset)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate 	processorid_t i;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	if (pset < 0)
2680Sstevel@tonic-gate 		fatal("processor set %d is out of range\n", pset);
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	/*
2710Sstevel@tonic-gate 	 * Only one processor set can be specified.
2720Sstevel@tonic-gate 	 */
2730Sstevel@tonic-gate 	if (g_pset != PS_NONE)
2740Sstevel@tonic-gate 		fatal("at most one processor set may be specified\n");
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	/*
2770Sstevel@tonic-gate 	 * One cannot select processors _and_ a processor set.
2780Sstevel@tonic-gate 	 */
2790Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus; i++)
2800Sstevel@tonic-gate 		if (g_present[i])
2810Sstevel@tonic-gate 			break;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	if (i != g_max_cpus)
2840Sstevel@tonic-gate 		fatal("cannot specify both a processor and a processor set\n");
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	g_pset = pset;
2870Sstevel@tonic-gate 	g_pset_ncpus = g_max_cpus;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	if (pset_info(g_pset, NULL, &g_pset_ncpus, g_pset_cpus) == -1)
2900Sstevel@tonic-gate 		fatal("invalid processor set: %d\n", g_pset);
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	if (g_pset_ncpus == 0)
2930Sstevel@tonic-gate 		fatal("processor set %d empty\n", g_pset);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	for (i = 0; i < g_pset_ncpus; i++)
2960Sstevel@tonic-gate 		g_present[g_pset_cpus[i]] = 1;
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate static void
3000Sstevel@tonic-gate check_pset(void)
3010Sstevel@tonic-gate {
3020Sstevel@tonic-gate 	uint_t ncpus = g_max_cpus;
3030Sstevel@tonic-gate 	processorid_t i;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	if (g_pset == PS_NONE)
3060Sstevel@tonic-gate 		return;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	if (pset_info(g_pset, NULL, &ncpus, g_pset_cpus) == -1) {
3090Sstevel@tonic-gate 		if (errno == EINVAL)
3100Sstevel@tonic-gate 			fatal("processor set %d destroyed\n", g_pset);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 		fatal("couldn't get info for processor set %d", g_pset);
3130Sstevel@tonic-gate 	}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	if (ncpus == 0)
3160Sstevel@tonic-gate 		fatal("processor set %d empty\n", g_pset);
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	if (ncpus == g_pset_ncpus) {
3190Sstevel@tonic-gate 		for (i = 0; i < g_pset_ncpus; i++) {
3200Sstevel@tonic-gate 			if (!g_present[g_pset_cpus[i]])
3210Sstevel@tonic-gate 				break;
3220Sstevel@tonic-gate 		}
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 		/*
3250Sstevel@tonic-gate 		 * If the number of CPUs hasn't changed, and every CPU
3260Sstevel@tonic-gate 		 * in the processor set is also selected, we know that the
3270Sstevel@tonic-gate 		 * processor set itself hasn't changed.
3280Sstevel@tonic-gate 		 */
3290Sstevel@tonic-gate 		if (i == g_pset_ncpus)
3300Sstevel@tonic-gate 			return;
3310Sstevel@tonic-gate 	}
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	/*
3340Sstevel@tonic-gate 	 * If we're here, we have a new processor set.  First, we need
3350Sstevel@tonic-gate 	 * to zero out the present array.
3360Sstevel@tonic-gate 	 */
3370Sstevel@tonic-gate 	bzero(g_present, sizeof (processorid_t) * g_max_cpus);
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	g_pset_ncpus = ncpus;
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	for (i = 0; i < g_pset_ncpus; i++)
3420Sstevel@tonic-gate 		g_present[g_pset_cpus[i]] = 1;
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate int
3460Sstevel@tonic-gate main(int argc, char **argv)
3470Sstevel@tonic-gate {
3480Sstevel@tonic-gate 	dtrace_prog_t *prog;
3490Sstevel@tonic-gate 	dtrace_proginfo_t info;
3500Sstevel@tonic-gate 	int err, i, indefinite = 1;
3510Sstevel@tonic-gate 	long iter;
3520Sstevel@tonic-gate 	processorid_t id;
3530Sstevel@tonic-gate 	struct sigaction act;
3540Sstevel@tonic-gate 	struct itimerspec ts;
3550Sstevel@tonic-gate 	struct sigevent ev;
3560Sstevel@tonic-gate 	sigset_t set;
3570Sstevel@tonic-gate 	timer_t tid;
3580Sstevel@tonic-gate 	char *end;
3590Sstevel@tonic-gate 	char c;
3600Sstevel@tonic-gate 	hrtime_t last, now;
3610Sstevel@tonic-gate 	dtrace_optval_t statustime;
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
3640Sstevel@tonic-gate 	act.sa_flags = 0;
3650Sstevel@tonic-gate 	act.sa_handler = set_width;
3660Sstevel@tonic-gate 	(void) sigaction(SIGWINCH, &act, NULL);
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
3690Sstevel@tonic-gate 	act.sa_flags = 0;
3700Sstevel@tonic-gate 	act.sa_handler = intr;
3710Sstevel@tonic-gate 	(void) sigaction(SIGUSR1, &act, NULL);
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
3740Sstevel@tonic-gate 	act.sa_flags = 0;
3750Sstevel@tonic-gate 	act.sa_handler = status;
3760Sstevel@tonic-gate 	(void) sigaction(SIGUSR2, &act, NULL);
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	act.sa_handler = set_width;
3790Sstevel@tonic-gate 	(void) sigaction(SIGWINCH, &act, NULL);
3800Sstevel@tonic-gate 	set_width();
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	(void) sigemptyset(&set);
3830Sstevel@tonic-gate 	(void) sigaddset(&set, SIGUSR1);
3840Sstevel@tonic-gate 	(void) sigaddset(&set, SIGWINCH);
3850Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &set, NULL);
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	ev.sigev_notify = SIGEV_SIGNAL;
3880Sstevel@tonic-gate 	ev.sigev_signo = SIGUSR1;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1)
3910Sstevel@tonic-gate 		fatal("cannot create CLOCK_HIGHRES timer");
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	g_max_cpus = sysconf(_SC_CPUID_MAX) + 1;
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	if ((g_present = malloc(sizeof (processorid_t) * g_max_cpus)) == NULL)
3960Sstevel@tonic-gate 		fatal("could not allocate g_present array\n");
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	bzero(g_present, sizeof (processorid_t) * g_max_cpus);
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	g_pset_cpus = malloc(sizeof (processorid_t) * g_max_cpus);
4010Sstevel@tonic-gate 	if (g_pset_cpus == NULL)
4020Sstevel@tonic-gate 		fatal("could not allocate g_pset_cpus");
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	bzero(g_pset_cpus, sizeof (processorid_t) * g_max_cpus);
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "c:C:")) != EOF) {
4070Sstevel@tonic-gate 		switch (c) {
4080Sstevel@tonic-gate 		case 'c': {
4090Sstevel@tonic-gate 			/*
4100Sstevel@tonic-gate 			 * We allow CPUs to be specified as an optionally
4110Sstevel@tonic-gate 			 * comma separated list of either CPU IDs or ranges
4120Sstevel@tonic-gate 			 * of CPU IDs.
4130Sstevel@tonic-gate 			 */
4140Sstevel@tonic-gate 			char *s = strtok(optarg, ",");
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 			while (s != NULL) {
4170Sstevel@tonic-gate 				id = strtoul(s, &end, 0);
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 				if (id == ULONG_MAX && errno == ERANGE) {
4200Sstevel@tonic-gate 					*end = '\0';
4210Sstevel@tonic-gate 					fatal("invalid cpu '%s'\n", s);
4220Sstevel@tonic-gate 				}
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 				if (*(s = end) != '\0') {
4250Sstevel@tonic-gate 					processorid_t p;
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 					if (*s != '-')
4280Sstevel@tonic-gate 						fatal("invalid cpu '%s'\n", s);
4290Sstevel@tonic-gate 					p = strtoul(++s, &end, 0);
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 					if (*end != '\0' ||
4320Sstevel@tonic-gate 					    (p == ULONG_MAX && errno == ERANGE))
4330Sstevel@tonic-gate 						fatal("invalid cpu '%s'\n", s);
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 					select_cpus(id, p);
4360Sstevel@tonic-gate 				} else {
4370Sstevel@tonic-gate 					select_cpu(id);
4380Sstevel@tonic-gate 				}
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 				s = strtok(NULL, ",");
4410Sstevel@tonic-gate 			}
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 			break;
4440Sstevel@tonic-gate 		}
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 		case 'C': {
4470Sstevel@tonic-gate 			psetid_t pset = strtoul(optarg, &end, 0);
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 			if (*end != '\0' ||
4500Sstevel@tonic-gate 			    (pset == ULONG_MAX && errno == ERANGE))
4510Sstevel@tonic-gate 				fatal("invalid processor set '%s'\n", optarg);
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 			select_pset(pset);
4540Sstevel@tonic-gate 			break;
4550Sstevel@tonic-gate 		}
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 		default:
4580Sstevel@tonic-gate 			usage();
4590Sstevel@tonic-gate 		}
4600Sstevel@tonic-gate 	}
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	if (optind != argc) {
4630Sstevel@tonic-gate 		g_sleeptime = strtol(argv[optind], &end, 10);
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 		if (*end != NULL || g_sleeptime == 0)
4660Sstevel@tonic-gate 			fatal("invalid interval '%s'\n", argv[1]);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 		if (g_sleeptime <= 0)
4690Sstevel@tonic-gate 			fatal("interval must be greater than zero.\n");
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 		if (g_sleeptime == LONG_MAX && errno == ERANGE)
4720Sstevel@tonic-gate 			fatal("invalid interval '%s'\n", argv[optind]);
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 		if (++optind != argc) {
4750Sstevel@tonic-gate 			char *s = argv[optind];
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 			iter = strtol(s, &end, 0);
4780Sstevel@tonic-gate 			indefinite = 0;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 			if (*end != '\0' || iter <= 0 ||
4810Sstevel@tonic-gate 			    (iter == LONG_MAX && errno == ERANGE))
4820Sstevel@tonic-gate 				fatal("invalid count '%s'\n", s);
4830Sstevel@tonic-gate 		}
4840Sstevel@tonic-gate 	}
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	ts.it_value.tv_sec = g_sleeptime;
4870Sstevel@tonic-gate 	ts.it_value.tv_nsec = 0;
4880Sstevel@tonic-gate 	ts.it_interval.tv_sec = g_sleeptime;
4890Sstevel@tonic-gate 	ts.it_interval.tv_nsec = 0;
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1)
4920Sstevel@tonic-gate 		fatal("cannot set time on CLOCK_REALTIME timer");
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus && !g_present[i]; i++)
4950Sstevel@tonic-gate 		continue;
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	if (i == g_max_cpus) {
4980Sstevel@tonic-gate 		for (i = 0; i < g_max_cpus; i++)
4990Sstevel@tonic-gate 			g_present[i] = p_online(i, P_STATUS) == -1 ? 0 : 1;
5000Sstevel@tonic-gate 	}
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	if ((g_dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) {
5030Sstevel@tonic-gate 		fatal("cannot open dtrace library: %s\n",
5040Sstevel@tonic-gate 		    dtrace_errmsg(NULL, err));
5050Sstevel@tonic-gate 	}
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	if ((prog = dtrace_program_strcompile(g_dtp, g_prog,
5080Sstevel@tonic-gate 	    DTRACE_PROBESPEC_NAME, 0, 0, NULL)) == NULL)
5090Sstevel@tonic-gate 		fatal("invalid program");
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	if (dtrace_program_exec(g_dtp, prog, &info) == -1)
5120Sstevel@tonic-gate 		fatal("failed to enable probes");
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	if (dtrace_setopt(g_dtp, "aggsize", "128k") == -1)
5150Sstevel@tonic-gate 		fatal("failed to set 'aggsize'");
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	if (dtrace_setopt(g_dtp, "aggrate", "0") == -1)
5180Sstevel@tonic-gate 		fatal("failed to set 'aggrate'");
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	if (dtrace_setopt(g_dtp, "aggpercpu", 0) == -1)
5210Sstevel@tonic-gate 		fatal("failed to set 'aggpercpu'");
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	if (dtrace_go(g_dtp) != 0)
5240Sstevel@tonic-gate 		fatal("dtrace_go()");
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	last = gethrtime();
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	if (dtrace_getopt(g_dtp, "statusrate", &statustime) == -1)
5290Sstevel@tonic-gate 		fatal("failed to get 'statusrate'");
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	if (statustime < ((dtrace_optval_t)g_sleeptime * NANOSEC)) {
5320Sstevel@tonic-gate 		ev.sigev_notify = SIGEV_SIGNAL;
5330Sstevel@tonic-gate 		ev.sigev_signo = SIGUSR2;
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 		if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1)
5360Sstevel@tonic-gate 			fatal("cannot create status timer");
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 		ts.it_value.tv_sec = statustime / NANOSEC;
5390Sstevel@tonic-gate 		ts.it_value.tv_nsec = statustime % NANOSEC;
5400Sstevel@tonic-gate 		ts.it_interval = ts.it_value;
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 		if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1)
5430Sstevel@tonic-gate 			fatal("cannot set time on status timer");
5440Sstevel@tonic-gate 	}
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	(void) sigemptyset(&set);
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	while (indefinite || iter) {
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 		(void) sigsuspend(&set);
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 		(void) dtrace_status(g_dtp);
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 		if (g_intr == 0)
5550Sstevel@tonic-gate 			continue;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 		iter--;
5580Sstevel@tonic-gate 		g_intr--;
5590Sstevel@tonic-gate 		check_pset();
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 		now = gethrtime();
5620Sstevel@tonic-gate 		g_interval = now - last;
5630Sstevel@tonic-gate 		last = now;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 		if (dtrace_aggregate_snap(g_dtp) != 0)
5660Sstevel@tonic-gate 			fatal("failed to add to aggregate");
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 		g_start = g_end = 0;
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 		do {
5710Sstevel@tonic-gate 			g_header = 1;
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 			if (dtrace_aggregate_walk_keyvarsorted(g_dtp,
5740Sstevel@tonic-gate 			    walk, NULL) != 0)
5750Sstevel@tonic-gate 				fatal("failed to sort aggregate");
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 			if (g_start == g_end)
5780Sstevel@tonic-gate 				break;
5790Sstevel@tonic-gate 		} while ((g_start = g_end) < g_max_cpus);
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 		dtrace_aggregate_clear(g_dtp);
5820Sstevel@tonic-gate 	}
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	return (0);
5850Sstevel@tonic-gate }
586