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