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