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