Home | History | Annotate | Line # | Download | only in nfsstat
nfsstat.c revision 1.13
      1 /*	$NetBSD: nfsstat.c,v 1.13 1998/07/05 08:15:16 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Rick Macklem at The University of Guelph.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #ifndef lint
     41 __COPYRIGHT("@(#) Copyright (c) 1983, 1989, 1993\n\
     42 	The Regents of the University of California.  All rights reserved.\n");
     43 #endif /* not lint */
     44 
     45 #ifndef lint
     46 #if 0
     47 static char sccsid[] = "from: @(#)nfsstat.c	8.1 (Berkeley) 6/6/93";
     48 #else
     49 __RCSID("$NetBSD: nfsstat.c,v 1.13 1998/07/05 08:15:16 mrg Exp $");
     50 #endif
     51 #endif /* not lint */
     52 
     53 #include <sys/param.h>
     54 #include <sys/mount.h>
     55 #include <sys/sysctl.h>
     56 
     57 #include <nfs/rpcv2.h>
     58 #include <nfs/nfsproto.h>
     59 #include <nfs/nfs.h>
     60 
     61 #include <ctype.h>
     62 #include <err.h>
     63 #include <errno.h>
     64 #include <fcntl.h>
     65 #include <kvm.h>
     66 #include <limits.h>
     67 #include <nlist.h>
     68 #include <paths.h>
     69 #include <signal.h>
     70 #include <stdlib.h>
     71 #include <stdio.h>
     72 #include <string.h>
     73 #include <unistd.h>
     74 
     75 struct nlist nl[] = {
     76 #define	N_NFSSTAT	0
     77 	{ "_nfsstats" },
     78 	{ "" },
     79 };
     80 kvm_t *kd;
     81 
     82 void	catchalarm __P((int));
     83 void	intpr __P((u_long));
     84 int	main __P((int, char **));
     85 void	printhdr __P((void));
     86 void	sidewaysintpr __P((u_int, u_long));
     87 void	usage __P((void));
     88 
     89 int     printall, clientinfo, serverinfo;
     90 
     91 int
     92 main(argc, argv)
     93 	int argc;
     94 	char **argv;
     95 {
     96 	extern int optind;
     97 	extern char *optarg;
     98 	u_int interval;
     99 	int ch;
    100 	char *memf, *nlistf;
    101 	char errbuf[_POSIX2_LINE_MAX];
    102 
    103 	interval = 0;
    104 	memf = nlistf = NULL;
    105 	printall = 1;
    106 	while ((ch = getopt(argc, argv, "M:N:w:cs")) != -1)
    107 		switch(ch) {
    108 		case 'M':
    109 			memf = optarg;
    110 			break;
    111 		case 'N':
    112 			nlistf = optarg;
    113 			break;
    114 		case 'w':
    115 			interval = atoi(optarg);
    116 			break;
    117 		case 's':
    118 		        serverinfo = 1;
    119 			printall = 0;
    120 			break;
    121 		case 'c':
    122 		        clientinfo = 1;
    123 			printall = 0;
    124 			break;
    125 		case '?':
    126 		default:
    127 			usage();
    128 		}
    129 	argc -= optind;
    130 	argv += optind;
    131 
    132 #define	BACKWARD_COMPATIBILITY
    133 #ifdef	BACKWARD_COMPATIBILITY
    134 	if (*argv) {
    135 		interval = atoi(*argv);
    136 		if (*++argv) {
    137 			nlistf = *argv;
    138 			if (*++argv)
    139 				memf = *argv;
    140 		}
    141 	}
    142 #endif
    143 	/*
    144 	 * Discard setgid privileges if not the running kernel so that bad
    145 	 * guys can't print interesting stuff from kernel memory.
    146 	 */
    147 	if (nlistf != NULL || memf != NULL)
    148 		setgid(getgid());
    149 
    150 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == 0)
    151 		errx(1, "kvm_openfiles: %s", errbuf);
    152 	setgid(getgid());	/* do this now anyway */
    153 	if (kvm_nlist(kd, nl) != 0)
    154 		errx(1, "kvm_nlist: can't get names");
    155 
    156 	if (interval)
    157 		sidewaysintpr(interval, nl[N_NFSSTAT].n_value);
    158 	else
    159 		intpr(nl[N_NFSSTAT].n_value);
    160 	exit(0);
    161 }
    162 
    163 /*
    164  * Print a description of the nfs stats.
    165  */
    166 void
    167 intpr(nfsstataddr)
    168 	u_long nfsstataddr;
    169 {
    170 	struct nfsstats nfsstats;
    171 
    172 	if (kvm_read(kd, (u_long)nfsstataddr, (char *)&nfsstats,
    173 	     sizeof(struct nfsstats)) != sizeof(struct nfsstats))
    174 		errx(1, "kvm_read failed");
    175 	if (printall || clientinfo) {
    176 		printf("Client Info:\n");
    177 		printf("Rpc Counts:\n");
    178 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    179 		    "Getattr", "Setattr", "Lookup", "Readlink", "Read",
    180 		    "Write", "Create", "Remove");
    181 		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    182 		    nfsstats.rpccnt[NFSPROC_GETATTR],
    183 		    nfsstats.rpccnt[NFSPROC_SETATTR],
    184 		    nfsstats.rpccnt[NFSPROC_LOOKUP],
    185 		    nfsstats.rpccnt[NFSPROC_READLINK],
    186 		    nfsstats.rpccnt[NFSPROC_READ],
    187 		    nfsstats.rpccnt[NFSPROC_WRITE],
    188 		    nfsstats.rpccnt[NFSPROC_CREATE],
    189 		    nfsstats.rpccnt[NFSPROC_REMOVE]);
    190 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    191 		    "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
    192 		    "Readdir", "RdirPlus", "Access");
    193 		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    194 		    nfsstats.rpccnt[NFSPROC_RENAME],
    195 		    nfsstats.rpccnt[NFSPROC_LINK],
    196 		    nfsstats.rpccnt[NFSPROC_SYMLINK],
    197 		    nfsstats.rpccnt[NFSPROC_MKDIR],
    198 		    nfsstats.rpccnt[NFSPROC_RMDIR],
    199 		    nfsstats.rpccnt[NFSPROC_READDIR],
    200 		    nfsstats.rpccnt[NFSPROC_READDIRPLUS],
    201 		    nfsstats.rpccnt[NFSPROC_ACCESS]);
    202 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    203 		    "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
    204 		    "GLease", "Vacate", "Evict");
    205 		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    206 		    nfsstats.rpccnt[NFSPROC_MKNOD],
    207 		    nfsstats.rpccnt[NFSPROC_FSSTAT],
    208 		    nfsstats.rpccnt[NFSPROC_FSINFO],
    209 		    nfsstats.rpccnt[NFSPROC_PATHCONF],
    210 		    nfsstats.rpccnt[NFSPROC_COMMIT],
    211 		    nfsstats.rpccnt[NQNFSPROC_GETLEASE],
    212 		    nfsstats.rpccnt[NQNFSPROC_VACATED],
    213 		    nfsstats.rpccnt[NQNFSPROC_EVICTED]);
    214 		printf("Rpc Info:\n");
    215 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
    216 		    "TimedOut", "Invalid", "X Replies", "Retries", "Requests");
    217 		printf("%9d %9d %9d %9d %9d\n",
    218 		    nfsstats.rpctimeouts,
    219 		    nfsstats.rpcinvalid,
    220 		    nfsstats.rpcunexpected,
    221 		    nfsstats.rpcretries,
    222 		    nfsstats.rpcrequests);
    223 		printf("Cache Info:\n");
    224 		printf("%9.9s %9.9s %9.9s %9.9s",
    225 		    "Attr Hits", "Misses", "Lkup Hits", "Misses");
    226 		printf(" %9.9s %9.9s %9.9s %9.9s\n",
    227 		    "BioR Hits", "Misses", "BioW Hits", "Misses");
    228 		printf("%9d %9d %9d %9d",
    229 		    nfsstats.attrcache_hits, nfsstats.attrcache_misses,
    230 		    nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
    231 		printf(" %9d %9d %9d %9d\n",
    232 		    nfsstats.biocache_reads-nfsstats.read_bios,
    233 		    nfsstats.read_bios,
    234 		    nfsstats.biocache_writes-nfsstats.write_bios,
    235 		    nfsstats.write_bios);
    236 		printf("%9.9s %9.9s %9.9s %9.9s",
    237 		    "BioRLHits", "Misses", "BioD Hits", "Misses");
    238 		printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
    239 		printf("%9d %9d %9d %9d",
    240 		    nfsstats.biocache_readlinks-nfsstats.readlink_bios,
    241 		    nfsstats.readlink_bios,
    242 		    nfsstats.biocache_readdirs-nfsstats.readdir_bios,
    243 		    nfsstats.readdir_bios);
    244 		printf(" %9d %9d\n",
    245 		    nfsstats.direofcache_hits, nfsstats.direofcache_misses);
    246 	}
    247 	if (printall || (clientinfo && serverinfo)){
    248 		printf("\n");
    249 	}
    250 	if (printall || serverinfo){
    251 		printf("Server Info:\n");
    252 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    253 		    "Getattr", "Setattr", "Lookup", "Readlink", "Read",
    254 		    "Write", "Create", "Remove");
    255 		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    256 		    nfsstats.srvrpccnt[NFSPROC_GETATTR],
    257 		    nfsstats.srvrpccnt[NFSPROC_SETATTR],
    258 		    nfsstats.srvrpccnt[NFSPROC_LOOKUP],
    259 		    nfsstats.srvrpccnt[NFSPROC_READLINK],
    260 		    nfsstats.srvrpccnt[NFSPROC_READ],
    261 		    nfsstats.srvrpccnt[NFSPROC_WRITE],
    262 		    nfsstats.srvrpccnt[NFSPROC_CREATE],
    263 		    nfsstats.srvrpccnt[NFSPROC_REMOVE]);
    264 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    265 		    "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
    266 		    "Readdir", "RdirPlus", "Access");
    267 		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    268 		    nfsstats.srvrpccnt[NFSPROC_RENAME],
    269 		    nfsstats.srvrpccnt[NFSPROC_LINK],
    270 		    nfsstats.srvrpccnt[NFSPROC_SYMLINK],
    271 		    nfsstats.srvrpccnt[NFSPROC_MKDIR],
    272 		    nfsstats.srvrpccnt[NFSPROC_RMDIR],
    273 		    nfsstats.srvrpccnt[NFSPROC_READDIR],
    274 		    nfsstats.srvrpccnt[NFSPROC_READDIRPLUS],
    275 		    nfsstats.srvrpccnt[NFSPROC_ACCESS]);
    276 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    277 		    "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
    278 		    "GLease", "Vacate", "Evict");
    279 		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    280 		    nfsstats.srvrpccnt[NFSPROC_MKNOD],
    281 		    nfsstats.srvrpccnt[NFSPROC_FSSTAT],
    282 		    nfsstats.srvrpccnt[NFSPROC_FSINFO],
    283 		    nfsstats.srvrpccnt[NFSPROC_PATHCONF],
    284 		    nfsstats.srvrpccnt[NFSPROC_COMMIT],
    285 		    nfsstats.srvrpccnt[NQNFSPROC_GETLEASE],
    286 		    nfsstats.srvrpccnt[NQNFSPROC_VACATED],
    287 		    nfsstats.srvrpccnt[NQNFSPROC_EVICTED]);
    288 		printf("Server Ret-Failed\n");
    289 		printf("%17d\n", nfsstats.srvrpc_errs);
    290 		printf("Server Faults\n");
    291 		printf("%13d\n", nfsstats.srv_errs);
    292 		printf("Server Cache Stats:\n");
    293 		printf("%9.9s %9.9s %9.9s %9.9s\n",
    294 		    "Inprog", "Idem", "Non-idem", "Misses");
    295 		printf("%9d %9d %9d %9d\n",
    296 		    nfsstats.srvcache_inproghits,
    297 		    nfsstats.srvcache_idemdonehits,
    298 		    nfsstats.srvcache_nonidemdonehits,
    299 		    nfsstats.srvcache_misses);
    300 		printf("Server Lease Stats:\n");
    301 		printf("%9.9s %9.9s %9.9s\n",
    302 		    "Leases", "PeakL", "GLeases");
    303 		printf("%9d %9d %9d\n",
    304 		    nfsstats.srvnqnfs_leases,
    305 		    nfsstats.srvnqnfs_maxleases,
    306 		    nfsstats.srvnqnfs_getleases);
    307 		printf("Server Write Gathering:\n");
    308 		printf("%9.9s %9.9s %9.9s\n",
    309 		    "WriteOps", "WriteRPC", "Opsaved");
    310 		printf("%9d %9d %9d\n",
    311 		    nfsstats.srvvop_writes,
    312 		    nfsstats.srvrpccnt[NFSPROC_WRITE],
    313 		    nfsstats.srvrpccnt[NFSPROC_WRITE] - nfsstats.srvvop_writes);
    314 	}
    315 }
    316 
    317 u_char	signalled;			/* set if alarm goes off "early" */
    318 
    319 /*
    320  * Print a running summary of nfs statistics.
    321  * Repeat display every interval seconds, showing statistics
    322  * collected over that interval.  Assumes that interval is non-zero.
    323  * First line printed at top of screen is always cumulative.
    324  */
    325 void
    326 sidewaysintpr(interval, off)
    327 	u_int interval;
    328 	u_long off;
    329 {
    330 	struct nfsstats nfsstats, lastst;
    331 	int hdrcnt, oldmask;
    332 
    333 	(void)signal(SIGALRM, catchalarm);
    334 	signalled = 0;
    335 	(void)alarm(interval);
    336 	memset((caddr_t)&lastst, 0, sizeof(lastst));
    337 
    338 	for (hdrcnt = 1;;) {
    339 		if (!--hdrcnt) {
    340 			printhdr();
    341 			hdrcnt = 20;
    342 		}
    343 		if (kvm_read(kd, off, (char *)&nfsstats, sizeof(nfsstats))
    344 			!= sizeof(nfsstats))
    345 			errx(1, "kvm_read failed");
    346 		if (printall || clientinfo)
    347 			printf("Client: %8d %8d %8d %8d %8d %8d %8d %8d\n",
    348 			    nfsstats.rpccnt[NFSPROC_GETATTR] -
    349 			    lastst.rpccnt[NFSPROC_GETATTR],
    350 			    nfsstats.rpccnt[NFSPROC_LOOKUP] -
    351 			    lastst.rpccnt[NFSPROC_LOOKUP],
    352 			    nfsstats.rpccnt[NFSPROC_READLINK] -
    353 			    lastst.rpccnt[NFSPROC_READLINK],
    354 			    nfsstats.rpccnt[NFSPROC_READ] -
    355 			    lastst.rpccnt[NFSPROC_READ],
    356 			    nfsstats.rpccnt[NFSPROC_WRITE] -
    357 			    lastst.rpccnt[NFSPROC_WRITE],
    358 			    nfsstats.rpccnt[NFSPROC_RENAME] -
    359 			    lastst.rpccnt[NFSPROC_RENAME],
    360 			    nfsstats.rpccnt[NFSPROC_ACCESS] -
    361 			    lastst.rpccnt[NFSPROC_ACCESS],
    362 			    (nfsstats.rpccnt[NFSPROC_READDIR] -
    363 			    lastst.rpccnt[NFSPROC_READDIR]) +
    364 			    (nfsstats.rpccnt[NFSPROC_READDIRPLUS] -
    365 			    lastst.rpccnt[NFSPROC_READDIRPLUS]));
    366 		if (printall || serverinfo)
    367 			printf("Server: %8d %8d %8d %8d %8d %8d %8d %8d\n",
    368 			    nfsstats.srvrpccnt[NFSPROC_GETATTR] -
    369 			    lastst.srvrpccnt[NFSPROC_GETATTR],
    370 			    nfsstats.srvrpccnt[NFSPROC_LOOKUP] -
    371 			    lastst.srvrpccnt[NFSPROC_LOOKUP],
    372 			    nfsstats.srvrpccnt[NFSPROC_READLINK] -
    373 			    lastst.srvrpccnt[NFSPROC_READLINK],
    374 			    nfsstats.srvrpccnt[NFSPROC_READ] -
    375 			    lastst.srvrpccnt[NFSPROC_READ],
    376 			    nfsstats.srvrpccnt[NFSPROC_WRITE] -
    377 			    lastst.srvrpccnt[NFSPROC_WRITE],
    378 			    nfsstats.srvrpccnt[NFSPROC_RENAME] -
    379 			    lastst.srvrpccnt[NFSPROC_RENAME],
    380 			    nfsstats.srvrpccnt[NFSPROC_ACCESS] -
    381 			    lastst.srvrpccnt[NFSPROC_ACCESS],
    382 			    (nfsstats.srvrpccnt[NFSPROC_READDIR] -
    383 			    lastst.srvrpccnt[NFSPROC_READDIR]) +
    384 			    (nfsstats.srvrpccnt[NFSPROC_READDIRPLUS] -
    385 			    lastst.srvrpccnt[NFSPROC_READDIRPLUS]));
    386 		lastst = nfsstats;
    387 		fflush(stdout);
    388 		oldmask = sigblock(sigmask(SIGALRM));
    389 		if (!signalled)
    390 			sigpause(0);
    391 		sigsetmask(oldmask);
    392 		signalled = 0;
    393 		(void)alarm(interval);
    394 	}
    395 	/*NOTREACHED*/
    396 }
    397 
    398 void
    399 printhdr()
    400 {
    401 
    402 	printf("        %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s\n",
    403 	    "Getattr", "Lookup", "Readlink", "Read", "Write", "Rename",
    404 	    "Access", "Readdir");
    405 	fflush(stdout);
    406 }
    407 
    408 /*
    409  * Called if an interval expires before sidewaysintpr has completed a loop.
    410  * Sets a flag to not wait for the alarm.
    411  */
    412 void
    413 catchalarm(dummy)
    414 	int dummy;
    415 {
    416 
    417 	signalled = 1;
    418 }
    419 
    420 void
    421 usage()
    422 {
    423 
    424 	(void)fprintf(stderr,
    425 		  "usage: nfsstat [-cs] [-M core] [-N system] [-w interval]\n");
    426 	exit(1);
    427 }
    428