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 52723Scth * Common Development and Distribution License (the "License"). 62723Scth * 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 /* 22*5461Stc35445 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include <sys/pset.h> 290Sstevel@tonic-gate #include <sys/types.h> 300Sstevel@tonic-gate #include <sys/time.h> 310Sstevel@tonic-gate #include <sys/sysinfo.h> 320Sstevel@tonic-gate 330Sstevel@tonic-gate #include <assert.h> 340Sstevel@tonic-gate #include <stdio.h> 350Sstevel@tonic-gate #include <stdlib.h> 360Sstevel@tonic-gate #include <stdarg.h> 370Sstevel@tonic-gate #include <ctype.h> 380Sstevel@tonic-gate #include <unistd.h> 390Sstevel@tonic-gate #include <memory.h> 400Sstevel@tonic-gate #include <string.h> 410Sstevel@tonic-gate #include <strings.h> 420Sstevel@tonic-gate #include <fcntl.h> 430Sstevel@tonic-gate #include <errno.h> 440Sstevel@tonic-gate #include <kstat.h> 450Sstevel@tonic-gate #include <poll.h> 46*5461Stc35445 #include <signal.h> 470Sstevel@tonic-gate 480Sstevel@tonic-gate #include "statcommon.h" 490Sstevel@tonic-gate 500Sstevel@tonic-gate #define SNAP(s, i, l, n) ((s) ? agg_proc_snap(s, i, l, n) : 0) 510Sstevel@tonic-gate 520Sstevel@tonic-gate #define REPRINT 20 530Sstevel@tonic-gate 54*5461Stc35445 char *cmdname = "mpstat"; 55*5461Stc35445 int caught_cont = 0; 560Sstevel@tonic-gate 570Sstevel@tonic-gate static int hz; 580Sstevel@tonic-gate static int display_pset = -1; 590Sstevel@tonic-gate static int show_set = 0; 600Sstevel@tonic-gate static int suppress_state; 610Sstevel@tonic-gate 620Sstevel@tonic-gate static void print_header(int, int); 630Sstevel@tonic-gate static void show_cpu_usage(struct snapshot *, struct snapshot *, int); 640Sstevel@tonic-gate static void usage(void); 650Sstevel@tonic-gate 660Sstevel@tonic-gate int 670Sstevel@tonic-gate main(int argc, char **argv) 680Sstevel@tonic-gate { 690Sstevel@tonic-gate int c; 700Sstevel@tonic-gate int display_agg = 0; 710Sstevel@tonic-gate int iter = 1; 720Sstevel@tonic-gate int interval = 0; 730Sstevel@tonic-gate char *endptr; 740Sstevel@tonic-gate int infinite_cycles = 0; 750Sstevel@tonic-gate kstat_ctl_t *kc; 760Sstevel@tonic-gate struct snapshot *old = NULL; 770Sstevel@tonic-gate struct snapshot *new = NULL; 780Sstevel@tonic-gate enum snapshot_types types = SNAP_CPUS; 79*5461Stc35445 hrtime_t start_n; 80*5461Stc35445 hrtime_t period_n; 810Sstevel@tonic-gate 820Sstevel@tonic-gate while ((c = getopt(argc, argv, "apP:q")) != (int)EOF) 830Sstevel@tonic-gate switch (c) { 840Sstevel@tonic-gate case 'a': 850Sstevel@tonic-gate /* 860Sstevel@tonic-gate * Display aggregate data for processor sets. 870Sstevel@tonic-gate */ 880Sstevel@tonic-gate display_agg = 1; 890Sstevel@tonic-gate break; 900Sstevel@tonic-gate case 'p': 910Sstevel@tonic-gate /* 920Sstevel@tonic-gate * Display all processor sets. 930Sstevel@tonic-gate */ 940Sstevel@tonic-gate if (display_pset != -1) 950Sstevel@tonic-gate usage(); 960Sstevel@tonic-gate show_set = 1; 970Sstevel@tonic-gate break; 980Sstevel@tonic-gate case 'P': 990Sstevel@tonic-gate /* 1000Sstevel@tonic-gate * Display specific processor set. 1010Sstevel@tonic-gate */ 1020Sstevel@tonic-gate if (show_set == 1) 1030Sstevel@tonic-gate usage(); 1040Sstevel@tonic-gate display_pset = (int)strtol 1050Sstevel@tonic-gate (optarg, &endptr, 10); 1060Sstevel@tonic-gate if (*endptr != NULL) 1070Sstevel@tonic-gate usage(); 1080Sstevel@tonic-gate /* 1090Sstevel@tonic-gate * Not valid to specify a negative processor 1100Sstevel@tonic-gate * set value. 1110Sstevel@tonic-gate */ 1120Sstevel@tonic-gate if (display_pset < 0) 1130Sstevel@tonic-gate usage(); 1140Sstevel@tonic-gate break; 1150Sstevel@tonic-gate case 'q': 1160Sstevel@tonic-gate suppress_state = 1; 1170Sstevel@tonic-gate break; 1180Sstevel@tonic-gate case '?': 1190Sstevel@tonic-gate usage(); 1200Sstevel@tonic-gate break; 1210Sstevel@tonic-gate } 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate hz = sysconf(_SC_CLK_TCK); 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate if (argc > optind) { 1260Sstevel@tonic-gate interval = (int)strtol(argv[optind], &endptr, 10); 1270Sstevel@tonic-gate if (*endptr != NULL) 1280Sstevel@tonic-gate usage(); 129*5461Stc35445 period_n = (hrtime_t)interval * NANOSEC; 1300Sstevel@tonic-gate if (argc > optind + 1) { 1310Sstevel@tonic-gate iter = (unsigned int)strtoul 1320Sstevel@tonic-gate (argv[optind + 1], &endptr, 10); 1330Sstevel@tonic-gate if (*endptr != NULL || iter < 0) 1340Sstevel@tonic-gate usage(); 1350Sstevel@tonic-gate if (iter == 0) 1360Sstevel@tonic-gate return (0); 1370Sstevel@tonic-gate } else { 1380Sstevel@tonic-gate infinite_cycles = 1; 1390Sstevel@tonic-gate } 1400Sstevel@tonic-gate } 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate if (display_agg || show_set || display_pset != -1) 1430Sstevel@tonic-gate types |= SNAP_PSETS; 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate kc = open_kstat(); 1460Sstevel@tonic-gate 147*5461Stc35445 /* Set up handler for SIGCONT */ 148*5461Stc35445 if (signal(SIGCONT, cont_handler) == SIG_ERR) 149*5461Stc35445 fail(1, "signal failed"); 150*5461Stc35445 151*5461Stc35445 start_n = gethrtime(); 152*5461Stc35445 1530Sstevel@tonic-gate while (infinite_cycles || iter > 0) { 1540Sstevel@tonic-gate free_snapshot(old); 1550Sstevel@tonic-gate old = new; 1560Sstevel@tonic-gate new = acquire_snapshot(kc, types, NULL); 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate if (!suppress_state) 1590Sstevel@tonic-gate snapshot_report_changes(old, new); 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate /* if config changed, show stats from boot */ 1620Sstevel@tonic-gate if (snapshot_has_changed(old, new)) { 1630Sstevel@tonic-gate free_snapshot(old); 1640Sstevel@tonic-gate old = NULL; 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate show_cpu_usage(old, new, display_agg); 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate if (!infinite_cycles && --iter < 1) 1700Sstevel@tonic-gate break; 1710Sstevel@tonic-gate 172*5461Stc35445 /* Have a kip */ 173*5461Stc35445 sleep_until(&start_n, period_n, infinite_cycles, &caught_cont); 1740Sstevel@tonic-gate } 1752723Scth (void) kstat_close(kc); 1762723Scth 1770Sstevel@tonic-gate return (0); 1780Sstevel@tonic-gate } 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate /* 1810Sstevel@tonic-gate * Print an mpstat output header. 1820Sstevel@tonic-gate */ 1830Sstevel@tonic-gate static void 1840Sstevel@tonic-gate print_header(int display_agg, int show_set) 1850Sstevel@tonic-gate { 1860Sstevel@tonic-gate if (display_agg == 1) 1870Sstevel@tonic-gate (void) printf("SET minf mjf xcal intr ithr csw icsw migr " 1880Sstevel@tonic-gate "smtx srw syscl usr sys wt idl sze"); 1890Sstevel@tonic-gate else { 1900Sstevel@tonic-gate (void) printf("CPU minf mjf xcal intr ithr csw icsw migr " 1910Sstevel@tonic-gate "smtx srw syscl usr sys wt idl"); 1920Sstevel@tonic-gate if (show_set == 1) 1930Sstevel@tonic-gate (void) printf(" set"); 1940Sstevel@tonic-gate } 1950Sstevel@tonic-gate (void) printf("\n"); 1960Sstevel@tonic-gate } 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate static void 1990Sstevel@tonic-gate print_cpu(struct cpu_snapshot *c1, struct cpu_snapshot *c2) 2000Sstevel@tonic-gate { 2010Sstevel@tonic-gate uint64_t ticks = 0; 2020Sstevel@tonic-gate double etime, percent; 2030Sstevel@tonic-gate kstat_t *old_vm = NULL; 2040Sstevel@tonic-gate kstat_t *old_sys = NULL; 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate if (display_pset != -1 && display_pset != c2->cs_pset_id) 2070Sstevel@tonic-gate return; 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate /* 2100Sstevel@tonic-gate * the first mpstat output will have c1 = NULL, to give 2110Sstevel@tonic-gate * results since boot 2120Sstevel@tonic-gate */ 2130Sstevel@tonic-gate if (c1) { 2140Sstevel@tonic-gate old_vm = &c1->cs_vm; 2150Sstevel@tonic-gate old_sys = &c1->cs_sys; 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate /* check there are stats to report */ 2180Sstevel@tonic-gate if (!CPU_ACTIVE(c1)) 2190Sstevel@tonic-gate return; 2200Sstevel@tonic-gate } 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate /* check there are stats to report */ 2230Sstevel@tonic-gate if (!CPU_ACTIVE(c2)) 2240Sstevel@tonic-gate return; 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate ticks = cpu_ticks_delta(old_sys, &c2->cs_sys); 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate etime = (double)ticks / hz; 2290Sstevel@tonic-gate if (etime == 0.0) /* Prevent divide by zero errors */ 2300Sstevel@tonic-gate etime = 1.0; 2310Sstevel@tonic-gate percent = 100.0 / etime / hz; 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate (void) printf("%3d %4.0f %3.0f %4.0f %5.0f %4.0f " 2340Sstevel@tonic-gate "%4.0f %4.0f %4.0f %4.0f %4.0f %5.0f %3.0f %3.0f " 2350Sstevel@tonic-gate "%3.0f %3.0f", 2360Sstevel@tonic-gate c2->cs_id, 2370Sstevel@tonic-gate (kstat_delta(old_vm, &c2->cs_vm, "hat_fault") + 2380Sstevel@tonic-gate kstat_delta(old_vm, &c2->cs_vm, "as_fault")) / etime, 2390Sstevel@tonic-gate kstat_delta(old_vm, &c2->cs_vm, "maj_fault") / etime, 2400Sstevel@tonic-gate kstat_delta(old_sys, &c2->cs_sys, "xcalls") / etime, 2410Sstevel@tonic-gate kstat_delta(old_sys, &c2->cs_sys, "intr") / etime, 2420Sstevel@tonic-gate kstat_delta(old_sys, &c2->cs_sys, "intrthread") / etime, 2430Sstevel@tonic-gate kstat_delta(old_sys, &c2->cs_sys, "pswitch") / etime, 2440Sstevel@tonic-gate kstat_delta(old_sys, &c2->cs_sys, "inv_swtch") / etime, 2450Sstevel@tonic-gate kstat_delta(old_sys, &c2->cs_sys, "cpumigrate") / etime, 2460Sstevel@tonic-gate kstat_delta(old_sys, &c2->cs_sys, "mutex_adenters") / etime, 2470Sstevel@tonic-gate (kstat_delta(old_sys, &c2->cs_sys, "rw_rdfails") + 2480Sstevel@tonic-gate kstat_delta(old_sys, &c2->cs_sys, "rw_wrfails")) / etime, 2490Sstevel@tonic-gate kstat_delta(old_sys, &c2->cs_sys, "syscall") / etime, 2500Sstevel@tonic-gate kstat_delta(old_sys, &c2->cs_sys, "cpu_ticks_user") * percent, 2510Sstevel@tonic-gate kstat_delta(old_sys, &c2->cs_sys, "cpu_ticks_kernel") * percent, 2520Sstevel@tonic-gate kstat_delta(old_sys, &c2->cs_sys, "cpu_ticks_wait") * percent, 2530Sstevel@tonic-gate kstat_delta(old_sys, &c2->cs_sys, "cpu_ticks_idle") * percent); 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate if (show_set) 2560Sstevel@tonic-gate (void) printf(" %3d", c2->cs_pset_id); 2570Sstevel@tonic-gate (void) printf("\n"); 2580Sstevel@tonic-gate } 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate /*ARGSUSED*/ 2610Sstevel@tonic-gate static void 2620Sstevel@tonic-gate compare_cpu(void *v1, void *v2, void *data) 2630Sstevel@tonic-gate { 2640Sstevel@tonic-gate struct cpu_snapshot *c1 = (struct cpu_snapshot *)v1; 2650Sstevel@tonic-gate struct cpu_snapshot *c2 = (struct cpu_snapshot *)v2; 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate if (c2 == NULL) 2680Sstevel@tonic-gate return; 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate print_cpu(c1, c2); 2710Sstevel@tonic-gate } 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate static int 2740Sstevel@tonic-gate pset_has_stats(struct pset_snapshot *p) 2750Sstevel@tonic-gate { 2760Sstevel@tonic-gate int count = 0; 2770Sstevel@tonic-gate size_t i; 2780Sstevel@tonic-gate for (i = 0; i < p->ps_nr_cpus; i++) { 2790Sstevel@tonic-gate if (CPU_ACTIVE(p->ps_cpus[i])) 2800Sstevel@tonic-gate count++; 2810Sstevel@tonic-gate } 2820Sstevel@tonic-gate return (count); 2830Sstevel@tonic-gate } 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate static void 2860Sstevel@tonic-gate agg_stat(kstat_t *k1, kstat_t *k2, char *name) 2870Sstevel@tonic-gate { 2880Sstevel@tonic-gate kstat_named_t *ksn = kstat_data_lookup(k1, name); 2890Sstevel@tonic-gate kstat_named_t *ksn2 = kstat_data_lookup(k2, name); 2900Sstevel@tonic-gate ksn->value.ui64 += ksn2->value.ui64; 2910Sstevel@tonic-gate } 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate static kstat_t * 2940Sstevel@tonic-gate agg_vm(struct pset_snapshot *p, kstat_t *ks) 2950Sstevel@tonic-gate { 2960Sstevel@tonic-gate size_t i; 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate if (p->ps_nr_cpus == NULL) 2990Sstevel@tonic-gate return (NULL); 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate if (kstat_copy(&p->ps_cpus[0]->cs_vm, ks)) 3020Sstevel@tonic-gate return (NULL); 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate for (i = 1; i < p->ps_nr_cpus; i++) { 3050Sstevel@tonic-gate agg_stat(ks, &p->ps_cpus[i]->cs_vm, "hat_fault"); 3060Sstevel@tonic-gate agg_stat(ks, &p->ps_cpus[i]->cs_vm, "as_fault"); 3070Sstevel@tonic-gate agg_stat(ks, &p->ps_cpus[i]->cs_vm, "maj_fault"); 3080Sstevel@tonic-gate } 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate return (ks); 3110Sstevel@tonic-gate } 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate static kstat_t * 3140Sstevel@tonic-gate agg_sys(struct pset_snapshot *p, kstat_t *ks) 3150Sstevel@tonic-gate { 3160Sstevel@tonic-gate size_t i; 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate if (p->ps_nr_cpus == NULL) 3190Sstevel@tonic-gate return (NULL); 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate if (kstat_copy(&p->ps_cpus[0]->cs_sys, ks)) 3220Sstevel@tonic-gate return (NULL); 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate for (i = 1; i < p->ps_nr_cpus; i++) { 3250Sstevel@tonic-gate agg_stat(ks, &p->ps_cpus[i]->cs_sys, "xcalls"); 3260Sstevel@tonic-gate agg_stat(ks, &p->ps_cpus[i]->cs_sys, "intr"); 3270Sstevel@tonic-gate agg_stat(ks, &p->ps_cpus[i]->cs_sys, "intrthread"); 3280Sstevel@tonic-gate agg_stat(ks, &p->ps_cpus[i]->cs_sys, "pswitch"); 3290Sstevel@tonic-gate agg_stat(ks, &p->ps_cpus[i]->cs_sys, "inv_swtch"); 3300Sstevel@tonic-gate agg_stat(ks, &p->ps_cpus[i]->cs_sys, "cpumigrate"); 3310Sstevel@tonic-gate agg_stat(ks, &p->ps_cpus[i]->cs_sys, "mutex_adenters"); 3320Sstevel@tonic-gate agg_stat(ks, &p->ps_cpus[i]->cs_sys, "rw_rdfails"); 3330Sstevel@tonic-gate agg_stat(ks, &p->ps_cpus[i]->cs_sys, "rw_wrfails"); 3340Sstevel@tonic-gate agg_stat(ks, &p->ps_cpus[i]->cs_sys, "syscall"); 3350Sstevel@tonic-gate agg_stat(ks, &p->ps_cpus[i]->cs_sys, "cpu_ticks_user"); 3360Sstevel@tonic-gate agg_stat(ks, &p->ps_cpus[i]->cs_sys, "cpu_ticks_kernel"); 3370Sstevel@tonic-gate agg_stat(ks, &p->ps_cpus[i]->cs_sys, "cpu_ticks_wait"); 3380Sstevel@tonic-gate agg_stat(ks, &p->ps_cpus[i]->cs_sys, "cpu_ticks_idle"); 3390Sstevel@tonic-gate } 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate return (ks); 3420Sstevel@tonic-gate } 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate static uint64_t 3450Sstevel@tonic-gate get_nr_ticks(struct pset_snapshot *p1, struct pset_snapshot *p2) 3460Sstevel@tonic-gate { 3470Sstevel@tonic-gate kstat_t *old = NULL; 3480Sstevel@tonic-gate kstat_t *new = NULL; 3490Sstevel@tonic-gate size_t i = 0; 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate for (i = 0; p1 && i < p1->ps_nr_cpus; i++) { 3520Sstevel@tonic-gate if (p1->ps_cpus[i]->cs_sys.ks_data) { 3530Sstevel@tonic-gate old = &p1->ps_cpus[i]->cs_sys; 3540Sstevel@tonic-gate break; 3550Sstevel@tonic-gate } 3560Sstevel@tonic-gate } 3570Sstevel@tonic-gate 3580Sstevel@tonic-gate for (i = 0; p2 && i < p2->ps_nr_cpus; i++) { 3590Sstevel@tonic-gate if (p2->ps_cpus[i]->cs_sys.ks_data) { 3600Sstevel@tonic-gate new = &p2->ps_cpus[i]->cs_sys; 3610Sstevel@tonic-gate break; 3620Sstevel@tonic-gate } 3630Sstevel@tonic-gate } 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate if (old == NULL && new == NULL) 3660Sstevel@tonic-gate return (0); 3670Sstevel@tonic-gate 3680Sstevel@tonic-gate if (new == NULL) { 3690Sstevel@tonic-gate new = old; 3700Sstevel@tonic-gate old = NULL; 3710Sstevel@tonic-gate } 3720Sstevel@tonic-gate 3730Sstevel@tonic-gate return (cpu_ticks_delta(old, new)); 3740Sstevel@tonic-gate } 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate static void 3770Sstevel@tonic-gate print_pset(struct pset_snapshot *p1, struct pset_snapshot *p2) 3780Sstevel@tonic-gate { 3790Sstevel@tonic-gate uint64_t ticks = 0; 3800Sstevel@tonic-gate double etime, percent; 3810Sstevel@tonic-gate kstat_t old_vm; 3820Sstevel@tonic-gate kstat_t old_sys; 3830Sstevel@tonic-gate kstat_t new_vm; 3840Sstevel@tonic-gate kstat_t new_sys; 3850Sstevel@tonic-gate 3860Sstevel@tonic-gate if (display_pset != -1 && display_pset != p2->ps_id) 3870Sstevel@tonic-gate return; 3880Sstevel@tonic-gate 3890Sstevel@tonic-gate if ((p1 && !pset_has_stats(p1)) || !pset_has_stats(p2)) 3900Sstevel@tonic-gate return; 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate old_vm.ks_data = old_sys.ks_data = NULL; 3930Sstevel@tonic-gate new_vm.ks_data = new_sys.ks_data = NULL; 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate /* 3960Sstevel@tonic-gate * FIXME: these aggs will count "new" or disappeared cpus 3970Sstevel@tonic-gate * in a set, leaving an apparent huge change. 3980Sstevel@tonic-gate */ 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate /* 4010Sstevel@tonic-gate * the first mpstat output will have p1 = NULL, to give 4020Sstevel@tonic-gate * results since boot 4030Sstevel@tonic-gate */ 4040Sstevel@tonic-gate if (p1) { 4050Sstevel@tonic-gate if (!agg_vm(p1, &old_vm) || !agg_sys(p1, &old_sys)) 4060Sstevel@tonic-gate goto out; 4070Sstevel@tonic-gate } 4080Sstevel@tonic-gate 4090Sstevel@tonic-gate if (!agg_vm(p2, &new_vm) || !agg_sys(p2, &new_sys)) 4100Sstevel@tonic-gate goto out; 4110Sstevel@tonic-gate 4120Sstevel@tonic-gate ticks = get_nr_ticks(p1, p2); 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate etime = (double)ticks / hz; 4150Sstevel@tonic-gate if (etime == 0.0) /* Prevent divide by zero errors */ 4160Sstevel@tonic-gate etime = 1.0; 4170Sstevel@tonic-gate percent = 100.0 / p2->ps_nr_cpus / etime / hz; 4180Sstevel@tonic-gate 4190Sstevel@tonic-gate (void) printf("%3d %4.0f %3.0f %4.0f %5.0f %4.0f " 4200Sstevel@tonic-gate "%4.0f %4.0f %4.0f %4.0f %4.0f %5.0f %3.0f %3.0f " 4210Sstevel@tonic-gate "%3.0f %3.0f %3d\n", 4220Sstevel@tonic-gate p2->ps_id, 4230Sstevel@tonic-gate (kstat_delta(&old_vm, &new_vm, "hat_fault") + 4240Sstevel@tonic-gate kstat_delta(&old_vm, &new_vm, "as_fault")) / etime, 4250Sstevel@tonic-gate kstat_delta(&old_vm, &new_vm, "maj_fault") / etime, 4260Sstevel@tonic-gate kstat_delta(&old_sys, &new_sys, "xcalls") / etime, 4270Sstevel@tonic-gate kstat_delta(&old_sys, &new_sys, "intr") / etime, 4280Sstevel@tonic-gate kstat_delta(&old_sys, &new_sys, "intrthread") / etime, 4290Sstevel@tonic-gate kstat_delta(&old_sys, &new_sys, "pswitch") / etime, 4300Sstevel@tonic-gate kstat_delta(&old_sys, &new_sys, "inv_swtch") / etime, 4310Sstevel@tonic-gate kstat_delta(&old_sys, &new_sys, "cpumigrate") / etime, 4320Sstevel@tonic-gate kstat_delta(&old_sys, &new_sys, "mutex_adenters") / etime, 4330Sstevel@tonic-gate (kstat_delta(&old_sys, &new_sys, "rw_rdfails") + 4340Sstevel@tonic-gate kstat_delta(&old_sys, &new_sys, "rw_wrfails")) / etime, 4350Sstevel@tonic-gate kstat_delta(&old_sys, &new_sys, "syscall") / etime, 4360Sstevel@tonic-gate kstat_delta(&old_sys, &new_sys, "cpu_ticks_user") * percent, 4370Sstevel@tonic-gate kstat_delta(&old_sys, &new_sys, "cpu_ticks_kernel") * percent, 4380Sstevel@tonic-gate kstat_delta(&old_sys, &new_sys, "cpu_ticks_wait") * percent, 4390Sstevel@tonic-gate kstat_delta(&old_sys, &new_sys, "cpu_ticks_idle") * percent, 4400Sstevel@tonic-gate p2->ps_nr_cpus); 4410Sstevel@tonic-gate 4420Sstevel@tonic-gate out: 4430Sstevel@tonic-gate free(old_vm.ks_data); 4440Sstevel@tonic-gate free(old_sys.ks_data); 4450Sstevel@tonic-gate free(new_vm.ks_data); 4460Sstevel@tonic-gate free(new_sys.ks_data); 4470Sstevel@tonic-gate } 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate /*ARGSUSED*/ 4500Sstevel@tonic-gate static void 4510Sstevel@tonic-gate compare_pset(void *v1, void *v2, void *data) 4520Sstevel@tonic-gate { 4530Sstevel@tonic-gate struct pset_snapshot *p1 = (struct pset_snapshot *)v1; 4540Sstevel@tonic-gate struct pset_snapshot *p2 = (struct pset_snapshot *)v2; 4550Sstevel@tonic-gate 4560Sstevel@tonic-gate if (p2 == NULL) 4570Sstevel@tonic-gate return; 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate print_pset(p1, p2); 4600Sstevel@tonic-gate } 4610Sstevel@tonic-gate 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate /* 4640Sstevel@tonic-gate * Report statistics for a sample interval. 4650Sstevel@tonic-gate */ 4660Sstevel@tonic-gate static void 4670Sstevel@tonic-gate show_cpu_usage(struct snapshot *old, struct snapshot *new, int display_agg) 4680Sstevel@tonic-gate { 4690Sstevel@tonic-gate static int lines_until_reprint = 0; 4700Sstevel@tonic-gate enum snapshot_types type = SNAP_CPUS; 4710Sstevel@tonic-gate snapshot_cb cb = compare_cpu; 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate if (lines_until_reprint == 0 || nr_active_cpus(new) > 1) { 4740Sstevel@tonic-gate print_header(display_agg, show_set); 4750Sstevel@tonic-gate lines_until_reprint = REPRINT; 4760Sstevel@tonic-gate } 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate lines_until_reprint--; 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate if (display_agg) { 4810Sstevel@tonic-gate type = SNAP_PSETS; 4820Sstevel@tonic-gate cb = compare_pset; 4830Sstevel@tonic-gate } 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate /* print stats since boot the first time round */ 4860Sstevel@tonic-gate (void) snapshot_walk(type, old, new, cb, NULL); 4870Sstevel@tonic-gate (void) fflush(stdout); 4880Sstevel@tonic-gate } 4890Sstevel@tonic-gate 4900Sstevel@tonic-gate /* 4910Sstevel@tonic-gate * Usage message on error. 4920Sstevel@tonic-gate */ 4930Sstevel@tonic-gate static void 4940Sstevel@tonic-gate usage(void) 4950Sstevel@tonic-gate { 4960Sstevel@tonic-gate (void) fprintf(stderr, 4970Sstevel@tonic-gate "Usage: mpstat [-aq] [-p | -P processor_set] [interval [count]]\n"); 4980Sstevel@tonic-gate exit(1); 4990Sstevel@tonic-gate } 500