xref: /onnv-gate/usr/src/cmd/rcap/rcapstat/rcapstat.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 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/stat.h>
31*0Sstevel@tonic-gate #include <stdlib.h>
32*0Sstevel@tonic-gate #include <fcntl.h>
33*0Sstevel@tonic-gate #include <errno.h>
34*0Sstevel@tonic-gate #include <stdio.h>
35*0Sstevel@tonic-gate #include <unistd.h>
36*0Sstevel@tonic-gate #include <string.h>
37*0Sstevel@tonic-gate #include <strings.h>
38*0Sstevel@tonic-gate #include <libintl.h>
39*0Sstevel@tonic-gate #include <locale.h>
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #include "rcapd.h"
42*0Sstevel@tonic-gate #include "utils.h"
43*0Sstevel@tonic-gate #include "rcapd_stat.h"
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate static char mode[RC_MODE_LEN];
46*0Sstevel@tonic-gate static rcapd_stat_hdr_t hdr;
47*0Sstevel@tonic-gate static int global;
48*0Sstevel@tonic-gate static int unformatted;
49*0Sstevel@tonic-gate static time_t stat_mod = 0;
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate typedef struct col {
52*0Sstevel@tonic-gate 	rcid_t		col_id;
53*0Sstevel@tonic-gate 	char		col_name[LC_NAME_LEN];
54*0Sstevel@tonic-gate 	uint64_t	col_nproc;
55*0Sstevel@tonic-gate 	uint64_t	col_vmsize;
56*0Sstevel@tonic-gate 	uint64_t	col_rsssize;
57*0Sstevel@tonic-gate 	uint64_t	col_rsslimit;
58*0Sstevel@tonic-gate 	uint64_t	col_paged_eff;
59*0Sstevel@tonic-gate 	uint64_t	col_paged_eff_old;
60*0Sstevel@tonic-gate 	uint64_t	col_paged_eff_avg;
61*0Sstevel@tonic-gate 	uint64_t	col_paged_att;
62*0Sstevel@tonic-gate 	uint64_t	col_paged_att_old;
63*0Sstevel@tonic-gate 	uint64_t	col_paged_att_avg;
64*0Sstevel@tonic-gate 	uint64_t	col_count;
65*0Sstevel@tonic-gate 	int		col_fresh;
66*0Sstevel@tonic-gate 	struct col	*col_next;
67*0Sstevel@tonic-gate 	struct col	*col_prev;
68*0Sstevel@tonic-gate 	lcollection_stat_t	col_src_stat;
69*0Sstevel@tonic-gate 	lcollection_stat_t	col_old_stat;
70*0Sstevel@tonic-gate } col_t;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate static col_t *col_head;
73*0Sstevel@tonic-gate static int ncol;
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate static col_t *
76*0Sstevel@tonic-gate col_find(rcid_t id)
77*0Sstevel@tonic-gate {
78*0Sstevel@tonic-gate 	col_t *col;
79*0Sstevel@tonic-gate 	for (col = col_head; col != NULL; col = col->col_next)
80*0Sstevel@tonic-gate 		if (col->col_id == id)
81*0Sstevel@tonic-gate 			return (col);
82*0Sstevel@tonic-gate 	return (NULL);
83*0Sstevel@tonic-gate }
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate static col_t *
86*0Sstevel@tonic-gate col_insert(rcid_t id)
87*0Sstevel@tonic-gate {
88*0Sstevel@tonic-gate 	col_t *new_col;
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	new_col = malloc(sizeof (col_t));
91*0Sstevel@tonic-gate 	if (new_col == NULL) {
92*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("rcapstat: malloc() failed\n"));
93*0Sstevel@tonic-gate 		exit(E_ERROR);
94*0Sstevel@tonic-gate 	}
95*0Sstevel@tonic-gate 	(void) memset(new_col, 0, sizeof (col_t));
96*0Sstevel@tonic-gate 	new_col->col_next = col_head;
97*0Sstevel@tonic-gate 	new_col->col_id = id;
98*0Sstevel@tonic-gate 	if (col_head != NULL)
99*0Sstevel@tonic-gate 		col_head->col_prev = new_col;
100*0Sstevel@tonic-gate 	col_head = new_col;
101*0Sstevel@tonic-gate 	ncol++;
102*0Sstevel@tonic-gate 	return (new_col);
103*0Sstevel@tonic-gate }
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate static void
106*0Sstevel@tonic-gate col_remove(col_t *col)
107*0Sstevel@tonic-gate {
108*0Sstevel@tonic-gate 	if (col->col_prev != NULL)
109*0Sstevel@tonic-gate 		col->col_prev->col_next = col->col_next;
110*0Sstevel@tonic-gate 	if (col->col_next != NULL)
111*0Sstevel@tonic-gate 		col->col_next->col_prev = col->col_prev;
112*0Sstevel@tonic-gate 	if (col_head == col)
113*0Sstevel@tonic-gate 		col_head = col->col_next;
114*0Sstevel@tonic-gate 	ncol--;
115*0Sstevel@tonic-gate 	free(col);
116*0Sstevel@tonic-gate }
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate static void
119*0Sstevel@tonic-gate usage()
120*0Sstevel@tonic-gate {
121*0Sstevel@tonic-gate 	(void) fprintf(stderr,
122*0Sstevel@tonic-gate 	    gettext("usage: rcapstat [-g] [interval [count]]\n"));
123*0Sstevel@tonic-gate 	exit(E_USAGE);
124*0Sstevel@tonic-gate }
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate static void
127*0Sstevel@tonic-gate format_size(char *str, uint64_t size, int length)
128*0Sstevel@tonic-gate {
129*0Sstevel@tonic-gate 	char tag = 'K';
130*0Sstevel@tonic-gate 	if (size >= 10000) {
131*0Sstevel@tonic-gate 		size = (size + 512) / 1024;
132*0Sstevel@tonic-gate 		tag = 'M';
133*0Sstevel@tonic-gate 		if (size >= 10000) {
134*0Sstevel@tonic-gate 			size = (size + 512) / 1024;
135*0Sstevel@tonic-gate 			tag = 'G';
136*0Sstevel@tonic-gate 		}
137*0Sstevel@tonic-gate 	}
138*0Sstevel@tonic-gate 	(void) snprintf(str, length, "%4lld%c", size, tag);
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate static int
142*0Sstevel@tonic-gate read_stats()
143*0Sstevel@tonic-gate {
144*0Sstevel@tonic-gate 	int fd;
145*0Sstevel@tonic-gate 	int proc_fd;
146*0Sstevel@tonic-gate 	char procfile[20];
147*0Sstevel@tonic-gate 	pid_t pid;
148*0Sstevel@tonic-gate 	col_t *col, *col_next;
149*0Sstevel@tonic-gate 	lcollection_report_t report;
150*0Sstevel@tonic-gate 	struct stat st;
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	if ((fd = open(STAT_FILE_DEFAULT, O_RDONLY)) < 0) {
153*0Sstevel@tonic-gate 		warn(gettext("rcapd is not active\n"));
154*0Sstevel@tonic-gate 		return (E_ERROR);
155*0Sstevel@tonic-gate 	}
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	if (fstat(fd, &st) == 0)
158*0Sstevel@tonic-gate 		stat_mod = st.st_mtime;
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	if (read(fd, &hdr, sizeof (hdr)) != sizeof (hdr)) {
161*0Sstevel@tonic-gate 		(void) fprintf(stderr,
162*0Sstevel@tonic-gate 		    gettext("rcapstat: can't read stat file header: %s\n"),
163*0Sstevel@tonic-gate 		    strerror(errno));
164*0Sstevel@tonic-gate 		(void) close(fd);
165*0Sstevel@tonic-gate 		return (E_ERROR);
166*0Sstevel@tonic-gate 	}
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	/*
169*0Sstevel@tonic-gate 	 * Check if rcapd is running
170*0Sstevel@tonic-gate 	 */
171*0Sstevel@tonic-gate 	pid = hdr.rs_pid;
172*0Sstevel@tonic-gate 	(void) snprintf(procfile, 20, "/proc/%ld/psinfo", pid);
173*0Sstevel@tonic-gate 	if ((proc_fd = open(procfile, O_RDONLY)) < 0) {
174*0Sstevel@tonic-gate 		warn(gettext("rcapd is not active\n"));
175*0Sstevel@tonic-gate 		(void) close(fd);
176*0Sstevel@tonic-gate 		return (E_ERROR);
177*0Sstevel@tonic-gate 	}
178*0Sstevel@tonic-gate 	(void) close(proc_fd);
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	(void) strncpy(mode, hdr.rs_mode, RC_MODE_LEN);
181*0Sstevel@tonic-gate 	for (col = col_head; col != NULL; col = col->col_next) {
182*0Sstevel@tonic-gate 		col->col_fresh = 0;
183*0Sstevel@tonic-gate 		col->col_paged_eff = 0;
184*0Sstevel@tonic-gate 		col->col_paged_att = 0;
185*0Sstevel@tonic-gate 	}
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	while (read(fd, &report, sizeof (report)) == sizeof (report)) {
188*0Sstevel@tonic-gate 		col = col_find(report.lcol_id);
189*0Sstevel@tonic-gate 		if (col == NULL) {
190*0Sstevel@tonic-gate 			col = col_insert(report.lcol_id);
191*0Sstevel@tonic-gate 			col->col_paged_eff_old = col->col_paged_eff =
192*0Sstevel@tonic-gate 			    report.lcol_stat.lcols_pg_eff;
193*0Sstevel@tonic-gate 			col->col_paged_att_old = col->col_paged_att =
194*0Sstevel@tonic-gate 			    report.lcol_stat.lcols_pg_att;
195*0Sstevel@tonic-gate 			col->col_count = 0;
196*0Sstevel@tonic-gate 		}
197*0Sstevel@tonic-gate 		(void) strncpy(col->col_name, report.lcol_name, LC_NAME_LEN);
198*0Sstevel@tonic-gate 		col->col_vmsize = report.lcol_image_size;
199*0Sstevel@tonic-gate 		col->col_rsssize = report.lcol_rss;
200*0Sstevel@tonic-gate 		col->col_rsslimit = report.lcol_rss_cap;
201*0Sstevel@tonic-gate 		col->col_fresh = 1;
202*0Sstevel@tonic-gate 		if (report.lcol_stat.lcols_pg_eff > col->col_paged_eff_old) {
203*0Sstevel@tonic-gate 			col->col_paged_eff =
204*0Sstevel@tonic-gate 			    report.lcol_stat.lcols_pg_eff -
205*0Sstevel@tonic-gate 			    col->col_paged_eff_old;
206*0Sstevel@tonic-gate 			if (report.lcol_stat.lcols_scan_count > col->col_count)
207*0Sstevel@tonic-gate 				col->col_paged_eff_avg =
208*0Sstevel@tonic-gate 				    col->col_paged_eff /
209*0Sstevel@tonic-gate 				    (report.lcol_stat.lcols_scan_count -
210*0Sstevel@tonic-gate 				    col->col_count);
211*0Sstevel@tonic-gate 		} else {
212*0Sstevel@tonic-gate 			col->col_paged_eff_avg = 0;
213*0Sstevel@tonic-gate 		}
214*0Sstevel@tonic-gate 		if (report.lcol_stat.lcols_pg_att > col->col_paged_att_old) {
215*0Sstevel@tonic-gate 			col->col_paged_att =
216*0Sstevel@tonic-gate 			    report.lcol_stat.lcols_pg_att -
217*0Sstevel@tonic-gate 			    col->col_paged_att_old;
218*0Sstevel@tonic-gate 			if (report.lcol_stat.lcols_scan_count > col->col_count)
219*0Sstevel@tonic-gate 				col->col_paged_att_avg =
220*0Sstevel@tonic-gate 				    col->col_paged_att /
221*0Sstevel@tonic-gate 				    (report.lcol_stat.lcols_scan_count -
222*0Sstevel@tonic-gate 				    col->col_count);
223*0Sstevel@tonic-gate 		} else {
224*0Sstevel@tonic-gate 			col->col_paged_att_avg = 0;
225*0Sstevel@tonic-gate 		}
226*0Sstevel@tonic-gate 		col->col_paged_eff_old = report.lcol_stat.lcols_pg_eff;
227*0Sstevel@tonic-gate 		col->col_paged_att_old = report.lcol_stat.lcols_pg_att;
228*0Sstevel@tonic-gate 		col->col_nproc =
229*0Sstevel@tonic-gate 		    report.lcol_stat.lcols_proc_in -
230*0Sstevel@tonic-gate 		    report.lcol_stat.lcols_proc_out;
231*0Sstevel@tonic-gate 		col->col_count = report.lcol_stat.lcols_scan_count;
232*0Sstevel@tonic-gate 		col->col_src_stat = report.lcol_stat;
233*0Sstevel@tonic-gate 	}
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	/*
236*0Sstevel@tonic-gate 	 * Remove stale data
237*0Sstevel@tonic-gate 	 */
238*0Sstevel@tonic-gate 	col = col_head;
239*0Sstevel@tonic-gate 	while (col != NULL) {
240*0Sstevel@tonic-gate 		col_next = col->col_next;
241*0Sstevel@tonic-gate 		if (col->col_fresh == 0)
242*0Sstevel@tonic-gate 			col_remove(col);
243*0Sstevel@tonic-gate 		col = col_next;
244*0Sstevel@tonic-gate 	}
245*0Sstevel@tonic-gate 	(void) close(fd);
246*0Sstevel@tonic-gate 	return (E_SUCCESS);
247*0Sstevel@tonic-gate }
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate /*
250*0Sstevel@tonic-gate  * Print each collection's interval statistics.
251*0Sstevel@tonic-gate  */
252*0Sstevel@tonic-gate /*ARGSUSED*/
253*0Sstevel@tonic-gate static void
254*0Sstevel@tonic-gate print_unformatted_stats(void)
255*0Sstevel@tonic-gate {
256*0Sstevel@tonic-gate 	col_t *col;
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate #define	DELTA(field) \
259*0Sstevel@tonic-gate 	(col->col_src_stat.field - col->col_old_stat.field)
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	col = col_head;
262*0Sstevel@tonic-gate 	while (col != NULL) {
263*0Sstevel@tonic-gate 		if (bcmp(&col->col_src_stat, &col->col_old_stat,
264*0Sstevel@tonic-gate 		    sizeof (col->col_src_stat)) == 0) {
265*0Sstevel@tonic-gate 			col = col->col_next;
266*0Sstevel@tonic-gate 			continue;
267*0Sstevel@tonic-gate 		}
268*0Sstevel@tonic-gate 		(void) printf("%s %s status: succeeded/attempted (k): "
269*0Sstevel@tonic-gate 		    "%llu/%llu, ineffective/scans/unenforced/samplings:  "
270*0Sstevel@tonic-gate 		    "%llu/%llu/%llu/%llu, RSS min/max (k): %llu/%llu, cap %llu "
271*0Sstevel@tonic-gate 		    "kB, processes/thpt: %llu/%llu, %llu scans over %lld ms\n",
272*0Sstevel@tonic-gate 		    mode, col->col_name, DELTA(lcols_pg_eff),
273*0Sstevel@tonic-gate 		    DELTA(lcols_pg_att), DELTA(lcols_scan_ineffective),
274*0Sstevel@tonic-gate 		    DELTA(lcols_scan), DELTA(lcols_unenforced_cap),
275*0Sstevel@tonic-gate 		    DELTA(lcols_rss_sample), col->col_src_stat.lcols_min_rss,
276*0Sstevel@tonic-gate 		    col->col_src_stat.lcols_max_rss, col->col_rsslimit,
277*0Sstevel@tonic-gate 		    (col->col_src_stat.lcols_proc_in -
278*0Sstevel@tonic-gate 		    col->col_old_stat.lcols_proc_out), DELTA(lcols_proc_out),
279*0Sstevel@tonic-gate 		    DELTA(lcols_scan_count), DELTA(lcols_scan_time_complete) /
280*0Sstevel@tonic-gate 		    (NANOSEC / MILLISEC));
281*0Sstevel@tonic-gate 		col->col_old_stat = col->col_src_stat;
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate 		col = col->col_next;
284*0Sstevel@tonic-gate 	}
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 	if (global)
287*0Sstevel@tonic-gate 		(void) printf(gettext("physical memory utilization: %3u%%   "
288*0Sstevel@tonic-gate 		    "cap enforcement threshold: %3u%%\n"), hdr.rs_pressure_cur,
289*0Sstevel@tonic-gate 		    hdr.rs_pressure_cap);
290*0Sstevel@tonic-gate #undef DELTA
291*0Sstevel@tonic-gate }
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate static void
294*0Sstevel@tonic-gate print_stats()
295*0Sstevel@tonic-gate {
296*0Sstevel@tonic-gate 	col_t *col;
297*0Sstevel@tonic-gate 	char size[6];
298*0Sstevel@tonic-gate 	char limit[6];
299*0Sstevel@tonic-gate 	char rss[6];
300*0Sstevel@tonic-gate 	char paged_att[6];
301*0Sstevel@tonic-gate 	char paged_eff[6];
302*0Sstevel@tonic-gate 	char paged_att_avg[6];
303*0Sstevel@tonic-gate 	char paged_eff_avg[6];
304*0Sstevel@tonic-gate 	static int count = 0;
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 	/*
307*0Sstevel@tonic-gate 	 * Print a header once every 20 times if we're only displaying reports
308*0Sstevel@tonic-gate 	 * for one collection (10 times if -g is used).  Print a header every
309*0Sstevel@tonic-gate 	 * interval otherwise.
310*0Sstevel@tonic-gate 	 */
311*0Sstevel@tonic-gate 	if (count == 0 || ncol != 1)
312*0Sstevel@tonic-gate 		(void) printf("%6s %-15s %5s %5s %5s %5s %5s %5s %5s %5s\n",
313*0Sstevel@tonic-gate 		    "id", mode, "nproc", "vm", "rss", "cap",
314*0Sstevel@tonic-gate 		    "at", "avgat", "pg", "avgpg");
315*0Sstevel@tonic-gate 	if (++count >= 20 || (count >= 10 && global != 0) || ncol != 1)
316*0Sstevel@tonic-gate 		count = 0;
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate 	for (col = col_head; col != NULL; col = col->col_next) {
319*0Sstevel@tonic-gate 		format_size(size, col->col_vmsize, 6);
320*0Sstevel@tonic-gate 		format_size(rss, col->col_rsssize, 6);
321*0Sstevel@tonic-gate 		format_size(limit, col->col_rsslimit, 6);
322*0Sstevel@tonic-gate 		format_size(paged_att, col->col_paged_att, 6);
323*0Sstevel@tonic-gate 		format_size(paged_eff, col->col_paged_eff, 6);
324*0Sstevel@tonic-gate 		format_size(paged_att_avg, col->col_paged_att_avg, 6);
325*0Sstevel@tonic-gate 		format_size(paged_eff_avg, col->col_paged_eff_avg, 6);
326*0Sstevel@tonic-gate 		(void) printf("%6lld %-15s %5lld %5s %5s %5s %5s %5s %5s %5s\n",
327*0Sstevel@tonic-gate 		    (long long)col->col_id, col->col_name, col->col_nproc,
328*0Sstevel@tonic-gate 		    size, rss, limit,
329*0Sstevel@tonic-gate 		    paged_att, paged_att_avg,
330*0Sstevel@tonic-gate 		    paged_eff, paged_eff_avg);
331*0Sstevel@tonic-gate 	}
332*0Sstevel@tonic-gate 	if (global)
333*0Sstevel@tonic-gate 		(void) printf(gettext("physical memory utilization: %3u%%   "
334*0Sstevel@tonic-gate 		    "cap enforcement threshold: %3u%%\n"), hdr.rs_pressure_cur,
335*0Sstevel@tonic-gate 		    hdr.rs_pressure_cap);
336*0Sstevel@tonic-gate }
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate int
339*0Sstevel@tonic-gate main(int argc, char *argv[])
340*0Sstevel@tonic-gate {
341*0Sstevel@tonic-gate 	int interval = 5;
342*0Sstevel@tonic-gate 	int count;
343*0Sstevel@tonic-gate 	int always = 1;
344*0Sstevel@tonic-gate 	int opt;
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
347*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
348*0Sstevel@tonic-gate 	(void) setprogname("rcapstat");
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 	global = unformatted = 0;
351*0Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "gu")) != (int)EOF) {
352*0Sstevel@tonic-gate 		switch (opt) {
353*0Sstevel@tonic-gate 		case 'g':
354*0Sstevel@tonic-gate 			global = 1;
355*0Sstevel@tonic-gate 			break;
356*0Sstevel@tonic-gate 		case 'u':
357*0Sstevel@tonic-gate 			unformatted = 1;
358*0Sstevel@tonic-gate 			break;
359*0Sstevel@tonic-gate 		default:
360*0Sstevel@tonic-gate 			usage();
361*0Sstevel@tonic-gate 		}
362*0Sstevel@tonic-gate 	}
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate 	if (argc > optind)
365*0Sstevel@tonic-gate 		if ((interval = xatoi(argv[optind++])) <= 0)
366*0Sstevel@tonic-gate 			die(gettext("invalid interval specified\n"));
367*0Sstevel@tonic-gate 	if (argc > optind) {
368*0Sstevel@tonic-gate 		if ((count = xatoi(argv[optind++])) <= 0)
369*0Sstevel@tonic-gate 			die(gettext("invalid count specified\n"));
370*0Sstevel@tonic-gate 		always = 0;
371*0Sstevel@tonic-gate 	}
372*0Sstevel@tonic-gate 	if (argc > optind)
373*0Sstevel@tonic-gate 		usage();
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	while (always || count-- > 0) {
376*0Sstevel@tonic-gate 		if (read_stats() != E_SUCCESS)
377*0Sstevel@tonic-gate 			return (E_ERROR);
378*0Sstevel@tonic-gate 		if (!unformatted) {
379*0Sstevel@tonic-gate 			print_stats();
380*0Sstevel@tonic-gate 			fflush(stdout);
381*0Sstevel@tonic-gate 			if (count || always)
382*0Sstevel@tonic-gate 				(void) sleep(interval);
383*0Sstevel@tonic-gate 		} else {
384*0Sstevel@tonic-gate 			struct stat st;
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate 			print_unformatted_stats();
387*0Sstevel@tonic-gate 			fflush(stdout);
388*0Sstevel@tonic-gate 			while (stat(STAT_FILE_DEFAULT, &st) == 0 &&
389*0Sstevel@tonic-gate 			    st.st_mtime == stat_mod)
390*0Sstevel@tonic-gate 				usleep((useconds_t)(0.2 * MICROSEC));
391*0Sstevel@tonic-gate 		}
392*0Sstevel@tonic-gate 	}
393*0Sstevel@tonic-gate 
394*0Sstevel@tonic-gate 	return (E_SUCCESS);
395*0Sstevel@tonic-gate }
396