xref: /onnv-gate/usr/src/cmd/stat/mpstat/mpstat.c (revision 10265:b988146c84e5)
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 /*
229123Sjohn.levon@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 <sys/pset.h>
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/time.h>
290Sstevel@tonic-gate #include <sys/sysinfo.h>
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <assert.h>
320Sstevel@tonic-gate #include <stdio.h>
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <stdarg.h>
350Sstevel@tonic-gate #include <ctype.h>
360Sstevel@tonic-gate #include <unistd.h>
370Sstevel@tonic-gate #include <memory.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate #include <strings.h>
400Sstevel@tonic-gate #include <fcntl.h>
410Sstevel@tonic-gate #include <errno.h>
420Sstevel@tonic-gate #include <kstat.h>
430Sstevel@tonic-gate #include <poll.h>
445461Stc35445 #include <signal.h>
459123Sjohn.levon@sun.com #include <locale.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include "statcommon.h"
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #define	SNAP(s, i, l, n)	((s) ? agg_proc_snap(s, i, l, n) : 0)
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #define	REPRINT		20
520Sstevel@tonic-gate 
535461Stc35445 char *cmdname = "mpstat";
545461Stc35445 int caught_cont = 0;
550Sstevel@tonic-gate 
56*10265SKrishnendu.Sadhukhan@Sun.COM static uint_t timestamp_fmt = NODATE;
579123Sjohn.levon@sun.com 
580Sstevel@tonic-gate static int hz;
590Sstevel@tonic-gate static int display_pset = -1;
600Sstevel@tonic-gate static int show_set = 0;
610Sstevel@tonic-gate static int suppress_state;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate static void print_header(int, int);
640Sstevel@tonic-gate static void show_cpu_usage(struct snapshot *, struct snapshot *, int);
650Sstevel@tonic-gate static void usage(void);
660Sstevel@tonic-gate 
670Sstevel@tonic-gate int
main(int argc,char ** argv)680Sstevel@tonic-gate main(int argc, char **argv)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate 	int c;
710Sstevel@tonic-gate 	int display_agg = 0;
720Sstevel@tonic-gate 	int iter = 1;
730Sstevel@tonic-gate 	int interval = 0;
740Sstevel@tonic-gate 	char *endptr;
750Sstevel@tonic-gate 	int infinite_cycles = 0;
760Sstevel@tonic-gate 	kstat_ctl_t *kc;
770Sstevel@tonic-gate 	struct snapshot *old = NULL;
780Sstevel@tonic-gate 	struct snapshot *new = NULL;
790Sstevel@tonic-gate 	enum snapshot_types types = SNAP_CPUS;
805461Stc35445 	hrtime_t start_n;
815461Stc35445 	hrtime_t period_n;
820Sstevel@tonic-gate 
839123Sjohn.levon@sun.com 	(void) setlocale(LC_ALL, "");
849123Sjohn.levon@sun.com #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
859123Sjohn.levon@sun.com #define	TEXT_DOMAIN "SYS_TEST"		/* Use this only if it weren't */
869123Sjohn.levon@sun.com #endif
879123Sjohn.levon@sun.com 	(void) textdomain(TEXT_DOMAIN);
889123Sjohn.levon@sun.com 
899123Sjohn.levon@sun.com 	while ((c = getopt(argc, argv, "apP:qT:")) != (int)EOF)
900Sstevel@tonic-gate 		switch (c) {
910Sstevel@tonic-gate 			case 'a':
920Sstevel@tonic-gate 				/*
930Sstevel@tonic-gate 				 * Display aggregate data for processor sets.
940Sstevel@tonic-gate 				 */
950Sstevel@tonic-gate 				display_agg = 1;
960Sstevel@tonic-gate 				break;
970Sstevel@tonic-gate 			case 'p':
980Sstevel@tonic-gate 				/*
990Sstevel@tonic-gate 				 * Display all processor sets.
1000Sstevel@tonic-gate 				 */
1010Sstevel@tonic-gate 				if (display_pset != -1)
1020Sstevel@tonic-gate 					usage();
1030Sstevel@tonic-gate 				show_set = 1;
1040Sstevel@tonic-gate 				break;
1050Sstevel@tonic-gate 			case 'P':
1060Sstevel@tonic-gate 				/*
1070Sstevel@tonic-gate 				 * Display specific processor set.
1080Sstevel@tonic-gate 				 */
1090Sstevel@tonic-gate 				if (show_set == 1)
1100Sstevel@tonic-gate 					usage();
1110Sstevel@tonic-gate 				display_pset = (int)strtol
1120Sstevel@tonic-gate 				    (optarg, &endptr, 10);
1130Sstevel@tonic-gate 				if (*endptr != NULL)
1140Sstevel@tonic-gate 					usage();
1150Sstevel@tonic-gate 				/*
1160Sstevel@tonic-gate 				 * Not valid to specify a negative processor
1170Sstevel@tonic-gate 				 * set value.
1180Sstevel@tonic-gate 				 */
1190Sstevel@tonic-gate 				if (display_pset < 0)
1200Sstevel@tonic-gate 					usage();
1210Sstevel@tonic-gate 				break;
1220Sstevel@tonic-gate 			case 'q':
1230Sstevel@tonic-gate 				suppress_state = 1;
1240Sstevel@tonic-gate 				break;
1259123Sjohn.levon@sun.com 			case 'T':
1269123Sjohn.levon@sun.com 				if (optarg) {
1279123Sjohn.levon@sun.com 					if (*optarg == 'u')
1289123Sjohn.levon@sun.com 						timestamp_fmt = UDATE;
1299123Sjohn.levon@sun.com 					else if (*optarg == 'd')
1309123Sjohn.levon@sun.com 						timestamp_fmt = DDATE;
1319123Sjohn.levon@sun.com 					else
1329123Sjohn.levon@sun.com 						usage();
1339123Sjohn.levon@sun.com 				} else {
1349123Sjohn.levon@sun.com 					usage();
1359123Sjohn.levon@sun.com 				}
1369123Sjohn.levon@sun.com 				break;
1370Sstevel@tonic-gate 			case '?':
1380Sstevel@tonic-gate 				usage();
1390Sstevel@tonic-gate 				break;
1400Sstevel@tonic-gate 		}
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	hz = sysconf(_SC_CLK_TCK);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (argc > optind) {
1450Sstevel@tonic-gate 		interval = (int)strtol(argv[optind], &endptr, 10);
1460Sstevel@tonic-gate 		if (*endptr != NULL)
1470Sstevel@tonic-gate 			usage();
1485461Stc35445 		period_n = (hrtime_t)interval * NANOSEC;
1490Sstevel@tonic-gate 		if (argc > optind + 1) {
1500Sstevel@tonic-gate 			iter = (unsigned int)strtoul
1510Sstevel@tonic-gate 			    (argv[optind + 1], &endptr, 10);
1520Sstevel@tonic-gate 			if (*endptr != NULL || iter < 0)
1530Sstevel@tonic-gate 				usage();
1540Sstevel@tonic-gate 			if (iter == 0)
1550Sstevel@tonic-gate 				return (0);
1560Sstevel@tonic-gate 		} else {
1570Sstevel@tonic-gate 			infinite_cycles = 1;
1580Sstevel@tonic-gate 		}
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	if (display_agg || show_set || display_pset != -1)
1620Sstevel@tonic-gate 		types |= SNAP_PSETS;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	kc = open_kstat();
1650Sstevel@tonic-gate 
1665461Stc35445 	/* Set up handler for SIGCONT */
1675461Stc35445 	if (signal(SIGCONT, cont_handler) == SIG_ERR)
1685461Stc35445 		fail(1, "signal failed");
1695461Stc35445 
1705461Stc35445 	start_n = gethrtime();
1715461Stc35445 
1720Sstevel@tonic-gate 	while (infinite_cycles || iter > 0) {
1730Sstevel@tonic-gate 		free_snapshot(old);
1740Sstevel@tonic-gate 		old = new;
1750Sstevel@tonic-gate 		new = acquire_snapshot(kc, types, NULL);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 		if (!suppress_state)
1780Sstevel@tonic-gate 			snapshot_report_changes(old, new);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 		/* if config changed, show stats from boot */
1810Sstevel@tonic-gate 		if (snapshot_has_changed(old, new)) {
1820Sstevel@tonic-gate 			free_snapshot(old);
1830Sstevel@tonic-gate 			old = NULL;
1840Sstevel@tonic-gate 		}
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 		show_cpu_usage(old, new, display_agg);
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 		if (!infinite_cycles && --iter < 1)
1890Sstevel@tonic-gate 			break;
1900Sstevel@tonic-gate 
1915461Stc35445 		/* Have a kip */
1925461Stc35445 		sleep_until(&start_n, period_n, infinite_cycles, &caught_cont);
1930Sstevel@tonic-gate 	}
1942723Scth 	(void) kstat_close(kc);
1952723Scth 
1960Sstevel@tonic-gate 	return (0);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate /*
2000Sstevel@tonic-gate  * Print an mpstat output header.
2010Sstevel@tonic-gate  */
2020Sstevel@tonic-gate static void
print_header(int display_agg,int show_set)2030Sstevel@tonic-gate print_header(int display_agg, int show_set)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate 	if (display_agg == 1)
2060Sstevel@tonic-gate 		(void) printf("SET minf mjf xcal  intr ithr  csw icsw migr "
2070Sstevel@tonic-gate 		    "smtx  srw syscl  usr sys  wt idl sze");
2080Sstevel@tonic-gate 	else {
2090Sstevel@tonic-gate 		(void) printf("CPU minf mjf xcal  intr ithr  csw icsw migr "
2100Sstevel@tonic-gate 		    "smtx  srw syscl  usr sys  wt idl");
2110Sstevel@tonic-gate 		if (show_set == 1)
2120Sstevel@tonic-gate 			(void) printf(" set");
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 	(void) printf("\n");
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate static void
print_cpu(struct cpu_snapshot * c1,struct cpu_snapshot * c2)2180Sstevel@tonic-gate print_cpu(struct cpu_snapshot *c1, struct cpu_snapshot *c2)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate 	uint64_t ticks = 0;
2210Sstevel@tonic-gate 	double etime, percent;
2220Sstevel@tonic-gate 	kstat_t *old_vm = NULL;
2230Sstevel@tonic-gate 	kstat_t *old_sys = NULL;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	if (display_pset != -1 && display_pset != c2->cs_pset_id)
2260Sstevel@tonic-gate 		return;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	/*
2290Sstevel@tonic-gate 	 * the first mpstat output will have c1 = NULL, to give
2300Sstevel@tonic-gate 	 * results since boot
2310Sstevel@tonic-gate 	 */
2320Sstevel@tonic-gate 	if (c1) {
2330Sstevel@tonic-gate 		old_vm = &c1->cs_vm;
2340Sstevel@tonic-gate 		old_sys = &c1->cs_sys;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 		/* check there are stats to report */
2370Sstevel@tonic-gate 		if (!CPU_ACTIVE(c1))
2380Sstevel@tonic-gate 			return;
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	/* check there are stats to report */
2420Sstevel@tonic-gate 	if (!CPU_ACTIVE(c2))
2430Sstevel@tonic-gate 		return;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	ticks = cpu_ticks_delta(old_sys, &c2->cs_sys);
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	etime = (double)ticks / hz;
2480Sstevel@tonic-gate 	if (etime == 0.0) /* Prevent divide by zero errors */
2490Sstevel@tonic-gate 		etime = 1.0;
2500Sstevel@tonic-gate 	percent = 100.0 / etime / hz;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	(void) printf("%3d %4.0f %3.0f %4.0f %5.0f %4.0f "
2539123Sjohn.levon@sun.com 	    "%4.0f %4.0f %4.0f %4.0f %4.0f %5.0f  %3.0f %3.0f "
2549123Sjohn.levon@sun.com 	    "%3.0f %3.0f",
2559123Sjohn.levon@sun.com 	    c2->cs_id,
2569123Sjohn.levon@sun.com 	    (kstat_delta(old_vm, &c2->cs_vm, "hat_fault") +
2579123Sjohn.levon@sun.com 	    kstat_delta(old_vm, &c2->cs_vm, "as_fault")) / etime,
2589123Sjohn.levon@sun.com 	    kstat_delta(old_vm, &c2->cs_vm, "maj_fault") / etime,
2599123Sjohn.levon@sun.com 	    kstat_delta(old_sys, &c2->cs_sys, "xcalls") / etime,
2609123Sjohn.levon@sun.com 	    kstat_delta(old_sys, &c2->cs_sys, "intr") / etime,
2619123Sjohn.levon@sun.com 	    kstat_delta(old_sys, &c2->cs_sys, "intrthread") / etime,
2629123Sjohn.levon@sun.com 	    kstat_delta(old_sys, &c2->cs_sys, "pswitch") / etime,
2639123Sjohn.levon@sun.com 	    kstat_delta(old_sys, &c2->cs_sys, "inv_swtch") / etime,
2649123Sjohn.levon@sun.com 	    kstat_delta(old_sys, &c2->cs_sys, "cpumigrate") / etime,
2659123Sjohn.levon@sun.com 	    kstat_delta(old_sys, &c2->cs_sys, "mutex_adenters") / etime,
2669123Sjohn.levon@sun.com 	    (kstat_delta(old_sys, &c2->cs_sys, "rw_rdfails") +
2679123Sjohn.levon@sun.com 	    kstat_delta(old_sys, &c2->cs_sys, "rw_wrfails")) / etime,
2689123Sjohn.levon@sun.com 	    kstat_delta(old_sys, &c2->cs_sys, "syscall") / etime,
2699123Sjohn.levon@sun.com 	    kstat_delta(old_sys, &c2->cs_sys, "cpu_ticks_user") * percent,
2709123Sjohn.levon@sun.com 	    kstat_delta(old_sys, &c2->cs_sys, "cpu_ticks_kernel") * percent,
2719123Sjohn.levon@sun.com 	    kstat_delta(old_sys, &c2->cs_sys, "cpu_ticks_wait") * percent,
2729123Sjohn.levon@sun.com 	    kstat_delta(old_sys, &c2->cs_sys, "cpu_ticks_idle") * percent);
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	if (show_set)
2750Sstevel@tonic-gate 		(void) printf(" %3d", c2->cs_pset_id);
2760Sstevel@tonic-gate 	(void) printf("\n");
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate /*ARGSUSED*/
2800Sstevel@tonic-gate static void
compare_cpu(void * v1,void * v2,void * data)2810Sstevel@tonic-gate compare_cpu(void *v1, void *v2, void *data)
2820Sstevel@tonic-gate {
2830Sstevel@tonic-gate 	struct cpu_snapshot *c1 = (struct cpu_snapshot *)v1;
2840Sstevel@tonic-gate 	struct cpu_snapshot *c2 = (struct cpu_snapshot *)v2;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	if (c2 == NULL)
2870Sstevel@tonic-gate 		return;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	print_cpu(c1, c2);
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate static int
pset_has_stats(struct pset_snapshot * p)2930Sstevel@tonic-gate pset_has_stats(struct pset_snapshot *p)
2940Sstevel@tonic-gate {
2950Sstevel@tonic-gate 	int count = 0;
2960Sstevel@tonic-gate 	size_t i;
2970Sstevel@tonic-gate 	for (i = 0; i < p->ps_nr_cpus; i++) {
2980Sstevel@tonic-gate 		if (CPU_ACTIVE(p->ps_cpus[i]))
2990Sstevel@tonic-gate 			count++;
3000Sstevel@tonic-gate 	}
3010Sstevel@tonic-gate 	return (count);
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate static void
agg_stat(kstat_t * k1,kstat_t * k2,char * name)3050Sstevel@tonic-gate agg_stat(kstat_t *k1, kstat_t *k2, char *name)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate 	kstat_named_t *ksn = kstat_data_lookup(k1, name);
3080Sstevel@tonic-gate 	kstat_named_t *ksn2 = kstat_data_lookup(k2, name);
3090Sstevel@tonic-gate 	ksn->value.ui64 += ksn2->value.ui64;
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate static kstat_t *
agg_vm(struct pset_snapshot * p,kstat_t * ks)3130Sstevel@tonic-gate agg_vm(struct pset_snapshot *p, kstat_t *ks)
3140Sstevel@tonic-gate {
3150Sstevel@tonic-gate 	size_t i;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	if (p->ps_nr_cpus == NULL)
3180Sstevel@tonic-gate 		return (NULL);
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	if (kstat_copy(&p->ps_cpus[0]->cs_vm, ks))
3210Sstevel@tonic-gate 		return (NULL);
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	for (i = 1; i < p->ps_nr_cpus; i++) {
3240Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_vm, "hat_fault");
3250Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_vm, "as_fault");
3260Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_vm, "maj_fault");
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	return (ks);
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate static kstat_t *
agg_sys(struct pset_snapshot * p,kstat_t * ks)3330Sstevel@tonic-gate agg_sys(struct pset_snapshot *p, kstat_t *ks)
3340Sstevel@tonic-gate {
3350Sstevel@tonic-gate 	size_t i;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	if (p->ps_nr_cpus == NULL)
3380Sstevel@tonic-gate 		return (NULL);
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	if (kstat_copy(&p->ps_cpus[0]->cs_sys, ks))
3410Sstevel@tonic-gate 		return (NULL);
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	for (i = 1; i < p->ps_nr_cpus; i++) {
3440Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "xcalls");
3450Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "intr");
3460Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "intrthread");
3470Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "pswitch");
3480Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "inv_swtch");
3490Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "cpumigrate");
3500Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "mutex_adenters");
3510Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "rw_rdfails");
3520Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "rw_wrfails");
3530Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "syscall");
3540Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "cpu_ticks_user");
3550Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "cpu_ticks_kernel");
3560Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "cpu_ticks_wait");
3570Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "cpu_ticks_idle");
3580Sstevel@tonic-gate 	}
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	return (ks);
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate static uint64_t
get_nr_ticks(struct pset_snapshot * p1,struct pset_snapshot * p2)3640Sstevel@tonic-gate get_nr_ticks(struct pset_snapshot *p1, struct pset_snapshot *p2)
3650Sstevel@tonic-gate {
3660Sstevel@tonic-gate 	kstat_t *old = NULL;
3670Sstevel@tonic-gate 	kstat_t *new = NULL;
3680Sstevel@tonic-gate 	size_t i = 0;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	for (i = 0; p1 && i < p1->ps_nr_cpus; i++) {
3710Sstevel@tonic-gate 		if (p1->ps_cpus[i]->cs_sys.ks_data) {
3720Sstevel@tonic-gate 			old = &p1->ps_cpus[i]->cs_sys;
3730Sstevel@tonic-gate 			break;
3740Sstevel@tonic-gate 		}
3750Sstevel@tonic-gate 	}
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	for (i = 0; p2 && i < p2->ps_nr_cpus; i++) {
3780Sstevel@tonic-gate 		if (p2->ps_cpus[i]->cs_sys.ks_data) {
3790Sstevel@tonic-gate 			new = &p2->ps_cpus[i]->cs_sys;
3800Sstevel@tonic-gate 			break;
3810Sstevel@tonic-gate 		}
3820Sstevel@tonic-gate 	}
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	if (old == NULL && new == NULL)
3850Sstevel@tonic-gate 		return (0);
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	if (new == NULL) {
3880Sstevel@tonic-gate 		new = old;
3890Sstevel@tonic-gate 		old = NULL;
3900Sstevel@tonic-gate 	}
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	return (cpu_ticks_delta(old, new));
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate static void
print_pset(struct pset_snapshot * p1,struct pset_snapshot * p2)3960Sstevel@tonic-gate print_pset(struct pset_snapshot *p1, struct pset_snapshot *p2)
3970Sstevel@tonic-gate {
3980Sstevel@tonic-gate 	uint64_t ticks = 0;
3990Sstevel@tonic-gate 	double etime, percent;
4000Sstevel@tonic-gate 	kstat_t old_vm;
4010Sstevel@tonic-gate 	kstat_t old_sys;
4020Sstevel@tonic-gate 	kstat_t new_vm;
4030Sstevel@tonic-gate 	kstat_t new_sys;
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	if (display_pset != -1 && display_pset != p2->ps_id)
4060Sstevel@tonic-gate 		return;
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	if ((p1 && !pset_has_stats(p1)) || !pset_has_stats(p2))
4090Sstevel@tonic-gate 		return;
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	old_vm.ks_data = old_sys.ks_data = NULL;
4120Sstevel@tonic-gate 	new_vm.ks_data = new_sys.ks_data = NULL;
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	/*
4150Sstevel@tonic-gate 	 * FIXME: these aggs will count "new" or disappeared cpus
4160Sstevel@tonic-gate 	 * in a set, leaving an apparent huge change.
4170Sstevel@tonic-gate 	 */
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	/*
4200Sstevel@tonic-gate 	 * the first mpstat output will have p1 = NULL, to give
4210Sstevel@tonic-gate 	 * results since boot
4220Sstevel@tonic-gate 	 */
4230Sstevel@tonic-gate 	if (p1) {
4240Sstevel@tonic-gate 		if (!agg_vm(p1, &old_vm) || !agg_sys(p1, &old_sys))
4250Sstevel@tonic-gate 			goto out;
4260Sstevel@tonic-gate 	}
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	if (!agg_vm(p2, &new_vm) || !agg_sys(p2, &new_sys))
4290Sstevel@tonic-gate 		goto out;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	ticks = get_nr_ticks(p1, p2);
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	etime = (double)ticks / hz;
4340Sstevel@tonic-gate 	if (etime == 0.0) /* Prevent divide by zero errors */
4350Sstevel@tonic-gate 		etime = 1.0;
4360Sstevel@tonic-gate 	percent = 100.0 / p2->ps_nr_cpus / etime / hz;
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	(void) printf("%3d %4.0f %3.0f %4.0f %5.0f %4.0f "
4399123Sjohn.levon@sun.com 	    "%4.0f %4.0f %4.0f %4.0f %4.0f %5.0f  %3.0f %3.0f "
4409123Sjohn.levon@sun.com 	    "%3.0f %3.0f %3d\n",
4419123Sjohn.levon@sun.com 	    p2->ps_id,
4429123Sjohn.levon@sun.com 	    (kstat_delta(&old_vm, &new_vm, "hat_fault") +
4439123Sjohn.levon@sun.com 	    kstat_delta(&old_vm, &new_vm, "as_fault")) / etime,
4449123Sjohn.levon@sun.com 	    kstat_delta(&old_vm, &new_vm, "maj_fault") / etime,
4459123Sjohn.levon@sun.com 	    kstat_delta(&old_sys, &new_sys, "xcalls") / etime,
4469123Sjohn.levon@sun.com 	    kstat_delta(&old_sys, &new_sys, "intr") / etime,
4479123Sjohn.levon@sun.com 	    kstat_delta(&old_sys, &new_sys, "intrthread") / etime,
4489123Sjohn.levon@sun.com 	    kstat_delta(&old_sys, &new_sys, "pswitch") / etime,
4499123Sjohn.levon@sun.com 	    kstat_delta(&old_sys, &new_sys, "inv_swtch") / etime,
4509123Sjohn.levon@sun.com 	    kstat_delta(&old_sys, &new_sys, "cpumigrate") / etime,
4519123Sjohn.levon@sun.com 	    kstat_delta(&old_sys, &new_sys, "mutex_adenters") / etime,
4529123Sjohn.levon@sun.com 	    (kstat_delta(&old_sys, &new_sys, "rw_rdfails") +
4539123Sjohn.levon@sun.com 	    kstat_delta(&old_sys, &new_sys, "rw_wrfails")) / etime,
4549123Sjohn.levon@sun.com 	    kstat_delta(&old_sys, &new_sys, "syscall") / etime,
4559123Sjohn.levon@sun.com 	    kstat_delta(&old_sys, &new_sys, "cpu_ticks_user") * percent,
4569123Sjohn.levon@sun.com 	    kstat_delta(&old_sys, &new_sys, "cpu_ticks_kernel") * percent,
4579123Sjohn.levon@sun.com 	    kstat_delta(&old_sys, &new_sys, "cpu_ticks_wait") * percent,
4589123Sjohn.levon@sun.com 	    kstat_delta(&old_sys, &new_sys, "cpu_ticks_idle") * percent,
4599123Sjohn.levon@sun.com 	    p2->ps_nr_cpus);
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate out:
4620Sstevel@tonic-gate 	free(old_vm.ks_data);
4630Sstevel@tonic-gate 	free(old_sys.ks_data);
4640Sstevel@tonic-gate 	free(new_vm.ks_data);
4650Sstevel@tonic-gate 	free(new_sys.ks_data);
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate /*ARGSUSED*/
4690Sstevel@tonic-gate static void
compare_pset(void * v1,void * v2,void * data)4700Sstevel@tonic-gate compare_pset(void *v1, void *v2, void *data)
4710Sstevel@tonic-gate {
4720Sstevel@tonic-gate 	struct pset_snapshot *p1 = (struct pset_snapshot *)v1;
4730Sstevel@tonic-gate 	struct pset_snapshot *p2 = (struct pset_snapshot *)v2;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	if (p2 == NULL)
4760Sstevel@tonic-gate 		return;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	print_pset(p1, p2);
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate /*
4830Sstevel@tonic-gate  * Report statistics for a sample interval.
4840Sstevel@tonic-gate  */
4850Sstevel@tonic-gate static void
show_cpu_usage(struct snapshot * old,struct snapshot * new,int display_agg)4860Sstevel@tonic-gate show_cpu_usage(struct snapshot *old, struct snapshot *new, int display_agg)
4870Sstevel@tonic-gate {
4880Sstevel@tonic-gate 	static int lines_until_reprint = 0;
4890Sstevel@tonic-gate 	enum snapshot_types type = SNAP_CPUS;
4900Sstevel@tonic-gate 	snapshot_cb cb = compare_cpu;
4910Sstevel@tonic-gate 
4929123Sjohn.levon@sun.com 	if (timestamp_fmt != NODATE)
493*10265SKrishnendu.Sadhukhan@Sun.COM 		print_timestamp(timestamp_fmt);
4949123Sjohn.levon@sun.com 
4950Sstevel@tonic-gate 	if (lines_until_reprint == 0 || nr_active_cpus(new) > 1) {
4960Sstevel@tonic-gate 		print_header(display_agg, show_set);
4970Sstevel@tonic-gate 		lines_until_reprint = REPRINT;
4980Sstevel@tonic-gate 	}
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	lines_until_reprint--;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	if (display_agg) {
5030Sstevel@tonic-gate 		type = SNAP_PSETS;
5040Sstevel@tonic-gate 		cb = compare_pset;
5050Sstevel@tonic-gate 	}
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	/* print stats since boot the first time round */
5080Sstevel@tonic-gate 	(void) snapshot_walk(type, old, new, cb, NULL);
5090Sstevel@tonic-gate 	(void) fflush(stdout);
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate /*
5130Sstevel@tonic-gate  * Usage message on error.
5140Sstevel@tonic-gate  */
5150Sstevel@tonic-gate static void
usage(void)5160Sstevel@tonic-gate usage(void)
5170Sstevel@tonic-gate {
5180Sstevel@tonic-gate 	(void) fprintf(stderr,
5199123Sjohn.levon@sun.com 	    "Usage: mpstat [-aq] [-p | -P processor_set] [-T d|u] "
5209123Sjohn.levon@sun.com 	    "[interval [count]]\n");
5210Sstevel@tonic-gate 	exit(1);
5220Sstevel@tonic-gate }
523