Home | History | Annotate | Line # | Download | only in nfsstat
nfsstat.c revision 1.2
      1 /*
      2  * Copyright (c) 1983, 1989 Regents of the University of California.
      3  * 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 char copyright[] =
     39 "@(#) Copyright (c) 1983, 1989 Regents of the University of California.\n\
     40  All rights reserved.\n";
     41 #endif /* not lint */
     42 
     43 #ifndef lint
     44 /*static char sccsid[] = "from: @(#)nfsstat.c	5.9 (Berkeley) 7/1/91";*/
     45 static char rcsid[] = "$Id: nfsstat.c,v 1.2 1993/08/01 18:10:36 mycroft Exp $";
     46 #endif /* not lint */
     47 
     48 #include <sys/param.h>
     49 #if BSD >= 199103
     50 #define NEWVM
     51 #endif
     52 #ifndef NEWVM
     53 #include <sys/vmmac.h>
     54 #include <machine/pte.h>
     55 #endif
     56 #include <sys/mount.h>
     57 #include <nfs/nfsv2.h>
     58 #include <nfs/nfs.h>
     59 #include <signal.h>
     60 #include <fcntl.h>
     61 #include <ctype.h>
     62 #include <errno.h>
     63 #include <nlist.h>
     64 #include <unistd.h>
     65 #include <stdio.h>
     66 #include <stdlib.h>
     67 #include <string.h>
     68 #include <paths.h>
     69 
     70 struct nlist nl[] = {
     71 #define	N_NFSSTAT	0
     72 	{ "_nfsstats" },
     73 #ifndef NEWVM
     74 #define	N_SYSMAP	1
     75 	{ "_Sysmap" },
     76 #define	N_SYSSIZE	2
     77 	{ "_Syssize" },
     78 #endif
     79 	"",
     80 };
     81 
     82 #ifndef NEWVM
     83 struct pte *Sysmap;
     84 #endif
     85 
     86 int kflag, kmem;
     87 char *kernel = _PATH_UNIX;
     88 char *kmemf = _PATH_KMEM;
     89 
     90 off_t klseek();
     91 void intpr(), printhdr(), sidewaysintpr(), usage();
     92 
     93 main(argc, argv)
     94 	int argc;
     95 	char **argv;
     96 {
     97 	extern int optind;
     98 	extern char *optarg;
     99 	u_int interval;
    100 	int ch;
    101 
    102 	interval = 0;
    103 	while ((ch = getopt(argc, argv, "M:N:w:")) != EOF)
    104 		switch(ch) {
    105 		case 'M':
    106 			kmemf = optarg;
    107 			kflag = 1;
    108 			break;
    109 		case 'N':
    110 			kernel = optarg;
    111 			break;
    112 		case 'w':
    113 			interval = atoi(optarg);
    114 			break;
    115 		case '?':
    116 		default:
    117 			usage();
    118 		}
    119 	argc -= optind;
    120 	argv += optind;
    121 
    122 #define	BACKWARD_COMPATIBILITY
    123 #ifdef	BACKWARD_COMPATIBILITY
    124 	if (*argv) {
    125 		kernel = *++argv;
    126 		if (*++argv) {
    127 			kmemf = *argv;
    128 			kflag = 1;
    129 		}
    130 	}
    131 #endif
    132 	if (nlist(kernel, nl) < 0 || nl[0].n_type == 0) {
    133 		(void)fprintf(stderr, "nfsstate: %s: no namelist\n", kernel);
    134 		exit(1);
    135 	}
    136 	kmem = open(kmemf, O_RDONLY);
    137 	if (kmem < 0) {
    138 		(void)fprintf(stderr,
    139 		    "nfsstat: %s: %s\n", kmemf, strerror(errno));
    140 		exit(1);
    141 	}
    142 	if (kflag) {
    143 #ifdef NEWVM
    144 		(void)fprintf(stderr, "nfsstat: can't do core files yet\n");
    145 		exit(1);
    146 #else
    147 		off_t off;
    148 
    149 		Sysmap = (struct pte *)
    150 		   malloc((u_int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
    151 		if (!Sysmap) {
    152 			(void)fprintf(stderr, "nfsstat: %s\n", strerror(errno));
    153 			exit(1);
    154 		}
    155 		off = nl[N_SYSMAP].n_value & ~KERNBASE;
    156 		(void)lseek(kmem, off, L_SET);
    157 		(void)read(kmem, (char *)Sysmap,
    158 		    (int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
    159 #endif
    160 	}
    161 
    162 	if (!nl[N_NFSSTAT].n_value) {
    163 		(void)fprintf(stderr, "nfsstat: nfsstats symbol not defined\n");
    164 		exit(1);
    165 	}
    166 	if (interval)
    167 		sidewaysintpr(interval, nl[N_NFSSTAT].n_value);
    168 	else
    169 		intpr(nl[N_NFSSTAT].n_value);
    170 	exit(0);
    171 }
    172 
    173 /*
    174  * Print a description of the network interfaces.
    175  */
    176 void
    177 intpr(nfsstataddr)
    178 	off_t nfsstataddr;
    179 {
    180 	struct nfsstats nfsstats;
    181 
    182 	klseek(kmem, nfsstataddr, 0L);
    183 	read(kmem, (char *)&nfsstats, sizeof(struct nfsstats));
    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[1],
    191 		nfsstats.rpccnt[2],
    192 		nfsstats.rpccnt[4],
    193 		nfsstats.rpccnt[5],
    194 		nfsstats.rpccnt[6],
    195 		nfsstats.rpccnt[8],
    196 		nfsstats.rpccnt[9],
    197 		nfsstats.rpccnt[10]);
    198 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    199 		"Rename", "Link", "Symlink", "Mkdir", "Rmdir",
    200 		"Readdir", "Statfs");
    201 	printf("%9d %9d %9d %9d %9d %9d %9d\n",
    202 		nfsstats.rpccnt[11],
    203 		nfsstats.rpccnt[12],
    204 		nfsstats.rpccnt[13],
    205 		nfsstats.rpccnt[14],
    206 		nfsstats.rpccnt[15],
    207 		nfsstats.rpccnt[16],
    208 		nfsstats.rpccnt[17]);
    209 	printf("Rpc Info:\n");
    210 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
    211 		"TimedOut", "Invalid", "X Replies", "Retries", "Requests");
    212 	printf("%9d %9d %9d %9d %9d\n",
    213 		nfsstats.rpctimeouts,
    214 		nfsstats.rpcinvalid,
    215 		nfsstats.rpcunexpected,
    216 		nfsstats.rpcretries,
    217 		nfsstats.rpcrequests);
    218 	printf("Cache Info:\n");
    219 	printf("%9.9s %9.9s %9.9s %9.9s",
    220 		"Attr Hits", "Misses", "Lkup Hits", "Misses");
    221 	printf(" %9.9s %9.9s %9.9s %9.9s\n",
    222 		"BioR Hits", "Misses", "BioW Hits", "Misses");
    223 	printf("%9d %9d %9d %9d",
    224 		nfsstats.attrcache_hits, nfsstats.attrcache_misses,
    225 		nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
    226 	printf(" %9d %9d %9d %9d\n",
    227 		nfsstats.biocache_reads-nfsstats.read_bios,
    228 		nfsstats.read_bios,
    229 		nfsstats.biocache_writes-nfsstats.write_bios,
    230 		nfsstats.write_bios);
    231 	printf("%9.9s %9.9s %9.9s %9.9s",
    232 		"BioRLHits", "Misses", "BioD Hits", "Misses");
    233 	printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
    234 	printf("%9d %9d %9d %9d",
    235 		nfsstats.biocache_readlinks-nfsstats.readlink_bios,
    236 		nfsstats.readlink_bios,
    237 		nfsstats.biocache_readdirs-nfsstats.readdir_bios,
    238 		nfsstats.readdir_bios);
    239 	printf(" %9d %9d\n",
    240 		nfsstats.direofcache_hits, nfsstats.direofcache_misses);
    241 	printf("\nServer Info:\n");
    242 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    243 		"Getattr", "Setattr", "Lookup", "Readlink", "Read",
    244 		"Write", "Create", "Remove");
    245 	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    246 		nfsstats.srvrpccnt[1],
    247 		nfsstats.srvrpccnt[2],
    248 		nfsstats.srvrpccnt[4],
    249 		nfsstats.srvrpccnt[5],
    250 		nfsstats.srvrpccnt[6],
    251 		nfsstats.srvrpccnt[8],
    252 		nfsstats.srvrpccnt[9],
    253 		nfsstats.srvrpccnt[10]);
    254 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    255 		"Rename", "Link", "Symlink", "Mkdir", "Rmdir",
    256 		"Readdir", "Statfs");
    257 	printf("%9d %9d %9d %9d %9d %9d %9d\n",
    258 		nfsstats.srvrpccnt[11],
    259 		nfsstats.srvrpccnt[12],
    260 		nfsstats.srvrpccnt[13],
    261 		nfsstats.srvrpccnt[14],
    262 		nfsstats.srvrpccnt[15],
    263 		nfsstats.srvrpccnt[16],
    264 		nfsstats.srvrpccnt[17]);
    265 	printf("Server Ret-Failed\n");
    266 	printf("%17d\n", nfsstats.srvrpc_errs);
    267 	printf("Server Faults\n");
    268 	printf("%13d\n", nfsstats.srv_errs);
    269 	printf("Server Cache Stats:\n");
    270 	printf("%9.9s %9.9s %9.9s %9.9s\n",
    271 		"Inprog", "Idem", "Non-idem", "Misses");
    272 	printf("%9d %9d %9d %9d\n",
    273 		nfsstats.srvcache_inproghits,
    274 		nfsstats.srvcache_idemdonehits,
    275 		nfsstats.srvcache_nonidemdonehits,
    276 		nfsstats.srvcache_misses);
    277 }
    278 
    279 u_char	signalled;			/* set if alarm goes off "early" */
    280 
    281 /*
    282  * Print a running summary of nfs statistics.
    283  * Repeat display every interval seconds, showing statistics
    284  * collected over that interval.  Assumes that interval is non-zero.
    285  * First line printed at top of screen is always cumulative.
    286  */
    287 void
    288 sidewaysintpr(interval, off)
    289 	u_int interval;
    290 	off_t off;
    291 {
    292 	struct nfsstats nfsstats, lastst;
    293 	int hdrcnt, oldmask;
    294 	void catchalarm();
    295 
    296 	klseek(kmem, off, 0L);
    297 
    298 	(void)signal(SIGALRM, catchalarm);
    299 	signalled = 0;
    300 	(void)alarm(interval);
    301 	bzero((caddr_t)&lastst, sizeof(lastst));
    302 
    303 	for (hdrcnt = 1;;) {
    304 		if (!--hdrcnt) {
    305 			printhdr();
    306 			hdrcnt = 20;
    307 		}
    308 		klseek(kmem, off, 0L);
    309 		read(kmem, (char *)&nfsstats, sizeof nfsstats);
    310 		printf("Client: %8d %8d %8d %8d %8d %8d %8d %8d\n",
    311 		    nfsstats.rpccnt[1]-lastst.rpccnt[1],
    312 		    nfsstats.rpccnt[4]-lastst.rpccnt[4],
    313 		    nfsstats.rpccnt[5]-lastst.rpccnt[5],
    314 		    nfsstats.rpccnt[6]-lastst.rpccnt[6],
    315 		    nfsstats.rpccnt[8]-lastst.rpccnt[8],
    316 		    nfsstats.rpccnt[11]-lastst.rpccnt[11],
    317 		    nfsstats.rpccnt[12]-lastst.rpccnt[12],
    318 		    nfsstats.rpccnt[16]-lastst.rpccnt[16]);
    319 		printf("Server: %8d %8d %8d %8d %8d %8d %8d %8d\n",
    320 		    nfsstats.srvrpccnt[1]-lastst.srvrpccnt[1],
    321 		    nfsstats.srvrpccnt[4]-lastst.srvrpccnt[4],
    322 		    nfsstats.srvrpccnt[5]-lastst.srvrpccnt[5],
    323 		    nfsstats.srvrpccnt[6]-lastst.srvrpccnt[6],
    324 		    nfsstats.srvrpccnt[8]-lastst.srvrpccnt[8],
    325 		    nfsstats.srvrpccnt[11]-lastst.srvrpccnt[11],
    326 		    nfsstats.srvrpccnt[12]-lastst.srvrpccnt[12],
    327 		    nfsstats.srvrpccnt[16]-lastst.srvrpccnt[16]);
    328 		lastst = nfsstats;
    329 		fflush(stdout);
    330 		oldmask = sigblock(sigmask(SIGALRM));
    331 		if (!signalled)
    332 			sigpause(0);
    333 		sigsetmask(oldmask);
    334 		signalled = 0;
    335 		(void)alarm(interval);
    336 	}
    337 	/*NOTREACHED*/
    338 }
    339 
    340 void
    341 printhdr()
    342 {
    343 	printf("        %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s\n",
    344 	    "Getattr", "Lookup", "Readlink", "Read", "Write", "Rename",
    345 	    "Link", "Readdir");
    346 	fflush(stdout);
    347 }
    348 
    349 /*
    350  * Called if an interval expires before sidewaysintpr has completed a loop.
    351  * Sets a flag to not wait for the alarm.
    352  */
    353 void
    354 catchalarm()
    355 {
    356 	signalled = 1;
    357 }
    358 
    359 /*
    360  * Seek into the kernel for a value.
    361  */
    362 off_t
    363 klseek(fd, base, off)
    364 	int fd, off;
    365 	off_t base;
    366 {
    367 #ifndef NEWVM
    368 	if (kflag) {
    369 		/* get kernel pte */
    370 		base &= ~KERNBASE;
    371 		base = ctob(Sysmap[btop(base)].pg_pfnum) + (base & PGOFSET);
    372 	}
    373 #endif
    374 	return (lseek(fd, base, off));
    375 }
    376 
    377 void
    378 usage()
    379 {
    380 	(void)fprintf(stderr,
    381 	    "usage: nfsstat [-M core] [-N system] [-w interval]\n");
    382 	exit(1);
    383 }
    384