xref: /freebsd/usr.bin/nfsstat/nfsstat.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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 #if 0
45 static char sccsid[] = "@(#)nfsstat.c	8.2 (Berkeley) 3/31/95";
46 #endif
47 static const char rcsid[] =
48   "$FreeBSD$";
49 #endif /* not lint */
50 
51 #include <sys/param.h>
52 #include <sys/mount.h>
53 #include <sys/time.h>
54 #include <sys/sysctl.h>
55 #include <nfs/rpcv2.h>
56 #include <nfs/nfsproto.h>
57 #include <nfsclient/nfs.h>
58 #include <nfsserver/nfs.h>
59 #include <signal.h>
60 #include <fcntl.h>
61 #include <ctype.h>
62 #include <errno.h>
63 #include <kvm.h>
64 #include <limits.h>
65 #include <nlist.h>
66 #include <unistd.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <paths.h>
71 #include <err.h>
72 
73 struct nlist nl[] = {
74 #define	N_NFSSTAT	0
75 	{ "nfsstats" },
76 #define	N_NFSRVSTAT	1
77 	{ "nfsrvstats" },
78 	"",
79 };
80 kvm_t *kd;
81 
82 static int deadkernel = 0;
83 static int widemode = 0;
84 static int zflag = 0;
85 
86 void intpr(int, int);
87 void printhdr(int, int);
88 void sidewaysintpr(u_int, int, int);
89 void usage(void);
90 char *sperc1(int, int);
91 char *sperc2(int, int);
92 
93 #define DELTA(field)	(nfsstats.field - lastst.field)
94 
95 int
96 main(int argc, char **argv)
97 {
98 	u_int interval;
99 	int clientOnly = -1;
100 	int serverOnly = -1;
101 	int ch;
102 	char *memf, *nlistf;
103 	char errbuf[_POSIX2_LINE_MAX];
104 
105 	interval = 0;
106 	memf = nlistf = NULL;
107 	while ((ch = getopt(argc, argv, "csWM:N:w:z")) != -1)
108 		switch(ch) {
109 		case 'M':
110 			memf = optarg;
111 			break;
112 		case 'N':
113 			nlistf = optarg;
114 			break;
115 		case 'W':
116 			widemode = 1;
117 			break;
118 		case 'w':
119 			interval = atoi(optarg);
120 			break;
121 		case 'c':
122 			clientOnly = 1;
123 			if (serverOnly < 0)
124 				serverOnly = 0;
125 			break;
126 		case 's':
127 			serverOnly = 1;
128 			if (clientOnly < 0)
129 				clientOnly = 0;
130 			break;
131 		case 'z':
132 			zflag = 1;
133 			break;
134 		case '?':
135 		default:
136 			usage();
137 		}
138 	argc -= optind;
139 	argv += optind;
140 
141 #define	BACKWARD_COMPATIBILITY
142 #ifdef	BACKWARD_COMPATIBILITY
143 	if (*argv) {
144 		interval = atoi(*argv);
145 		if (*++argv) {
146 			nlistf = *argv;
147 			if (*++argv)
148 				memf = *argv;
149 		}
150 	}
151 #endif
152 	if (nlistf != NULL || memf != NULL) {
153 		deadkernel = 1;
154 
155 		if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
156 					errbuf)) == 0) {
157 			errx(1, "kvm_openfiles: %s", errbuf);
158 		}
159 		if (kvm_nlist(kd, nl) != 0) {
160 			errx(1, "kvm_nlist: can't get names");
161 		}
162 	}
163 
164 	if (interval)
165 		sidewaysintpr(interval, clientOnly, serverOnly);
166 	else
167 		intpr(clientOnly, serverOnly);
168 	exit(0);
169 }
170 
171 /*
172  * Read the nfs stats using sysctl(3) for live kernels, or kvm_read
173  * for dead ones.
174  */
175 void
176 readstats(struct nfsstats **stp, struct nfsrvstats **srvstp, int zero)
177 {
178 	union {
179 		struct nfsstats client;
180 		struct nfsrvstats server;
181 	} zerostat;
182 	size_t buflen;
183 
184 	if (deadkernel) {
185 		if (*stp != NULL && kvm_read(kd, (u_long)nl[N_NFSSTAT].n_value,
186 		    *stp, sizeof(struct nfsstats)) < 0) {
187 			*stp = NULL;
188 		}
189 		if (*srvstp != NULL && kvm_read(kd,
190 		    (u_long)nl[N_NFSRVSTAT].n_value, *srvstp,
191 		    sizeof(struct nfsrvstats)) < 0) {
192 			*srvstp = NULL;
193 		}
194 	} else {
195 		if (zero)
196 			bzero(&zerostat, sizeof(zerostat));
197 		buflen = sizeof(struct nfsstats);
198 		if (*stp != NULL && sysctlbyname("vfs.nfs.nfsstats", *stp,
199 		    &buflen, zero ? &zerostat : NULL, zero ? buflen : 0) < 0) {
200 			if (errno != ENOENT)
201 				err(1, "sysctl: vfs.nfs.nfsstats");
202 			*stp = NULL;
203 		}
204 		buflen = sizeof(struct nfsrvstats);
205 		if (*srvstp != NULL && sysctlbyname("vfs.nfsrv.nfsrvstats",
206 		    *srvstp, &buflen, zero ? &zerostat : NULL,
207 		    zero ? buflen : 0) < 0) {
208 			if (errno != ENOENT)
209 				err(1, "sysctl: vfs.nfsrv.nfsrvstats");
210 			*srvstp = NULL;
211 		}
212 	}
213 }
214 
215 /*
216  * Print a description of the nfs stats.
217  */
218 void
219 intpr(int clientOnly, int serverOnly)
220 {
221 	struct nfsstats nfsstats, *nfsstatsp;
222 	struct nfsrvstats nfsrvstats, *nfsrvstatsp;
223 
224 	/*
225 	 * Only read the stats we are going to display to avoid zeroing
226 	 * stats the user didn't request.
227 	 */
228 	if (clientOnly)
229 		nfsstatsp = &nfsstats;
230 	else
231 		nfsstatsp = NULL;
232 	if (serverOnly)
233 		nfsrvstatsp = &nfsrvstats;
234 	else
235 		nfsrvstatsp = NULL;
236 
237 	readstats(&nfsstatsp, &nfsrvstatsp, zflag);
238 
239 	if (clientOnly && !nfsstatsp) {
240 		printf("Client not present!\n");
241 		clientOnly = 0;
242 	}
243 	if (clientOnly) {
244 		printf("Client Info:\n");
245 		printf("Rpc Counts:\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.rpccnt[NFSPROC_GETATTR],
251 			nfsstats.rpccnt[NFSPROC_SETATTR],
252 			nfsstats.rpccnt[NFSPROC_LOOKUP],
253 			nfsstats.rpccnt[NFSPROC_READLINK],
254 			nfsstats.rpccnt[NFSPROC_READ],
255 			nfsstats.rpccnt[NFSPROC_WRITE],
256 			nfsstats.rpccnt[NFSPROC_CREATE],
257 			nfsstats.rpccnt[NFSPROC_REMOVE]);
258 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
259 			"Rename", "Link", "Symlink", "Mkdir", "Rmdir",
260 			"Readdir", "RdirPlus", "Access");
261 		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
262 			nfsstats.rpccnt[NFSPROC_RENAME],
263 			nfsstats.rpccnt[NFSPROC_LINK],
264 			nfsstats.rpccnt[NFSPROC_SYMLINK],
265 			nfsstats.rpccnt[NFSPROC_MKDIR],
266 			nfsstats.rpccnt[NFSPROC_RMDIR],
267 			nfsstats.rpccnt[NFSPROC_READDIR],
268 			nfsstats.rpccnt[NFSPROC_READDIRPLUS],
269 			nfsstats.rpccnt[NFSPROC_ACCESS]);
270 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
271 			"Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit");
272 		printf("%9d %9d %9d %9d %9d\n",
273 			nfsstats.rpccnt[NFSPROC_MKNOD],
274 			nfsstats.rpccnt[NFSPROC_FSSTAT],
275 			nfsstats.rpccnt[NFSPROC_FSINFO],
276 			nfsstats.rpccnt[NFSPROC_PATHCONF],
277 			nfsstats.rpccnt[NFSPROC_COMMIT]);
278 		printf("Rpc Info:\n");
279 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
280 			"TimedOut", "Invalid", "X Replies", "Retries",
281 			"Requests");
282 		printf("%9d %9d %9d %9d %9d\n",
283 			nfsstats.rpctimeouts,
284 			nfsstats.rpcinvalid,
285 			nfsstats.rpcunexpected,
286 			nfsstats.rpcretries,
287 			nfsstats.rpcrequests);
288 		printf("Cache Info:\n");
289 		printf("%9.9s %9.9s %9.9s %9.9s",
290 			"Attr Hits", "Misses", "Lkup Hits", "Misses");
291 		printf(" %9.9s %9.9s %9.9s %9.9s\n",
292 			"BioR Hits", "Misses", "BioW Hits", "Misses");
293 		printf("%9d %9d %9d %9d",
294 			nfsstats.attrcache_hits, nfsstats.attrcache_misses,
295 			nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
296 		printf(" %9d %9d %9d %9d\n",
297 			nfsstats.biocache_reads-nfsstats.read_bios,
298 			nfsstats.read_bios,
299 			nfsstats.biocache_writes-nfsstats.write_bios,
300 			nfsstats.write_bios);
301 		printf("%9.9s %9.9s %9.9s %9.9s",
302 			"BioRLHits", "Misses", "BioD Hits", "Misses");
303 		printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
304 		printf("%9d %9d %9d %9d",
305 			nfsstats.biocache_readlinks-nfsstats.readlink_bios,
306 			nfsstats.readlink_bios,
307 			nfsstats.biocache_readdirs-nfsstats.readdir_bios,
308 			nfsstats.readdir_bios);
309 		printf(" %9d %9d\n",
310 			nfsstats.direofcache_hits, nfsstats.direofcache_misses);
311 	}
312 	if (serverOnly && !nfsrvstatsp) {
313 		printf("Server not present!\n");
314 		serverOnly = 0;
315 	}
316 	if (serverOnly) {
317 		printf("\nServer Info:\n");
318 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
319 			"Getattr", "Setattr", "Lookup", "Readlink", "Read",
320 			"Write", "Create", "Remove");
321 		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
322 			nfsrvstats.srvrpccnt[NFSPROC_GETATTR],
323 			nfsrvstats.srvrpccnt[NFSPROC_SETATTR],
324 			nfsrvstats.srvrpccnt[NFSPROC_LOOKUP],
325 			nfsrvstats.srvrpccnt[NFSPROC_READLINK],
326 			nfsrvstats.srvrpccnt[NFSPROC_READ],
327 			nfsrvstats.srvrpccnt[NFSPROC_WRITE],
328 			nfsrvstats.srvrpccnt[NFSPROC_CREATE],
329 			nfsrvstats.srvrpccnt[NFSPROC_REMOVE]);
330 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
331 			"Rename", "Link", "Symlink", "Mkdir", "Rmdir",
332 			"Readdir", "RdirPlus", "Access");
333 		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
334 			nfsrvstats.srvrpccnt[NFSPROC_RENAME],
335 			nfsrvstats.srvrpccnt[NFSPROC_LINK],
336 			nfsrvstats.srvrpccnt[NFSPROC_SYMLINK],
337 			nfsrvstats.srvrpccnt[NFSPROC_MKDIR],
338 			nfsrvstats.srvrpccnt[NFSPROC_RMDIR],
339 			nfsrvstats.srvrpccnt[NFSPROC_READDIR],
340 			nfsrvstats.srvrpccnt[NFSPROC_READDIRPLUS],
341 			nfsrvstats.srvrpccnt[NFSPROC_ACCESS]);
342 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
343 			"Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit");
344 		printf("%9d %9d %9d %9d %9d\n",
345 			nfsrvstats.srvrpccnt[NFSPROC_MKNOD],
346 			nfsrvstats.srvrpccnt[NFSPROC_FSSTAT],
347 			nfsrvstats.srvrpccnt[NFSPROC_FSINFO],
348 			nfsrvstats.srvrpccnt[NFSPROC_PATHCONF],
349 			nfsrvstats.srvrpccnt[NFSPROC_COMMIT]);
350 		printf("Server Ret-Failed\n");
351 		printf("%17d\n", nfsrvstats.srvrpc_errs);
352 		printf("Server Faults\n");
353 		printf("%13d\n", nfsrvstats.srv_errs);
354 		printf("Server Cache Stats:\n");
355 		printf("%9.9s %9.9s %9.9s %9.9s\n",
356 			"Inprog", "Idem", "Non-idem", "Misses");
357 		printf("%9d %9d %9d %9d\n",
358 			nfsrvstats.srvcache_inproghits,
359 			nfsrvstats.srvcache_idemdonehits,
360 			nfsrvstats.srvcache_nonidemdonehits,
361 			nfsrvstats.srvcache_misses);
362 		printf("Server Write Gathering:\n");
363 		printf("%9.9s %9.9s %9.9s\n",
364 			"WriteOps", "WriteRPC", "Opsaved");
365 		printf("%9d %9d %9d\n",
366 			nfsrvstats.srvvop_writes,
367 			nfsrvstats.srvrpccnt[NFSPROC_WRITE],
368 			nfsrvstats.srvrpccnt[NFSPROC_WRITE] -
369 			    nfsrvstats.srvvop_writes);
370 	}
371 }
372 
373 u_char	signalled;			/* set if alarm goes off "early" */
374 
375 /*
376  * Print a running summary of nfs statistics.
377  * Repeat display every interval seconds, showing statistics
378  * collected over that interval.  Assumes that interval is non-zero.
379  * First line printed at top of screen is always cumulative.
380  */
381 void
382 sidewaysintpr(u_int interval, int clientOnly, int serverOnly)
383 {
384 	struct nfsstats nfsstats, lastst, *nfsstatsp;
385 	struct nfsrvstats nfsrvstats, lastsrvst, *nfsrvstatsp;
386 	int hdrcnt = 1;
387 
388 	nfsstatsp = &lastst;
389 	nfsrvstatsp = &lastsrvst;
390 	readstats(&nfsstatsp, &nfsrvstatsp, 0);
391 	if (clientOnly && !nfsstatsp) {
392 		printf("Client not present!\n");
393 		clientOnly = 0;
394 	}
395 	if (serverOnly && !nfsrvstatsp) {
396 		printf("Server not present!\n");
397 		serverOnly = 0;
398 	}
399 	sleep(interval);
400 
401 	for (;;) {
402 		nfsstatsp = &nfsstats;
403 		nfsrvstatsp = &nfsrvstats;
404 		readstats(&nfsstatsp, &nfsrvstatsp, 0);
405 
406 		if (--hdrcnt == 0) {
407 			printhdr(clientOnly, serverOnly);
408 			if (clientOnly && serverOnly)
409 				hdrcnt = 10;
410 			else
411 				hdrcnt = 20;
412 		}
413 		if (clientOnly) {
414 		    printf("%s %6d %6d %6d %6d %6d %6d %6d %6d",
415 			((clientOnly && serverOnly) ? "Client:" : ""),
416 			DELTA(attrcache_hits) + DELTA(attrcache_misses),
417 			DELTA(lookupcache_hits) + DELTA(lookupcache_misses),
418 			DELTA(biocache_readlinks),
419 			DELTA(biocache_reads),
420 			DELTA(biocache_writes),
421 			nfsstats.rpccnt[NFSPROC_RENAME]-lastst.rpccnt[NFSPROC_RENAME],
422 			DELTA(accesscache_hits) + DELTA(accesscache_misses),
423 			DELTA(biocache_readdirs)
424 		    );
425 		    if (widemode) {
426 			    printf(" %s %s %s %s %s %s",
427 				sperc1(DELTA(attrcache_hits),
428 				    DELTA(attrcache_misses)),
429 				sperc1(DELTA(lookupcache_hits),
430 				    DELTA(lookupcache_misses)),
431 				sperc2(DELTA(biocache_reads),
432 				    DELTA(read_bios)),
433 				sperc2(DELTA(biocache_writes),
434 				    DELTA(write_bios)),
435 				sperc1(DELTA(accesscache_hits),
436 				    DELTA(accesscache_misses)),
437 				sperc2(DELTA(biocache_readdirs),
438 				    DELTA(readdir_bios))
439 			    );
440 		    }
441 		    printf("\n");
442 		    lastst = nfsstats;
443 		}
444 		if (serverOnly) {
445 		    printf("%s %6d %6d %6d %6d %6d %6d %6d %6d",
446 			((clientOnly && serverOnly) ? "Server:" : ""),
447 			nfsrvstats.srvrpccnt[NFSPROC_GETATTR]-lastsrvst.srvrpccnt[NFSPROC_GETATTR],
448 			nfsrvstats.srvrpccnt[NFSPROC_LOOKUP]-lastsrvst.srvrpccnt[NFSPROC_LOOKUP],
449 			nfsrvstats.srvrpccnt[NFSPROC_READLINK]-lastsrvst.srvrpccnt[NFSPROC_READLINK],
450 			nfsrvstats.srvrpccnt[NFSPROC_READ]-lastsrvst.srvrpccnt[NFSPROC_READ],
451 			nfsrvstats.srvrpccnt[NFSPROC_WRITE]-lastsrvst.srvrpccnt[NFSPROC_WRITE],
452 			nfsrvstats.srvrpccnt[NFSPROC_RENAME]-lastsrvst.srvrpccnt[NFSPROC_RENAME],
453 			nfsrvstats.srvrpccnt[NFSPROC_ACCESS]-lastsrvst.srvrpccnt[NFSPROC_ACCESS],
454 			(nfsrvstats.srvrpccnt[NFSPROC_READDIR]-lastsrvst.srvrpccnt[NFSPROC_READDIR])
455 			+(nfsrvstats.srvrpccnt[NFSPROC_READDIRPLUS]-lastsrvst.srvrpccnt[NFSPROC_READDIRPLUS]));
456 		    printf("\n");
457 		    lastsrvst = nfsrvstats;
458 		}
459 		fflush(stdout);
460 		sleep(interval);
461 	}
462 	/*NOTREACHED*/
463 }
464 
465 void
466 printhdr(int clientOnly, int serverOnly)
467 {
468 	printf("%s%6.6s %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s",
469 	    ((serverOnly && clientOnly) ? "        " : " "),
470 	    "GtAttr", "Lookup", "Rdlink", "Read", "Write", "Rename",
471 	    "Access", "Rddir");
472 	if (widemode && clientOnly) {
473 		printf(" Attr Lkup BioR BioW Accs BioD");
474 	}
475 	printf("\n");
476 	fflush(stdout);
477 }
478 
479 void
480 usage(void)
481 {
482 	(void)fprintf(stderr,
483 	    "usage: nfsstat [-cszW] [-M core] [-N system] [-w interval]\n");
484 	exit(1);
485 }
486 
487 static char SPBuf[64][8];
488 static int SPIndex;
489 
490 char *
491 sperc1(int hits, int misses)
492 {
493 	char *p = SPBuf[SPIndex];
494 
495 	if (hits + misses) {
496 		sprintf(p, "%3d%%",
497 		    (int)(char)((quad_t)hits * 100 / (hits + misses)));
498 	} else {
499 		sprintf(p, "   -");
500 	}
501 	SPIndex = (SPIndex + 1) & 63;
502 	return(p);
503 }
504 
505 char *
506 sperc2(int ttl, int misses)
507 {
508 	char *p = SPBuf[SPIndex];
509 
510 	if (ttl) {
511 		sprintf(p, "%3d%%",
512 		    (int)(char)((quad_t)(ttl - misses) * 100 / ttl));
513 	} else {
514 		sprintf(p, "   -");
515 	}
516 	SPIndex = (SPIndex + 1) & 63;
517 	return(p);
518 }
519 
520