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