xref: /netbsd-src/usr.bin/nfsstat/nfsstat.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: nfsstat.c,v 1.9 1997/10/19 06:27:21 lukem Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Rick Macklem at The University of Guelph.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __COPYRIGHT("@(#) Copyright (c) 1983, 1989, 1993\n\
42 	The Regents of the University of California.  All rights reserved.\n");
43 #endif /* not lint */
44 
45 #ifndef lint
46 #if 0
47 static char sccsid[] = "from: @(#)nfsstat.c	8.1 (Berkeley) 6/6/93";
48 #else
49 __RCSID("$NetBSD: nfsstat.c,v 1.9 1997/10/19 06:27:21 lukem Exp $");
50 #endif
51 #endif /* not lint */
52 
53 #include <sys/param.h>
54 #include <sys/mount.h>
55 #include <sys/sysctl.h>
56 
57 #include <nfs/rpcv2.h>
58 #include <nfs/nfsproto.h>
59 #include <nfs/nfs.h>
60 
61 #include <ctype.h>
62 #include <err.h>
63 #include <errno.h>
64 #include <fcntl.h>
65 #include <kvm.h>
66 #include <limits.h>
67 #include <nlist.h>
68 #include <paths.h>
69 #include <signal.h>
70 #include <stdlib.h>
71 #include <stdio.h>
72 #include <string.h>
73 #include <unistd.h>
74 
75 struct nlist nl[] = {
76 #define	N_NFSSTAT	0
77 	{ "_nfsstats" },
78 	{ "" },
79 };
80 kvm_t *kd;
81 
82 void	catchalarm __P((int));
83 void	intpr __P((u_long));
84 int	main __P((int, char **));
85 void	printhdr __P((void));
86 void	sidewaysintpr __P((u_int, u_long));
87 void	usage __P((void));
88 
89 int
90 main(argc, argv)
91 	int argc;
92 	char **argv;
93 {
94 	extern int optind;
95 	extern char *optarg;
96 	u_int interval;
97 	int ch;
98 	char *memf, *nlistf;
99 	char errbuf[_POSIX2_LINE_MAX];
100 
101 	interval = 0;
102 	memf = nlistf = NULL;
103 	while ((ch = getopt(argc, argv, "M: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 			interval = atoi(optarg);
113 			break;
114 		case '?':
115 		default:
116 			usage();
117 		}
118 	argc -= optind;
119 	argv += optind;
120 
121 #define	BACKWARD_COMPATIBILITY
122 #ifdef	BACKWARD_COMPATIBILITY
123 	if (*argv) {
124 		interval = atoi(*argv);
125 		if (*++argv) {
126 			nlistf = *argv;
127 			if (*++argv)
128 				memf = *argv;
129 		}
130 	}
131 #endif
132 	/*
133 	 * Discard setgid privileges if not the running kernel so that bad
134 	 * guys can't print interesting stuff from kernel memory.
135 	 */
136 	if (nlistf != NULL || memf != NULL)
137 		setgid(getgid());
138 
139 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == 0)
140 		errx(1, "kvm_openfiles: %s", errbuf);
141 	if (kvm_nlist(kd, nl) != 0)
142 		errx(1, "kvm_nlist: can't get names");
143 
144 	if (interval)
145 		sidewaysintpr(interval, nl[N_NFSSTAT].n_value);
146 	else
147 		intpr(nl[N_NFSSTAT].n_value);
148 	exit(0);
149 }
150 
151 /*
152  * Print a description of the nfs stats.
153  */
154 void
155 intpr(nfsstataddr)
156 	u_long nfsstataddr;
157 {
158 	struct nfsstats nfsstats;
159 
160 	if (kvm_read(kd, (u_long)nfsstataddr,
161 	    (char *)&nfsstats, sizeof(struct nfsstats)) < 0)
162 		errx(1, "kvm_read failed");
163 	printf("Client Info:\n");
164 	printf("Rpc Counts:\n");
165 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
166 		"Getattr", "Setattr", "Lookup", "Readlink", "Read",
167 		"Write", "Create", "Remove");
168 	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
169 		nfsstats.rpccnt[NFSPROC_GETATTR],
170 		nfsstats.rpccnt[NFSPROC_SETATTR],
171 		nfsstats.rpccnt[NFSPROC_LOOKUP],
172 		nfsstats.rpccnt[NFSPROC_READLINK],
173 		nfsstats.rpccnt[NFSPROC_READ],
174 		nfsstats.rpccnt[NFSPROC_WRITE],
175 		nfsstats.rpccnt[NFSPROC_CREATE],
176 		nfsstats.rpccnt[NFSPROC_REMOVE]);
177 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
178 		"Rename", "Link", "Symlink", "Mkdir", "Rmdir",
179 		"Readdir", "RdirPlus", "Access");
180 	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
181 		nfsstats.rpccnt[NFSPROC_RENAME],
182 		nfsstats.rpccnt[NFSPROC_LINK],
183 		nfsstats.rpccnt[NFSPROC_SYMLINK],
184 		nfsstats.rpccnt[NFSPROC_MKDIR],
185 		nfsstats.rpccnt[NFSPROC_RMDIR],
186 		nfsstats.rpccnt[NFSPROC_READDIR],
187 		nfsstats.rpccnt[NFSPROC_READDIRPLUS],
188 		nfsstats.rpccnt[NFSPROC_ACCESS]);
189 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
190 		"Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
191 		"GLease", "Vacate", "Evict");
192 	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
193 		nfsstats.rpccnt[NFSPROC_MKNOD],
194 		nfsstats.rpccnt[NFSPROC_FSSTAT],
195 		nfsstats.rpccnt[NFSPROC_FSINFO],
196 		nfsstats.rpccnt[NFSPROC_PATHCONF],
197 		nfsstats.rpccnt[NFSPROC_COMMIT],
198 		nfsstats.rpccnt[NQNFSPROC_GETLEASE],
199 		nfsstats.rpccnt[NQNFSPROC_VACATED],
200 		nfsstats.rpccnt[NQNFSPROC_EVICTED]);
201 	printf("Rpc Info:\n");
202 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
203 		"TimedOut", "Invalid", "X Replies", "Retries", "Requests");
204 	printf("%9d %9d %9d %9d %9d\n",
205 		nfsstats.rpctimeouts,
206 		nfsstats.rpcinvalid,
207 		nfsstats.rpcunexpected,
208 		nfsstats.rpcretries,
209 		nfsstats.rpcrequests);
210 	printf("Cache Info:\n");
211 	printf("%9.9s %9.9s %9.9s %9.9s",
212 		"Attr Hits", "Misses", "Lkup Hits", "Misses");
213 	printf(" %9.9s %9.9s %9.9s %9.9s\n",
214 		"BioR Hits", "Misses", "BioW Hits", "Misses");
215 	printf("%9d %9d %9d %9d",
216 		nfsstats.attrcache_hits, nfsstats.attrcache_misses,
217 		nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
218 	printf(" %9d %9d %9d %9d\n",
219 		nfsstats.biocache_reads-nfsstats.read_bios,
220 		nfsstats.read_bios,
221 		nfsstats.biocache_writes-nfsstats.write_bios,
222 		nfsstats.write_bios);
223 	printf("%9.9s %9.9s %9.9s %9.9s",
224 		"BioRLHits", "Misses", "BioD Hits", "Misses");
225 	printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
226 	printf("%9d %9d %9d %9d",
227 		nfsstats.biocache_readlinks-nfsstats.readlink_bios,
228 		nfsstats.readlink_bios,
229 		nfsstats.biocache_readdirs-nfsstats.readdir_bios,
230 		nfsstats.readdir_bios);
231 	printf(" %9d %9d\n",
232 		nfsstats.direofcache_hits, nfsstats.direofcache_misses);
233 	printf("\nServer Info:\n");
234 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
235 		"Getattr", "Setattr", "Lookup", "Readlink", "Read",
236 		"Write", "Create", "Remove");
237 	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
238 		nfsstats.srvrpccnt[NFSPROC_GETATTR],
239 		nfsstats.srvrpccnt[NFSPROC_SETATTR],
240 		nfsstats.srvrpccnt[NFSPROC_LOOKUP],
241 		nfsstats.srvrpccnt[NFSPROC_READLINK],
242 		nfsstats.srvrpccnt[NFSPROC_READ],
243 		nfsstats.srvrpccnt[NFSPROC_WRITE],
244 		nfsstats.srvrpccnt[NFSPROC_CREATE],
245 		nfsstats.srvrpccnt[NFSPROC_REMOVE]);
246 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
247 		"Rename", "Link", "Symlink", "Mkdir", "Rmdir",
248 		"Readdir", "RdirPlus", "Access");
249 	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
250 		nfsstats.srvrpccnt[NFSPROC_RENAME],
251 		nfsstats.srvrpccnt[NFSPROC_LINK],
252 		nfsstats.srvrpccnt[NFSPROC_SYMLINK],
253 		nfsstats.srvrpccnt[NFSPROC_MKDIR],
254 		nfsstats.srvrpccnt[NFSPROC_RMDIR],
255 		nfsstats.srvrpccnt[NFSPROC_READDIR],
256 		nfsstats.srvrpccnt[NFSPROC_READDIRPLUS],
257 		nfsstats.srvrpccnt[NFSPROC_ACCESS]);
258 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
259 		"Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
260 		"GLease", "Vacate", "Evict");
261 	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
262 		nfsstats.srvrpccnt[NFSPROC_MKNOD],
263 		nfsstats.srvrpccnt[NFSPROC_FSSTAT],
264 		nfsstats.srvrpccnt[NFSPROC_FSINFO],
265 		nfsstats.srvrpccnt[NFSPROC_PATHCONF],
266 		nfsstats.srvrpccnt[NFSPROC_COMMIT],
267 		nfsstats.srvrpccnt[NQNFSPROC_GETLEASE],
268 		nfsstats.srvrpccnt[NQNFSPROC_VACATED],
269 		nfsstats.srvrpccnt[NQNFSPROC_EVICTED]);
270 	printf("Server Ret-Failed\n");
271 	printf("%17d\n", nfsstats.srvrpc_errs);
272 	printf("Server Faults\n");
273 	printf("%13d\n", nfsstats.srv_errs);
274 	printf("Server Cache Stats:\n");
275 	printf("%9.9s %9.9s %9.9s %9.9s\n",
276 		"Inprog", "Idem", "Non-idem", "Misses");
277 	printf("%9d %9d %9d %9d\n",
278 		nfsstats.srvcache_inproghits,
279 		nfsstats.srvcache_idemdonehits,
280 		nfsstats.srvcache_nonidemdonehits,
281 		nfsstats.srvcache_misses);
282 	printf("Server Lease Stats:\n");
283 	printf("%9.9s %9.9s %9.9s\n",
284 		"Leases", "PeakL", "GLeases");
285 	printf("%9d %9d %9d\n",
286 		nfsstats.srvnqnfs_leases,
287 		nfsstats.srvnqnfs_maxleases,
288 		nfsstats.srvnqnfs_getleases);
289 	printf("Server Write Gathering:\n");
290 	printf("%9.9s %9.9s %9.9s\n",
291 		"WriteOps", "WriteRPC", "Opsaved");
292 	printf("%9d %9d %9d\n",
293 		nfsstats.srvvop_writes,
294 		nfsstats.srvrpccnt[NFSPROC_WRITE],
295 		nfsstats.srvrpccnt[NFSPROC_WRITE] - nfsstats.srvvop_writes);
296 }
297 
298 u_char	signalled;			/* set if alarm goes off "early" */
299 
300 /*
301  * Print a running summary of nfs statistics.
302  * Repeat display every interval seconds, showing statistics
303  * collected over that interval.  Assumes that interval is non-zero.
304  * First line printed at top of screen is always cumulative.
305  */
306 void
307 sidewaysintpr(interval, off)
308 	u_int interval;
309 	u_long off;
310 {
311 	struct nfsstats nfsstats, lastst;
312 	int hdrcnt, oldmask;
313 
314 	(void)signal(SIGALRM, catchalarm);
315 	signalled = 0;
316 	(void)alarm(interval);
317 	memset((caddr_t)&lastst, 0, sizeof(lastst));
318 
319 	for (hdrcnt = 1;;) {
320 		if (!--hdrcnt) {
321 			printhdr();
322 			hdrcnt = 20;
323 		}
324 		if (kvm_read(kd, off, (char *)&nfsstats, sizeof nfsstats) < 0)
325 			errx(1, "kvm_read failed");
326 		printf("Client: %8d %8d %8d %8d %8d %8d %8d %8d\n",
327 		    nfsstats.rpccnt[NFSPROC_GETATTR]-lastst.rpccnt[NFSPROC_GETATTR],
328 		    nfsstats.rpccnt[NFSPROC_LOOKUP]-lastst.rpccnt[NFSPROC_LOOKUP],
329 		    nfsstats.rpccnt[NFSPROC_READLINK]-lastst.rpccnt[NFSPROC_READLINK],
330 		    nfsstats.rpccnt[NFSPROC_READ]-lastst.rpccnt[NFSPROC_READ],
331 		    nfsstats.rpccnt[NFSPROC_WRITE]-lastst.rpccnt[NFSPROC_WRITE],
332 		    nfsstats.rpccnt[NFSPROC_RENAME]-lastst.rpccnt[NFSPROC_RENAME],
333 		    nfsstats.rpccnt[NFSPROC_ACCESS]-lastst.rpccnt[NFSPROC_ACCESS],
334 		    (nfsstats.rpccnt[NFSPROC_READDIR]-lastst.rpccnt[NFSPROC_READDIR])
335 		    +(nfsstats.rpccnt[NFSPROC_READDIRPLUS]-lastst.rpccnt[NFSPROC_READDIRPLUS]));
336 		printf("Server: %8d %8d %8d %8d %8d %8d %8d %8d\n",
337 		    nfsstats.srvrpccnt[NFSPROC_GETATTR]-lastst.srvrpccnt[NFSPROC_GETATTR],
338 		    nfsstats.srvrpccnt[NFSPROC_LOOKUP]-lastst.srvrpccnt[NFSPROC_LOOKUP],
339 		    nfsstats.srvrpccnt[NFSPROC_READLINK]-lastst.srvrpccnt[NFSPROC_READLINK],
340 		    nfsstats.srvrpccnt[NFSPROC_READ]-lastst.srvrpccnt[NFSPROC_READ],
341 		    nfsstats.srvrpccnt[NFSPROC_WRITE]-lastst.srvrpccnt[NFSPROC_WRITE],
342 		    nfsstats.srvrpccnt[NFSPROC_RENAME]-lastst.srvrpccnt[NFSPROC_RENAME],
343 		    nfsstats.srvrpccnt[NFSPROC_ACCESS]-lastst.srvrpccnt[NFSPROC_ACCESS],
344 		    (nfsstats.srvrpccnt[NFSPROC_READDIR]-lastst.srvrpccnt[NFSPROC_READDIR])
345 		    +(nfsstats.srvrpccnt[NFSPROC_READDIRPLUS]-lastst.srvrpccnt[NFSPROC_READDIRPLUS]));
346 		lastst = nfsstats;
347 		fflush(stdout);
348 		oldmask = sigblock(sigmask(SIGALRM));
349 		if (!signalled)
350 			sigpause(0);
351 		sigsetmask(oldmask);
352 		signalled = 0;
353 		(void)alarm(interval);
354 	}
355 	/*NOTREACHED*/
356 }
357 
358 void
359 printhdr()
360 {
361 	printf("        %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s\n",
362 	    "Getattr", "Lookup", "Readlink", "Read", "Write", "Rename",
363 	    "Access", "Readdir");
364 	fflush(stdout);
365 }
366 
367 /*
368  * Called if an interval expires before sidewaysintpr has completed a loop.
369  * Sets a flag to not wait for the alarm.
370  */
371 void
372 catchalarm(dummy)
373 	int dummy;
374 {
375 	signalled = 1;
376 }
377 
378 void
379 usage()
380 {
381 	(void)fprintf(stderr,
382 	    "usage: nfsstat [-M core] [-N system] [-w interval]\n");
383 	exit(1);
384 }
385