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