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