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