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