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
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
38 static char copyright[] =
39 "@(#) Copyright (c) 1983, 1989, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)nfsstat.c 8.2 (Berkeley) 3/31/95";
47 static const char rcsid[] =
51 #include <sys/param.h>
52 #include <sys/mount.h>
54 #include <sys/sysctl.h>
55 #include <nfs/rpcv2.h>
56 #include <nfs/nfsproto.h>
57 #include <nfsclient/nfs.h>
58 #include <nfsserver/nfs.h>
82 static int deadkernel = 0;
83 static int widemode = 0;
87 void printhdr(int, int);
88 void sidewaysintpr(u_int, int, int);
90 char *sperc1(int, int);
91 char *sperc2(int, int);
93 #define DELTA(field) (nfsstats.field - lastst.field)
96 main(int argc, char **argv)
103 char errbuf[_POSIX2_LINE_MAX];
106 memf = nlistf = NULL;
107 while ((ch = getopt(argc, argv, "csWM:N:w:z")) != -1)
119 interval = atoi(optarg);
141 #define BACKWARD_COMPATIBILITY
142 #ifdef BACKWARD_COMPATIBILITY
144 interval = atoi(*argv);
152 if (nlistf != NULL || memf != NULL) {
155 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
157 errx(1, "kvm_openfiles: %s", errbuf);
159 if (kvm_nlist(kd, nl) != 0) {
160 errx(1, "kvm_nlist: can't get names");
165 sidewaysintpr(interval, clientOnly, serverOnly);
167 intpr(clientOnly, serverOnly);
172 * Read the nfs stats using sysctl(3) for live kernels, or kvm_read
176 readstats(struct nfsstats **stp, struct nfsrvstats **srvstp, int zero)
179 struct nfsstats client;
180 struct nfsrvstats server;
185 if (*stp != NULL && kvm_read(kd, (u_long)nl[N_NFSSTAT].n_value,
186 *stp, sizeof(struct nfsstats)) < 0) {
189 if (*srvstp != NULL && kvm_read(kd,
190 (u_long)nl[N_NFSRVSTAT].n_value, *srvstp,
191 sizeof(struct nfsrvstats)) < 0) {
196 bzero(&zerostat, sizeof(zerostat));
197 buflen = sizeof(struct nfsstats);
198 if (*stp != NULL && sysctlbyname("vfs.nfs.nfsstats", *stp,
199 &buflen, zero ? &zerostat : NULL, zero ? buflen : 0) < 0) {
201 err(1, "sysctl: vfs.nfs.nfsstats");
204 buflen = sizeof(struct nfsrvstats);
205 if (*srvstp != NULL && sysctlbyname("vfs.nfsrv.nfsrvstats",
206 *srvstp, &buflen, zero ? &zerostat : NULL,
207 zero ? buflen : 0) < 0) {
209 err(1, "sysctl: vfs.nfsrv.nfsrvstats");
216 * Print a description of the nfs stats.
219 intpr(int clientOnly, int serverOnly)
221 struct nfsstats nfsstats, *nfsstatsp;
222 struct nfsrvstats nfsrvstats, *nfsrvstatsp;
225 * Only read the stats we are going to display to avoid zeroing
226 * stats the user didn't request.
229 nfsstatsp = &nfsstats;
233 nfsrvstatsp = &nfsrvstats;
237 readstats(&nfsstatsp, &nfsrvstatsp, zflag);
239 if (clientOnly && !nfsstatsp) {
240 printf("Client not present!\n");
244 printf("Client Info:\n");
245 printf("Rpc Counts:\n");
246 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
247 "Getattr", "Setattr", "Lookup", "Readlink", "Read",
248 "Write", "Create", "Remove");
249 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
250 nfsstats.rpccnt[NFSPROC_GETATTR],
251 nfsstats.rpccnt[NFSPROC_SETATTR],
252 nfsstats.rpccnt[NFSPROC_LOOKUP],
253 nfsstats.rpccnt[NFSPROC_READLINK],
254 nfsstats.rpccnt[NFSPROC_READ],
255 nfsstats.rpccnt[NFSPROC_WRITE],
256 nfsstats.rpccnt[NFSPROC_CREATE],
257 nfsstats.rpccnt[NFSPROC_REMOVE]);
258 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
259 "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
260 "Readdir", "RdirPlus", "Access");
261 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
262 nfsstats.rpccnt[NFSPROC_RENAME],
263 nfsstats.rpccnt[NFSPROC_LINK],
264 nfsstats.rpccnt[NFSPROC_SYMLINK],
265 nfsstats.rpccnt[NFSPROC_MKDIR],
266 nfsstats.rpccnt[NFSPROC_RMDIR],
267 nfsstats.rpccnt[NFSPROC_READDIR],
268 nfsstats.rpccnt[NFSPROC_READDIRPLUS],
269 nfsstats.rpccnt[NFSPROC_ACCESS]);
270 printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
271 "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit");
272 printf("%9d %9d %9d %9d %9d\n",
273 nfsstats.rpccnt[NFSPROC_MKNOD],
274 nfsstats.rpccnt[NFSPROC_FSSTAT],
275 nfsstats.rpccnt[NFSPROC_FSINFO],
276 nfsstats.rpccnt[NFSPROC_PATHCONF],
277 nfsstats.rpccnt[NFSPROC_COMMIT]);
278 printf("Rpc Info:\n");
279 printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
280 "TimedOut", "Invalid", "X Replies", "Retries",
282 printf("%9d %9d %9d %9d %9d\n",
283 nfsstats.rpctimeouts,
285 nfsstats.rpcunexpected,
287 nfsstats.rpcrequests);
288 printf("Cache Info:\n");
289 printf("%9.9s %9.9s %9.9s %9.9s",
290 "Attr Hits", "Misses", "Lkup Hits", "Misses");
291 printf(" %9.9s %9.9s %9.9s %9.9s\n",
292 "BioR Hits", "Misses", "BioW Hits", "Misses");
293 printf("%9d %9d %9d %9d",
294 nfsstats.attrcache_hits, nfsstats.attrcache_misses,
295 nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
296 printf(" %9d %9d %9d %9d\n",
297 nfsstats.biocache_reads-nfsstats.read_bios,
299 nfsstats.biocache_writes-nfsstats.write_bios,
300 nfsstats.write_bios);
301 printf("%9.9s %9.9s %9.9s %9.9s",
302 "BioRLHits", "Misses", "BioD Hits", "Misses");
303 printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
304 printf("%9d %9d %9d %9d",
305 nfsstats.biocache_readlinks-nfsstats.readlink_bios,
306 nfsstats.readlink_bios,
307 nfsstats.biocache_readdirs-nfsstats.readdir_bios,
308 nfsstats.readdir_bios);
310 nfsstats.direofcache_hits, nfsstats.direofcache_misses);
312 if (serverOnly && !nfsrvstatsp) {
313 printf("Server not present!\n");
317 printf("\nServer Info:\n");
318 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
319 "Getattr", "Setattr", "Lookup", "Readlink", "Read",
320 "Write", "Create", "Remove");
321 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
322 nfsrvstats.srvrpccnt[NFSPROC_GETATTR],
323 nfsrvstats.srvrpccnt[NFSPROC_SETATTR],
324 nfsrvstats.srvrpccnt[NFSPROC_LOOKUP],
325 nfsrvstats.srvrpccnt[NFSPROC_READLINK],
326 nfsrvstats.srvrpccnt[NFSPROC_READ],
327 nfsrvstats.srvrpccnt[NFSPROC_WRITE],
328 nfsrvstats.srvrpccnt[NFSPROC_CREATE],
329 nfsrvstats.srvrpccnt[NFSPROC_REMOVE]);
330 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
331 "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
332 "Readdir", "RdirPlus", "Access");
333 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
334 nfsrvstats.srvrpccnt[NFSPROC_RENAME],
335 nfsrvstats.srvrpccnt[NFSPROC_LINK],
336 nfsrvstats.srvrpccnt[NFSPROC_SYMLINK],
337 nfsrvstats.srvrpccnt[NFSPROC_MKDIR],
338 nfsrvstats.srvrpccnt[NFSPROC_RMDIR],
339 nfsrvstats.srvrpccnt[NFSPROC_READDIR],
340 nfsrvstats.srvrpccnt[NFSPROC_READDIRPLUS],
341 nfsrvstats.srvrpccnt[NFSPROC_ACCESS]);
342 printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
343 "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit");
344 printf("%9d %9d %9d %9d %9d\n",
345 nfsrvstats.srvrpccnt[NFSPROC_MKNOD],
346 nfsrvstats.srvrpccnt[NFSPROC_FSSTAT],
347 nfsrvstats.srvrpccnt[NFSPROC_FSINFO],
348 nfsrvstats.srvrpccnt[NFSPROC_PATHCONF],
349 nfsrvstats.srvrpccnt[NFSPROC_COMMIT]);
350 printf("Server Ret-Failed\n");
351 printf("%17d\n", nfsrvstats.srvrpc_errs);
352 printf("Server Faults\n");
353 printf("%13d\n", nfsrvstats.srv_errs);
354 printf("Server Cache Stats:\n");
355 printf("%9.9s %9.9s %9.9s %9.9s\n",
356 "Inprog", "Idem", "Non-idem", "Misses");
357 printf("%9d %9d %9d %9d\n",
358 nfsrvstats.srvcache_inproghits,
359 nfsrvstats.srvcache_idemdonehits,
360 nfsrvstats.srvcache_nonidemdonehits,
361 nfsrvstats.srvcache_misses);
362 printf("Server Write Gathering:\n");
363 printf("%9.9s %9.9s %9.9s\n",
364 "WriteOps", "WriteRPC", "Opsaved");
365 printf("%9d %9d %9d\n",
366 nfsrvstats.srvvop_writes,
367 nfsrvstats.srvrpccnt[NFSPROC_WRITE],
368 nfsrvstats.srvrpccnt[NFSPROC_WRITE] -
369 nfsrvstats.srvvop_writes);
373 u_char signalled; /* set if alarm goes off "early" */
376 * Print a running summary of nfs statistics.
377 * Repeat display every interval seconds, showing statistics
378 * collected over that interval. Assumes that interval is non-zero.
379 * First line printed at top of screen is always cumulative.
382 sidewaysintpr(u_int interval, int clientOnly, int serverOnly)
384 struct nfsstats nfsstats, lastst, *nfsstatsp;
385 struct nfsrvstats nfsrvstats, lastsrvst, *nfsrvstatsp;
389 nfsrvstatsp = &lastsrvst;
390 readstats(&nfsstatsp, &nfsrvstatsp, 0);
391 if (clientOnly && !nfsstatsp) {
392 printf("Client not present!\n");
395 if (serverOnly && !nfsrvstatsp) {
396 printf("Server not present!\n");
402 nfsstatsp = &nfsstats;
403 nfsrvstatsp = &nfsrvstats;
404 readstats(&nfsstatsp, &nfsrvstatsp, 0);
407 printhdr(clientOnly, serverOnly);
408 if (clientOnly && serverOnly)
414 printf("%s %6d %6d %6d %6d %6d %6d %6d %6d",
415 ((clientOnly && serverOnly) ? "Client:" : ""),
416 DELTA(attrcache_hits) + DELTA(attrcache_misses),
417 DELTA(lookupcache_hits) + DELTA(lookupcache_misses),
418 DELTA(biocache_readlinks),
419 DELTA(biocache_reads),
420 DELTA(biocache_writes),
421 nfsstats.rpccnt[NFSPROC_RENAME]-lastst.rpccnt[NFSPROC_RENAME],
422 DELTA(accesscache_hits) + DELTA(accesscache_misses),
423 DELTA(biocache_readdirs)
426 printf(" %s %s %s %s %s %s",
427 sperc1(DELTA(attrcache_hits),
428 DELTA(attrcache_misses)),
429 sperc1(DELTA(lookupcache_hits),
430 DELTA(lookupcache_misses)),
431 sperc2(DELTA(biocache_reads),
433 sperc2(DELTA(biocache_writes),
435 sperc1(DELTA(accesscache_hits),
436 DELTA(accesscache_misses)),
437 sperc2(DELTA(biocache_readdirs),
445 printf("%s %6d %6d %6d %6d %6d %6d %6d %6d",
446 ((clientOnly && serverOnly) ? "Server:" : ""),
447 nfsrvstats.srvrpccnt[NFSPROC_GETATTR]-lastsrvst.srvrpccnt[NFSPROC_GETATTR],
448 nfsrvstats.srvrpccnt[NFSPROC_LOOKUP]-lastsrvst.srvrpccnt[NFSPROC_LOOKUP],
449 nfsrvstats.srvrpccnt[NFSPROC_READLINK]-lastsrvst.srvrpccnt[NFSPROC_READLINK],
450 nfsrvstats.srvrpccnt[NFSPROC_READ]-lastsrvst.srvrpccnt[NFSPROC_READ],
451 nfsrvstats.srvrpccnt[NFSPROC_WRITE]-lastsrvst.srvrpccnt[NFSPROC_WRITE],
452 nfsrvstats.srvrpccnt[NFSPROC_RENAME]-lastsrvst.srvrpccnt[NFSPROC_RENAME],
453 nfsrvstats.srvrpccnt[NFSPROC_ACCESS]-lastsrvst.srvrpccnt[NFSPROC_ACCESS],
454 (nfsrvstats.srvrpccnt[NFSPROC_READDIR]-lastsrvst.srvrpccnt[NFSPROC_READDIR])
455 +(nfsrvstats.srvrpccnt[NFSPROC_READDIRPLUS]-lastsrvst.srvrpccnt[NFSPROC_READDIRPLUS]));
457 lastsrvst = nfsrvstats;
466 printhdr(int clientOnly, int serverOnly)
468 printf("%s%6.6s %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s",
469 ((serverOnly && clientOnly) ? " " : " "),
470 "GtAttr", "Lookup", "Rdlink", "Read", "Write", "Rename",
472 if (widemode && clientOnly) {
473 printf(" Attr Lkup BioR BioW Accs BioD");
482 (void)fprintf(stderr,
483 "usage: nfsstat [-cszW] [-M core] [-N system] [-w interval]\n");
487 static char SPBuf[64][8];
491 sperc1(int hits, int misses)
493 char *p = SPBuf[SPIndex];
497 (int)(char)((quad_t)hits * 100 / (hits + misses)));
501 SPIndex = (SPIndex + 1) & 63;
506 sperc2(int ttl, int misses)
508 char *p = SPBuf[SPIndex];
512 (int)(char)((quad_t)(ttl - misses) * 100 / ttl));
516 SPIndex = (SPIndex + 1) & 63;