xref: /titanic_51/usr/src/cmd/intrstat/intrstat.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
31*7c478bd9Sstevel@tonic-gate #include <dtrace.h>
32*7c478bd9Sstevel@tonic-gate #include <errno.h>
33*7c478bd9Sstevel@tonic-gate #include <string.h>
34*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
35*7c478bd9Sstevel@tonic-gate #include <unistd.h>
36*7c478bd9Sstevel@tonic-gate #include <limits.h>
37*7c478bd9Sstevel@tonic-gate #include <strings.h>
38*7c478bd9Sstevel@tonic-gate #include <termio.h>
39*7c478bd9Sstevel@tonic-gate #include <signal.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #define	INTRSTAT_COLUMN_OFFS		14
42*7c478bd9Sstevel@tonic-gate #define	INTRSTAT_COLUMNS_PER_CPU	15
43*7c478bd9Sstevel@tonic-gate #define	INTRSTAT_CPUS_PER_LINE(w)	\
44*7c478bd9Sstevel@tonic-gate 	(((w) - INTRSTAT_COLUMN_OFFS) / INTRSTAT_COLUMNS_PER_CPU)
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate static dtrace_hdl_t *g_dtp;
47*7c478bd9Sstevel@tonic-gate static int *g_present;
48*7c478bd9Sstevel@tonic-gate static int g_max_cpus;
49*7c478bd9Sstevel@tonic-gate static int g_start, g_end;
50*7c478bd9Sstevel@tonic-gate static int g_header;
51*7c478bd9Sstevel@tonic-gate static long g_sleeptime = 1;
52*7c478bd9Sstevel@tonic-gate static hrtime_t g_interval = NANOSEC;
53*7c478bd9Sstevel@tonic-gate static int g_intr;
54*7c478bd9Sstevel@tonic-gate static psetid_t g_pset = PS_NONE;
55*7c478bd9Sstevel@tonic-gate static processorid_t *g_pset_cpus;
56*7c478bd9Sstevel@tonic-gate static uint_t g_pset_ncpus;
57*7c478bd9Sstevel@tonic-gate static int g_cpus_per_line = INTRSTAT_CPUS_PER_LINE(80);
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate static const char *g_pname = "intrstat";
60*7c478bd9Sstevel@tonic-gate static const char *g_prog =
61*7c478bd9Sstevel@tonic-gate "interrupt-start"
62*7c478bd9Sstevel@tonic-gate "{"
63*7c478bd9Sstevel@tonic-gate "	self->ts = vtimestamp;"
64*7c478bd9Sstevel@tonic-gate "}"
65*7c478bd9Sstevel@tonic-gate ""
66*7c478bd9Sstevel@tonic-gate "interrupt-complete"
67*7c478bd9Sstevel@tonic-gate "/self->ts/"
68*7c478bd9Sstevel@tonic-gate "{"
69*7c478bd9Sstevel@tonic-gate "	this->devi = (struct dev_info *)arg0;"
70*7c478bd9Sstevel@tonic-gate "	@counts[stringof(`devnamesp[this->devi->devi_major].dn_name),"
71*7c478bd9Sstevel@tonic-gate "	     this->devi->devi_instance] = count();"
72*7c478bd9Sstevel@tonic-gate "	@times[stringof(`devnamesp[this->devi->devi_major].dn_name),"
73*7c478bd9Sstevel@tonic-gate "	     this->devi->devi_instance] = sum(vtimestamp - self->ts);"
74*7c478bd9Sstevel@tonic-gate "	self->ts = 0;"
75*7c478bd9Sstevel@tonic-gate "}";
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate static void
78*7c478bd9Sstevel@tonic-gate usage(void)
79*7c478bd9Sstevel@tonic-gate {
80*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
81*7c478bd9Sstevel@tonic-gate 	    "usage:  intrstat [ -C psrset | -c cpulist ] "
82*7c478bd9Sstevel@tonic-gate 	    "[interval [ count]]\n");
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
85*7c478bd9Sstevel@tonic-gate }
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate static void
88*7c478bd9Sstevel@tonic-gate fatal(const char *fmt, ...)
89*7c478bd9Sstevel@tonic-gate {
90*7c478bd9Sstevel@tonic-gate 	va_list ap;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", g_pname);
95*7c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	if (fmt[strlen(fmt) - 1] != '\n')
98*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s\n",
99*7c478bd9Sstevel@tonic-gate 		    dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
102*7c478bd9Sstevel@tonic-gate }
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
105*7c478bd9Sstevel@tonic-gate static void
106*7c478bd9Sstevel@tonic-gate intr(int signo)
107*7c478bd9Sstevel@tonic-gate {
108*7c478bd9Sstevel@tonic-gate 	g_intr++;
109*7c478bd9Sstevel@tonic-gate }
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate static void
112*7c478bd9Sstevel@tonic-gate status(void)
113*7c478bd9Sstevel@tonic-gate {}
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate static void
116*7c478bd9Sstevel@tonic-gate set_width(void)
117*7c478bd9Sstevel@tonic-gate {
118*7c478bd9Sstevel@tonic-gate 	struct winsize win;
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	if (!isatty(fileno(stdout)))
121*7c478bd9Sstevel@tonic-gate 		return;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(stdout), TIOCGWINSZ, &win) == -1)
124*7c478bd9Sstevel@tonic-gate 		return;
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	if (win.ws_col == 0) {
127*7c478bd9Sstevel@tonic-gate 		/*
128*7c478bd9Sstevel@tonic-gate 		 * If TIOCGWINSZ returned 0 for the columns, just return --
129*7c478bd9Sstevel@tonic-gate 		 * thereby using the default value of g_cpus_per_line.  (This
130*7c478bd9Sstevel@tonic-gate 		 * happens, e.g., when running over a tip line.)
131*7c478bd9Sstevel@tonic-gate 		 */
132*7c478bd9Sstevel@tonic-gate 		return;
133*7c478bd9Sstevel@tonic-gate 	}
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	g_cpus_per_line = INTRSTAT_CPUS_PER_LINE(win.ws_col);
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	if (g_cpus_per_line < 1)
138*7c478bd9Sstevel@tonic-gate 		g_cpus_per_line = 1;
139*7c478bd9Sstevel@tonic-gate }
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate static void
142*7c478bd9Sstevel@tonic-gate print_header()
143*7c478bd9Sstevel@tonic-gate {
144*7c478bd9Sstevel@tonic-gate 	int i, j;
145*7c478bd9Sstevel@tonic-gate 	char c[256];
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	if (!g_header)
148*7c478bd9Sstevel@tonic-gate 		return;
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	(void) printf("\n%12s |", "device");
151*7c478bd9Sstevel@tonic-gate 	for (i = g_start, j = 0; i < g_max_cpus; i++) {
152*7c478bd9Sstevel@tonic-gate 		if (!g_present[i])
153*7c478bd9Sstevel@tonic-gate 			continue;
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 		(void) sprintf(c, "cpu%d", i);
156*7c478bd9Sstevel@tonic-gate 		(void) printf(" %9s %%tim", c);
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 		if (++j >= g_cpus_per_line)
159*7c478bd9Sstevel@tonic-gate 			break;
160*7c478bd9Sstevel@tonic-gate 	}
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	(void) printf("\n-------------+");
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	while (j--)
165*7c478bd9Sstevel@tonic-gate 		(void) printf("---------------");
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	(void) printf("\n");
168*7c478bd9Sstevel@tonic-gate 	g_header = 0;
169*7c478bd9Sstevel@tonic-gate }
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
172*7c478bd9Sstevel@tonic-gate static int
173*7c478bd9Sstevel@tonic-gate walk(dtrace_aggdata_t *data, void *arg)
174*7c478bd9Sstevel@tonic-gate {
175*7c478bd9Sstevel@tonic-gate 	dtrace_aggdesc_t *aggdesc = data->dtada_desc;
176*7c478bd9Sstevel@tonic-gate 	dtrace_recdesc_t *nrec, *irec;
177*7c478bd9Sstevel@tonic-gate 	char *name, c[256];
178*7c478bd9Sstevel@tonic-gate 	int32_t *instance;
179*7c478bd9Sstevel@tonic-gate 	static dtrace_aggdata_t *count;
180*7c478bd9Sstevel@tonic-gate 	int i, j;
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 	if (count == NULL) {
183*7c478bd9Sstevel@tonic-gate 		count = data;
184*7c478bd9Sstevel@tonic-gate 		return (DTRACE_AGGWALK_NEXT);
185*7c478bd9Sstevel@tonic-gate 	}
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 	nrec = &aggdesc->dtagd_rec[1];
188*7c478bd9Sstevel@tonic-gate 	irec = &aggdesc->dtagd_rec[2];
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	name = data->dtada_data + nrec->dtrd_offset;
191*7c478bd9Sstevel@tonic-gate 	/* LINTED - alignment */
192*7c478bd9Sstevel@tonic-gate 	instance = (int32_t *)(data->dtada_data + irec->dtrd_offset);
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	for (i = g_start, j = 0; i < g_max_cpus && j < g_cpus_per_line; i++) {
195*7c478bd9Sstevel@tonic-gate 		/* LINTED - alignment */
196*7c478bd9Sstevel@tonic-gate 		uint64_t time = *((uint64_t *)(data->dtada_percpu[i]));
197*7c478bd9Sstevel@tonic-gate 		/* LINTED - alignment */
198*7c478bd9Sstevel@tonic-gate 		uint64_t n = *((uint64_t *)(count->dtada_percpu[i]));
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 		if (!g_present[i])
201*7c478bd9Sstevel@tonic-gate 			continue;
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 		if (j++ == 0) {
204*7c478bd9Sstevel@tonic-gate 			print_header();
205*7c478bd9Sstevel@tonic-gate 			(void) snprintf(c, sizeof (c), "%s#%d",
206*7c478bd9Sstevel@tonic-gate 				name, *instance);
207*7c478bd9Sstevel@tonic-gate 			(void) printf("%12s |", c);
208*7c478bd9Sstevel@tonic-gate 		}
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 		(void) printf(" %9lld %4.1f",
211*7c478bd9Sstevel@tonic-gate 		    (unsigned long long)((double)n /
212*7c478bd9Sstevel@tonic-gate 		    ((double)g_interval / (double)NANOSEC)),
213*7c478bd9Sstevel@tonic-gate 		    ((double)time * (double)100.0) / (double)g_interval);
214*7c478bd9Sstevel@tonic-gate 	}
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 	(void) printf(j ? "\n" : "");
217*7c478bd9Sstevel@tonic-gate 	g_end = i;
218*7c478bd9Sstevel@tonic-gate 	count = NULL;
219*7c478bd9Sstevel@tonic-gate 	return (DTRACE_AGGWALK_NEXT);
220*7c478bd9Sstevel@tonic-gate }
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate static void
223*7c478bd9Sstevel@tonic-gate select_cpu(processorid_t cpu)
224*7c478bd9Sstevel@tonic-gate {
225*7c478bd9Sstevel@tonic-gate 	if (g_pset != PS_NONE)
226*7c478bd9Sstevel@tonic-gate 		fatal("cannot specify both a processor set and a processor\n");
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 	if (cpu < 0 || cpu >= g_max_cpus)
229*7c478bd9Sstevel@tonic-gate 		fatal("cpu %d out of range\n", cpu);
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate 	if (p_online(cpu, P_STATUS) == -1) {
232*7c478bd9Sstevel@tonic-gate 		if (errno != EINVAL)
233*7c478bd9Sstevel@tonic-gate 			fatal("could not get status for cpu %d", cpu);
234*7c478bd9Sstevel@tonic-gate 		fatal("cpu %d not present\n", cpu);
235*7c478bd9Sstevel@tonic-gate 	}
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 	g_present[cpu] = 1;
238*7c478bd9Sstevel@tonic-gate }
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate static void
241*7c478bd9Sstevel@tonic-gate select_cpus(processorid_t low, processorid_t high)
242*7c478bd9Sstevel@tonic-gate {
243*7c478bd9Sstevel@tonic-gate 	if (g_pset != PS_NONE)
244*7c478bd9Sstevel@tonic-gate 		fatal("cannot specify both a processor set and processors\n");
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 	if (low < 0 || low >= g_max_cpus)
247*7c478bd9Sstevel@tonic-gate 		fatal("invalid cpu '%d'\n", low);
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 	if (high < 0 || high >= g_max_cpus)
250*7c478bd9Sstevel@tonic-gate 		fatal("invalid cpu '%d'\n", high);
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 	if (low >= high)
253*7c478bd9Sstevel@tonic-gate 		fatal("invalid range '%d' to '%d'\n", low, high);
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	do {
256*7c478bd9Sstevel@tonic-gate 		if (p_online(low, P_STATUS) != -1)
257*7c478bd9Sstevel@tonic-gate 			g_present[low] = 1;
258*7c478bd9Sstevel@tonic-gate 	} while (++low <= high);
259*7c478bd9Sstevel@tonic-gate }
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate static void
262*7c478bd9Sstevel@tonic-gate select_pset(psetid_t pset)
263*7c478bd9Sstevel@tonic-gate {
264*7c478bd9Sstevel@tonic-gate 	processorid_t i;
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 	if (pset < 0)
267*7c478bd9Sstevel@tonic-gate 		fatal("processor set %d is out of range\n", pset);
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 	/*
270*7c478bd9Sstevel@tonic-gate 	 * Only one processor set can be specified.
271*7c478bd9Sstevel@tonic-gate 	 */
272*7c478bd9Sstevel@tonic-gate 	if (g_pset != PS_NONE)
273*7c478bd9Sstevel@tonic-gate 		fatal("at most one processor set may be specified\n");
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate 	/*
276*7c478bd9Sstevel@tonic-gate 	 * One cannot select processors _and_ a processor set.
277*7c478bd9Sstevel@tonic-gate 	 */
278*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus; i++)
279*7c478bd9Sstevel@tonic-gate 		if (g_present[i])
280*7c478bd9Sstevel@tonic-gate 			break;
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 	if (i != g_max_cpus)
283*7c478bd9Sstevel@tonic-gate 		fatal("cannot specify both a processor and a processor set\n");
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate 	g_pset = pset;
286*7c478bd9Sstevel@tonic-gate 	g_pset_ncpus = g_max_cpus;
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 	if (pset_info(g_pset, NULL, &g_pset_ncpus, g_pset_cpus) == -1)
289*7c478bd9Sstevel@tonic-gate 		fatal("invalid processor set: %d\n", g_pset);
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate 	if (g_pset_ncpus == 0)
292*7c478bd9Sstevel@tonic-gate 		fatal("processor set %d empty\n", g_pset);
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_pset_ncpus; i++)
295*7c478bd9Sstevel@tonic-gate 		g_present[g_pset_cpus[i]] = 1;
296*7c478bd9Sstevel@tonic-gate }
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate static void
299*7c478bd9Sstevel@tonic-gate check_pset(void)
300*7c478bd9Sstevel@tonic-gate {
301*7c478bd9Sstevel@tonic-gate 	uint_t ncpus = g_max_cpus;
302*7c478bd9Sstevel@tonic-gate 	processorid_t i;
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate 	if (g_pset == PS_NONE)
305*7c478bd9Sstevel@tonic-gate 		return;
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate 	if (pset_info(g_pset, NULL, &ncpus, g_pset_cpus) == -1) {
308*7c478bd9Sstevel@tonic-gate 		if (errno == EINVAL)
309*7c478bd9Sstevel@tonic-gate 			fatal("processor set %d destroyed\n", g_pset);
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate 		fatal("couldn't get info for processor set %d", g_pset);
312*7c478bd9Sstevel@tonic-gate 	}
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate 	if (ncpus == 0)
315*7c478bd9Sstevel@tonic-gate 		fatal("processor set %d empty\n", g_pset);
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate 	if (ncpus == g_pset_ncpus) {
318*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < g_pset_ncpus; i++) {
319*7c478bd9Sstevel@tonic-gate 			if (!g_present[g_pset_cpus[i]])
320*7c478bd9Sstevel@tonic-gate 				break;
321*7c478bd9Sstevel@tonic-gate 		}
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 		/*
324*7c478bd9Sstevel@tonic-gate 		 * If the number of CPUs hasn't changed, and every CPU
325*7c478bd9Sstevel@tonic-gate 		 * in the processor set is also selected, we know that the
326*7c478bd9Sstevel@tonic-gate 		 * processor set itself hasn't changed.
327*7c478bd9Sstevel@tonic-gate 		 */
328*7c478bd9Sstevel@tonic-gate 		if (i == g_pset_ncpus)
329*7c478bd9Sstevel@tonic-gate 			return;
330*7c478bd9Sstevel@tonic-gate 	}
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate 	/*
333*7c478bd9Sstevel@tonic-gate 	 * If we're here, we have a new processor set.  First, we need
334*7c478bd9Sstevel@tonic-gate 	 * to zero out the present array.
335*7c478bd9Sstevel@tonic-gate 	 */
336*7c478bd9Sstevel@tonic-gate 	bzero(g_present, sizeof (processorid_t) * g_max_cpus);
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate 	g_pset_ncpus = ncpus;
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_pset_ncpus; i++)
341*7c478bd9Sstevel@tonic-gate 		g_present[g_pset_cpus[i]] = 1;
342*7c478bd9Sstevel@tonic-gate }
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate int
345*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
346*7c478bd9Sstevel@tonic-gate {
347*7c478bd9Sstevel@tonic-gate 	dtrace_prog_t *prog;
348*7c478bd9Sstevel@tonic-gate 	dtrace_proginfo_t info;
349*7c478bd9Sstevel@tonic-gate 	int err, i, indefinite = 1;
350*7c478bd9Sstevel@tonic-gate 	long iter;
351*7c478bd9Sstevel@tonic-gate 	processorid_t id;
352*7c478bd9Sstevel@tonic-gate 	struct sigaction act;
353*7c478bd9Sstevel@tonic-gate 	struct itimerspec ts;
354*7c478bd9Sstevel@tonic-gate 	struct sigevent ev;
355*7c478bd9Sstevel@tonic-gate 	sigset_t set;
356*7c478bd9Sstevel@tonic-gate 	timer_t tid;
357*7c478bd9Sstevel@tonic-gate 	char *end;
358*7c478bd9Sstevel@tonic-gate 	char c;
359*7c478bd9Sstevel@tonic-gate 	hrtime_t last, now;
360*7c478bd9Sstevel@tonic-gate 	dtrace_optval_t statustime;
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
363*7c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
364*7c478bd9Sstevel@tonic-gate 	act.sa_handler = set_width;
365*7c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGWINCH, &act, NULL);
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
368*7c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
369*7c478bd9Sstevel@tonic-gate 	act.sa_handler = intr;
370*7c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR1, &act, NULL);
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
373*7c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
374*7c478bd9Sstevel@tonic-gate 	act.sa_handler = status;
375*7c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR2, &act, NULL);
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate 	act.sa_handler = set_width;
378*7c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGWINCH, &act, NULL);
379*7c478bd9Sstevel@tonic-gate 	set_width();
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
382*7c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGUSR1);
383*7c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGWINCH);
384*7c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &set, NULL);
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate 	ev.sigev_notify = SIGEV_SIGNAL;
387*7c478bd9Sstevel@tonic-gate 	ev.sigev_signo = SIGUSR1;
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate 	if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1)
390*7c478bd9Sstevel@tonic-gate 		fatal("cannot create CLOCK_HIGHRES timer");
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 	g_max_cpus = sysconf(_SC_CPUID_MAX) + 1;
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate 	if ((g_present = malloc(sizeof (processorid_t) * g_max_cpus)) == NULL)
395*7c478bd9Sstevel@tonic-gate 		fatal("could not allocate g_present array\n");
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate 	bzero(g_present, sizeof (processorid_t) * g_max_cpus);
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate 	g_pset_cpus = malloc(sizeof (processorid_t) * g_max_cpus);
400*7c478bd9Sstevel@tonic-gate 	if (g_pset_cpus == NULL)
401*7c478bd9Sstevel@tonic-gate 		fatal("could not allocate g_pset_cpus");
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate 	bzero(g_pset_cpus, sizeof (processorid_t) * g_max_cpus);
404*7c478bd9Sstevel@tonic-gate 
405*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "c:C:")) != EOF) {
406*7c478bd9Sstevel@tonic-gate 		switch (c) {
407*7c478bd9Sstevel@tonic-gate 		case 'c': {
408*7c478bd9Sstevel@tonic-gate 			/*
409*7c478bd9Sstevel@tonic-gate 			 * We allow CPUs to be specified as an optionally
410*7c478bd9Sstevel@tonic-gate 			 * comma separated list of either CPU IDs or ranges
411*7c478bd9Sstevel@tonic-gate 			 * of CPU IDs.
412*7c478bd9Sstevel@tonic-gate 			 */
413*7c478bd9Sstevel@tonic-gate 			char *s = strtok(optarg, ",");
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 			while (s != NULL) {
416*7c478bd9Sstevel@tonic-gate 				id = strtoul(s, &end, 0);
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate 				if (id == ULONG_MAX && errno == ERANGE) {
419*7c478bd9Sstevel@tonic-gate 					*end = '\0';
420*7c478bd9Sstevel@tonic-gate 					fatal("invalid cpu '%s'\n", s);
421*7c478bd9Sstevel@tonic-gate 				}
422*7c478bd9Sstevel@tonic-gate 
423*7c478bd9Sstevel@tonic-gate 				if (*(s = end) != '\0') {
424*7c478bd9Sstevel@tonic-gate 					processorid_t p;
425*7c478bd9Sstevel@tonic-gate 
426*7c478bd9Sstevel@tonic-gate 					if (*s != '-')
427*7c478bd9Sstevel@tonic-gate 						fatal("invalid cpu '%s'\n", s);
428*7c478bd9Sstevel@tonic-gate 					p = strtoul(++s, &end, 0);
429*7c478bd9Sstevel@tonic-gate 
430*7c478bd9Sstevel@tonic-gate 					if (*end != '\0' ||
431*7c478bd9Sstevel@tonic-gate 					    (p == ULONG_MAX && errno == ERANGE))
432*7c478bd9Sstevel@tonic-gate 						fatal("invalid cpu '%s'\n", s);
433*7c478bd9Sstevel@tonic-gate 
434*7c478bd9Sstevel@tonic-gate 					select_cpus(id, p);
435*7c478bd9Sstevel@tonic-gate 				} else {
436*7c478bd9Sstevel@tonic-gate 					select_cpu(id);
437*7c478bd9Sstevel@tonic-gate 				}
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate 				s = strtok(NULL, ",");
440*7c478bd9Sstevel@tonic-gate 			}
441*7c478bd9Sstevel@tonic-gate 
442*7c478bd9Sstevel@tonic-gate 			break;
443*7c478bd9Sstevel@tonic-gate 		}
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate 		case 'C': {
446*7c478bd9Sstevel@tonic-gate 			psetid_t pset = strtoul(optarg, &end, 0);
447*7c478bd9Sstevel@tonic-gate 
448*7c478bd9Sstevel@tonic-gate 			if (*end != '\0' ||
449*7c478bd9Sstevel@tonic-gate 			    (pset == ULONG_MAX && errno == ERANGE))
450*7c478bd9Sstevel@tonic-gate 				fatal("invalid processor set '%s'\n", optarg);
451*7c478bd9Sstevel@tonic-gate 
452*7c478bd9Sstevel@tonic-gate 			select_pset(pset);
453*7c478bd9Sstevel@tonic-gate 			break;
454*7c478bd9Sstevel@tonic-gate 		}
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate 		default:
457*7c478bd9Sstevel@tonic-gate 			usage();
458*7c478bd9Sstevel@tonic-gate 		}
459*7c478bd9Sstevel@tonic-gate 	}
460*7c478bd9Sstevel@tonic-gate 
461*7c478bd9Sstevel@tonic-gate 	if (optind != argc) {
462*7c478bd9Sstevel@tonic-gate 		g_sleeptime = strtol(argv[optind], &end, 10);
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 		if (*end != NULL || g_sleeptime == 0)
465*7c478bd9Sstevel@tonic-gate 			fatal("invalid interval '%s'\n", argv[1]);
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate 		if (g_sleeptime <= 0)
468*7c478bd9Sstevel@tonic-gate 			fatal("interval must be greater than zero.\n");
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate 		if (g_sleeptime == LONG_MAX && errno == ERANGE)
471*7c478bd9Sstevel@tonic-gate 			fatal("invalid interval '%s'\n", argv[optind]);
472*7c478bd9Sstevel@tonic-gate 
473*7c478bd9Sstevel@tonic-gate 		if (++optind != argc) {
474*7c478bd9Sstevel@tonic-gate 			char *s = argv[optind];
475*7c478bd9Sstevel@tonic-gate 
476*7c478bd9Sstevel@tonic-gate 			iter = strtol(s, &end, 0);
477*7c478bd9Sstevel@tonic-gate 			indefinite = 0;
478*7c478bd9Sstevel@tonic-gate 
479*7c478bd9Sstevel@tonic-gate 			if (*end != '\0' || iter <= 0 ||
480*7c478bd9Sstevel@tonic-gate 			    (iter == LONG_MAX && errno == ERANGE))
481*7c478bd9Sstevel@tonic-gate 				fatal("invalid count '%s'\n", s);
482*7c478bd9Sstevel@tonic-gate 		}
483*7c478bd9Sstevel@tonic-gate 	}
484*7c478bd9Sstevel@tonic-gate 
485*7c478bd9Sstevel@tonic-gate 	ts.it_value.tv_sec = g_sleeptime;
486*7c478bd9Sstevel@tonic-gate 	ts.it_value.tv_nsec = 0;
487*7c478bd9Sstevel@tonic-gate 	ts.it_interval.tv_sec = g_sleeptime;
488*7c478bd9Sstevel@tonic-gate 	ts.it_interval.tv_nsec = 0;
489*7c478bd9Sstevel@tonic-gate 
490*7c478bd9Sstevel@tonic-gate 	if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1)
491*7c478bd9Sstevel@tonic-gate 		fatal("cannot set time on CLOCK_REALTIME timer");
492*7c478bd9Sstevel@tonic-gate 
493*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_max_cpus && !g_present[i]; i++)
494*7c478bd9Sstevel@tonic-gate 		continue;
495*7c478bd9Sstevel@tonic-gate 
496*7c478bd9Sstevel@tonic-gate 	if (i == g_max_cpus) {
497*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < g_max_cpus; i++)
498*7c478bd9Sstevel@tonic-gate 			g_present[i] = p_online(i, P_STATUS) == -1 ? 0 : 1;
499*7c478bd9Sstevel@tonic-gate 	}
500*7c478bd9Sstevel@tonic-gate 
501*7c478bd9Sstevel@tonic-gate 	if ((g_dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) {
502*7c478bd9Sstevel@tonic-gate 		fatal("cannot open dtrace library: %s\n",
503*7c478bd9Sstevel@tonic-gate 		    dtrace_errmsg(NULL, err));
504*7c478bd9Sstevel@tonic-gate 	}
505*7c478bd9Sstevel@tonic-gate 
506*7c478bd9Sstevel@tonic-gate 	if ((prog = dtrace_program_strcompile(g_dtp, g_prog,
507*7c478bd9Sstevel@tonic-gate 	    DTRACE_PROBESPEC_NAME, 0, 0, NULL)) == NULL)
508*7c478bd9Sstevel@tonic-gate 		fatal("invalid program");
509*7c478bd9Sstevel@tonic-gate 
510*7c478bd9Sstevel@tonic-gate 	if (dtrace_program_exec(g_dtp, prog, &info) == -1)
511*7c478bd9Sstevel@tonic-gate 		fatal("failed to enable probes");
512*7c478bd9Sstevel@tonic-gate 
513*7c478bd9Sstevel@tonic-gate 	if (dtrace_setopt(g_dtp, "aggsize", "128k") == -1)
514*7c478bd9Sstevel@tonic-gate 		fatal("failed to set 'aggsize'");
515*7c478bd9Sstevel@tonic-gate 
516*7c478bd9Sstevel@tonic-gate 	if (dtrace_setopt(g_dtp, "aggrate", "0") == -1)
517*7c478bd9Sstevel@tonic-gate 		fatal("failed to set 'aggrate'");
518*7c478bd9Sstevel@tonic-gate 
519*7c478bd9Sstevel@tonic-gate 	if (dtrace_setopt(g_dtp, "aggpercpu", 0) == -1)
520*7c478bd9Sstevel@tonic-gate 		fatal("failed to set 'aggpercpu'");
521*7c478bd9Sstevel@tonic-gate 
522*7c478bd9Sstevel@tonic-gate 	if (dtrace_go(g_dtp) != 0)
523*7c478bd9Sstevel@tonic-gate 		fatal("dtrace_go()");
524*7c478bd9Sstevel@tonic-gate 
525*7c478bd9Sstevel@tonic-gate 	last = gethrtime();
526*7c478bd9Sstevel@tonic-gate 
527*7c478bd9Sstevel@tonic-gate 	if (dtrace_getopt(g_dtp, "statusrate", &statustime) == -1)
528*7c478bd9Sstevel@tonic-gate 		fatal("failed to get 'statusrate'");
529*7c478bd9Sstevel@tonic-gate 
530*7c478bd9Sstevel@tonic-gate 	if (statustime < ((dtrace_optval_t)g_sleeptime * NANOSEC)) {
531*7c478bd9Sstevel@tonic-gate 		ev.sigev_notify = SIGEV_SIGNAL;
532*7c478bd9Sstevel@tonic-gate 		ev.sigev_signo = SIGUSR2;
533*7c478bd9Sstevel@tonic-gate 
534*7c478bd9Sstevel@tonic-gate 		if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1)
535*7c478bd9Sstevel@tonic-gate 			fatal("cannot create status timer");
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate 		ts.it_value.tv_sec = statustime / NANOSEC;
538*7c478bd9Sstevel@tonic-gate 		ts.it_value.tv_nsec = statustime % NANOSEC;
539*7c478bd9Sstevel@tonic-gate 		ts.it_interval = ts.it_value;
540*7c478bd9Sstevel@tonic-gate 
541*7c478bd9Sstevel@tonic-gate 		if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1)
542*7c478bd9Sstevel@tonic-gate 			fatal("cannot set time on status timer");
543*7c478bd9Sstevel@tonic-gate 	}
544*7c478bd9Sstevel@tonic-gate 
545*7c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
546*7c478bd9Sstevel@tonic-gate 
547*7c478bd9Sstevel@tonic-gate 	while (indefinite || iter) {
548*7c478bd9Sstevel@tonic-gate 
549*7c478bd9Sstevel@tonic-gate 		(void) sigsuspend(&set);
550*7c478bd9Sstevel@tonic-gate 
551*7c478bd9Sstevel@tonic-gate 		(void) dtrace_status(g_dtp);
552*7c478bd9Sstevel@tonic-gate 
553*7c478bd9Sstevel@tonic-gate 		if (g_intr == 0)
554*7c478bd9Sstevel@tonic-gate 			continue;
555*7c478bd9Sstevel@tonic-gate 
556*7c478bd9Sstevel@tonic-gate 		iter--;
557*7c478bd9Sstevel@tonic-gate 		g_intr--;
558*7c478bd9Sstevel@tonic-gate 		check_pset();
559*7c478bd9Sstevel@tonic-gate 
560*7c478bd9Sstevel@tonic-gate 		now = gethrtime();
561*7c478bd9Sstevel@tonic-gate 		g_interval = now - last;
562*7c478bd9Sstevel@tonic-gate 		last = now;
563*7c478bd9Sstevel@tonic-gate 
564*7c478bd9Sstevel@tonic-gate 		if (dtrace_aggregate_snap(g_dtp) != 0)
565*7c478bd9Sstevel@tonic-gate 			fatal("failed to add to aggregate");
566*7c478bd9Sstevel@tonic-gate 
567*7c478bd9Sstevel@tonic-gate 		g_start = g_end = 0;
568*7c478bd9Sstevel@tonic-gate 
569*7c478bd9Sstevel@tonic-gate 		do {
570*7c478bd9Sstevel@tonic-gate 			g_header = 1;
571*7c478bd9Sstevel@tonic-gate 
572*7c478bd9Sstevel@tonic-gate 			if (dtrace_aggregate_walk_keyvarsorted(g_dtp,
573*7c478bd9Sstevel@tonic-gate 			    walk, NULL) != 0)
574*7c478bd9Sstevel@tonic-gate 				fatal("failed to sort aggregate");
575*7c478bd9Sstevel@tonic-gate 
576*7c478bd9Sstevel@tonic-gate 			if (g_start == g_end)
577*7c478bd9Sstevel@tonic-gate 				break;
578*7c478bd9Sstevel@tonic-gate 		} while ((g_start = g_end) < g_max_cpus);
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate 		dtrace_aggregate_clear(g_dtp);
581*7c478bd9Sstevel@tonic-gate 	}
582*7c478bd9Sstevel@tonic-gate 
583*7c478bd9Sstevel@tonic-gate 	return (0);
584*7c478bd9Sstevel@tonic-gate }
585