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