xref: /onnv-gate/usr/src/cmd/busstat/busstat.c (revision 0:68f95e015346)
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 2004 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 <stdio.h>
30*0Sstevel@tonic-gate #include <stdlib.h>
31*0Sstevel@tonic-gate #include <strings.h>
32*0Sstevel@tonic-gate #include <time.h>
33*0Sstevel@tonic-gate #include <signal.h>
34*0Sstevel@tonic-gate #include <sys/types.h>
35*0Sstevel@tonic-gate #include <sys/stat.h>
36*0Sstevel@tonic-gate #include <sys/time.h>
37*0Sstevel@tonic-gate #include <sys/modctl.h>
38*0Sstevel@tonic-gate #include <sys/systeminfo.h>
39*0Sstevel@tonic-gate #include <limits.h>
40*0Sstevel@tonic-gate #include <signal.h>
41*0Sstevel@tonic-gate #include <fcntl.h>
42*0Sstevel@tonic-gate #include <unistd.h>
43*0Sstevel@tonic-gate #include <stropts.h>
44*0Sstevel@tonic-gate #include <locale.h>
45*0Sstevel@tonic-gate #include <libintl.h>
46*0Sstevel@tonic-gate #include <libgen.h>
47*0Sstevel@tonic-gate #include <nl_types.h>
48*0Sstevel@tonic-gate #include <kstat.h>
49*0Sstevel@tonic-gate #include <ctype.h>
50*0Sstevel@tonic-gate #include <signal.h>
51*0Sstevel@tonic-gate #include <errno.h>
52*0Sstevel@tonic-gate #include <time.h>
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate #include "busstat.h"
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate /* Global defines */
58*0Sstevel@tonic-gate static int		delta = TRUE;
59*0Sstevel@tonic-gate static int		banner = TRUE;
60*0Sstevel@tonic-gate static int		max_pic_num = 1;
61*0Sstevel@tonic-gate static int		initial_read = TRUE;
62*0Sstevel@tonic-gate static char		*pgmname;
63*0Sstevel@tonic-gate static kstat_ctl_t	*kc;			/* libkstat cookie */
64*0Sstevel@tonic-gate static dev_node_t	*dev_list_head	= NULL;
65*0Sstevel@tonic-gate static dev_node_t	*dev_list_tail	= NULL;
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate /*
68*0Sstevel@tonic-gate  * Global flags.
69*0Sstevel@tonic-gate  */
70*0Sstevel@tonic-gate static char	curr_dev_name[KSTAT_STRLEN];
71*0Sstevel@tonic-gate static int	curr_inst_num;
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate static void print_evt(void);
74*0Sstevel@tonic-gate static void print_dev(int, char *);
75*0Sstevel@tonic-gate static void parse_cmd(int);
76*0Sstevel@tonic-gate static void parse_dev_inst(char *);
77*0Sstevel@tonic-gate static void parse_pic_evt(char *);
78*0Sstevel@tonic-gate static void add_dev_node(char *, int);
79*0Sstevel@tonic-gate static void add_all_dev_node(char *);
80*0Sstevel@tonic-gate static void add_evt_node(dev_node_t *);
81*0Sstevel@tonic-gate static void modify_evt_node(dev_node_t *, char *);
82*0Sstevel@tonic-gate static void prune_evt_nodes(dev_node_t *);
83*0Sstevel@tonic-gate static void setup_evts(void);
84*0Sstevel@tonic-gate static void set_evt(dev_node_t *);
85*0Sstevel@tonic-gate static void read_evts(void);
86*0Sstevel@tonic-gate static void read_r_evt_node(dev_node_t *, int, kstat_named_t *);
87*0Sstevel@tonic-gate static void read_w_evt_node(dev_node_t *, int, kstat_named_t *);
88*0Sstevel@tonic-gate static void check_dr_ops(void);
89*0Sstevel@tonic-gate static void remove_dev_node(dev_node_t *);
90*0Sstevel@tonic-gate static dev_node_t *find_dev_node(char *, int, int);
91*0Sstevel@tonic-gate static kstat_t *find_pic_kstat(char *, int, char *);
92*0Sstevel@tonic-gate static int64_t is_num(char *);
93*0Sstevel@tonic-gate static void print_banner(void);
94*0Sstevel@tonic-gate static void print_timestamp(void);
95*0Sstevel@tonic-gate static void usage(void);
96*0Sstevel@tonic-gate static void *safe_malloc(size_t);
97*0Sstevel@tonic-gate static void set_timer(int);
98*0Sstevel@tonic-gate static void handle_sig(int);
99*0Sstevel@tonic-gate static int strisnum(const char *);
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate int
main(int argc,char ** argv)102*0Sstevel@tonic-gate main(int argc, char **argv)
103*0Sstevel@tonic-gate {
104*0Sstevel@tonic-gate 	int		c, i;
105*0Sstevel@tonic-gate 	int		interval = 1;	/* Interval between displays */
106*0Sstevel@tonic-gate 	int		count = 0;	/* Number of times to sample */
107*0Sstevel@tonic-gate 	int		write_evts = FALSE;
108*0Sstevel@tonic-gate 	int		pos = 0;
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
111*0Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
112*0Sstevel@tonic-gate #endif
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	/* For I18N */
115*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
116*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	pgmname = basename(argv[0]);
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	if ((kc = kstat_open()) == NULL) {
121*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: could not "
122*0Sstevel@tonic-gate 			"open /dev/kstat\n"), pgmname);
123*0Sstevel@tonic-gate 		exit(1);
124*0Sstevel@tonic-gate 	}
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "e:w:r:ahln")) != EOF) {
127*0Sstevel@tonic-gate 		switch (c) {
128*0Sstevel@tonic-gate 		case 'a':
129*0Sstevel@tonic-gate 			delta = FALSE;
130*0Sstevel@tonic-gate 			break;
131*0Sstevel@tonic-gate 		case 'e':
132*0Sstevel@tonic-gate 			(void) print_evt();
133*0Sstevel@tonic-gate 			break;
134*0Sstevel@tonic-gate 		case 'h':
135*0Sstevel@tonic-gate 			usage();
136*0Sstevel@tonic-gate 			break;
137*0Sstevel@tonic-gate 		case 'l':
138*0Sstevel@tonic-gate 			(void) print_dev(argc, argv[argc-1]);
139*0Sstevel@tonic-gate 			break;
140*0Sstevel@tonic-gate 		case 'n':
141*0Sstevel@tonic-gate 			banner = FALSE;
142*0Sstevel@tonic-gate 			break;
143*0Sstevel@tonic-gate 		case 'r':
144*0Sstevel@tonic-gate 			(void) parse_cmd(READ_EVT);
145*0Sstevel@tonic-gate 			break;
146*0Sstevel@tonic-gate 		case 'w':
147*0Sstevel@tonic-gate 			(void) parse_cmd(WRITE_EVT);
148*0Sstevel@tonic-gate 			write_evts = TRUE;
149*0Sstevel@tonic-gate 			break;
150*0Sstevel@tonic-gate 		default:
151*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: invalid "
152*0Sstevel@tonic-gate 				"option\n"), pgmname);
153*0Sstevel@tonic-gate 			usage();
154*0Sstevel@tonic-gate 			break;
155*0Sstevel@tonic-gate 		}
156*0Sstevel@tonic-gate 	}
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	if ((argc == 1) || (dev_list_head == NULL))
159*0Sstevel@tonic-gate 		usage();
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	/*
162*0Sstevel@tonic-gate 	 * validate remaining operands are numeric.
163*0Sstevel@tonic-gate 	 */
164*0Sstevel@tonic-gate 	pos = optind;
165*0Sstevel@tonic-gate 	while (pos < argc) {
166*0Sstevel@tonic-gate 		if (strisnum(argv[pos]) == 0) {
167*0Sstevel@tonic-gate 			(void) fprintf(stderr,
168*0Sstevel@tonic-gate 				gettext("%s: syntax error\n"),
169*0Sstevel@tonic-gate 				pgmname);
170*0Sstevel@tonic-gate 			usage();
171*0Sstevel@tonic-gate 		}
172*0Sstevel@tonic-gate 		pos++;
173*0Sstevel@tonic-gate 	}
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	if (optind < argc) {
176*0Sstevel@tonic-gate 		if ((interval = atoi(argv[optind])) == 0) {
177*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: invalid "
178*0Sstevel@tonic-gate 				"interval value\n"), pgmname);
179*0Sstevel@tonic-gate 			exit(1);
180*0Sstevel@tonic-gate 		}
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 		optind++;
183*0Sstevel@tonic-gate 		if (optind < argc)
184*0Sstevel@tonic-gate 			if ((count = atoi(argv[optind])) <= 0) {
185*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: "
186*0Sstevel@tonic-gate 					"invalid iteration value.\n"),
187*0Sstevel@tonic-gate 					    pgmname);
188*0Sstevel@tonic-gate 				exit(1);
189*0Sstevel@tonic-gate 			}
190*0Sstevel@tonic-gate 	}
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	set_timer(interval);
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	/*
195*0Sstevel@tonic-gate 	 * Set events for the first time.
196*0Sstevel@tonic-gate 	 */
197*0Sstevel@tonic-gate 	if (write_evts == TRUE)
198*0Sstevel@tonic-gate 		setup_evts();
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	if (count > 0) {
202*0Sstevel@tonic-gate 		for (i = 0; i < count; i++) {
203*0Sstevel@tonic-gate 			if (banner)
204*0Sstevel@tonic-gate 				print_banner();
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 			check_dr_ops();
207*0Sstevel@tonic-gate 			read_evts();
208*0Sstevel@tonic-gate 			(void) fflush(stdout);
209*0Sstevel@tonic-gate 			(void) pause();
210*0Sstevel@tonic-gate 		}
211*0Sstevel@tonic-gate 	} else {
212*0Sstevel@tonic-gate 		for (;;) {
213*0Sstevel@tonic-gate 			if (banner)
214*0Sstevel@tonic-gate 				print_banner();
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 			check_dr_ops();
217*0Sstevel@tonic-gate 			read_evts();
218*0Sstevel@tonic-gate 			(void) fflush(stdout);
219*0Sstevel@tonic-gate 			(void) pause();
220*0Sstevel@tonic-gate 		}
221*0Sstevel@tonic-gate 	}
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	read_evts();
224*0Sstevel@tonic-gate 	return (0);
225*0Sstevel@tonic-gate }
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate /*
229*0Sstevel@tonic-gate  * Display all the events that can be set on a device.
230*0Sstevel@tonic-gate  */
231*0Sstevel@tonic-gate void
print_evt()232*0Sstevel@tonic-gate print_evt()
233*0Sstevel@tonic-gate {
234*0Sstevel@tonic-gate 	kstat_t		*cnt_ksp;
235*0Sstevel@tonic-gate 	kstat_t		*pic_ksp;
236*0Sstevel@tonic-gate 	kstat_named_t	*cnt_data;
237*0Sstevel@tonic-gate 	kstat_named_t	*pic_data;
238*0Sstevel@tonic-gate 	char		*device = NULL;
239*0Sstevel@tonic-gate 	char		*value;
240*0Sstevel@tonic-gate 	int		inst_num = -1;
241*0Sstevel@tonic-gate 	int		i = 0;
242*0Sstevel@tonic-gate 	int		j;
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	value = optarg;
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	/*
247*0Sstevel@tonic-gate 	 * Search through the value string for a numeric char which will
248*0Sstevel@tonic-gate 	 * be the device instance number, if the user specified one. If
249*0Sstevel@tonic-gate 	 * the user did not specify an instance then the return value from
250*0Sstevel@tonic-gate 	 * strscpn will be equal to the string length. In this case we
251*0Sstevel@tonic-gate 	 * use a default value of -1 for the kstat_lookup which causes
252*0Sstevel@tonic-gate 	 * the device number to be ignored during the search.
253*0Sstevel@tonic-gate 	 */
254*0Sstevel@tonic-gate 	if (((i = strcspn(value, "0123456789")) > 0) && (i != strlen(value))) {
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 		device = safe_malloc(sizeof (char) * i+1);
257*0Sstevel@tonic-gate 		device[i] = '\0';
258*0Sstevel@tonic-gate 		(void) strncpy(device, value, i);
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 		value = value + i;
261*0Sstevel@tonic-gate 		inst_num = atoi(value);
262*0Sstevel@tonic-gate 	}
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 	/*
265*0Sstevel@tonic-gate 	 * No instance specified.
266*0Sstevel@tonic-gate 	 */
267*0Sstevel@tonic-gate 	if (device == NULL)
268*0Sstevel@tonic-gate 		device = value;
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	/*
271*0Sstevel@tonic-gate 	 * Get the "counters" kstat, so that we can get
272*0Sstevel@tonic-gate 	 * the names of the "picN" kstats, which hold the
273*0Sstevel@tonic-gate 	 * event names.
274*0Sstevel@tonic-gate 	 */
275*0Sstevel@tonic-gate 	if ((cnt_ksp = kstat_lookup(kc, device, inst_num, "counters"))
276*0Sstevel@tonic-gate 								== NULL) {
277*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: invalid device "
278*0Sstevel@tonic-gate 			"name or instance (%s)\n"), pgmname, device);
279*0Sstevel@tonic-gate 		exit(1);
280*0Sstevel@tonic-gate 	}
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	if (kstat_read(kc, cnt_ksp, NULL) == FAIL) {
283*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: could not read "
284*0Sstevel@tonic-gate 			"kstat.\n"), pgmname);
285*0Sstevel@tonic-gate 		exit(1);
286*0Sstevel@tonic-gate 	}
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 	cnt_data = (kstat_named_t *)cnt_ksp->ks_data;
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 	/*
291*0Sstevel@tonic-gate 	 * Start at 1 as the first entry in the "counters"
292*0Sstevel@tonic-gate 	 * kstat is the pcr value/name. We are looking for the
293*0Sstevel@tonic-gate 	 * name of the "picN" kstats. For each one found store
294*0Sstevel@tonic-gate 	 * a pointer to it in pic_data[].
295*0Sstevel@tonic-gate 	 */
296*0Sstevel@tonic-gate 	if (cnt_ksp->ks_ndata <= 1) {
297*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: invalid kstat "
298*0Sstevel@tonic-gate 			"structure.\n"), pgmname);
299*0Sstevel@tonic-gate 		exit(1);
300*0Sstevel@tonic-gate 	}
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 	for (i = 1; i < cnt_ksp->ks_ndata; i++) {
303*0Sstevel@tonic-gate 		if ((pic_ksp = find_pic_kstat(device, inst_num,
304*0Sstevel@tonic-gate 			cnt_data[i].name)) == NULL) {
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: could not read "
307*0Sstevel@tonic-gate 				"pic kstat data structure for %s\n"),
308*0Sstevel@tonic-gate 				    pgmname, cnt_ksp->ks_module);
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 			exit(1);
311*0Sstevel@tonic-gate 		}
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 		if (kstat_read(kc, pic_ksp, NULL) == FAIL) {
314*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: could not read "
315*0Sstevel@tonic-gate 				"pic kstat.\n"), pgmname);
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 			exit(1);
318*0Sstevel@tonic-gate 		}
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate 		pic_data = (kstat_named_t *)pic_ksp->ks_data;
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate 		(void) printf(gettext("pic%-8d\n"), i-1);
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate 		for (j = 0; j < pic_ksp->ks_ndata-1; j++) {
325*0Sstevel@tonic-gate 			(void) printf("%-30s\n", pic_data[j].name);
326*0Sstevel@tonic-gate 		}
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 		(void) printf("\n");
329*0Sstevel@tonic-gate 	}
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 	exit(0);
332*0Sstevel@tonic-gate }
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate /*
336*0Sstevel@tonic-gate  * Display the names and instances of the devices on the system
337*0Sstevel@tonic-gate  * which can support performance monitoring.
338*0Sstevel@tonic-gate  */
339*0Sstevel@tonic-gate void
print_dev(int argc,char * str)340*0Sstevel@tonic-gate print_dev(int argc, char *str)
341*0Sstevel@tonic-gate {
342*0Sstevel@tonic-gate 	kstat_t	*ksp;
343*0Sstevel@tonic-gate 	static int first_time = 1;
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 	if ((argc > 2) || (strcmp(str, "-l") != 0)) {
346*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: no arguments "
347*0Sstevel@tonic-gate 			"permitted with -l option.\n"),
348*0Sstevel@tonic-gate 			    pgmname);
349*0Sstevel@tonic-gate 		usage();
350*0Sstevel@tonic-gate 		exit(1);
351*0Sstevel@tonic-gate 	}
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate 	/*
354*0Sstevel@tonic-gate 	 * For each device node, print the node name (device
355*0Sstevel@tonic-gate 	 * name) and the instance numbers.
356*0Sstevel@tonic-gate 	 */
357*0Sstevel@tonic-gate 	for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
358*0Sstevel@tonic-gate 		if ((strcmp(ksp->ks_class, "bus") == 0) &&
359*0Sstevel@tonic-gate 			(strcmp(ksp->ks_name, "counters") == 0)) {
360*0Sstevel@tonic-gate 					if (first_time) {
361*0Sstevel@tonic-gate 						(void) printf(gettext("Busstat "
362*0Sstevel@tonic-gate 							"Device(s):\n"));
363*0Sstevel@tonic-gate 						first_time = 0;
364*0Sstevel@tonic-gate 					}
365*0Sstevel@tonic-gate 					(void) printf("%s%d ", ksp->ks_module,
366*0Sstevel@tonic-gate 						ksp->ks_instance);
367*0Sstevel@tonic-gate 		}
368*0Sstevel@tonic-gate 	}
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate 	if (first_time)
371*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: No devices available "
372*0Sstevel@tonic-gate 			"in system."), pgmname);
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate 	(void) printf("\n");
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate 	exit(0);
377*0Sstevel@tonic-gate }
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate /*
380*0Sstevel@tonic-gate  * Parses the cmd line, checks all the values and
381*0Sstevel@tonic-gate  * creates the appropiate data structures.
382*0Sstevel@tonic-gate  */
383*0Sstevel@tonic-gate void
parse_cmd(int mode)384*0Sstevel@tonic-gate parse_cmd(int mode)
385*0Sstevel@tonic-gate {
386*0Sstevel@tonic-gate 	char		*options = optarg, *value;
387*0Sstevel@tonic-gate 	int		arg_num	= 0;
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 	while ((value = (char *)strtok(options, ",=")) != NULL) {
390*0Sstevel@tonic-gate 		/*
391*0Sstevel@tonic-gate 		 * First arg must be device name.
392*0Sstevel@tonic-gate 		 */
393*0Sstevel@tonic-gate 		if (!arg_num) {
394*0Sstevel@tonic-gate 			parse_dev_inst(value);
395*0Sstevel@tonic-gate 		} else {
396*0Sstevel@tonic-gate 			if (mode == READ_EVT) {
397*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: "
398*0Sstevel@tonic-gate 					"event names or pic values not "
399*0Sstevel@tonic-gate 					"permitted with -r option.\n"),
400*0Sstevel@tonic-gate 					    pgmname);
401*0Sstevel@tonic-gate 					usage();
402*0Sstevel@tonic-gate 				exit(1);
403*0Sstevel@tonic-gate 			}
404*0Sstevel@tonic-gate 			/*
405*0Sstevel@tonic-gate 			 * Now dealing with pic values.
406*0Sstevel@tonic-gate 			 */
407*0Sstevel@tonic-gate 			parse_pic_evt(value);
408*0Sstevel@tonic-gate 		}
409*0Sstevel@tonic-gate 		/*
410*0Sstevel@tonic-gate 		 * After first strtok call, must set first arg
411*0Sstevel@tonic-gate 		 * to null if wish to parse rest of string.
412*0Sstevel@tonic-gate 		 * See strtok man page.
413*0Sstevel@tonic-gate 		 */
414*0Sstevel@tonic-gate 		if (options != NULL)
415*0Sstevel@tonic-gate 			options = NULL;
416*0Sstevel@tonic-gate 		arg_num++;
417*0Sstevel@tonic-gate 	}
418*0Sstevel@tonic-gate }
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate /*
422*0Sstevel@tonic-gate  * Parse the device name/instance section of the
423*0Sstevel@tonic-gate  * command line.
424*0Sstevel@tonic-gate  */
425*0Sstevel@tonic-gate void
parse_dev_inst(char * value)426*0Sstevel@tonic-gate parse_dev_inst(char *value)
427*0Sstevel@tonic-gate {
428*0Sstevel@tonic-gate 	int		i;
429*0Sstevel@tonic-gate 	char		*device	= NULL;
430*0Sstevel@tonic-gate 	int		malloc_flag = FALSE;
431*0Sstevel@tonic-gate 
432*0Sstevel@tonic-gate 	if (strlen(value) == 0) {
433*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: No device name given.\n"),
434*0Sstevel@tonic-gate 			pgmname);
435*0Sstevel@tonic-gate 		exit(1);
436*0Sstevel@tonic-gate 	}
437*0Sstevel@tonic-gate 
438*0Sstevel@tonic-gate 	/*
439*0Sstevel@tonic-gate 	 * Break string into device name and
440*0Sstevel@tonic-gate 	 * instance number (if given).
441*0Sstevel@tonic-gate 	 */
442*0Sstevel@tonic-gate 	if ((i = strcspn(value, "0123456789")) > 0) {
443*0Sstevel@tonic-gate 		if (i != strlen(value)) {
444*0Sstevel@tonic-gate 			device = safe_malloc(sizeof (char) * i+1);
445*0Sstevel@tonic-gate 			device[i] = '\0';
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate 			(void) strncpy(device, value, i);
448*0Sstevel@tonic-gate 			malloc_flag = TRUE;
449*0Sstevel@tonic-gate 
450*0Sstevel@tonic-gate 			value = value + i;
451*0Sstevel@tonic-gate 		}
452*0Sstevel@tonic-gate 	}
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate 	/*
455*0Sstevel@tonic-gate 	 * No instance was specified so we assume
456*0Sstevel@tonic-gate 	 * the user wants to use ALL instances.
457*0Sstevel@tonic-gate 	 */
458*0Sstevel@tonic-gate 	if (device == NULL) {
459*0Sstevel@tonic-gate 		if ((device = value) == NULL) {
460*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: no device "
461*0Sstevel@tonic-gate 				"specified\n"), pgmname);
462*0Sstevel@tonic-gate 			exit(1);
463*0Sstevel@tonic-gate 		}
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate 		/*
466*0Sstevel@tonic-gate 		 * Set global flags.
467*0Sstevel@tonic-gate 		 */
468*0Sstevel@tonic-gate 		(void) strcpy(curr_dev_name, device);
469*0Sstevel@tonic-gate 		curr_inst_num = -1;
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate 		add_all_dev_node(device);
472*0Sstevel@tonic-gate 		goto clean_up;
473*0Sstevel@tonic-gate 	}
474*0Sstevel@tonic-gate 
475*0Sstevel@tonic-gate 	/*
476*0Sstevel@tonic-gate 	 * Set global flags.
477*0Sstevel@tonic-gate 	 */
478*0Sstevel@tonic-gate 	(void) strcpy(curr_dev_name, device);
479*0Sstevel@tonic-gate 	curr_inst_num = atoi(value);
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate 	add_dev_node(device, curr_inst_num);
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate clean_up:
484*0Sstevel@tonic-gate 	if (malloc_flag) {
485*0Sstevel@tonic-gate 		free(device);
486*0Sstevel@tonic-gate 	}
487*0Sstevel@tonic-gate }
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate 
490*0Sstevel@tonic-gate /*
491*0Sstevel@tonic-gate  * Adds new event nodes to existing ones, modifies existing ones, or
492*0Sstevel@tonic-gate  * prunes existing ones.
493*0Sstevel@tonic-gate  *
494*0Sstevel@tonic-gate  * A specific instance call will overwrite an earlier all
495*0Sstevel@tonic-gate  * instances call, but *not* vice-versa.
496*0Sstevel@tonic-gate  *
497*0Sstevel@tonic-gate  * All the state transitions are given below.
498*0Sstevel@tonic-gate  *
499*0Sstevel@tonic-gate  *
500*0Sstevel@tonic-gate  *                       Call Type
501*0Sstevel@tonic-gate  * STATE |  Specific Instance          All Instances.
502*0Sstevel@tonic-gate  * ======================================================
503*0Sstevel@tonic-gate  * INIT  | Change state to       | Change state to ALL,
504*0Sstevel@tonic-gate  *       | INST, add events      | add events.
505*0Sstevel@tonic-gate  *       |                       |
506*0Sstevel@tonic-gate  * INST  | State unchanged,      | No change.
507*0Sstevel@tonic-gate  *       | Add events.           |
508*0Sstevel@tonic-gate  *       |                       |
509*0Sstevel@tonic-gate  * ALL   | Change state to       | State unchanged,
510*0Sstevel@tonic-gate  *       | INST, replace events. | add events.
511*0Sstevel@tonic-gate  */
512*0Sstevel@tonic-gate void
parse_pic_evt(char * value)513*0Sstevel@tonic-gate parse_pic_evt(char *value)
514*0Sstevel@tonic-gate {
515*0Sstevel@tonic-gate 	dev_node_t	*dev_node;
516*0Sstevel@tonic-gate 	char		*evt_name;
517*0Sstevel@tonic-gate 	int		pic_num;
518*0Sstevel@tonic-gate 
519*0Sstevel@tonic-gate 	if (strlen(value) <= PIC_STR_LEN) {
520*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: no pic number "
521*0Sstevel@tonic-gate 			"specified.\n"), pgmname);
522*0Sstevel@tonic-gate 		exit(1);
523*0Sstevel@tonic-gate 	}
524*0Sstevel@tonic-gate 
525*0Sstevel@tonic-gate 	if (strncmp(value, "pic", PIC_STR_LEN) != 0) {
526*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: missing pic "
527*0Sstevel@tonic-gate 			"specifier\n"), pgmname);
528*0Sstevel@tonic-gate 		usage();
529*0Sstevel@tonic-gate 	}
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 	/*
532*0Sstevel@tonic-gate 	 * Step over the 'pic' part of the string to
533*0Sstevel@tonic-gate 	 * get the pic number.
534*0Sstevel@tonic-gate 	 */
535*0Sstevel@tonic-gate 	value = value + PIC_STR_LEN;
536*0Sstevel@tonic-gate 	pic_num = atoi(value);
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate 	if ((pic_num == -1) || (pic_num > max_pic_num -1)) {
539*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: invalid pic "
540*0Sstevel@tonic-gate 			"number.\n"), pgmname);
541*0Sstevel@tonic-gate 		exit(1);
542*0Sstevel@tonic-gate 	}
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate 	if ((evt_name = (char *)strtok(NULL, "=,")) == NULL) {
545*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: no event "
546*0Sstevel@tonic-gate 			"specified.\n"), pgmname);
547*0Sstevel@tonic-gate 		exit(1);
548*0Sstevel@tonic-gate 	}
549*0Sstevel@tonic-gate 
550*0Sstevel@tonic-gate 	/*
551*0Sstevel@tonic-gate 	 * Dealing with a specific instance.
552*0Sstevel@tonic-gate 	 */
553*0Sstevel@tonic-gate 	if (curr_inst_num >= 0) {
554*0Sstevel@tonic-gate 		if ((dev_node = find_dev_node(curr_dev_name,
555*0Sstevel@tonic-gate 			curr_inst_num, pic_num)) == NULL) {
556*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: could not find "
557*0Sstevel@tonic-gate 				"data structures for %s\n"),
558*0Sstevel@tonic-gate 				    pgmname, curr_dev_name);
559*0Sstevel@tonic-gate 			exit(1);
560*0Sstevel@tonic-gate 		}
561*0Sstevel@tonic-gate 
562*0Sstevel@tonic-gate 		if (dev_node->r_w == EVT_READ) {
563*0Sstevel@tonic-gate 			modify_evt_node(dev_node, evt_name);
564*0Sstevel@tonic-gate 			dev_node->r_w = EVT_WRITE;
565*0Sstevel@tonic-gate 			dev_node->state = STATE_INST;
566*0Sstevel@tonic-gate 
567*0Sstevel@tonic-gate 		} else if ((dev_node->r_w == EVT_WRITE) &&
568*0Sstevel@tonic-gate 			(dev_node->state == STATE_ALL)) {
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate 			prune_evt_nodes(dev_node);
571*0Sstevel@tonic-gate 			modify_evt_node(dev_node, evt_name);
572*0Sstevel@tonic-gate 			dev_node->state = STATE_INST;
573*0Sstevel@tonic-gate 
574*0Sstevel@tonic-gate 		} else if ((dev_node->r_w == EVT_WRITE) &&
575*0Sstevel@tonic-gate 			(dev_node->state == STATE_INST)) {
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate 			add_evt_node(dev_node);
578*0Sstevel@tonic-gate 			modify_evt_node(dev_node, evt_name);
579*0Sstevel@tonic-gate 		}
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate 		return;
582*0Sstevel@tonic-gate 	}
583*0Sstevel@tonic-gate 
584*0Sstevel@tonic-gate 	/*
585*0Sstevel@tonic-gate 	 * Dealing with all instances of a specific device.
586*0Sstevel@tonic-gate 	 */
587*0Sstevel@tonic-gate 	dev_node = dev_list_head;
588*0Sstevel@tonic-gate 	while (dev_node != NULL) {
589*0Sstevel@tonic-gate 		if ((strcmp(dev_node->name, curr_dev_name) == 0) &&
590*0Sstevel@tonic-gate 			(dev_node->pic_num == pic_num)) {
591*0Sstevel@tonic-gate 
592*0Sstevel@tonic-gate 			if (dev_node->r_w == EVT_READ) {
593*0Sstevel@tonic-gate 				modify_evt_node(dev_node,
594*0Sstevel@tonic-gate 					evt_name);
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 				dev_node->r_w = EVT_WRITE;
597*0Sstevel@tonic-gate 				dev_node->state = STATE_ALL;
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 			} else if ((dev_node->r_w == EVT_WRITE) &&
600*0Sstevel@tonic-gate 				(dev_node->state == STATE_ALL)) {
601*0Sstevel@tonic-gate 
602*0Sstevel@tonic-gate 				add_evt_node(dev_node);
603*0Sstevel@tonic-gate 				modify_evt_node(dev_node, evt_name);
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate 			}
606*0Sstevel@tonic-gate 		}
607*0Sstevel@tonic-gate 		dev_node = dev_node->next;
608*0Sstevel@tonic-gate 	}
609*0Sstevel@tonic-gate }
610*0Sstevel@tonic-gate 
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate /*
613*0Sstevel@tonic-gate  * Create a dev_node structure for this device if one does not
614*0Sstevel@tonic-gate  * already exist.
615*0Sstevel@tonic-gate  */
616*0Sstevel@tonic-gate void
add_dev_node(char * dev_name,int inst_num)617*0Sstevel@tonic-gate add_dev_node(char *dev_name, int inst_num)
618*0Sstevel@tonic-gate {
619*0Sstevel@tonic-gate 	dev_node_t	*new_dev_node;
620*0Sstevel@tonic-gate 	kstat_named_t	*cnt_data;
621*0Sstevel@tonic-gate 	kstat_t		*cnt_ksp;
622*0Sstevel@tonic-gate 	kstat_t		*pic_ksp;
623*0Sstevel@tonic-gate 	int		pic_num;
624*0Sstevel@tonic-gate 
625*0Sstevel@tonic-gate 
626*0Sstevel@tonic-gate 	if ((cnt_ksp = kstat_lookup(kc, dev_name,
627*0Sstevel@tonic-gate 		inst_num, "counters")) == NULL) {
628*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: invalid device "
629*0Sstevel@tonic-gate 			"name or instance (%s%d)\n"), pgmname,
630*0Sstevel@tonic-gate 				dev_name, inst_num);
631*0Sstevel@tonic-gate 		exit(1);
632*0Sstevel@tonic-gate 	}
633*0Sstevel@tonic-gate 
634*0Sstevel@tonic-gate 	if (kstat_read(kc, cnt_ksp, NULL) == FAIL) {
635*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s : could not read counters "
636*0Sstevel@tonic-gate 			"kstat for device %s.\n"), pgmname, dev_name);
637*0Sstevel@tonic-gate 		exit(1);
638*0Sstevel@tonic-gate 	}
639*0Sstevel@tonic-gate 
640*0Sstevel@tonic-gate 	cnt_data = (kstat_named_t *)cnt_ksp->ks_data;
641*0Sstevel@tonic-gate 
642*0Sstevel@tonic-gate 	if (cnt_ksp->ks_ndata <= 1) {
643*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s : invalid "
644*0Sstevel@tonic-gate 			"kstat structure.\n"), pgmname);
645*0Sstevel@tonic-gate 		exit(1);
646*0Sstevel@tonic-gate 	}
647*0Sstevel@tonic-gate 
648*0Sstevel@tonic-gate 	/*
649*0Sstevel@tonic-gate 	 * max_pic_num used to format headers correctly
650*0Sstevel@tonic-gate 	 * for printing.
651*0Sstevel@tonic-gate 	 */
652*0Sstevel@tonic-gate 	if (cnt_ksp->ks_ndata-1 > max_pic_num)
653*0Sstevel@tonic-gate 		max_pic_num = cnt_ksp->ks_ndata-1;
654*0Sstevel@tonic-gate 
655*0Sstevel@tonic-gate 	/* for each pic... */
656*0Sstevel@tonic-gate 	for (pic_num = 0; pic_num < cnt_ksp->ks_ndata-1; pic_num++) {
657*0Sstevel@tonic-gate 		if (find_dev_node(dev_name, inst_num, pic_num) != NULL) {
658*0Sstevel@tonic-gate 			/* Node already exists */
659*0Sstevel@tonic-gate 			continue;
660*0Sstevel@tonic-gate 		}
661*0Sstevel@tonic-gate 
662*0Sstevel@tonic-gate 		new_dev_node = safe_malloc(sizeof (dev_node_t));
663*0Sstevel@tonic-gate 		bzero(new_dev_node, sizeof (dev_node_t));
664*0Sstevel@tonic-gate 
665*0Sstevel@tonic-gate 		(void) strcpy(new_dev_node->name, dev_name);
666*0Sstevel@tonic-gate 		new_dev_node->dev_inst = inst_num;
667*0Sstevel@tonic-gate 		new_dev_node->pic_num = pic_num;
668*0Sstevel@tonic-gate 
669*0Sstevel@tonic-gate 		new_dev_node->cnt_ksp = cnt_ksp;
670*0Sstevel@tonic-gate 
671*0Sstevel@tonic-gate 		if ((pic_ksp = find_pic_kstat(dev_name, inst_num,
672*0Sstevel@tonic-gate 			cnt_data[pic_num+1].name)) == NULL) {
673*0Sstevel@tonic-gate 
674*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: could not find "
675*0Sstevel@tonic-gate 				"pic kstat structure for %s.\n"),
676*0Sstevel@tonic-gate 				    pgmname, cnt_ksp->ks_module);
677*0Sstevel@tonic-gate 			exit(1);
678*0Sstevel@tonic-gate 		}
679*0Sstevel@tonic-gate 
680*0Sstevel@tonic-gate 		new_dev_node->pic_ksp = pic_ksp;
681*0Sstevel@tonic-gate 
682*0Sstevel@tonic-gate 		add_evt_node(new_dev_node);
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate 		new_dev_node->state = STATE_INIT;
685*0Sstevel@tonic-gate 		new_dev_node->r_w = EVT_READ;
686*0Sstevel@tonic-gate 
687*0Sstevel@tonic-gate 		if (dev_list_head == NULL) {
688*0Sstevel@tonic-gate 			dev_list_head = new_dev_node;
689*0Sstevel@tonic-gate 			dev_list_tail = new_dev_node;
690*0Sstevel@tonic-gate 
691*0Sstevel@tonic-gate 		} else if (find_dev_node(dev_name, inst_num, pic_num) == NULL) {
692*0Sstevel@tonic-gate 			dev_list_tail->next = new_dev_node;
693*0Sstevel@tonic-gate 			dev_list_tail = new_dev_node;
694*0Sstevel@tonic-gate 		}
695*0Sstevel@tonic-gate 	}
696*0Sstevel@tonic-gate }
697*0Sstevel@tonic-gate 
698*0Sstevel@tonic-gate 
699*0Sstevel@tonic-gate /*
700*0Sstevel@tonic-gate  * Add all possible instances of a device.
701*0Sstevel@tonic-gate  */
702*0Sstevel@tonic-gate void
add_all_dev_node(char * dev_name)703*0Sstevel@tonic-gate add_all_dev_node(char *dev_name)
704*0Sstevel@tonic-gate {
705*0Sstevel@tonic-gate 	kstat_t	*ksp;
706*0Sstevel@tonic-gate 	int	match = 0;
707*0Sstevel@tonic-gate 
708*0Sstevel@tonic-gate 	for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
709*0Sstevel@tonic-gate 		if ((strcmp(ksp->ks_class, "bus") == 0) &&
710*0Sstevel@tonic-gate 			(strcmp(ksp->ks_name, "counters") == 0) &&
711*0Sstevel@tonic-gate 			(strcmp(ksp->ks_module, dev_name) == 0)) {
712*0Sstevel@tonic-gate 				match = 1;
713*0Sstevel@tonic-gate 				add_dev_node(dev_name, ksp->ks_instance);
714*0Sstevel@tonic-gate 		}
715*0Sstevel@tonic-gate 	}
716*0Sstevel@tonic-gate 
717*0Sstevel@tonic-gate 	if (match == 0) {
718*0Sstevel@tonic-gate 		(void) fprintf(stderr,
719*0Sstevel@tonic-gate 			gettext("%s: invalid device name (%s)\n"),
720*0Sstevel@tonic-gate 			pgmname, dev_name);
721*0Sstevel@tonic-gate 		exit(1);
722*0Sstevel@tonic-gate 	}
723*0Sstevel@tonic-gate }
724*0Sstevel@tonic-gate 
725*0Sstevel@tonic-gate 
726*0Sstevel@tonic-gate /*
727*0Sstevel@tonic-gate  * Add an event node to a specified device node.
728*0Sstevel@tonic-gate  */
729*0Sstevel@tonic-gate void
add_evt_node(dev_node_t * dev_node)730*0Sstevel@tonic-gate add_evt_node(dev_node_t *dev_node)
731*0Sstevel@tonic-gate {
732*0Sstevel@tonic-gate 	evt_node_t	*new_evt_node;
733*0Sstevel@tonic-gate 	evt_node_t	*curr_evt_node;
734*0Sstevel@tonic-gate 
735*0Sstevel@tonic-gate 	new_evt_node = safe_malloc(sizeof (evt_node_t));
736*0Sstevel@tonic-gate 	bzero(new_evt_node, sizeof (evt_node_t));
737*0Sstevel@tonic-gate 
738*0Sstevel@tonic-gate 	(void) strcpy(new_evt_node->evt_name, "");
739*0Sstevel@tonic-gate 
740*0Sstevel@tonic-gate 	if (dev_node->evt_node == NULL) {
741*0Sstevel@tonic-gate 		dev_node->evt_node = new_evt_node;
742*0Sstevel@tonic-gate 		new_evt_node->next = new_evt_node;
743*0Sstevel@tonic-gate 		return;
744*0Sstevel@tonic-gate 	} else {
745*0Sstevel@tonic-gate 		curr_evt_node = dev_node->evt_node;
746*0Sstevel@tonic-gate 		while (curr_evt_node->next != dev_node->evt_node)
747*0Sstevel@tonic-gate 			curr_evt_node = curr_evt_node->next;
748*0Sstevel@tonic-gate 
749*0Sstevel@tonic-gate 		curr_evt_node->next = new_evt_node;
750*0Sstevel@tonic-gate 		new_evt_node->next = dev_node->evt_node;
751*0Sstevel@tonic-gate 	}
752*0Sstevel@tonic-gate }
753*0Sstevel@tonic-gate 
754*0Sstevel@tonic-gate 
755*0Sstevel@tonic-gate /*
756*0Sstevel@tonic-gate  * Fill in or change the fields of an evt node.
757*0Sstevel@tonic-gate  */
758*0Sstevel@tonic-gate void
modify_evt_node(dev_node_t * dev_node,char * evt_name)759*0Sstevel@tonic-gate modify_evt_node(dev_node_t *dev_node, char *evt_name)
760*0Sstevel@tonic-gate {
761*0Sstevel@tonic-gate 	evt_node_t	*evt_node;
762*0Sstevel@tonic-gate 	kstat_t		*pic_ksp;
763*0Sstevel@tonic-gate 	kstat_named_t	*pic_data;
764*0Sstevel@tonic-gate 	int64_t		evt_num = 0;
765*0Sstevel@tonic-gate 	int		evt_match = 0;
766*0Sstevel@tonic-gate 	int		i;
767*0Sstevel@tonic-gate 
768*0Sstevel@tonic-gate 	evt_node = dev_node->evt_node;
769*0Sstevel@tonic-gate 
770*0Sstevel@tonic-gate 	/*
771*0Sstevel@tonic-gate 	 * Find the last event node.
772*0Sstevel@tonic-gate 	 */
773*0Sstevel@tonic-gate 	if (evt_node->next != evt_node) {
774*0Sstevel@tonic-gate 		while (evt_node->next != dev_node->evt_node) {
775*0Sstevel@tonic-gate 			evt_node = evt_node->next;
776*0Sstevel@tonic-gate 		}
777*0Sstevel@tonic-gate 	}
778*0Sstevel@tonic-gate 
779*0Sstevel@tonic-gate 	evt_node->prev_count = 0;
780*0Sstevel@tonic-gate 	evt_node->total = 0;
781*0Sstevel@tonic-gate 
782*0Sstevel@tonic-gate 	pic_ksp = dev_node->pic_ksp;
783*0Sstevel@tonic-gate 
784*0Sstevel@tonic-gate 	if (kstat_read(kc, pic_ksp, NULL) == FAIL) {
785*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: could not read "
786*0Sstevel@tonic-gate 			"pic kstat.\n"), pgmname);
787*0Sstevel@tonic-gate 		exit(1);
788*0Sstevel@tonic-gate 	}
789*0Sstevel@tonic-gate 
790*0Sstevel@tonic-gate 	pic_data = (kstat_named_t *)dev_node->pic_ksp->ks_data;
791*0Sstevel@tonic-gate 
792*0Sstevel@tonic-gate 	/*
793*0Sstevel@tonic-gate 	 * The event can either be given as a event name (string) or
794*0Sstevel@tonic-gate 	 * as a pcr mask. If given as pcr mask, we try to match it
795*0Sstevel@tonic-gate 	 * to an event name, and use that name. Otherwise we just use
796*0Sstevel@tonic-gate 	 * the pcr mask value.
797*0Sstevel@tonic-gate 	 */
798*0Sstevel@tonic-gate 	if ((evt_num = is_num(evt_name)) == EVT_STR) {
799*0Sstevel@tonic-gate 		(void) strcpy(evt_node->evt_name, evt_name);
800*0Sstevel@tonic-gate 
801*0Sstevel@tonic-gate 		for (i = 0; i < dev_node->pic_ksp->ks_ndata; i++) {
802*0Sstevel@tonic-gate 			if (strcmp(evt_name, pic_data[i].name) == 0) {
803*0Sstevel@tonic-gate 				evt_node->evt_pcr_mask = pic_data[i].value.ui64;
804*0Sstevel@tonic-gate 				return;
805*0Sstevel@tonic-gate 			}
806*0Sstevel@tonic-gate 		}
807*0Sstevel@tonic-gate 
808*0Sstevel@tonic-gate 		(void) fprintf(stderr,
809*0Sstevel@tonic-gate 			gettext("%s: %s is not a valid event name.\n"),
810*0Sstevel@tonic-gate 			pgmname, evt_name);
811*0Sstevel@tonic-gate 		exit(1);
812*0Sstevel@tonic-gate 
813*0Sstevel@tonic-gate 	} else {
814*0Sstevel@tonic-gate 		/*
815*0Sstevel@tonic-gate 		 * See if the pcr mask given by the user matches that for any
816*0Sstevel@tonic-gate 		 * existing event.
817*0Sstevel@tonic-gate 		 */
818*0Sstevel@tonic-gate 		for (i = 0; i < dev_node->pic_ksp->ks_ndata; i++) {
819*0Sstevel@tonic-gate 			if (evt_num == pic_data[i].value.ui64) {
820*0Sstevel@tonic-gate 				(void) strcpy(evt_node->evt_name,
821*0Sstevel@tonic-gate 					pic_data[i].name);
822*0Sstevel@tonic-gate 				evt_match = 1;
823*0Sstevel@tonic-gate 				break;
824*0Sstevel@tonic-gate 			}
825*0Sstevel@tonic-gate 		}
826*0Sstevel@tonic-gate 
827*0Sstevel@tonic-gate 		if (evt_match == 0)
828*0Sstevel@tonic-gate 			(void) sprintf(evt_node->evt_name, "%llx", evt_num);
829*0Sstevel@tonic-gate 
830*0Sstevel@tonic-gate 		evt_node->evt_pcr_mask = evt_num;
831*0Sstevel@tonic-gate 	}
832*0Sstevel@tonic-gate }
833*0Sstevel@tonic-gate 
834*0Sstevel@tonic-gate 
835*0Sstevel@tonic-gate /*
836*0Sstevel@tonic-gate  * Removes all bar one of the evt_nodes that are hanging off the
837*0Sstevel@tonic-gate  * specified dev_node.
838*0Sstevel@tonic-gate  */
839*0Sstevel@tonic-gate void
prune_evt_nodes(dev_node_t * dev_node)840*0Sstevel@tonic-gate prune_evt_nodes(dev_node_t *dev_node)
841*0Sstevel@tonic-gate {
842*0Sstevel@tonic-gate 	evt_node_t	*next_evt_node;
843*0Sstevel@tonic-gate 	evt_node_t	*curr_evt_node;
844*0Sstevel@tonic-gate 
845*0Sstevel@tonic-gate 	/*
846*0Sstevel@tonic-gate 	 * Only one evt node, nothing for us to do.
847*0Sstevel@tonic-gate 	 */
848*0Sstevel@tonic-gate 	if (dev_node->evt_node->next == dev_node->evt_node) {
849*0Sstevel@tonic-gate 		return;
850*0Sstevel@tonic-gate 	}
851*0Sstevel@tonic-gate 
852*0Sstevel@tonic-gate 	curr_evt_node = dev_node->evt_node->next;
853*0Sstevel@tonic-gate 	dev_node->evt_node->next = dev_node->evt_node;
854*0Sstevel@tonic-gate 
855*0Sstevel@tonic-gate 	while (curr_evt_node != dev_node->evt_node) {
856*0Sstevel@tonic-gate 		next_evt_node = curr_evt_node->next;
857*0Sstevel@tonic-gate 		free(curr_evt_node);
858*0Sstevel@tonic-gate 		curr_evt_node = next_evt_node;
859*0Sstevel@tonic-gate 	}
860*0Sstevel@tonic-gate }
861*0Sstevel@tonic-gate 
862*0Sstevel@tonic-gate 
863*0Sstevel@tonic-gate /*
864*0Sstevel@tonic-gate  * Set the events for each pic on each device instance.
865*0Sstevel@tonic-gate  */
866*0Sstevel@tonic-gate void
setup_evts()867*0Sstevel@tonic-gate setup_evts()
868*0Sstevel@tonic-gate {
869*0Sstevel@tonic-gate 	dev_node_t	*dev_node;
870*0Sstevel@tonic-gate 
871*0Sstevel@tonic-gate 	dev_node = dev_list_head;
872*0Sstevel@tonic-gate 
873*0Sstevel@tonic-gate 	while (dev_node != NULL) {
874*0Sstevel@tonic-gate 		if (dev_node->r_w == EVT_WRITE)
875*0Sstevel@tonic-gate 			set_evt(dev_node);
876*0Sstevel@tonic-gate 
877*0Sstevel@tonic-gate 		dev_node = dev_node->next;
878*0Sstevel@tonic-gate 	}
879*0Sstevel@tonic-gate }
880*0Sstevel@tonic-gate 
881*0Sstevel@tonic-gate 
882*0Sstevel@tonic-gate /*
883*0Sstevel@tonic-gate  * Set the appropiate events. Only called for event nodes
884*0Sstevel@tonic-gate  * that are marked EVT_WRITE.
885*0Sstevel@tonic-gate  */
886*0Sstevel@tonic-gate void
set_evt(dev_node_t * dev_node)887*0Sstevel@tonic-gate set_evt(dev_node_t *dev_node)
888*0Sstevel@tonic-gate {
889*0Sstevel@tonic-gate 	kstat_named_t	*cnt_data;
890*0Sstevel@tonic-gate 	kstat_named_t	*pic_data;
891*0Sstevel@tonic-gate 	kstat_t		*cnt_ksp;
892*0Sstevel@tonic-gate 	kstat_t		*pic_ksp;
893*0Sstevel@tonic-gate 	evt_node_t	*evt_node;
894*0Sstevel@tonic-gate 	uint64_t	clear_pcr_mask;
895*0Sstevel@tonic-gate 	uint64_t	pcr;
896*0Sstevel@tonic-gate 	int		pic_num;
897*0Sstevel@tonic-gate 
898*0Sstevel@tonic-gate 	cnt_ksp = dev_node->cnt_ksp;
899*0Sstevel@tonic-gate 	pic_ksp = dev_node->pic_ksp;
900*0Sstevel@tonic-gate 	pic_num = dev_node->pic_num;
901*0Sstevel@tonic-gate 	evt_node = dev_node->evt_node;
902*0Sstevel@tonic-gate 
903*0Sstevel@tonic-gate 	/* Read the "counters" kstat */
904*0Sstevel@tonic-gate 	if (kstat_read(kc, cnt_ksp, NULL) == FAIL) {
905*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: could "
906*0Sstevel@tonic-gate 			"not set event's.\n"), pgmname);
907*0Sstevel@tonic-gate 		exit(1);
908*0Sstevel@tonic-gate 	}
909*0Sstevel@tonic-gate 
910*0Sstevel@tonic-gate 	cnt_data = (kstat_named_t *)cnt_ksp->ks_data;
911*0Sstevel@tonic-gate 
912*0Sstevel@tonic-gate 	if (kstat_read(kc, pic_ksp, NULL) == FAIL) {
913*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: could "
914*0Sstevel@tonic-gate 			"not set event's.\n"), pgmname);
915*0Sstevel@tonic-gate 		exit(1);
916*0Sstevel@tonic-gate 	}
917*0Sstevel@tonic-gate 
918*0Sstevel@tonic-gate 	pic_data = (kstat_named_t *)pic_ksp->ks_data;
919*0Sstevel@tonic-gate 	clear_pcr_mask = pic_data[pic_ksp->ks_ndata-1].value.ui64;
920*0Sstevel@tonic-gate 
921*0Sstevel@tonic-gate 	if ((pic_num < 0) || (pic_num > cnt_ksp->ks_ndata-1)) {
922*0Sstevel@tonic-gate 		(void) fprintf(stderr,
923*0Sstevel@tonic-gate 			gettext("%s: invalid pic #%d.\n"),
924*0Sstevel@tonic-gate 			pgmname, pic_num);
925*0Sstevel@tonic-gate 		exit(1);
926*0Sstevel@tonic-gate 	}
927*0Sstevel@tonic-gate 
928*0Sstevel@tonic-gate 	/*
929*0Sstevel@tonic-gate 	 * Store the previous value that is on the pic
930*0Sstevel@tonic-gate 	 * so that we can calculate the delta value
931*0Sstevel@tonic-gate 	 * later.
932*0Sstevel@tonic-gate 	 */
933*0Sstevel@tonic-gate 	evt_node->prev_count = cnt_data[pic_num+1].value.ui64;
934*0Sstevel@tonic-gate 
935*0Sstevel@tonic-gate 
936*0Sstevel@tonic-gate 	/*
937*0Sstevel@tonic-gate 	 * Read the current pcr value from device.
938*0Sstevel@tonic-gate 	 */
939*0Sstevel@tonic-gate 	pcr = cnt_data[0].value.ui64;
940*0Sstevel@tonic-gate 
941*0Sstevel@tonic-gate 	/*
942*0Sstevel@tonic-gate 	 * Clear the section of the pcr which corresponds to the
943*0Sstevel@tonic-gate 	 * pic we are setting events on. Also clear the pcr value
944*0Sstevel@tonic-gate 	 * which is stored in the instance node.
945*0Sstevel@tonic-gate 	 *
946*0Sstevel@tonic-gate 	 */
947*0Sstevel@tonic-gate 	pcr = pcr & clear_pcr_mask;
948*0Sstevel@tonic-gate 
949*0Sstevel@tonic-gate 	/*
950*0Sstevel@tonic-gate 	 * Set the event.
951*0Sstevel@tonic-gate 	 */
952*0Sstevel@tonic-gate 	pcr = pcr | evt_node->evt_pcr_mask;
953*0Sstevel@tonic-gate 	cnt_data[0].value.ui64 = pcr;
954*0Sstevel@tonic-gate 
955*0Sstevel@tonic-gate 	/*
956*0Sstevel@tonic-gate 	 * Write the value back to the kstat, to make it
957*0Sstevel@tonic-gate 	 * visible to the underlying driver.
958*0Sstevel@tonic-gate 	 */
959*0Sstevel@tonic-gate 	if (kstat_write(kc, cnt_ksp, NULL) == FAIL) {
960*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: could not set events "
961*0Sstevel@tonic-gate 					"(setting events requires root "
962*0Sstevel@tonic-gate 					    "permission).\n"), pgmname);
963*0Sstevel@tonic-gate 		exit(1);
964*0Sstevel@tonic-gate 	}
965*0Sstevel@tonic-gate }
966*0Sstevel@tonic-gate 
967*0Sstevel@tonic-gate 
968*0Sstevel@tonic-gate /*
969*0Sstevel@tonic-gate  * Works through the list of device nodes, reading events
970*0Sstevel@tonic-gate  * and where appropiate setting new events (multiplexing).
971*0Sstevel@tonic-gate  */
972*0Sstevel@tonic-gate void
read_evts()973*0Sstevel@tonic-gate read_evts()
974*0Sstevel@tonic-gate {
975*0Sstevel@tonic-gate 	dev_node_t	*dev_node;
976*0Sstevel@tonic-gate 	kstat_t		*cnt_ksp;
977*0Sstevel@tonic-gate 	kstat_named_t	*cnt_data;
978*0Sstevel@tonic-gate 	char		tmp_str[30];
979*0Sstevel@tonic-gate 	int		iter = 0;
980*0Sstevel@tonic-gate 
981*0Sstevel@tonic-gate 	dev_node = dev_list_head;
982*0Sstevel@tonic-gate 
983*0Sstevel@tonic-gate 	while (dev_node != NULL) {
984*0Sstevel@tonic-gate 		if (iter == 0)
985*0Sstevel@tonic-gate 			print_timestamp();
986*0Sstevel@tonic-gate 		/*
987*0Sstevel@tonic-gate 		 * First read of all the counters is done
988*0Sstevel@tonic-gate 		 * to establish a baseline for the counts.
989*0Sstevel@tonic-gate 		 * This data is not printed.
990*0Sstevel@tonic-gate 		 */
991*0Sstevel@tonic-gate 		if ((!initial_read) && (iter == 0)) {
992*0Sstevel@tonic-gate 			(void) snprintf(tmp_str, sizeof (tmp_str), "%s%d",
993*0Sstevel@tonic-gate 				dev_node->name, dev_node->dev_inst);
994*0Sstevel@tonic-gate 			(void) printf("%-7s", tmp_str);
995*0Sstevel@tonic-gate 		}
996*0Sstevel@tonic-gate 
997*0Sstevel@tonic-gate 		cnt_ksp = (kstat_t *)dev_node->cnt_ksp;
998*0Sstevel@tonic-gate 
999*0Sstevel@tonic-gate 		if (kstat_read(kc, cnt_ksp, NULL) == FAIL) {
1000*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: device %s%d "
1001*0Sstevel@tonic-gate 				"(pic %d) no longer valid.\n"),
1002*0Sstevel@tonic-gate 				    pgmname, dev_node->name,
1003*0Sstevel@tonic-gate 				    dev_node->dev_inst,
1004*0Sstevel@tonic-gate 				    dev_node->pic_num);
1005*0Sstevel@tonic-gate 			remove_dev_node(dev_node);
1006*0Sstevel@tonic-gate 			dev_node = dev_list_head;
1007*0Sstevel@tonic-gate 			continue;
1008*0Sstevel@tonic-gate 		}
1009*0Sstevel@tonic-gate 
1010*0Sstevel@tonic-gate 		cnt_data = (kstat_named_t *)cnt_ksp->ks_data;
1011*0Sstevel@tonic-gate 
1012*0Sstevel@tonic-gate 		if (dev_node->r_w == EVT_READ) {
1013*0Sstevel@tonic-gate 			read_r_evt_node(dev_node, dev_node->pic_num, cnt_data);
1014*0Sstevel@tonic-gate 			iter++;
1015*0Sstevel@tonic-gate 		} else {
1016*0Sstevel@tonic-gate 			read_w_evt_node(dev_node, dev_node->pic_num, cnt_data);
1017*0Sstevel@tonic-gate 			iter++;
1018*0Sstevel@tonic-gate 		}
1019*0Sstevel@tonic-gate 
1020*0Sstevel@tonic-gate 		if ((!initial_read) && (iter == max_pic_num)) {
1021*0Sstevel@tonic-gate 			iter = 0;
1022*0Sstevel@tonic-gate 			(void) printf("\n");
1023*0Sstevel@tonic-gate 		}
1024*0Sstevel@tonic-gate 
1025*0Sstevel@tonic-gate 		/*
1026*0Sstevel@tonic-gate 		 * If there is more than one event node
1027*0Sstevel@tonic-gate 		 * per-pic then we are multiplexing.
1028*0Sstevel@tonic-gate 		 */
1029*0Sstevel@tonic-gate 		if ((dev_node->evt_node->next != dev_node->evt_node) &&
1030*0Sstevel@tonic-gate 			(!initial_read)) {
1031*0Sstevel@tonic-gate 				dev_node->evt_node = dev_node->evt_node->next;
1032*0Sstevel@tonic-gate 				set_evt(dev_node);
1033*0Sstevel@tonic-gate 		}
1034*0Sstevel@tonic-gate 		dev_node = dev_node->next;
1035*0Sstevel@tonic-gate 	}
1036*0Sstevel@tonic-gate 	initial_read = FALSE;
1037*0Sstevel@tonic-gate }
1038*0Sstevel@tonic-gate 
1039*0Sstevel@tonic-gate 
1040*0Sstevel@tonic-gate /*
1041*0Sstevel@tonic-gate  * Read a node that is marked as EVT_READ
1042*0Sstevel@tonic-gate  */
1043*0Sstevel@tonic-gate void
read_r_evt_node(dev_node_t * dev_node,int pic_num,kstat_named_t * cnt_data)1044*0Sstevel@tonic-gate read_r_evt_node(dev_node_t *dev_node, int pic_num, kstat_named_t *cnt_data)
1045*0Sstevel@tonic-gate {
1046*0Sstevel@tonic-gate 	evt_node_t	*evt_node;
1047*0Sstevel@tonic-gate 	kstat_t		*pic_ksp;
1048*0Sstevel@tonic-gate 	kstat_named_t	*pic_data;
1049*0Sstevel@tonic-gate 	uint64_t	pcr_read;
1050*0Sstevel@tonic-gate 	uint64_t	clear_pcr_mask;
1051*0Sstevel@tonic-gate 	uint64_t	delta_count;
1052*0Sstevel@tonic-gate 	int		i;
1053*0Sstevel@tonic-gate 	int		match = 0;
1054*0Sstevel@tonic-gate 	int		evt_blank = 1;
1055*0Sstevel@tonic-gate 
1056*0Sstevel@tonic-gate 	evt_node = dev_node->evt_node;
1057*0Sstevel@tonic-gate 
1058*0Sstevel@tonic-gate 	pic_ksp = (kstat_t *)dev_node->pic_ksp;
1059*0Sstevel@tonic-gate 
1060*0Sstevel@tonic-gate 	if (kstat_read(kc, pic_ksp, NULL) == FAIL) {
1061*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: device %s%d "
1062*0Sstevel@tonic-gate 			"(pic %d) no longer valid.\n"), pgmname,
1063*0Sstevel@tonic-gate 			    dev_node->name, dev_node->dev_inst,
1064*0Sstevel@tonic-gate 			    dev_node->pic_num);
1065*0Sstevel@tonic-gate 		remove_dev_node(dev_node);
1066*0Sstevel@tonic-gate 		return;
1067*0Sstevel@tonic-gate 	}
1068*0Sstevel@tonic-gate 
1069*0Sstevel@tonic-gate 	pic_data = (kstat_named_t *)pic_ksp->ks_data;
1070*0Sstevel@tonic-gate 	clear_pcr_mask = pic_data[pic_ksp->ks_ndata-1].value.ui64;
1071*0Sstevel@tonic-gate 
1072*0Sstevel@tonic-gate 	/*
1073*0Sstevel@tonic-gate 	 * Get PCR value from device. We extract the portion
1074*0Sstevel@tonic-gate 	 * of the PCR relating to the pic we are interested by
1075*0Sstevel@tonic-gate 	 * AND'ing the inverse of the clear mask for this pic.
1076*0Sstevel@tonic-gate 	 *
1077*0Sstevel@tonic-gate 	 * The clear mask is usually used to clear the appropiate
1078*0Sstevel@tonic-gate 	 * section of the PCR before we write events into it. So
1079*0Sstevel@tonic-gate 	 * by using the inverse of the mask, we zero everything
1080*0Sstevel@tonic-gate 	 * *but* the section we are interested in.
1081*0Sstevel@tonic-gate 	 */
1082*0Sstevel@tonic-gate 	pcr_read = cnt_data[0].value.ui64;
1083*0Sstevel@tonic-gate 	pcr_read = pcr_read & ~(clear_pcr_mask);
1084*0Sstevel@tonic-gate 
1085*0Sstevel@tonic-gate 	/*
1086*0Sstevel@tonic-gate 	 * If the event name is blank this is the first time that
1087*0Sstevel@tonic-gate 	 * this node has been accessed, so we read the pcr and
1088*0Sstevel@tonic-gate 	 * from that we get the event name if it exists.
1089*0Sstevel@tonic-gate 	 *
1090*0Sstevel@tonic-gate 	 * If the pcr read from the device does not match that
1091*0Sstevel@tonic-gate 	 * stored in the node, then it means that the event has
1092*0Sstevel@tonic-gate 	 * changed from its previous value, so we need to re-read
1093*0Sstevel@tonic-gate 	 * all the values.
1094*0Sstevel@tonic-gate 	 */
1095*0Sstevel@tonic-gate 	if ((strcmp(evt_node->evt_name, "") == 0) ||
1096*0Sstevel@tonic-gate 		(pcr_read != evt_node->evt_pcr_mask)) {
1097*0Sstevel@tonic-gate 
1098*0Sstevel@tonic-gate 		for (i = 0; i < pic_ksp->ks_ndata-1; i++) {
1099*0Sstevel@tonic-gate 			if (pcr_read == pic_data[i].value.ui64) {
1100*0Sstevel@tonic-gate 				match = TRUE;
1101*0Sstevel@tonic-gate 				break;
1102*0Sstevel@tonic-gate 			}
1103*0Sstevel@tonic-gate 		}
1104*0Sstevel@tonic-gate 
1105*0Sstevel@tonic-gate 		/*
1106*0Sstevel@tonic-gate 		 * Able to resolve pcr value to a event name.
1107*0Sstevel@tonic-gate 		 */
1108*0Sstevel@tonic-gate 		if (match) {
1109*0Sstevel@tonic-gate 			(void) strcpy(evt_node->evt_name, pic_data[i].name);
1110*0Sstevel@tonic-gate 			evt_node->evt_pcr_mask = pcr_read;
1111*0Sstevel@tonic-gate 			evt_node->total = 0;
1112*0Sstevel@tonic-gate 			evt_node->prev_count =
1113*0Sstevel@tonic-gate 				cnt_data[pic_num+1].value.ui64;
1114*0Sstevel@tonic-gate 
1115*0Sstevel@tonic-gate 			if ((evt_blank) && (!initial_read)) {
1116*0Sstevel@tonic-gate 				(void) printf("%s\t%-8d\t",
1117*0Sstevel@tonic-gate 					evt_node->evt_name, 0);
1118*0Sstevel@tonic-gate 				evt_blank = 0;
1119*0Sstevel@tonic-gate 			}
1120*0Sstevel@tonic-gate 
1121*0Sstevel@tonic-gate 		} else {
1122*0Sstevel@tonic-gate 			(void) sprintf(evt_node->evt_name, "0x%llx", pcr_read);
1123*0Sstevel@tonic-gate 			evt_node->evt_pcr_mask = pcr_read;
1124*0Sstevel@tonic-gate 			evt_node->total = 0;
1125*0Sstevel@tonic-gate 			evt_node->prev_count =
1126*0Sstevel@tonic-gate 				cnt_data[pic_num+1].value.ui64;
1127*0Sstevel@tonic-gate 
1128*0Sstevel@tonic-gate 			if ((evt_blank) && (!initial_read)) {
1129*0Sstevel@tonic-gate 				(void) printf("%s\t%-8d\t",
1130*0Sstevel@tonic-gate 					evt_node->evt_name, 0);
1131*0Sstevel@tonic-gate 				evt_blank = 0;
1132*0Sstevel@tonic-gate 			}
1133*0Sstevel@tonic-gate 
1134*0Sstevel@tonic-gate 		}
1135*0Sstevel@tonic-gate 	} else {
1136*0Sstevel@tonic-gate 		/* Deal with wraparound of the counters */
1137*0Sstevel@tonic-gate 		if (cnt_data[pic_num+1].value.ui64 < evt_node->prev_count) {
1138*0Sstevel@tonic-gate 
1139*0Sstevel@tonic-gate 			delta_count = (UINT32_MAX-evt_node->prev_count) +
1140*0Sstevel@tonic-gate 				cnt_data[pic_num+1].value.ui64;
1141*0Sstevel@tonic-gate 		} else {
1142*0Sstevel@tonic-gate 			/* Calcalate delta value */
1143*0Sstevel@tonic-gate 			delta_count = cnt_data[pic_num+1].value.ui64
1144*0Sstevel@tonic-gate 						- evt_node->prev_count;
1145*0Sstevel@tonic-gate 		}
1146*0Sstevel@tonic-gate 
1147*0Sstevel@tonic-gate 
1148*0Sstevel@tonic-gate 		/*
1149*0Sstevel@tonic-gate 		 * Store value so that we can calculate delta next
1150*0Sstevel@tonic-gate 		 * time through.
1151*0Sstevel@tonic-gate 		 */
1152*0Sstevel@tonic-gate 		evt_node->prev_count = cnt_data[pic_num+1].value.ui64;
1153*0Sstevel@tonic-gate 
1154*0Sstevel@tonic-gate 		/* Update count total */
1155*0Sstevel@tonic-gate 		evt_node->total += delta_count;
1156*0Sstevel@tonic-gate 
1157*0Sstevel@tonic-gate 		if (delta) {
1158*0Sstevel@tonic-gate 			(void) printf("%-20s %-9lld   ",
1159*0Sstevel@tonic-gate 				evt_node->evt_name, delta_count);
1160*0Sstevel@tonic-gate 		} else {
1161*0Sstevel@tonic-gate 
1162*0Sstevel@tonic-gate 			(void) printf("%-20s %-9lld   ",
1163*0Sstevel@tonic-gate 				evt_node->evt_name, evt_node->total);
1164*0Sstevel@tonic-gate 		}
1165*0Sstevel@tonic-gate 	}
1166*0Sstevel@tonic-gate }
1167*0Sstevel@tonic-gate 
1168*0Sstevel@tonic-gate 
1169*0Sstevel@tonic-gate /*
1170*0Sstevel@tonic-gate  * Read event nodes marked as EVT_WRITE
1171*0Sstevel@tonic-gate  */
1172*0Sstevel@tonic-gate void
read_w_evt_node(dev_node_t * dev_node,int pic_num,kstat_named_t * cnt_data)1173*0Sstevel@tonic-gate read_w_evt_node(dev_node_t *dev_node, int pic_num, kstat_named_t *cnt_data)
1174*0Sstevel@tonic-gate {
1175*0Sstevel@tonic-gate 	kstat_t		*pic_ksp;
1176*0Sstevel@tonic-gate 	kstat_named_t	*pic_data;
1177*0Sstevel@tonic-gate 	evt_node_t	*evt_node;
1178*0Sstevel@tonic-gate 	uint64_t	delta_count;
1179*0Sstevel@tonic-gate 	uint64_t	pcr_read;
1180*0Sstevel@tonic-gate 	uint64_t	clear_pcr_mask;
1181*0Sstevel@tonic-gate 
1182*0Sstevel@tonic-gate 	evt_node = dev_node->evt_node;
1183*0Sstevel@tonic-gate 
1184*0Sstevel@tonic-gate 	pic_ksp = (kstat_t *)dev_node->pic_ksp;
1185*0Sstevel@tonic-gate 
1186*0Sstevel@tonic-gate 	if (kstat_read(kc, pic_ksp, NULL) == FAIL) {
1187*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: could not read "
1188*0Sstevel@tonic-gate 			"%s%d\n"), pgmname, dev_node->name,
1189*0Sstevel@tonic-gate 			    dev_node->dev_inst);
1190*0Sstevel@tonic-gate 		remove_dev_node(dev_node);
1191*0Sstevel@tonic-gate 		return;
1192*0Sstevel@tonic-gate 	}
1193*0Sstevel@tonic-gate 
1194*0Sstevel@tonic-gate 	pic_data = (kstat_named_t *)pic_ksp->ks_data;
1195*0Sstevel@tonic-gate 	clear_pcr_mask = pic_data[pic_ksp->ks_ndata-1].value.ui64;
1196*0Sstevel@tonic-gate 
1197*0Sstevel@tonic-gate 	/*
1198*0Sstevel@tonic-gate 	 * Get PCR value from device. We extract the portion
1199*0Sstevel@tonic-gate 	 * of the PCR relating to the pic we are interested by
1200*0Sstevel@tonic-gate 	 * AND'ing the inverse of the clear mask for this pic.
1201*0Sstevel@tonic-gate 	 *
1202*0Sstevel@tonic-gate 	 * The clear mask is usually used to clear the appropiate
1203*0Sstevel@tonic-gate 	 * section of the PCR before we write events into it. So
1204*0Sstevel@tonic-gate 	 * by using the inverse of the mask, we zero everything
1205*0Sstevel@tonic-gate 	 * *but* the section we are interested in.
1206*0Sstevel@tonic-gate 	 */
1207*0Sstevel@tonic-gate 	pcr_read = cnt_data[0].value.ui64;
1208*0Sstevel@tonic-gate 	pcr_read = pcr_read & ~(clear_pcr_mask);
1209*0Sstevel@tonic-gate 
1210*0Sstevel@tonic-gate 	/*
1211*0Sstevel@tonic-gate 	 * If the pcr value from the device does not match the
1212*0Sstevel@tonic-gate 	 * stored value, then the events on at least one of the
1213*0Sstevel@tonic-gate 	 * pics must have been change by another busstat instance.
1214*0Sstevel@tonic-gate 	 *
1215*0Sstevel@tonic-gate 	 * Regard this as a fatal error.
1216*0Sstevel@tonic-gate 	 */
1217*0Sstevel@tonic-gate 	if (pcr_read != evt_node->evt_pcr_mask) {
1218*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: events changed (possibly "
1219*0Sstevel@tonic-gate 			"by another busstat).\n"), pgmname);
1220*0Sstevel@tonic-gate 		exit(2);
1221*0Sstevel@tonic-gate 	}
1222*0Sstevel@tonic-gate 
1223*0Sstevel@tonic-gate 	/*
1224*0Sstevel@tonic-gate 	 * Calculate delta, and then store value just read to allow us to
1225*0Sstevel@tonic-gate 	 * calculate delta next time around.
1226*0Sstevel@tonic-gate 	 */
1227*0Sstevel@tonic-gate 	/* Deal with wraparound of the counters */
1228*0Sstevel@tonic-gate 	if (cnt_data[pic_num+1].value.ui64 < evt_node->prev_count) {
1229*0Sstevel@tonic-gate 
1230*0Sstevel@tonic-gate 		delta_count = (UINT32_MAX-evt_node->prev_count) +
1231*0Sstevel@tonic-gate 			cnt_data[pic_num+1].value.ui64;
1232*0Sstevel@tonic-gate 	} else {
1233*0Sstevel@tonic-gate 		/* Calcalate delta value */
1234*0Sstevel@tonic-gate 		delta_count = cnt_data[pic_num+1].value.ui64
1235*0Sstevel@tonic-gate 			- evt_node->prev_count;
1236*0Sstevel@tonic-gate 	}
1237*0Sstevel@tonic-gate 
1238*0Sstevel@tonic-gate 	evt_node->prev_count = cnt_data[pic_num+1].value.ui64;
1239*0Sstevel@tonic-gate 
1240*0Sstevel@tonic-gate 	if (initial_read) {
1241*0Sstevel@tonic-gate 		evt_node->total = 0;
1242*0Sstevel@tonic-gate 
1243*0Sstevel@tonic-gate 	} else {
1244*0Sstevel@tonic-gate 		/* Update count total */
1245*0Sstevel@tonic-gate 		evt_node->total += delta_count;
1246*0Sstevel@tonic-gate 
1247*0Sstevel@tonic-gate 		if (delta) {
1248*0Sstevel@tonic-gate 			(void) printf("%-20s %-9lld   ",
1249*0Sstevel@tonic-gate 				evt_node->evt_name, delta_count);
1250*0Sstevel@tonic-gate 		} else {
1251*0Sstevel@tonic-gate 			(void) printf("%-20s %-9lld   ",
1252*0Sstevel@tonic-gate 				evt_node->evt_name, evt_node->total);
1253*0Sstevel@tonic-gate 		}
1254*0Sstevel@tonic-gate 	}
1255*0Sstevel@tonic-gate }
1256*0Sstevel@tonic-gate 
1257*0Sstevel@tonic-gate 
1258*0Sstevel@tonic-gate /*
1259*0Sstevel@tonic-gate  * Check to see if any DR operations have occured, and deal with the
1260*0Sstevel@tonic-gate  * consequences.
1261*0Sstevel@tonic-gate  *
1262*0Sstevel@tonic-gate  * Use the Kstat chain ID to check for DR operations. If the ID has
1263*0Sstevel@tonic-gate  * changed then some kstats on system have been modified, we check
1264*0Sstevel@tonic-gate  * all the data structures to see are they still valid. If they are
1265*0Sstevel@tonic-gate  * not we remove them.
1266*0Sstevel@tonic-gate  */
1267*0Sstevel@tonic-gate void
check_dr_ops()1268*0Sstevel@tonic-gate check_dr_ops()
1269*0Sstevel@tonic-gate {
1270*0Sstevel@tonic-gate 	dev_node_t	*dev_node;
1271*0Sstevel@tonic-gate 	kid_t		new_id;
1272*0Sstevel@tonic-gate 	kstat_t		*ksp;
1273*0Sstevel@tonic-gate 	int		match = 0;
1274*0Sstevel@tonic-gate 
1275*0Sstevel@tonic-gate 	if ((new_id = kstat_chain_update(kc)) < 0) {
1276*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: could not get "
1277*0Sstevel@tonic-gate 			"kstat chain id\n"), pgmname);
1278*0Sstevel@tonic-gate 		exit(1);
1279*0Sstevel@tonic-gate 	}
1280*0Sstevel@tonic-gate 
1281*0Sstevel@tonic-gate 	if (new_id == 0) {
1282*0Sstevel@tonic-gate 		/* Kstat chain has not changed. */
1283*0Sstevel@tonic-gate 		return;
1284*0Sstevel@tonic-gate 	}
1285*0Sstevel@tonic-gate 
1286*0Sstevel@tonic-gate 	/*
1287*0Sstevel@tonic-gate 	 * Scan the chain of device nodes, making sure that their associated
1288*0Sstevel@tonic-gate 	 * kstats are still present. If not we remove the appropiate node.
1289*0Sstevel@tonic-gate 	 */
1290*0Sstevel@tonic-gate 	dev_node = dev_list_head;
1291*0Sstevel@tonic-gate 
1292*0Sstevel@tonic-gate 	while (dev_node != NULL) {
1293*0Sstevel@tonic-gate 		for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
1294*0Sstevel@tonic-gate 			if ((strcmp("bus", ksp->ks_class) == 0) &&
1295*0Sstevel@tonic-gate 				(strcmp("counters", ksp->ks_name) == 0) &&
1296*0Sstevel@tonic-gate 				(strcmp(dev_node->name, ksp->ks_module) == 0) &&
1297*0Sstevel@tonic-gate 				(ksp->ks_instance == dev_node->dev_inst)) {
1298*0Sstevel@tonic-gate 					match = 1;
1299*0Sstevel@tonic-gate 					break;
1300*0Sstevel@tonic-gate 			}
1301*0Sstevel@tonic-gate 		}
1302*0Sstevel@tonic-gate 		if (match == 0) {
1303*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: device %s%d"
1304*0Sstevel@tonic-gate 				" (pic %d) no longer valid.\n"), pgmname,
1305*0Sstevel@tonic-gate 				    dev_node->name, dev_node->dev_inst,
1306*0Sstevel@tonic-gate 				    dev_node->pic_num);
1307*0Sstevel@tonic-gate 
1308*0Sstevel@tonic-gate 			remove_dev_node(dev_node);
1309*0Sstevel@tonic-gate 		}
1310*0Sstevel@tonic-gate 		dev_node = dev_node->next;
1311*0Sstevel@tonic-gate 	}
1312*0Sstevel@tonic-gate }
1313*0Sstevel@tonic-gate 
1314*0Sstevel@tonic-gate 
1315*0Sstevel@tonic-gate 
1316*0Sstevel@tonic-gate /*
1317*0Sstevel@tonic-gate  * Remove a device node and its associated event nodes.
1318*0Sstevel@tonic-gate  */
1319*0Sstevel@tonic-gate void
remove_dev_node(dev_node_t * dev_node)1320*0Sstevel@tonic-gate remove_dev_node(dev_node_t *dev_node)
1321*0Sstevel@tonic-gate {
1322*0Sstevel@tonic-gate 	dev_node_t	*curr_node;
1323*0Sstevel@tonic-gate 	dev_node_t	*prev_node;
1324*0Sstevel@tonic-gate 	evt_node_t	*curr_evt_node;
1325*0Sstevel@tonic-gate 	evt_node_t	*next_evt_node;
1326*0Sstevel@tonic-gate 	evt_node_t	*start_pos;
1327*0Sstevel@tonic-gate 
1328*0Sstevel@tonic-gate 	curr_node = dev_list_head;
1329*0Sstevel@tonic-gate 
1330*0Sstevel@tonic-gate 	if (curr_node == dev_node) {
1331*0Sstevel@tonic-gate 		dev_list_head = dev_node->next;
1332*0Sstevel@tonic-gate 
1333*0Sstevel@tonic-gate 		if (dev_list_head == NULL) {
1334*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: no "
1335*0Sstevel@tonic-gate 				"devices left to monitor.\n"),
1336*0Sstevel@tonic-gate 				    pgmname);
1337*0Sstevel@tonic-gate 			exit(1);
1338*0Sstevel@tonic-gate 		}
1339*0Sstevel@tonic-gate 
1340*0Sstevel@tonic-gate 		/* Remove each event node first */
1341*0Sstevel@tonic-gate 		start_pos = dev_node->evt_node;
1342*0Sstevel@tonic-gate 		curr_evt_node = start_pos->next;
1343*0Sstevel@tonic-gate 
1344*0Sstevel@tonic-gate 		while (curr_evt_node != start_pos) {
1345*0Sstevel@tonic-gate 			next_evt_node = curr_evt_node->next;
1346*0Sstevel@tonic-gate 
1347*0Sstevel@tonic-gate 			free(curr_evt_node);
1348*0Sstevel@tonic-gate 			curr_evt_node = next_evt_node;
1349*0Sstevel@tonic-gate 		}
1350*0Sstevel@tonic-gate 
1351*0Sstevel@tonic-gate 		free(start_pos);
1352*0Sstevel@tonic-gate 		free(dev_node);
1353*0Sstevel@tonic-gate 		return;
1354*0Sstevel@tonic-gate 	}
1355*0Sstevel@tonic-gate 
1356*0Sstevel@tonic-gate 	/* Find the device node */
1357*0Sstevel@tonic-gate 	prev_node = dev_list_head;
1358*0Sstevel@tonic-gate 	curr_node = prev_node->next;
1359*0Sstevel@tonic-gate 
1360*0Sstevel@tonic-gate 	while (curr_node != NULL) {
1361*0Sstevel@tonic-gate 		if (curr_node == dev_node) {
1362*0Sstevel@tonic-gate 			prev_node->next = curr_node->next;
1363*0Sstevel@tonic-gate 
1364*0Sstevel@tonic-gate 			/* Remove each event node first */
1365*0Sstevel@tonic-gate 			start_pos = dev_node->evt_node;
1366*0Sstevel@tonic-gate 			curr_evt_node = start_pos->next;
1367*0Sstevel@tonic-gate 
1368*0Sstevel@tonic-gate 			while (curr_evt_node != start_pos) {
1369*0Sstevel@tonic-gate 				next_evt_node = curr_evt_node->next;
1370*0Sstevel@tonic-gate 
1371*0Sstevel@tonic-gate 				free(curr_evt_node);
1372*0Sstevel@tonic-gate 				curr_evt_node = next_evt_node;
1373*0Sstevel@tonic-gate 			}
1374*0Sstevel@tonic-gate 			free(start_pos);
1375*0Sstevel@tonic-gate 
1376*0Sstevel@tonic-gate 			free(dev_node);
1377*0Sstevel@tonic-gate 			return;
1378*0Sstevel@tonic-gate 		}
1379*0Sstevel@tonic-gate 		prev_node = curr_node;
1380*0Sstevel@tonic-gate 		curr_node = curr_node->next;
1381*0Sstevel@tonic-gate 	}
1382*0Sstevel@tonic-gate }
1383*0Sstevel@tonic-gate 
1384*0Sstevel@tonic-gate 
1385*0Sstevel@tonic-gate /*
1386*0Sstevel@tonic-gate  * Find a device node in the linked list of dev_nodes. Match
1387*0Sstevel@tonic-gate  * is done on device name, and instance number.
1388*0Sstevel@tonic-gate  */
1389*0Sstevel@tonic-gate dev_node_t *
find_dev_node(char * name,int inst_num,int pic_num)1390*0Sstevel@tonic-gate find_dev_node(char *name, int inst_num, int pic_num)
1391*0Sstevel@tonic-gate {
1392*0Sstevel@tonic-gate 	dev_node_t	*curr_node;
1393*0Sstevel@tonic-gate 
1394*0Sstevel@tonic-gate 	curr_node = dev_list_head;
1395*0Sstevel@tonic-gate 
1396*0Sstevel@tonic-gate 	while (curr_node != NULL) {
1397*0Sstevel@tonic-gate 		if ((strcmp(curr_node->name, name) == 0) &&
1398*0Sstevel@tonic-gate 			(curr_node->dev_inst == inst_num) &&
1399*0Sstevel@tonic-gate 			(curr_node->pic_num == pic_num)) {
1400*0Sstevel@tonic-gate 				return (curr_node);
1401*0Sstevel@tonic-gate 		}
1402*0Sstevel@tonic-gate 
1403*0Sstevel@tonic-gate 		curr_node = curr_node->next;
1404*0Sstevel@tonic-gate 	}
1405*0Sstevel@tonic-gate 
1406*0Sstevel@tonic-gate 	return (NULL);
1407*0Sstevel@tonic-gate }
1408*0Sstevel@tonic-gate 
1409*0Sstevel@tonic-gate 
1410*0Sstevel@tonic-gate /*
1411*0Sstevel@tonic-gate  * Determines whether the string represents a event name
1412*0Sstevel@tonic-gate  * or a numeric value. Numeric value can be dec, hex
1413*0Sstevel@tonic-gate  * or octal. All are converted to long int.
1414*0Sstevel@tonic-gate  */
1415*0Sstevel@tonic-gate int64_t
is_num(char * name)1416*0Sstevel@tonic-gate is_num(char *name)
1417*0Sstevel@tonic-gate {
1418*0Sstevel@tonic-gate 	char	*remainder = NULL;
1419*0Sstevel@tonic-gate 	int64_t	num;
1420*0Sstevel@tonic-gate 
1421*0Sstevel@tonic-gate 	num = (int64_t)strtol(name, &remainder, 0);
1422*0Sstevel@tonic-gate 
1423*0Sstevel@tonic-gate 	if (name == remainder) {
1424*0Sstevel@tonic-gate 		return (EVT_STR);
1425*0Sstevel@tonic-gate 	} else {
1426*0Sstevel@tonic-gate 		return (num);
1427*0Sstevel@tonic-gate 	}
1428*0Sstevel@tonic-gate }
1429*0Sstevel@tonic-gate 
1430*0Sstevel@tonic-gate 
1431*0Sstevel@tonic-gate /*
1432*0Sstevel@tonic-gate  * Find a pointer to the specified picN kstat. First
1433*0Sstevel@tonic-gate  * search for the specific kstat, and if that can't
1434*0Sstevel@tonic-gate  * be found search for any picN kstat belonging to this device.
1435*0Sstevel@tonic-gate  */
1436*0Sstevel@tonic-gate kstat_t *
find_pic_kstat(char * dev_name,int inst_num,char * pic)1437*0Sstevel@tonic-gate find_pic_kstat(char *dev_name, int inst_num, char *pic)
1438*0Sstevel@tonic-gate {
1439*0Sstevel@tonic-gate 	kstat_t	*ksp;
1440*0Sstevel@tonic-gate 	kstat_t	*p_ksp;
1441*0Sstevel@tonic-gate 
1442*0Sstevel@tonic-gate 	/* Look for specific picN kstat */
1443*0Sstevel@tonic-gate 	if ((p_ksp = kstat_lookup(kc, dev_name, inst_num, pic)) == NULL) {
1444*0Sstevel@tonic-gate 
1445*0Sstevel@tonic-gate 		for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
1446*0Sstevel@tonic-gate 			if ((strcmp(ksp->ks_class, "bus") == 0) &&
1447*0Sstevel@tonic-gate 				(strcmp(ksp->ks_name, pic) == 0) &&
1448*0Sstevel@tonic-gate 				(strcmp(ksp->ks_module, dev_name) == 0)) {
1449*0Sstevel@tonic-gate 
1450*0Sstevel@tonic-gate 						return (ksp);
1451*0Sstevel@tonic-gate 			}
1452*0Sstevel@tonic-gate 		}
1453*0Sstevel@tonic-gate 	}
1454*0Sstevel@tonic-gate 	return (p_ksp);
1455*0Sstevel@tonic-gate }
1456*0Sstevel@tonic-gate 
1457*0Sstevel@tonic-gate 
1458*0Sstevel@tonic-gate /*
1459*0Sstevel@tonic-gate  * Print column titles.
1460*0Sstevel@tonic-gate  * Can be turned off by -n option.
1461*0Sstevel@tonic-gate  */
1462*0Sstevel@tonic-gate void
print_banner()1463*0Sstevel@tonic-gate print_banner()
1464*0Sstevel@tonic-gate {
1465*0Sstevel@tonic-gate 	int		i;
1466*0Sstevel@tonic-gate 
1467*0Sstevel@tonic-gate 	(void) printf("time dev    ");
1468*0Sstevel@tonic-gate 
1469*0Sstevel@tonic-gate 	for (i = 0; i < max_pic_num; i++)
1470*0Sstevel@tonic-gate 		(void) printf("event%d               "
1471*0Sstevel@tonic-gate 			"pic%d        ", i, i);
1472*0Sstevel@tonic-gate 
1473*0Sstevel@tonic-gate 	(void) printf("\n");
1474*0Sstevel@tonic-gate 
1475*0Sstevel@tonic-gate 	banner = FALSE;
1476*0Sstevel@tonic-gate }
1477*0Sstevel@tonic-gate 
1478*0Sstevel@tonic-gate 
1479*0Sstevel@tonic-gate /*
1480*0Sstevel@tonic-gate  * Print the elapsed time in seconds, since the last call.
1481*0Sstevel@tonic-gate  */
1482*0Sstevel@tonic-gate void
print_timestamp()1483*0Sstevel@tonic-gate print_timestamp()
1484*0Sstevel@tonic-gate {
1485*0Sstevel@tonic-gate 	static hrtime_t	curr_time = 0;
1486*0Sstevel@tonic-gate 	static hrtime_t total_elapsed = 0;
1487*0Sstevel@tonic-gate 	hrtime_t	new_time = 0;
1488*0Sstevel@tonic-gate 	hrtime_t	elapsed = 0;
1489*0Sstevel@tonic-gate 	hrtime_t	rem = 0;
1490*0Sstevel@tonic-gate 
1491*0Sstevel@tonic-gate 	if (initial_read)	{
1492*0Sstevel@tonic-gate 		curr_time = (uint64_t)gethrtime();
1493*0Sstevel@tonic-gate 		return;
1494*0Sstevel@tonic-gate 	}
1495*0Sstevel@tonic-gate 
1496*0Sstevel@tonic-gate 	new_time = gethrtime();
1497*0Sstevel@tonic-gate 
1498*0Sstevel@tonic-gate 	elapsed = (new_time - curr_time)/NANO;
1499*0Sstevel@tonic-gate 
1500*0Sstevel@tonic-gate 	/* Round up time value if necessary */
1501*0Sstevel@tonic-gate 	rem = (new_time - curr_time)%NANO;
1502*0Sstevel@tonic-gate 	if (rem >= NANO/2)
1503*0Sstevel@tonic-gate 		elapsed += 1;
1504*0Sstevel@tonic-gate 
1505*0Sstevel@tonic-gate 	total_elapsed += elapsed;
1506*0Sstevel@tonic-gate 
1507*0Sstevel@tonic-gate 	(void) printf("%-4llu ", total_elapsed);
1508*0Sstevel@tonic-gate 
1509*0Sstevel@tonic-gate 	curr_time = new_time;
1510*0Sstevel@tonic-gate }
1511*0Sstevel@tonic-gate 
1512*0Sstevel@tonic-gate 
1513*0Sstevel@tonic-gate void
usage()1514*0Sstevel@tonic-gate usage()
1515*0Sstevel@tonic-gate {
1516*0Sstevel@tonic-gate 	(void) printf(gettext("Usage : busstat [-a] [-h] [-l] [-n]\n"
1517*0Sstevel@tonic-gate 		"                [-e device-inst]\n"
1518*0Sstevel@tonic-gate 		"                [-w device-inst "
1519*0Sstevel@tonic-gate 					"[,pic0=<event>] [,picN=<event>] ]\n"
1520*0Sstevel@tonic-gate 		"                [-r device-inst]\n"
1521*0Sstevel@tonic-gate 		"                [ interval [count] ]\n"));
1522*0Sstevel@tonic-gate 
1523*0Sstevel@tonic-gate 	exit(2);
1524*0Sstevel@tonic-gate }
1525*0Sstevel@tonic-gate 
1526*0Sstevel@tonic-gate 
1527*0Sstevel@tonic-gate void *
safe_malloc(size_t size)1528*0Sstevel@tonic-gate safe_malloc(size_t size)
1529*0Sstevel@tonic-gate {
1530*0Sstevel@tonic-gate 	void *a;
1531*0Sstevel@tonic-gate 
1532*0Sstevel@tonic-gate 	if ((a = malloc(size)) == NULL) {
1533*0Sstevel@tonic-gate 		(void) fprintf(stderr,
1534*0Sstevel@tonic-gate 			gettext("%s: out of memory.\n"), pgmname);
1535*0Sstevel@tonic-gate 		exit(1);
1536*0Sstevel@tonic-gate 	}
1537*0Sstevel@tonic-gate 
1538*0Sstevel@tonic-gate 	return (a);
1539*0Sstevel@tonic-gate }
1540*0Sstevel@tonic-gate 
1541*0Sstevel@tonic-gate /*
1542*0Sstevel@tonic-gate  * Create and arm the timer.
1543*0Sstevel@tonic-gate  */
1544*0Sstevel@tonic-gate void
set_timer(int interval)1545*0Sstevel@tonic-gate set_timer(int interval)
1546*0Sstevel@tonic-gate {
1547*0Sstevel@tonic-gate 	timer_t		t_id;		/* Timer id */
1548*0Sstevel@tonic-gate 	itimerspec_t	time_struct;
1549*0Sstevel@tonic-gate 	struct sigevent	sig_struct;
1550*0Sstevel@tonic-gate 	struct sigaction act;
1551*0Sstevel@tonic-gate 
1552*0Sstevel@tonic-gate 	bzero(&sig_struct, sizeof (struct sigevent));
1553*0Sstevel@tonic-gate 	bzero(&act, sizeof (struct sigaction));
1554*0Sstevel@tonic-gate 
1555*0Sstevel@tonic-gate 	/* Create timer */
1556*0Sstevel@tonic-gate 	sig_struct.sigev_notify = SIGEV_SIGNAL;
1557*0Sstevel@tonic-gate 	sig_struct.sigev_signo = SIGUSR1;
1558*0Sstevel@tonic-gate 	sig_struct.sigev_value.sival_int = 0;
1559*0Sstevel@tonic-gate 
1560*0Sstevel@tonic-gate 	if (timer_create(CLOCK_REALTIME, &sig_struct, &t_id) != 0) {
1561*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: Timer creation failed.\n"),
1562*0Sstevel@tonic-gate 			pgmname);
1563*0Sstevel@tonic-gate 		exit(1);
1564*0Sstevel@tonic-gate 	}
1565*0Sstevel@tonic-gate 
1566*0Sstevel@tonic-gate 	act.sa_handler = handle_sig;
1567*0Sstevel@tonic-gate 
1568*0Sstevel@tonic-gate 	if (sigaction(SIGUSR1, &act, NULL) != 0) {
1569*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: could not setup signal "
1570*0Sstevel@tonic-gate 			"handler"), pgmname);
1571*0Sstevel@tonic-gate 		exit(1);
1572*0Sstevel@tonic-gate 	}
1573*0Sstevel@tonic-gate 
1574*0Sstevel@tonic-gate 	time_struct.it_value.tv_sec = interval;
1575*0Sstevel@tonic-gate 	time_struct.it_value.tv_nsec = 0;
1576*0Sstevel@tonic-gate 	time_struct.it_interval.tv_sec = interval;
1577*0Sstevel@tonic-gate 	time_struct.it_interval.tv_nsec = 0;
1578*0Sstevel@tonic-gate 
1579*0Sstevel@tonic-gate 	/* Arm timer */
1580*0Sstevel@tonic-gate 	if ((timer_settime(t_id, 0, &time_struct, NULL)) != 0) {
1581*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: Setting timer failed.\n"),
1582*0Sstevel@tonic-gate 			pgmname);
1583*0Sstevel@tonic-gate 		exit(1);
1584*0Sstevel@tonic-gate 	}
1585*0Sstevel@tonic-gate }
1586*0Sstevel@tonic-gate 
1587*0Sstevel@tonic-gate 
1588*0Sstevel@tonic-gate /* ARGSUSED */
1589*0Sstevel@tonic-gate void
handle_sig(int x)1590*0Sstevel@tonic-gate handle_sig(int x)
1591*0Sstevel@tonic-gate {
1592*0Sstevel@tonic-gate }
1593*0Sstevel@tonic-gate 
1594*0Sstevel@tonic-gate /*
1595*0Sstevel@tonic-gate  * return a boolean value indicating whether or not
1596*0Sstevel@tonic-gate  * a string consists solely of characters which are
1597*0Sstevel@tonic-gate  * digits 0..9
1598*0Sstevel@tonic-gate  */
1599*0Sstevel@tonic-gate int
strisnum(const char * s)1600*0Sstevel@tonic-gate strisnum(const char *s)
1601*0Sstevel@tonic-gate {
1602*0Sstevel@tonic-gate 	for (; *s != '\0'; s++) {
1603*0Sstevel@tonic-gate 		if (*s < '0' || *s > '9')
1604*0Sstevel@tonic-gate 			return (0);
1605*0Sstevel@tonic-gate 	}
1606*0Sstevel@tonic-gate 	return (1);
1607*0Sstevel@tonic-gate }
1608