xref: /titanic_50/usr/src/cmd/intrstat/intrstat.c (revision 2b6abe65936ca77751aaaa51399e77c1b105b284)
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
5*2b6abe65SJonathan Haslam  * Common Development and Distribution License (the "License").
6*2b6abe65SJonathan Haslam  * 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  */
217c478bd9Sstevel@tonic-gate /*
22*2b6abe65SJonathan Haslam  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <stdarg.h>
287c478bd9Sstevel@tonic-gate #include <dtrace.h>
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <limits.h>
347c478bd9Sstevel@tonic-gate #include <strings.h>
357c478bd9Sstevel@tonic-gate #include <termio.h>
367c478bd9Sstevel@tonic-gate #include <signal.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #define	INTRSTAT_COLUMN_OFFS		14
397c478bd9Sstevel@tonic-gate #define	INTRSTAT_COLUMNS_PER_CPU	15
407c478bd9Sstevel@tonic-gate #define	INTRSTAT_CPUS_PER_LINE(w)	\
417c478bd9Sstevel@tonic-gate 	(((w) - INTRSTAT_COLUMN_OFFS) / INTRSTAT_COLUMNS_PER_CPU)
42*2b6abe65SJonathan Haslam #define	INTRSTAT_OPTSTR			"x:c:C:"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static dtrace_hdl_t *g_dtp;
457c478bd9Sstevel@tonic-gate static int *g_present;
467c478bd9Sstevel@tonic-gate static int g_max_cpus;
477c478bd9Sstevel@tonic-gate static int g_start, g_end;
487c478bd9Sstevel@tonic-gate static int g_header;
497c478bd9Sstevel@tonic-gate static long g_sleeptime = 1;
507c478bd9Sstevel@tonic-gate static hrtime_t g_interval = NANOSEC;
517c478bd9Sstevel@tonic-gate static int g_intr;
527c478bd9Sstevel@tonic-gate static psetid_t g_pset = PS_NONE;
537c478bd9Sstevel@tonic-gate static processorid_t *g_pset_cpus;
547c478bd9Sstevel@tonic-gate static uint_t g_pset_ncpus;
557c478bd9Sstevel@tonic-gate static int g_cpus_per_line = INTRSTAT_CPUS_PER_LINE(80);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static const char *g_pname = "intrstat";
587c478bd9Sstevel@tonic-gate static const char *g_prog =
597c478bd9Sstevel@tonic-gate "interrupt-start"
600b38a8bdSahl "/arg0 != NULL/"
617c478bd9Sstevel@tonic-gate "{"
627c478bd9Sstevel@tonic-gate "	self->ts = vtimestamp;"
637c478bd9Sstevel@tonic-gate "}"
647c478bd9Sstevel@tonic-gate ""
657c478bd9Sstevel@tonic-gate "interrupt-complete"
667c478bd9Sstevel@tonic-gate "/self->ts/"
677c478bd9Sstevel@tonic-gate "{"
687c478bd9Sstevel@tonic-gate "	this->devi = (struct dev_info *)arg0;"
697c478bd9Sstevel@tonic-gate "	@counts[stringof(`devnamesp[this->devi->devi_major].dn_name),"
707c478bd9Sstevel@tonic-gate "	     this->devi->devi_instance] = count();"
717c478bd9Sstevel@tonic-gate "	@times[stringof(`devnamesp[this->devi->devi_major].dn_name),"
727c478bd9Sstevel@tonic-gate "	     this->devi->devi_instance] = sum(vtimestamp - self->ts);"
737c478bd9Sstevel@tonic-gate "	self->ts = 0;"
747c478bd9Sstevel@tonic-gate "}";
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static void
777c478bd9Sstevel@tonic-gate usage(void)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
80*2b6abe65SJonathan Haslam 	    "usage:  intrstat [ -C psrset | -c cpulist ]  [-x opt[=val]] "
817c478bd9Sstevel@tonic-gate 	    "[interval [ count]]\n");
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static void
877c478bd9Sstevel@tonic-gate fatal(const char *fmt, ...)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	va_list ap;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", g_pname);
947c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	if (fmt[strlen(fmt) - 1] != '\n')
977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s\n",
987c478bd9Sstevel@tonic-gate 		    dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1047c478bd9Sstevel@tonic-gate static void
1057c478bd9Sstevel@tonic-gate intr(int signo)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	g_intr++;
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static void
1117c478bd9Sstevel@tonic-gate status(void)
1127c478bd9Sstevel@tonic-gate {}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static void
1157c478bd9Sstevel@tonic-gate set_width(void)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	struct winsize win;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if (!isatty(fileno(stdout)))
1207c478bd9Sstevel@tonic-gate 		return;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(stdout), TIOCGWINSZ, &win) == -1)
1237c478bd9Sstevel@tonic-gate 		return;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	if (win.ws_col == 0) {
1267c478bd9Sstevel@tonic-gate 		/*
1277c478bd9Sstevel@tonic-gate 		 * If TIOCGWINSZ returned 0 for the columns, just return --
1287c478bd9Sstevel@tonic-gate 		 * thereby using the default value of g_cpus_per_line.  (This
1297c478bd9Sstevel@tonic-gate 		 * happens, e.g., when running over a tip line.)
1307c478bd9Sstevel@tonic-gate 		 */
1317c478bd9Sstevel@tonic-gate 		return;
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	g_cpus_per_line = INTRSTAT_CPUS_PER_LINE(win.ws_col);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	if (g_cpus_per_line < 1)
1377c478bd9Sstevel@tonic-gate 		g_cpus_per_line = 1;
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate static void
1417c478bd9Sstevel@tonic-gate print_header()
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate 	int i, j;
1447c478bd9Sstevel@tonic-gate 	char c[256];
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	if (!g_header)
1477c478bd9Sstevel@tonic-gate 		return;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	(void) printf("\n%12s |", "device");
1507c478bd9Sstevel@tonic-gate 	for (i = g_start, j = 0; i < g_max_cpus; i++) {
1517c478bd9Sstevel@tonic-gate 		if (!g_present[i])
1527c478bd9Sstevel@tonic-gate 			continue;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 		(void) sprintf(c, "cpu%d", i);
1557c478bd9Sstevel@tonic-gate 		(void) printf(" %9s %%tim", c);
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		if (++j >= g_cpus_per_line)
1587c478bd9Sstevel@tonic-gate 			break;
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	(void) printf("\n-------------+");
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	while (j--)
1647c478bd9Sstevel@tonic-gate 		(void) printf("---------------");
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	(void) printf("\n");
1677c478bd9Sstevel@tonic-gate 	g_header = 0;
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1717c478bd9Sstevel@tonic-gate static int
172a1b5e537Sbmc walk(const dtrace_aggdata_t *data, void *arg)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	dtrace_aggdesc_t *aggdesc = data->dtada_desc;
1757c478bd9Sstevel@tonic-gate 	dtrace_recdesc_t *nrec, *irec;
1767c478bd9Sstevel@tonic-gate 	char *name, c[256];
1777c478bd9Sstevel@tonic-gate 	int32_t *instance;
178a1b5e537Sbmc 	static const dtrace_aggdata_t *count;
1797c478bd9Sstevel@tonic-gate 	int i, j;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	if (count == NULL) {
1827c478bd9Sstevel@tonic-gate 		count = data;
1837c478bd9Sstevel@tonic-gate 		return (DTRACE_AGGWALK_NEXT);
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	nrec = &aggdesc->dtagd_rec[1];
1877c478bd9Sstevel@tonic-gate 	irec = &aggdesc->dtagd_rec[2];
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	name = data->dtada_data + nrec->dtrd_offset;
1907c478bd9Sstevel@tonic-gate 	/* LINTED - alignment */
1917c478bd9Sstevel@tonic-gate 	instance = (int32_t *)(data->dtada_data + irec->dtrd_offset);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	for (i = g_start, j = 0; i < g_max_cpus && j < g_cpus_per_line; i++) {
1947c478bd9Sstevel@tonic-gate 		/* LINTED - alignment */
1957c478bd9Sstevel@tonic-gate 		uint64_t time = *((uint64_t *)(data->dtada_percpu[i]));
1967c478bd9Sstevel@tonic-gate 		/* LINTED - alignment */
1977c478bd9Sstevel@tonic-gate 		uint64_t n = *((uint64_t *)(count->dtada_percpu[i]));
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 		if (!g_present[i])
2007c478bd9Sstevel@tonic-gate 			continue;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 		if (j++ == 0) {
2037c478bd9Sstevel@tonic-gate 			print_header();
2047c478bd9Sstevel@tonic-gate 			(void) snprintf(c, sizeof (c), "%s#%d",
2057c478bd9Sstevel@tonic-gate 			    name, *instance);
2067c478bd9Sstevel@tonic-gate 			(void) printf("%12s |", c);
2077c478bd9Sstevel@tonic-gate 		}
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 		(void) printf(" %9lld %4.1f",
2107c478bd9Sstevel@tonic-gate 		    (unsigned long long)((double)n /
2117c478bd9Sstevel@tonic-gate 		    ((double)g_interval / (double)NANOSEC)),
2127c478bd9Sstevel@tonic-gate 		    ((double)time * (double)100.0) / (double)g_interval);
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	(void) printf(j ? "\n" : "");
2167c478bd9Sstevel@tonic-gate 	g_end = i;
2177c478bd9Sstevel@tonic-gate 	count = NULL;
2187c478bd9Sstevel@tonic-gate 	return (DTRACE_AGGWALK_NEXT);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate static void
2227c478bd9Sstevel@tonic-gate select_cpu(processorid_t cpu)
2237c478bd9Sstevel@tonic-gate {
2247c478bd9Sstevel@tonic-gate 	if (g_pset != PS_NONE)
2257c478bd9Sstevel@tonic-gate 		fatal("cannot specify both a processor set and a processor\n");
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	if (cpu < 0 || cpu >= g_max_cpus)
2287c478bd9Sstevel@tonic-gate 		fatal("cpu %d out of range\n", cpu);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	if (p_online(cpu, P_STATUS) == -1) {
2317c478bd9Sstevel@tonic-gate 		if (errno != EINVAL)
2327c478bd9Sstevel@tonic-gate 			fatal("could not get status for cpu %d", cpu);
2337c478bd9Sstevel@tonic-gate 		fatal("cpu %d not present\n", cpu);
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	g_present[cpu] = 1;
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate static void
2407c478bd9Sstevel@tonic-gate select_cpus(processorid_t low, processorid_t high)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	if (g_pset != PS_NONE)
2437c478bd9Sstevel@tonic-gate 		fatal("cannot specify both a processor set and processors\n");
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if (low < 0 || low >= g_max_cpus)
2467c478bd9Sstevel@tonic-gate 		fatal("invalid cpu '%d'\n", low);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	if (high < 0 || high >= g_max_cpus)
2497c478bd9Sstevel@tonic-gate 		fatal("invalid cpu '%d'\n", high);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	if (low >= high)
2527c478bd9Sstevel@tonic-gate 		fatal("invalid range '%d' to '%d'\n", low, high);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	do {
2557c478bd9Sstevel@tonic-gate 		if (p_online(low, P_STATUS) != -1)
2567c478bd9Sstevel@tonic-gate 			g_present[low] = 1;
2577c478bd9Sstevel@tonic-gate 	} while (++low <= high);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate static void
2617c478bd9Sstevel@tonic-gate select_pset(psetid_t pset)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	processorid_t i;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	if (pset < 0)
2667c478bd9Sstevel@tonic-gate 		fatal("processor set %d is out of range\n", pset);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	/*
2697c478bd9Sstevel@tonic-gate 	 * Only one processor set can be specified.
2707c478bd9Sstevel@tonic-gate 	 */
2717c478bd9Sstevel@tonic-gate 	if (g_pset != PS_NONE)
2727c478bd9Sstevel@tonic-gate 		fatal("at most one processor set may be specified\n");
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	/*
2757c478bd9Sstevel@tonic-gate 	 * One cannot select processors _and_ a processor set.
2767c478bd9Sstevel@tonic-gate 	 */
2777c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus; i++)
2787c478bd9Sstevel@tonic-gate 		if (g_present[i])
2797c478bd9Sstevel@tonic-gate 			break;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	if (i != g_max_cpus)
2827c478bd9Sstevel@tonic-gate 		fatal("cannot specify both a processor and a processor set\n");
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	g_pset = pset;
2857c478bd9Sstevel@tonic-gate 	g_pset_ncpus = g_max_cpus;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	if (pset_info(g_pset, NULL, &g_pset_ncpus, g_pset_cpus) == -1)
2887c478bd9Sstevel@tonic-gate 		fatal("invalid processor set: %d\n", g_pset);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	if (g_pset_ncpus == 0)
2917c478bd9Sstevel@tonic-gate 		fatal("processor set %d empty\n", g_pset);
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_pset_ncpus; i++)
2947c478bd9Sstevel@tonic-gate 		g_present[g_pset_cpus[i]] = 1;
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate static void
2987c478bd9Sstevel@tonic-gate check_pset(void)
2997c478bd9Sstevel@tonic-gate {
3007c478bd9Sstevel@tonic-gate 	uint_t ncpus = g_max_cpus;
3017c478bd9Sstevel@tonic-gate 	processorid_t i;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if (g_pset == PS_NONE)
3047c478bd9Sstevel@tonic-gate 		return;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	if (pset_info(g_pset, NULL, &ncpus, g_pset_cpus) == -1) {
3077c478bd9Sstevel@tonic-gate 		if (errno == EINVAL)
3087c478bd9Sstevel@tonic-gate 			fatal("processor set %d destroyed\n", g_pset);
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 		fatal("couldn't get info for processor set %d", g_pset);
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	if (ncpus == 0)
3147c478bd9Sstevel@tonic-gate 		fatal("processor set %d empty\n", g_pset);
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	if (ncpus == g_pset_ncpus) {
3177c478bd9Sstevel@tonic-gate 		for (i = 0; i < g_pset_ncpus; i++) {
3187c478bd9Sstevel@tonic-gate 			if (!g_present[g_pset_cpus[i]])
3197c478bd9Sstevel@tonic-gate 				break;
3207c478bd9Sstevel@tonic-gate 		}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 		/*
3237c478bd9Sstevel@tonic-gate 		 * If the number of CPUs hasn't changed, and every CPU
3247c478bd9Sstevel@tonic-gate 		 * in the processor set is also selected, we know that the
3257c478bd9Sstevel@tonic-gate 		 * processor set itself hasn't changed.
3267c478bd9Sstevel@tonic-gate 		 */
3277c478bd9Sstevel@tonic-gate 		if (i == g_pset_ncpus)
3287c478bd9Sstevel@tonic-gate 			return;
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	/*
3327c478bd9Sstevel@tonic-gate 	 * If we're here, we have a new processor set.  First, we need
3337c478bd9Sstevel@tonic-gate 	 * to zero out the present array.
3347c478bd9Sstevel@tonic-gate 	 */
3357c478bd9Sstevel@tonic-gate 	bzero(g_present, sizeof (processorid_t) * g_max_cpus);
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	g_pset_ncpus = ncpus;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_pset_ncpus; i++)
3407c478bd9Sstevel@tonic-gate 		g_present[g_pset_cpus[i]] = 1;
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate int
3447c478bd9Sstevel@tonic-gate main(int argc, char **argv)
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate 	dtrace_prog_t *prog;
3477c478bd9Sstevel@tonic-gate 	dtrace_proginfo_t info;
3487c478bd9Sstevel@tonic-gate 	int err, i, indefinite = 1;
3497c478bd9Sstevel@tonic-gate 	long iter;
3507c478bd9Sstevel@tonic-gate 	processorid_t id;
3517c478bd9Sstevel@tonic-gate 	struct sigaction act;
3527c478bd9Sstevel@tonic-gate 	struct itimerspec ts;
3537c478bd9Sstevel@tonic-gate 	struct sigevent ev;
3547c478bd9Sstevel@tonic-gate 	sigset_t set;
3557c478bd9Sstevel@tonic-gate 	timer_t tid;
356*2b6abe65SJonathan Haslam 	char *end, *p;
3577c478bd9Sstevel@tonic-gate 	char c;
3587c478bd9Sstevel@tonic-gate 	hrtime_t last, now;
3597c478bd9Sstevel@tonic-gate 	dtrace_optval_t statustime;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
3627c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
3637c478bd9Sstevel@tonic-gate 	act.sa_handler = set_width;
3647c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGWINCH, &act, NULL);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
3677c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
3687c478bd9Sstevel@tonic-gate 	act.sa_handler = intr;
3697c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR1, &act, NULL);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
3727c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
3737c478bd9Sstevel@tonic-gate 	act.sa_handler = status;
3747c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR2, &act, NULL);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	act.sa_handler = set_width;
3777c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGWINCH, &act, NULL);
3787c478bd9Sstevel@tonic-gate 	set_width();
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
3817c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGUSR1);
3827c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGWINCH);
3837c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &set, NULL);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	ev.sigev_notify = SIGEV_SIGNAL;
3867c478bd9Sstevel@tonic-gate 	ev.sigev_signo = SIGUSR1;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1)
3897c478bd9Sstevel@tonic-gate 		fatal("cannot create CLOCK_HIGHRES timer");
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	g_max_cpus = sysconf(_SC_CPUID_MAX) + 1;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	if ((g_present = malloc(sizeof (processorid_t) * g_max_cpus)) == NULL)
3947c478bd9Sstevel@tonic-gate 		fatal("could not allocate g_present array\n");
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	bzero(g_present, sizeof (processorid_t) * g_max_cpus);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	g_pset_cpus = malloc(sizeof (processorid_t) * g_max_cpus);
3997c478bd9Sstevel@tonic-gate 	if (g_pset_cpus == NULL)
4007c478bd9Sstevel@tonic-gate 		fatal("could not allocate g_pset_cpus");
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	bzero(g_pset_cpus, sizeof (processorid_t) * g_max_cpus);
4037c478bd9Sstevel@tonic-gate 
404*2b6abe65SJonathan Haslam 	while ((c = getopt(argc, argv, INTRSTAT_OPTSTR)) != EOF) {
4057c478bd9Sstevel@tonic-gate 		switch (c) {
4067c478bd9Sstevel@tonic-gate 		case 'c': {
4077c478bd9Sstevel@tonic-gate 			/*
4087c478bd9Sstevel@tonic-gate 			 * We allow CPUs to be specified as an optionally
4097c478bd9Sstevel@tonic-gate 			 * comma separated list of either CPU IDs or ranges
4107c478bd9Sstevel@tonic-gate 			 * of CPU IDs.
4117c478bd9Sstevel@tonic-gate 			 */
4127c478bd9Sstevel@tonic-gate 			char *s = strtok(optarg, ",");
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 			while (s != NULL) {
4157c478bd9Sstevel@tonic-gate 				id = strtoul(s, &end, 0);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 				if (id == ULONG_MAX && errno == ERANGE) {
4187c478bd9Sstevel@tonic-gate 					*end = '\0';
4197c478bd9Sstevel@tonic-gate 					fatal("invalid cpu '%s'\n", s);
4207c478bd9Sstevel@tonic-gate 				}
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 				if (*(s = end) != '\0') {
4237c478bd9Sstevel@tonic-gate 					processorid_t p;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 					if (*s != '-')
4267c478bd9Sstevel@tonic-gate 						fatal("invalid cpu '%s'\n", s);
4277c478bd9Sstevel@tonic-gate 					p = strtoul(++s, &end, 0);
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 					if (*end != '\0' ||
4307c478bd9Sstevel@tonic-gate 					    (p == ULONG_MAX && errno == ERANGE))
4317c478bd9Sstevel@tonic-gate 						fatal("invalid cpu '%s'\n", s);
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 					select_cpus(id, p);
4347c478bd9Sstevel@tonic-gate 				} else {
4357c478bd9Sstevel@tonic-gate 					select_cpu(id);
4367c478bd9Sstevel@tonic-gate 				}
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 				s = strtok(NULL, ",");
4397c478bd9Sstevel@tonic-gate 			}
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 			break;
4427c478bd9Sstevel@tonic-gate 		}
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 		case 'C': {
4457c478bd9Sstevel@tonic-gate 			psetid_t pset = strtoul(optarg, &end, 0);
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 			if (*end != '\0' ||
4487c478bd9Sstevel@tonic-gate 			    (pset == ULONG_MAX && errno == ERANGE))
4497c478bd9Sstevel@tonic-gate 				fatal("invalid processor set '%s'\n", optarg);
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 			select_pset(pset);
4527c478bd9Sstevel@tonic-gate 			break;
4537c478bd9Sstevel@tonic-gate 		}
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 		default:
456*2b6abe65SJonathan Haslam 			if (strchr(INTRSTAT_OPTSTR, c) == NULL)
4577c478bd9Sstevel@tonic-gate 				usage();
4587c478bd9Sstevel@tonic-gate 		}
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 
461*2b6abe65SJonathan Haslam 	if ((g_dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) {
462*2b6abe65SJonathan Haslam 		fatal("cannot open dtrace library: %s\n",
463*2b6abe65SJonathan Haslam 		    dtrace_errmsg(NULL, err));
464*2b6abe65SJonathan Haslam 	}
465*2b6abe65SJonathan Haslam 
466*2b6abe65SJonathan Haslam 	if ((prog = dtrace_program_strcompile(g_dtp, g_prog,
467*2b6abe65SJonathan Haslam 	    DTRACE_PROBESPEC_NAME, 0, 0, NULL)) == NULL)
468*2b6abe65SJonathan Haslam 		fatal("invalid program");
469*2b6abe65SJonathan Haslam 
470*2b6abe65SJonathan Haslam 	if (dtrace_program_exec(g_dtp, prog, &info) == -1)
471*2b6abe65SJonathan Haslam 		fatal("failed to enable probes");
472*2b6abe65SJonathan Haslam 
473*2b6abe65SJonathan Haslam 	if (dtrace_setopt(g_dtp, "aggsize", "128k") == -1)
474*2b6abe65SJonathan Haslam 		fatal("failed to set 'aggsize'");
475*2b6abe65SJonathan Haslam 
476*2b6abe65SJonathan Haslam 	if (dtrace_setopt(g_dtp, "aggrate", "0") == -1)
477*2b6abe65SJonathan Haslam 		fatal("failed to set 'aggrate'");
478*2b6abe65SJonathan Haslam 
479*2b6abe65SJonathan Haslam 	if (dtrace_setopt(g_dtp, "aggpercpu", 0) == -1)
480*2b6abe65SJonathan Haslam 		fatal("failed to set 'aggpercpu'");
481*2b6abe65SJonathan Haslam 
482*2b6abe65SJonathan Haslam 	optind = 1;
483*2b6abe65SJonathan Haslam 	while ((c = getopt(argc, argv, INTRSTAT_OPTSTR)) != EOF) {
484*2b6abe65SJonathan Haslam 		switch (c) {
485*2b6abe65SJonathan Haslam 		case 'x':
486*2b6abe65SJonathan Haslam 			if ((p = strchr(optarg, '=')) != NULL)
487*2b6abe65SJonathan Haslam 				*p++ = '\0';
488*2b6abe65SJonathan Haslam 
489*2b6abe65SJonathan Haslam 			if (dtrace_setopt(g_dtp, optarg, p) != 0)
490*2b6abe65SJonathan Haslam 				fatal("failed to set -x %s", optarg);
491*2b6abe65SJonathan Haslam 			break;
492*2b6abe65SJonathan Haslam 		}
493*2b6abe65SJonathan Haslam 	}
494*2b6abe65SJonathan Haslam 
4957c478bd9Sstevel@tonic-gate 	if (optind != argc) {
4967c478bd9Sstevel@tonic-gate 		g_sleeptime = strtol(argv[optind], &end, 10);
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 		if (*end != NULL || g_sleeptime == 0)
4997c478bd9Sstevel@tonic-gate 			fatal("invalid interval '%s'\n", argv[1]);
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 		if (g_sleeptime <= 0)
5027c478bd9Sstevel@tonic-gate 			fatal("interval must be greater than zero.\n");
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 		if (g_sleeptime == LONG_MAX && errno == ERANGE)
5057c478bd9Sstevel@tonic-gate 			fatal("invalid interval '%s'\n", argv[optind]);
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 		if (++optind != argc) {
5087c478bd9Sstevel@tonic-gate 			char *s = argv[optind];
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 			iter = strtol(s, &end, 0);
5117c478bd9Sstevel@tonic-gate 			indefinite = 0;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 			if (*end != '\0' || iter <= 0 ||
5147c478bd9Sstevel@tonic-gate 			    (iter == LONG_MAX && errno == ERANGE))
5157c478bd9Sstevel@tonic-gate 				fatal("invalid count '%s'\n", s);
5167c478bd9Sstevel@tonic-gate 		}
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	ts.it_value.tv_sec = g_sleeptime;
5207c478bd9Sstevel@tonic-gate 	ts.it_value.tv_nsec = 0;
5217c478bd9Sstevel@tonic-gate 	ts.it_interval.tv_sec = g_sleeptime;
5227c478bd9Sstevel@tonic-gate 	ts.it_interval.tv_nsec = 0;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1)
5257c478bd9Sstevel@tonic-gate 		fatal("cannot set time on CLOCK_REALTIME timer");
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus && !g_present[i]; i++)
5287c478bd9Sstevel@tonic-gate 		continue;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	if (i == g_max_cpus) {
5317c478bd9Sstevel@tonic-gate 		for (i = 0; i < g_max_cpus; i++)
5327c478bd9Sstevel@tonic-gate 			g_present[i] = p_online(i, P_STATUS) == -1 ? 0 : 1;
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	if (dtrace_go(g_dtp) != 0)
5367c478bd9Sstevel@tonic-gate 		fatal("dtrace_go()");
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	last = gethrtime();
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	if (dtrace_getopt(g_dtp, "statusrate", &statustime) == -1)
5417c478bd9Sstevel@tonic-gate 		fatal("failed to get 'statusrate'");
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	if (statustime < ((dtrace_optval_t)g_sleeptime * NANOSEC)) {
5447c478bd9Sstevel@tonic-gate 		ev.sigev_notify = SIGEV_SIGNAL;
5457c478bd9Sstevel@tonic-gate 		ev.sigev_signo = SIGUSR2;
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 		if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1)
5487c478bd9Sstevel@tonic-gate 			fatal("cannot create status timer");
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 		ts.it_value.tv_sec = statustime / NANOSEC;
5517c478bd9Sstevel@tonic-gate 		ts.it_value.tv_nsec = statustime % NANOSEC;
5527c478bd9Sstevel@tonic-gate 		ts.it_interval = ts.it_value;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 		if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1)
5557c478bd9Sstevel@tonic-gate 			fatal("cannot set time on status timer");
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	while (indefinite || iter) {
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 		(void) sigsuspend(&set);
5637c478bd9Sstevel@tonic-gate 
564*2b6abe65SJonathan Haslam 		if (dtrace_status(g_dtp) == -1)
565*2b6abe65SJonathan Haslam 			fatal("dtrace_status()");
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 		if (g_intr == 0)
5687c478bd9Sstevel@tonic-gate 			continue;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 		iter--;
5717c478bd9Sstevel@tonic-gate 		g_intr--;
5727c478bd9Sstevel@tonic-gate 		check_pset();
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 		now = gethrtime();
5757c478bd9Sstevel@tonic-gate 		g_interval = now - last;
5767c478bd9Sstevel@tonic-gate 		last = now;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 		if (dtrace_aggregate_snap(g_dtp) != 0)
5797c478bd9Sstevel@tonic-gate 			fatal("failed to add to aggregate");
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 		g_start = g_end = 0;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 		do {
5847c478bd9Sstevel@tonic-gate 			g_header = 1;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 			if (dtrace_aggregate_walk_keyvarsorted(g_dtp,
5877c478bd9Sstevel@tonic-gate 			    walk, NULL) != 0)
5887c478bd9Sstevel@tonic-gate 				fatal("failed to sort aggregate");
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 			if (g_start == g_end)
5917c478bd9Sstevel@tonic-gate 				break;
5927c478bd9Sstevel@tonic-gate 		} while ((g_start = g_end) < g_max_cpus);
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 		dtrace_aggregate_clear(g_dtp);
5957c478bd9Sstevel@tonic-gate 	}
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	return (0);
5987c478bd9Sstevel@tonic-gate }
599