Annotation of src/usr.bin/nfsstat/nfsstat.c, Revision 1.16
1.16 ! millert 1: /* $OpenBSD: nfsstat.c,v 1.15 2003/01/15 21:25:44 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.
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.16 ! millert 51: static char *rcsid = "$OpenBSD: nfsstat.c,v 1.15 2003/01/15 21:25:44 millert 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.12 millert 85: volatile sig_atomic_t signalled; /* set if alarm goes off "early" */
1.8 mickey 86: int nfs_id;
1.1 deraadt 87:
1.13 millert 88: void getnfsstats(struct nfsstats *);
89: void printhdr(void);
90: void intpr(u_int);
91: void sidewaysintpr(u_int, u_int);
92: void usage(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);
1.10 provos 119: if (*optarg == '\0' || *p != '\0')
1.8 mickey 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:
1.16 ! millert 164: for (; nfs_id; nfs_id--) {
1.8 mickey 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;
1.16 ! millert 178: }
! 179: if (nfs_id == 0)
1.8 mickey 180: errx(1, "cannot find nfs filesystem id");
181: }
182:
183: if (interval)
184: sidewaysintpr(interval, display);
185: else
186: intpr(display);
187:
188: return 0;
189: }
190:
191: void
192: getnfsstats(p)
193: struct nfsstats *p;
194: {
1.9 mickey 195: if (kd) {
196: if (kvm_read(kd, nl[N_NFSSTAT].n_value, p, sizeof(*p)) != sizeof(*p))
197: errx(1, "kvm_read failed");
198: } else {
199: int mib[3];
200: size_t len = sizeof(*p);
1.1 deraadt 201:
1.9 mickey 202: mib[0] = CTL_VFS;
203: mib[1] = nfs_id; /* 2 */
204: mib[2] = NFS_NFSSTATS;
1.1 deraadt 205:
1.9 mickey 206: if (sysctl(mib, 3, p, &len, NULL, 0))
207: err(1, "sysctl");
208: }
1.1 deraadt 209: }
210:
211: /*
212: * Print a description of the nfs stats.
213: */
1.9 mickey 214: void
1.8 mickey 215: intpr(display)
1.4 kstailey 216: u_int display;
1.1 deraadt 217: {
218: struct nfsstats nfsstats;
219:
1.8 mickey 220: getnfsstats(&nfsstats);
221:
1.4 kstailey 222: if (display & SHOW_CLIENT) {
223: printf("Client Info:\n");
224: printf("Rpc Counts:\n");
225: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
226: "Getattr", "Setattr", "Lookup", "Readlink", "Read",
227: "Write", "Create", "Remove");
228: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
229: nfsstats.rpccnt[NFSPROC_GETATTR],
230: nfsstats.rpccnt[NFSPROC_SETATTR],
231: nfsstats.rpccnt[NFSPROC_LOOKUP],
232: nfsstats.rpccnt[NFSPROC_READLINK],
233: nfsstats.rpccnt[NFSPROC_READ],
234: nfsstats.rpccnt[NFSPROC_WRITE],
235: nfsstats.rpccnt[NFSPROC_CREATE],
236: nfsstats.rpccnt[NFSPROC_REMOVE]);
237: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
238: "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
239: "Readdir", "RdirPlus", "Access");
240: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
241: nfsstats.rpccnt[NFSPROC_RENAME],
242: nfsstats.rpccnt[NFSPROC_LINK],
243: nfsstats.rpccnt[NFSPROC_SYMLINK],
244: nfsstats.rpccnt[NFSPROC_MKDIR],
245: nfsstats.rpccnt[NFSPROC_RMDIR],
246: nfsstats.rpccnt[NFSPROC_READDIR],
247: nfsstats.rpccnt[NFSPROC_READDIRPLUS],
248: nfsstats.rpccnt[NFSPROC_ACCESS]);
1.11 markus 249: printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
250: "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit");
251: printf("%9d %9d %9d %9d %9d\n",
1.4 kstailey 252: nfsstats.rpccnt[NFSPROC_MKNOD],
253: nfsstats.rpccnt[NFSPROC_FSSTAT],
254: nfsstats.rpccnt[NFSPROC_FSINFO],
255: nfsstats.rpccnt[NFSPROC_PATHCONF],
1.11 markus 256: nfsstats.rpccnt[NFSPROC_COMMIT]);
1.4 kstailey 257: printf("Rpc Info:\n");
258: printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
259: "TimedOut", "Invalid", "X Replies", "Retries", "Requests");
260: printf("%9d %9d %9d %9d %9d\n",
261: nfsstats.rpctimeouts,
262: nfsstats.rpcinvalid,
263: nfsstats.rpcunexpected,
264: nfsstats.rpcretries,
265: nfsstats.rpcrequests);
266: printf("Cache Info:\n");
267: printf("%9.9s %9.9s %9.9s %9.9s",
268: "Attr Hits", "Misses", "Lkup Hits", "Misses");
269: printf(" %9.9s %9.9s %9.9s %9.9s\n",
270: "BioR Hits", "Misses", "BioW Hits", "Misses");
271: printf("%9d %9d %9d %9d",
272: nfsstats.attrcache_hits, nfsstats.attrcache_misses,
273: nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
274: printf(" %9d %9d %9d %9d\n",
275: nfsstats.biocache_reads-nfsstats.read_bios,
276: nfsstats.read_bios,
277: nfsstats.biocache_writes-nfsstats.write_bios,
278: nfsstats.write_bios);
279: printf("%9.9s %9.9s %9.9s %9.9s",
280: "BioRLHits", "Misses", "BioD Hits", "Misses");
281: printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
282: printf("%9d %9d %9d %9d",
283: nfsstats.biocache_readlinks-nfsstats.readlink_bios,
284: nfsstats.readlink_bios,
285: nfsstats.biocache_readdirs-nfsstats.readdir_bios,
286: nfsstats.readdir_bios);
287: printf(" %9d %9d\n",
288: nfsstats.direofcache_hits, nfsstats.direofcache_misses);
289: }
290: if (display & SHOW_SERVER) {
291: printf("\nServer Info:\n");
292: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
293: "Getattr", "Setattr", "Lookup", "Readlink", "Read",
294: "Write", "Create", "Remove");
295: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
296: nfsstats.srvrpccnt[NFSPROC_GETATTR],
297: nfsstats.srvrpccnt[NFSPROC_SETATTR],
298: nfsstats.srvrpccnt[NFSPROC_LOOKUP],
299: nfsstats.srvrpccnt[NFSPROC_READLINK],
300: nfsstats.srvrpccnt[NFSPROC_READ],
301: nfsstats.srvrpccnt[NFSPROC_WRITE],
302: nfsstats.srvrpccnt[NFSPROC_CREATE],
303: nfsstats.srvrpccnt[NFSPROC_REMOVE]);
304: printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
305: "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
306: "Readdir", "RdirPlus", "Access");
307: printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
308: nfsstats.srvrpccnt[NFSPROC_RENAME],
309: nfsstats.srvrpccnt[NFSPROC_LINK],
310: nfsstats.srvrpccnt[NFSPROC_SYMLINK],
311: nfsstats.srvrpccnt[NFSPROC_MKDIR],
312: nfsstats.srvrpccnt[NFSPROC_RMDIR],
313: nfsstats.srvrpccnt[NFSPROC_READDIR],
314: nfsstats.srvrpccnt[NFSPROC_READDIRPLUS],
315: nfsstats.srvrpccnt[NFSPROC_ACCESS]);
1.11 markus 316: printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
317: "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit");
318: printf("%9d %9d %9d %9d %9d\n",
1.4 kstailey 319: nfsstats.srvrpccnt[NFSPROC_MKNOD],
320: nfsstats.srvrpccnt[NFSPROC_FSSTAT],
321: nfsstats.srvrpccnt[NFSPROC_FSINFO],
322: nfsstats.srvrpccnt[NFSPROC_PATHCONF],
1.11 markus 323: nfsstats.srvrpccnt[NFSPROC_COMMIT]);
1.4 kstailey 324: printf("Server Ret-Failed\n");
325: printf("%17d\n", nfsstats.srvrpc_errs);
326: printf("Server Faults\n");
327: printf("%13d\n", nfsstats.srv_errs);
328: printf("Server Cache Stats:\n");
329: printf("%9.9s %9.9s %9.9s %9.9s\n",
330: "Inprog", "Idem", "Non-idem", "Misses");
331: printf("%9d %9d %9d %9d\n",
332: nfsstats.srvcache_inproghits,
333: nfsstats.srvcache_idemdonehits,
334: nfsstats.srvcache_nonidemdonehits,
335: nfsstats.srvcache_misses);
336: printf("Server Write Gathering:\n");
337: printf("%9.9s %9.9s %9.9s\n",
338: "WriteOps", "WriteRPC", "Opsaved");
339: printf("%9d %9d %9d\n",
340: nfsstats.srvvop_writes,
341: nfsstats.srvrpccnt[NFSPROC_WRITE],
342: nfsstats.srvrpccnt[NFSPROC_WRITE] - nfsstats.srvvop_writes);
343: }
1.1 deraadt 344: }
345:
346: /*
347: * Print a running summary of nfs statistics.
348: * Repeat display every interval seconds, showing statistics
349: * collected over that interval. Assumes that interval is non-zero.
350: * First line printed at top of screen is always cumulative.
351: */
1.9 mickey 352: void
1.8 mickey 353: sidewaysintpr(interval, display)
1.1 deraadt 354: u_int interval;
1.4 kstailey 355: u_int display;
1.1 deraadt 356: {
357: struct nfsstats nfsstats, lastst;
1.12 millert 358: int hdrcnt;
359: sigset_t emptyset;
1.1 deraadt 360: void catchalarm();
361:
362: (void)signal(SIGALRM, catchalarm);
363: signalled = 0;
364: (void)alarm(interval);
365: bzero((caddr_t)&lastst, sizeof(lastst));
366:
367: for (hdrcnt = 1;;) {
368: if (!--hdrcnt) {
369: printhdr();
370: hdrcnt = 20;
371: }
1.8 mickey 372:
373: getnfsstats(&nfsstats);
374:
1.4 kstailey 375: if (display & SHOW_CLIENT)
376: printf("Client: %8d %8d %8d %8d %8d %8d %8d %8d\n",
1.2 niklas 377: nfsstats.rpccnt[NFSPROC_GETATTR]-lastst.rpccnt[NFSPROC_GETATTR],
378: nfsstats.rpccnt[NFSPROC_LOOKUP]-lastst.rpccnt[NFSPROC_LOOKUP],
379: nfsstats.rpccnt[NFSPROC_READLINK]-lastst.rpccnt[NFSPROC_READLINK],
380: nfsstats.rpccnt[NFSPROC_READ]-lastst.rpccnt[NFSPROC_READ],
381: nfsstats.rpccnt[NFSPROC_WRITE]-lastst.rpccnt[NFSPROC_WRITE],
382: nfsstats.rpccnt[NFSPROC_RENAME]-lastst.rpccnt[NFSPROC_RENAME],
383: nfsstats.rpccnt[NFSPROC_ACCESS]-lastst.rpccnt[NFSPROC_ACCESS],
384: (nfsstats.rpccnt[NFSPROC_READDIR]-lastst.rpccnt[NFSPROC_READDIR])
385: +(nfsstats.rpccnt[NFSPROC_READDIRPLUS]-lastst.rpccnt[NFSPROC_READDIRPLUS]));
1.4 kstailey 386: if (display & SHOW_SERVER)
387: printf("Server: %8d %8d %8d %8d %8d %8d %8d %8d\n",
1.2 niklas 388: nfsstats.srvrpccnt[NFSPROC_GETATTR]-lastst.srvrpccnt[NFSPROC_GETATTR],
389: nfsstats.srvrpccnt[NFSPROC_LOOKUP]-lastst.srvrpccnt[NFSPROC_LOOKUP],
390: nfsstats.srvrpccnt[NFSPROC_READLINK]-lastst.srvrpccnt[NFSPROC_READLINK],
391: nfsstats.srvrpccnt[NFSPROC_READ]-lastst.srvrpccnt[NFSPROC_READ],
392: nfsstats.srvrpccnt[NFSPROC_WRITE]-lastst.srvrpccnt[NFSPROC_WRITE],
393: nfsstats.srvrpccnt[NFSPROC_RENAME]-lastst.srvrpccnt[NFSPROC_RENAME],
394: nfsstats.srvrpccnt[NFSPROC_ACCESS]-lastst.srvrpccnt[NFSPROC_ACCESS],
395: (nfsstats.srvrpccnt[NFSPROC_READDIR]-lastst.srvrpccnt[NFSPROC_READDIR])
396: +(nfsstats.srvrpccnt[NFSPROC_READDIRPLUS]-lastst.srvrpccnt[NFSPROC_READDIRPLUS]));
1.1 deraadt 397: lastst = nfsstats;
398: fflush(stdout);
1.12 millert 399: sigemptyset(&emptyset);
1.1 deraadt 400: if (!signalled)
1.12 millert 401: sigsuspend(&emptyset);
1.1 deraadt 402: signalled = 0;
403: (void)alarm(interval);
404: }
405: /*NOTREACHED*/
406: }
407:
1.9 mickey 408: void
1.1 deraadt 409: printhdr()
410: {
411: printf(" %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s\n",
412: "Getattr", "Lookup", "Readlink", "Read", "Write", "Rename",
1.2 niklas 413: "Access", "Readdir");
1.1 deraadt 414: fflush(stdout);
415: }
416:
417: /*
418: * Called if an interval expires before sidewaysintpr has completed a loop.
419: * Sets a flag to not wait for the alarm.
420: */
421: void
422: catchalarm()
423: {
424: signalled = 1;
425: }
426:
1.9 mickey 427: void
1.1 deraadt 428: usage()
429: {
1.8 mickey 430: extern char *__progname;
1.9 mickey 431: fprintf(stderr,
432: "usage: %s [-M core] [-N system] [-s] [-c] [-w interval]\n",
433: __progname);
1.1 deraadt 434: exit(1);
435: }