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