xref: /titanic_44/usr/src/cmd/fs.d/cachefs/cachefsstat/cachefsstat.c (revision 47644099886aa8d3f43120b9eede3044342be473)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*47644099Sgt29601  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <stdarg.h>
337c478bd9Sstevel@tonic-gate #include <libintl.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
367c478bd9Sstevel@tonic-gate #include <sys/stat.h>
377c478bd9Sstevel@tonic-gate #include <kstat.h>
387c478bd9Sstevel@tonic-gate #include <locale.h>
397c478bd9Sstevel@tonic-gate #include <sys/fs/cachefs_log.h>
407c478bd9Sstevel@tonic-gate #include "stats.h"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate void usage(char *);
437c478bd9Sstevel@tonic-gate void pr_err(char *, ...);
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static int zflag;
467c478bd9Sstevel@tonic-gate char *prog;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate static void print_stats(stats_cookie_t *, cachefs_kstat_key_t *, int);
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)517c478bd9Sstevel@tonic-gate main(int argc, char **argv)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	int rc = 0;
547c478bd9Sstevel@tonic-gate 	int i, c, errflg = 0;
557c478bd9Sstevel@tonic-gate 	stats_cookie_t *sc = NULL;
567c478bd9Sstevel@tonic-gate 	cachefs_kstat_key_t *key;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
597c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
607c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
617c478bd9Sstevel@tonic-gate #endif /* TEXT_DOMAIN */
627c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	if (prog = strrchr(argv[0], '/'))
657c478bd9Sstevel@tonic-gate 		++prog;
667c478bd9Sstevel@tonic-gate 	else
677c478bd9Sstevel@tonic-gate 		prog = argv[0];
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "z")) != EOF)
707c478bd9Sstevel@tonic-gate 		switch (c) {
717c478bd9Sstevel@tonic-gate 		case 'z':
727c478bd9Sstevel@tonic-gate 			++zflag;
737c478bd9Sstevel@tonic-gate 			break;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 		case '?':
767c478bd9Sstevel@tonic-gate 		default:
777c478bd9Sstevel@tonic-gate 			++errflg;
787c478bd9Sstevel@tonic-gate 			break;
797c478bd9Sstevel@tonic-gate 		}
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	if (errflg) {
827c478bd9Sstevel@tonic-gate 		usage(NULL);
837c478bd9Sstevel@tonic-gate 		rc = -1;
847c478bd9Sstevel@tonic-gate 		goto out;
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	/*
887c478bd9Sstevel@tonic-gate 	 * handle multiple mountpoints specified on command line
897c478bd9Sstevel@tonic-gate 	 */
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	for (i = optind; i < argc; i++) {
927c478bd9Sstevel@tonic-gate 		if ((sc = stats_create_mountpath(argv[i], prog)) == NULL) {
937c478bd9Sstevel@tonic-gate 			pr_err(gettext("Cannot use %s"), argv[i]);
947c478bd9Sstevel@tonic-gate 			rc = 1;
957c478bd9Sstevel@tonic-gate 			continue;
967c478bd9Sstevel@tonic-gate 		}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 		if (stats_inerror(sc)) {
997c478bd9Sstevel@tonic-gate 			pr_err(stats_errorstr(sc));
1007c478bd9Sstevel@tonic-gate 			rc = stats_errno(sc);
1017c478bd9Sstevel@tonic-gate 			continue;
1027c478bd9Sstevel@tonic-gate 		}
1037c478bd9Sstevel@tonic-gate 		print_stats(sc, key = stats_getkey(sc), zflag);
1047c478bd9Sstevel@tonic-gate 		if (stats_inerror(sc)) {
1057c478bd9Sstevel@tonic-gate 			pr_err(stats_errorstr(sc));
1067c478bd9Sstevel@tonic-gate 			rc = stats_errno(sc);
1077c478bd9Sstevel@tonic-gate 		}
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 		stats_destroy(sc);
1107c478bd9Sstevel@tonic-gate 		free(key);
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	/*
1147c478bd9Sstevel@tonic-gate 	 * handle the case where no mountpoints were specified,
1157c478bd9Sstevel@tonic-gate 	 * i.e. show stats for all.
1167c478bd9Sstevel@tonic-gate 	 */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	if (optind >= argc) {
1197c478bd9Sstevel@tonic-gate 		sc = stats_create_unbound(prog);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 		while ((key = stats_next(sc)) != NULL) {
1227c478bd9Sstevel@tonic-gate 			if (! key->ks_mounted) {
1237c478bd9Sstevel@tonic-gate 				free(key);
1247c478bd9Sstevel@tonic-gate 				continue;
1257c478bd9Sstevel@tonic-gate 			}
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 			print_stats(sc, key, zflag);
1287c478bd9Sstevel@tonic-gate 			if (stats_inerror(sc)) {
1297c478bd9Sstevel@tonic-gate 				pr_err(stats_errorstr(sc));
1307c478bd9Sstevel@tonic-gate 				rc = stats_errno(sc);
1317c478bd9Sstevel@tonic-gate 			}
1327c478bd9Sstevel@tonic-gate 			free(key);
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 		stats_destroy(sc);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate out:
1387c478bd9Sstevel@tonic-gate 	return (rc);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate static void
print_stats(stats_cookie_t * sc,cachefs_kstat_key_t * key,int zero)1427c478bd9Sstevel@tonic-gate print_stats(stats_cookie_t *sc, cachefs_kstat_key_t *key, int zero)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	uint_t misses, passes, fails, modifies;
1457c478bd9Sstevel@tonic-gate 	uint_t hitp, passtotal;
1467c478bd9Sstevel@tonic-gate 	uint_t gccount;
1477c478bd9Sstevel@tonic-gate 	u_longlong_t hits;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	hits = (u_longlong_t)stats_hits(sc);
1507c478bd9Sstevel@tonic-gate 	misses = stats_misses(sc);
1517c478bd9Sstevel@tonic-gate 	if (hits + misses != 0)
1527c478bd9Sstevel@tonic-gate 		hitp = (uint_t)((100 * hits) / (hits + misses));
1537c478bd9Sstevel@tonic-gate 	else
1547c478bd9Sstevel@tonic-gate 		hitp = 100;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	passes = stats_passes(sc);
1577c478bd9Sstevel@tonic-gate 	fails = stats_fails(sc);
1587c478bd9Sstevel@tonic-gate 	passtotal = passes + fails;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	modifies = stats_modifies(sc);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	gccount = stats_gc_count(sc);
1637c478bd9Sstevel@tonic-gate 
164*47644099Sgt29601 	printf("\n    %s\n", (char *)(uintptr_t)key->ks_mountpoint);
1657c478bd9Sstevel@tonic-gate 	printf(gettext(
1667c478bd9Sstevel@tonic-gate 		"\t         cache hit rate: %5u%% (%llu hits, %u misses)\n"),
1677c478bd9Sstevel@tonic-gate 		hitp, hits, misses);
1687c478bd9Sstevel@tonic-gate 	printf(gettext("\t     consistency checks: %6d (%d pass, %d fail)\n"),
1697c478bd9Sstevel@tonic-gate 	    passtotal, passes, fails);
1707c478bd9Sstevel@tonic-gate 	printf(gettext("\t               modifies: %6d\n"), modifies);
1717c478bd9Sstevel@tonic-gate 	printf(gettext("\t     garbage collection: %6d\n"), gccount);
1727c478bd9Sstevel@tonic-gate 	if (gccount != 0) {
1737c478bd9Sstevel@tonic-gate 		time_t gctime = stats_gc_time(sc);
1747c478bd9Sstevel@tonic-gate 		time_t before = stats_gc_before(sc);
1757c478bd9Sstevel@tonic-gate 		time_t after = stats_gc_after(sc);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 		if (gctime != (time_t)0)
1787c478bd9Sstevel@tonic-gate 			printf(gettext("\tlast garbage collection: %s"),
1797c478bd9Sstevel@tonic-gate 			    ctime(&gctime));
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if (zero)
1837c478bd9Sstevel@tonic-gate 		(void) stats_zero_stats(sc);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate  *
1897c478bd9Sstevel@tonic-gate  *			usage
1907c478bd9Sstevel@tonic-gate  *
1917c478bd9Sstevel@tonic-gate  * Description:
1927c478bd9Sstevel@tonic-gate  *	Prints a short usage message.
1937c478bd9Sstevel@tonic-gate  * Arguments:
1947c478bd9Sstevel@tonic-gate  *	msgp	message to include with the usage message
1957c478bd9Sstevel@tonic-gate  * Returns:
1967c478bd9Sstevel@tonic-gate  * Preconditions:
1977c478bd9Sstevel@tonic-gate  */
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate void
usage(char * msgp)2007c478bd9Sstevel@tonic-gate usage(char *msgp)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	if (msgp) {
2037c478bd9Sstevel@tonic-gate 		pr_err("%s", msgp);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	fprintf(stderr,
2077c478bd9Sstevel@tonic-gate 	    gettext("Usage: cachefsstat [ -z ] [ path ... ]\n"));
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate /*
2117c478bd9Sstevel@tonic-gate  *
2127c478bd9Sstevel@tonic-gate  *			pr_err
2137c478bd9Sstevel@tonic-gate  *
2147c478bd9Sstevel@tonic-gate  * Description:
2157c478bd9Sstevel@tonic-gate  *	Prints an error message to stderr.
2167c478bd9Sstevel@tonic-gate  * Arguments:
2177c478bd9Sstevel@tonic-gate  *	fmt	printf style format
2187c478bd9Sstevel@tonic-gate  *	...	arguments for fmt
2197c478bd9Sstevel@tonic-gate  * Returns:
2207c478bd9Sstevel@tonic-gate  * Preconditions:
2217c478bd9Sstevel@tonic-gate  *	precond(fmt)
2227c478bd9Sstevel@tonic-gate  */
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate void
pr_err(char * fmt,...)2257c478bd9Sstevel@tonic-gate pr_err(char *fmt, ...)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate 	va_list ap;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
2307c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("cachefsstat: "));
2317c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
2327c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
2337c478bd9Sstevel@tonic-gate 	va_end(ap);
2347c478bd9Sstevel@tonic-gate }
235