xref: /netbsd-src/usr.bin/nfsstat/nfsstat.c (revision 89c5a767f8fc7a4633b2d409966e2becbb98ff92)
1 /*	$NetBSD: nfsstat.c,v 1.14 1998/07/06 07:50:20 mrg 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.14 1998/07/06 07:50:20 mrg 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     printall, clientinfo, serverinfo;
90 
91 int
92 main(argc, argv)
93 	int argc;
94 	char **argv;
95 {
96 	extern int optind;
97 	extern char *optarg;
98 	u_int interval;
99 	int ch;
100 	char *memf, *nlistf;
101 	char errbuf[_POSIX2_LINE_MAX];
102 	gid_t egid = getegid();
103 
104 	(void)setegid(getgid());
105 	interval = 0;
106 	memf = nlistf = NULL;
107 	printall = 1;
108 	while ((ch = getopt(argc, argv, "M:N:w:cs")) != -1)
109 		switch(ch) {
110 		case 'M':
111 			memf = optarg;
112 			break;
113 		case 'N':
114 			nlistf = optarg;
115 			break;
116 		case 'w':
117 			interval = atoi(optarg);
118 			break;
119 		case 's':
120 		        serverinfo = 1;
121 			printall = 0;
122 			break;
123 		case 'c':
124 		        clientinfo = 1;
125 			printall = 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, we toss
147 	 * them away totally so that bad guys can't print interesting stuff
148 	 * from kernel memory, otherwise switch back to kmem for the
149 	 * duration of the kvm_openfiles() call.
150 	 */
151 	if (nlistf != NULL || memf != NULL)
152 		(void)setgid(getgid());
153 	else
154 		(void)setegid(egid);
155 
156 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == 0)
157 		errx(1, "kvm_openfiles: %s", errbuf);
158 
159 	/* get rid of it now anyway */
160 	if (nlistf == NULL && memf == NULL)
161 		(void)setgid(getgid());
162 
163 	if (kvm_nlist(kd, nl) != 0)
164 		errx(1, "kvm_nlist: can't get names");
165 
166 	if (interval)
167 		sidewaysintpr(interval, nl[N_NFSSTAT].n_value);
168 	else
169 		intpr(nl[N_NFSSTAT].n_value);
170 	exit(0);
171 }
172 
173 /*
174  * Print a description of the nfs stats.
175  */
176 void
177 intpr(nfsstataddr)
178 	u_long nfsstataddr;
179 {
180 	struct nfsstats nfsstats;
181 
182 	if (kvm_read(kd, (u_long)nfsstataddr, (char *)&nfsstats,
183 	     sizeof(struct nfsstats)) != sizeof(struct nfsstats))
184 		errx(1, "kvm_read failed");
185 	if (printall || clientinfo) {
186 		printf("Client Info:\n");
187 		printf("Rpc Counts:\n");
188 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
189 		    "Getattr", "Setattr", "Lookup", "Readlink", "Read",
190 		    "Write", "Create", "Remove");
191 		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
192 		    nfsstats.rpccnt[NFSPROC_GETATTR],
193 		    nfsstats.rpccnt[NFSPROC_SETATTR],
194 		    nfsstats.rpccnt[NFSPROC_LOOKUP],
195 		    nfsstats.rpccnt[NFSPROC_READLINK],
196 		    nfsstats.rpccnt[NFSPROC_READ],
197 		    nfsstats.rpccnt[NFSPROC_WRITE],
198 		    nfsstats.rpccnt[NFSPROC_CREATE],
199 		    nfsstats.rpccnt[NFSPROC_REMOVE]);
200 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
201 		    "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
202 		    "Readdir", "RdirPlus", "Access");
203 		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
204 		    nfsstats.rpccnt[NFSPROC_RENAME],
205 		    nfsstats.rpccnt[NFSPROC_LINK],
206 		    nfsstats.rpccnt[NFSPROC_SYMLINK],
207 		    nfsstats.rpccnt[NFSPROC_MKDIR],
208 		    nfsstats.rpccnt[NFSPROC_RMDIR],
209 		    nfsstats.rpccnt[NFSPROC_READDIR],
210 		    nfsstats.rpccnt[NFSPROC_READDIRPLUS],
211 		    nfsstats.rpccnt[NFSPROC_ACCESS]);
212 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
213 		    "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
214 		    "GLease", "Vacate", "Evict");
215 		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
216 		    nfsstats.rpccnt[NFSPROC_MKNOD],
217 		    nfsstats.rpccnt[NFSPROC_FSSTAT],
218 		    nfsstats.rpccnt[NFSPROC_FSINFO],
219 		    nfsstats.rpccnt[NFSPROC_PATHCONF],
220 		    nfsstats.rpccnt[NFSPROC_COMMIT],
221 		    nfsstats.rpccnt[NQNFSPROC_GETLEASE],
222 		    nfsstats.rpccnt[NQNFSPROC_VACATED],
223 		    nfsstats.rpccnt[NQNFSPROC_EVICTED]);
224 		printf("Rpc Info:\n");
225 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
226 		    "TimedOut", "Invalid", "X Replies", "Retries", "Requests");
227 		printf("%9d %9d %9d %9d %9d\n",
228 		    nfsstats.rpctimeouts,
229 		    nfsstats.rpcinvalid,
230 		    nfsstats.rpcunexpected,
231 		    nfsstats.rpcretries,
232 		    nfsstats.rpcrequests);
233 		printf("Cache Info:\n");
234 		printf("%9.9s %9.9s %9.9s %9.9s",
235 		    "Attr Hits", "Misses", "Lkup Hits", "Misses");
236 		printf(" %9.9s %9.9s %9.9s %9.9s\n",
237 		    "BioR Hits", "Misses", "BioW Hits", "Misses");
238 		printf("%9d %9d %9d %9d",
239 		    nfsstats.attrcache_hits, nfsstats.attrcache_misses,
240 		    nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
241 		printf(" %9d %9d %9d %9d\n",
242 		    nfsstats.biocache_reads-nfsstats.read_bios,
243 		    nfsstats.read_bios,
244 		    nfsstats.biocache_writes-nfsstats.write_bios,
245 		    nfsstats.write_bios);
246 		printf("%9.9s %9.9s %9.9s %9.9s",
247 		    "BioRLHits", "Misses", "BioD Hits", "Misses");
248 		printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
249 		printf("%9d %9d %9d %9d",
250 		    nfsstats.biocache_readlinks-nfsstats.readlink_bios,
251 		    nfsstats.readlink_bios,
252 		    nfsstats.biocache_readdirs-nfsstats.readdir_bios,
253 		    nfsstats.readdir_bios);
254 		printf(" %9d %9d\n",
255 		    nfsstats.direofcache_hits, nfsstats.direofcache_misses);
256 	}
257 	if (printall || (clientinfo && serverinfo)){
258 		printf("\n");
259 	}
260 	if (printall || serverinfo){
261 		printf("Server Info:\n");
262 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
263 		    "Getattr", "Setattr", "Lookup", "Readlink", "Read",
264 		    "Write", "Create", "Remove");
265 		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
266 		    nfsstats.srvrpccnt[NFSPROC_GETATTR],
267 		    nfsstats.srvrpccnt[NFSPROC_SETATTR],
268 		    nfsstats.srvrpccnt[NFSPROC_LOOKUP],
269 		    nfsstats.srvrpccnt[NFSPROC_READLINK],
270 		    nfsstats.srvrpccnt[NFSPROC_READ],
271 		    nfsstats.srvrpccnt[NFSPROC_WRITE],
272 		    nfsstats.srvrpccnt[NFSPROC_CREATE],
273 		    nfsstats.srvrpccnt[NFSPROC_REMOVE]);
274 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
275 		    "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
276 		    "Readdir", "RdirPlus", "Access");
277 		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
278 		    nfsstats.srvrpccnt[NFSPROC_RENAME],
279 		    nfsstats.srvrpccnt[NFSPROC_LINK],
280 		    nfsstats.srvrpccnt[NFSPROC_SYMLINK],
281 		    nfsstats.srvrpccnt[NFSPROC_MKDIR],
282 		    nfsstats.srvrpccnt[NFSPROC_RMDIR],
283 		    nfsstats.srvrpccnt[NFSPROC_READDIR],
284 		    nfsstats.srvrpccnt[NFSPROC_READDIRPLUS],
285 		    nfsstats.srvrpccnt[NFSPROC_ACCESS]);
286 		printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
287 		    "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
288 		    "GLease", "Vacate", "Evict");
289 		printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
290 		    nfsstats.srvrpccnt[NFSPROC_MKNOD],
291 		    nfsstats.srvrpccnt[NFSPROC_FSSTAT],
292 		    nfsstats.srvrpccnt[NFSPROC_FSINFO],
293 		    nfsstats.srvrpccnt[NFSPROC_PATHCONF],
294 		    nfsstats.srvrpccnt[NFSPROC_COMMIT],
295 		    nfsstats.srvrpccnt[NQNFSPROC_GETLEASE],
296 		    nfsstats.srvrpccnt[NQNFSPROC_VACATED],
297 		    nfsstats.srvrpccnt[NQNFSPROC_EVICTED]);
298 		printf("Server Ret-Failed\n");
299 		printf("%17d\n", nfsstats.srvrpc_errs);
300 		printf("Server Faults\n");
301 		printf("%13d\n", nfsstats.srv_errs);
302 		printf("Server Cache Stats:\n");
303 		printf("%9.9s %9.9s %9.9s %9.9s\n",
304 		    "Inprog", "Idem", "Non-idem", "Misses");
305 		printf("%9d %9d %9d %9d\n",
306 		    nfsstats.srvcache_inproghits,
307 		    nfsstats.srvcache_idemdonehits,
308 		    nfsstats.srvcache_nonidemdonehits,
309 		    nfsstats.srvcache_misses);
310 		printf("Server Lease Stats:\n");
311 		printf("%9.9s %9.9s %9.9s\n",
312 		    "Leases", "PeakL", "GLeases");
313 		printf("%9d %9d %9d\n",
314 		    nfsstats.srvnqnfs_leases,
315 		    nfsstats.srvnqnfs_maxleases,
316 		    nfsstats.srvnqnfs_getleases);
317 		printf("Server Write Gathering:\n");
318 		printf("%9.9s %9.9s %9.9s\n",
319 		    "WriteOps", "WriteRPC", "Opsaved");
320 		printf("%9d %9d %9d\n",
321 		    nfsstats.srvvop_writes,
322 		    nfsstats.srvrpccnt[NFSPROC_WRITE],
323 		    nfsstats.srvrpccnt[NFSPROC_WRITE] - nfsstats.srvvop_writes);
324 	}
325 }
326 
327 u_char	signalled;			/* set if alarm goes off "early" */
328 
329 /*
330  * Print a running summary of nfs statistics.
331  * Repeat display every interval seconds, showing statistics
332  * collected over that interval.  Assumes that interval is non-zero.
333  * First line printed at top of screen is always cumulative.
334  */
335 void
336 sidewaysintpr(interval, off)
337 	u_int interval;
338 	u_long off;
339 {
340 	struct nfsstats nfsstats, lastst;
341 	int hdrcnt, oldmask;
342 
343 	(void)signal(SIGALRM, catchalarm);
344 	signalled = 0;
345 	(void)alarm(interval);
346 	memset((caddr_t)&lastst, 0, sizeof(lastst));
347 
348 	for (hdrcnt = 1;;) {
349 		if (!--hdrcnt) {
350 			printhdr();
351 			hdrcnt = 20;
352 		}
353 		if (kvm_read(kd, off, (char *)&nfsstats, sizeof(nfsstats))
354 			!= sizeof(nfsstats))
355 			errx(1, "kvm_read failed");
356 		if (printall || clientinfo)
357 			printf("Client: %8d %8d %8d %8d %8d %8d %8d %8d\n",
358 			    nfsstats.rpccnt[NFSPROC_GETATTR] -
359 			    lastst.rpccnt[NFSPROC_GETATTR],
360 			    nfsstats.rpccnt[NFSPROC_LOOKUP] -
361 			    lastst.rpccnt[NFSPROC_LOOKUP],
362 			    nfsstats.rpccnt[NFSPROC_READLINK] -
363 			    lastst.rpccnt[NFSPROC_READLINK],
364 			    nfsstats.rpccnt[NFSPROC_READ] -
365 			    lastst.rpccnt[NFSPROC_READ],
366 			    nfsstats.rpccnt[NFSPROC_WRITE] -
367 			    lastst.rpccnt[NFSPROC_WRITE],
368 			    nfsstats.rpccnt[NFSPROC_RENAME] -
369 			    lastst.rpccnt[NFSPROC_RENAME],
370 			    nfsstats.rpccnt[NFSPROC_ACCESS] -
371 			    lastst.rpccnt[NFSPROC_ACCESS],
372 			    (nfsstats.rpccnt[NFSPROC_READDIR] -
373 			    lastst.rpccnt[NFSPROC_READDIR]) +
374 			    (nfsstats.rpccnt[NFSPROC_READDIRPLUS] -
375 			    lastst.rpccnt[NFSPROC_READDIRPLUS]));
376 		if (printall || serverinfo)
377 			printf("Server: %8d %8d %8d %8d %8d %8d %8d %8d\n",
378 			    nfsstats.srvrpccnt[NFSPROC_GETATTR] -
379 			    lastst.srvrpccnt[NFSPROC_GETATTR],
380 			    nfsstats.srvrpccnt[NFSPROC_LOOKUP] -
381 			    lastst.srvrpccnt[NFSPROC_LOOKUP],
382 			    nfsstats.srvrpccnt[NFSPROC_READLINK] -
383 			    lastst.srvrpccnt[NFSPROC_READLINK],
384 			    nfsstats.srvrpccnt[NFSPROC_READ] -
385 			    lastst.srvrpccnt[NFSPROC_READ],
386 			    nfsstats.srvrpccnt[NFSPROC_WRITE] -
387 			    lastst.srvrpccnt[NFSPROC_WRITE],
388 			    nfsstats.srvrpccnt[NFSPROC_RENAME] -
389 			    lastst.srvrpccnt[NFSPROC_RENAME],
390 			    nfsstats.srvrpccnt[NFSPROC_ACCESS] -
391 			    lastst.srvrpccnt[NFSPROC_ACCESS],
392 			    (nfsstats.srvrpccnt[NFSPROC_READDIR] -
393 			    lastst.srvrpccnt[NFSPROC_READDIR]) +
394 			    (nfsstats.srvrpccnt[NFSPROC_READDIRPLUS] -
395 			    lastst.srvrpccnt[NFSPROC_READDIRPLUS]));
396 		lastst = nfsstats;
397 		fflush(stdout);
398 		oldmask = sigblock(sigmask(SIGALRM));
399 		if (!signalled)
400 			sigpause(0);
401 		sigsetmask(oldmask);
402 		signalled = 0;
403 		(void)alarm(interval);
404 	}
405 	/*NOTREACHED*/
406 }
407 
408 void
409 printhdr()
410 {
411 
412 	printf("        %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s\n",
413 	    "Getattr", "Lookup", "Readlink", "Read", "Write", "Rename",
414 	    "Access", "Readdir");
415 	fflush(stdout);
416 }
417 
418 /*
419  * Called if an interval expires before sidewaysintpr has completed a loop.
420  * Sets a flag to not wait for the alarm.
421  */
422 void
423 catchalarm(dummy)
424 	int dummy;
425 {
426 
427 	signalled = 1;
428 }
429 
430 void
431 usage()
432 {
433 
434 	(void)fprintf(stderr,
435 		  "usage: nfsstat [-cs] [-M core] [-N system] [-w interval]\n");
436 	exit(1);
437 }
438