Home | History | Annotate | Line # | Download | only in nfsstat
nfsstat.c revision 1.3
      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.3 1994/05/27 11:19:08 cgd 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(), printhdr(), sidewaysintpr(), usage();
     93 
     94 main(argc, argv)
     95 	int argc;
     96 	char **argv;
     97 {
     98 	extern int optind;
     99 	extern char *optarg;
    100 	u_int interval;
    101 	int ch;
    102 
    103 	interval = 0;
    104 	while ((ch = getopt(argc, argv, "M:N:w:")) != EOF)
    105 		switch(ch) {
    106 		case 'M':
    107 			kmemf = optarg;
    108 			kflag = 1;
    109 			break;
    110 		case 'N':
    111 			kernel = optarg;
    112 			break;
    113 		case 'w':
    114 			interval = atoi(optarg);
    115 			break;
    116 		case '?':
    117 		default:
    118 			usage();
    119 		}
    120 	argc -= optind;
    121 	argv += optind;
    122 
    123 #define	BACKWARD_COMPATIBILITY
    124 #ifdef	BACKWARD_COMPATIBILITY
    125 	if (*argv) {
    126 		kernel = *++argv;
    127 		if (*++argv) {
    128 			kmemf = *argv;
    129 			kflag = 1;
    130 		}
    131 	}
    132 #endif
    133 	if (nlist(kernel, nl) < 0 || nl[0].n_type == 0) {
    134 		(void)fprintf(stderr, "nfsstate: %s: no namelist\n", kernel);
    135 		exit(1);
    136 	}
    137 	kmem = kvm_open(NULL,NULL,NULL,O_RDONLY,"kvm_open");
    138 	if (kmem == NULL) {
    139 		(void)fprintf(stderr,
    140 		    "nfsstat: %s: %s\n", kmemf, strerror(errno));
    141 		kvm_close(kmem);
    142 		exit(1);
    143 	}
    144 	if (kflag) {
    145 #ifdef NEWVM
    146 		(void)fprintf(stderr, "nfsstat: can't do core files yet\n");
    147 		exit(1);
    148 #else
    149 		off_t off;
    150 
    151 		Sysmap = (struct pte *)
    152 		   malloc((u_int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
    153 		if (!Sysmap) {
    154 			(void)fprintf(stderr, "nfsstat: %s\n", strerror(errno));
    155 			exit(1);
    156 		}
    157 		kvm_read(kmem, (long) nl[N_SYSMAP].n_value, (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 		kvm_close(kmem);
    165 		exit(1);
    166 	}
    167 	if (interval)
    168 		sidewaysintpr(interval, nl[N_NFSSTAT].n_value);
    169 	else
    170 		intpr(nl[N_NFSSTAT].n_value);
    171 	kvm_close(kmem);
    172 	exit(0);
    173 }
    174 
    175 /*
    176  * Print a description of the network interfaces.
    177  */
    178 void
    179 intpr(nfsstataddr)
    180 	off_t nfsstataddr;
    181 {
    182 	struct nfsstats nfsstats;
    183 
    184 	kvm_read(kmem, nfsstataddr, (char *)&nfsstats, sizeof(struct nfsstats));
    185 	printf("Client Info:\n");
    186 	printf("Rpc Counts:\n");
    187 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    188 		"Getattr", "Setattr", "Lookup", "Readlink", "Read",
    189 		"Write", "Create", "Remove");
    190 	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    191 		nfsstats.rpccnt[1],
    192 		nfsstats.rpccnt[2],
    193 		nfsstats.rpccnt[4],
    194 		nfsstats.rpccnt[5],
    195 		nfsstats.rpccnt[6],
    196 		nfsstats.rpccnt[8],
    197 		nfsstats.rpccnt[9],
    198 		nfsstats.rpccnt[10]);
    199 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    200 		"Rename", "Link", "Symlink", "Mkdir", "Rmdir",
    201 		"Readdir", "Statfs");
    202 	printf("%9d %9d %9d %9d %9d %9d %9d\n",
    203 		nfsstats.rpccnt[11],
    204 		nfsstats.rpccnt[12],
    205 		nfsstats.rpccnt[13],
    206 		nfsstats.rpccnt[14],
    207 		nfsstats.rpccnt[15],
    208 		nfsstats.rpccnt[16],
    209 		nfsstats.rpccnt[17]);
    210 	printf("Rpc Info:\n");
    211 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
    212 		"TimedOut", "Invalid", "X Replies", "Retries", "Requests");
    213 	printf("%9d %9d %9d %9d %9d\n",
    214 		nfsstats.rpctimeouts,
    215 		nfsstats.rpcinvalid,
    216 		nfsstats.rpcunexpected,
    217 		nfsstats.rpcretries,
    218 		nfsstats.rpcrequests);
    219 	printf("Cache Info:\n");
    220 	printf("%9.9s %9.9s %9.9s %9.9s",
    221 		"Attr Hits", "Misses", "Lkup Hits", "Misses");
    222 	printf(" %9.9s %9.9s %9.9s %9.9s\n",
    223 		"BioR Hits", "Misses", "BioW Hits", "Misses");
    224 	printf("%9d %9d %9d %9d",
    225 		nfsstats.attrcache_hits, nfsstats.attrcache_misses,
    226 		nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
    227 	printf(" %9d %9d %9d %9d\n",
    228 		nfsstats.biocache_reads-nfsstats.read_bios,
    229 		nfsstats.read_bios,
    230 		nfsstats.biocache_writes-nfsstats.write_bios,
    231 		nfsstats.write_bios);
    232 	printf("%9.9s %9.9s %9.9s %9.9s",
    233 		"BioRLHits", "Misses", "BioD Hits", "Misses");
    234 	printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
    235 	printf("%9d %9d %9d %9d",
    236 		nfsstats.biocache_readlinks-nfsstats.readlink_bios,
    237 		nfsstats.readlink_bios,
    238 		nfsstats.biocache_readdirs-nfsstats.readdir_bios,
    239 		nfsstats.readdir_bios);
    240 	printf(" %9d %9d\n",
    241 		nfsstats.direofcache_hits, nfsstats.direofcache_misses);
    242 	printf("\nServer Info:\n");
    243 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    244 		"Getattr", "Setattr", "Lookup", "Readlink", "Read",
    245 		"Write", "Create", "Remove");
    246 	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
    247 		nfsstats.srvrpccnt[1],
    248 		nfsstats.srvrpccnt[2],
    249 		nfsstats.srvrpccnt[4],
    250 		nfsstats.srvrpccnt[5],
    251 		nfsstats.srvrpccnt[6],
    252 		nfsstats.srvrpccnt[8],
    253 		nfsstats.srvrpccnt[9],
    254 		nfsstats.srvrpccnt[10]);
    255 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
    256 		"Rename", "Link", "Symlink", "Mkdir", "Rmdir",
    257 		"Readdir", "Statfs");
    258 	printf("%9d %9d %9d %9d %9d %9d %9d\n",
    259 		nfsstats.srvrpccnt[11],
    260 		nfsstats.srvrpccnt[12],
    261 		nfsstats.srvrpccnt[13],
    262 		nfsstats.srvrpccnt[14],
    263 		nfsstats.srvrpccnt[15],
    264 		nfsstats.srvrpccnt[16],
    265 		nfsstats.srvrpccnt[17]);
    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 }
    279 
    280 u_char	signalled;			/* set if alarm goes off "early" */
    281 
    282 /*
    283  * Print a running summary of nfs statistics.
    284  * Repeat display every interval seconds, showing statistics
    285  * collected over that interval.  Assumes that interval is non-zero.
    286  * First line printed at top of screen is always cumulative.
    287  */
    288 void
    289 sidewaysintpr(interval, off)
    290 	u_int interval;
    291 	off_t off;
    292 {
    293 	struct nfsstats nfsstats, lastst;
    294 	int hdrcnt, oldmask;
    295 	void catchalarm();
    296 
    297 	(void)signal(SIGALRM, catchalarm);
    298 	signalled = 0;
    299 	(void)alarm(interval);
    300 	bzero((caddr_t)&lastst, sizeof(lastst));
    301 
    302 	for (hdrcnt = 1;;) {
    303 		if (!--hdrcnt) {
    304 			printhdr();
    305 			hdrcnt = 20;
    306 		}
    307 		kvm_read(kmem, off, (char *)&nfsstats,
    308 			sizeof(struct nfsstats));
    309 		printf("Client: %8d %8d %8d %8d %8d %8d %8d %8d\n",
    310 		    nfsstats.rpccnt[1]-lastst.rpccnt[1],
    311 		    nfsstats.rpccnt[4]-lastst.rpccnt[4],
    312 		    nfsstats.rpccnt[5]-lastst.rpccnt[5],
    313 		    nfsstats.rpccnt[6]-lastst.rpccnt[6],
    314 		    nfsstats.rpccnt[8]-lastst.rpccnt[8],
    315 		    nfsstats.rpccnt[11]-lastst.rpccnt[11],
    316 		    nfsstats.rpccnt[12]-lastst.rpccnt[12],
    317 		    nfsstats.rpccnt[16]-lastst.rpccnt[16]);
    318 		printf("Server: %8d %8d %8d %8d %8d %8d %8d %8d\n",
    319 		    nfsstats.srvrpccnt[1]-lastst.srvrpccnt[1],
    320 		    nfsstats.srvrpccnt[4]-lastst.srvrpccnt[4],
    321 		    nfsstats.srvrpccnt[5]-lastst.srvrpccnt[5],
    322 		    nfsstats.srvrpccnt[6]-lastst.srvrpccnt[6],
    323 		    nfsstats.srvrpccnt[8]-lastst.srvrpccnt[8],
    324 		    nfsstats.srvrpccnt[11]-lastst.srvrpccnt[11],
    325 		    nfsstats.srvrpccnt[12]-lastst.srvrpccnt[12],
    326 		    nfsstats.srvrpccnt[16]-lastst.srvrpccnt[16]);
    327 		lastst = nfsstats;
    328 		fflush(stdout);
    329 		oldmask = sigblock(sigmask(SIGALRM));
    330 		if (!signalled)
    331 			sigpause(0);
    332 		sigsetmask(oldmask);
    333 		signalled = 0;
    334 		(void)alarm(interval);
    335 	}
    336 	/*NOTREACHED*/
    337 }
    338 
    339 void
    340 printhdr()
    341 {
    342 	printf("        %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s\n",
    343 	    "Getattr", "Lookup", "Readlink", "Read", "Write", "Rename",
    344 	    "Link", "Readdir");
    345 	fflush(stdout);
    346 }
    347 
    348 /*
    349  * Called if an interval expires before sidewaysintpr has completed a loop.
    350  * Sets a flag to not wait for the alarm.
    351  */
    352 void
    353 catchalarm()
    354 {
    355 	signalled = 1;
    356 }
    357 
    358 void
    359 usage()
    360 {
    361 	(void)fprintf(stderr,
    362 	    "usage: nfsstat [-M core] [-N system] [-w interval]\n");
    363 	exit(1);
    364 }
    365