nfsstat.c revision 24360
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/*static char sccsid[] = "From: @(#)nfsstat.c	8.1 (Berkeley) 6/6/93";*/
45static const char rcsid[] =
46	"$Id: nfsstat.c,v 1.8 1997/02/22 19:56:25 peter Exp $";
47#endif /* not lint */
48
49#include <sys/param.h>
50#include <sys/mount.h>
51#include <sys/time.h>
52#include <sys/sysctl.h>
53#include <nfs/rpcv2.h>
54#include <nfs/nfsproto.h>
55#include <nfs/nfs.h>
56#include <signal.h>
57#include <fcntl.h>
58#include <ctype.h>
59#include <errno.h>
60#include <kvm.h>
61#include <nlist.h>
62#include <unistd.h>
63#include <stdio.h>
64#include <stdlib.h>
65#include <string.h>
66#include <paths.h>
67#include <err.h>
68
69struct nlist nl[] = {
70#define	N_NFSSTAT	0
71	{ "_nfsstats" },
72	"",
73};
74kvm_t *kd;
75
76static int deadkernel = 0;
77
78void intpr __P((void));
79void printhdr __P((void));
80void sidewaysintpr __P((u_int));
81void usage __P((void));
82
83main(argc, argv)
84	int argc;
85	char **argv;
86{
87	extern int optind;
88	extern char *optarg;
89	u_int interval;
90	int ch;
91	char *memf, *nlistf;
92	char errbuf[80];
93
94	interval = 0;
95	memf = nlistf = NULL;
96	while ((ch = getopt(argc, argv, "M:N:w:")) != -1)
97		switch(ch) {
98		case 'M':
99			memf = optarg;
100			break;
101		case 'N':
102			nlistf = optarg;
103			break;
104		case 'w':
105			interval = atoi(optarg);
106			break;
107		case '?':
108		default:
109			usage();
110		}
111	argc -= optind;
112	argv += optind;
113
114#define	BACKWARD_COMPATIBILITY
115#ifdef	BACKWARD_COMPATIBILITY
116	if (*argv) {
117		interval = atoi(*argv);
118		if (*++argv) {
119			nlistf = *argv;
120			if (*++argv)
121				memf = *argv;
122		}
123	}
124#endif
125	/*
126	 * Discard setgid privileges if not the running kernel so that bad
127	 * guys can't print interesting stuff from kernel memory.
128	 */
129	if (nlistf != NULL || memf != NULL) {
130		setgid(getgid());
131		deadkernel = 1;
132
133		if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
134					errbuf)) == 0) {
135			errx(1, "kvm_openfiles: %s", errbuf);
136		}
137		if (kvm_nlist(kd, nl) != 0) {
138			errx(1, "kvm_nlist: can't get names");
139		}
140	}
141
142	if (interval)
143		sidewaysintpr(interval);
144	else
145		intpr();
146	exit(0);
147}
148
149/*
150 * Read the nfs stats using sysctl(3) for live kernels, or kvm_read
151 * for dead ones.
152 */
153void
154readstats(stp)
155	struct nfsstats *stp;
156{
157	if(deadkernel) {
158		if(kvm_read(kd, (u_long)nl[N_NFSSTAT].n_value, stp,
159			    sizeof *stp) < 0) {
160			err(1, "kvm_read");
161		}
162	} else {
163		int name[3];
164		size_t buflen = sizeof *stp;
165
166		name[0] = CTL_VFS;
167		name[1] = MOUNT_NFS;
168		name[2] = NFS_NFSSTATS;
169
170		if(sysctl(name, 3, stp, &buflen, (void *)0, (size_t)0) < 0) {
171			err(1, "sysctl");
172		}
173	}
174}
175
176/*
177 * Print a description of the nfs stats.
178 */
179void
180intpr()
181{
182	struct nfsstats nfsstats;
183
184	readstats(&nfsstats);
185
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	printf("\nServer Info:\n");
257	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
258		"Getattr", "Setattr", "Lookup", "Readlink", "Read",
259		"Write", "Create", "Remove");
260	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
261		nfsstats.srvrpccnt[NFSPROC_GETATTR],
262		nfsstats.srvrpccnt[NFSPROC_SETATTR],
263		nfsstats.srvrpccnt[NFSPROC_LOOKUP],
264		nfsstats.srvrpccnt[NFSPROC_READLINK],
265		nfsstats.srvrpccnt[NFSPROC_READ],
266		nfsstats.srvrpccnt[NFSPROC_WRITE],
267		nfsstats.srvrpccnt[NFSPROC_CREATE],
268		nfsstats.srvrpccnt[NFSPROC_REMOVE]);
269	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
270		"Rename", "Link", "Symlink", "Mkdir", "Rmdir",
271		"Readdir", "RdirPlus", "Access");
272	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
273		nfsstats.srvrpccnt[NFSPROC_RENAME],
274		nfsstats.srvrpccnt[NFSPROC_LINK],
275		nfsstats.srvrpccnt[NFSPROC_SYMLINK],
276		nfsstats.srvrpccnt[NFSPROC_MKDIR],
277		nfsstats.srvrpccnt[NFSPROC_RMDIR],
278		nfsstats.srvrpccnt[NFSPROC_READDIR],
279		nfsstats.srvrpccnt[NFSPROC_READDIRPLUS],
280		nfsstats.srvrpccnt[NFSPROC_ACCESS]);
281	printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
282		"Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
283		"GLease", "Vacate", "Evict");
284	printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
285		nfsstats.srvrpccnt[NFSPROC_MKNOD],
286		nfsstats.srvrpccnt[NFSPROC_FSSTAT],
287		nfsstats.srvrpccnt[NFSPROC_FSINFO],
288		nfsstats.srvrpccnt[NFSPROC_PATHCONF],
289		nfsstats.srvrpccnt[NFSPROC_COMMIT],
290		nfsstats.srvrpccnt[NQNFSPROC_GETLEASE],
291		nfsstats.srvrpccnt[NQNFSPROC_VACATED],
292		nfsstats.srvrpccnt[NQNFSPROC_EVICTED]);
293	printf("Server Ret-Failed\n");
294	printf("%17d\n", nfsstats.srvrpc_errs);
295	printf("Server Faults\n");
296	printf("%13d\n", nfsstats.srv_errs);
297	printf("Server Cache Stats:\n");
298	printf("%9.9s %9.9s %9.9s %9.9s\n",
299		"Inprog", "Idem", "Non-idem", "Misses");
300	printf("%9d %9d %9d %9d\n",
301		nfsstats.srvcache_inproghits,
302		nfsstats.srvcache_idemdonehits,
303		nfsstats.srvcache_nonidemdonehits,
304		nfsstats.srvcache_misses);
305	printf("Server Lease Stats:\n");
306	printf("%9.9s %9.9s %9.9s\n",
307		"Leases", "PeakL", "GLeases");
308	printf("%9d %9d %9d\n",
309		nfsstats.srvnqnfs_leases,
310		nfsstats.srvnqnfs_maxleases,
311		nfsstats.srvnqnfs_getleases);
312	printf("Server Write Gathering:\n");
313	printf("%9.9s %9.9s %9.9s\n",
314		"WriteOps", "WriteRPC", "Opsaved");
315	printf("%9d %9d %9d\n",
316		nfsstats.srvvop_writes,
317		nfsstats.srvrpccnt[NFSPROC_WRITE],
318		nfsstats.srvrpccnt[NFSPROC_WRITE] - nfsstats.srvvop_writes);
319}
320
321u_char	signalled;			/* set if alarm goes off "early" */
322
323/*
324 * Print a running summary of nfs statistics.
325 * Repeat display every interval seconds, showing statistics
326 * collected over that interval.  Assumes that interval is non-zero.
327 * First line printed at top of screen is always cumulative.
328 */
329void
330sidewaysintpr(interval)
331	u_int interval;
332{
333	struct nfsstats nfsstats, lastst;
334	int hdrcnt, oldmask;
335	void catchalarm();
336
337	(void)signal(SIGALRM, catchalarm);
338	signalled = 0;
339	(void)alarm(interval);
340	bzero((caddr_t)&lastst, sizeof(lastst));
341
342	for (hdrcnt = 1;;) {
343		if (!--hdrcnt) {
344			printhdr();
345			hdrcnt = 20;
346		}
347		readstats(&nfsstats);
348		printf("Client: %8d %8d %8d %8d %8d %8d %8d %8d\n",
349		    nfsstats.rpccnt[NFSPROC_GETATTR]-lastst.rpccnt[NFSPROC_GETATTR],
350		    nfsstats.rpccnt[NFSPROC_LOOKUP]-lastst.rpccnt[NFSPROC_LOOKUP],
351		    nfsstats.rpccnt[NFSPROC_READLINK]-lastst.rpccnt[NFSPROC_READLINK],
352		    nfsstats.rpccnt[NFSPROC_READ]-lastst.rpccnt[NFSPROC_READ],
353		    nfsstats.rpccnt[NFSPROC_WRITE]-lastst.rpccnt[NFSPROC_WRITE],
354		    nfsstats.rpccnt[NFSPROC_RENAME]-lastst.rpccnt[NFSPROC_RENAME],
355		    nfsstats.rpccnt[NFSPROC_ACCESS]-lastst.rpccnt[NFSPROC_ACCESS],
356		    (nfsstats.rpccnt[NFSPROC_READDIR]-lastst.rpccnt[NFSPROC_READDIR])
357		    +(nfsstats.rpccnt[NFSPROC_READDIRPLUS]-lastst.rpccnt[NFSPROC_READDIRPLUS]));
358		printf("Server: %8d %8d %8d %8d %8d %8d %8d %8d\n",
359		    nfsstats.srvrpccnt[NFSPROC_GETATTR]-lastst.srvrpccnt[NFSPROC_GETATTR],
360		    nfsstats.srvrpccnt[NFSPROC_LOOKUP]-lastst.srvrpccnt[NFSPROC_LOOKUP],
361		    nfsstats.srvrpccnt[NFSPROC_READLINK]-lastst.srvrpccnt[NFSPROC_READLINK],
362		    nfsstats.srvrpccnt[NFSPROC_READ]-lastst.srvrpccnt[NFSPROC_READ],
363		    nfsstats.srvrpccnt[NFSPROC_WRITE]-lastst.srvrpccnt[NFSPROC_WRITE],
364		    nfsstats.srvrpccnt[NFSPROC_RENAME]-lastst.srvrpccnt[NFSPROC_RENAME],
365		    nfsstats.srvrpccnt[NFSPROC_ACCESS]-lastst.srvrpccnt[NFSPROC_ACCESS],
366		    (nfsstats.srvrpccnt[NFSPROC_READDIR]-lastst.srvrpccnt[NFSPROC_READDIR])
367		    +(nfsstats.srvrpccnt[NFSPROC_READDIRPLUS]-lastst.srvrpccnt[NFSPROC_READDIRPLUS]));
368		lastst = nfsstats;
369		fflush(stdout);
370		oldmask = sigblock(sigmask(SIGALRM));
371		if (!signalled)
372			sigpause(0);
373		sigsetmask(oldmask);
374		signalled = 0;
375		(void)alarm(interval);
376	}
377	/*NOTREACHED*/
378}
379
380void
381printhdr()
382{
383	printf("        %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s\n",
384	    "Getattr", "Lookup", "Readlink", "Read", "Write", "Rename",
385	    "Access", "Readdir");
386	fflush(stdout);
387}
388
389/*
390 * Called if an interval expires before sidewaysintpr has completed a loop.
391 * Sets a flag to not wait for the alarm.
392 */
393void
394catchalarm()
395{
396	signalled = 1;
397}
398
399void
400usage()
401{
402	(void)fprintf(stderr,
403	    "usage: nfsstat [-M core] [-N system] [-w interval]\n");
404	exit(1);
405}
406