xref: /freebsd/usr.bin/nfsstat/nfsstat.c (revision afe61c15161c324a7af299a9b8457aba5afc92db)
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 static char sccsid[] = "@(#)nfsstat.c	8.1 (Berkeley) 6/6/93";
45 #endif /* not lint */
46 
47 #include <sys/param.h>
48 #if BSD >= 199103
49 #define NEWVM
50 #endif
51 #ifndef NEWVM
52 #include <sys/vmmac.h>
53 #include <sys/ucred.h>
54 #include <machine/pte.h>
55 #endif
56 #include <sys/mount.h>
57 #include <nfs/nfsv2.h>
58 #include <nfs/nfs.h>
59 #include <signal.h>
60 #include <fcntl.h>
61 #include <ctype.h>
62 #include <errno.h>
63 #include <kvm.h>
64 #include <nlist.h>
65 #include <unistd.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <paths.h>
70 
71 struct nlist nl[] = {
72 #define	N_NFSSTAT	0
73 	{ "_nfsstats" },
74 	"",
75 };
76 kvm_t *kd;
77 
78 void intpr(), printhdr(), sidewaysintpr(), usage();
79 
80 main(argc, argv)
81 	int argc;
82 	char **argv;
83 {
84 	extern int optind;
85 	extern char *optarg;
86 	u_int interval;
87 	int ch;
88 	char *memf, *nlistf;
89 	char errbuf[80];
90 
91 	interval = 0;
92 	memf = nlistf = NULL;
93 	while ((ch = getopt(argc, argv, "M:N:w:")) != EOF)
94 		switch(ch) {
95 		case 'M':
96 			memf = optarg;
97 			break;
98 		case 'N':
99 			nlistf = optarg;
100 			break;
101 		case 'w':
102 			interval = atoi(optarg);
103 			break;
104 		case '?':
105 		default:
106 			usage();
107 		}
108 	argc -= optind;
109 	argv += optind;
110 
111 #define	BACKWARD_COMPATIBILITY
112 #ifdef	BACKWARD_COMPATIBILITY
113 	if (*argv) {
114 		interval = atoi(*argv);
115 		if (*++argv) {
116 			nlistf = *argv;
117 			if (*++argv)
118 				memf = *argv;
119 		}
120 	}
121 #endif
122 	/*
123 	 * Discard setgid privileges if not the running kernel so that bad
124 	 * guys can't print interesting stuff from kernel memory.
125 	 */
126 	if (nlistf != NULL || memf != NULL)
127 		setgid(getgid());
128 
129 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == 0) {
130 		fprintf(stderr, "nfsstat: kvm_openfiles: %s\n", errbuf);
131 		exit(1);
132 	}
133 	if (kvm_nlist(kd, nl) != 0) {
134 		fprintf(stderr, "nfsstat: kvm_nlist: can't get names\n");
135 		exit(1);
136 	}
137 
138 	if (interval)
139 		sidewaysintpr(interval, nl[N_NFSSTAT].n_value);
140 	else
141 		intpr(nl[N_NFSSTAT].n_value);
142 	exit(0);
143 }
144 
145 /*
146  * Print a description of the nfs stats.
147  */
148 void
149 intpr(nfsstataddr)
150 	u_long nfsstataddr;
151 {
152 	struct nfsstats nfsstats;
153 
154 	if (kvm_read(kd, (u_long)nfsstataddr, (char *)&nfsstats, sizeof(struct nfsstats)) < 0) {
155 		fprintf(stderr, "nfsstat: kvm_read failed\n");
156 		exit(1);
157 	}
158 	printf("Client Info:\n");
159 	printf("Rpc Counts:\n");
160 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
161 		"Getattr", "Setattr", "Lookup", "Readlink", "Read",
162 		"Write", "Create", "Remove");
163 	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
164 		nfsstats.rpccnt[NFSPROC_GETATTR],
165 		nfsstats.rpccnt[NFSPROC_SETATTR],
166 		nfsstats.rpccnt[NFSPROC_LOOKUP],
167 		nfsstats.rpccnt[NFSPROC_READLINK],
168 		nfsstats.rpccnt[NFSPROC_READ],
169 		nfsstats.rpccnt[NFSPROC_WRITE],
170 		nfsstats.rpccnt[NFSPROC_CREATE],
171 		nfsstats.rpccnt[NFSPROC_REMOVE]);
172 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
173 		"Rename", "Link", "Symlink", "Mkdir", "Rmdir",
174 		"Readdir", "Statfs", "RdirLook");
175 	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
176 		nfsstats.rpccnt[NFSPROC_RENAME],
177 		nfsstats.rpccnt[NFSPROC_LINK],
178 		nfsstats.rpccnt[NFSPROC_SYMLINK],
179 		nfsstats.rpccnt[NFSPROC_MKDIR],
180 		nfsstats.rpccnt[NFSPROC_RMDIR],
181 		nfsstats.rpccnt[NFSPROC_READDIR],
182 		nfsstats.rpccnt[NFSPROC_STATFS],
183 		nfsstats.rpccnt[NQNFSPROC_READDIRLOOK]);
184 	printf("%9.9s %9.9s %9.9s\n",
185 		"GLease", "Vacate", "Evict");
186 	printf("%9d %9d %9d\n",
187 		nfsstats.rpccnt[NQNFSPROC_GETLEASE],
188 		nfsstats.rpccnt[NQNFSPROC_VACATED],
189 		nfsstats.rpccnt[NQNFSPROC_EVICTED]);
190 	printf("Rpc Info:\n");
191 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
192 		"TimedOut", "Invalid", "X Replies", "Retries", "Requests");
193 	printf("%9d %9d %9d %9d %9d\n",
194 		nfsstats.rpctimeouts,
195 		nfsstats.rpcinvalid,
196 		nfsstats.rpcunexpected,
197 		nfsstats.rpcretries,
198 		nfsstats.rpcrequests);
199 	printf("Cache Info:\n");
200 	printf("%9.9s %9.9s %9.9s %9.9s",
201 		"Attr Hits", "Misses", "Lkup Hits", "Misses");
202 	printf(" %9.9s %9.9s %9.9s %9.9s\n",
203 		"BioR Hits", "Misses", "BioW Hits", "Misses");
204 	printf("%9d %9d %9d %9d",
205 		nfsstats.attrcache_hits, nfsstats.attrcache_misses,
206 		nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
207 	printf(" %9d %9d %9d %9d\n",
208 		nfsstats.biocache_reads-nfsstats.read_bios,
209 		nfsstats.read_bios,
210 		nfsstats.biocache_writes-nfsstats.write_bios,
211 		nfsstats.write_bios);
212 	printf("%9.9s %9.9s %9.9s %9.9s",
213 		"BioRLHits", "Misses", "BioD Hits", "Misses");
214 	printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
215 	printf("%9d %9d %9d %9d",
216 		nfsstats.biocache_readlinks-nfsstats.readlink_bios,
217 		nfsstats.readlink_bios,
218 		nfsstats.biocache_readdirs-nfsstats.readdir_bios,
219 		nfsstats.readdir_bios);
220 	printf(" %9d %9d\n",
221 		nfsstats.direofcache_hits, nfsstats.direofcache_misses);
222 	printf("\nServer Info:\n");
223 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
224 		"Getattr", "Setattr", "Lookup", "Readlink", "Read",
225 		"Write", "Create", "Remove");
226 	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
227 		nfsstats.srvrpccnt[NFSPROC_GETATTR],
228 		nfsstats.srvrpccnt[NFSPROC_SETATTR],
229 		nfsstats.srvrpccnt[NFSPROC_LOOKUP],
230 		nfsstats.srvrpccnt[NFSPROC_READLINK],
231 		nfsstats.srvrpccnt[NFSPROC_READ],
232 		nfsstats.srvrpccnt[NFSPROC_WRITE],
233 		nfsstats.srvrpccnt[NFSPROC_CREATE],
234 		nfsstats.srvrpccnt[NFSPROC_REMOVE]);
235 	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
236 		"Rename", "Link", "Symlink", "Mkdir", "Rmdir",
237 		"Readdir", "Statfs", "RdirLook");
238 	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
239 		nfsstats.srvrpccnt[NFSPROC_RENAME],
240 		nfsstats.srvrpccnt[NFSPROC_LINK],
241 		nfsstats.srvrpccnt[NFSPROC_SYMLINK],
242 		nfsstats.srvrpccnt[NFSPROC_MKDIR],
243 		nfsstats.srvrpccnt[NFSPROC_RMDIR],
244 		nfsstats.srvrpccnt[NFSPROC_READDIR],
245 		nfsstats.srvrpccnt[NFSPROC_STATFS],
246 		nfsstats.srvrpccnt[NQNFSPROC_READDIRLOOK]);
247 	printf("%9.9s %9.9s %9.9s\n",
248 		"GLease", "Vacate", "Evict");
249 	printf("%9d %9d %9d\n",
250 		nfsstats.srvrpccnt[NQNFSPROC_GETLEASE],
251 		nfsstats.srvrpccnt[NQNFSPROC_VACATED],
252 		nfsstats.srvrpccnt[NQNFSPROC_EVICTED]);
253 	printf("Server Ret-Failed\n");
254 	printf("%17d\n", nfsstats.srvrpc_errs);
255 	printf("Server Faults\n");
256 	printf("%13d\n", nfsstats.srv_errs);
257 	printf("Server Cache Stats:\n");
258 	printf("%9.9s %9.9s %9.9s %9.9s\n",
259 		"Inprog", "Idem", "Non-idem", "Misses");
260 	printf("%9d %9d %9d %9d\n",
261 		nfsstats.srvcache_inproghits,
262 		nfsstats.srvcache_idemdonehits,
263 		nfsstats.srvcache_nonidemdonehits,
264 		nfsstats.srvcache_misses);
265 	printf("Server Lease Stats:\n");
266 	printf("%9.9s %9.9s %9.9s\n",
267 		"Leases", "PeakL", "GLeases");
268 	printf("%9d %9d %9d\n",
269 		nfsstats.srvnqnfs_leases,
270 		nfsstats.srvnqnfs_maxleases,
271 		nfsstats.srvnqnfs_getleases);
272 }
273 
274 u_char	signalled;			/* set if alarm goes off "early" */
275 
276 /*
277  * Print a running summary of nfs statistics.
278  * Repeat display every interval seconds, showing statistics
279  * collected over that interval.  Assumes that interval is non-zero.
280  * First line printed at top of screen is always cumulative.
281  */
282 void
283 sidewaysintpr(interval, off)
284 	u_int interval;
285 	u_long off;
286 {
287 	struct nfsstats nfsstats, lastst;
288 	int hdrcnt, oldmask;
289 	void catchalarm();
290 
291 	(void)signal(SIGALRM, catchalarm);
292 	signalled = 0;
293 	(void)alarm(interval);
294 	bzero((caddr_t)&lastst, sizeof(lastst));
295 
296 	for (hdrcnt = 1;;) {
297 		if (!--hdrcnt) {
298 			printhdr();
299 			hdrcnt = 20;
300 		}
301 		if (kvm_read(kd, off, (char *)&nfsstats, sizeof nfsstats) < 0) {
302 			fprintf(stderr, "nfsstat: kvm_read failed\n");
303 			exit(1);
304 		}
305 		printf("Client: %8d %8d %8d %8d %8d %8d %8d %8d\n",
306 		    nfsstats.rpccnt[1]-lastst.rpccnt[1],
307 		    nfsstats.rpccnt[4]-lastst.rpccnt[4],
308 		    nfsstats.rpccnt[5]-lastst.rpccnt[5],
309 		    nfsstats.rpccnt[6]-lastst.rpccnt[6],
310 		    nfsstats.rpccnt[8]-lastst.rpccnt[8],
311 		    nfsstats.rpccnt[11]-lastst.rpccnt[11],
312 		    nfsstats.rpccnt[12]-lastst.rpccnt[12],
313 		    nfsstats.rpccnt[16]-lastst.rpccnt[16]);
314 		printf("Server: %8d %8d %8d %8d %8d %8d %8d %8d\n",
315 		    nfsstats.srvrpccnt[1]-lastst.srvrpccnt[1],
316 		    nfsstats.srvrpccnt[4]-lastst.srvrpccnt[4],
317 		    nfsstats.srvrpccnt[5]-lastst.srvrpccnt[5],
318 		    nfsstats.srvrpccnt[6]-lastst.srvrpccnt[6],
319 		    nfsstats.srvrpccnt[8]-lastst.srvrpccnt[8],
320 		    nfsstats.srvrpccnt[11]-lastst.srvrpccnt[11],
321 		    nfsstats.srvrpccnt[12]-lastst.srvrpccnt[12],
322 		    nfsstats.srvrpccnt[16]-lastst.srvrpccnt[16]);
323 		lastst = nfsstats;
324 		fflush(stdout);
325 		oldmask = sigblock(sigmask(SIGALRM));
326 		if (!signalled)
327 			sigpause(0);
328 		sigsetmask(oldmask);
329 		signalled = 0;
330 		(void)alarm(interval);
331 	}
332 	/*NOTREACHED*/
333 }
334 
335 void
336 printhdr()
337 {
338 	printf("        %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s\n",
339 	    "Getattr", "Lookup", "Readlink", "Read", "Write", "Rename",
340 	    "Link", "Readdir");
341 	fflush(stdout);
342 }
343 
344 /*
345  * Called if an interval expires before sidewaysintpr has completed a loop.
346  * Sets a flag to not wait for the alarm.
347  */
348 void
349 catchalarm()
350 {
351 	signalled = 1;
352 }
353 
354 void
355 usage()
356 {
357 	(void)fprintf(stderr,
358 	    "usage: nfsstat [-M core] [-N system] [-w interval]\n");
359 	exit(1);
360 }
361