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