Home | History | Annotate | Line # | Download | only in nfsstat
nfsstat.c revision 1.7
      1 /*	$NetBSD: nfsstat.c,v 1.7 1996/03/03 17:21:30 thorpej 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 #ifndef lint
     40 static char copyright[] =
     41 "@(#) 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 static char *rcsid = "$NetBSD: nfsstat.c,v 1.7 1996/03/03 17:21:30 thorpej Exp $";
     50 #endif
     51 #endif /* not lint */
     52 
     53 #include <sys/param.h>
     54 #include <sys/mount.h>
     55 #include <sys/sysctl.h>
     56 #include <nfs/rpcv2.h>
     57 #include <nfs/nfsproto.h>
     58 #include <nfs/nfs.h>
     59 #include <signal.h>
     60 #include <fcntl.h>
     61 #include <ctype.h>
     62 #include <errno.h>
     63 #include <kvm.h>
     64 #include <nlist.h>
     65 #include <unistd.h>
     66 #include <stdio.h>
     67 #include <stdlib.h>
     68 #include <string.h>
     69 #include <paths.h>
     70 #include <err.h>
     71 
     72 struct nlist nl[] = {
     73 #define	N_NFSSTAT	0
     74 	{ "_nfsstats" },
     75 	"",
     76 };
     77 kvm_t *kd;
     78 
     79 void intpr(), printhdr(), sidewaysintpr(), usage();
     80 
     81 main(argc, argv)
     82 	int argc;
     83 	char **argv;
     84 {
     85 	extern int optind;
     86 	extern char *optarg;
     87 	u_int interval;
     88 	int ch;
     89 	char *memf, *nlistf;
     90 	char errbuf[80];
     91 
     92 	interval = 0;
     93 	memf = nlistf = NULL;
     94 	while ((ch = getopt(argc, argv, "M:N:w:")) != EOF)
     95 		switch(ch) {
     96 		case 'M':
     97 			memf = optarg;
     98 			break;
     99 		case 'N':
    100 			nlistf = optarg;
    101 			break;
    102 		case 'w':
    103 			interval = atoi(optarg);
    104 			break;
    105 		case '?':
    106 		default:
    107 			usage();
    108 		}
    109 	argc -= optind;
    110 	argv += optind;
    111 
    112 #define	BACKWARD_COMPATIBILITY
    113 #ifdef	BACKWARD_COMPATIBILITY
    114 	if (*argv) {
    115 		interval = atoi(*argv);
    116 		if (*++argv) {
    117 			nlistf = *argv;
    118 			if (*++argv)
    119 				memf = *argv;
    120 		}
    121 	}
    122 #endif
    123 	/*
    124 	 * Discard setgid privileges if not the running kernel so that bad
    125 	 * guys can't print interesting stuff from kernel memory.
    126 	 */
    127 	if (nlistf != NULL || memf != NULL)
    128 		setgid(getgid());
    129 
    130 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == 0) {
    131 		fprintf(stderr, "nfsstat: kvm_openfiles: %s\n", errbuf);
    132 		exit(1);
    133 	}
    134 	if (kvm_nlist(kd, nl) != 0) {
    135 		fprintf(stderr, "nfsstat: kvm_nlist: can't get names\n");
    136 		exit(1);
    137 	}
    138 
    139 	if (interval)
    140 		sidewaysintpr(interval, nl[N_NFSSTAT].n_value);
    141 	else
    142 		intpr(nl[N_NFSSTAT].n_value);
    143 	exit(0);
    144 }
    145 
    146 /*
    147  * Print a description of the nfs stats.
    148  */
    149 void
    150 intpr(nfsstataddr)
    151 	u_long nfsstataddr;
    152 {
    153 	struct nfsstats nfsstats;
    154 
    155 	if (kvm_read(kd, (u_long)nfsstataddr, (char *)&nfsstats, sizeof(struct nfsstats)) < 0) {
    156 		fprintf(stderr, "nfsstat: kvm_read failed\n");
    157 		exit(1);
    158 	}
    159 	printf("Client Info:\n");
    160 	printf("Rpc Counts:\n");
    161 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    162 		"Getattr", "Setattr", "Lookup", "Readlink", "Read",
    163 		"Write", "Create", "Remove");
    164 	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    165 		nfsstats.rpccnt[NFSPROC_GETATTR],
    166 		nfsstats.rpccnt[NFSPROC_SETATTR],
    167 		nfsstats.rpccnt[NFSPROC_LOOKUP],
    168 		nfsstats.rpccnt[NFSPROC_READLINK],
    169 		nfsstats.rpccnt[NFSPROC_READ],
    170 		nfsstats.rpccnt[NFSPROC_WRITE],
    171 		nfsstats.rpccnt[NFSPROC_CREATE],
    172 		nfsstats.rpccnt[NFSPROC_REMOVE]);
    173 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    174 		"Rename", "Link", "Symlink", "Mkdir", "Rmdir",
    175 		"Readdir", "RdirPlus", "Access");
    176 	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    177 		nfsstats.rpccnt[NFSPROC_RENAME],
    178 		nfsstats.rpccnt[NFSPROC_LINK],
    179 		nfsstats.rpccnt[NFSPROC_SYMLINK],
    180 		nfsstats.rpccnt[NFSPROC_MKDIR],
    181 		nfsstats.rpccnt[NFSPROC_RMDIR],
    182 		nfsstats.rpccnt[NFSPROC_READDIR],
    183 		nfsstats.rpccnt[NFSPROC_READDIRPLUS],
    184 		nfsstats.rpccnt[NFSPROC_ACCESS]);
    185 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    186 		"Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
    187 		"GLease", "Vacate", "Evict");
    188 	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    189 		nfsstats.rpccnt[NFSPROC_MKNOD],
    190 		nfsstats.rpccnt[NFSPROC_FSSTAT],
    191 		nfsstats.rpccnt[NFSPROC_FSINFO],
    192 		nfsstats.rpccnt[NFSPROC_PATHCONF],
    193 		nfsstats.rpccnt[NFSPROC_COMMIT],
    194 		nfsstats.rpccnt[NQNFSPROC_GETLEASE],
    195 		nfsstats.rpccnt[NQNFSPROC_VACATED],
    196 		nfsstats.rpccnt[NQNFSPROC_EVICTED]);
    197 	printf("Rpc Info:\n");
    198 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
    199 		"TimedOut", "Invalid", "X Replies", "Retries", "Requests");
    200 	printf("%9d %9d %9d %9d %9d\n",
    201 		nfsstats.rpctimeouts,
    202 		nfsstats.rpcinvalid,
    203 		nfsstats.rpcunexpected,
    204 		nfsstats.rpcretries,
    205 		nfsstats.rpcrequests);
    206 	printf("Cache Info:\n");
    207 	printf("%9.9s %9.9s %9.9s %9.9s",
    208 		"Attr Hits", "Misses", "Lkup Hits", "Misses");
    209 	printf(" %9.9s %9.9s %9.9s %9.9s\n",
    210 		"BioR Hits", "Misses", "BioW Hits", "Misses");
    211 	printf("%9d %9d %9d %9d",
    212 		nfsstats.attrcache_hits, nfsstats.attrcache_misses,
    213 		nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
    214 	printf(" %9d %9d %9d %9d\n",
    215 		nfsstats.biocache_reads-nfsstats.read_bios,
    216 		nfsstats.read_bios,
    217 		nfsstats.biocache_writes-nfsstats.write_bios,
    218 		nfsstats.write_bios);
    219 	printf("%9.9s %9.9s %9.9s %9.9s",
    220 		"BioRLHits", "Misses", "BioD Hits", "Misses");
    221 	printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
    222 	printf("%9d %9d %9d %9d",
    223 		nfsstats.biocache_readlinks-nfsstats.readlink_bios,
    224 		nfsstats.readlink_bios,
    225 		nfsstats.biocache_readdirs-nfsstats.readdir_bios,
    226 		nfsstats.readdir_bios);
    227 	printf(" %9d %9d\n",
    228 		nfsstats.direofcache_hits, nfsstats.direofcache_misses);
    229 	printf("\nServer Info:\n");
    230 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    231 		"Getattr", "Setattr", "Lookup", "Readlink", "Read",
    232 		"Write", "Create", "Remove");
    233 	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    234 		nfsstats.srvrpccnt[NFSPROC_GETATTR],
    235 		nfsstats.srvrpccnt[NFSPROC_SETATTR],
    236 		nfsstats.srvrpccnt[NFSPROC_LOOKUP],
    237 		nfsstats.srvrpccnt[NFSPROC_READLINK],
    238 		nfsstats.srvrpccnt[NFSPROC_READ],
    239 		nfsstats.srvrpccnt[NFSPROC_WRITE],
    240 		nfsstats.srvrpccnt[NFSPROC_CREATE],
    241 		nfsstats.srvrpccnt[NFSPROC_REMOVE]);
    242 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    243 		"Rename", "Link", "Symlink", "Mkdir", "Rmdir",
    244 		"Readdir", "RdirPlus", "Access");
    245 	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    246 		nfsstats.srvrpccnt[NFSPROC_RENAME],
    247 		nfsstats.srvrpccnt[NFSPROC_LINK],
    248 		nfsstats.srvrpccnt[NFSPROC_SYMLINK],
    249 		nfsstats.srvrpccnt[NFSPROC_MKDIR],
    250 		nfsstats.srvrpccnt[NFSPROC_RMDIR],
    251 		nfsstats.srvrpccnt[NFSPROC_READDIR],
    252 		nfsstats.srvrpccnt[NFSPROC_READDIRPLUS],
    253 		nfsstats.srvrpccnt[NFSPROC_ACCESS]);
    254 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    255 		"Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
    256 		"GLease", "Vacate", "Evict");
    257 	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    258 		nfsstats.srvrpccnt[NFSPROC_MKNOD],
    259 		nfsstats.srvrpccnt[NFSPROC_FSSTAT],
    260 		nfsstats.srvrpccnt[NFSPROC_FSINFO],
    261 		nfsstats.srvrpccnt[NFSPROC_PATHCONF],
    262 		nfsstats.srvrpccnt[NFSPROC_COMMIT],
    263 		nfsstats.srvrpccnt[NQNFSPROC_GETLEASE],
    264 		nfsstats.srvrpccnt[NQNFSPROC_VACATED],
    265 		nfsstats.srvrpccnt[NQNFSPROC_EVICTED]);
    266 	printf("Server Ret-Failed\n");
    267 	printf("%17d\n", nfsstats.srvrpc_errs);
    268 	printf("Server Faults\n");
    269 	printf("%13d\n", nfsstats.srv_errs);
    270 	printf("Server Cache Stats:\n");
    271 	printf("%9.9s %9.9s %9.9s %9.9s\n",
    272 		"Inprog", "Idem", "Non-idem", "Misses");
    273 	printf("%9d %9d %9d %9d\n",
    274 		nfsstats.srvcache_inproghits,
    275 		nfsstats.srvcache_idemdonehits,
    276 		nfsstats.srvcache_nonidemdonehits,
    277 		nfsstats.srvcache_misses);
    278 	printf("Server Lease Stats:\n");
    279 	printf("%9.9s %9.9s %9.9s\n",
    280 		"Leases", "PeakL", "GLeases");
    281 	printf("%9d %9d %9d\n",
    282 		nfsstats.srvnqnfs_leases,
    283 		nfsstats.srvnqnfs_maxleases,
    284 		nfsstats.srvnqnfs_getleases);
    285 	printf("Server Write Gathering:\n");
    286 	printf("%9.9s %9.9s %9.9s\n",
    287 		"WriteOps", "WriteRPC", "Opsaved");
    288 	printf("%9d %9d %9d\n",
    289 		nfsstats.srvvop_writes,
    290 		nfsstats.srvrpccnt[NFSPROC_WRITE],
    291 		nfsstats.srvrpccnt[NFSPROC_WRITE] - nfsstats.srvvop_writes);
    292 }
    293 
    294 u_char	signalled;			/* set if alarm goes off "early" */
    295 
    296 /*
    297  * Print a running summary of nfs statistics.
    298  * Repeat display every interval seconds, showing statistics
    299  * collected over that interval.  Assumes that interval is non-zero.
    300  * First line printed at top of screen is always cumulative.
    301  */
    302 void
    303 sidewaysintpr(interval, off)
    304 	u_int interval;
    305 	u_long off;
    306 {
    307 	struct nfsstats nfsstats, lastst;
    308 	int hdrcnt, oldmask;
    309 	void catchalarm();
    310 
    311 	(void)signal(SIGALRM, catchalarm);
    312 	signalled = 0;
    313 	(void)alarm(interval);
    314 	bzero((caddr_t)&lastst, sizeof(lastst));
    315 
    316 	for (hdrcnt = 1;;) {
    317 		if (!--hdrcnt) {
    318 			printhdr();
    319 			hdrcnt = 20;
    320 		}
    321 		if (kvm_read(kd, off, (char *)&nfsstats, sizeof nfsstats) < 0) {
    322 			fprintf(stderr, "nfsstat: kvm_read failed\n");
    323 			exit(1);
    324 		}
    325 		printf("Client: %8d %8d %8d %8d %8d %8d %8d %8d\n",
    326 		    nfsstats.rpccnt[NFSPROC_GETATTR]-lastst.rpccnt[NFSPROC_GETATTR],
    327 		    nfsstats.rpccnt[NFSPROC_LOOKUP]-lastst.rpccnt[NFSPROC_LOOKUP],
    328 		    nfsstats.rpccnt[NFSPROC_READLINK]-lastst.rpccnt[NFSPROC_READLINK],
    329 		    nfsstats.rpccnt[NFSPROC_READ]-lastst.rpccnt[NFSPROC_READ],
    330 		    nfsstats.rpccnt[NFSPROC_WRITE]-lastst.rpccnt[NFSPROC_WRITE],
    331 		    nfsstats.rpccnt[NFSPROC_RENAME]-lastst.rpccnt[NFSPROC_RENAME],
    332 		    nfsstats.rpccnt[NFSPROC_ACCESS]-lastst.rpccnt[NFSPROC_ACCESS],
    333 		    (nfsstats.rpccnt[NFSPROC_READDIR]-lastst.rpccnt[NFSPROC_READDIR])
    334 		    +(nfsstats.rpccnt[NFSPROC_READDIRPLUS]-lastst.rpccnt[NFSPROC_READDIRPLUS]));
    335 		printf("Server: %8d %8d %8d %8d %8d %8d %8d %8d\n",
    336 		    nfsstats.srvrpccnt[NFSPROC_GETATTR]-lastst.srvrpccnt[NFSPROC_GETATTR],
    337 		    nfsstats.srvrpccnt[NFSPROC_LOOKUP]-lastst.srvrpccnt[NFSPROC_LOOKUP],
    338 		    nfsstats.srvrpccnt[NFSPROC_READLINK]-lastst.srvrpccnt[NFSPROC_READLINK],
    339 		    nfsstats.srvrpccnt[NFSPROC_READ]-lastst.srvrpccnt[NFSPROC_READ],
    340 		    nfsstats.srvrpccnt[NFSPROC_WRITE]-lastst.srvrpccnt[NFSPROC_WRITE],
    341 		    nfsstats.srvrpccnt[NFSPROC_RENAME]-lastst.srvrpccnt[NFSPROC_RENAME],
    342 		    nfsstats.srvrpccnt[NFSPROC_ACCESS]-lastst.srvrpccnt[NFSPROC_ACCESS],
    343 		    (nfsstats.srvrpccnt[NFSPROC_READDIR]-lastst.srvrpccnt[NFSPROC_READDIR])
    344 		    +(nfsstats.srvrpccnt[NFSPROC_READDIRPLUS]-lastst.srvrpccnt[NFSPROC_READDIRPLUS]));
    345 		lastst = nfsstats;
    346 		fflush(stdout);
    347 		oldmask = sigblock(sigmask(SIGALRM));
    348 		if (!signalled)
    349 			sigpause(0);
    350 		sigsetmask(oldmask);
    351 		signalled = 0;
    352 		(void)alarm(interval);
    353 	}
    354 	/*NOTREACHED*/
    355 }
    356 
    357 void
    358 printhdr()
    359 {
    360 	printf("        %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s\n",
    361 	    "Getattr", "Lookup", "Readlink", "Read", "Write", "Rename",
    362 	    "Access", "Readdir");
    363 	fflush(stdout);
    364 }
    365 
    366 /*
    367  * Called if an interval expires before sidewaysintpr has completed a loop.
    368  * Sets a flag to not wait for the alarm.
    369  */
    370 void
    371 catchalarm()
    372 {
    373 	signalled = 1;
    374 }
    375 
    376 void
    377 usage()
    378 {
    379 	(void)fprintf(stderr,
    380 	    "usage: nfsstat [-M core] [-N system] [-w interval]\n");
    381 	exit(1);
    382 }
    383