xref: /titanic_51/usr/src/cmd/intrstat/intrstat.c (revision 26fd77009b17f8c8fb32eb362584cfd635e87ad9)
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
52b6abe65SJonathan Haslam  * Common Development and Distribution License (the "License").
62b6abe65SJonathan 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 /*
222b6abe65SJonathan 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>
37*26fd7700SKrishnendu Sadhukhan - Sun Microsystems #include <locale.h>
38*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 
39*26fd7700SKrishnendu Sadhukhan - Sun Microsystems #include "statcommon.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #define	INTRSTAT_COLUMN_OFFS		14
427c478bd9Sstevel@tonic-gate #define	INTRSTAT_COLUMNS_PER_CPU	15
437c478bd9Sstevel@tonic-gate #define	INTRSTAT_CPUS_PER_LINE(w)	\
447c478bd9Sstevel@tonic-gate 	(((w) - INTRSTAT_COLUMN_OFFS) / INTRSTAT_COLUMNS_PER_CPU)
45*26fd7700SKrishnendu Sadhukhan - Sun Microsystems #define	INTRSTAT_OPTSTR			"x:c:C:T:"
46*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 
47*26fd7700SKrishnendu Sadhukhan - Sun Microsystems static uint_t timestamp_fmt = NODATE;
48*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 
49*26fd7700SKrishnendu Sadhukhan - Sun Microsystems #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
50*26fd7700SKrishnendu Sadhukhan - Sun Microsystems #define	TEXT_DOMAIN "SYS_TEST"		/* Use this only if it isn't */
51*26fd7700SKrishnendu Sadhukhan - Sun Microsystems #endif
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static dtrace_hdl_t *g_dtp;
547c478bd9Sstevel@tonic-gate static int *g_present;
557c478bd9Sstevel@tonic-gate static int g_max_cpus;
567c478bd9Sstevel@tonic-gate static int g_start, g_end;
577c478bd9Sstevel@tonic-gate static int g_header;
587c478bd9Sstevel@tonic-gate static long g_sleeptime = 1;
597c478bd9Sstevel@tonic-gate static hrtime_t g_interval = NANOSEC;
607c478bd9Sstevel@tonic-gate static int g_intr;
617c478bd9Sstevel@tonic-gate static psetid_t g_pset = PS_NONE;
627c478bd9Sstevel@tonic-gate static processorid_t *g_pset_cpus;
637c478bd9Sstevel@tonic-gate static uint_t g_pset_ncpus;
647c478bd9Sstevel@tonic-gate static int g_cpus_per_line = INTRSTAT_CPUS_PER_LINE(80);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate static const char *g_pname = "intrstat";
677c478bd9Sstevel@tonic-gate static const char *g_prog =
687c478bd9Sstevel@tonic-gate "interrupt-start"
690b38a8bdSahl "/arg0 != NULL/"
707c478bd9Sstevel@tonic-gate "{"
717c478bd9Sstevel@tonic-gate "	self->ts = vtimestamp;"
727c478bd9Sstevel@tonic-gate "}"
737c478bd9Sstevel@tonic-gate ""
747c478bd9Sstevel@tonic-gate "interrupt-complete"
757c478bd9Sstevel@tonic-gate "/self->ts/"
767c478bd9Sstevel@tonic-gate "{"
777c478bd9Sstevel@tonic-gate "	this->devi = (struct dev_info *)arg0;"
787c478bd9Sstevel@tonic-gate "	@counts[stringof(`devnamesp[this->devi->devi_major].dn_name),"
797c478bd9Sstevel@tonic-gate "	     this->devi->devi_instance] = count();"
807c478bd9Sstevel@tonic-gate "	@times[stringof(`devnamesp[this->devi->devi_major].dn_name),"
817c478bd9Sstevel@tonic-gate "	     this->devi->devi_instance] = sum(vtimestamp - self->ts);"
827c478bd9Sstevel@tonic-gate "	self->ts = 0;"
837c478bd9Sstevel@tonic-gate "}";
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate static void
867c478bd9Sstevel@tonic-gate usage(void)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
892b6abe65SJonathan Haslam 	    "usage:  intrstat [ -C psrset | -c cpulist ]  [-x opt[=val]] "
90*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	    "[-T d|u] [interval [ count]]\n");
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static void
967c478bd9Sstevel@tonic-gate fatal(const char *fmt, ...)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	va_list ap;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", g_pname);
1037c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if (fmt[strlen(fmt) - 1] != '\n')
1067c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s\n",
1077c478bd9Sstevel@tonic-gate 		    dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1137c478bd9Sstevel@tonic-gate static void
1147c478bd9Sstevel@tonic-gate intr(int signo)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	g_intr++;
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate static void
1207c478bd9Sstevel@tonic-gate status(void)
1217c478bd9Sstevel@tonic-gate {}
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static void
1247c478bd9Sstevel@tonic-gate set_width(void)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	struct winsize win;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	if (!isatty(fileno(stdout)))
1297c478bd9Sstevel@tonic-gate 		return;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(stdout), TIOCGWINSZ, &win) == -1)
1327c478bd9Sstevel@tonic-gate 		return;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	if (win.ws_col == 0) {
1357c478bd9Sstevel@tonic-gate 		/*
1367c478bd9Sstevel@tonic-gate 		 * If TIOCGWINSZ returned 0 for the columns, just return --
1377c478bd9Sstevel@tonic-gate 		 * thereby using the default value of g_cpus_per_line.  (This
1387c478bd9Sstevel@tonic-gate 		 * happens, e.g., when running over a tip line.)
1397c478bd9Sstevel@tonic-gate 		 */
1407c478bd9Sstevel@tonic-gate 		return;
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	g_cpus_per_line = INTRSTAT_CPUS_PER_LINE(win.ws_col);
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	if (g_cpus_per_line < 1)
1467c478bd9Sstevel@tonic-gate 		g_cpus_per_line = 1;
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate static void
1507c478bd9Sstevel@tonic-gate print_header()
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	int i, j;
1537c478bd9Sstevel@tonic-gate 	char c[256];
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	if (!g_header)
1567c478bd9Sstevel@tonic-gate 		return;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	(void) printf("\n%12s |", "device");
1597c478bd9Sstevel@tonic-gate 	for (i = g_start, j = 0; i < g_max_cpus; i++) {
1607c478bd9Sstevel@tonic-gate 		if (!g_present[i])
1617c478bd9Sstevel@tonic-gate 			continue;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 		(void) sprintf(c, "cpu%d", i);
1647c478bd9Sstevel@tonic-gate 		(void) printf(" %9s %%tim", c);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 		if (++j >= g_cpus_per_line)
1677c478bd9Sstevel@tonic-gate 			break;
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	(void) printf("\n-------------+");
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	while (j--)
1737c478bd9Sstevel@tonic-gate 		(void) printf("---------------");
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	(void) printf("\n");
1767c478bd9Sstevel@tonic-gate 	g_header = 0;
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1807c478bd9Sstevel@tonic-gate static int
181a1b5e537Sbmc walk(const dtrace_aggdata_t *data, void *arg)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	dtrace_aggdesc_t *aggdesc = data->dtada_desc;
1847c478bd9Sstevel@tonic-gate 	dtrace_recdesc_t *nrec, *irec;
1857c478bd9Sstevel@tonic-gate 	char *name, c[256];
1867c478bd9Sstevel@tonic-gate 	int32_t *instance;
187a1b5e537Sbmc 	static const dtrace_aggdata_t *count;
1887c478bd9Sstevel@tonic-gate 	int i, j;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if (count == NULL) {
1917c478bd9Sstevel@tonic-gate 		count = data;
1927c478bd9Sstevel@tonic-gate 		return (DTRACE_AGGWALK_NEXT);
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	nrec = &aggdesc->dtagd_rec[1];
1967c478bd9Sstevel@tonic-gate 	irec = &aggdesc->dtagd_rec[2];
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	name = data->dtada_data + nrec->dtrd_offset;
1997c478bd9Sstevel@tonic-gate 	/* LINTED - alignment */
2007c478bd9Sstevel@tonic-gate 	instance = (int32_t *)(data->dtada_data + irec->dtrd_offset);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	for (i = g_start, j = 0; i < g_max_cpus && j < g_cpus_per_line; i++) {
2037c478bd9Sstevel@tonic-gate 		/* LINTED - alignment */
2047c478bd9Sstevel@tonic-gate 		uint64_t time = *((uint64_t *)(data->dtada_percpu[i]));
2057c478bd9Sstevel@tonic-gate 		/* LINTED - alignment */
2067c478bd9Sstevel@tonic-gate 		uint64_t n = *((uint64_t *)(count->dtada_percpu[i]));
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		if (!g_present[i])
2097c478bd9Sstevel@tonic-gate 			continue;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 		if (j++ == 0) {
2127c478bd9Sstevel@tonic-gate 			print_header();
2137c478bd9Sstevel@tonic-gate 			(void) snprintf(c, sizeof (c), "%s#%d",
2147c478bd9Sstevel@tonic-gate 			    name, *instance);
2157c478bd9Sstevel@tonic-gate 			(void) printf("%12s |", c);
2167c478bd9Sstevel@tonic-gate 		}
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		(void) printf(" %9lld %4.1f",
2197c478bd9Sstevel@tonic-gate 		    (unsigned long long)((double)n /
2207c478bd9Sstevel@tonic-gate 		    ((double)g_interval / (double)NANOSEC)),
2217c478bd9Sstevel@tonic-gate 		    ((double)time * (double)100.0) / (double)g_interval);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	(void) printf(j ? "\n" : "");
2257c478bd9Sstevel@tonic-gate 	g_end = i;
2267c478bd9Sstevel@tonic-gate 	count = NULL;
2277c478bd9Sstevel@tonic-gate 	return (DTRACE_AGGWALK_NEXT);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate static void
2317c478bd9Sstevel@tonic-gate select_cpu(processorid_t cpu)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	if (g_pset != PS_NONE)
2347c478bd9Sstevel@tonic-gate 		fatal("cannot specify both a processor set and a processor\n");
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	if (cpu < 0 || cpu >= g_max_cpus)
2377c478bd9Sstevel@tonic-gate 		fatal("cpu %d out of range\n", cpu);
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	if (p_online(cpu, P_STATUS) == -1) {
2407c478bd9Sstevel@tonic-gate 		if (errno != EINVAL)
2417c478bd9Sstevel@tonic-gate 			fatal("could not get status for cpu %d", cpu);
2427c478bd9Sstevel@tonic-gate 		fatal("cpu %d not present\n", cpu);
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	g_present[cpu] = 1;
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate static void
2497c478bd9Sstevel@tonic-gate select_cpus(processorid_t low, processorid_t high)
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate 	if (g_pset != PS_NONE)
2527c478bd9Sstevel@tonic-gate 		fatal("cannot specify both a processor set and processors\n");
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	if (low < 0 || low >= g_max_cpus)
2557c478bd9Sstevel@tonic-gate 		fatal("invalid cpu '%d'\n", low);
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	if (high < 0 || high >= g_max_cpus)
2587c478bd9Sstevel@tonic-gate 		fatal("invalid cpu '%d'\n", high);
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	if (low >= high)
2617c478bd9Sstevel@tonic-gate 		fatal("invalid range '%d' to '%d'\n", low, high);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	do {
2647c478bd9Sstevel@tonic-gate 		if (p_online(low, P_STATUS) != -1)
2657c478bd9Sstevel@tonic-gate 			g_present[low] = 1;
2667c478bd9Sstevel@tonic-gate 	} while (++low <= high);
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate static void
2707c478bd9Sstevel@tonic-gate select_pset(psetid_t pset)
2717c478bd9Sstevel@tonic-gate {
2727c478bd9Sstevel@tonic-gate 	processorid_t i;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	if (pset < 0)
2757c478bd9Sstevel@tonic-gate 		fatal("processor set %d is out of range\n", pset);
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	/*
2787c478bd9Sstevel@tonic-gate 	 * Only one processor set can be specified.
2797c478bd9Sstevel@tonic-gate 	 */
2807c478bd9Sstevel@tonic-gate 	if (g_pset != PS_NONE)
2817c478bd9Sstevel@tonic-gate 		fatal("at most one processor set may be specified\n");
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	/*
2847c478bd9Sstevel@tonic-gate 	 * One cannot select processors _and_ a processor set.
2857c478bd9Sstevel@tonic-gate 	 */
2867c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus; i++)
2877c478bd9Sstevel@tonic-gate 		if (g_present[i])
2887c478bd9Sstevel@tonic-gate 			break;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	if (i != g_max_cpus)
2917c478bd9Sstevel@tonic-gate 		fatal("cannot specify both a processor and a processor set\n");
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	g_pset = pset;
2947c478bd9Sstevel@tonic-gate 	g_pset_ncpus = g_max_cpus;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	if (pset_info(g_pset, NULL, &g_pset_ncpus, g_pset_cpus) == -1)
2977c478bd9Sstevel@tonic-gate 		fatal("invalid processor set: %d\n", g_pset);
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if (g_pset_ncpus == 0)
3007c478bd9Sstevel@tonic-gate 		fatal("processor set %d empty\n", g_pset);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_pset_ncpus; i++)
3037c478bd9Sstevel@tonic-gate 		g_present[g_pset_cpus[i]] = 1;
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate static void
3077c478bd9Sstevel@tonic-gate check_pset(void)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	uint_t ncpus = g_max_cpus;
3107c478bd9Sstevel@tonic-gate 	processorid_t i;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	if (g_pset == PS_NONE)
3137c478bd9Sstevel@tonic-gate 		return;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	if (pset_info(g_pset, NULL, &ncpus, g_pset_cpus) == -1) {
3167c478bd9Sstevel@tonic-gate 		if (errno == EINVAL)
3177c478bd9Sstevel@tonic-gate 			fatal("processor set %d destroyed\n", g_pset);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 		fatal("couldn't get info for processor set %d", g_pset);
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	if (ncpus == 0)
3237c478bd9Sstevel@tonic-gate 		fatal("processor set %d empty\n", g_pset);
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	if (ncpus == g_pset_ncpus) {
3267c478bd9Sstevel@tonic-gate 		for (i = 0; i < g_pset_ncpus; i++) {
3277c478bd9Sstevel@tonic-gate 			if (!g_present[g_pset_cpus[i]])
3287c478bd9Sstevel@tonic-gate 				break;
3297c478bd9Sstevel@tonic-gate 		}
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 		/*
3327c478bd9Sstevel@tonic-gate 		 * If the number of CPUs hasn't changed, and every CPU
3337c478bd9Sstevel@tonic-gate 		 * in the processor set is also selected, we know that the
3347c478bd9Sstevel@tonic-gate 		 * processor set itself hasn't changed.
3357c478bd9Sstevel@tonic-gate 		 */
3367c478bd9Sstevel@tonic-gate 		if (i == g_pset_ncpus)
3377c478bd9Sstevel@tonic-gate 			return;
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	/*
3417c478bd9Sstevel@tonic-gate 	 * If we're here, we have a new processor set.  First, we need
3427c478bd9Sstevel@tonic-gate 	 * to zero out the present array.
3437c478bd9Sstevel@tonic-gate 	 */
3447c478bd9Sstevel@tonic-gate 	bzero(g_present, sizeof (processorid_t) * g_max_cpus);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	g_pset_ncpus = ncpus;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_pset_ncpus; i++)
3497c478bd9Sstevel@tonic-gate 		g_present[g_pset_cpus[i]] = 1;
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate int
3537c478bd9Sstevel@tonic-gate main(int argc, char **argv)
3547c478bd9Sstevel@tonic-gate {
3557c478bd9Sstevel@tonic-gate 	dtrace_prog_t *prog;
3567c478bd9Sstevel@tonic-gate 	dtrace_proginfo_t info;
3577c478bd9Sstevel@tonic-gate 	int err, i, indefinite = 1;
3587c478bd9Sstevel@tonic-gate 	long iter;
3597c478bd9Sstevel@tonic-gate 	processorid_t id;
3607c478bd9Sstevel@tonic-gate 	struct sigaction act;
3617c478bd9Sstevel@tonic-gate 	struct itimerspec ts;
3627c478bd9Sstevel@tonic-gate 	struct sigevent ev;
3637c478bd9Sstevel@tonic-gate 	sigset_t set;
3647c478bd9Sstevel@tonic-gate 	timer_t tid;
3652b6abe65SJonathan Haslam 	char *end, *p;
3667c478bd9Sstevel@tonic-gate 	char c;
3677c478bd9Sstevel@tonic-gate 	hrtime_t last, now;
3687c478bd9Sstevel@tonic-gate 	dtrace_optval_t statustime;
3697c478bd9Sstevel@tonic-gate 
370*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	(void) setlocale(LC_ALL, "");
371*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	(void) textdomain(TEXT_DOMAIN);
372*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 
3737c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
3747c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
3757c478bd9Sstevel@tonic-gate 	act.sa_handler = set_width;
3767c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGWINCH, &act, NULL);
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
3797c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
3807c478bd9Sstevel@tonic-gate 	act.sa_handler = intr;
3817c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR1, &act, NULL);
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
3847c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
3857c478bd9Sstevel@tonic-gate 	act.sa_handler = status;
3867c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR2, &act, NULL);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	act.sa_handler = set_width;
3897c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGWINCH, &act, NULL);
3907c478bd9Sstevel@tonic-gate 	set_width();
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
3937c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGUSR1);
3947c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGWINCH);
3957c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &set, NULL);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	ev.sigev_notify = SIGEV_SIGNAL;
3987c478bd9Sstevel@tonic-gate 	ev.sigev_signo = SIGUSR1;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1)
4017c478bd9Sstevel@tonic-gate 		fatal("cannot create CLOCK_HIGHRES timer");
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	g_max_cpus = sysconf(_SC_CPUID_MAX) + 1;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	if ((g_present = malloc(sizeof (processorid_t) * g_max_cpus)) == NULL)
4067c478bd9Sstevel@tonic-gate 		fatal("could not allocate g_present array\n");
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	bzero(g_present, sizeof (processorid_t) * g_max_cpus);
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	g_pset_cpus = malloc(sizeof (processorid_t) * g_max_cpus);
4117c478bd9Sstevel@tonic-gate 	if (g_pset_cpus == NULL)
4127c478bd9Sstevel@tonic-gate 		fatal("could not allocate g_pset_cpus");
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	bzero(g_pset_cpus, sizeof (processorid_t) * g_max_cpus);
4157c478bd9Sstevel@tonic-gate 
4162b6abe65SJonathan Haslam 	while ((c = getopt(argc, argv, INTRSTAT_OPTSTR)) != EOF) {
4177c478bd9Sstevel@tonic-gate 		switch (c) {
4187c478bd9Sstevel@tonic-gate 		case 'c': {
4197c478bd9Sstevel@tonic-gate 			/*
4207c478bd9Sstevel@tonic-gate 			 * We allow CPUs to be specified as an optionally
4217c478bd9Sstevel@tonic-gate 			 * comma separated list of either CPU IDs or ranges
4227c478bd9Sstevel@tonic-gate 			 * of CPU IDs.
4237c478bd9Sstevel@tonic-gate 			 */
4247c478bd9Sstevel@tonic-gate 			char *s = strtok(optarg, ",");
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 			while (s != NULL) {
4277c478bd9Sstevel@tonic-gate 				id = strtoul(s, &end, 0);
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 				if (id == ULONG_MAX && errno == ERANGE) {
4307c478bd9Sstevel@tonic-gate 					*end = '\0';
4317c478bd9Sstevel@tonic-gate 					fatal("invalid cpu '%s'\n", s);
4327c478bd9Sstevel@tonic-gate 				}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 				if (*(s = end) != '\0') {
4357c478bd9Sstevel@tonic-gate 					processorid_t p;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 					if (*s != '-')
4387c478bd9Sstevel@tonic-gate 						fatal("invalid cpu '%s'\n", s);
4397c478bd9Sstevel@tonic-gate 					p = strtoul(++s, &end, 0);
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 					if (*end != '\0' ||
4427c478bd9Sstevel@tonic-gate 					    (p == ULONG_MAX && errno == ERANGE))
4437c478bd9Sstevel@tonic-gate 						fatal("invalid cpu '%s'\n", s);
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 					select_cpus(id, p);
4467c478bd9Sstevel@tonic-gate 				} else {
4477c478bd9Sstevel@tonic-gate 					select_cpu(id);
4487c478bd9Sstevel@tonic-gate 				}
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 				s = strtok(NULL, ",");
4517c478bd9Sstevel@tonic-gate 			}
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 			break;
4547c478bd9Sstevel@tonic-gate 		}
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 		case 'C': {
4577c478bd9Sstevel@tonic-gate 			psetid_t pset = strtoul(optarg, &end, 0);
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 			if (*end != '\0' ||
4607c478bd9Sstevel@tonic-gate 			    (pset == ULONG_MAX && errno == ERANGE))
4617c478bd9Sstevel@tonic-gate 				fatal("invalid processor set '%s'\n", optarg);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 			select_pset(pset);
4647c478bd9Sstevel@tonic-gate 			break;
4657c478bd9Sstevel@tonic-gate 		}
4667c478bd9Sstevel@tonic-gate 
467*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 		case 'T':
468*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 			if (optarg) {
469*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 				if (*optarg == 'u')
470*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 					timestamp_fmt = UDATE;
471*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 				else if (*optarg == 'd')
472*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 					timestamp_fmt = DDATE;
473*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 				else
474*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 					usage();
475*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 			} else {
476*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 				usage();
477*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 			}
478*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 			break;
479*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 
4807c478bd9Sstevel@tonic-gate 		default:
4812b6abe65SJonathan Haslam 			if (strchr(INTRSTAT_OPTSTR, c) == NULL)
4827c478bd9Sstevel@tonic-gate 				usage();
4837c478bd9Sstevel@tonic-gate 		}
4847c478bd9Sstevel@tonic-gate 	}
4857c478bd9Sstevel@tonic-gate 
4862b6abe65SJonathan Haslam 	if ((g_dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) {
4872b6abe65SJonathan Haslam 		fatal("cannot open dtrace library: %s\n",
4882b6abe65SJonathan Haslam 		    dtrace_errmsg(NULL, err));
4892b6abe65SJonathan Haslam 	}
4902b6abe65SJonathan Haslam 
4912b6abe65SJonathan Haslam 	if ((prog = dtrace_program_strcompile(g_dtp, g_prog,
4922b6abe65SJonathan Haslam 	    DTRACE_PROBESPEC_NAME, 0, 0, NULL)) == NULL)
4932b6abe65SJonathan Haslam 		fatal("invalid program");
4942b6abe65SJonathan Haslam 
4952b6abe65SJonathan Haslam 	if (dtrace_program_exec(g_dtp, prog, &info) == -1)
4962b6abe65SJonathan Haslam 		fatal("failed to enable probes");
4972b6abe65SJonathan Haslam 
4982b6abe65SJonathan Haslam 	if (dtrace_setopt(g_dtp, "aggsize", "128k") == -1)
4992b6abe65SJonathan Haslam 		fatal("failed to set 'aggsize'");
5002b6abe65SJonathan Haslam 
5012b6abe65SJonathan Haslam 	if (dtrace_setopt(g_dtp, "aggrate", "0") == -1)
5022b6abe65SJonathan Haslam 		fatal("failed to set 'aggrate'");
5032b6abe65SJonathan Haslam 
5042b6abe65SJonathan Haslam 	if (dtrace_setopt(g_dtp, "aggpercpu", 0) == -1)
5052b6abe65SJonathan Haslam 		fatal("failed to set 'aggpercpu'");
5062b6abe65SJonathan Haslam 
5072b6abe65SJonathan Haslam 	optind = 1;
5082b6abe65SJonathan Haslam 	while ((c = getopt(argc, argv, INTRSTAT_OPTSTR)) != EOF) {
5092b6abe65SJonathan Haslam 		switch (c) {
5102b6abe65SJonathan Haslam 		case 'x':
5112b6abe65SJonathan Haslam 			if ((p = strchr(optarg, '=')) != NULL)
5122b6abe65SJonathan Haslam 				*p++ = '\0';
5132b6abe65SJonathan Haslam 
5142b6abe65SJonathan Haslam 			if (dtrace_setopt(g_dtp, optarg, p) != 0)
5152b6abe65SJonathan Haslam 				fatal("failed to set -x %s", optarg);
5162b6abe65SJonathan Haslam 			break;
5172b6abe65SJonathan Haslam 		}
5182b6abe65SJonathan Haslam 	}
5192b6abe65SJonathan Haslam 
5207c478bd9Sstevel@tonic-gate 	if (optind != argc) {
5217c478bd9Sstevel@tonic-gate 		g_sleeptime = strtol(argv[optind], &end, 10);
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 		if (*end != NULL || g_sleeptime == 0)
5247c478bd9Sstevel@tonic-gate 			fatal("invalid interval '%s'\n", argv[1]);
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 		if (g_sleeptime <= 0)
5277c478bd9Sstevel@tonic-gate 			fatal("interval must be greater than zero.\n");
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 		if (g_sleeptime == LONG_MAX && errno == ERANGE)
5307c478bd9Sstevel@tonic-gate 			fatal("invalid interval '%s'\n", argv[optind]);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		if (++optind != argc) {
5337c478bd9Sstevel@tonic-gate 			char *s = argv[optind];
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 			iter = strtol(s, &end, 0);
5367c478bd9Sstevel@tonic-gate 			indefinite = 0;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 			if (*end != '\0' || iter <= 0 ||
5397c478bd9Sstevel@tonic-gate 			    (iter == LONG_MAX && errno == ERANGE))
5407c478bd9Sstevel@tonic-gate 				fatal("invalid count '%s'\n", s);
5417c478bd9Sstevel@tonic-gate 		}
5427c478bd9Sstevel@tonic-gate 	}
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	ts.it_value.tv_sec = g_sleeptime;
5457c478bd9Sstevel@tonic-gate 	ts.it_value.tv_nsec = 0;
5467c478bd9Sstevel@tonic-gate 	ts.it_interval.tv_sec = g_sleeptime;
5477c478bd9Sstevel@tonic-gate 	ts.it_interval.tv_nsec = 0;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1)
5507c478bd9Sstevel@tonic-gate 		fatal("cannot set time on CLOCK_REALTIME timer");
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus && !g_present[i]; i++)
5537c478bd9Sstevel@tonic-gate 		continue;
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	if (i == g_max_cpus) {
5567c478bd9Sstevel@tonic-gate 		for (i = 0; i < g_max_cpus; i++)
5577c478bd9Sstevel@tonic-gate 			g_present[i] = p_online(i, P_STATUS) == -1 ? 0 : 1;
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	if (dtrace_go(g_dtp) != 0)
5617c478bd9Sstevel@tonic-gate 		fatal("dtrace_go()");
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	last = gethrtime();
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	if (dtrace_getopt(g_dtp, "statusrate", &statustime) == -1)
5667c478bd9Sstevel@tonic-gate 		fatal("failed to get 'statusrate'");
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	if (statustime < ((dtrace_optval_t)g_sleeptime * NANOSEC)) {
5697c478bd9Sstevel@tonic-gate 		ev.sigev_notify = SIGEV_SIGNAL;
5707c478bd9Sstevel@tonic-gate 		ev.sigev_signo = SIGUSR2;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 		if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1)
5737c478bd9Sstevel@tonic-gate 			fatal("cannot create status timer");
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 		ts.it_value.tv_sec = statustime / NANOSEC;
5767c478bd9Sstevel@tonic-gate 		ts.it_value.tv_nsec = statustime % NANOSEC;
5777c478bd9Sstevel@tonic-gate 		ts.it_interval = ts.it_value;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 		if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1)
5807c478bd9Sstevel@tonic-gate 			fatal("cannot set time on status timer");
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	while (indefinite || iter) {
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 		(void) sigsuspend(&set);
5887c478bd9Sstevel@tonic-gate 
5892b6abe65SJonathan Haslam 		if (dtrace_status(g_dtp) == -1)
5902b6abe65SJonathan Haslam 			fatal("dtrace_status()");
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 		if (g_intr == 0)
5937c478bd9Sstevel@tonic-gate 			continue;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 		iter--;
5967c478bd9Sstevel@tonic-gate 		g_intr--;
5977c478bd9Sstevel@tonic-gate 		check_pset();
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 		now = gethrtime();
6007c478bd9Sstevel@tonic-gate 		g_interval = now - last;
6017c478bd9Sstevel@tonic-gate 		last = now;
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 		if (dtrace_aggregate_snap(g_dtp) != 0)
6047c478bd9Sstevel@tonic-gate 			fatal("failed to add to aggregate");
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 		g_start = g_end = 0;
6077c478bd9Sstevel@tonic-gate 
608*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 		if (timestamp_fmt != NODATE)
609*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 			print_timestamp(timestamp_fmt);
610*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 
6117c478bd9Sstevel@tonic-gate 		do {
6127c478bd9Sstevel@tonic-gate 			g_header = 1;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 			if (dtrace_aggregate_walk_keyvarsorted(g_dtp,
6157c478bd9Sstevel@tonic-gate 			    walk, NULL) != 0)
6167c478bd9Sstevel@tonic-gate 				fatal("failed to sort aggregate");
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 			if (g_start == g_end)
6197c478bd9Sstevel@tonic-gate 				break;
6207c478bd9Sstevel@tonic-gate 		} while ((g_start = g_end) < g_max_cpus);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 		dtrace_aggregate_clear(g_dtp);
6237c478bd9Sstevel@tonic-gate 	}
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	return (0);
6267c478bd9Sstevel@tonic-gate }
627