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