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