Annotation of src/usr.bin/nfsstat/nfsstat.c, Revision 1.36
1.36 ! guenther 1: /* $OpenBSD: nfsstat.c,v 1.35 2015/10/23 08:18:30 tedu 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.
1.18 millert 19: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
1.34 deraadt 36: #include <sys/types.h>
1.1 deraadt 37: #include <sys/mount.h>
1.2 niklas 38: #include <sys/sysctl.h>
1.36 ! guenther 39: #include <sys/time.h>
1.2 niklas 40: #include <nfs/rpcv2.h>
41: #include <nfs/nfsproto.h>
1.1 deraadt 42: #include <nfs/nfs.h>
43: #include <signal.h>
1.9 mickey 44: #include <fcntl.h>
45: #include <ctype.h>
1.1 deraadt 46: #include <errno.h>
1.9 mickey 47: #include <kvm.h>
48: #include <nlist.h>
1.1 deraadt 49: #include <unistd.h>
50: #include <stdio.h>
51: #include <stdlib.h>
52: #include <string.h>
1.9 mickey 53: #include <limits.h>
54: #include <paths.h>
1.2 niklas 55: #include <err.h>
1.1 deraadt 56:
1.4 kstailey 57: #define SHOW_SERVER 0x01
58: #define SHOW_CLIENT 0x02
59: #define SHOW_ALL (SHOW_SERVER | SHOW_CLIENT)
60:
1.9 mickey 61: struct nlist nl[] = {
62: #define N_NFSSTAT 0
63: { "_nfsstats" },
64: { "" },
65: };
66: kvm_t *kd;
1.12 millert 67: volatile sig_atomic_t signalled; /* set if alarm goes off "early" */
1.8 mickey 68: int nfs_id;
1.1 deraadt 69:
1.13 millert 70: void getnfsstats(struct nfsstats *);
71: void printhdr(void);
72: void intpr(u_int);
73: void sidewaysintpr(u_int, u_int);
74: void usage(void);
1.21 deraadt 75: void catchalarm(int);
1.1 deraadt 76:
1.8 mickey 77: int
1.19 deraadt 78: main(int argc, char *argv[])
1.1 deraadt 79: {
1.29 deraadt 80: u_int interval, display = SHOW_ALL;
1.1 deraadt 81: extern int optind;
82: extern char *optarg;
1.9 mickey 83: char *memf, *nlistf;
1.29 deraadt 84: const char *errstr;
1.1 deraadt 85: int ch;
86:
87: interval = 0;
1.9 mickey 88: memf = nlistf = NULL;
1.22 tedu 89: while ((ch = getopt(argc, argv, "cM:N:sw:")) != -1)
1.1 deraadt 90: switch(ch) {
91: case 'M':
1.9 mickey 92: memf = optarg;
93: break;
1.1 deraadt 94: case 'N':
1.9 mickey 95: nlistf = optarg;
1.1 deraadt 96: break;
97: case 'w':
1.29 deraadt 98: interval = (u_int)strtonum(optarg, 0, 1000, &errstr);
99: if (errstr)
100: errx(1, "invalid interval %s: %s",
101: optarg, errstr);
1.1 deraadt 102: break;
1.4 kstailey 103: case 's':
104: display = SHOW_SERVER;
105: break;
106: case 'c':
107: display = SHOW_CLIENT;
108: break;
1.1 deraadt 109: case '?':
110: default:
111: usage();
112: }
113: argc -= optind;
114: argv += optind;
115:
1.35 tedu 116: if (argc) {
1.29 deraadt 117: interval = (u_int)strtonum(*argv, 0, 1000, &errstr);
118: if (errstr)
119: errx(1, "invalid interval %s: %s", *argv, errstr);
1.35 tedu 120: ++argv;
121: --argc;
1.9 mickey 122: }
123: if (nlistf || memf) {
124: char errbuf[_POSIX2_LINE_MAX];
125:
126: if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == 0)
127: errx(1, "nfsstat: %s", errbuf);
128: if (kvm_nlist(kd, nl) != 0)
129: errx(1, "kvm_nlist: can't get names");
130: } else {
1.17 millert 131: int mib[4];
1.8 mickey 132: size_t len;
133:
134: mib[0] = CTL_VFS;
135: mib[1] = VFS_GENERIC;
136: mib[2] = VFS_MAXTYPENUM;
137: len = sizeof(nfs_id);
138: if (sysctl(mib, 3, &nfs_id, &len, NULL, 0))
139: err(1, "sysctl: VFS_MAXTYPENUM");
140:
1.16 millert 141: for (; nfs_id; nfs_id--) {
1.8 mickey 142: struct vfsconf vfsc;
143:
144: mib[0] = CTL_VFS;
145: mib[1] = VFS_GENERIC;
146: mib[2] = VFS_CONF;
147: mib[3] = nfs_id;
148:
149: len = sizeof(vfsc);
150: if (sysctl(mib, 4, &vfsc, &len, NULL, 0))
151: continue;
152:
153: if (!strcmp(vfsc.vfc_name, MOUNT_NFS))
154: break;
1.16 millert 155: }
156: if (nfs_id == 0)
1.8 mickey 157: errx(1, "cannot find nfs filesystem id");
158: }
159:
160: if (interval)
161: sidewaysintpr(interval, display);
162: else
163: intpr(display);
164:
165: return 0;
166: }
167:
168: void
1.19 deraadt 169: getnfsstats(struct nfsstats *p)
1.8 mickey 170: {
1.9 mickey 171: if (kd) {
172: if (kvm_read(kd, nl[N_NFSSTAT].n_value, p, sizeof(*p)) != sizeof(*p))
173: errx(1, "kvm_read failed");
174: } else {
175: int mib[3];
176: size_t len = sizeof(*p);
1.1 deraadt 177:
1.9 mickey 178: mib[0] = CTL_VFS;
179: mib[1] = nfs_id; /* 2 */
180: mib[2] = NFS_NFSSTATS;
1.1 deraadt 181:
1.9 mickey 182: if (sysctl(mib, 3, p, &len, NULL, 0))
183: err(1, "sysctl");
184: }
1.1 deraadt 185: }
186:
187: /*
188: * Print a description of the nfs stats.
189: */
1.9 mickey 190: void
1.19 deraadt 191: intpr(u_int display)
1.1 deraadt 192: {
193: struct nfsstats nfsstats;
194:
1.8 mickey 195: getnfsstats(&nfsstats);
196:
1.4 kstailey 197: if (display & SHOW_CLIENT) {
198: printf("Client Info:\n");
199: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
1.23 deraadt 200: "Getattr", "Setattr", "Lookup", "Readlink", "Read",
201: "Write", "Create", "Remove");
1.28 millert 202: printf("%9llu %9llu %9llu %9llu %9llu %9llu %9llu %9llu\n",
1.23 deraadt 203: nfsstats.rpccnt[NFSPROC_GETATTR],
204: nfsstats.rpccnt[NFSPROC_SETATTR],
205: nfsstats.rpccnt[NFSPROC_LOOKUP],
206: nfsstats.rpccnt[NFSPROC_READLINK],
207: nfsstats.rpccnt[NFSPROC_READ],
208: nfsstats.rpccnt[NFSPROC_WRITE],
209: nfsstats.rpccnt[NFSPROC_CREATE],
210: nfsstats.rpccnt[NFSPROC_REMOVE]);
1.4 kstailey 211: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
1.23 deraadt 212: "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
213: "Readdir", "RdirPlus", "Access");
1.28 millert 214: printf("%9llu %9llu %9llu %9llu %9llu %9llu %9llu %9llu\n",
1.23 deraadt 215: nfsstats.rpccnt[NFSPROC_RENAME],
216: nfsstats.rpccnt[NFSPROC_LINK],
217: nfsstats.rpccnt[NFSPROC_SYMLINK],
218: nfsstats.rpccnt[NFSPROC_MKDIR],
219: nfsstats.rpccnt[NFSPROC_RMDIR],
220: nfsstats.rpccnt[NFSPROC_READDIR],
221: nfsstats.rpccnt[NFSPROC_READDIRPLUS],
222: nfsstats.rpccnt[NFSPROC_ACCESS]);
1.11 markus 223: printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
1.23 deraadt 224: "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit");
1.28 millert 225: printf("%9llu %9llu %9llu %9llu %9llu\n",
1.23 deraadt 226: nfsstats.rpccnt[NFSPROC_MKNOD],
227: nfsstats.rpccnt[NFSPROC_FSSTAT],
228: nfsstats.rpccnt[NFSPROC_FSINFO],
229: nfsstats.rpccnt[NFSPROC_PATHCONF],
230: nfsstats.rpccnt[NFSPROC_COMMIT]);
1.4 kstailey 231: printf("Rpc Info:\n");
1.30 thib 232: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
233: "TimedOut", "Invalid", "X Replies", "Retries", "Requests",
234: "FrcSync");
235: printf("%9llu %9llu %9llu %9llu %9llu %9llu\n",
1.23 deraadt 236: nfsstats.rpctimeouts,
237: nfsstats.rpcinvalid,
238: nfsstats.rpcunexpected,
239: nfsstats.rpcretries,
1.30 thib 240: nfsstats.rpcrequests,
241: nfsstats.forcedsync);
1.4 kstailey 242: printf("Cache Info:\n");
243: printf("%9.9s %9.9s %9.9s %9.9s",
1.23 deraadt 244: "Attr Hits", "Misses", "Lkup Hits", "Misses");
1.4 kstailey 245: printf(" %9.9s %9.9s %9.9s %9.9s\n",
1.23 deraadt 246: "BioR Hits", "Misses", "BioW Hits", "Misses");
1.28 millert 247: printf("%9llu %9llu %9llu %9llu",
1.23 deraadt 248: nfsstats.attrcache_hits, nfsstats.attrcache_misses,
249: nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
1.28 millert 250: printf(" %9llu %9llu %9llu %9llu\n",
1.23 deraadt 251: nfsstats.biocache_reads-nfsstats.read_bios,
252: nfsstats.read_bios,
253: nfsstats.biocache_writes-nfsstats.write_bios,
254: nfsstats.write_bios);
1.4 kstailey 255: printf("%9.9s %9.9s %9.9s %9.9s",
1.23 deraadt 256: "BioRLHits", "Misses", "BioD Hits", "Misses");
1.4 kstailey 257: printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
1.28 millert 258: printf("%9llu %9llu %9llu %9llu",
1.23 deraadt 259: nfsstats.biocache_readlinks-nfsstats.readlink_bios,
260: nfsstats.readlink_bios,
261: nfsstats.biocache_readdirs-nfsstats.readdir_bios,
262: nfsstats.readdir_bios);
1.28 millert 263: printf(" %9llu %9llu\n",
1.23 deraadt 264: nfsstats.direofcache_hits, nfsstats.direofcache_misses);
1.4 kstailey 265: }
1.25 pedro 266:
267: if (display == SHOW_ALL)
268: printf("\n");
269:
1.4 kstailey 270: if (display & SHOW_SERVER) {
1.25 pedro 271: printf("Server Info:\n");
1.4 kstailey 272: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
1.23 deraadt 273: "Getattr", "Setattr", "Lookup", "Readlink", "Read",
274: "Write", "Create", "Remove");
1.28 millert 275: printf("%9llu %9llu %9llu %9llu %9llu %9llu %9llu %9llu\n",
1.23 deraadt 276: nfsstats.srvrpccnt[NFSPROC_GETATTR],
277: nfsstats.srvrpccnt[NFSPROC_SETATTR],
278: nfsstats.srvrpccnt[NFSPROC_LOOKUP],
279: nfsstats.srvrpccnt[NFSPROC_READLINK],
280: nfsstats.srvrpccnt[NFSPROC_READ],
281: nfsstats.srvrpccnt[NFSPROC_WRITE],
282: nfsstats.srvrpccnt[NFSPROC_CREATE],
283: nfsstats.srvrpccnt[NFSPROC_REMOVE]);
1.4 kstailey 284: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
1.23 deraadt 285: "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
286: "Readdir", "RdirPlus", "Access");
1.28 millert 287: printf("%9llu %9llu %9llu %9llu %9llu %9llu %9llu %9llu\n",
1.23 deraadt 288: nfsstats.srvrpccnt[NFSPROC_RENAME],
289: nfsstats.srvrpccnt[NFSPROC_LINK],
290: nfsstats.srvrpccnt[NFSPROC_SYMLINK],
291: nfsstats.srvrpccnt[NFSPROC_MKDIR],
292: nfsstats.srvrpccnt[NFSPROC_RMDIR],
293: nfsstats.srvrpccnt[NFSPROC_READDIR],
294: nfsstats.srvrpccnt[NFSPROC_READDIRPLUS],
295: nfsstats.srvrpccnt[NFSPROC_ACCESS]);
1.11 markus 296: printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
1.23 deraadt 297: "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit");
1.28 millert 298: printf("%9llu %9llu %9llu %9llu %9llu\n",
1.23 deraadt 299: nfsstats.srvrpccnt[NFSPROC_MKNOD],
300: nfsstats.srvrpccnt[NFSPROC_FSSTAT],
301: nfsstats.srvrpccnt[NFSPROC_FSINFO],
302: nfsstats.srvrpccnt[NFSPROC_PATHCONF],
303: nfsstats.srvrpccnt[NFSPROC_COMMIT]);
1.4 kstailey 304: printf("Server Ret-Failed\n");
1.28 millert 305: printf("%17llu\n", nfsstats.srvrpc_errs);
1.4 kstailey 306: printf("Server Faults\n");
1.28 millert 307: printf("%13llu\n", nfsstats.srv_errs);
1.4 kstailey 308: printf("Server Cache Stats:\n");
309: printf("%9.9s %9.9s %9.9s %9.9s\n",
1.23 deraadt 310: "Inprog", "Idem", "Non-idem", "Misses");
1.28 millert 311: printf("%9llu %9llu %9llu %9llu\n",
1.23 deraadt 312: nfsstats.srvcache_inproghits,
313: nfsstats.srvcache_idemdonehits,
314: nfsstats.srvcache_nonidemdonehits,
315: nfsstats.srvcache_misses);
1.4 kstailey 316: printf("Server Write Gathering:\n");
317: printf("%9.9s %9.9s %9.9s\n",
1.23 deraadt 318: "WriteOps", "WriteRPC", "Opsaved");
1.28 millert 319: printf("%9llu %9llu %9llu\n",
1.23 deraadt 320: nfsstats.srvvop_writes,
321: nfsstats.srvrpccnt[NFSPROC_WRITE],
322: nfsstats.srvrpccnt[NFSPROC_WRITE] - nfsstats.srvvop_writes);
1.4 kstailey 323: }
1.1 deraadt 324: }
325:
326: /*
327: * Print a running summary of nfs statistics.
328: * Repeat display every interval seconds, showing statistics
329: * collected over that interval. Assumes that interval is non-zero.
330: * First line printed at top of screen is always cumulative.
331: */
1.9 mickey 332: void
1.19 deraadt 333: sidewaysintpr(u_int interval, u_int display)
1.1 deraadt 334: {
335: struct nfsstats nfsstats, lastst;
1.12 millert 336: int hdrcnt;
337: sigset_t emptyset;
1.1 deraadt 338:
339: (void)signal(SIGALRM, catchalarm);
340: signalled = 0;
341: (void)alarm(interval);
1.29 deraadt 342: bzero(&lastst, sizeof(lastst));
1.1 deraadt 343:
344: for (hdrcnt = 1;;) {
345: if (!--hdrcnt) {
346: printhdr();
347: hdrcnt = 20;
348: }
1.8 mickey 349:
350: getnfsstats(&nfsstats);
351:
1.4 kstailey 352: if (display & SHOW_CLIENT)
1.27 deraadt 353: printf("Client: "
354: "%8llu %8llu %8llu %8llu %8llu %8llu %8llu %8llu\n",
355: nfsstats.rpccnt[NFSPROC_GETATTR] -
356: lastst.rpccnt[NFSPROC_GETATTR],
357: nfsstats.rpccnt[NFSPROC_LOOKUP] -
358: lastst.rpccnt[NFSPROC_LOOKUP],
359: nfsstats.rpccnt[NFSPROC_READLINK] -
360: lastst.rpccnt[NFSPROC_READLINK],
361: nfsstats.rpccnt[NFSPROC_READ] -
362: lastst.rpccnt[NFSPROC_READ],
363: nfsstats.rpccnt[NFSPROC_WRITE] -
364: lastst.rpccnt[NFSPROC_WRITE],
365: nfsstats.rpccnt[NFSPROC_RENAME] -
366: lastst.rpccnt[NFSPROC_RENAME],
367: nfsstats.rpccnt[NFSPROC_ACCESS] -
368: lastst.rpccnt[NFSPROC_ACCESS],
369: (nfsstats.rpccnt[NFSPROC_READDIR] -
370: lastst.rpccnt[NFSPROC_READDIR]) +
371: (nfsstats.rpccnt[NFSPROC_READDIRPLUS] -
372: lastst.rpccnt[NFSPROC_READDIRPLUS]));
1.4 kstailey 373: if (display & SHOW_SERVER)
1.27 deraadt 374: printf("Server: "
375: "%8llu %8llu %8llu %8llu %8llu %8llu %8llu %8llu\n",
376: nfsstats.srvrpccnt[NFSPROC_GETATTR] -
377: lastst.srvrpccnt[NFSPROC_GETATTR],
378: nfsstats.srvrpccnt[NFSPROC_LOOKUP] -
379: lastst.srvrpccnt[NFSPROC_LOOKUP],
380: nfsstats.srvrpccnt[NFSPROC_READLINK] -
381: lastst.srvrpccnt[NFSPROC_READLINK],
382: nfsstats.srvrpccnt[NFSPROC_READ] -
383: lastst.srvrpccnt[NFSPROC_READ],
384: nfsstats.srvrpccnt[NFSPROC_WRITE] -
385: lastst.srvrpccnt[NFSPROC_WRITE],
386: nfsstats.srvrpccnt[NFSPROC_RENAME] -
387: lastst.srvrpccnt[NFSPROC_RENAME],
388: nfsstats.srvrpccnt[NFSPROC_ACCESS] -
389: lastst.srvrpccnt[NFSPROC_ACCESS],
390: (nfsstats.srvrpccnt[NFSPROC_READDIR] -
391: lastst.srvrpccnt[NFSPROC_READDIR]) +
392: (nfsstats.srvrpccnt[NFSPROC_READDIRPLUS] -
393: lastst.srvrpccnt[NFSPROC_READDIRPLUS]));
1.1 deraadt 394: lastst = nfsstats;
395: fflush(stdout);
1.12 millert 396: sigemptyset(&emptyset);
1.1 deraadt 397: if (!signalled)
1.12 millert 398: sigsuspend(&emptyset);
1.1 deraadt 399: signalled = 0;
400: (void)alarm(interval);
401: }
402: /*NOTREACHED*/
403: }
404:
1.9 mickey 405: void
1.19 deraadt 406: printhdr(void)
1.1 deraadt 407: {
408: printf(" %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s\n",
409: "Getattr", "Lookup", "Readlink", "Read", "Write", "Rename",
1.2 niklas 410: "Access", "Readdir");
1.1 deraadt 411: fflush(stdout);
412: }
413:
414: /*
415: * Called if an interval expires before sidewaysintpr has completed a loop.
416: * Sets a flag to not wait for the alarm.
417: */
1.24 deraadt 418: /* ARGSUSED */
1.1 deraadt 419: void
1.20 deraadt 420: catchalarm(int signo)
1.1 deraadt 421: {
422: signalled = 1;
423: }
424:
1.9 mickey 425: void
1.19 deraadt 426: usage(void)
1.1 deraadt 427: {
1.8 mickey 428: extern char *__progname;
1.22 tedu 429:
1.31 sobrado 430: fprintf(stderr, "usage: %s [-cs] [-M core] [-N system] [-w wait]\n",
1.9 mickey 431: __progname);
1.1 deraadt 432: exit(1);
433: }