Annotation of src/usr.bin/nfsstat/nfsstat.c, Revision 1.9
1.9 ! mickey 1: /* $OpenBSD: nfsstat.c,v 1.8 2000/04/18 15:24:26 mickey 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.9 ! mickey 51: static char *rcsid = "$OpenBSD: nfsstat.c,v 1.8 2000/04/18 15:24:26 mickey 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>
1.9 ! mickey 62: #include <fcntl.h>
! 63: #include <ctype.h>
1.1 deraadt 64: #include <errno.h>
1.9 ! mickey 65: #include <kvm.h>
! 66: #include <nlist.h>
1.1 deraadt 67: #include <unistd.h>
68: #include <stdio.h>
69: #include <stdlib.h>
70: #include <string.h>
1.9 ! mickey 71: #include <limits.h>
! 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.9 ! mickey 79: struct nlist nl[] = {
! 80: #define N_NFSSTAT 0
! 81: { "_nfsstats" },
! 82: { "" },
! 83: };
! 84: kvm_t *kd;
1.8 mickey 85: u_char signalled; /* set if alarm goes off "early" */
86: int nfs_id;
1.1 deraadt 87:
1.8 mickey 88: void getnfsstats __P((struct nfsstats *));
1.9 ! mickey 89: void printhdr __P((void));
! 90: void intpr __P((u_int));
! 91: void sidewaysintpr __P((u_int, u_int));
! 92: void usage __P((void));
1.1 deraadt 93:
1.8 mickey 94: int
1.1 deraadt 95: main(argc, argv)
96: int argc;
97: char **argv;
98: {
99: extern int optind;
100: extern char *optarg;
1.8 mickey 101: char *p;
1.1 deraadt 102: u_int interval;
1.4 kstailey 103: u_int display = SHOW_ALL;
1.9 ! mickey 104: char *memf, *nlistf;
1.1 deraadt 105: int ch;
106:
107: interval = 0;
1.9 ! mickey 108: memf = nlistf = NULL;
1.6 millert 109: while ((ch = getopt(argc, argv, "M:N:w:sc")) != -1)
1.1 deraadt 110: switch(ch) {
111: case 'M':
1.9 ! mickey 112: memf = optarg;
! 113: break;
1.1 deraadt 114: case 'N':
1.9 ! mickey 115: nlistf = optarg;
1.1 deraadt 116: break;
117: case 'w':
1.8 mickey 118: interval = (u_int)strtol(optarg, &p, 0);
119: if (*optarg != '\0' && *p == '\0')
120: errx(1, "invalid interval");
1.1 deraadt 121: break;
1.4 kstailey 122: case 's':
123: display = SHOW_SERVER;
124: break;
125: case 'c':
126: display = SHOW_CLIENT;
127: break;
1.1 deraadt 128: case '?':
129: default:
130: usage();
131: }
132: argc -= optind;
133: argv += optind;
134:
1.9 ! mickey 135: #define BACKWARD_COMPATIBILITY
! 136: #ifdef BACKWARD_COMPATIBILITY
! 137: if (*argv) {
! 138: interval = atoi(*argv);
! 139: if (*++argv) {
! 140: nlistf = *argv;
! 141: if (*++argv)
! 142: memf = *argv;
! 143: }
! 144: }
! 145: #endif
! 146: if (nlistf || memf) {
! 147: char errbuf[_POSIX2_LINE_MAX];
! 148:
! 149: if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == 0)
! 150: errx(1, "nfsstat: %s", errbuf);
! 151: if (kvm_nlist(kd, nl) != 0)
! 152: errx(1, "kvm_nlist: can't get names");
! 153: } else {
1.8 mickey 154: int mib[3];
155: size_t len;
156:
157: mib[0] = CTL_VFS;
158: mib[1] = VFS_GENERIC;
159: mib[2] = VFS_MAXTYPENUM;
160: len = sizeof(nfs_id);
161: if (sysctl(mib, 3, &nfs_id, &len, NULL, 0))
162: err(1, "sysctl: VFS_MAXTYPENUM");
163:
164: while (nfs_id--) {
165: struct vfsconf vfsc;
166:
167: mib[0] = CTL_VFS;
168: mib[1] = VFS_GENERIC;
169: mib[2] = VFS_CONF;
170: mib[3] = nfs_id;
171:
172: len = sizeof(vfsc);
173: if (sysctl(mib, 4, &vfsc, &len, NULL, 0))
174: continue;
175:
176: if (!strcmp(vfsc.vfc_name, MOUNT_NFS))
177: break;
178: }
179:
180: if (nfs_id < 0)
181: errx(1, "cannot find nfs filesystem id");
182: }
183:
184: if (interval)
185: sidewaysintpr(interval, display);
186: else
187: intpr(display);
188:
189: return 0;
190: }
191:
192: void
193: getnfsstats(p)
194: struct nfsstats *p;
195: {
1.9 ! mickey 196: if (kd) {
! 197: if (kvm_read(kd, nl[N_NFSSTAT].n_value, p, sizeof(*p)) != sizeof(*p))
! 198: errx(1, "kvm_read failed");
! 199: } else {
! 200: int mib[3];
! 201: size_t len = sizeof(*p);
1.1 deraadt 202:
1.9 ! mickey 203: mib[0] = CTL_VFS;
! 204: mib[1] = nfs_id; /* 2 */
! 205: mib[2] = NFS_NFSSTATS;
1.1 deraadt 206:
1.9 ! mickey 207: if (sysctl(mib, 3, p, &len, NULL, 0))
! 208: err(1, "sysctl");
! 209: }
1.1 deraadt 210: }
211:
212: /*
213: * Print a description of the nfs stats.
214: */
1.9 ! mickey 215: void
1.8 mickey 216: intpr(display)
1.4 kstailey 217: u_int display;
1.1 deraadt 218: {
219: struct nfsstats nfsstats;
220:
1.8 mickey 221: getnfsstats(&nfsstats);
222:
1.4 kstailey 223: if (display & SHOW_CLIENT) {
224: printf("Client Info:\n");
225: printf("Rpc Counts:\n");
226: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
227: "Getattr", "Setattr", "Lookup", "Readlink", "Read",
228: "Write", "Create", "Remove");
229: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
230: nfsstats.rpccnt[NFSPROC_GETATTR],
231: nfsstats.rpccnt[NFSPROC_SETATTR],
232: nfsstats.rpccnt[NFSPROC_LOOKUP],
233: nfsstats.rpccnt[NFSPROC_READLINK],
234: nfsstats.rpccnt[NFSPROC_READ],
235: nfsstats.rpccnt[NFSPROC_WRITE],
236: nfsstats.rpccnt[NFSPROC_CREATE],
237: nfsstats.rpccnt[NFSPROC_REMOVE]);
238: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
239: "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
240: "Readdir", "RdirPlus", "Access");
241: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
242: nfsstats.rpccnt[NFSPROC_RENAME],
243: nfsstats.rpccnt[NFSPROC_LINK],
244: nfsstats.rpccnt[NFSPROC_SYMLINK],
245: nfsstats.rpccnt[NFSPROC_MKDIR],
246: nfsstats.rpccnt[NFSPROC_RMDIR],
247: nfsstats.rpccnt[NFSPROC_READDIR],
248: nfsstats.rpccnt[NFSPROC_READDIRPLUS],
249: nfsstats.rpccnt[NFSPROC_ACCESS]);
250: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
251: "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
252: "GLease", "Vacate", "Evict");
253: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
254: nfsstats.rpccnt[NFSPROC_MKNOD],
255: nfsstats.rpccnt[NFSPROC_FSSTAT],
256: nfsstats.rpccnt[NFSPROC_FSINFO],
257: nfsstats.rpccnt[NFSPROC_PATHCONF],
258: nfsstats.rpccnt[NFSPROC_COMMIT],
259: nfsstats.rpccnt[NQNFSPROC_GETLEASE],
260: nfsstats.rpccnt[NQNFSPROC_VACATED],
261: nfsstats.rpccnt[NQNFSPROC_EVICTED]);
262: printf("Rpc Info:\n");
263: printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
264: "TimedOut", "Invalid", "X Replies", "Retries", "Requests");
265: printf("%9d %9d %9d %9d %9d\n",
266: nfsstats.rpctimeouts,
267: nfsstats.rpcinvalid,
268: nfsstats.rpcunexpected,
269: nfsstats.rpcretries,
270: nfsstats.rpcrequests);
271: printf("Cache Info:\n");
272: printf("%9.9s %9.9s %9.9s %9.9s",
273: "Attr Hits", "Misses", "Lkup Hits", "Misses");
274: printf(" %9.9s %9.9s %9.9s %9.9s\n",
275: "BioR Hits", "Misses", "BioW Hits", "Misses");
276: printf("%9d %9d %9d %9d",
277: nfsstats.attrcache_hits, nfsstats.attrcache_misses,
278: nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
279: printf(" %9d %9d %9d %9d\n",
280: nfsstats.biocache_reads-nfsstats.read_bios,
281: nfsstats.read_bios,
282: nfsstats.biocache_writes-nfsstats.write_bios,
283: nfsstats.write_bios);
284: printf("%9.9s %9.9s %9.9s %9.9s",
285: "BioRLHits", "Misses", "BioD Hits", "Misses");
286: printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
287: printf("%9d %9d %9d %9d",
288: nfsstats.biocache_readlinks-nfsstats.readlink_bios,
289: nfsstats.readlink_bios,
290: nfsstats.biocache_readdirs-nfsstats.readdir_bios,
291: nfsstats.readdir_bios);
292: printf(" %9d %9d\n",
293: nfsstats.direofcache_hits, nfsstats.direofcache_misses);
294: }
295: if (display & SHOW_SERVER) {
296: printf("\nServer Info:\n");
297: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
298: "Getattr", "Setattr", "Lookup", "Readlink", "Read",
299: "Write", "Create", "Remove");
300: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
301: nfsstats.srvrpccnt[NFSPROC_GETATTR],
302: nfsstats.srvrpccnt[NFSPROC_SETATTR],
303: nfsstats.srvrpccnt[NFSPROC_LOOKUP],
304: nfsstats.srvrpccnt[NFSPROC_READLINK],
305: nfsstats.srvrpccnt[NFSPROC_READ],
306: nfsstats.srvrpccnt[NFSPROC_WRITE],
307: nfsstats.srvrpccnt[NFSPROC_CREATE],
308: nfsstats.srvrpccnt[NFSPROC_REMOVE]);
309: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
310: "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
311: "Readdir", "RdirPlus", "Access");
312: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
313: nfsstats.srvrpccnt[NFSPROC_RENAME],
314: nfsstats.srvrpccnt[NFSPROC_LINK],
315: nfsstats.srvrpccnt[NFSPROC_SYMLINK],
316: nfsstats.srvrpccnt[NFSPROC_MKDIR],
317: nfsstats.srvrpccnt[NFSPROC_RMDIR],
318: nfsstats.srvrpccnt[NFSPROC_READDIR],
319: nfsstats.srvrpccnt[NFSPROC_READDIRPLUS],
320: nfsstats.srvrpccnt[NFSPROC_ACCESS]);
321: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
322: "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
323: "GLease", "Vacate", "Evict");
324: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
325: nfsstats.srvrpccnt[NFSPROC_MKNOD],
326: nfsstats.srvrpccnt[NFSPROC_FSSTAT],
327: nfsstats.srvrpccnt[NFSPROC_FSINFO],
328: nfsstats.srvrpccnt[NFSPROC_PATHCONF],
329: nfsstats.srvrpccnt[NFSPROC_COMMIT],
330: nfsstats.srvrpccnt[NQNFSPROC_GETLEASE],
331: nfsstats.srvrpccnt[NQNFSPROC_VACATED],
332: nfsstats.srvrpccnt[NQNFSPROC_EVICTED]);
333: printf("Server Ret-Failed\n");
334: printf("%17d\n", nfsstats.srvrpc_errs);
335: printf("Server Faults\n");
336: printf("%13d\n", nfsstats.srv_errs);
337: printf("Server Cache Stats:\n");
338: printf("%9.9s %9.9s %9.9s %9.9s\n",
339: "Inprog", "Idem", "Non-idem", "Misses");
340: printf("%9d %9d %9d %9d\n",
341: nfsstats.srvcache_inproghits,
342: nfsstats.srvcache_idemdonehits,
343: nfsstats.srvcache_nonidemdonehits,
344: nfsstats.srvcache_misses);
345: printf("Server Lease Stats:\n");
346: printf("%9.9s %9.9s %9.9s\n",
347: "Leases", "PeakL", "GLeases");
348: printf("%9d %9d %9d\n",
349: nfsstats.srvnqnfs_leases,
350: nfsstats.srvnqnfs_maxleases,
351: nfsstats.srvnqnfs_getleases);
352: printf("Server Write Gathering:\n");
353: printf("%9.9s %9.9s %9.9s\n",
354: "WriteOps", "WriteRPC", "Opsaved");
355: printf("%9d %9d %9d\n",
356: nfsstats.srvvop_writes,
357: nfsstats.srvrpccnt[NFSPROC_WRITE],
358: nfsstats.srvrpccnt[NFSPROC_WRITE] - nfsstats.srvvop_writes);
359: }
1.1 deraadt 360: }
361:
362: /*
363: * Print a running summary of nfs statistics.
364: * Repeat display every interval seconds, showing statistics
365: * collected over that interval. Assumes that interval is non-zero.
366: * First line printed at top of screen is always cumulative.
367: */
1.9 ! mickey 368: void
1.8 mickey 369: sidewaysintpr(interval, display)
1.1 deraadt 370: u_int interval;
1.4 kstailey 371: u_int display;
1.1 deraadt 372: {
373: struct nfsstats nfsstats, lastst;
374: int hdrcnt, oldmask;
375: void catchalarm();
376:
377: (void)signal(SIGALRM, catchalarm);
378: signalled = 0;
379: (void)alarm(interval);
380: bzero((caddr_t)&lastst, sizeof(lastst));
381:
382: for (hdrcnt = 1;;) {
383: if (!--hdrcnt) {
384: printhdr();
385: hdrcnt = 20;
386: }
1.8 mickey 387:
388: getnfsstats(&nfsstats);
389:
1.4 kstailey 390: if (display & SHOW_CLIENT)
391: printf("Client: %8d %8d %8d %8d %8d %8d %8d %8d\n",
1.2 niklas 392: nfsstats.rpccnt[NFSPROC_GETATTR]-lastst.rpccnt[NFSPROC_GETATTR],
393: nfsstats.rpccnt[NFSPROC_LOOKUP]-lastst.rpccnt[NFSPROC_LOOKUP],
394: nfsstats.rpccnt[NFSPROC_READLINK]-lastst.rpccnt[NFSPROC_READLINK],
395: nfsstats.rpccnt[NFSPROC_READ]-lastst.rpccnt[NFSPROC_READ],
396: nfsstats.rpccnt[NFSPROC_WRITE]-lastst.rpccnt[NFSPROC_WRITE],
397: nfsstats.rpccnt[NFSPROC_RENAME]-lastst.rpccnt[NFSPROC_RENAME],
398: nfsstats.rpccnt[NFSPROC_ACCESS]-lastst.rpccnt[NFSPROC_ACCESS],
399: (nfsstats.rpccnt[NFSPROC_READDIR]-lastst.rpccnt[NFSPROC_READDIR])
400: +(nfsstats.rpccnt[NFSPROC_READDIRPLUS]-lastst.rpccnt[NFSPROC_READDIRPLUS]));
1.4 kstailey 401: if (display & SHOW_SERVER)
402: printf("Server: %8d %8d %8d %8d %8d %8d %8d %8d\n",
1.2 niklas 403: nfsstats.srvrpccnt[NFSPROC_GETATTR]-lastst.srvrpccnt[NFSPROC_GETATTR],
404: nfsstats.srvrpccnt[NFSPROC_LOOKUP]-lastst.srvrpccnt[NFSPROC_LOOKUP],
405: nfsstats.srvrpccnt[NFSPROC_READLINK]-lastst.srvrpccnt[NFSPROC_READLINK],
406: nfsstats.srvrpccnt[NFSPROC_READ]-lastst.srvrpccnt[NFSPROC_READ],
407: nfsstats.srvrpccnt[NFSPROC_WRITE]-lastst.srvrpccnt[NFSPROC_WRITE],
408: nfsstats.srvrpccnt[NFSPROC_RENAME]-lastst.srvrpccnt[NFSPROC_RENAME],
409: nfsstats.srvrpccnt[NFSPROC_ACCESS]-lastst.srvrpccnt[NFSPROC_ACCESS],
410: (nfsstats.srvrpccnt[NFSPROC_READDIR]-lastst.srvrpccnt[NFSPROC_READDIR])
411: +(nfsstats.srvrpccnt[NFSPROC_READDIRPLUS]-lastst.srvrpccnt[NFSPROC_READDIRPLUS]));
1.1 deraadt 412: lastst = nfsstats;
413: fflush(stdout);
414: oldmask = sigblock(sigmask(SIGALRM));
415: if (!signalled)
416: sigpause(0);
417: sigsetmask(oldmask);
418: signalled = 0;
419: (void)alarm(interval);
420: }
421: /*NOTREACHED*/
422: }
423:
1.9 ! mickey 424: void
1.1 deraadt 425: printhdr()
426: {
427: printf(" %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s\n",
428: "Getattr", "Lookup", "Readlink", "Read", "Write", "Rename",
1.2 niklas 429: "Access", "Readdir");
1.1 deraadt 430: fflush(stdout);
431: }
432:
433: /*
434: * Called if an interval expires before sidewaysintpr has completed a loop.
435: * Sets a flag to not wait for the alarm.
436: */
437: void
438: catchalarm()
439: {
440: signalled = 1;
441: }
442:
1.9 ! mickey 443: void
1.1 deraadt 444: usage()
445: {
1.8 mickey 446: extern char *__progname;
1.9 ! mickey 447: fprintf(stderr,
! 448: "usage: %s [-M core] [-N system] [-s] [-c] [-w interval]\n",
! 449: __progname);
1.1 deraadt 450: exit(1);
451: }