amd64: Copy MPTable PCI interrput routing code from i386
[dragonfly.git] / usr.bin / nfsstat / nfsstat.c
blobe58194e511c6e009207093275743cb18443c0cca
1 /*
2 * Copyright (c) 1983, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
36 * @(#) Copyright (c) 1983, 1989, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)nfsstat.c 8.2 (Berkeley) 3/31/95
38 * $FreeBSD: src/usr.bin/nfsstat/nfsstat.c,v 1.15.2.3 2001/06/06 20:25:58 tmm Exp $
39 * $DragonFly: src/usr.bin/nfsstat/nfsstat.c,v 1.4 2004/07/16 00:55:04 hmp Exp $
42 #include <sys/param.h>
43 #include <sys/mount.h>
44 #include <sys/time.h>
45 #include <sys/sysctl.h>
46 #include <nfs/rpcv2.h>
47 #include <nfs/nfsproto.h>
48 #include <nfs/nfs.h>
49 #include <signal.h>
50 #include <fcntl.h>
51 #include <ctype.h>
52 #include <errno.h>
53 #include <kvm.h>
54 #include <limits.h>
55 #include <nlist.h>
56 #include <unistd.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <paths.h>
61 #include <err.h>
63 struct nlist nl[] = {
64 #define N_NFSSTAT 0
65 { "_nfsstats" },
66 "",
68 kvm_t *kd;
70 static int deadkernel = 0;
71 static int widemode = 0;
73 void intpr(int, int);
74 void printhdr(int, int);
75 void sidewaysintpr(u_int, int, int);
76 void usage(void);
77 char *sperc1(int, int);
78 char *sperc2(int, int);
80 #define DELTA(field) (nfsstats.field - lastst.field)
82 int
83 main(int argc, char **argv)
85 u_int interval;
86 int clientOnly = -1;
87 int serverOnly = -1;
88 int ch;
89 char *memf, *nlistf;
90 char errbuf[_POSIX2_LINE_MAX];
92 interval = 0;
93 memf = nlistf = NULL;
94 while ((ch = getopt(argc, argv, "csWM:N:w:")) != -1)
95 switch(ch) {
96 case 'M':
97 memf = optarg;
98 break;
99 case 'N':
100 nlistf = optarg;
101 break;
102 case 'W':
103 widemode = 1;
104 break;
105 case 'w':
106 interval = atoi(optarg);
107 break;
108 case 'c':
109 clientOnly = 1;
110 if (serverOnly < 0)
111 serverOnly = 0;
112 break;
113 case 's':
114 serverOnly = 1;
115 if (clientOnly < 0)
116 clientOnly = 0;
117 break;
118 case '?':
119 default:
120 usage();
122 argc -= optind;
123 argv += optind;
125 #define BACKWARD_COMPATIBILITY
126 #ifdef BACKWARD_COMPATIBILITY
127 if (*argv) {
128 interval = atoi(*argv);
129 if (*++argv) {
130 nlistf = *argv;
131 if (*++argv)
132 memf = *argv;
135 #endif
136 if (nlistf != NULL || memf != NULL) {
137 deadkernel = 1;
139 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
140 errbuf)) == 0) {
141 errx(1, "kvm_openfiles: %s", errbuf);
143 if (kvm_nlist(kd, nl) != 0) {
144 errx(1, "kvm_nlist: can't get names");
148 if (interval)
149 sidewaysintpr(interval, clientOnly, serverOnly);
150 else
151 intpr(clientOnly, serverOnly);
152 exit(0);
156 * Read the nfs stats using sysctl(3) for live kernels, or kvm_read
157 * for dead ones.
159 void
160 readstats(struct nfsstats *stp)
162 if(deadkernel) {
163 if(kvm_read(kd, (u_long)nl[N_NFSSTAT].n_value, stp,
164 sizeof *stp) < 0) {
165 err(1, "kvm_read");
167 } else {
168 int name[3];
169 size_t buflen = sizeof *stp;
170 struct vfsconf vfc;
172 if (getvfsbyname("nfs", &vfc) < 0)
173 err(1, "getvfsbyname: NFS not compiled into kernel");
174 name[0] = CTL_VFS;
175 name[1] = vfc.vfc_typenum;
176 name[2] = NFS_NFSSTATS;
177 if (sysctl(name, 3, stp, &buflen, NULL, (size_t)0) < 0) {
178 err(1, "sysctl");
184 * Print a description of the nfs stats.
186 void
187 intpr(int clientOnly, int serverOnly)
189 struct nfsstats nfsstats;
191 readstats(&nfsstats);
193 if (clientOnly) {
194 printf("Client Info:\n");
195 printf("Rpc Counts:\n");
196 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
197 "Getattr", "Setattr", "Lookup", "Readlink", "Read",
198 "Write", "Create", "Remove");
199 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
200 nfsstats.rpccnt[NFSPROC_GETATTR],
201 nfsstats.rpccnt[NFSPROC_SETATTR],
202 nfsstats.rpccnt[NFSPROC_LOOKUP],
203 nfsstats.rpccnt[NFSPROC_READLINK],
204 nfsstats.rpccnt[NFSPROC_READ],
205 nfsstats.rpccnt[NFSPROC_WRITE],
206 nfsstats.rpccnt[NFSPROC_CREATE],
207 nfsstats.rpccnt[NFSPROC_REMOVE]);
208 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
209 "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
210 "Readdir", "RdirPlus", "Access");
211 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
212 nfsstats.rpccnt[NFSPROC_RENAME],
213 nfsstats.rpccnt[NFSPROC_LINK],
214 nfsstats.rpccnt[NFSPROC_SYMLINK],
215 nfsstats.rpccnt[NFSPROC_MKDIR],
216 nfsstats.rpccnt[NFSPROC_RMDIR],
217 nfsstats.rpccnt[NFSPROC_READDIR],
218 nfsstats.rpccnt[NFSPROC_READDIRPLUS],
219 nfsstats.rpccnt[NFSPROC_ACCESS]);
220 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
221 "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
222 "GLease", "Vacate", "Evict");
223 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
224 nfsstats.rpccnt[NFSPROC_MKNOD],
225 nfsstats.rpccnt[NFSPROC_FSSTAT],
226 nfsstats.rpccnt[NFSPROC_FSINFO],
227 nfsstats.rpccnt[NFSPROC_PATHCONF],
228 nfsstats.rpccnt[NFSPROC_COMMIT],
229 nfsstats.rpccnt[NQNFSPROC_GETLEASE],
230 nfsstats.rpccnt[NQNFSPROC_VACATED],
231 nfsstats.rpccnt[NQNFSPROC_EVICTED]);
232 printf("Rpc Info:\n");
233 printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
234 "TimedOut", "Invalid", "X Replies", "Retries",
235 "Requests");
236 printf("%9d %9d %9d %9d %9d\n",
237 nfsstats.rpctimeouts,
238 nfsstats.rpcinvalid,
239 nfsstats.rpcunexpected,
240 nfsstats.rpcretries,
241 nfsstats.rpcrequests);
242 printf("Cache Info:\n");
243 printf("%9.9s %9.9s %9.9s %9.9s",
244 "Attr Hits", "Misses", "Lkup Hits", "Misses");
245 printf(" %9.9s %9.9s %9.9s %9.9s\n",
246 "BioR Hits", "Misses", "BioW Hits", "Misses");
247 printf("%9d %9d %9d %9d",
248 nfsstats.attrcache_hits, nfsstats.attrcache_misses,
249 nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
250 printf(" %9d %9d %9d %9d\n",
251 nfsstats.biocache_reads-nfsstats.read_bios,
252 nfsstats.read_bios,
253 nfsstats.biocache_writes-nfsstats.write_bios,
254 nfsstats.write_bios);
255 printf("%9.9s %9.9s %9.9s %9.9s",
256 "BioRLHits", "Misses", "BioD Hits", "Misses");
257 printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
258 printf("%9d %9d %9d %9d",
259 nfsstats.biocache_readlinks-nfsstats.readlink_bios,
260 nfsstats.readlink_bios,
261 nfsstats.biocache_readdirs-nfsstats.readdir_bios,
262 nfsstats.readdir_bios);
263 printf(" %9d %9d\n",
264 nfsstats.direofcache_hits, nfsstats.direofcache_misses);
266 if (serverOnly) {
267 printf("\nServer Info:\n");
268 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
269 "Getattr", "Setattr", "Lookup", "Readlink", "Read",
270 "Write", "Create", "Remove");
271 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
272 nfsstats.srvrpccnt[NFSPROC_GETATTR],
273 nfsstats.srvrpccnt[NFSPROC_SETATTR],
274 nfsstats.srvrpccnt[NFSPROC_LOOKUP],
275 nfsstats.srvrpccnt[NFSPROC_READLINK],
276 nfsstats.srvrpccnt[NFSPROC_READ],
277 nfsstats.srvrpccnt[NFSPROC_WRITE],
278 nfsstats.srvrpccnt[NFSPROC_CREATE],
279 nfsstats.srvrpccnt[NFSPROC_REMOVE]);
280 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
281 "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
282 "Readdir", "RdirPlus", "Access");
283 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
284 nfsstats.srvrpccnt[NFSPROC_RENAME],
285 nfsstats.srvrpccnt[NFSPROC_LINK],
286 nfsstats.srvrpccnt[NFSPROC_SYMLINK],
287 nfsstats.srvrpccnt[NFSPROC_MKDIR],
288 nfsstats.srvrpccnt[NFSPROC_RMDIR],
289 nfsstats.srvrpccnt[NFSPROC_READDIR],
290 nfsstats.srvrpccnt[NFSPROC_READDIRPLUS],
291 nfsstats.srvrpccnt[NFSPROC_ACCESS]);
292 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
293 "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
294 "GLease", "Vacate", "Evict");
295 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
296 nfsstats.srvrpccnt[NFSPROC_MKNOD],
297 nfsstats.srvrpccnt[NFSPROC_FSSTAT],
298 nfsstats.srvrpccnt[NFSPROC_FSINFO],
299 nfsstats.srvrpccnt[NFSPROC_PATHCONF],
300 nfsstats.srvrpccnt[NFSPROC_COMMIT],
301 nfsstats.srvrpccnt[NQNFSPROC_GETLEASE],
302 nfsstats.srvrpccnt[NQNFSPROC_VACATED],
303 nfsstats.srvrpccnt[NQNFSPROC_EVICTED]);
304 printf("Server Ret-Failed\n");
305 printf("%17d\n", nfsstats.srvrpc_errs);
306 printf("Server Faults\n");
307 printf("%13d\n", nfsstats.srv_errs);
308 printf("Server Cache Stats:\n");
309 printf("%9.9s %9.9s %9.9s %9.9s\n",
310 "Inprog", "Idem", "Non-idem", "Misses");
311 printf("%9d %9d %9d %9d\n",
312 nfsstats.srvcache_inproghits,
313 nfsstats.srvcache_idemdonehits,
314 nfsstats.srvcache_nonidemdonehits,
315 nfsstats.srvcache_misses);
316 printf("Server Lease Stats:\n");
317 printf("%9.9s %9.9s %9.9s\n",
318 "Leases", "PeakL", "GLeases");
319 printf("%9d %9d %9d\n",
320 nfsstats.srvnqnfs_leases,
321 nfsstats.srvnqnfs_maxleases,
322 nfsstats.srvnqnfs_getleases);
323 printf("Server Write Gathering:\n");
324 printf("%9.9s %9.9s %9.9s\n",
325 "WriteOps", "WriteRPC", "Opsaved");
326 printf("%9d %9d %9d\n",
327 nfsstats.srvvop_writes,
328 nfsstats.srvrpccnt[NFSPROC_WRITE],
329 nfsstats.srvrpccnt[NFSPROC_WRITE] -
330 nfsstats.srvvop_writes);
334 u_char signalled; /* set if alarm goes off "early" */
337 * Print a running summary of nfs statistics.
338 * Repeat display every interval seconds, showing statistics
339 * collected over that interval. Assumes that interval is non-zero.
340 * First line printed at top of screen is always cumulative.
342 void
343 sidewaysintpr(u_int interval, int clientOnly, int serverOnly)
345 struct nfsstats nfsstats, lastst;
346 int hdrcnt = 1;
348 readstats(&lastst);
349 sleep(interval);
351 for (;;) {
352 readstats(&nfsstats);
354 if (--hdrcnt == 0) {
355 printhdr(clientOnly, serverOnly);
356 if (clientOnly && serverOnly)
357 hdrcnt = 10;
358 else
359 hdrcnt = 20;
361 if (clientOnly) {
362 printf("%s %6d %6d %6d %6d %6d %6d %6d %6d",
363 ((clientOnly && serverOnly) ? "Client:" : ""),
364 DELTA(attrcache_hits) + DELTA(attrcache_misses),
365 DELTA(lookupcache_hits) + DELTA(lookupcache_misses),
366 DELTA(biocache_readlinks),
367 DELTA(biocache_reads),
368 DELTA(biocache_writes),
369 nfsstats.rpccnt[NFSPROC_RENAME]-lastst.rpccnt[NFSPROC_RENAME],
370 DELTA(accesscache_hits) + DELTA(accesscache_misses),
371 DELTA(biocache_readdirs)
373 if (widemode) {
374 printf(" %s %s %s %s %s %s",
375 sperc1(DELTA(attrcache_hits),
376 DELTA(attrcache_misses)),
377 sperc1(DELTA(lookupcache_hits),
378 DELTA(lookupcache_misses)),
379 sperc2(DELTA(biocache_reads),
380 DELTA(read_bios)),
381 sperc2(DELTA(biocache_writes),
382 DELTA(write_bios)),
383 sperc1(DELTA(accesscache_hits),
384 DELTA(accesscache_misses)),
385 sperc2(DELTA(biocache_readdirs),
386 DELTA(readdir_bios))
389 printf("\n");
391 if (serverOnly) {
392 printf("%s %6d %6d %6d %6d %6d %6d %6d %6d",
393 ((clientOnly && serverOnly) ? "Server:" : ""),
394 nfsstats.srvrpccnt[NFSPROC_GETATTR]-lastst.srvrpccnt[NFSPROC_GETATTR],
395 nfsstats.srvrpccnt[NFSPROC_LOOKUP]-lastst.srvrpccnt[NFSPROC_LOOKUP],
396 nfsstats.srvrpccnt[NFSPROC_READLINK]-lastst.srvrpccnt[NFSPROC_READLINK],
397 nfsstats.srvrpccnt[NFSPROC_READ]-lastst.srvrpccnt[NFSPROC_READ],
398 nfsstats.srvrpccnt[NFSPROC_WRITE]-lastst.srvrpccnt[NFSPROC_WRITE],
399 nfsstats.srvrpccnt[NFSPROC_RENAME]-lastst.srvrpccnt[NFSPROC_RENAME],
400 nfsstats.srvrpccnt[NFSPROC_ACCESS]-lastst.srvrpccnt[NFSPROC_ACCESS],
401 (nfsstats.srvrpccnt[NFSPROC_READDIR]-lastst.srvrpccnt[NFSPROC_READDIR])
402 +(nfsstats.srvrpccnt[NFSPROC_READDIRPLUS]-lastst.srvrpccnt[NFSPROC_READDIRPLUS]));
403 printf("\n");
405 lastst = nfsstats;
406 fflush(stdout);
407 sleep(interval);
409 /*NOTREACHED*/
412 void
413 printhdr(int clientOnly, int serverOnly)
415 printf("%s%6.6s %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s",
416 ((serverOnly && clientOnly) ? " " : " "),
417 "GtAttr", "Lookup", "Rdlink", "Read", "Write", "Rename",
418 "Access", "Rddir");
419 if (widemode && clientOnly) {
420 printf(" Attr Lkup BioR BioW Accs BioD");
422 printf("\n");
423 fflush(stdout);
426 void
427 usage(void)
429 (void)fprintf(stderr,
430 "usage: nfsstat [-csW] [-M core] [-N system] [-w interval]\n");
431 exit(1);
434 static char SPBuf[64][8];
435 static int SPIndex;
437 char *
438 sperc1(int hits, int misses)
440 char *p = SPBuf[SPIndex];
442 if (hits + misses) {
443 sprintf(p, "%3d%%",
444 (int)(char)((quad_t)hits * 100 / (hits + misses)));
445 } else {
446 sprintf(p, " -");
448 SPIndex = (SPIndex + 1) & 63;
449 return(p);
452 char *
453 sperc2(int ttl, int misses)
455 char *p = SPBuf[SPIndex];
457 if (ttl) {
458 sprintf(p, "%3d%%",
459 (int)(char)((quad_t)(ttl - misses) * 100 / ttl));
460 } else {
461 sprintf(p, " -");
463 SPIndex = (SPIndex + 1) & 63;
464 return(p);