1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <stdlib.h>
31*0Sstevel@tonic-gate #include <string.h>
32*0Sstevel@tonic-gate #include <stdarg.h>
33*0Sstevel@tonic-gate #include <libintl.h>
34*0Sstevel@tonic-gate #include <sys/types.h>
35*0Sstevel@tonic-gate #include <sys/ioctl.h>
36*0Sstevel@tonic-gate #include <sys/stat.h>
37*0Sstevel@tonic-gate #include <kstat.h>
38*0Sstevel@tonic-gate #include <locale.h>
39*0Sstevel@tonic-gate #include <sys/fs/cachefs_log.h>
40*0Sstevel@tonic-gate #include "stats.h"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate void usage(char *);
43*0Sstevel@tonic-gate void pr_err(char *, ...);
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate static int zflag;
46*0Sstevel@tonic-gate char *prog;
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate static void print_stats(stats_cookie_t *, cachefs_kstat_key_t *, int);
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate int
51*0Sstevel@tonic-gate main(int argc, char **argv)
52*0Sstevel@tonic-gate {
53*0Sstevel@tonic-gate 	int rc = 0;
54*0Sstevel@tonic-gate 	int i, c, errflg = 0;
55*0Sstevel@tonic-gate 	stats_cookie_t *sc = NULL;
56*0Sstevel@tonic-gate 	cachefs_kstat_key_t *key;
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
59*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
60*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
61*0Sstevel@tonic-gate #endif /* TEXT_DOMAIN */
62*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 	if (prog = strrchr(argv[0], '/'))
65*0Sstevel@tonic-gate 		++prog;
66*0Sstevel@tonic-gate 	else
67*0Sstevel@tonic-gate 		prog = argv[0];
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "z")) != EOF)
70*0Sstevel@tonic-gate 		switch (c) {
71*0Sstevel@tonic-gate 		case 'z':
72*0Sstevel@tonic-gate 			++zflag;
73*0Sstevel@tonic-gate 			break;
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 		case '?':
76*0Sstevel@tonic-gate 		default:
77*0Sstevel@tonic-gate 			++errflg;
78*0Sstevel@tonic-gate 			break;
79*0Sstevel@tonic-gate 		}
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 	if (errflg) {
82*0Sstevel@tonic-gate 		usage(NULL);
83*0Sstevel@tonic-gate 		rc = -1;
84*0Sstevel@tonic-gate 		goto out;
85*0Sstevel@tonic-gate 	}
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	/*
88*0Sstevel@tonic-gate 	 * handle multiple mountpoints specified on command line
89*0Sstevel@tonic-gate 	 */
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	for (i = optind; i < argc; i++) {
92*0Sstevel@tonic-gate 		if ((sc = stats_create_mountpath(argv[i], prog)) == NULL) {
93*0Sstevel@tonic-gate 			pr_err(gettext("Cannot use %s"), argv[i]);
94*0Sstevel@tonic-gate 			rc = 1;
95*0Sstevel@tonic-gate 			continue;
96*0Sstevel@tonic-gate 		}
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 		if (stats_inerror(sc)) {
99*0Sstevel@tonic-gate 			pr_err(stats_errorstr(sc));
100*0Sstevel@tonic-gate 			rc = stats_errno(sc);
101*0Sstevel@tonic-gate 			continue;
102*0Sstevel@tonic-gate 		}
103*0Sstevel@tonic-gate 		print_stats(sc, key = stats_getkey(sc), zflag);
104*0Sstevel@tonic-gate 		if (stats_inerror(sc)) {
105*0Sstevel@tonic-gate 			pr_err(stats_errorstr(sc));
106*0Sstevel@tonic-gate 			rc = stats_errno(sc);
107*0Sstevel@tonic-gate 		}
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 		stats_destroy(sc);
110*0Sstevel@tonic-gate 		free(key);
111*0Sstevel@tonic-gate 	}
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	/*
114*0Sstevel@tonic-gate 	 * handle the case where no mountpoints were specified,
115*0Sstevel@tonic-gate 	 * i.e. show stats for all.
116*0Sstevel@tonic-gate 	 */
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	if (optind >= argc) {
119*0Sstevel@tonic-gate 		sc = stats_create_unbound(prog);
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 		while ((key = stats_next(sc)) != NULL) {
122*0Sstevel@tonic-gate 			if (! key->ks_mounted) {
123*0Sstevel@tonic-gate 				free(key);
124*0Sstevel@tonic-gate 				continue;
125*0Sstevel@tonic-gate 			}
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 			print_stats(sc, key, zflag);
128*0Sstevel@tonic-gate 			if (stats_inerror(sc)) {
129*0Sstevel@tonic-gate 				pr_err(stats_errorstr(sc));
130*0Sstevel@tonic-gate 				rc = stats_errno(sc);
131*0Sstevel@tonic-gate 			}
132*0Sstevel@tonic-gate 			free(key);
133*0Sstevel@tonic-gate 		}
134*0Sstevel@tonic-gate 		stats_destroy(sc);
135*0Sstevel@tonic-gate 	}
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate out:
138*0Sstevel@tonic-gate 	return (rc);
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate static void
142*0Sstevel@tonic-gate print_stats(stats_cookie_t *sc, cachefs_kstat_key_t *key, int zero)
143*0Sstevel@tonic-gate {
144*0Sstevel@tonic-gate 	uint_t misses, passes, fails, modifies;
145*0Sstevel@tonic-gate 	uint_t hitp, passtotal;
146*0Sstevel@tonic-gate 	uint_t gccount;
147*0Sstevel@tonic-gate 	u_longlong_t hits;
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	hits = (u_longlong_t)stats_hits(sc);
150*0Sstevel@tonic-gate 	misses = stats_misses(sc);
151*0Sstevel@tonic-gate 	if (hits + misses != 0)
152*0Sstevel@tonic-gate 		hitp = (uint_t)((100 * hits) / (hits + misses));
153*0Sstevel@tonic-gate 	else
154*0Sstevel@tonic-gate 		hitp = 100;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	passes = stats_passes(sc);
157*0Sstevel@tonic-gate 	fails = stats_fails(sc);
158*0Sstevel@tonic-gate 	passtotal = passes + fails;
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	modifies = stats_modifies(sc);
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	gccount = stats_gc_count(sc);
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	printf("\n    %s\n", (char *)key->ks_mountpoint);
165*0Sstevel@tonic-gate 	printf(gettext(
166*0Sstevel@tonic-gate 		"\t         cache hit rate: %5u%% (%llu hits, %u misses)\n"),
167*0Sstevel@tonic-gate 		hitp, hits, misses);
168*0Sstevel@tonic-gate 	printf(gettext("\t     consistency checks: %6d (%d pass, %d fail)\n"),
169*0Sstevel@tonic-gate 	    passtotal, passes, fails);
170*0Sstevel@tonic-gate 	printf(gettext("\t               modifies: %6d\n"), modifies);
171*0Sstevel@tonic-gate 	printf(gettext("\t     garbage collection: %6d\n"), gccount);
172*0Sstevel@tonic-gate 	if (gccount != 0) {
173*0Sstevel@tonic-gate 		time_t gctime = stats_gc_time(sc);
174*0Sstevel@tonic-gate 		time_t before = stats_gc_before(sc);
175*0Sstevel@tonic-gate 		time_t after = stats_gc_after(sc);
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 		if (gctime != (time_t)0)
178*0Sstevel@tonic-gate 			printf(gettext("\tlast garbage collection: %s"),
179*0Sstevel@tonic-gate 			    ctime(&gctime));
180*0Sstevel@tonic-gate 	}
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 	if (zero)
183*0Sstevel@tonic-gate 		(void) stats_zero_stats(sc);
184*0Sstevel@tonic-gate }
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate /*
188*0Sstevel@tonic-gate  *
189*0Sstevel@tonic-gate  *			usage
190*0Sstevel@tonic-gate  *
191*0Sstevel@tonic-gate  * Description:
192*0Sstevel@tonic-gate  *	Prints a short usage message.
193*0Sstevel@tonic-gate  * Arguments:
194*0Sstevel@tonic-gate  *	msgp	message to include with the usage message
195*0Sstevel@tonic-gate  * Returns:
196*0Sstevel@tonic-gate  * Preconditions:
197*0Sstevel@tonic-gate  */
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate void
200*0Sstevel@tonic-gate usage(char *msgp)
201*0Sstevel@tonic-gate {
202*0Sstevel@tonic-gate 	if (msgp) {
203*0Sstevel@tonic-gate 		pr_err("%s", msgp);
204*0Sstevel@tonic-gate 	}
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	fprintf(stderr,
207*0Sstevel@tonic-gate 	    gettext("Usage: cachefsstat [ -z ] [ path ... ]\n"));
208*0Sstevel@tonic-gate }
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate /*
211*0Sstevel@tonic-gate  *
212*0Sstevel@tonic-gate  *			pr_err
213*0Sstevel@tonic-gate  *
214*0Sstevel@tonic-gate  * Description:
215*0Sstevel@tonic-gate  *	Prints an error message to stderr.
216*0Sstevel@tonic-gate  * Arguments:
217*0Sstevel@tonic-gate  *	fmt	printf style format
218*0Sstevel@tonic-gate  *	...	arguments for fmt
219*0Sstevel@tonic-gate  * Returns:
220*0Sstevel@tonic-gate  * Preconditions:
221*0Sstevel@tonic-gate  *	precond(fmt)
222*0Sstevel@tonic-gate  */
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate void
225*0Sstevel@tonic-gate pr_err(char *fmt, ...)
226*0Sstevel@tonic-gate {
227*0Sstevel@tonic-gate 	va_list ap;
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	va_start(ap, fmt);
230*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("cachefsstat: "));
231*0Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
232*0Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
233*0Sstevel@tonic-gate 	va_end(ap);
234*0Sstevel@tonic-gate }
235