xref: /illumos-gate/usr/src/cmd/intrstat/intrstat.c (revision ef150c2b)
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>
3726fd7700SKrishnendu Sadhukhan - Sun Microsystems #include <locale.h>
3826fd7700SKrishnendu Sadhukhan - Sun Microsystems 
3926fd7700SKrishnendu 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)
4526fd7700SKrishnendu Sadhukhan - Sun Microsystems #define	INTRSTAT_OPTSTR			"x:c:C:T:"
4626fd7700SKrishnendu Sadhukhan - Sun Microsystems 
4726fd7700SKrishnendu Sadhukhan - Sun Microsystems static uint_t timestamp_fmt = NODATE;
4826fd7700SKrishnendu Sadhukhan - Sun Microsystems 
4926fd7700SKrishnendu Sadhukhan - Sun Microsystems #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
5026fd7700SKrishnendu Sadhukhan - Sun Microsystems #define	TEXT_DOMAIN "SYS_TEST"		/* Use this only if it isn't */
5126fd7700SKrishnendu 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
usage(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]] "
9026fd7700SKrishnendu 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
fatal(const char * fmt,...)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
intr(int signo)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
status(int signal __unused)12030699046SRichard Lowe status(int signal __unused)
12130699046SRichard Lowe {
12230699046SRichard Lowe }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate static void
set_width(int sig __unused)12530699046SRichard Lowe set_width(int sig __unused)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	struct winsize win;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if (!isatty(fileno(stdout)))
1307c478bd9Sstevel@tonic-gate 		return;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(stdout), TIOCGWINSZ, &win) == -1)
1337c478bd9Sstevel@tonic-gate 		return;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	if (win.ws_col == 0) {
1367c478bd9Sstevel@tonic-gate 		/*
1377c478bd9Sstevel@tonic-gate 		 * If TIOCGWINSZ returned 0 for the columns, just return --
1387c478bd9Sstevel@tonic-gate 		 * thereby using the default value of g_cpus_per_line.  (This
1397c478bd9Sstevel@tonic-gate 		 * happens, e.g., when running over a tip line.)
1407c478bd9Sstevel@tonic-gate 		 */
1417c478bd9Sstevel@tonic-gate 		return;
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	g_cpus_per_line = INTRSTAT_CPUS_PER_LINE(win.ws_col);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	if (g_cpus_per_line < 1)
1477c478bd9Sstevel@tonic-gate 		g_cpus_per_line = 1;
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate static void
print_header()1517c478bd9Sstevel@tonic-gate print_header()
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	int i, j;
1547c478bd9Sstevel@tonic-gate 	char c[256];
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if (!g_header)
1577c478bd9Sstevel@tonic-gate 		return;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	(void) printf("\n%12s |", "device");
1607c478bd9Sstevel@tonic-gate 	for (i = g_start, j = 0; i < g_max_cpus; i++) {
1617c478bd9Sstevel@tonic-gate 		if (!g_present[i])
1627c478bd9Sstevel@tonic-gate 			continue;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 		(void) sprintf(c, "cpu%d", i);
1657c478bd9Sstevel@tonic-gate 		(void) printf(" %9s %%tim", c);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 		if (++j >= g_cpus_per_line)
1687c478bd9Sstevel@tonic-gate 			break;
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	(void) printf("\n-------------+");
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	while (j--)
1747c478bd9Sstevel@tonic-gate 		(void) printf("---------------");
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	(void) printf("\n");
1777c478bd9Sstevel@tonic-gate 	g_header = 0;
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1817c478bd9Sstevel@tonic-gate static int
walk(const dtrace_aggdata_t * data,void * arg)182a1b5e537Sbmc walk(const dtrace_aggdata_t *data, void *arg)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	dtrace_aggdesc_t *aggdesc = data->dtada_desc;
1857c478bd9Sstevel@tonic-gate 	dtrace_recdesc_t *nrec, *irec;
1867c478bd9Sstevel@tonic-gate 	char *name, c[256];
1877c478bd9Sstevel@tonic-gate 	int32_t *instance;
188a1b5e537Sbmc 	static const dtrace_aggdata_t *count;
1897c478bd9Sstevel@tonic-gate 	int i, j;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	if (count == NULL) {
1927c478bd9Sstevel@tonic-gate 		count = data;
1937c478bd9Sstevel@tonic-gate 		return (DTRACE_AGGWALK_NEXT);
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	nrec = &aggdesc->dtagd_rec[1];
1977c478bd9Sstevel@tonic-gate 	irec = &aggdesc->dtagd_rec[2];
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	name = data->dtada_data + nrec->dtrd_offset;
2007c478bd9Sstevel@tonic-gate 	/* LINTED - alignment */
2017c478bd9Sstevel@tonic-gate 	instance = (int32_t *)(data->dtada_data + irec->dtrd_offset);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	for (i = g_start, j = 0; i < g_max_cpus && j < g_cpus_per_line; i++) {
2047c478bd9Sstevel@tonic-gate 		/* LINTED - alignment */
2057c478bd9Sstevel@tonic-gate 		uint64_t time = *((uint64_t *)(data->dtada_percpu[i]));
2067c478bd9Sstevel@tonic-gate 		/* LINTED - alignment */
2077c478bd9Sstevel@tonic-gate 		uint64_t n = *((uint64_t *)(count->dtada_percpu[i]));
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 		if (!g_present[i])
2107c478bd9Sstevel@tonic-gate 			continue;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		if (j++ == 0) {
2137c478bd9Sstevel@tonic-gate 			print_header();
2147c478bd9Sstevel@tonic-gate 			(void) snprintf(c, sizeof (c), "%s#%d",
2152b6abe65SJonathan Haslam 			    name, *instance);
2167c478bd9Sstevel@tonic-gate 			(void) printf("%12s |", c);
2177c478bd9Sstevel@tonic-gate 		}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 		(void) printf(" %9lld %4.1f",
2207c478bd9Sstevel@tonic-gate 		    (unsigned long long)((double)n /
2217c478bd9Sstevel@tonic-gate 		    ((double)g_interval / (double)NANOSEC)),
2227c478bd9Sstevel@tonic-gate 		    ((double)time * (double)100.0) / (double)g_interval);
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	(void) printf(j ? "\n" : "");
2267c478bd9Sstevel@tonic-gate 	g_end = i;
2277c478bd9Sstevel@tonic-gate 	count = NULL;
2287c478bd9Sstevel@tonic-gate 	return (DTRACE_AGGWALK_NEXT);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate static void
select_cpu(processorid_t cpu)2327c478bd9Sstevel@tonic-gate select_cpu(processorid_t cpu)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate 	if (g_pset != PS_NONE)
2357c478bd9Sstevel@tonic-gate 		fatal("cannot specify both a processor set and a processor\n");
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	if (cpu < 0 || cpu >= g_max_cpus)
2387c478bd9Sstevel@tonic-gate 		fatal("cpu %d out of range\n", cpu);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	if (p_online(cpu, P_STATUS) == -1) {
2417c478bd9Sstevel@tonic-gate 		if (errno != EINVAL)
2427c478bd9Sstevel@tonic-gate 			fatal("could not get status for cpu %d", cpu);
2437c478bd9Sstevel@tonic-gate 		fatal("cpu %d not present\n", cpu);
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	g_present[cpu] = 1;
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate static void
select_cpus(processorid_t low,processorid_t high)2507c478bd9Sstevel@tonic-gate select_cpus(processorid_t low, processorid_t high)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	if (g_pset != PS_NONE)
2537c478bd9Sstevel@tonic-gate 		fatal("cannot specify both a processor set and processors\n");
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	if (low < 0 || low >= g_max_cpus)
2567c478bd9Sstevel@tonic-gate 		fatal("invalid cpu '%d'\n", low);
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	if (high < 0 || high >= g_max_cpus)
2597c478bd9Sstevel@tonic-gate 		fatal("invalid cpu '%d'\n", high);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if (low >= high)
2627c478bd9Sstevel@tonic-gate 		fatal("invalid range '%d' to '%d'\n", low, high);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	do {
2657c478bd9Sstevel@tonic-gate 		if (p_online(low, P_STATUS) != -1)
2667c478bd9Sstevel@tonic-gate 			g_present[low] = 1;
2677c478bd9Sstevel@tonic-gate 	} while (++low <= high);
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate static void
select_pset(psetid_t pset)2717c478bd9Sstevel@tonic-gate select_pset(psetid_t pset)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 	processorid_t i;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	if (pset < 0)
2767c478bd9Sstevel@tonic-gate 		fatal("processor set %d is out of range\n", pset);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	/*
2797c478bd9Sstevel@tonic-gate 	 * Only one processor set can be specified.
2807c478bd9Sstevel@tonic-gate 	 */
2817c478bd9Sstevel@tonic-gate 	if (g_pset != PS_NONE)
2827c478bd9Sstevel@tonic-gate 		fatal("at most one processor set may be specified\n");
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	/*
2857c478bd9Sstevel@tonic-gate 	 * One cannot select processors _and_ a processor set.
2867c478bd9Sstevel@tonic-gate 	 */
2877c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus; i++)
2887c478bd9Sstevel@tonic-gate 		if (g_present[i])
2897c478bd9Sstevel@tonic-gate 			break;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	if (i != g_max_cpus)
2927c478bd9Sstevel@tonic-gate 		fatal("cannot specify both a processor and a processor set\n");
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	g_pset = pset;
2957c478bd9Sstevel@tonic-gate 	g_pset_ncpus = g_max_cpus;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	if (pset_info(g_pset, NULL, &g_pset_ncpus, g_pset_cpus) == -1)
2987c478bd9Sstevel@tonic-gate 		fatal("invalid processor set: %d\n", g_pset);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	if (g_pset_ncpus == 0)
3017c478bd9Sstevel@tonic-gate 		fatal("processor set %d empty\n", g_pset);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_pset_ncpus; i++)
3047c478bd9Sstevel@tonic-gate 		g_present[g_pset_cpus[i]] = 1;
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate static void
check_pset(void)3087c478bd9Sstevel@tonic-gate check_pset(void)
3097c478bd9Sstevel@tonic-gate {
3107c478bd9Sstevel@tonic-gate 	uint_t ncpus = g_max_cpus;
3117c478bd9Sstevel@tonic-gate 	processorid_t i;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	if (g_pset == PS_NONE)
3147c478bd9Sstevel@tonic-gate 		return;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	if (pset_info(g_pset, NULL, &ncpus, g_pset_cpus) == -1) {
3177c478bd9Sstevel@tonic-gate 		if (errno == EINVAL)
3187c478bd9Sstevel@tonic-gate 			fatal("processor set %d destroyed\n", g_pset);
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 		fatal("couldn't get info for processor set %d", g_pset);
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	if (ncpus == 0)
3247c478bd9Sstevel@tonic-gate 		fatal("processor set %d empty\n", g_pset);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	if (ncpus == g_pset_ncpus) {
3277c478bd9Sstevel@tonic-gate 		for (i = 0; i < g_pset_ncpus; i++) {
3287c478bd9Sstevel@tonic-gate 			if (!g_present[g_pset_cpus[i]])
3297c478bd9Sstevel@tonic-gate 				break;
3307c478bd9Sstevel@tonic-gate 		}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 		/*
3337c478bd9Sstevel@tonic-gate 		 * If the number of CPUs hasn't changed, and every CPU
3347c478bd9Sstevel@tonic-gate 		 * in the processor set is also selected, we know that the
3357c478bd9Sstevel@tonic-gate 		 * processor set itself hasn't changed.
3367c478bd9Sstevel@tonic-gate 		 */
3377c478bd9Sstevel@tonic-gate 		if (i == g_pset_ncpus)
3387c478bd9Sstevel@tonic-gate 			return;
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/*
3427c478bd9Sstevel@tonic-gate 	 * If we're here, we have a new processor set.  First, we need
3437c478bd9Sstevel@tonic-gate 	 * to zero out the present array.
3447c478bd9Sstevel@tonic-gate 	 */
3457c478bd9Sstevel@tonic-gate 	bzero(g_present, sizeof (processorid_t) * g_max_cpus);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	g_pset_ncpus = ncpus;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_pset_ncpus; i++)
3507c478bd9Sstevel@tonic-gate 		g_present[g_pset_cpus[i]] = 1;
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)3547c478bd9Sstevel@tonic-gate main(int argc, char **argv)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate 	dtrace_prog_t *prog;
3577c478bd9Sstevel@tonic-gate 	dtrace_proginfo_t info;
3587c478bd9Sstevel@tonic-gate 	int err, i, indefinite = 1;
3597c478bd9Sstevel@tonic-gate 	long iter;
3607c478bd9Sstevel@tonic-gate 	processorid_t id;
3617c478bd9Sstevel@tonic-gate 	struct sigaction act;
3627c478bd9Sstevel@tonic-gate 	struct itimerspec ts;
3637c478bd9Sstevel@tonic-gate 	struct sigevent ev;
3647c478bd9Sstevel@tonic-gate 	sigset_t set;
3657c478bd9Sstevel@tonic-gate 	timer_t tid;
3662b6abe65SJonathan Haslam 	char *end, *p;
367*ef150c2bSRichard Lowe 	int c;
3687c478bd9Sstevel@tonic-gate 	hrtime_t last, now;
3697c478bd9Sstevel@tonic-gate 	dtrace_optval_t statustime;
3707c478bd9Sstevel@tonic-gate 
37126fd7700SKrishnendu Sadhukhan - Sun Microsystems 	(void) setlocale(LC_ALL, "");
37226fd7700SKrishnendu Sadhukhan - Sun Microsystems 	(void) textdomain(TEXT_DOMAIN);
37326fd7700SKrishnendu Sadhukhan - Sun Microsystems 
3747c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
3757c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
3767c478bd9Sstevel@tonic-gate 	act.sa_handler = set_width;
3777c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGWINCH, &act, NULL);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
3807c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
3817c478bd9Sstevel@tonic-gate 	act.sa_handler = intr;
3827c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR1, &act, NULL);
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
3857c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
3867c478bd9Sstevel@tonic-gate 	act.sa_handler = status;
3877c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR2, &act, NULL);
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	act.sa_handler = set_width;
3907c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGWINCH, &act, NULL);
39130699046SRichard Lowe 	set_width(0);
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
3947c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGUSR1);
3957c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGWINCH);
3967c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &set, NULL);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	ev.sigev_notify = SIGEV_SIGNAL;
3997c478bd9Sstevel@tonic-gate 	ev.sigev_signo = SIGUSR1;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1)
4027c478bd9Sstevel@tonic-gate 		fatal("cannot create CLOCK_HIGHRES timer");
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	g_max_cpus = sysconf(_SC_CPUID_MAX) + 1;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	if ((g_present = malloc(sizeof (processorid_t) * g_max_cpus)) == NULL)
4077c478bd9Sstevel@tonic-gate 		fatal("could not allocate g_present array\n");
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	bzero(g_present, sizeof (processorid_t) * g_max_cpus);
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	g_pset_cpus = malloc(sizeof (processorid_t) * g_max_cpus);
4127c478bd9Sstevel@tonic-gate 	if (g_pset_cpus == NULL)
4137c478bd9Sstevel@tonic-gate 		fatal("could not allocate g_pset_cpus");
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	bzero(g_pset_cpus, sizeof (processorid_t) * g_max_cpus);
4167c478bd9Sstevel@tonic-gate 
4172b6abe65SJonathan Haslam 	while ((c = getopt(argc, argv, INTRSTAT_OPTSTR)) != EOF) {
4187c478bd9Sstevel@tonic-gate 		switch (c) {
4197c478bd9Sstevel@tonic-gate 		case 'c': {
4207c478bd9Sstevel@tonic-gate 			/*
4217c478bd9Sstevel@tonic-gate 			 * We allow CPUs to be specified as an optionally
4227c478bd9Sstevel@tonic-gate 			 * comma separated list of either CPU IDs or ranges
4237c478bd9Sstevel@tonic-gate 			 * of CPU IDs.
4247c478bd9Sstevel@tonic-gate 			 */
4257c478bd9Sstevel@tonic-gate 			char *s = strtok(optarg, ",");
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 			while (s != NULL) {
4287c478bd9Sstevel@tonic-gate 				id = strtoul(s, &end, 0);
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 				if (id == ULONG_MAX && errno == ERANGE) {
4317c478bd9Sstevel@tonic-gate 					*end = '\0';
4327c478bd9Sstevel@tonic-gate 					fatal("invalid cpu '%s'\n", s);
4337c478bd9Sstevel@tonic-gate 				}
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 				if (*(s = end) != '\0') {
4367c478bd9Sstevel@tonic-gate 					processorid_t p;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 					if (*s != '-')
4397c478bd9Sstevel@tonic-gate 						fatal("invalid cpu '%s'\n", s);
4407c478bd9Sstevel@tonic-gate 					p = strtoul(++s, &end, 0);
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 					if (*end != '\0' ||
4437c478bd9Sstevel@tonic-gate 					    (p == ULONG_MAX && errno == ERANGE))
4447c478bd9Sstevel@tonic-gate 						fatal("invalid cpu '%s'\n", s);
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 					select_cpus(id, p);
4477c478bd9Sstevel@tonic-gate 				} else {
4487c478bd9Sstevel@tonic-gate 					select_cpu(id);
4497c478bd9Sstevel@tonic-gate 				}
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 				s = strtok(NULL, ",");
4527c478bd9Sstevel@tonic-gate 			}
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 			break;
4557c478bd9Sstevel@tonic-gate 		}
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 		case 'C': {
4587c478bd9Sstevel@tonic-gate 			psetid_t pset = strtoul(optarg, &end, 0);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 			if (*end != '\0' ||
4617c478bd9Sstevel@tonic-gate 			    (pset == ULONG_MAX && errno == ERANGE))
4627c478bd9Sstevel@tonic-gate 				fatal("invalid processor set '%s'\n", optarg);
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 			select_pset(pset);
4657c478bd9Sstevel@tonic-gate 			break;
4667c478bd9Sstevel@tonic-gate 		}
4677c478bd9Sstevel@tonic-gate 
46826fd7700SKrishnendu Sadhukhan - Sun Microsystems 		case 'T':
46926fd7700SKrishnendu Sadhukhan - Sun Microsystems 			if (optarg) {
47026fd7700SKrishnendu Sadhukhan - Sun Microsystems 				if (*optarg == 'u')
47126fd7700SKrishnendu Sadhukhan - Sun Microsystems 					timestamp_fmt = UDATE;
47226fd7700SKrishnendu Sadhukhan - Sun Microsystems 				else if (*optarg == 'd')
47326fd7700SKrishnendu Sadhukhan - Sun Microsystems 					timestamp_fmt = DDATE;
47426fd7700SKrishnendu Sadhukhan - Sun Microsystems 				else
47526fd7700SKrishnendu Sadhukhan - Sun Microsystems 					usage();
47626fd7700SKrishnendu Sadhukhan - Sun Microsystems 			} else {
47726fd7700SKrishnendu Sadhukhan - Sun Microsystems 				usage();
47826fd7700SKrishnendu Sadhukhan - Sun Microsystems 			}
47926fd7700SKrishnendu Sadhukhan - Sun Microsystems 			break;
48026fd7700SKrishnendu Sadhukhan - Sun Microsystems 
4817c478bd9Sstevel@tonic-gate 		default:
4822b6abe65SJonathan Haslam 			if (strchr(INTRSTAT_OPTSTR, c) == NULL)
4832b6abe65SJonathan Haslam 				usage();
4842b6abe65SJonathan Haslam 		}
4852b6abe65SJonathan Haslam 	}
4862b6abe65SJonathan Haslam 
4872b6abe65SJonathan Haslam 	if ((g_dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) {
4882b6abe65SJonathan Haslam 		fatal("cannot open dtrace library: %s\n",
4892b6abe65SJonathan Haslam 		    dtrace_errmsg(NULL, err));
4902b6abe65SJonathan Haslam 	}
4912b6abe65SJonathan Haslam 
4922b6abe65SJonathan Haslam 	if ((prog = dtrace_program_strcompile(g_dtp, g_prog,
4932b6abe65SJonathan Haslam 	    DTRACE_PROBESPEC_NAME, 0, 0, NULL)) == NULL)
4942b6abe65SJonathan Haslam 		fatal("invalid program");
4952b6abe65SJonathan Haslam 
4962b6abe65SJonathan Haslam 	if (dtrace_program_exec(g_dtp, prog, &info) == -1)
4972b6abe65SJonathan Haslam 		fatal("failed to enable probes");
4982b6abe65SJonathan Haslam 
4992b6abe65SJonathan Haslam 	if (dtrace_setopt(g_dtp, "aggsize", "128k") == -1)
5002b6abe65SJonathan Haslam 		fatal("failed to set 'aggsize'");
5012b6abe65SJonathan Haslam 
5022b6abe65SJonathan Haslam 	if (dtrace_setopt(g_dtp, "aggrate", "0") == -1)
5032b6abe65SJonathan Haslam 		fatal("failed to set 'aggrate'");
5042b6abe65SJonathan Haslam 
5052b6abe65SJonathan Haslam 	if (dtrace_setopt(g_dtp, "aggpercpu", 0) == -1)
5062b6abe65SJonathan Haslam 		fatal("failed to set 'aggpercpu'");
5072b6abe65SJonathan Haslam 
5082b6abe65SJonathan Haslam 	optind = 1;
5092b6abe65SJonathan Haslam 	while ((c = getopt(argc, argv, INTRSTAT_OPTSTR)) != EOF) {
5102b6abe65SJonathan Haslam 		switch (c) {
5112b6abe65SJonathan Haslam 		case 'x':
5122b6abe65SJonathan Haslam 			if ((p = strchr(optarg, '=')) != NULL)
5132b6abe65SJonathan Haslam 				*p++ = '\0';
5142b6abe65SJonathan Haslam 
5152b6abe65SJonathan Haslam 			if (dtrace_setopt(g_dtp, optarg, p) != 0)
5162b6abe65SJonathan Haslam 				fatal("failed to set -x %s", optarg);
5172b6abe65SJonathan Haslam 			break;
5187c478bd9Sstevel@tonic-gate 		}
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	if (optind != argc) {
5227c478bd9Sstevel@tonic-gate 		g_sleeptime = strtol(argv[optind], &end, 10);
5237c478bd9Sstevel@tonic-gate 
52420a3e33eSToomas Soome 		if (*end != '\0' || g_sleeptime == 0)
5257c478bd9Sstevel@tonic-gate 			fatal("invalid interval '%s'\n", argv[1]);
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 		if (g_sleeptime <= 0)
5287c478bd9Sstevel@tonic-gate 			fatal("interval must be greater than zero.\n");
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 		if (g_sleeptime == LONG_MAX && errno == ERANGE)
5317c478bd9Sstevel@tonic-gate 			fatal("invalid interval '%s'\n", argv[optind]);
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 		if (++optind != argc) {
5347c478bd9Sstevel@tonic-gate 			char *s = argv[optind];
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 			iter = strtol(s, &end, 0);
5377c478bd9Sstevel@tonic-gate 			indefinite = 0;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 			if (*end != '\0' || iter <= 0 ||
5407c478bd9Sstevel@tonic-gate 			    (iter == LONG_MAX && errno == ERANGE))
5417c478bd9Sstevel@tonic-gate 				fatal("invalid count '%s'\n", s);
5427c478bd9Sstevel@tonic-gate 		}
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	ts.it_value.tv_sec = g_sleeptime;
5467c478bd9Sstevel@tonic-gate 	ts.it_value.tv_nsec = 0;
5477c478bd9Sstevel@tonic-gate 	ts.it_interval.tv_sec = g_sleeptime;
5487c478bd9Sstevel@tonic-gate 	ts.it_interval.tv_nsec = 0;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1)
5517c478bd9Sstevel@tonic-gate 		fatal("cannot set time on CLOCK_REALTIME timer");
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus && !g_present[i]; i++)
5547c478bd9Sstevel@tonic-gate 		continue;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	if (i == g_max_cpus) {
5577c478bd9Sstevel@tonic-gate 		for (i = 0; i < g_max_cpus; i++)
5587c478bd9Sstevel@tonic-gate 			g_present[i] = p_online(i, P_STATUS) == -1 ? 0 : 1;
5597c478bd9Sstevel@tonic-gate 	}
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	if (dtrace_go(g_dtp) != 0)
5627c478bd9Sstevel@tonic-gate 		fatal("dtrace_go()");
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	last = gethrtime();
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	if (dtrace_getopt(g_dtp, "statusrate", &statustime) == -1)
5677c478bd9Sstevel@tonic-gate 		fatal("failed to get 'statusrate'");
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	if (statustime < ((dtrace_optval_t)g_sleeptime * NANOSEC)) {
5707c478bd9Sstevel@tonic-gate 		ev.sigev_notify = SIGEV_SIGNAL;
5717c478bd9Sstevel@tonic-gate 		ev.sigev_signo = SIGUSR2;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 		if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1)
5747c478bd9Sstevel@tonic-gate 			fatal("cannot create status timer");
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 		ts.it_value.tv_sec = statustime / NANOSEC;
5777c478bd9Sstevel@tonic-gate 		ts.it_value.tv_nsec = statustime % NANOSEC;
5787c478bd9Sstevel@tonic-gate 		ts.it_interval = ts.it_value;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 		if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1)
5817c478bd9Sstevel@tonic-gate 			fatal("cannot set time on status timer");
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	while (indefinite || iter) {
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 		(void) sigsuspend(&set);
5897c478bd9Sstevel@tonic-gate 
5902b6abe65SJonathan Haslam 		if (dtrace_status(g_dtp) == -1)
5912b6abe65SJonathan Haslam 			fatal("dtrace_status()");
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 		if (g_intr == 0)
5947c478bd9Sstevel@tonic-gate 			continue;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 		iter--;
5977c478bd9Sstevel@tonic-gate 		g_intr--;
5987c478bd9Sstevel@tonic-gate 		check_pset();
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 		now = gethrtime();
6017c478bd9Sstevel@tonic-gate 		g_interval = now - last;
6027c478bd9Sstevel@tonic-gate 		last = now;
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 		if (dtrace_aggregate_snap(g_dtp) != 0)
6057c478bd9Sstevel@tonic-gate 			fatal("failed to add to aggregate");
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 		g_start = g_end = 0;
6087c478bd9Sstevel@tonic-gate 
60926fd7700SKrishnendu Sadhukhan - Sun Microsystems 		if (timestamp_fmt != NODATE)
61026fd7700SKrishnendu Sadhukhan - Sun Microsystems 			print_timestamp(timestamp_fmt);
61126fd7700SKrishnendu Sadhukhan - Sun Microsystems 
6127c478bd9Sstevel@tonic-gate 		do {
6137c478bd9Sstevel@tonic-gate 			g_header = 1;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 			if (dtrace_aggregate_walk_keyvarsorted(g_dtp,
6167c478bd9Sstevel@tonic-gate 			    walk, NULL) != 0)
6177c478bd9Sstevel@tonic-gate 				fatal("failed to sort aggregate");
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 			if (g_start == g_end)
6207c478bd9Sstevel@tonic-gate 				break;
6217c478bd9Sstevel@tonic-gate 		} while ((g_start = g_end) < g_max_cpus);
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 		dtrace_aggregate_clear(g_dtp);
6247c478bd9Sstevel@tonic-gate 	}
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	return (0);
6277c478bd9Sstevel@tonic-gate }
628