Home | History | Annotate | Line # | Download | only in nfsstat
nfsstat.c revision 1.4
      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.4 1994/06/09 15:49:01 pk 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 #include <kvm.h>
     70 
     71 struct nlist nl[] = {
     72 #define	N_NFSSTAT	0
     73 	{ "_nfsstats" },
     74 #ifndef NEWVM
     75 #define	N_SYSMAP	1
     76 	{ "_Sysmap" },
     77 #define	N_SYSSIZE	2
     78 	{ "_Syssize" },
     79 #endif
     80 	"",
     81 };
     82 
     83 #ifndef NEWVM
     84 struct pte *Sysmap;
     85 #endif
     86 
     87 int kflag;
     88 kvm_t *kmem;
     89 char *kernel = _PATH_UNIX;
     90 char *kmemf = _PATH_KMEM;
     91 
     92 void intpr __P((off_t));
     93 void printhdr __P((void));
     94 void sidewaysintpr __P((u_int, off_t));
     95 void usage __P((void));
     96 
     97 main(argc, argv)
     98 	int argc;
     99 	char **argv;
    100 {
    101 	extern int optind;
    102 	extern char *optarg;
    103 	u_int interval;
    104 	int ch;
    105 
    106 	interval = 0;
    107 	while ((ch = getopt(argc, argv, "M:N:w:")) != EOF)
    108 		switch(ch) {
    109 		case 'M':
    110 			kmemf = optarg;
    111 			kflag = 1;
    112 			break;
    113 		case 'N':
    114 			kernel = optarg;
    115 			break;
    116 		case 'w':
    117 			interval = atoi(optarg);
    118 			break;
    119 		case '?':
    120 		default:
    121 			usage();
    122 		}
    123 	argc -= optind;
    124 	argv += optind;
    125 
    126 #define	BACKWARD_COMPATIBILITY
    127 #ifdef	BACKWARD_COMPATIBILITY
    128 	if (*argv) {
    129 		kernel = *++argv;
    130 		if (*++argv) {
    131 			kmemf = *argv;
    132 			kflag = 1;
    133 		}
    134 	}
    135 #endif
    136 	if (nlist(kernel, nl) < 0 || nl[0].n_type == 0) {
    137 		(void)fprintf(stderr, "nfsstate: %s: no namelist\n", kernel);
    138 		exit(1);
    139 	}
    140 	kmem = kvm_open(NULL,NULL,NULL,O_RDONLY,"kvm_open");
    141 	if (kmem == NULL) {
    142 		(void)fprintf(stderr,
    143 		    "nfsstat: %s: %s\n", kmemf, strerror(errno));
    144 		kvm_close(kmem);
    145 		exit(1);
    146 	}
    147 	if (kflag) {
    148 #ifdef NEWVM
    149 		(void)fprintf(stderr, "nfsstat: can't do core files yet\n");
    150 		exit(1);
    151 #else
    152 		off_t off;
    153 
    154 		Sysmap = (struct pte *)
    155 		   malloc((u_int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
    156 		if (!Sysmap) {
    157 			(void)fprintf(stderr, "nfsstat: %s\n", strerror(errno));
    158 			exit(1);
    159 		}
    160 		kvm_read(kmem, (long) nl[N_SYSMAP].n_value, (char *)Sysmap,
    161 			(int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
    162 #endif
    163 	}
    164 
    165 	if (!nl[N_NFSSTAT].n_value) {
    166 		(void)fprintf(stderr, "nfsstat: nfsstats symbol not defined\n");
    167 		kvm_close(kmem);
    168 		exit(1);
    169 	}
    170 	if (interval)
    171 		sidewaysintpr(interval, (off_t)nl[N_NFSSTAT].n_value);
    172 	else
    173 		intpr((off_t)nl[N_NFSSTAT].n_value);
    174 	kvm_close(kmem);
    175 	exit(0);
    176 }
    177 
    178 /*
    179  * Print a description of the network interfaces.
    180  */
    181 void
    182 intpr(nfsstataddr)
    183 	off_t nfsstataddr;
    184 {
    185 	struct nfsstats nfsstats;
    186 
    187 	kvm_read(kmem, nfsstataddr, (char *)&nfsstats, sizeof(struct nfsstats));
    188 	printf("Client Info:\n");
    189 	printf("Rpc Counts:\n");
    190 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    191 		"Getattr", "Setattr", "Lookup", "Readlink", "Read",
    192 		"Write", "Create", "Remove");
    193 	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    194 		nfsstats.rpccnt[1],
    195 		nfsstats.rpccnt[2],
    196 		nfsstats.rpccnt[4],
    197 		nfsstats.rpccnt[5],
    198 		nfsstats.rpccnt[6],
    199 		nfsstats.rpccnt[8],
    200 		nfsstats.rpccnt[9],
    201 		nfsstats.rpccnt[10]);
    202 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    203 		"Rename", "Link", "Symlink", "Mkdir", "Rmdir",
    204 		"Readdir", "Statfs");
    205 	printf("%9d %9d %9d %9d %9d %9d %9d\n",
    206 		nfsstats.rpccnt[11],
    207 		nfsstats.rpccnt[12],
    208 		nfsstats.rpccnt[13],
    209 		nfsstats.rpccnt[14],
    210 		nfsstats.rpccnt[15],
    211 		nfsstats.rpccnt[16],
    212 		nfsstats.rpccnt[17]);
    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 	printf("\nServer Info:\n");
    246 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    247 		"Getattr", "Setattr", "Lookup", "Readlink", "Read",
    248 		"Write", "Create", "Remove");
    249 	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    250 		nfsstats.srvrpccnt[1],
    251 		nfsstats.srvrpccnt[2],
    252 		nfsstats.srvrpccnt[4],
    253 		nfsstats.srvrpccnt[5],
    254 		nfsstats.srvrpccnt[6],
    255 		nfsstats.srvrpccnt[8],
    256 		nfsstats.srvrpccnt[9],
    257 		nfsstats.srvrpccnt[10]);
    258 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    259 		"Rename", "Link", "Symlink", "Mkdir", "Rmdir",
    260 		"Readdir", "Statfs");
    261 	printf("%9d %9d %9d %9d %9d %9d %9d\n",
    262 		nfsstats.srvrpccnt[11],
    263 		nfsstats.srvrpccnt[12],
    264 		nfsstats.srvrpccnt[13],
    265 		nfsstats.srvrpccnt[14],
    266 		nfsstats.srvrpccnt[15],
    267 		nfsstats.srvrpccnt[16],
    268 		nfsstats.srvrpccnt[17]);
    269 	printf("Server Ret-Failed\n");
    270 	printf("%17d\n", nfsstats.srvrpc_errs);
    271 	printf("Server Faults\n");
    272 	printf("%13d\n", nfsstats.srv_errs);
    273 	printf("Server Cache Stats:\n");
    274 	printf("%9.9s %9.9s %9.9s %9.9s\n",
    275 		"Inprog", "Idem", "Non-idem", "Misses");
    276 	printf("%9d %9d %9d %9d\n",
    277 		nfsstats.srvcache_inproghits,
    278 		nfsstats.srvcache_idemdonehits,
    279 		nfsstats.srvcache_nonidemdonehits,
    280 		nfsstats.srvcache_misses);
    281 }
    282 
    283 u_char	signalled;			/* set if alarm goes off "early" */
    284 
    285 /*
    286  * Print a running summary of nfs statistics.
    287  * Repeat display every interval seconds, showing statistics
    288  * collected over that interval.  Assumes that interval is non-zero.
    289  * First line printed at top of screen is always cumulative.
    290  */
    291 void
    292 sidewaysintpr(interval, off)
    293 	u_int interval;
    294 	off_t off;
    295 {
    296 	struct nfsstats nfsstats, lastst;
    297 	int hdrcnt, oldmask;
    298 	void catchalarm();
    299 
    300 	(void)signal(SIGALRM, catchalarm);
    301 	signalled = 0;
    302 	(void)alarm(interval);
    303 	bzero((caddr_t)&lastst, sizeof(lastst));
    304 
    305 	for (hdrcnt = 1;;) {
    306 		if (!--hdrcnt) {
    307 			printhdr();
    308 			hdrcnt = 20;
    309 		}
    310 		kvm_read(kmem, off, (char *)&nfsstats,
    311 			sizeof(struct nfsstats));
    312 		printf("Client: %8d %8d %8d %8d %8d %8d %8d %8d\n",
    313 		    nfsstats.rpccnt[1]-lastst.rpccnt[1],
    314 		    nfsstats.rpccnt[4]-lastst.rpccnt[4],
    315 		    nfsstats.rpccnt[5]-lastst.rpccnt[5],
    316 		    nfsstats.rpccnt[6]-lastst.rpccnt[6],
    317 		    nfsstats.rpccnt[8]-lastst.rpccnt[8],
    318 		    nfsstats.rpccnt[11]-lastst.rpccnt[11],
    319 		    nfsstats.rpccnt[12]-lastst.rpccnt[12],
    320 		    nfsstats.rpccnt[16]-lastst.rpccnt[16]);
    321 		printf("Server: %8d %8d %8d %8d %8d %8d %8d %8d\n",
    322 		    nfsstats.srvrpccnt[1]-lastst.srvrpccnt[1],
    323 		    nfsstats.srvrpccnt[4]-lastst.srvrpccnt[4],
    324 		    nfsstats.srvrpccnt[5]-lastst.srvrpccnt[5],
    325 		    nfsstats.srvrpccnt[6]-lastst.srvrpccnt[6],
    326 		    nfsstats.srvrpccnt[8]-lastst.srvrpccnt[8],
    327 		    nfsstats.srvrpccnt[11]-lastst.srvrpccnt[11],
    328 		    nfsstats.srvrpccnt[12]-lastst.srvrpccnt[12],
    329 		    nfsstats.srvrpccnt[16]-lastst.srvrpccnt[16]);
    330 		lastst = nfsstats;
    331 		fflush(stdout);
    332 		oldmask = sigblock(sigmask(SIGALRM));
    333 		if (!signalled)
    334 			sigpause(0);
    335 		sigsetmask(oldmask);
    336 		signalled = 0;
    337 		(void)alarm(interval);
    338 	}
    339 	/*NOTREACHED*/
    340 }
    341 
    342 void
    343 printhdr()
    344 {
    345 	printf("        %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s\n",
    346 	    "Getattr", "Lookup", "Readlink", "Read", "Write", "Rename",
    347 	    "Link", "Readdir");
    348 	fflush(stdout);
    349 }
    350 
    351 /*
    352  * Called if an interval expires before sidewaysintpr has completed a loop.
    353  * Sets a flag to not wait for the alarm.
    354  */
    355 void
    356 catchalarm()
    357 {
    358 	signalled = 1;
    359 }
    360 
    361 void
    362 usage()
    363 {
    364 	(void)fprintf(stderr,
    365 	    "usage: nfsstat [-M core] [-N system] [-w interval]\n");
    366 	exit(1);
    367 }
    368