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