xref: /titanic_53/usr/src/cmd/sa/sadc.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 2004 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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
30*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /*
34*7c478bd9Sstevel@tonic-gate  * sadc.c writes system activity binary data to a file or stdout.
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  * Usage: sadc [t n] [file]
37*7c478bd9Sstevel@tonic-gate  *
38*7c478bd9Sstevel@tonic-gate  * if t and n are not specified, it writes a dummy record to data file. This
39*7c478bd9Sstevel@tonic-gate  * usage is particularly used at system booting.  If t and n are specified, it
40*7c478bd9Sstevel@tonic-gate  * writes system data n times to file every t seconds.  In both cases, if file
41*7c478bd9Sstevel@tonic-gate  * is not specified, it writes data to stdout.
42*7c478bd9Sstevel@tonic-gate  */
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/flock.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/var.h>
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate #include <ctype.h>
54*7c478bd9Sstevel@tonic-gate #include <errno.h>
55*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
56*7c478bd9Sstevel@tonic-gate #include <kstat.h>
57*7c478bd9Sstevel@tonic-gate #include <memory.h>
58*7c478bd9Sstevel@tonic-gate #include <nlist.h>
59*7c478bd9Sstevel@tonic-gate #include <signal.h>
60*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
61*7c478bd9Sstevel@tonic-gate #include <stdio.h>
62*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
63*7c478bd9Sstevel@tonic-gate #include <string.h>
64*7c478bd9Sstevel@tonic-gate #include <time.h>
65*7c478bd9Sstevel@tonic-gate #include <unistd.h>
66*7c478bd9Sstevel@tonic-gate #include <strings.h>
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate #include "sa.h"
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate #define	MAX(x1, x2)	((x1) >= (x2) ? (x1) : (x2))
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate static	kstat_ctl_t	*kc;		/* libkstat cookie */
73*7c478bd9Sstevel@tonic-gate static	int	ncpus;
74*7c478bd9Sstevel@tonic-gate static	int	oncpus;
75*7c478bd9Sstevel@tonic-gate static	kstat_t	**cpu_stat_list = NULL;
76*7c478bd9Sstevel@tonic-gate static	kstat_t	**ocpu_stat_list = NULL;
77*7c478bd9Sstevel@tonic-gate static	int	ncaches;
78*7c478bd9Sstevel@tonic-gate static	kstat_t	**kmem_cache_list = NULL;
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate static	kstat_t	*sysinfo_ksp, *vminfo_ksp, *var_ksp;
81*7c478bd9Sstevel@tonic-gate static	kstat_t *system_misc_ksp, *ufs_inode_ksp, *kmem_oversize_ksp;
82*7c478bd9Sstevel@tonic-gate static	kstat_t *file_cache_ksp;
83*7c478bd9Sstevel@tonic-gate static	kstat_named_t *ufs_inode_size_knp, *nproc_knp;
84*7c478bd9Sstevel@tonic-gate static	kstat_named_t *file_total_knp, *file_avail_knp;
85*7c478bd9Sstevel@tonic-gate static	kstat_named_t *oversize_alloc_knp, *oversize_fail_knp;
86*7c478bd9Sstevel@tonic-gate static	int slab_create_index, slab_destroy_index, slab_size_index;
87*7c478bd9Sstevel@tonic-gate static	int buf_size_index, buf_avail_index, alloc_fail_index;
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate static	struct	iodevinfo zeroiodev = { NULL, NULL };
90*7c478bd9Sstevel@tonic-gate static	struct	iodevinfo *firstiodev = NULL;
91*7c478bd9Sstevel@tonic-gate static	struct	iodevinfo *lastiodev = NULL;
92*7c478bd9Sstevel@tonic-gate static	struct	iodevinfo *snip = NULL;
93*7c478bd9Sstevel@tonic-gate static	ulong_t	niodevs;
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate static	void	all_stat_init(void);
96*7c478bd9Sstevel@tonic-gate static	int	all_stat_load(void);
97*7c478bd9Sstevel@tonic-gate static	void	fail(int, char *, ...);
98*7c478bd9Sstevel@tonic-gate static	void	safe_zalloc(void **, int, int);
99*7c478bd9Sstevel@tonic-gate static	kid_t	safe_kstat_read(kstat_ctl_t *, kstat_t *, void *);
100*7c478bd9Sstevel@tonic-gate static	kstat_t	*safe_kstat_lookup(kstat_ctl_t *, char *, int, char *);
101*7c478bd9Sstevel@tonic-gate static	void	*safe_kstat_data_lookup(kstat_t *, char *);
102*7c478bd9Sstevel@tonic-gate static	int	safe_kstat_data_index(kstat_t *, char *);
103*7c478bd9Sstevel@tonic-gate static	void	init_iodevs(void);
104*7c478bd9Sstevel@tonic-gate static	int	iodevinfo_load(void);
105*7c478bd9Sstevel@tonic-gate static	int	kstat_copy(const kstat_t *, kstat_t *);
106*7c478bd9Sstevel@tonic-gate static	void	diff_two_arrays(kstat_t ** const [], size_t, size_t,
107*7c478bd9Sstevel@tonic-gate     kstat_t ** const []);
108*7c478bd9Sstevel@tonic-gate static	void	compute_cpu_stat_adj(void);
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate static	char	*cmdname = "sadc";
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate static	struct var var;
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate static	struct sa d;
115*7c478bd9Sstevel@tonic-gate static	int64_t	cpu_stat_adj[CPU_STATES] = {0};
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate static	long	ninode;
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate int
120*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate 	int ct;
123*7c478bd9Sstevel@tonic-gate 	unsigned ti;
124*7c478bd9Sstevel@tonic-gate 	int fp;
125*7c478bd9Sstevel@tonic-gate 	time_t min;
126*7c478bd9Sstevel@tonic-gate 	struct stat buf;
127*7c478bd9Sstevel@tonic-gate 	char *fname;
128*7c478bd9Sstevel@tonic-gate 	struct iodevinfo *iodev;
129*7c478bd9Sstevel@tonic-gate 	off_t flength;
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	ct = argc >= 3? atoi(argv[2]): 0;
132*7c478bd9Sstevel@tonic-gate 	min = time((time_t *)0);
133*7c478bd9Sstevel@tonic-gate 	ti = argc >= 3? atoi(argv[1]): 0;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	if ((kc = kstat_open()) == NULL)
136*7c478bd9Sstevel@tonic-gate 		fail(1, "kstat_open(): can't open /dev/kstat");
137*7c478bd9Sstevel@tonic-gate 	all_stat_init();
138*7c478bd9Sstevel@tonic-gate 	init_iodevs();
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	if (argc == 3 || argc == 1) {
141*7c478bd9Sstevel@tonic-gate 		/*
142*7c478bd9Sstevel@tonic-gate 		 * no data file is specified, direct data to stdout.
143*7c478bd9Sstevel@tonic-gate 		 */
144*7c478bd9Sstevel@tonic-gate 		fp = 1;
145*7c478bd9Sstevel@tonic-gate 	} else {
146*7c478bd9Sstevel@tonic-gate 		struct flock lock;
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 		fname = (argc == 2) ? argv[1] : argv[3];
149*7c478bd9Sstevel@tonic-gate 		/*
150*7c478bd9Sstevel@tonic-gate 		 * Open or Create a data file. If the file doesn't exist, then
151*7c478bd9Sstevel@tonic-gate 		 * it will be created.
152*7c478bd9Sstevel@tonic-gate 		 */
153*7c478bd9Sstevel@tonic-gate 		if ((fp = open(fname, O_WRONLY | O_APPEND | O_CREAT, 0644))
154*7c478bd9Sstevel@tonic-gate 		    == -1)
155*7c478bd9Sstevel@tonic-gate 			fail(1, "can't open data file");
156*7c478bd9Sstevel@tonic-gate 		/*
157*7c478bd9Sstevel@tonic-gate 		 * Lock the entire data file to prevent data corruption
158*7c478bd9Sstevel@tonic-gate 		 */
159*7c478bd9Sstevel@tonic-gate 		lock.l_type = F_WRLCK;
160*7c478bd9Sstevel@tonic-gate 		lock.l_whence = SEEK_SET;
161*7c478bd9Sstevel@tonic-gate 		lock.l_start = 0;
162*7c478bd9Sstevel@tonic-gate 		lock.l_len = 0;
163*7c478bd9Sstevel@tonic-gate 		if (fcntl(fp, F_SETLK, &lock) == -1)
164*7c478bd9Sstevel@tonic-gate 			fail(1, "can't lock data file");
165*7c478bd9Sstevel@tonic-gate 		/*
166*7c478bd9Sstevel@tonic-gate 		 * Get data file statistics for use in determining whether
167*7c478bd9Sstevel@tonic-gate 		 * truncation required and where rollback recovery should
168*7c478bd9Sstevel@tonic-gate 		 * be applied.
169*7c478bd9Sstevel@tonic-gate 		 */
170*7c478bd9Sstevel@tonic-gate 		if (fstat(fp, &buf) == -1)
171*7c478bd9Sstevel@tonic-gate 			fail(1, "can't get data file information");
172*7c478bd9Sstevel@tonic-gate 		/*
173*7c478bd9Sstevel@tonic-gate 		 * If the data file was opened and is too old, truncate it
174*7c478bd9Sstevel@tonic-gate 		 */
175*7c478bd9Sstevel@tonic-gate 		if (min - buf.st_mtime > 86400)
176*7c478bd9Sstevel@tonic-gate 			if (ftruncate(fp, 0) == -1)
177*7c478bd9Sstevel@tonic-gate 				fail(1, "can't truncate data file");
178*7c478bd9Sstevel@tonic-gate 		/*
179*7c478bd9Sstevel@tonic-gate 		 * Remember filesize for rollback on error (bug #1223549)
180*7c478bd9Sstevel@tonic-gate 		 */
181*7c478bd9Sstevel@tonic-gate 		flength = buf.st_size;
182*7c478bd9Sstevel@tonic-gate 	}
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	memset(&d, 0, sizeof (d));
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	/*
187*7c478bd9Sstevel@tonic-gate 	 * If n == 0, write the additional dummy record.
188*7c478bd9Sstevel@tonic-gate 	 */
189*7c478bd9Sstevel@tonic-gate 	if (ct == 0) {
190*7c478bd9Sstevel@tonic-gate 		d.valid = 0;
191*7c478bd9Sstevel@tonic-gate 		d.ts = min;
192*7c478bd9Sstevel@tonic-gate 		d.niodevs = niodevs;
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 		if (write(fp, &d, sizeof (struct sa)) != sizeof (struct sa))
195*7c478bd9Sstevel@tonic-gate 			ftruncate(fp, flength), fail(1, "write failed");
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 		for (iodev = firstiodev; iodev; iodev = iodev->next) {
198*7c478bd9Sstevel@tonic-gate 			if (write(fp, iodev, sizeof (struct iodevinfo)) !=
199*7c478bd9Sstevel@tonic-gate 			    sizeof (struct iodevinfo))
200*7c478bd9Sstevel@tonic-gate 				ftruncate(fp, flength), fail(1, "write failed");
201*7c478bd9Sstevel@tonic-gate 		}
202*7c478bd9Sstevel@tonic-gate 	}
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	for (;;) {
205*7c478bd9Sstevel@tonic-gate 		do {
206*7c478bd9Sstevel@tonic-gate 			(void) kstat_chain_update(kc);
207*7c478bd9Sstevel@tonic-gate 			all_stat_init();
208*7c478bd9Sstevel@tonic-gate 			init_iodevs();
209*7c478bd9Sstevel@tonic-gate 		} while (all_stat_load() || iodevinfo_load());
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 		d.ts = time((time_t *)0);
212*7c478bd9Sstevel@tonic-gate 		d.valid = 1;
213*7c478bd9Sstevel@tonic-gate 		d.niodevs = niodevs;
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 		if (write(fp, &d, sizeof (struct sa)) != sizeof (struct sa))
216*7c478bd9Sstevel@tonic-gate 			ftruncate(fp, flength), fail(1, "write failed");
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 		for (iodev = firstiodev; iodev; iodev = iodev->next) {
219*7c478bd9Sstevel@tonic-gate 			if (write(fp, iodev, sizeof (struct iodevinfo)) !=
220*7c478bd9Sstevel@tonic-gate 			    sizeof (struct iodevinfo))
221*7c478bd9Sstevel@tonic-gate 				ftruncate(fp, flength), fail(1, "write failed");
222*7c478bd9Sstevel@tonic-gate 		}
223*7c478bd9Sstevel@tonic-gate 		if (--ct > 0) {
224*7c478bd9Sstevel@tonic-gate 			sleep(ti);
225*7c478bd9Sstevel@tonic-gate 		} else {
226*7c478bd9Sstevel@tonic-gate 			close(fp);
227*7c478bd9Sstevel@tonic-gate 			return (0);
228*7c478bd9Sstevel@tonic-gate 		}
229*7c478bd9Sstevel@tonic-gate 	}
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
232*7c478bd9Sstevel@tonic-gate }
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate /*
235*7c478bd9Sstevel@tonic-gate  * Get various KIDs for subsequent all_stat_load operations.
236*7c478bd9Sstevel@tonic-gate  */
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate static void
239*7c478bd9Sstevel@tonic-gate all_stat_init(void)
240*7c478bd9Sstevel@tonic-gate {
241*7c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 	/*
244*7c478bd9Sstevel@tonic-gate 	 * Initialize global statistics
245*7c478bd9Sstevel@tonic-gate 	 */
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 	sysinfo_ksp	= safe_kstat_lookup(kc, "unix", 0, "sysinfo");
248*7c478bd9Sstevel@tonic-gate 	vminfo_ksp	= safe_kstat_lookup(kc, "unix", 0, "vminfo");
249*7c478bd9Sstevel@tonic-gate 	kmem_oversize_ksp = safe_kstat_lookup(kc, "vmem", -1, "kmem_oversize");
250*7c478bd9Sstevel@tonic-gate 	var_ksp		= safe_kstat_lookup(kc, "unix", 0, "var");
251*7c478bd9Sstevel@tonic-gate 	system_misc_ksp	= safe_kstat_lookup(kc, "unix", 0, "system_misc");
252*7c478bd9Sstevel@tonic-gate 	file_cache_ksp	= safe_kstat_lookup(kc, "unix", 0, "file_cache");
253*7c478bd9Sstevel@tonic-gate 	ufs_inode_ksp	= kstat_lookup(kc, "ufs", 0, "inode_cache");
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	safe_kstat_read(kc, system_misc_ksp, NULL);
256*7c478bd9Sstevel@tonic-gate 	nproc_knp	= safe_kstat_data_lookup(system_misc_ksp, "nproc");
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 	safe_kstat_read(kc, file_cache_ksp, NULL);
259*7c478bd9Sstevel@tonic-gate 	file_avail_knp = safe_kstat_data_lookup(file_cache_ksp, "buf_avail");
260*7c478bd9Sstevel@tonic-gate 	file_total_knp = safe_kstat_data_lookup(file_cache_ksp, "buf_total");
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	safe_kstat_read(kc, kmem_oversize_ksp, NULL);
263*7c478bd9Sstevel@tonic-gate 	oversize_alloc_knp = safe_kstat_data_lookup(kmem_oversize_ksp,
264*7c478bd9Sstevel@tonic-gate 	    "mem_total");
265*7c478bd9Sstevel@tonic-gate 	oversize_fail_knp = safe_kstat_data_lookup(kmem_oversize_ksp, "fail");
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate 	if (ufs_inode_ksp != NULL) {
268*7c478bd9Sstevel@tonic-gate 		safe_kstat_read(kc, ufs_inode_ksp, NULL);
269*7c478bd9Sstevel@tonic-gate 		ufs_inode_size_knp = safe_kstat_data_lookup(ufs_inode_ksp,
270*7c478bd9Sstevel@tonic-gate 			"size");
271*7c478bd9Sstevel@tonic-gate 		ninode = ((kstat_named_t *)
272*7c478bd9Sstevel@tonic-gate 			safe_kstat_data_lookup(ufs_inode_ksp,
273*7c478bd9Sstevel@tonic-gate 			"maxsize"))->value.l;
274*7c478bd9Sstevel@tonic-gate 	}
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate 	/*
277*7c478bd9Sstevel@tonic-gate 	 * Load constant values now -- no need to reread each time
278*7c478bd9Sstevel@tonic-gate 	 */
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 	safe_kstat_read(kc, var_ksp, (void *) &var);
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 	/*
283*7c478bd9Sstevel@tonic-gate 	 * Initialize per-CPU and per-kmem-cache statistics
284*7c478bd9Sstevel@tonic-gate 	 */
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 	ncpus = ncaches = 0;
287*7c478bd9Sstevel@tonic-gate 	for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
288*7c478bd9Sstevel@tonic-gate 		if (strncmp(ksp->ks_name, "cpu_stat", 8) == 0)
289*7c478bd9Sstevel@tonic-gate 			ncpus++;
290*7c478bd9Sstevel@tonic-gate 		if (strcmp(ksp->ks_class, "kmem_cache") == 0)
291*7c478bd9Sstevel@tonic-gate 			ncaches++;
292*7c478bd9Sstevel@tonic-gate 	}
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 	safe_zalloc((void **)&cpu_stat_list, ncpus * sizeof (kstat_t *), 1);
295*7c478bd9Sstevel@tonic-gate 	safe_zalloc((void **)&kmem_cache_list, ncaches * sizeof (kstat_t *), 1);
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate 	ncpus = ncaches = 0;
298*7c478bd9Sstevel@tonic-gate 	for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
299*7c478bd9Sstevel@tonic-gate 		if (strncmp(ksp->ks_name, "cpu_stat", 8) == 0 &&
300*7c478bd9Sstevel@tonic-gate 		    kstat_read(kc, ksp, NULL) != -1)
301*7c478bd9Sstevel@tonic-gate 			cpu_stat_list[ncpus++] = ksp;
302*7c478bd9Sstevel@tonic-gate 		if (strcmp(ksp->ks_class, "kmem_cache") == 0 &&
303*7c478bd9Sstevel@tonic-gate 		    kstat_read(kc, ksp, NULL) != -1)
304*7c478bd9Sstevel@tonic-gate 			kmem_cache_list[ncaches++] = ksp;
305*7c478bd9Sstevel@tonic-gate 	}
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate 	if (ncpus == 0)
308*7c478bd9Sstevel@tonic-gate 		fail(1, "can't find any cpu statistics");
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate 	if (ncaches == 0)
311*7c478bd9Sstevel@tonic-gate 		fail(1, "can't find any kmem_cache statistics");
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate 	ksp = kmem_cache_list[0];
314*7c478bd9Sstevel@tonic-gate 	safe_kstat_read(kc, ksp, NULL);
315*7c478bd9Sstevel@tonic-gate 	buf_size_index = safe_kstat_data_index(ksp, "buf_size");
316*7c478bd9Sstevel@tonic-gate 	slab_create_index = safe_kstat_data_index(ksp, "slab_create");
317*7c478bd9Sstevel@tonic-gate 	slab_destroy_index = safe_kstat_data_index(ksp, "slab_destroy");
318*7c478bd9Sstevel@tonic-gate 	slab_size_index = safe_kstat_data_index(ksp, "slab_size");
319*7c478bd9Sstevel@tonic-gate 	buf_avail_index = safe_kstat_data_index(ksp, "buf_avail");
320*7c478bd9Sstevel@tonic-gate 	alloc_fail_index = safe_kstat_data_index(ksp, "alloc_fail");
321*7c478bd9Sstevel@tonic-gate }
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate /*
324*7c478bd9Sstevel@tonic-gate  * load statistics, summing across CPUs where needed
325*7c478bd9Sstevel@tonic-gate  */
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate static int
328*7c478bd9Sstevel@tonic-gate all_stat_load(void)
329*7c478bd9Sstevel@tonic-gate {
330*7c478bd9Sstevel@tonic-gate 	int i, j;
331*7c478bd9Sstevel@tonic-gate 	cpu_stat_t cs;
332*7c478bd9Sstevel@tonic-gate 	ulong_t *np, *tp;
333*7c478bd9Sstevel@tonic-gate 	uint64_t cpu_tick[4] = {0, 0, 0, 0};
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate 	memset(&d, 0, sizeof (d));
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate 	/*
338*7c478bd9Sstevel@tonic-gate 	 * Global statistics
339*7c478bd9Sstevel@tonic-gate 	 */
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 	safe_kstat_read(kc, sysinfo_ksp, (void *) &d.si);
342*7c478bd9Sstevel@tonic-gate 	safe_kstat_read(kc, vminfo_ksp, (void *) &d.vmi);
343*7c478bd9Sstevel@tonic-gate 	safe_kstat_read(kc, system_misc_ksp, NULL);
344*7c478bd9Sstevel@tonic-gate 	safe_kstat_read(kc, file_cache_ksp, NULL);
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate 	if (ufs_inode_ksp != NULL) {
347*7c478bd9Sstevel@tonic-gate 		safe_kstat_read(kc, ufs_inode_ksp, NULL);
348*7c478bd9Sstevel@tonic-gate 		d.szinode = ufs_inode_size_knp->value.ul;
349*7c478bd9Sstevel@tonic-gate 	}
350*7c478bd9Sstevel@tonic-gate 
351*7c478bd9Sstevel@tonic-gate 	d.szfile = file_total_knp->value.ui64 - file_avail_knp->value.ui64;
352*7c478bd9Sstevel@tonic-gate 	d.szproc = nproc_knp->value.ul;
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate 	d.mszinode = (ninode > d.szinode) ? ninode : d.szinode;
355*7c478bd9Sstevel@tonic-gate 	d.mszfile = d.szfile;
356*7c478bd9Sstevel@tonic-gate 	d.mszproc = var.v_proc;
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate 	/*
359*7c478bd9Sstevel@tonic-gate 	 * Per-CPU statistics.
360*7c478bd9Sstevel@tonic-gate 	 */
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncpus; i++) {
363*7c478bd9Sstevel@tonic-gate 		if (kstat_read(kc, cpu_stat_list[i], (void *) &cs) == -1)
364*7c478bd9Sstevel@tonic-gate 			return (1);
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate 		np = (ulong_t *)&d.csi;
367*7c478bd9Sstevel@tonic-gate 		tp = (ulong_t *)&cs.cpu_sysinfo;
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate 		/*
370*7c478bd9Sstevel@tonic-gate 		 * Accumulate cpu ticks for CPU_IDLE, CPU_USER, CPU_KERNEL and
371*7c478bd9Sstevel@tonic-gate 		 * CPU_WAIT with respect to each of the cpus.
372*7c478bd9Sstevel@tonic-gate 		 */
373*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < CPU_STATES; j++)
374*7c478bd9Sstevel@tonic-gate 			cpu_tick[j] += tp[j];
375*7c478bd9Sstevel@tonic-gate 
376*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < sizeof (cpu_sysinfo_t); j += sizeof (ulong_t))
377*7c478bd9Sstevel@tonic-gate 			*np++ += *tp++;
378*7c478bd9Sstevel@tonic-gate 		np = (ulong_t *)&d.cvmi;
379*7c478bd9Sstevel@tonic-gate 		tp = (ulong_t *)&cs.cpu_vminfo;
380*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < sizeof (cpu_vminfo_t); j += sizeof (ulong_t))
381*7c478bd9Sstevel@tonic-gate 			*np++ += *tp++;
382*7c478bd9Sstevel@tonic-gate 	}
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate 	/*
385*7c478bd9Sstevel@tonic-gate 	 * Per-cache kmem statistics.
386*7c478bd9Sstevel@tonic-gate 	 */
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncaches; i++) {
389*7c478bd9Sstevel@tonic-gate 		kstat_named_t *knp;
390*7c478bd9Sstevel@tonic-gate 		u_longlong_t slab_create, slab_destroy, slab_size, mem_total;
391*7c478bd9Sstevel@tonic-gate 		u_longlong_t buf_size, buf_avail, alloc_fail;
392*7c478bd9Sstevel@tonic-gate 		int kmi_index;
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate 		if (kstat_read(kc, kmem_cache_list[i], NULL) == -1)
395*7c478bd9Sstevel@tonic-gate 			return (1);
396*7c478bd9Sstevel@tonic-gate 		knp = kmem_cache_list[i]->ks_data;
397*7c478bd9Sstevel@tonic-gate 		slab_create	= knp[slab_create_index].value.ui64;
398*7c478bd9Sstevel@tonic-gate 		slab_destroy	= knp[slab_destroy_index].value.ui64;
399*7c478bd9Sstevel@tonic-gate 		slab_size	= knp[slab_size_index].value.ui64;
400*7c478bd9Sstevel@tonic-gate 		buf_size	= knp[buf_size_index].value.ui64;
401*7c478bd9Sstevel@tonic-gate 		buf_avail	= knp[buf_avail_index].value.ui64;
402*7c478bd9Sstevel@tonic-gate 		alloc_fail	= knp[alloc_fail_index].value.ui64;
403*7c478bd9Sstevel@tonic-gate 		if (buf_size <= 256)
404*7c478bd9Sstevel@tonic-gate 			kmi_index = KMEM_SMALL;
405*7c478bd9Sstevel@tonic-gate 		else
406*7c478bd9Sstevel@tonic-gate 			kmi_index = KMEM_LARGE;
407*7c478bd9Sstevel@tonic-gate 		mem_total = (slab_create - slab_destroy) * slab_size;
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate 		d.kmi.km_mem[kmi_index] += (ulong_t)mem_total;
410*7c478bd9Sstevel@tonic-gate 		d.kmi.km_alloc[kmi_index] +=
411*7c478bd9Sstevel@tonic-gate 			(ulong_t)mem_total - buf_size * buf_avail;
412*7c478bd9Sstevel@tonic-gate 		d.kmi.km_fail[kmi_index] += (ulong_t)alloc_fail;
413*7c478bd9Sstevel@tonic-gate 	}
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 	safe_kstat_read(kc, kmem_oversize_ksp, NULL);
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate 	d.kmi.km_alloc[KMEM_OSIZE] = d.kmi.km_mem[KMEM_OSIZE] =
418*7c478bd9Sstevel@tonic-gate 		oversize_alloc_knp->value.ui64;
419*7c478bd9Sstevel@tonic-gate 	d.kmi.km_fail[KMEM_OSIZE] = oversize_fail_knp->value.ui64;
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate 	/*
422*7c478bd9Sstevel@tonic-gate 	 * Adjust CPU statistics so the delta calculations in sar will
423*7c478bd9Sstevel@tonic-gate 	 * be correct when facing changes to the set of online CPUs.
424*7c478bd9Sstevel@tonic-gate 	 */
425*7c478bd9Sstevel@tonic-gate 	compute_cpu_stat_adj();
426*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < CPU_STATES; i++)
427*7c478bd9Sstevel@tonic-gate 		d.csi.cpu[i] = cpu_tick[i] + cpu_stat_adj[i];
428*7c478bd9Sstevel@tonic-gate 
429*7c478bd9Sstevel@tonic-gate 	return (0);
430*7c478bd9Sstevel@tonic-gate }
431*7c478bd9Sstevel@tonic-gate 
432*7c478bd9Sstevel@tonic-gate static void
433*7c478bd9Sstevel@tonic-gate fail(int do_perror, char *message, ...)
434*7c478bd9Sstevel@tonic-gate {
435*7c478bd9Sstevel@tonic-gate 	va_list args;
436*7c478bd9Sstevel@tonic-gate 
437*7c478bd9Sstevel@tonic-gate 	va_start(args, message);
438*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s: ", cmdname);
439*7c478bd9Sstevel@tonic-gate 	vfprintf(stderr, message, args);
440*7c478bd9Sstevel@tonic-gate 	va_end(args);
441*7c478bd9Sstevel@tonic-gate 	if (do_perror)
442*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, ": %s", strerror(errno));
443*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\n");
444*7c478bd9Sstevel@tonic-gate 	exit(2);
445*7c478bd9Sstevel@tonic-gate }
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate static void
448*7c478bd9Sstevel@tonic-gate safe_zalloc(void **ptr, int size, int free_first)
449*7c478bd9Sstevel@tonic-gate {
450*7c478bd9Sstevel@tonic-gate 	if (free_first && *ptr != NULL)
451*7c478bd9Sstevel@tonic-gate 		free(*ptr);
452*7c478bd9Sstevel@tonic-gate 	if ((*ptr = malloc(size)) == NULL)
453*7c478bd9Sstevel@tonic-gate 		fail(1, "malloc failed");
454*7c478bd9Sstevel@tonic-gate 	memset(*ptr, 0, size);
455*7c478bd9Sstevel@tonic-gate }
456*7c478bd9Sstevel@tonic-gate 
457*7c478bd9Sstevel@tonic-gate static kid_t
458*7c478bd9Sstevel@tonic-gate safe_kstat_read(kstat_ctl_t *kc, kstat_t *ksp, void *data)
459*7c478bd9Sstevel@tonic-gate {
460*7c478bd9Sstevel@tonic-gate 	kid_t kstat_chain_id = kstat_read(kc, ksp, data);
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate 	if (kstat_chain_id == -1)
463*7c478bd9Sstevel@tonic-gate 		fail(1, "kstat_read(%x, '%s') failed", kc, ksp->ks_name);
464*7c478bd9Sstevel@tonic-gate 	return (kstat_chain_id);
465*7c478bd9Sstevel@tonic-gate }
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate static kstat_t *
468*7c478bd9Sstevel@tonic-gate safe_kstat_lookup(kstat_ctl_t *kc, char *ks_module, int ks_instance,
469*7c478bd9Sstevel@tonic-gate 	char *ks_name)
470*7c478bd9Sstevel@tonic-gate {
471*7c478bd9Sstevel@tonic-gate 	kstat_t *ksp = kstat_lookup(kc, ks_module, ks_instance, ks_name);
472*7c478bd9Sstevel@tonic-gate 
473*7c478bd9Sstevel@tonic-gate 	if (ksp == NULL)
474*7c478bd9Sstevel@tonic-gate 		fail(0, "kstat_lookup('%s', %d, '%s') failed",
475*7c478bd9Sstevel@tonic-gate 			ks_module == NULL ? "" : ks_module,
476*7c478bd9Sstevel@tonic-gate 			ks_instance,
477*7c478bd9Sstevel@tonic-gate 			ks_name == NULL ? "" : ks_name);
478*7c478bd9Sstevel@tonic-gate 	return (ksp);
479*7c478bd9Sstevel@tonic-gate }
480*7c478bd9Sstevel@tonic-gate 
481*7c478bd9Sstevel@tonic-gate static void *
482*7c478bd9Sstevel@tonic-gate safe_kstat_data_lookup(kstat_t *ksp, char *name)
483*7c478bd9Sstevel@tonic-gate {
484*7c478bd9Sstevel@tonic-gate 	void *fp = kstat_data_lookup(ksp, name);
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate 	if (fp == NULL)
487*7c478bd9Sstevel@tonic-gate 		fail(0, "kstat_data_lookup('%s', '%s') failed",
488*7c478bd9Sstevel@tonic-gate 			ksp->ks_name, name);
489*7c478bd9Sstevel@tonic-gate 	return (fp);
490*7c478bd9Sstevel@tonic-gate }
491*7c478bd9Sstevel@tonic-gate 
492*7c478bd9Sstevel@tonic-gate static int
493*7c478bd9Sstevel@tonic-gate safe_kstat_data_index(kstat_t *ksp, char *name)
494*7c478bd9Sstevel@tonic-gate {
495*7c478bd9Sstevel@tonic-gate 	return ((int)((char *)safe_kstat_data_lookup(ksp, name) -
496*7c478bd9Sstevel@tonic-gate 		(char *)ksp->ks_data) / (ksp->ks_data_size / ksp->ks_ndata));
497*7c478bd9Sstevel@tonic-gate }
498*7c478bd9Sstevel@tonic-gate 
499*7c478bd9Sstevel@tonic-gate static int
500*7c478bd9Sstevel@tonic-gate kscmp(kstat_t *ks1, kstat_t *ks2)
501*7c478bd9Sstevel@tonic-gate {
502*7c478bd9Sstevel@tonic-gate 	int cmp;
503*7c478bd9Sstevel@tonic-gate 
504*7c478bd9Sstevel@tonic-gate 	cmp = strcmp(ks1->ks_module, ks2->ks_module);
505*7c478bd9Sstevel@tonic-gate 	if (cmp != 0)
506*7c478bd9Sstevel@tonic-gate 		return (cmp);
507*7c478bd9Sstevel@tonic-gate 	cmp = ks1->ks_instance - ks2->ks_instance;
508*7c478bd9Sstevel@tonic-gate 	if (cmp != 0)
509*7c478bd9Sstevel@tonic-gate 		return (cmp);
510*7c478bd9Sstevel@tonic-gate 	return (strcmp(ks1->ks_name, ks2->ks_name));
511*7c478bd9Sstevel@tonic-gate }
512*7c478bd9Sstevel@tonic-gate 
513*7c478bd9Sstevel@tonic-gate static void
514*7c478bd9Sstevel@tonic-gate init_iodevs(void)
515*7c478bd9Sstevel@tonic-gate {
516*7c478bd9Sstevel@tonic-gate 	struct iodevinfo *iodev, *previodev, *comp;
517*7c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
518*7c478bd9Sstevel@tonic-gate 
519*7c478bd9Sstevel@tonic-gate 	iodev = &zeroiodev;
520*7c478bd9Sstevel@tonic-gate 	niodevs = 0;
521*7c478bd9Sstevel@tonic-gate 
522*7c478bd9Sstevel@tonic-gate 	/*
523*7c478bd9Sstevel@tonic-gate 	 * Patch the snip in the iodevinfo list (see below)
524*7c478bd9Sstevel@tonic-gate 	 */
525*7c478bd9Sstevel@tonic-gate 	if (snip)
526*7c478bd9Sstevel@tonic-gate 		lastiodev->next = snip;
527*7c478bd9Sstevel@tonic-gate 
528*7c478bd9Sstevel@tonic-gate 	for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
529*7c478bd9Sstevel@tonic-gate 
530*7c478bd9Sstevel@tonic-gate 		if (ksp->ks_type != KSTAT_TYPE_IO)
531*7c478bd9Sstevel@tonic-gate 			continue;
532*7c478bd9Sstevel@tonic-gate 		previodev = iodev;
533*7c478bd9Sstevel@tonic-gate 		if (iodev->next)
534*7c478bd9Sstevel@tonic-gate 			iodev = iodev->next;
535*7c478bd9Sstevel@tonic-gate 		else {
536*7c478bd9Sstevel@tonic-gate 			safe_zalloc((void **) &iodev->next,
537*7c478bd9Sstevel@tonic-gate 				sizeof (struct iodevinfo), 0);
538*7c478bd9Sstevel@tonic-gate 			iodev = iodev->next;
539*7c478bd9Sstevel@tonic-gate 			iodev->next = NULL;
540*7c478bd9Sstevel@tonic-gate 		}
541*7c478bd9Sstevel@tonic-gate 		iodev->ksp = ksp;
542*7c478bd9Sstevel@tonic-gate 		iodev->ks = *ksp;
543*7c478bd9Sstevel@tonic-gate 		memset((void *)&iodev->kios, 0, sizeof (kstat_io_t));
544*7c478bd9Sstevel@tonic-gate 		iodev->kios.wlastupdate = iodev->ks.ks_crtime;
545*7c478bd9Sstevel@tonic-gate 		iodev->kios.rlastupdate = iodev->ks.ks_crtime;
546*7c478bd9Sstevel@tonic-gate 
547*7c478bd9Sstevel@tonic-gate 		/*
548*7c478bd9Sstevel@tonic-gate 		 * Insertion sort on (ks_module, ks_instance, ks_name)
549*7c478bd9Sstevel@tonic-gate 		 */
550*7c478bd9Sstevel@tonic-gate 		comp = &zeroiodev;
551*7c478bd9Sstevel@tonic-gate 		while (kscmp(&iodev->ks, &comp->next->ks) > 0)
552*7c478bd9Sstevel@tonic-gate 			comp = comp->next;
553*7c478bd9Sstevel@tonic-gate 		if (previodev != comp) {
554*7c478bd9Sstevel@tonic-gate 			previodev->next = iodev->next;
555*7c478bd9Sstevel@tonic-gate 			iodev->next = comp->next;
556*7c478bd9Sstevel@tonic-gate 			comp->next = iodev;
557*7c478bd9Sstevel@tonic-gate 			iodev = previodev;
558*7c478bd9Sstevel@tonic-gate 		}
559*7c478bd9Sstevel@tonic-gate 		niodevs++;
560*7c478bd9Sstevel@tonic-gate 	}
561*7c478bd9Sstevel@tonic-gate 	/*
562*7c478bd9Sstevel@tonic-gate 	 * Put a snip in the linked list of iodevinfos.  The idea:
563*7c478bd9Sstevel@tonic-gate 	 * If there was a state change such that now there are fewer
564*7c478bd9Sstevel@tonic-gate 	 * iodevs, we snip the list and retain the tail, rather than
565*7c478bd9Sstevel@tonic-gate 	 * freeing it.  At the next state change, we clip the tail back on.
566*7c478bd9Sstevel@tonic-gate 	 * This prevents a lot of malloc/free activity, and it's simpler.
567*7c478bd9Sstevel@tonic-gate 	 */
568*7c478bd9Sstevel@tonic-gate 	lastiodev = iodev;
569*7c478bd9Sstevel@tonic-gate 	snip = iodev->next;
570*7c478bd9Sstevel@tonic-gate 	iodev->next = NULL;
571*7c478bd9Sstevel@tonic-gate 
572*7c478bd9Sstevel@tonic-gate 	firstiodev = zeroiodev.next;
573*7c478bd9Sstevel@tonic-gate }
574*7c478bd9Sstevel@tonic-gate 
575*7c478bd9Sstevel@tonic-gate static int
576*7c478bd9Sstevel@tonic-gate iodevinfo_load(void)
577*7c478bd9Sstevel@tonic-gate {
578*7c478bd9Sstevel@tonic-gate 	struct iodevinfo *iodev;
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate 	for (iodev = firstiodev; iodev; iodev = iodev->next) {
581*7c478bd9Sstevel@tonic-gate 		if (kstat_read(kc, iodev->ksp, (void *) &iodev->kios) == -1)
582*7c478bd9Sstevel@tonic-gate 			return (1);
583*7c478bd9Sstevel@tonic-gate 	}
584*7c478bd9Sstevel@tonic-gate 	return (0);
585*7c478bd9Sstevel@tonic-gate }
586*7c478bd9Sstevel@tonic-gate 
587*7c478bd9Sstevel@tonic-gate static int
588*7c478bd9Sstevel@tonic-gate kstat_copy(const kstat_t *src, kstat_t *dst)
589*7c478bd9Sstevel@tonic-gate {
590*7c478bd9Sstevel@tonic-gate 	*dst = *src;
591*7c478bd9Sstevel@tonic-gate 
592*7c478bd9Sstevel@tonic-gate 	if (src->ks_data != NULL) {
593*7c478bd9Sstevel@tonic-gate 		if ((dst->ks_data = malloc(src->ks_data_size)) == NULL)
594*7c478bd9Sstevel@tonic-gate 			return (-1);
595*7c478bd9Sstevel@tonic-gate 		bcopy(src->ks_data, dst->ks_data, src->ks_data_size);
596*7c478bd9Sstevel@tonic-gate 	} else {
597*7c478bd9Sstevel@tonic-gate 		dst->ks_data = NULL;
598*7c478bd9Sstevel@tonic-gate 		dst->ks_data_size = 0;
599*7c478bd9Sstevel@tonic-gate 	}
600*7c478bd9Sstevel@tonic-gate 	return (0);
601*7c478bd9Sstevel@tonic-gate }
602*7c478bd9Sstevel@tonic-gate 
603*7c478bd9Sstevel@tonic-gate /*
604*7c478bd9Sstevel@tonic-gate  * Determine what is different between two sets of kstats; s[0] and s[1]
605*7c478bd9Sstevel@tonic-gate  * are arrays of kstats of size ns0 and ns1, respectively, and sorted by
606*7c478bd9Sstevel@tonic-gate  * instance number.  u[0] and u[1] are two arrays which must be
607*7c478bd9Sstevel@tonic-gate  * caller-zallocated; each must be of size MAX(ns0, ns1).  When the
608*7c478bd9Sstevel@tonic-gate  * function terminates, u[0] contains all s[0]-unique items and u[1]
609*7c478bd9Sstevel@tonic-gate  * contains all s[1]-unique items.  Any unused entries in u[0] and u[1]
610*7c478bd9Sstevel@tonic-gate  * are left NULL.
611*7c478bd9Sstevel@tonic-gate  */
612*7c478bd9Sstevel@tonic-gate static void
613*7c478bd9Sstevel@tonic-gate diff_two_arrays(kstat_t ** const s[], size_t ns0, size_t ns1,
614*7c478bd9Sstevel@tonic-gate     kstat_t ** const u[])
615*7c478bd9Sstevel@tonic-gate {
616*7c478bd9Sstevel@tonic-gate 	kstat_t **s0p = s[0], **s1p = s[1];
617*7c478bd9Sstevel@tonic-gate 	kstat_t **u0p = u[0], **u1p = u[1];
618*7c478bd9Sstevel@tonic-gate 	int i = 0, j = 0;
619*7c478bd9Sstevel@tonic-gate 
620*7c478bd9Sstevel@tonic-gate 	while (i < ns0 && j < ns1) {
621*7c478bd9Sstevel@tonic-gate 		if ((*s0p)->ks_instance == (*s1p)->ks_instance) {
622*7c478bd9Sstevel@tonic-gate 			if ((*s0p)->ks_kid != (*s1p)->ks_kid) {
623*7c478bd9Sstevel@tonic-gate 				/*
624*7c478bd9Sstevel@tonic-gate 				 * The instance is the same, but this
625*7c478bd9Sstevel@tonic-gate 				 * CPU has been offline during the
626*7c478bd9Sstevel@tonic-gate 				 * interval, so we consider *u0p to
627*7c478bd9Sstevel@tonic-gate 				 * be s0p-unique, and similarly for
628*7c478bd9Sstevel@tonic-gate 				 * *u1p.
629*7c478bd9Sstevel@tonic-gate 				 */
630*7c478bd9Sstevel@tonic-gate 				*(u0p++) = *s0p;
631*7c478bd9Sstevel@tonic-gate 				*(u1p++) = *s1p;
632*7c478bd9Sstevel@tonic-gate 			}
633*7c478bd9Sstevel@tonic-gate 			s0p++;
634*7c478bd9Sstevel@tonic-gate 			i++;
635*7c478bd9Sstevel@tonic-gate 			s1p++;
636*7c478bd9Sstevel@tonic-gate 			j++;
637*7c478bd9Sstevel@tonic-gate 		} else if ((*s0p)->ks_instance < (*s1p)->ks_instance) {
638*7c478bd9Sstevel@tonic-gate 			*(u0p++) = *(s0p++);
639*7c478bd9Sstevel@tonic-gate 			i++;
640*7c478bd9Sstevel@tonic-gate 		} else {
641*7c478bd9Sstevel@tonic-gate 			*(u1p++) = *(s1p++);
642*7c478bd9Sstevel@tonic-gate 			j++;
643*7c478bd9Sstevel@tonic-gate 		}
644*7c478bd9Sstevel@tonic-gate 	}
645*7c478bd9Sstevel@tonic-gate 
646*7c478bd9Sstevel@tonic-gate 	while (i < ns0) {
647*7c478bd9Sstevel@tonic-gate 		*(u0p++) = *(s0p++);
648*7c478bd9Sstevel@tonic-gate 		i++;
649*7c478bd9Sstevel@tonic-gate 	}
650*7c478bd9Sstevel@tonic-gate 	while (j < ns1) {
651*7c478bd9Sstevel@tonic-gate 		*(u1p++) = *(s1p++);
652*7c478bd9Sstevel@tonic-gate 		j++;
653*7c478bd9Sstevel@tonic-gate 	}
654*7c478bd9Sstevel@tonic-gate }
655*7c478bd9Sstevel@tonic-gate 
656*7c478bd9Sstevel@tonic-gate static int
657*7c478bd9Sstevel@tonic-gate cpuid_compare(const void *p1, const void *p2)
658*7c478bd9Sstevel@tonic-gate {
659*7c478bd9Sstevel@tonic-gate 	return ((*(kstat_t **)p1)->ks_instance -
660*7c478bd9Sstevel@tonic-gate 	    (*(kstat_t **)p2)->ks_instance);
661*7c478bd9Sstevel@tonic-gate }
662*7c478bd9Sstevel@tonic-gate 
663*7c478bd9Sstevel@tonic-gate /*
664*7c478bd9Sstevel@tonic-gate  * Identify those CPUs which were not present for the whole interval so
665*7c478bd9Sstevel@tonic-gate  * their statistics can be removed from the aggregate.
666*7c478bd9Sstevel@tonic-gate  */
667*7c478bd9Sstevel@tonic-gate static void
668*7c478bd9Sstevel@tonic-gate compute_cpu_stat_adj(void)
669*7c478bd9Sstevel@tonic-gate {
670*7c478bd9Sstevel@tonic-gate 	int i, j;
671*7c478bd9Sstevel@tonic-gate 
672*7c478bd9Sstevel@tonic-gate 	if (ocpu_stat_list) {
673*7c478bd9Sstevel@tonic-gate 		kstat_t **s[2];
674*7c478bd9Sstevel@tonic-gate 		kstat_t **inarray[2];
675*7c478bd9Sstevel@tonic-gate 		int max_cpus = MAX(ncpus, oncpus);
676*7c478bd9Sstevel@tonic-gate 
677*7c478bd9Sstevel@tonic-gate 		qsort(cpu_stat_list, ncpus, sizeof (*cpu_stat_list),
678*7c478bd9Sstevel@tonic-gate 		    cpuid_compare);
679*7c478bd9Sstevel@tonic-gate 		qsort(ocpu_stat_list, oncpus, sizeof (*ocpu_stat_list),
680*7c478bd9Sstevel@tonic-gate 		    cpuid_compare);
681*7c478bd9Sstevel@tonic-gate 
682*7c478bd9Sstevel@tonic-gate 		s[0] = ocpu_stat_list;
683*7c478bd9Sstevel@tonic-gate 		s[1] = cpu_stat_list;
684*7c478bd9Sstevel@tonic-gate 
685*7c478bd9Sstevel@tonic-gate 		safe_zalloc((void *)&inarray[0], sizeof (**inarray) * max_cpus,
686*7c478bd9Sstevel@tonic-gate 		    0);
687*7c478bd9Sstevel@tonic-gate 		safe_zalloc((void *)&inarray[1], sizeof (**inarray) * max_cpus,
688*7c478bd9Sstevel@tonic-gate 		    0);
689*7c478bd9Sstevel@tonic-gate 		diff_two_arrays(s, oncpus, ncpus, inarray);
690*7c478bd9Sstevel@tonic-gate 
691*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < max_cpus; i++) {
692*7c478bd9Sstevel@tonic-gate 			if (inarray[0][i])
693*7c478bd9Sstevel@tonic-gate 				for (j = 0; j < CPU_STATES; j++)
694*7c478bd9Sstevel@tonic-gate 					cpu_stat_adj[j] +=
695*7c478bd9Sstevel@tonic-gate 					    ((cpu_stat_t *)inarray[0][i]
696*7c478bd9Sstevel@tonic-gate 					    ->ks_data)->cpu_sysinfo.cpu[j];
697*7c478bd9Sstevel@tonic-gate 			if (inarray[1][i])
698*7c478bd9Sstevel@tonic-gate 				for (j = 0; j < CPU_STATES; j++)
699*7c478bd9Sstevel@tonic-gate 					cpu_stat_adj[j] -=
700*7c478bd9Sstevel@tonic-gate 					    ((cpu_stat_t *)inarray[1][i]
701*7c478bd9Sstevel@tonic-gate 					    ->ks_data)->cpu_sysinfo.cpu[j];
702*7c478bd9Sstevel@tonic-gate 		}
703*7c478bd9Sstevel@tonic-gate 
704*7c478bd9Sstevel@tonic-gate 		free(inarray[0]);
705*7c478bd9Sstevel@tonic-gate 		free(inarray[1]);
706*7c478bd9Sstevel@tonic-gate 	}
707*7c478bd9Sstevel@tonic-gate 
708*7c478bd9Sstevel@tonic-gate 	/*
709*7c478bd9Sstevel@tonic-gate 	 * Preserve the last interval's CPU stats.
710*7c478bd9Sstevel@tonic-gate 	 */
711*7c478bd9Sstevel@tonic-gate 	if (cpu_stat_list) {
712*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < oncpus; i++)
713*7c478bd9Sstevel@tonic-gate 			free(ocpu_stat_list[i]->ks_data);
714*7c478bd9Sstevel@tonic-gate 
715*7c478bd9Sstevel@tonic-gate 		oncpus = ncpus;
716*7c478bd9Sstevel@tonic-gate 		safe_zalloc((void **)&ocpu_stat_list, oncpus *
717*7c478bd9Sstevel@tonic-gate 		    sizeof (*ocpu_stat_list), 1);
718*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < ncpus; i++) {
719*7c478bd9Sstevel@tonic-gate 			safe_zalloc((void *)&ocpu_stat_list[i],
720*7c478bd9Sstevel@tonic-gate 			    sizeof (*ocpu_stat_list[0]), 0);
721*7c478bd9Sstevel@tonic-gate 			if (kstat_copy(cpu_stat_list[i], ocpu_stat_list[i]))
722*7c478bd9Sstevel@tonic-gate 				fail(1, "kstat_copy() failed");
723*7c478bd9Sstevel@tonic-gate 		}
724*7c478bd9Sstevel@tonic-gate 	}
725*7c478bd9Sstevel@tonic-gate }
726