xref: /onnv-gate/usr/src/cmd/fs.d/cachefs/cachefsstat/cachefsstat.c (revision 633:04519cb7de3b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*633Sgt29601  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <stdarg.h>
330Sstevel@tonic-gate #include <libintl.h>
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <sys/ioctl.h>
360Sstevel@tonic-gate #include <sys/stat.h>
370Sstevel@tonic-gate #include <kstat.h>
380Sstevel@tonic-gate #include <locale.h>
390Sstevel@tonic-gate #include <sys/fs/cachefs_log.h>
400Sstevel@tonic-gate #include "stats.h"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate void usage(char *);
430Sstevel@tonic-gate void pr_err(char *, ...);
440Sstevel@tonic-gate 
450Sstevel@tonic-gate static int zflag;
460Sstevel@tonic-gate char *prog;
470Sstevel@tonic-gate 
480Sstevel@tonic-gate static void print_stats(stats_cookie_t *, cachefs_kstat_key_t *, int);
490Sstevel@tonic-gate 
500Sstevel@tonic-gate int
main(int argc,char ** argv)510Sstevel@tonic-gate main(int argc, char **argv)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate 	int rc = 0;
540Sstevel@tonic-gate 	int i, c, errflg = 0;
550Sstevel@tonic-gate 	stats_cookie_t *sc = NULL;
560Sstevel@tonic-gate 	cachefs_kstat_key_t *key;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
590Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
600Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
610Sstevel@tonic-gate #endif /* TEXT_DOMAIN */
620Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	if (prog = strrchr(argv[0], '/'))
650Sstevel@tonic-gate 		++prog;
660Sstevel@tonic-gate 	else
670Sstevel@tonic-gate 		prog = argv[0];
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "z")) != EOF)
700Sstevel@tonic-gate 		switch (c) {
710Sstevel@tonic-gate 		case 'z':
720Sstevel@tonic-gate 			++zflag;
730Sstevel@tonic-gate 			break;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 		case '?':
760Sstevel@tonic-gate 		default:
770Sstevel@tonic-gate 			++errflg;
780Sstevel@tonic-gate 			break;
790Sstevel@tonic-gate 		}
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	if (errflg) {
820Sstevel@tonic-gate 		usage(NULL);
830Sstevel@tonic-gate 		rc = -1;
840Sstevel@tonic-gate 		goto out;
850Sstevel@tonic-gate 	}
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	/*
880Sstevel@tonic-gate 	 * handle multiple mountpoints specified on command line
890Sstevel@tonic-gate 	 */
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	for (i = optind; i < argc; i++) {
920Sstevel@tonic-gate 		if ((sc = stats_create_mountpath(argv[i], prog)) == NULL) {
930Sstevel@tonic-gate 			pr_err(gettext("Cannot use %s"), argv[i]);
940Sstevel@tonic-gate 			rc = 1;
950Sstevel@tonic-gate 			continue;
960Sstevel@tonic-gate 		}
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 		if (stats_inerror(sc)) {
990Sstevel@tonic-gate 			pr_err(stats_errorstr(sc));
1000Sstevel@tonic-gate 			rc = stats_errno(sc);
1010Sstevel@tonic-gate 			continue;
1020Sstevel@tonic-gate 		}
1030Sstevel@tonic-gate 		print_stats(sc, key = stats_getkey(sc), zflag);
1040Sstevel@tonic-gate 		if (stats_inerror(sc)) {
1050Sstevel@tonic-gate 			pr_err(stats_errorstr(sc));
1060Sstevel@tonic-gate 			rc = stats_errno(sc);
1070Sstevel@tonic-gate 		}
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 		stats_destroy(sc);
1100Sstevel@tonic-gate 		free(key);
1110Sstevel@tonic-gate 	}
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	/*
1140Sstevel@tonic-gate 	 * handle the case where no mountpoints were specified,
1150Sstevel@tonic-gate 	 * i.e. show stats for all.
1160Sstevel@tonic-gate 	 */
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if (optind >= argc) {
1190Sstevel@tonic-gate 		sc = stats_create_unbound(prog);
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 		while ((key = stats_next(sc)) != NULL) {
1220Sstevel@tonic-gate 			if (! key->ks_mounted) {
1230Sstevel@tonic-gate 				free(key);
1240Sstevel@tonic-gate 				continue;
1250Sstevel@tonic-gate 			}
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 			print_stats(sc, key, zflag);
1280Sstevel@tonic-gate 			if (stats_inerror(sc)) {
1290Sstevel@tonic-gate 				pr_err(stats_errorstr(sc));
1300Sstevel@tonic-gate 				rc = stats_errno(sc);
1310Sstevel@tonic-gate 			}
1320Sstevel@tonic-gate 			free(key);
1330Sstevel@tonic-gate 		}
1340Sstevel@tonic-gate 		stats_destroy(sc);
1350Sstevel@tonic-gate 	}
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate out:
1380Sstevel@tonic-gate 	return (rc);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate static void
print_stats(stats_cookie_t * sc,cachefs_kstat_key_t * key,int zero)1420Sstevel@tonic-gate print_stats(stats_cookie_t *sc, cachefs_kstat_key_t *key, int zero)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate 	uint_t misses, passes, fails, modifies;
1450Sstevel@tonic-gate 	uint_t hitp, passtotal;
1460Sstevel@tonic-gate 	uint_t gccount;
1470Sstevel@tonic-gate 	u_longlong_t hits;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	hits = (u_longlong_t)stats_hits(sc);
1500Sstevel@tonic-gate 	misses = stats_misses(sc);
1510Sstevel@tonic-gate 	if (hits + misses != 0)
1520Sstevel@tonic-gate 		hitp = (uint_t)((100 * hits) / (hits + misses));
1530Sstevel@tonic-gate 	else
1540Sstevel@tonic-gate 		hitp = 100;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	passes = stats_passes(sc);
1570Sstevel@tonic-gate 	fails = stats_fails(sc);
1580Sstevel@tonic-gate 	passtotal = passes + fails;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	modifies = stats_modifies(sc);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	gccount = stats_gc_count(sc);
1630Sstevel@tonic-gate 
164*633Sgt29601 	printf("\n    %s\n", (char *)(uintptr_t)key->ks_mountpoint);
1650Sstevel@tonic-gate 	printf(gettext(
1660Sstevel@tonic-gate 		"\t         cache hit rate: %5u%% (%llu hits, %u misses)\n"),
1670Sstevel@tonic-gate 		hitp, hits, misses);
1680Sstevel@tonic-gate 	printf(gettext("\t     consistency checks: %6d (%d pass, %d fail)\n"),
1690Sstevel@tonic-gate 	    passtotal, passes, fails);
1700Sstevel@tonic-gate 	printf(gettext("\t               modifies: %6d\n"), modifies);
1710Sstevel@tonic-gate 	printf(gettext("\t     garbage collection: %6d\n"), gccount);
1720Sstevel@tonic-gate 	if (gccount != 0) {
1730Sstevel@tonic-gate 		time_t gctime = stats_gc_time(sc);
1740Sstevel@tonic-gate 		time_t before = stats_gc_before(sc);
1750Sstevel@tonic-gate 		time_t after = stats_gc_after(sc);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 		if (gctime != (time_t)0)
1780Sstevel@tonic-gate 			printf(gettext("\tlast garbage collection: %s"),
1790Sstevel@tonic-gate 			    ctime(&gctime));
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	if (zero)
1830Sstevel@tonic-gate 		(void) stats_zero_stats(sc);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate /*
1880Sstevel@tonic-gate  *
1890Sstevel@tonic-gate  *			usage
1900Sstevel@tonic-gate  *
1910Sstevel@tonic-gate  * Description:
1920Sstevel@tonic-gate  *	Prints a short usage message.
1930Sstevel@tonic-gate  * Arguments:
1940Sstevel@tonic-gate  *	msgp	message to include with the usage message
1950Sstevel@tonic-gate  * Returns:
1960Sstevel@tonic-gate  * Preconditions:
1970Sstevel@tonic-gate  */
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate void
usage(char * msgp)2000Sstevel@tonic-gate usage(char *msgp)
2010Sstevel@tonic-gate {
2020Sstevel@tonic-gate 	if (msgp) {
2030Sstevel@tonic-gate 		pr_err("%s", msgp);
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	fprintf(stderr,
2070Sstevel@tonic-gate 	    gettext("Usage: cachefsstat [ -z ] [ path ... ]\n"));
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate /*
2110Sstevel@tonic-gate  *
2120Sstevel@tonic-gate  *			pr_err
2130Sstevel@tonic-gate  *
2140Sstevel@tonic-gate  * Description:
2150Sstevel@tonic-gate  *	Prints an error message to stderr.
2160Sstevel@tonic-gate  * Arguments:
2170Sstevel@tonic-gate  *	fmt	printf style format
2180Sstevel@tonic-gate  *	...	arguments for fmt
2190Sstevel@tonic-gate  * Returns:
2200Sstevel@tonic-gate  * Preconditions:
2210Sstevel@tonic-gate  *	precond(fmt)
2220Sstevel@tonic-gate  */
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate void
pr_err(char * fmt,...)2250Sstevel@tonic-gate pr_err(char *fmt, ...)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate 	va_list ap;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	va_start(ap, fmt);
2300Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("cachefsstat: "));
2310Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
2320Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
2330Sstevel@tonic-gate 	va_end(ap);
2340Sstevel@tonic-gate }
235