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