Annotation of src/usr.bin/nfsstat/nfsstat.c, Revision 1.6
1.6 ! millert 1: /* $OpenBSD: nfsstat.c,v 1.5 1996/12/22 03:25:59 tholo Exp $ */
1.2 niklas 2: /* $NetBSD: nfsstat.c,v 1.7 1996/03/03 17:21:30 thorpej Exp $ */
3:
1.1 deraadt 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
1.2 niklas 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
1.6 ! millert 51: static char *rcsid = "$OpenBSD: nfsstat.c,v 1.5 1996/12/22 03:25:59 tholo Exp $";
1.2 niklas 52: #endif
1.1 deraadt 53: #endif /* not lint */
54:
55: #include <sys/param.h>
56: #include <sys/mount.h>
1.2 niklas 57: #include <sys/sysctl.h>
58: #include <nfs/rpcv2.h>
59: #include <nfs/nfsproto.h>
1.1 deraadt 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>
1.3 deraadt 71: #include <limits.h>
1.1 deraadt 72: #include <paths.h>
1.2 niklas 73: #include <err.h>
1.1 deraadt 74:
1.4 kstailey 75: #define SHOW_SERVER 0x01
76: #define SHOW_CLIENT 0x02
77: #define SHOW_ALL (SHOW_SERVER | SHOW_CLIENT)
78:
1.1 deraadt 79: struct nlist nl[] = {
80: #define N_NFSSTAT 0
81: { "_nfsstats" },
82: "",
83: };
84: kvm_t *kd;
85:
86: void intpr(), printhdr(), sidewaysintpr(), usage();
87:
88: main(argc, argv)
89: int argc;
90: char **argv;
91: {
92: extern int optind;
93: extern char *optarg;
94: u_int interval;
1.4 kstailey 95: u_int display = SHOW_ALL;
1.1 deraadt 96: int ch;
97: char *memf, *nlistf;
1.3 deraadt 98: char errbuf[_POSIX2_LINE_MAX];
1.1 deraadt 99:
100: interval = 0;
101: memf = nlistf = NULL;
1.6 ! millert 102: while ((ch = getopt(argc, argv, "M:N:w:sc")) != -1)
1.1 deraadt 103: switch(ch) {
104: case 'M':
105: memf = optarg;
106: break;
107: case 'N':
108: nlistf = optarg;
109: break;
110: case 'w':
111: interval = atoi(optarg);
112: break;
1.4 kstailey 113: case 's':
114: display = SHOW_SERVER;
115: break;
116: case 'c':
117: display = SHOW_CLIENT;
118: break;
1.1 deraadt 119: case '?':
120: default:
121: usage();
122: }
123: argc -= optind;
124: argv += optind;
125:
126: #define BACKWARD_COMPATIBILITY
127: #ifdef BACKWARD_COMPATIBILITY
128: if (*argv) {
129: interval = atoi(*argv);
130: if (*++argv) {
131: nlistf = *argv;
132: if (*++argv)
133: memf = *argv;
134: }
135: }
136: #endif
137: /*
138: * Discard setgid privileges if not the running kernel so that bad
139: * guys can't print interesting stuff from kernel memory.
140: */
1.5 tholo 141: if (nlistf != NULL || memf != NULL) {
142: setegid(getgid());
1.1 deraadt 143: setgid(getgid());
1.5 tholo 144: }
1.1 deraadt 145:
146: if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == 0) {
147: fprintf(stderr, "nfsstat: kvm_openfiles: %s\n", errbuf);
148: exit(1);
149: }
150: if (kvm_nlist(kd, nl) != 0) {
151: fprintf(stderr, "nfsstat: kvm_nlist: can't get names\n");
152: exit(1);
153: }
154:
155: if (interval)
1.4 kstailey 156: sidewaysintpr(interval, nl[N_NFSSTAT].n_value, display);
1.1 deraadt 157: else
1.4 kstailey 158: intpr(nl[N_NFSSTAT].n_value, display);
1.1 deraadt 159: exit(0);
160: }
161:
162: /*
163: * Print a description of the nfs stats.
164: */
165: void
1.4 kstailey 166: intpr(nfsstataddr, display)
1.1 deraadt 167: u_long nfsstataddr;
1.4 kstailey 168: u_int display;
1.1 deraadt 169: {
170: struct nfsstats nfsstats;
171:
172: if (kvm_read(kd, (u_long)nfsstataddr, (char *)&nfsstats, sizeof(struct nfsstats)) < 0) {
173: fprintf(stderr, "nfsstat: kvm_read failed\n");
174: exit(1);
175: }
1.4 kstailey 176: if (display & SHOW_CLIENT) {
177: printf("Client Info:\n");
178: printf("Rpc Counts:\n");
179: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
180: "Getattr", "Setattr", "Lookup", "Readlink", "Read",
181: "Write", "Create", "Remove");
182: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
183: nfsstats.rpccnt[NFSPROC_GETATTR],
184: nfsstats.rpccnt[NFSPROC_SETATTR],
185: nfsstats.rpccnt[NFSPROC_LOOKUP],
186: nfsstats.rpccnt[NFSPROC_READLINK],
187: nfsstats.rpccnt[NFSPROC_READ],
188: nfsstats.rpccnt[NFSPROC_WRITE],
189: nfsstats.rpccnt[NFSPROC_CREATE],
190: nfsstats.rpccnt[NFSPROC_REMOVE]);
191: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
192: "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
193: "Readdir", "RdirPlus", "Access");
194: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
195: nfsstats.rpccnt[NFSPROC_RENAME],
196: nfsstats.rpccnt[NFSPROC_LINK],
197: nfsstats.rpccnt[NFSPROC_SYMLINK],
198: nfsstats.rpccnt[NFSPROC_MKDIR],
199: nfsstats.rpccnt[NFSPROC_RMDIR],
200: nfsstats.rpccnt[NFSPROC_READDIR],
201: nfsstats.rpccnt[NFSPROC_READDIRPLUS],
202: nfsstats.rpccnt[NFSPROC_ACCESS]);
203: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
204: "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
205: "GLease", "Vacate", "Evict");
206: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
207: nfsstats.rpccnt[NFSPROC_MKNOD],
208: nfsstats.rpccnt[NFSPROC_FSSTAT],
209: nfsstats.rpccnt[NFSPROC_FSINFO],
210: nfsstats.rpccnt[NFSPROC_PATHCONF],
211: nfsstats.rpccnt[NFSPROC_COMMIT],
212: nfsstats.rpccnt[NQNFSPROC_GETLEASE],
213: nfsstats.rpccnt[NQNFSPROC_VACATED],
214: nfsstats.rpccnt[NQNFSPROC_EVICTED]);
215: printf("Rpc Info:\n");
216: printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
217: "TimedOut", "Invalid", "X Replies", "Retries", "Requests");
218: printf("%9d %9d %9d %9d %9d\n",
219: nfsstats.rpctimeouts,
220: nfsstats.rpcinvalid,
221: nfsstats.rpcunexpected,
222: nfsstats.rpcretries,
223: nfsstats.rpcrequests);
224: printf("Cache Info:\n");
225: printf("%9.9s %9.9s %9.9s %9.9s",
226: "Attr Hits", "Misses", "Lkup Hits", "Misses");
227: printf(" %9.9s %9.9s %9.9s %9.9s\n",
228: "BioR Hits", "Misses", "BioW Hits", "Misses");
229: printf("%9d %9d %9d %9d",
230: nfsstats.attrcache_hits, nfsstats.attrcache_misses,
231: nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
232: printf(" %9d %9d %9d %9d\n",
233: nfsstats.biocache_reads-nfsstats.read_bios,
234: nfsstats.read_bios,
235: nfsstats.biocache_writes-nfsstats.write_bios,
236: nfsstats.write_bios);
237: printf("%9.9s %9.9s %9.9s %9.9s",
238: "BioRLHits", "Misses", "BioD Hits", "Misses");
239: printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
240: printf("%9d %9d %9d %9d",
241: nfsstats.biocache_readlinks-nfsstats.readlink_bios,
242: nfsstats.readlink_bios,
243: nfsstats.biocache_readdirs-nfsstats.readdir_bios,
244: nfsstats.readdir_bios);
245: printf(" %9d %9d\n",
246: nfsstats.direofcache_hits, nfsstats.direofcache_misses);
247: }
248: if (display & SHOW_SERVER) {
249: printf("\nServer Info:\n");
250: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
251: "Getattr", "Setattr", "Lookup", "Readlink", "Read",
252: "Write", "Create", "Remove");
253: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
254: nfsstats.srvrpccnt[NFSPROC_GETATTR],
255: nfsstats.srvrpccnt[NFSPROC_SETATTR],
256: nfsstats.srvrpccnt[NFSPROC_LOOKUP],
257: nfsstats.srvrpccnt[NFSPROC_READLINK],
258: nfsstats.srvrpccnt[NFSPROC_READ],
259: nfsstats.srvrpccnt[NFSPROC_WRITE],
260: nfsstats.srvrpccnt[NFSPROC_CREATE],
261: nfsstats.srvrpccnt[NFSPROC_REMOVE]);
262: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
263: "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
264: "Readdir", "RdirPlus", "Access");
265: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
266: nfsstats.srvrpccnt[NFSPROC_RENAME],
267: nfsstats.srvrpccnt[NFSPROC_LINK],
268: nfsstats.srvrpccnt[NFSPROC_SYMLINK],
269: nfsstats.srvrpccnt[NFSPROC_MKDIR],
270: nfsstats.srvrpccnt[NFSPROC_RMDIR],
271: nfsstats.srvrpccnt[NFSPROC_READDIR],
272: nfsstats.srvrpccnt[NFSPROC_READDIRPLUS],
273: nfsstats.srvrpccnt[NFSPROC_ACCESS]);
274: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
275: "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
276: "GLease", "Vacate", "Evict");
277: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
278: nfsstats.srvrpccnt[NFSPROC_MKNOD],
279: nfsstats.srvrpccnt[NFSPROC_FSSTAT],
280: nfsstats.srvrpccnt[NFSPROC_FSINFO],
281: nfsstats.srvrpccnt[NFSPROC_PATHCONF],
282: nfsstats.srvrpccnt[NFSPROC_COMMIT],
283: nfsstats.srvrpccnt[NQNFSPROC_GETLEASE],
284: nfsstats.srvrpccnt[NQNFSPROC_VACATED],
285: nfsstats.srvrpccnt[NQNFSPROC_EVICTED]);
286: printf("Server Ret-Failed\n");
287: printf("%17d\n", nfsstats.srvrpc_errs);
288: printf("Server Faults\n");
289: printf("%13d\n", nfsstats.srv_errs);
290: printf("Server Cache Stats:\n");
291: printf("%9.9s %9.9s %9.9s %9.9s\n",
292: "Inprog", "Idem", "Non-idem", "Misses");
293: printf("%9d %9d %9d %9d\n",
294: nfsstats.srvcache_inproghits,
295: nfsstats.srvcache_idemdonehits,
296: nfsstats.srvcache_nonidemdonehits,
297: nfsstats.srvcache_misses);
298: printf("Server Lease Stats:\n");
299: printf("%9.9s %9.9s %9.9s\n",
300: "Leases", "PeakL", "GLeases");
301: printf("%9d %9d %9d\n",
302: nfsstats.srvnqnfs_leases,
303: nfsstats.srvnqnfs_maxleases,
304: nfsstats.srvnqnfs_getleases);
305: printf("Server Write Gathering:\n");
306: printf("%9.9s %9.9s %9.9s\n",
307: "WriteOps", "WriteRPC", "Opsaved");
308: printf("%9d %9d %9d\n",
309: nfsstats.srvvop_writes,
310: nfsstats.srvrpccnt[NFSPROC_WRITE],
311: nfsstats.srvrpccnt[NFSPROC_WRITE] - nfsstats.srvvop_writes);
312: }
1.1 deraadt 313: }
314:
315: u_char signalled; /* set if alarm goes off "early" */
316:
317: /*
318: * Print a running summary of nfs statistics.
319: * Repeat display every interval seconds, showing statistics
320: * collected over that interval. Assumes that interval is non-zero.
321: * First line printed at top of screen is always cumulative.
322: */
323: void
1.4 kstailey 324: sidewaysintpr(interval, off, display)
1.1 deraadt 325: u_int interval;
326: u_long off;
1.4 kstailey 327: u_int display;
1.1 deraadt 328: {
329: struct nfsstats nfsstats, lastst;
330: int hdrcnt, oldmask;
331: void catchalarm();
332:
333: (void)signal(SIGALRM, catchalarm);
334: signalled = 0;
335: (void)alarm(interval);
336: bzero((caddr_t)&lastst, sizeof(lastst));
337:
338: for (hdrcnt = 1;;) {
339: if (!--hdrcnt) {
340: printhdr();
341: hdrcnt = 20;
342: }
343: if (kvm_read(kd, off, (char *)&nfsstats, sizeof nfsstats) < 0) {
344: fprintf(stderr, "nfsstat: kvm_read failed\n");
345: exit(1);
346: }
1.4 kstailey 347: if (display & SHOW_CLIENT)
348: printf("Client: %8d %8d %8d %8d %8d %8d %8d %8d\n",
1.2 niklas 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]));
1.4 kstailey 358: if (display & SHOW_SERVER)
359: printf("Server: %8d %8d %8d %8d %8d %8d %8d %8d\n",
1.2 niklas 360: nfsstats.srvrpccnt[NFSPROC_GETATTR]-lastst.srvrpccnt[NFSPROC_GETATTR],
361: nfsstats.srvrpccnt[NFSPROC_LOOKUP]-lastst.srvrpccnt[NFSPROC_LOOKUP],
362: nfsstats.srvrpccnt[NFSPROC_READLINK]-lastst.srvrpccnt[NFSPROC_READLINK],
363: nfsstats.srvrpccnt[NFSPROC_READ]-lastst.srvrpccnt[NFSPROC_READ],
364: nfsstats.srvrpccnt[NFSPROC_WRITE]-lastst.srvrpccnt[NFSPROC_WRITE],
365: nfsstats.srvrpccnt[NFSPROC_RENAME]-lastst.srvrpccnt[NFSPROC_RENAME],
366: nfsstats.srvrpccnt[NFSPROC_ACCESS]-lastst.srvrpccnt[NFSPROC_ACCESS],
367: (nfsstats.srvrpccnt[NFSPROC_READDIR]-lastst.srvrpccnt[NFSPROC_READDIR])
368: +(nfsstats.srvrpccnt[NFSPROC_READDIRPLUS]-lastst.srvrpccnt[NFSPROC_READDIRPLUS]));
1.1 deraadt 369: lastst = nfsstats;
370: fflush(stdout);
371: oldmask = sigblock(sigmask(SIGALRM));
372: if (!signalled)
373: sigpause(0);
374: sigsetmask(oldmask);
375: signalled = 0;
376: (void)alarm(interval);
377: }
378: /*NOTREACHED*/
379: }
380:
381: void
382: printhdr()
383: {
384: printf(" %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s\n",
385: "Getattr", "Lookup", "Readlink", "Read", "Write", "Rename",
1.2 niklas 386: "Access", "Readdir");
1.1 deraadt 387: fflush(stdout);
388: }
389:
390: /*
391: * Called if an interval expires before sidewaysintpr has completed a loop.
392: * Sets a flag to not wait for the alarm.
393: */
394: void
395: catchalarm()
396: {
397: signalled = 1;
398: }
399:
400: void
401: usage()
402: {
403: (void)fprintf(stderr,
404: "usage: nfsstat [-M core] [-N system] [-w interval]\n");
405: exit(1);
406: }