Annotation of src/usr.bin/nfsstat/nfsstat.c, Revision 1.2
1.2 ! niklas 1: /* $OpenBSD$ */
! 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
! 51: static char *rcsid = "$OpenBSD$";
! 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>
71: #include <paths.h>
1.2 ! niklas 72: #include <err.h>
1.1 deraadt 73:
74: struct nlist nl[] = {
75: #define N_NFSSTAT 0
76: { "_nfsstats" },
77: "",
78: };
79: kvm_t *kd;
80:
81: void intpr(), printhdr(), sidewaysintpr(), usage();
82:
83: main(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:")) != EOF)
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:
132: if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == 0) {
133: fprintf(stderr, "nfsstat: kvm_openfiles: %s\n", errbuf);
134: exit(1);
135: }
136: if (kvm_nlist(kd, nl) != 0) {
137: fprintf(stderr, "nfsstat: kvm_nlist: can't get names\n");
138: exit(1);
139: }
140:
141: if (interval)
142: sidewaysintpr(interval, nl[N_NFSSTAT].n_value);
143: else
144: intpr(nl[N_NFSSTAT].n_value);
145: exit(0);
146: }
147:
148: /*
149: * Print a description of the nfs stats.
150: */
151: void
152: intpr(nfsstataddr)
153: u_long nfsstataddr;
154: {
155: struct nfsstats nfsstats;
156:
157: if (kvm_read(kd, (u_long)nfsstataddr, (char *)&nfsstats, sizeof(struct nfsstats)) < 0) {
158: fprintf(stderr, "nfsstat: kvm_read failed\n");
159: exit(1);
160: }
161: printf("Client Info:\n");
162: printf("Rpc Counts:\n");
163: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
164: "Getattr", "Setattr", "Lookup", "Readlink", "Read",
165: "Write", "Create", "Remove");
166: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
167: nfsstats.rpccnt[NFSPROC_GETATTR],
168: nfsstats.rpccnt[NFSPROC_SETATTR],
169: nfsstats.rpccnt[NFSPROC_LOOKUP],
170: nfsstats.rpccnt[NFSPROC_READLINK],
171: nfsstats.rpccnt[NFSPROC_READ],
172: nfsstats.rpccnt[NFSPROC_WRITE],
173: nfsstats.rpccnt[NFSPROC_CREATE],
174: nfsstats.rpccnt[NFSPROC_REMOVE]);
175: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
176: "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
1.2 ! niklas 177: "Readdir", "RdirPlus", "Access");
1.1 deraadt 178: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
179: nfsstats.rpccnt[NFSPROC_RENAME],
180: nfsstats.rpccnt[NFSPROC_LINK],
181: nfsstats.rpccnt[NFSPROC_SYMLINK],
182: nfsstats.rpccnt[NFSPROC_MKDIR],
183: nfsstats.rpccnt[NFSPROC_RMDIR],
184: nfsstats.rpccnt[NFSPROC_READDIR],
1.2 ! niklas 185: nfsstats.rpccnt[NFSPROC_READDIRPLUS],
! 186: nfsstats.rpccnt[NFSPROC_ACCESS]);
! 187: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
! 188: "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
1.1 deraadt 189: "GLease", "Vacate", "Evict");
1.2 ! niklas 190: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
! 191: nfsstats.rpccnt[NFSPROC_MKNOD],
! 192: nfsstats.rpccnt[NFSPROC_FSSTAT],
! 193: nfsstats.rpccnt[NFSPROC_FSINFO],
! 194: nfsstats.rpccnt[NFSPROC_PATHCONF],
! 195: nfsstats.rpccnt[NFSPROC_COMMIT],
1.1 deraadt 196: nfsstats.rpccnt[NQNFSPROC_GETLEASE],
197: nfsstats.rpccnt[NQNFSPROC_VACATED],
198: nfsstats.rpccnt[NQNFSPROC_EVICTED]);
199: printf("Rpc Info:\n");
200: printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
201: "TimedOut", "Invalid", "X Replies", "Retries", "Requests");
202: printf("%9d %9d %9d %9d %9d\n",
203: nfsstats.rpctimeouts,
204: nfsstats.rpcinvalid,
205: nfsstats.rpcunexpected,
206: nfsstats.rpcretries,
207: nfsstats.rpcrequests);
208: printf("Cache Info:\n");
209: printf("%9.9s %9.9s %9.9s %9.9s",
210: "Attr Hits", "Misses", "Lkup Hits", "Misses");
211: printf(" %9.9s %9.9s %9.9s %9.9s\n",
212: "BioR Hits", "Misses", "BioW Hits", "Misses");
213: printf("%9d %9d %9d %9d",
214: nfsstats.attrcache_hits, nfsstats.attrcache_misses,
215: nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
216: printf(" %9d %9d %9d %9d\n",
217: nfsstats.biocache_reads-nfsstats.read_bios,
218: nfsstats.read_bios,
219: nfsstats.biocache_writes-nfsstats.write_bios,
220: nfsstats.write_bios);
221: printf("%9.9s %9.9s %9.9s %9.9s",
222: "BioRLHits", "Misses", "BioD Hits", "Misses");
223: printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
224: printf("%9d %9d %9d %9d",
225: nfsstats.biocache_readlinks-nfsstats.readlink_bios,
226: nfsstats.readlink_bios,
227: nfsstats.biocache_readdirs-nfsstats.readdir_bios,
228: nfsstats.readdir_bios);
229: printf(" %9d %9d\n",
230: nfsstats.direofcache_hits, nfsstats.direofcache_misses);
231: printf("\nServer Info:\n");
232: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
233: "Getattr", "Setattr", "Lookup", "Readlink", "Read",
234: "Write", "Create", "Remove");
235: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
236: nfsstats.srvrpccnt[NFSPROC_GETATTR],
237: nfsstats.srvrpccnt[NFSPROC_SETATTR],
238: nfsstats.srvrpccnt[NFSPROC_LOOKUP],
239: nfsstats.srvrpccnt[NFSPROC_READLINK],
240: nfsstats.srvrpccnt[NFSPROC_READ],
241: nfsstats.srvrpccnt[NFSPROC_WRITE],
242: nfsstats.srvrpccnt[NFSPROC_CREATE],
243: nfsstats.srvrpccnt[NFSPROC_REMOVE]);
244: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
245: "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
1.2 ! niklas 246: "Readdir", "RdirPlus", "Access");
1.1 deraadt 247: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
248: nfsstats.srvrpccnt[NFSPROC_RENAME],
249: nfsstats.srvrpccnt[NFSPROC_LINK],
250: nfsstats.srvrpccnt[NFSPROC_SYMLINK],
251: nfsstats.srvrpccnt[NFSPROC_MKDIR],
252: nfsstats.srvrpccnt[NFSPROC_RMDIR],
253: nfsstats.srvrpccnt[NFSPROC_READDIR],
1.2 ! niklas 254: nfsstats.srvrpccnt[NFSPROC_READDIRPLUS],
! 255: nfsstats.srvrpccnt[NFSPROC_ACCESS]);
! 256: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
! 257: "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
1.1 deraadt 258: "GLease", "Vacate", "Evict");
1.2 ! niklas 259: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
! 260: nfsstats.srvrpccnt[NFSPROC_MKNOD],
! 261: nfsstats.srvrpccnt[NFSPROC_FSSTAT],
! 262: nfsstats.srvrpccnt[NFSPROC_FSINFO],
! 263: nfsstats.srvrpccnt[NFSPROC_PATHCONF],
! 264: nfsstats.srvrpccnt[NFSPROC_COMMIT],
1.1 deraadt 265: nfsstats.srvrpccnt[NQNFSPROC_GETLEASE],
266: nfsstats.srvrpccnt[NQNFSPROC_VACATED],
267: nfsstats.srvrpccnt[NQNFSPROC_EVICTED]);
268: printf("Server Ret-Failed\n");
269: printf("%17d\n", nfsstats.srvrpc_errs);
270: printf("Server Faults\n");
271: printf("%13d\n", nfsstats.srv_errs);
272: printf("Server Cache Stats:\n");
273: printf("%9.9s %9.9s %9.9s %9.9s\n",
274: "Inprog", "Idem", "Non-idem", "Misses");
275: printf("%9d %9d %9d %9d\n",
276: nfsstats.srvcache_inproghits,
277: nfsstats.srvcache_idemdonehits,
278: nfsstats.srvcache_nonidemdonehits,
279: nfsstats.srvcache_misses);
280: printf("Server Lease Stats:\n");
281: printf("%9.9s %9.9s %9.9s\n",
282: "Leases", "PeakL", "GLeases");
283: printf("%9d %9d %9d\n",
284: nfsstats.srvnqnfs_leases,
285: nfsstats.srvnqnfs_maxleases,
286: nfsstats.srvnqnfs_getleases);
1.2 ! niklas 287: printf("Server Write Gathering:\n");
! 288: printf("%9.9s %9.9s %9.9s\n",
! 289: "WriteOps", "WriteRPC", "Opsaved");
! 290: printf("%9d %9d %9d\n",
! 291: nfsstats.srvvop_writes,
! 292: nfsstats.srvrpccnt[NFSPROC_WRITE],
! 293: nfsstats.srvrpccnt[NFSPROC_WRITE] - nfsstats.srvvop_writes);
1.1 deraadt 294: }
295:
296: u_char signalled; /* set if alarm goes off "early" */
297:
298: /*
299: * Print a running summary of nfs statistics.
300: * Repeat display every interval seconds, showing statistics
301: * collected over that interval. Assumes that interval is non-zero.
302: * First line printed at top of screen is always cumulative.
303: */
304: void
305: sidewaysintpr(interval, off)
306: u_int interval;
307: u_long off;
308: {
309: struct nfsstats nfsstats, lastst;
310: int hdrcnt, oldmask;
311: void catchalarm();
312:
313: (void)signal(SIGALRM, catchalarm);
314: signalled = 0;
315: (void)alarm(interval);
316: bzero((caddr_t)&lastst, sizeof(lastst));
317:
318: for (hdrcnt = 1;;) {
319: if (!--hdrcnt) {
320: printhdr();
321: hdrcnt = 20;
322: }
323: if (kvm_read(kd, off, (char *)&nfsstats, sizeof nfsstats) < 0) {
324: fprintf(stderr, "nfsstat: kvm_read failed\n");
325: exit(1);
326: }
327: printf("Client: %8d %8d %8d %8d %8d %8d %8d %8d\n",
1.2 ! niklas 328: nfsstats.rpccnt[NFSPROC_GETATTR]-lastst.rpccnt[NFSPROC_GETATTR],
! 329: nfsstats.rpccnt[NFSPROC_LOOKUP]-lastst.rpccnt[NFSPROC_LOOKUP],
! 330: nfsstats.rpccnt[NFSPROC_READLINK]-lastst.rpccnt[NFSPROC_READLINK],
! 331: nfsstats.rpccnt[NFSPROC_READ]-lastst.rpccnt[NFSPROC_READ],
! 332: nfsstats.rpccnt[NFSPROC_WRITE]-lastst.rpccnt[NFSPROC_WRITE],
! 333: nfsstats.rpccnt[NFSPROC_RENAME]-lastst.rpccnt[NFSPROC_RENAME],
! 334: nfsstats.rpccnt[NFSPROC_ACCESS]-lastst.rpccnt[NFSPROC_ACCESS],
! 335: (nfsstats.rpccnt[NFSPROC_READDIR]-lastst.rpccnt[NFSPROC_READDIR])
! 336: +(nfsstats.rpccnt[NFSPROC_READDIRPLUS]-lastst.rpccnt[NFSPROC_READDIRPLUS]));
1.1 deraadt 337: printf("Server: %8d %8d %8d %8d %8d %8d %8d %8d\n",
1.2 ! niklas 338: nfsstats.srvrpccnt[NFSPROC_GETATTR]-lastst.srvrpccnt[NFSPROC_GETATTR],
! 339: nfsstats.srvrpccnt[NFSPROC_LOOKUP]-lastst.srvrpccnt[NFSPROC_LOOKUP],
! 340: nfsstats.srvrpccnt[NFSPROC_READLINK]-lastst.srvrpccnt[NFSPROC_READLINK],
! 341: nfsstats.srvrpccnt[NFSPROC_READ]-lastst.srvrpccnt[NFSPROC_READ],
! 342: nfsstats.srvrpccnt[NFSPROC_WRITE]-lastst.srvrpccnt[NFSPROC_WRITE],
! 343: nfsstats.srvrpccnt[NFSPROC_RENAME]-lastst.srvrpccnt[NFSPROC_RENAME],
! 344: nfsstats.srvrpccnt[NFSPROC_ACCESS]-lastst.srvrpccnt[NFSPROC_ACCESS],
! 345: (nfsstats.srvrpccnt[NFSPROC_READDIR]-lastst.srvrpccnt[NFSPROC_READDIR])
! 346: +(nfsstats.srvrpccnt[NFSPROC_READDIRPLUS]-lastst.srvrpccnt[NFSPROC_READDIRPLUS]));
1.1 deraadt 347: lastst = nfsstats;
348: fflush(stdout);
349: oldmask = sigblock(sigmask(SIGALRM));
350: if (!signalled)
351: sigpause(0);
352: sigsetmask(oldmask);
353: signalled = 0;
354: (void)alarm(interval);
355: }
356: /*NOTREACHED*/
357: }
358:
359: void
360: printhdr()
361: {
362: printf(" %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s\n",
363: "Getattr", "Lookup", "Readlink", "Read", "Write", "Rename",
1.2 ! niklas 364: "Access", "Readdir");
1.1 deraadt 365: fflush(stdout);
366: }
367:
368: /*
369: * Called if an interval expires before sidewaysintpr has completed a loop.
370: * Sets a flag to not wait for the alarm.
371: */
372: void
373: catchalarm()
374: {
375: signalled = 1;
376: }
377:
378: void
379: usage()
380: {
381: (void)fprintf(stderr,
382: "usage: nfsstat [-M core] [-N system] [-w interval]\n");
383: exit(1);
384: }