1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/pset.h>
30*0Sstevel@tonic-gate #include <sys/types.h>
31*0Sstevel@tonic-gate #include <sys/time.h>
32*0Sstevel@tonic-gate #include <sys/sysinfo.h>
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include <assert.h>
35*0Sstevel@tonic-gate #include <stdio.h>
36*0Sstevel@tonic-gate #include <stdlib.h>
37*0Sstevel@tonic-gate #include <stdarg.h>
38*0Sstevel@tonic-gate #include <ctype.h>
39*0Sstevel@tonic-gate #include <unistd.h>
40*0Sstevel@tonic-gate #include <memory.h>
41*0Sstevel@tonic-gate #include <string.h>
42*0Sstevel@tonic-gate #include <strings.h>
43*0Sstevel@tonic-gate #include <fcntl.h>
44*0Sstevel@tonic-gate #include <errno.h>
45*0Sstevel@tonic-gate #include <kstat.h>
46*0Sstevel@tonic-gate #include <poll.h>
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #include "statcommon.h"
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #define	SNAP(s, i, l, n)	((s) ? agg_proc_snap(s, i, l, n) : 0)
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate #define	REPRINT		20
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate char cmdname[] = "mpstat";
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate static int hz;
57*0Sstevel@tonic-gate static int display_pset = -1;
58*0Sstevel@tonic-gate static int show_set = 0;
59*0Sstevel@tonic-gate static int suppress_state;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate static void print_header(int, int);
62*0Sstevel@tonic-gate static void show_cpu_usage(struct snapshot *, struct snapshot *, int);
63*0Sstevel@tonic-gate static void usage(void);
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate int
66*0Sstevel@tonic-gate main(int argc, char **argv)
67*0Sstevel@tonic-gate {
68*0Sstevel@tonic-gate 	int c;
69*0Sstevel@tonic-gate 	int display_agg = 0;
70*0Sstevel@tonic-gate 	int iter = 1;
71*0Sstevel@tonic-gate 	int interval = 0;
72*0Sstevel@tonic-gate 	int poll_interval = 0;
73*0Sstevel@tonic-gate 	char *endptr;
74*0Sstevel@tonic-gate 	int infinite_cycles = 0;
75*0Sstevel@tonic-gate 	kstat_ctl_t *kc;
76*0Sstevel@tonic-gate 	struct snapshot *old = NULL;
77*0Sstevel@tonic-gate 	struct snapshot *new = NULL;
78*0Sstevel@tonic-gate 	enum snapshot_types types = SNAP_CPUS;
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "apP:q")) != (int)EOF)
81*0Sstevel@tonic-gate 		switch (c) {
82*0Sstevel@tonic-gate 			case 'a':
83*0Sstevel@tonic-gate 				/*
84*0Sstevel@tonic-gate 				 * Display aggregate data for processor sets.
85*0Sstevel@tonic-gate 				 */
86*0Sstevel@tonic-gate 				display_agg = 1;
87*0Sstevel@tonic-gate 				break;
88*0Sstevel@tonic-gate 			case 'p':
89*0Sstevel@tonic-gate 				/*
90*0Sstevel@tonic-gate 				 * Display all processor sets.
91*0Sstevel@tonic-gate 				 */
92*0Sstevel@tonic-gate 				if (display_pset != -1)
93*0Sstevel@tonic-gate 					usage();
94*0Sstevel@tonic-gate 				show_set = 1;
95*0Sstevel@tonic-gate 				break;
96*0Sstevel@tonic-gate 			case 'P':
97*0Sstevel@tonic-gate 				/*
98*0Sstevel@tonic-gate 				 * Display specific processor set.
99*0Sstevel@tonic-gate 				 */
100*0Sstevel@tonic-gate 				if (show_set == 1)
101*0Sstevel@tonic-gate 					usage();
102*0Sstevel@tonic-gate 				display_pset = (int)strtol
103*0Sstevel@tonic-gate 				    (optarg, &endptr, 10);
104*0Sstevel@tonic-gate 				if (*endptr != NULL)
105*0Sstevel@tonic-gate 					usage();
106*0Sstevel@tonic-gate 				/*
107*0Sstevel@tonic-gate 				 * Not valid to specify a negative processor
108*0Sstevel@tonic-gate 				 * set value.
109*0Sstevel@tonic-gate 				 */
110*0Sstevel@tonic-gate 				if (display_pset < 0)
111*0Sstevel@tonic-gate 					usage();
112*0Sstevel@tonic-gate 				break;
113*0Sstevel@tonic-gate 			case 'q':
114*0Sstevel@tonic-gate 				suppress_state = 1;
115*0Sstevel@tonic-gate 				break;
116*0Sstevel@tonic-gate 			case '?':
117*0Sstevel@tonic-gate 				usage();
118*0Sstevel@tonic-gate 				break;
119*0Sstevel@tonic-gate 		}
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	hz = sysconf(_SC_CLK_TCK);
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	if (argc > optind) {
124*0Sstevel@tonic-gate 		interval = (int)strtol(argv[optind], &endptr, 10);
125*0Sstevel@tonic-gate 		if (*endptr != NULL)
126*0Sstevel@tonic-gate 			usage();
127*0Sstevel@tonic-gate 		poll_interval = 1000 * interval;
128*0Sstevel@tonic-gate 		if (argc > optind + 1) {
129*0Sstevel@tonic-gate 			iter = (unsigned int)strtoul
130*0Sstevel@tonic-gate 			    (argv[optind + 1], &endptr, 10);
131*0Sstevel@tonic-gate 			if (*endptr != NULL || iter < 0)
132*0Sstevel@tonic-gate 				usage();
133*0Sstevel@tonic-gate 			if (iter == 0)
134*0Sstevel@tonic-gate 				return (0);
135*0Sstevel@tonic-gate 		} else {
136*0Sstevel@tonic-gate 			infinite_cycles = 1;
137*0Sstevel@tonic-gate 		}
138*0Sstevel@tonic-gate 	}
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	if (display_agg || show_set || display_pset != -1)
141*0Sstevel@tonic-gate 		types |= SNAP_PSETS;
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	kc = open_kstat();
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	while (infinite_cycles || iter > 0) {
146*0Sstevel@tonic-gate 		free_snapshot(old);
147*0Sstevel@tonic-gate 		old = new;
148*0Sstevel@tonic-gate 		new = acquire_snapshot(kc, types, NULL);
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 		if (!suppress_state)
151*0Sstevel@tonic-gate 			snapshot_report_changes(old, new);
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 		/* if config changed, show stats from boot */
154*0Sstevel@tonic-gate 		if (snapshot_has_changed(old, new)) {
155*0Sstevel@tonic-gate 			free_snapshot(old);
156*0Sstevel@tonic-gate 			old = NULL;
157*0Sstevel@tonic-gate 		}
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 		show_cpu_usage(old, new, display_agg);
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 		if (!infinite_cycles && --iter < 1)
162*0Sstevel@tonic-gate 			break;
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 		(void) poll(NULL, 0, poll_interval);
165*0Sstevel@tonic-gate 	}
166*0Sstevel@tonic-gate 	return (0);
167*0Sstevel@tonic-gate }
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate /*
170*0Sstevel@tonic-gate  * Print an mpstat output header.
171*0Sstevel@tonic-gate  */
172*0Sstevel@tonic-gate static void
173*0Sstevel@tonic-gate print_header(int display_agg, int show_set)
174*0Sstevel@tonic-gate {
175*0Sstevel@tonic-gate 	if (display_agg == 1)
176*0Sstevel@tonic-gate 		(void) printf("SET minf mjf xcal  intr ithr  csw icsw migr "
177*0Sstevel@tonic-gate 		    "smtx  srw syscl  usr sys  wt idl sze");
178*0Sstevel@tonic-gate 	else {
179*0Sstevel@tonic-gate 		(void) printf("CPU minf mjf xcal  intr ithr  csw icsw migr "
180*0Sstevel@tonic-gate 		    "smtx  srw syscl  usr sys  wt idl");
181*0Sstevel@tonic-gate 		if (show_set == 1)
182*0Sstevel@tonic-gate 			(void) printf(" set");
183*0Sstevel@tonic-gate 	}
184*0Sstevel@tonic-gate 	(void) printf("\n");
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate static void
188*0Sstevel@tonic-gate print_cpu(struct cpu_snapshot *c1, struct cpu_snapshot *c2)
189*0Sstevel@tonic-gate {
190*0Sstevel@tonic-gate 	uint64_t ticks = 0;
191*0Sstevel@tonic-gate 	double etime, percent;
192*0Sstevel@tonic-gate 	kstat_t *old_vm = NULL;
193*0Sstevel@tonic-gate 	kstat_t *old_sys = NULL;
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	if (display_pset != -1 && display_pset != c2->cs_pset_id)
196*0Sstevel@tonic-gate 		return;
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	/*
199*0Sstevel@tonic-gate 	 * the first mpstat output will have c1 = NULL, to give
200*0Sstevel@tonic-gate 	 * results since boot
201*0Sstevel@tonic-gate 	 */
202*0Sstevel@tonic-gate 	if (c1) {
203*0Sstevel@tonic-gate 		old_vm = &c1->cs_vm;
204*0Sstevel@tonic-gate 		old_sys = &c1->cs_sys;
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 		/* check there are stats to report */
207*0Sstevel@tonic-gate 		if (!CPU_ACTIVE(c1))
208*0Sstevel@tonic-gate 			return;
209*0Sstevel@tonic-gate 	}
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	/* check there are stats to report */
212*0Sstevel@tonic-gate 	if (!CPU_ACTIVE(c2))
213*0Sstevel@tonic-gate 		return;
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	ticks = cpu_ticks_delta(old_sys, &c2->cs_sys);
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	etime = (double)ticks / hz;
218*0Sstevel@tonic-gate 	if (etime == 0.0) /* Prevent divide by zero errors */
219*0Sstevel@tonic-gate 		etime = 1.0;
220*0Sstevel@tonic-gate 	percent = 100.0 / etime / hz;
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	(void) printf("%3d %4.0f %3.0f %4.0f %5.0f %4.0f "
223*0Sstevel@tonic-gate 	"%4.0f %4.0f %4.0f %4.0f %4.0f %5.0f  %3.0f %3.0f "
224*0Sstevel@tonic-gate 	"%3.0f %3.0f",
225*0Sstevel@tonic-gate 	c2->cs_id,
226*0Sstevel@tonic-gate 	(kstat_delta(old_vm, &c2->cs_vm, "hat_fault") +
227*0Sstevel@tonic-gate 		kstat_delta(old_vm, &c2->cs_vm, "as_fault")) / etime,
228*0Sstevel@tonic-gate 	kstat_delta(old_vm, &c2->cs_vm, "maj_fault") / etime,
229*0Sstevel@tonic-gate 	kstat_delta(old_sys, &c2->cs_sys, "xcalls") / etime,
230*0Sstevel@tonic-gate 	kstat_delta(old_sys, &c2->cs_sys, "intr") / etime,
231*0Sstevel@tonic-gate 	kstat_delta(old_sys, &c2->cs_sys, "intrthread") / etime,
232*0Sstevel@tonic-gate 	kstat_delta(old_sys, &c2->cs_sys, "pswitch") / etime,
233*0Sstevel@tonic-gate 	kstat_delta(old_sys, &c2->cs_sys, "inv_swtch") / etime,
234*0Sstevel@tonic-gate 	kstat_delta(old_sys, &c2->cs_sys, "cpumigrate") / etime,
235*0Sstevel@tonic-gate 	kstat_delta(old_sys, &c2->cs_sys, "mutex_adenters") / etime,
236*0Sstevel@tonic-gate 	(kstat_delta(old_sys, &c2->cs_sys, "rw_rdfails") +
237*0Sstevel@tonic-gate 	kstat_delta(old_sys, &c2->cs_sys, "rw_wrfails")) / etime,
238*0Sstevel@tonic-gate 	kstat_delta(old_sys, &c2->cs_sys, "syscall") / etime,
239*0Sstevel@tonic-gate 	kstat_delta(old_sys, &c2->cs_sys, "cpu_ticks_user") * percent,
240*0Sstevel@tonic-gate 	kstat_delta(old_sys, &c2->cs_sys, "cpu_ticks_kernel") * percent,
241*0Sstevel@tonic-gate 	kstat_delta(old_sys, &c2->cs_sys, "cpu_ticks_wait") * percent,
242*0Sstevel@tonic-gate 	kstat_delta(old_sys, &c2->cs_sys, "cpu_ticks_idle") * percent);
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	if (show_set)
245*0Sstevel@tonic-gate 		(void) printf(" %3d", c2->cs_pset_id);
246*0Sstevel@tonic-gate 	(void) printf("\n");
247*0Sstevel@tonic-gate }
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate /*ARGSUSED*/
250*0Sstevel@tonic-gate static void
251*0Sstevel@tonic-gate compare_cpu(void *v1, void *v2, void *data)
252*0Sstevel@tonic-gate {
253*0Sstevel@tonic-gate 	struct cpu_snapshot *c1 = (struct cpu_snapshot *)v1;
254*0Sstevel@tonic-gate 	struct cpu_snapshot *c2 = (struct cpu_snapshot *)v2;
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	if (c2 == NULL)
257*0Sstevel@tonic-gate 		return;
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate 	print_cpu(c1, c2);
260*0Sstevel@tonic-gate }
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate static int
263*0Sstevel@tonic-gate pset_has_stats(struct pset_snapshot *p)
264*0Sstevel@tonic-gate {
265*0Sstevel@tonic-gate 	int count = 0;
266*0Sstevel@tonic-gate 	size_t i;
267*0Sstevel@tonic-gate 	for (i = 0; i < p->ps_nr_cpus; i++) {
268*0Sstevel@tonic-gate 		if (CPU_ACTIVE(p->ps_cpus[i]))
269*0Sstevel@tonic-gate 			count++;
270*0Sstevel@tonic-gate 	}
271*0Sstevel@tonic-gate 	return (count);
272*0Sstevel@tonic-gate }
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate static void
275*0Sstevel@tonic-gate agg_stat(kstat_t *k1, kstat_t *k2, char *name)
276*0Sstevel@tonic-gate {
277*0Sstevel@tonic-gate 	kstat_named_t *ksn = kstat_data_lookup(k1, name);
278*0Sstevel@tonic-gate 	kstat_named_t *ksn2 = kstat_data_lookup(k2, name);
279*0Sstevel@tonic-gate 	ksn->value.ui64 += ksn2->value.ui64;
280*0Sstevel@tonic-gate }
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate static kstat_t *
283*0Sstevel@tonic-gate agg_vm(struct pset_snapshot *p, kstat_t *ks)
284*0Sstevel@tonic-gate {
285*0Sstevel@tonic-gate 	size_t i;
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 	if (p->ps_nr_cpus == NULL)
288*0Sstevel@tonic-gate 		return (NULL);
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 	if (kstat_copy(&p->ps_cpus[0]->cs_vm, ks))
291*0Sstevel@tonic-gate 		return (NULL);
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	for (i = 1; i < p->ps_nr_cpus; i++) {
294*0Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_vm, "hat_fault");
295*0Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_vm, "as_fault");
296*0Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_vm, "maj_fault");
297*0Sstevel@tonic-gate 	}
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	return (ks);
300*0Sstevel@tonic-gate }
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate static kstat_t *
303*0Sstevel@tonic-gate agg_sys(struct pset_snapshot *p, kstat_t *ks)
304*0Sstevel@tonic-gate {
305*0Sstevel@tonic-gate 	size_t i;
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate 	if (p->ps_nr_cpus == NULL)
308*0Sstevel@tonic-gate 		return (NULL);
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 	if (kstat_copy(&p->ps_cpus[0]->cs_sys, ks))
311*0Sstevel@tonic-gate 		return (NULL);
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 	for (i = 1; i < p->ps_nr_cpus; i++) {
314*0Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "xcalls");
315*0Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "intr");
316*0Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "intrthread");
317*0Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "pswitch");
318*0Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "inv_swtch");
319*0Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "cpumigrate");
320*0Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "mutex_adenters");
321*0Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "rw_rdfails");
322*0Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "rw_wrfails");
323*0Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "syscall");
324*0Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "cpu_ticks_user");
325*0Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "cpu_ticks_kernel");
326*0Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "cpu_ticks_wait");
327*0Sstevel@tonic-gate 		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "cpu_ticks_idle");
328*0Sstevel@tonic-gate 	}
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 	return (ks);
331*0Sstevel@tonic-gate }
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate static uint64_t
334*0Sstevel@tonic-gate get_nr_ticks(struct pset_snapshot *p1, struct pset_snapshot *p2)
335*0Sstevel@tonic-gate {
336*0Sstevel@tonic-gate 	kstat_t *old = NULL;
337*0Sstevel@tonic-gate 	kstat_t *new = NULL;
338*0Sstevel@tonic-gate 	size_t i = 0;
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 	for (i = 0; p1 && i < p1->ps_nr_cpus; i++) {
341*0Sstevel@tonic-gate 		if (p1->ps_cpus[i]->cs_sys.ks_data) {
342*0Sstevel@tonic-gate 			old = &p1->ps_cpus[i]->cs_sys;
343*0Sstevel@tonic-gate 			break;
344*0Sstevel@tonic-gate 		}
345*0Sstevel@tonic-gate 	}
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 	for (i = 0; p2 && i < p2->ps_nr_cpus; i++) {
348*0Sstevel@tonic-gate 		if (p2->ps_cpus[i]->cs_sys.ks_data) {
349*0Sstevel@tonic-gate 			new = &p2->ps_cpus[i]->cs_sys;
350*0Sstevel@tonic-gate 			break;
351*0Sstevel@tonic-gate 		}
352*0Sstevel@tonic-gate 	}
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 	if (old == NULL && new == NULL)
355*0Sstevel@tonic-gate 		return (0);
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 	if (new == NULL) {
358*0Sstevel@tonic-gate 		new = old;
359*0Sstevel@tonic-gate 		old = NULL;
360*0Sstevel@tonic-gate 	}
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate 	return (cpu_ticks_delta(old, new));
363*0Sstevel@tonic-gate }
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate static void
366*0Sstevel@tonic-gate print_pset(struct pset_snapshot *p1, struct pset_snapshot *p2)
367*0Sstevel@tonic-gate {
368*0Sstevel@tonic-gate 	uint64_t ticks = 0;
369*0Sstevel@tonic-gate 	double etime, percent;
370*0Sstevel@tonic-gate 	kstat_t old_vm;
371*0Sstevel@tonic-gate 	kstat_t old_sys;
372*0Sstevel@tonic-gate 	kstat_t new_vm;
373*0Sstevel@tonic-gate 	kstat_t new_sys;
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	if (display_pset != -1 && display_pset != p2->ps_id)
376*0Sstevel@tonic-gate 		return;
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 	if ((p1 && !pset_has_stats(p1)) || !pset_has_stats(p2))
379*0Sstevel@tonic-gate 		return;
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate 	old_vm.ks_data = old_sys.ks_data = NULL;
382*0Sstevel@tonic-gate 	new_vm.ks_data = new_sys.ks_data = NULL;
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate 	/*
385*0Sstevel@tonic-gate 	 * FIXME: these aggs will count "new" or disappeared cpus
386*0Sstevel@tonic-gate 	 * in a set, leaving an apparent huge change.
387*0Sstevel@tonic-gate 	 */
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 	/*
390*0Sstevel@tonic-gate 	 * the first mpstat output will have p1 = NULL, to give
391*0Sstevel@tonic-gate 	 * results since boot
392*0Sstevel@tonic-gate 	 */
393*0Sstevel@tonic-gate 	if (p1) {
394*0Sstevel@tonic-gate 		if (!agg_vm(p1, &old_vm) || !agg_sys(p1, &old_sys))
395*0Sstevel@tonic-gate 			goto out;
396*0Sstevel@tonic-gate 	}
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 	if (!agg_vm(p2, &new_vm) || !agg_sys(p2, &new_sys))
399*0Sstevel@tonic-gate 		goto out;
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 	ticks = get_nr_ticks(p1, p2);
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 	etime = (double)ticks / hz;
404*0Sstevel@tonic-gate 	if (etime == 0.0) /* Prevent divide by zero errors */
405*0Sstevel@tonic-gate 		etime = 1.0;
406*0Sstevel@tonic-gate 	percent = 100.0 / p2->ps_nr_cpus / etime / hz;
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 	(void) printf("%3d %4.0f %3.0f %4.0f %5.0f %4.0f "
409*0Sstevel@tonic-gate 	"%4.0f %4.0f %4.0f %4.0f %4.0f %5.0f  %3.0f %3.0f "
410*0Sstevel@tonic-gate 	"%3.0f %3.0f %3d\n",
411*0Sstevel@tonic-gate 	p2->ps_id,
412*0Sstevel@tonic-gate 	(kstat_delta(&old_vm, &new_vm, "hat_fault") +
413*0Sstevel@tonic-gate 		kstat_delta(&old_vm, &new_vm, "as_fault")) / etime,
414*0Sstevel@tonic-gate 	kstat_delta(&old_vm, &new_vm, "maj_fault") / etime,
415*0Sstevel@tonic-gate 	kstat_delta(&old_sys, &new_sys, "xcalls") / etime,
416*0Sstevel@tonic-gate 	kstat_delta(&old_sys, &new_sys, "intr") / etime,
417*0Sstevel@tonic-gate 	kstat_delta(&old_sys, &new_sys, "intrthread") / etime,
418*0Sstevel@tonic-gate 	kstat_delta(&old_sys, &new_sys, "pswitch") / etime,
419*0Sstevel@tonic-gate 	kstat_delta(&old_sys, &new_sys, "inv_swtch") / etime,
420*0Sstevel@tonic-gate 	kstat_delta(&old_sys, &new_sys, "cpumigrate") / etime,
421*0Sstevel@tonic-gate 	kstat_delta(&old_sys, &new_sys, "mutex_adenters") / etime,
422*0Sstevel@tonic-gate 	(kstat_delta(&old_sys, &new_sys, "rw_rdfails") +
423*0Sstevel@tonic-gate 	kstat_delta(&old_sys, &new_sys, "rw_wrfails")) / etime,
424*0Sstevel@tonic-gate 	kstat_delta(&old_sys, &new_sys, "syscall") / etime,
425*0Sstevel@tonic-gate 	kstat_delta(&old_sys, &new_sys, "cpu_ticks_user") * percent,
426*0Sstevel@tonic-gate 	kstat_delta(&old_sys, &new_sys, "cpu_ticks_kernel") * percent,
427*0Sstevel@tonic-gate 	kstat_delta(&old_sys, &new_sys, "cpu_ticks_wait") * percent,
428*0Sstevel@tonic-gate 	kstat_delta(&old_sys, &new_sys, "cpu_ticks_idle") * percent,
429*0Sstevel@tonic-gate 	p2->ps_nr_cpus);
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate out:
432*0Sstevel@tonic-gate 	free(old_vm.ks_data);
433*0Sstevel@tonic-gate 	free(old_sys.ks_data);
434*0Sstevel@tonic-gate 	free(new_vm.ks_data);
435*0Sstevel@tonic-gate 	free(new_sys.ks_data);
436*0Sstevel@tonic-gate }
437*0Sstevel@tonic-gate 
438*0Sstevel@tonic-gate /*ARGSUSED*/
439*0Sstevel@tonic-gate static void
440*0Sstevel@tonic-gate compare_pset(void *v1, void *v2, void *data)
441*0Sstevel@tonic-gate {
442*0Sstevel@tonic-gate 	struct pset_snapshot *p1 = (struct pset_snapshot *)v1;
443*0Sstevel@tonic-gate 	struct pset_snapshot *p2 = (struct pset_snapshot *)v2;
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate 	if (p2 == NULL)
446*0Sstevel@tonic-gate 		return;
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 	print_pset(p1, p2);
449*0Sstevel@tonic-gate }
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate /*
453*0Sstevel@tonic-gate  * Report statistics for a sample interval.
454*0Sstevel@tonic-gate  */
455*0Sstevel@tonic-gate static void
456*0Sstevel@tonic-gate show_cpu_usage(struct snapshot *old, struct snapshot *new, int display_agg)
457*0Sstevel@tonic-gate {
458*0Sstevel@tonic-gate 	static int lines_until_reprint = 0;
459*0Sstevel@tonic-gate 	enum snapshot_types type = SNAP_CPUS;
460*0Sstevel@tonic-gate 	snapshot_cb cb = compare_cpu;
461*0Sstevel@tonic-gate 
462*0Sstevel@tonic-gate 	if (lines_until_reprint == 0 || nr_active_cpus(new) > 1) {
463*0Sstevel@tonic-gate 		print_header(display_agg, show_set);
464*0Sstevel@tonic-gate 		lines_until_reprint = REPRINT;
465*0Sstevel@tonic-gate 	}
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate 	lines_until_reprint--;
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate 	if (display_agg) {
470*0Sstevel@tonic-gate 		type = SNAP_PSETS;
471*0Sstevel@tonic-gate 		cb = compare_pset;
472*0Sstevel@tonic-gate 	}
473*0Sstevel@tonic-gate 
474*0Sstevel@tonic-gate 	/* print stats since boot the first time round */
475*0Sstevel@tonic-gate 	(void) snapshot_walk(type, old, new, cb, NULL);
476*0Sstevel@tonic-gate 	(void) fflush(stdout);
477*0Sstevel@tonic-gate }
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate /*
480*0Sstevel@tonic-gate  * Usage message on error.
481*0Sstevel@tonic-gate  */
482*0Sstevel@tonic-gate static void
483*0Sstevel@tonic-gate usage(void)
484*0Sstevel@tonic-gate {
485*0Sstevel@tonic-gate 	(void) fprintf(stderr,
486*0Sstevel@tonic-gate 	    "Usage: mpstat [-aq] [-p | -P processor_set] [interval [count]]\n");
487*0Sstevel@tonic-gate 	exit(1);
488*0Sstevel@tonic-gate }
489