Home | History | Annotate | Line # | Download | only in nfsstat
nfsstat.c revision 1.15
      1 /*	$NetBSD: nfsstat.c,v 1.15 2000/04/14 06:11:09 simonb 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.15 2000/04/14 06:11:09 simonb 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 	u_int interval;
     97 	int ch;
     98 	char *memf, *nlistf;
     99 	char errbuf[_POSIX2_LINE_MAX];
    100 	gid_t egid = getegid();
    101 
    102 	(void)setegid(getgid());
    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, we toss
    145 	 * them away totally so that bad guys can't print interesting stuff
    146 	 * from kernel memory, otherwise switch back to kmem for the
    147 	 * duration of the kvm_openfiles() call.
    148 	 */
    149 	if (nlistf != NULL || memf != NULL)
    150 		(void)setgid(getgid());
    151 	else
    152 		(void)setegid(egid);
    153 
    154 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == 0)
    155 		errx(1, "kvm_openfiles: %s", errbuf);
    156 
    157 	/* get rid of it now anyway */
    158 	if (nlistf == NULL && memf == NULL)
    159 		(void)setgid(getgid());
    160 
    161 	if (kvm_nlist(kd, nl) != 0)
    162 		errx(1, "kvm_nlist: can't get names");
    163 
    164 	if (interval)
    165 		sidewaysintpr(interval, nl[N_NFSSTAT].n_value);
    166 	else
    167 		intpr(nl[N_NFSSTAT].n_value);
    168 	exit(0);
    169 }
    170 
    171 /*
    172  * Print a description of the nfs stats.
    173  */
    174 void
    175 intpr(nfsstataddr)
    176 	u_long nfsstataddr;
    177 {
    178 	struct nfsstats nfsstats;
    179 
    180 	if (kvm_read(kd, (u_long)nfsstataddr, (char *)&nfsstats,
    181 	     sizeof(struct nfsstats)) != sizeof(struct nfsstats))
    182 		errx(1, "kvm_read failed");
    183 	if (printall || clientinfo) {
    184 		printf("Client Info:\n");
    185 		printf("Rpc Counts:\n");
    186 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    187 		    "Getattr", "Setattr", "Lookup", "Readlink", "Read",
    188 		    "Write", "Create", "Remove");
    189 		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    190 		    nfsstats.rpccnt[NFSPROC_GETATTR],
    191 		    nfsstats.rpccnt[NFSPROC_SETATTR],
    192 		    nfsstats.rpccnt[NFSPROC_LOOKUP],
    193 		    nfsstats.rpccnt[NFSPROC_READLINK],
    194 		    nfsstats.rpccnt[NFSPROC_READ],
    195 		    nfsstats.rpccnt[NFSPROC_WRITE],
    196 		    nfsstats.rpccnt[NFSPROC_CREATE],
    197 		    nfsstats.rpccnt[NFSPROC_REMOVE]);
    198 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    199 		    "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
    200 		    "Readdir", "RdirPlus", "Access");
    201 		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    202 		    nfsstats.rpccnt[NFSPROC_RENAME],
    203 		    nfsstats.rpccnt[NFSPROC_LINK],
    204 		    nfsstats.rpccnt[NFSPROC_SYMLINK],
    205 		    nfsstats.rpccnt[NFSPROC_MKDIR],
    206 		    nfsstats.rpccnt[NFSPROC_RMDIR],
    207 		    nfsstats.rpccnt[NFSPROC_READDIR],
    208 		    nfsstats.rpccnt[NFSPROC_READDIRPLUS],
    209 		    nfsstats.rpccnt[NFSPROC_ACCESS]);
    210 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    211 		    "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
    212 		    "GLease", "Vacate", "Evict");
    213 		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    214 		    nfsstats.rpccnt[NFSPROC_MKNOD],
    215 		    nfsstats.rpccnt[NFSPROC_FSSTAT],
    216 		    nfsstats.rpccnt[NFSPROC_FSINFO],
    217 		    nfsstats.rpccnt[NFSPROC_PATHCONF],
    218 		    nfsstats.rpccnt[NFSPROC_COMMIT],
    219 		    nfsstats.rpccnt[NQNFSPROC_GETLEASE],
    220 		    nfsstats.rpccnt[NQNFSPROC_VACATED],
    221 		    nfsstats.rpccnt[NQNFSPROC_EVICTED]);
    222 		printf("Rpc Info:\n");
    223 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
    224 		    "TimedOut", "Invalid", "X Replies", "Retries", "Requests");
    225 		printf("%9d %9d %9d %9d %9d\n",
    226 		    nfsstats.rpctimeouts,
    227 		    nfsstats.rpcinvalid,
    228 		    nfsstats.rpcunexpected,
    229 		    nfsstats.rpcretries,
    230 		    nfsstats.rpcrequests);
    231 		printf("Cache Info:\n");
    232 		printf("%9.9s %9.9s %9.9s %9.9s",
    233 		    "Attr Hits", "Misses", "Lkup Hits", "Misses");
    234 		printf(" %9.9s %9.9s %9.9s %9.9s\n",
    235 		    "BioR Hits", "Misses", "BioW Hits", "Misses");
    236 		printf("%9d %9d %9d %9d",
    237 		    nfsstats.attrcache_hits, nfsstats.attrcache_misses,
    238 		    nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
    239 		printf(" %9d %9d %9d %9d\n",
    240 		    nfsstats.biocache_reads-nfsstats.read_bios,
    241 		    nfsstats.read_bios,
    242 		    nfsstats.biocache_writes-nfsstats.write_bios,
    243 		    nfsstats.write_bios);
    244 		printf("%9.9s %9.9s %9.9s %9.9s",
    245 		    "BioRLHits", "Misses", "BioD Hits", "Misses");
    246 		printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
    247 		printf("%9d %9d %9d %9d",
    248 		    nfsstats.biocache_readlinks-nfsstats.readlink_bios,
    249 		    nfsstats.readlink_bios,
    250 		    nfsstats.biocache_readdirs-nfsstats.readdir_bios,
    251 		    nfsstats.readdir_bios);
    252 		printf(" %9d %9d\n",
    253 		    nfsstats.direofcache_hits, nfsstats.direofcache_misses);
    254 	}
    255 	if (printall || (clientinfo && serverinfo)){
    256 		printf("\n");
    257 	}
    258 	if (printall || serverinfo){
    259 		printf("Server Info:\n");
    260 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    261 		    "Getattr", "Setattr", "Lookup", "Readlink", "Read",
    262 		    "Write", "Create", "Remove");
    263 		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    264 		    nfsstats.srvrpccnt[NFSPROC_GETATTR],
    265 		    nfsstats.srvrpccnt[NFSPROC_SETATTR],
    266 		    nfsstats.srvrpccnt[NFSPROC_LOOKUP],
    267 		    nfsstats.srvrpccnt[NFSPROC_READLINK],
    268 		    nfsstats.srvrpccnt[NFSPROC_READ],
    269 		    nfsstats.srvrpccnt[NFSPROC_WRITE],
    270 		    nfsstats.srvrpccnt[NFSPROC_CREATE],
    271 		    nfsstats.srvrpccnt[NFSPROC_REMOVE]);
    272 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    273 		    "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
    274 		    "Readdir", "RdirPlus", "Access");
    275 		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    276 		    nfsstats.srvrpccnt[NFSPROC_RENAME],
    277 		    nfsstats.srvrpccnt[NFSPROC_LINK],
    278 		    nfsstats.srvrpccnt[NFSPROC_SYMLINK],
    279 		    nfsstats.srvrpccnt[NFSPROC_MKDIR],
    280 		    nfsstats.srvrpccnt[NFSPROC_RMDIR],
    281 		    nfsstats.srvrpccnt[NFSPROC_READDIR],
    282 		    nfsstats.srvrpccnt[NFSPROC_READDIRPLUS],
    283 		    nfsstats.srvrpccnt[NFSPROC_ACCESS]);
    284 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    285 		    "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
    286 		    "GLease", "Vacate", "Evict");
    287 		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    288 		    nfsstats.srvrpccnt[NFSPROC_MKNOD],
    289 		    nfsstats.srvrpccnt[NFSPROC_FSSTAT],
    290 		    nfsstats.srvrpccnt[NFSPROC_FSINFO],
    291 		    nfsstats.srvrpccnt[NFSPROC_PATHCONF],
    292 		    nfsstats.srvrpccnt[NFSPROC_COMMIT],
    293 		    nfsstats.srvrpccnt[NQNFSPROC_GETLEASE],
    294 		    nfsstats.srvrpccnt[NQNFSPROC_VACATED],
    295 		    nfsstats.srvrpccnt[NQNFSPROC_EVICTED]);
    296 		printf("Server Ret-Failed\n");
    297 		printf("%17d\n", nfsstats.srvrpc_errs);
    298 		printf("Server Faults\n");
    299 		printf("%13d\n", nfsstats.srv_errs);
    300 		printf("Server Cache Stats:\n");
    301 		printf("%9.9s %9.9s %9.9s %9.9s\n",
    302 		    "Inprog", "Idem", "Non-idem", "Misses");
    303 		printf("%9d %9d %9d %9d\n",
    304 		    nfsstats.srvcache_inproghits,
    305 		    nfsstats.srvcache_idemdonehits,
    306 		    nfsstats.srvcache_nonidemdonehits,
    307 		    nfsstats.srvcache_misses);
    308 		printf("Server Lease Stats:\n");
    309 		printf("%9.9s %9.9s %9.9s\n",
    310 		    "Leases", "PeakL", "GLeases");
    311 		printf("%9d %9d %9d\n",
    312 		    nfsstats.srvnqnfs_leases,
    313 		    nfsstats.srvnqnfs_maxleases,
    314 		    nfsstats.srvnqnfs_getleases);
    315 		printf("Server Write Gathering:\n");
    316 		printf("%9.9s %9.9s %9.9s\n",
    317 		    "WriteOps", "WriteRPC", "Opsaved");
    318 		printf("%9d %9d %9d\n",
    319 		    nfsstats.srvvop_writes,
    320 		    nfsstats.srvrpccnt[NFSPROC_WRITE],
    321 		    nfsstats.srvrpccnt[NFSPROC_WRITE] - nfsstats.srvvop_writes);
    322 	}
    323 }
    324 
    325 u_char	signalled;			/* set if alarm goes off "early" */
    326 
    327 /*
    328  * Print a running summary of nfs statistics.
    329  * Repeat display every interval seconds, showing statistics
    330  * collected over that interval.  Assumes that interval is non-zero.
    331  * First line printed at top of screen is always cumulative.
    332  */
    333 void
    334 sidewaysintpr(interval, off)
    335 	u_int interval;
    336 	u_long off;
    337 {
    338 	struct nfsstats nfsstats, lastst;
    339 	int hdrcnt, oldmask;
    340 
    341 	(void)signal(SIGALRM, catchalarm);
    342 	signalled = 0;
    343 	(void)alarm(interval);
    344 	memset((caddr_t)&lastst, 0, sizeof(lastst));
    345 
    346 	for (hdrcnt = 1;;) {
    347 		if (!--hdrcnt) {
    348 			printhdr();
    349 			hdrcnt = 20;
    350 		}
    351 		if (kvm_read(kd, off, (char *)&nfsstats, sizeof(nfsstats))
    352 			!= sizeof(nfsstats))
    353 			errx(1, "kvm_read failed");
    354 		if (printall || clientinfo)
    355 			printf("Client: %8d %8d %8d %8d %8d %8d %8d %8d\n",
    356 			    nfsstats.rpccnt[NFSPROC_GETATTR] -
    357 			    lastst.rpccnt[NFSPROC_GETATTR],
    358 			    nfsstats.rpccnt[NFSPROC_LOOKUP] -
    359 			    lastst.rpccnt[NFSPROC_LOOKUP],
    360 			    nfsstats.rpccnt[NFSPROC_READLINK] -
    361 			    lastst.rpccnt[NFSPROC_READLINK],
    362 			    nfsstats.rpccnt[NFSPROC_READ] -
    363 			    lastst.rpccnt[NFSPROC_READ],
    364 			    nfsstats.rpccnt[NFSPROC_WRITE] -
    365 			    lastst.rpccnt[NFSPROC_WRITE],
    366 			    nfsstats.rpccnt[NFSPROC_RENAME] -
    367 			    lastst.rpccnt[NFSPROC_RENAME],
    368 			    nfsstats.rpccnt[NFSPROC_ACCESS] -
    369 			    lastst.rpccnt[NFSPROC_ACCESS],
    370 			    (nfsstats.rpccnt[NFSPROC_READDIR] -
    371 			    lastst.rpccnt[NFSPROC_READDIR]) +
    372 			    (nfsstats.rpccnt[NFSPROC_READDIRPLUS] -
    373 			    lastst.rpccnt[NFSPROC_READDIRPLUS]));
    374 		if (printall || serverinfo)
    375 			printf("Server: %8d %8d %8d %8d %8d %8d %8d %8d\n",
    376 			    nfsstats.srvrpccnt[NFSPROC_GETATTR] -
    377 			    lastst.srvrpccnt[NFSPROC_GETATTR],
    378 			    nfsstats.srvrpccnt[NFSPROC_LOOKUP] -
    379 			    lastst.srvrpccnt[NFSPROC_LOOKUP],
    380 			    nfsstats.srvrpccnt[NFSPROC_READLINK] -
    381 			    lastst.srvrpccnt[NFSPROC_READLINK],
    382 			    nfsstats.srvrpccnt[NFSPROC_READ] -
    383 			    lastst.srvrpccnt[NFSPROC_READ],
    384 			    nfsstats.srvrpccnt[NFSPROC_WRITE] -
    385 			    lastst.srvrpccnt[NFSPROC_WRITE],
    386 			    nfsstats.srvrpccnt[NFSPROC_RENAME] -
    387 			    lastst.srvrpccnt[NFSPROC_RENAME],
    388 			    nfsstats.srvrpccnt[NFSPROC_ACCESS] -
    389 			    lastst.srvrpccnt[NFSPROC_ACCESS],
    390 			    (nfsstats.srvrpccnt[NFSPROC_READDIR] -
    391 			    lastst.srvrpccnt[NFSPROC_READDIR]) +
    392 			    (nfsstats.srvrpccnt[NFSPROC_READDIRPLUS] -
    393 			    lastst.srvrpccnt[NFSPROC_READDIRPLUS]));
    394 		lastst = nfsstats;
    395 		fflush(stdout);
    396 		oldmask = sigblock(sigmask(SIGALRM));
    397 		if (!signalled)
    398 			sigpause(0);
    399 		sigsetmask(oldmask);
    400 		signalled = 0;
    401 		(void)alarm(interval);
    402 	}
    403 	/*NOTREACHED*/
    404 }
    405 
    406 void
    407 printhdr()
    408 {
    409 
    410 	printf("        %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s\n",
    411 	    "Getattr", "Lookup", "Readlink", "Read", "Write", "Rename",
    412 	    "Access", "Readdir");
    413 	fflush(stdout);
    414 }
    415 
    416 /*
    417  * Called if an interval expires before sidewaysintpr has completed a loop.
    418  * Sets a flag to not wait for the alarm.
    419  */
    420 void
    421 catchalarm(dummy)
    422 	int dummy;
    423 {
    424 
    425 	signalled = 1;
    426 }
    427 
    428 void
    429 usage()
    430 {
    431 
    432 	(void)fprintf(stderr,
    433 		  "usage: nfsstat [-cs] [-M core] [-N system] [-w interval]\n");
    434 	exit(1);
    435 }
    436