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