Annotation of src/usr.bin/nfsstat/nfsstat.c, Revision 1.7
1.7 ! deraadt 1: /* $OpenBSD: nfsstat.c,v 1.6 1997/01/15 23:42:57 millert 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.7 ! deraadt 51: static char *rcsid = "$OpenBSD: nfsstat.c,v 1.6 1997/01/15 23:42:57 millert 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:
1.7 ! deraadt 172: if (kvm_read(kd, (u_long)nfsstataddr, (char *)&nfsstats,
! 173: sizeof(struct nfsstats)) != sizeof(struct nfsstats)) {
1.1 deraadt 174: fprintf(stderr, "nfsstat: kvm_read failed\n");
175: exit(1);
176: }
1.4 kstailey 177: if (display & SHOW_CLIENT) {
178: printf("Client Info:\n");
179: printf("Rpc Counts:\n");
180: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
181: "Getattr", "Setattr", "Lookup", "Readlink", "Read",
182: "Write", "Create", "Remove");
183: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
184: nfsstats.rpccnt[NFSPROC_GETATTR],
185: nfsstats.rpccnt[NFSPROC_SETATTR],
186: nfsstats.rpccnt[NFSPROC_LOOKUP],
187: nfsstats.rpccnt[NFSPROC_READLINK],
188: nfsstats.rpccnt[NFSPROC_READ],
189: nfsstats.rpccnt[NFSPROC_WRITE],
190: nfsstats.rpccnt[NFSPROC_CREATE],
191: nfsstats.rpccnt[NFSPROC_REMOVE]);
192: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
193: "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
194: "Readdir", "RdirPlus", "Access");
195: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
196: nfsstats.rpccnt[NFSPROC_RENAME],
197: nfsstats.rpccnt[NFSPROC_LINK],
198: nfsstats.rpccnt[NFSPROC_SYMLINK],
199: nfsstats.rpccnt[NFSPROC_MKDIR],
200: nfsstats.rpccnt[NFSPROC_RMDIR],
201: nfsstats.rpccnt[NFSPROC_READDIR],
202: nfsstats.rpccnt[NFSPROC_READDIRPLUS],
203: nfsstats.rpccnt[NFSPROC_ACCESS]);
204: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
205: "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
206: "GLease", "Vacate", "Evict");
207: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
208: nfsstats.rpccnt[NFSPROC_MKNOD],
209: nfsstats.rpccnt[NFSPROC_FSSTAT],
210: nfsstats.rpccnt[NFSPROC_FSINFO],
211: nfsstats.rpccnt[NFSPROC_PATHCONF],
212: nfsstats.rpccnt[NFSPROC_COMMIT],
213: nfsstats.rpccnt[NQNFSPROC_GETLEASE],
214: nfsstats.rpccnt[NQNFSPROC_VACATED],
215: nfsstats.rpccnt[NQNFSPROC_EVICTED]);
216: printf("Rpc Info:\n");
217: printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
218: "TimedOut", "Invalid", "X Replies", "Retries", "Requests");
219: printf("%9d %9d %9d %9d %9d\n",
220: nfsstats.rpctimeouts,
221: nfsstats.rpcinvalid,
222: nfsstats.rpcunexpected,
223: nfsstats.rpcretries,
224: nfsstats.rpcrequests);
225: printf("Cache Info:\n");
226: printf("%9.9s %9.9s %9.9s %9.9s",
227: "Attr Hits", "Misses", "Lkup Hits", "Misses");
228: printf(" %9.9s %9.9s %9.9s %9.9s\n",
229: "BioR Hits", "Misses", "BioW Hits", "Misses");
230: printf("%9d %9d %9d %9d",
231: nfsstats.attrcache_hits, nfsstats.attrcache_misses,
232: nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
233: printf(" %9d %9d %9d %9d\n",
234: nfsstats.biocache_reads-nfsstats.read_bios,
235: nfsstats.read_bios,
236: nfsstats.biocache_writes-nfsstats.write_bios,
237: nfsstats.write_bios);
238: printf("%9.9s %9.9s %9.9s %9.9s",
239: "BioRLHits", "Misses", "BioD Hits", "Misses");
240: printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
241: printf("%9d %9d %9d %9d",
242: nfsstats.biocache_readlinks-nfsstats.readlink_bios,
243: nfsstats.readlink_bios,
244: nfsstats.biocache_readdirs-nfsstats.readdir_bios,
245: nfsstats.readdir_bios);
246: printf(" %9d %9d\n",
247: nfsstats.direofcache_hits, nfsstats.direofcache_misses);
248: }
249: if (display & SHOW_SERVER) {
250: printf("\nServer Info:\n");
251: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
252: "Getattr", "Setattr", "Lookup", "Readlink", "Read",
253: "Write", "Create", "Remove");
254: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
255: nfsstats.srvrpccnt[NFSPROC_GETATTR],
256: nfsstats.srvrpccnt[NFSPROC_SETATTR],
257: nfsstats.srvrpccnt[NFSPROC_LOOKUP],
258: nfsstats.srvrpccnt[NFSPROC_READLINK],
259: nfsstats.srvrpccnt[NFSPROC_READ],
260: nfsstats.srvrpccnt[NFSPROC_WRITE],
261: nfsstats.srvrpccnt[NFSPROC_CREATE],
262: nfsstats.srvrpccnt[NFSPROC_REMOVE]);
263: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
264: "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
265: "Readdir", "RdirPlus", "Access");
266: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
267: nfsstats.srvrpccnt[NFSPROC_RENAME],
268: nfsstats.srvrpccnt[NFSPROC_LINK],
269: nfsstats.srvrpccnt[NFSPROC_SYMLINK],
270: nfsstats.srvrpccnt[NFSPROC_MKDIR],
271: nfsstats.srvrpccnt[NFSPROC_RMDIR],
272: nfsstats.srvrpccnt[NFSPROC_READDIR],
273: nfsstats.srvrpccnt[NFSPROC_READDIRPLUS],
274: nfsstats.srvrpccnt[NFSPROC_ACCESS]);
275: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
276: "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
277: "GLease", "Vacate", "Evict");
278: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
279: nfsstats.srvrpccnt[NFSPROC_MKNOD],
280: nfsstats.srvrpccnt[NFSPROC_FSSTAT],
281: nfsstats.srvrpccnt[NFSPROC_FSINFO],
282: nfsstats.srvrpccnt[NFSPROC_PATHCONF],
283: nfsstats.srvrpccnt[NFSPROC_COMMIT],
284: nfsstats.srvrpccnt[NQNFSPROC_GETLEASE],
285: nfsstats.srvrpccnt[NQNFSPROC_VACATED],
286: nfsstats.srvrpccnt[NQNFSPROC_EVICTED]);
287: printf("Server Ret-Failed\n");
288: printf("%17d\n", nfsstats.srvrpc_errs);
289: printf("Server Faults\n");
290: printf("%13d\n", nfsstats.srv_errs);
291: printf("Server Cache Stats:\n");
292: printf("%9.9s %9.9s %9.9s %9.9s\n",
293: "Inprog", "Idem", "Non-idem", "Misses");
294: printf("%9d %9d %9d %9d\n",
295: nfsstats.srvcache_inproghits,
296: nfsstats.srvcache_idemdonehits,
297: nfsstats.srvcache_nonidemdonehits,
298: nfsstats.srvcache_misses);
299: printf("Server Lease Stats:\n");
300: printf("%9.9s %9.9s %9.9s\n",
301: "Leases", "PeakL", "GLeases");
302: printf("%9d %9d %9d\n",
303: nfsstats.srvnqnfs_leases,
304: nfsstats.srvnqnfs_maxleases,
305: nfsstats.srvnqnfs_getleases);
306: printf("Server Write Gathering:\n");
307: printf("%9.9s %9.9s %9.9s\n",
308: "WriteOps", "WriteRPC", "Opsaved");
309: printf("%9d %9d %9d\n",
310: nfsstats.srvvop_writes,
311: nfsstats.srvrpccnt[NFSPROC_WRITE],
312: nfsstats.srvrpccnt[NFSPROC_WRITE] - nfsstats.srvvop_writes);
313: }
1.1 deraadt 314: }
315:
316: u_char signalled; /* set if alarm goes off "early" */
317:
318: /*
319: * Print a running summary of nfs statistics.
320: * Repeat display every interval seconds, showing statistics
321: * collected over that interval. Assumes that interval is non-zero.
322: * First line printed at top of screen is always cumulative.
323: */
324: void
1.4 kstailey 325: sidewaysintpr(interval, off, display)
1.1 deraadt 326: u_int interval;
327: u_long off;
1.4 kstailey 328: u_int display;
1.1 deraadt 329: {
330: struct nfsstats nfsstats, lastst;
331: int hdrcnt, oldmask;
332: void catchalarm();
333:
334: (void)signal(SIGALRM, catchalarm);
335: signalled = 0;
336: (void)alarm(interval);
337: bzero((caddr_t)&lastst, sizeof(lastst));
338:
339: for (hdrcnt = 1;;) {
340: if (!--hdrcnt) {
341: printhdr();
342: hdrcnt = 20;
343: }
1.7 ! deraadt 344: if (kvm_read(kd, off, (char *)&nfsstats, sizeof nfsstats) !=
! 345: sizeof nfsstats) {
1.1 deraadt 346: fprintf(stderr, "nfsstat: kvm_read failed\n");
347: exit(1);
348: }
1.4 kstailey 349: if (display & SHOW_CLIENT)
350: printf("Client: %8d %8d %8d %8d %8d %8d %8d %8d\n",
1.2 niklas 351: nfsstats.rpccnt[NFSPROC_GETATTR]-lastst.rpccnt[NFSPROC_GETATTR],
352: nfsstats.rpccnt[NFSPROC_LOOKUP]-lastst.rpccnt[NFSPROC_LOOKUP],
353: nfsstats.rpccnt[NFSPROC_READLINK]-lastst.rpccnt[NFSPROC_READLINK],
354: nfsstats.rpccnt[NFSPROC_READ]-lastst.rpccnt[NFSPROC_READ],
355: nfsstats.rpccnt[NFSPROC_WRITE]-lastst.rpccnt[NFSPROC_WRITE],
356: nfsstats.rpccnt[NFSPROC_RENAME]-lastst.rpccnt[NFSPROC_RENAME],
357: nfsstats.rpccnt[NFSPROC_ACCESS]-lastst.rpccnt[NFSPROC_ACCESS],
358: (nfsstats.rpccnt[NFSPROC_READDIR]-lastst.rpccnt[NFSPROC_READDIR])
359: +(nfsstats.rpccnt[NFSPROC_READDIRPLUS]-lastst.rpccnt[NFSPROC_READDIRPLUS]));
1.4 kstailey 360: if (display & SHOW_SERVER)
361: printf("Server: %8d %8d %8d %8d %8d %8d %8d %8d\n",
1.2 niklas 362: nfsstats.srvrpccnt[NFSPROC_GETATTR]-lastst.srvrpccnt[NFSPROC_GETATTR],
363: nfsstats.srvrpccnt[NFSPROC_LOOKUP]-lastst.srvrpccnt[NFSPROC_LOOKUP],
364: nfsstats.srvrpccnt[NFSPROC_READLINK]-lastst.srvrpccnt[NFSPROC_READLINK],
365: nfsstats.srvrpccnt[NFSPROC_READ]-lastst.srvrpccnt[NFSPROC_READ],
366: nfsstats.srvrpccnt[NFSPROC_WRITE]-lastst.srvrpccnt[NFSPROC_WRITE],
367: nfsstats.srvrpccnt[NFSPROC_RENAME]-lastst.srvrpccnt[NFSPROC_RENAME],
368: nfsstats.srvrpccnt[NFSPROC_ACCESS]-lastst.srvrpccnt[NFSPROC_ACCESS],
369: (nfsstats.srvrpccnt[NFSPROC_READDIR]-lastst.srvrpccnt[NFSPROC_READDIR])
370: +(nfsstats.srvrpccnt[NFSPROC_READDIRPLUS]-lastst.srvrpccnt[NFSPROC_READDIRPLUS]));
1.1 deraadt 371: lastst = nfsstats;
372: fflush(stdout);
373: oldmask = sigblock(sigmask(SIGALRM));
374: if (!signalled)
375: sigpause(0);
376: sigsetmask(oldmask);
377: signalled = 0;
378: (void)alarm(interval);
379: }
380: /*NOTREACHED*/
381: }
382:
383: void
384: printhdr()
385: {
386: printf(" %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s\n",
387: "Getattr", "Lookup", "Readlink", "Read", "Write", "Rename",
1.2 niklas 388: "Access", "Readdir");
1.1 deraadt 389: fflush(stdout);
390: }
391:
392: /*
393: * Called if an interval expires before sidewaysintpr has completed a loop.
394: * Sets a flag to not wait for the alarm.
395: */
396: void
397: catchalarm()
398: {
399: signalled = 1;
400: }
401:
402: void
403: usage()
404: {
405: (void)fprintf(stderr,
406: "usage: nfsstat [-M core] [-N system] [-w interval]\n");
407: exit(1);
408: }