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
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.2 2003/06/17 04:29:30 dillon Exp $
42 #include <sys/param.h>
43 #include <sys/mount.h>
45 #include <sys/sysctl.h>
46 #include <nfs/rpcv2.h>
47 #include <nfs/nfsproto.h>
70 static int deadkernel = 0;
71 static int widemode = 0;
73 void intpr __P((int, int));
74 void printhdr __P((int, int));
75 void sidewaysintpr __P((u_int, int, int));
76 void usage __P((void));
77 char *sperc1 __P((int, int));
78 char *sperc2 __P((int, int));
80 #define DELTA(field) (nfsstats.field - lastst.field)
91 char errbuf[_POSIX2_LINE_MAX];
95 while ((ch = getopt(argc, argv, "csWM:N:w:")) != -1)
107 interval = atoi(optarg);
126 #define BACKWARD_COMPATIBILITY
127 #ifdef BACKWARD_COMPATIBILITY
129 interval = atoi(*argv);
137 if (nlistf != NULL || memf != NULL) {
140 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
142 errx(1, "kvm_openfiles: %s", errbuf);
144 if (kvm_nlist(kd, nl) != 0) {
145 errx(1, "kvm_nlist: can't get names");
150 sidewaysintpr(interval, clientOnly, serverOnly);
152 intpr(clientOnly, serverOnly);
157 * Read the nfs stats using sysctl(3) for live kernels, or kvm_read
162 struct nfsstats *stp;
165 if(kvm_read(kd, (u_long)nl[N_NFSSTAT].n_value, stp,
171 size_t buflen = sizeof *stp;
174 if (getvfsbyname("nfs", &vfc) < 0)
175 err(1, "getvfsbyname: NFS not compiled into kernel");
177 name[1] = vfc.vfc_typenum;
178 name[2] = NFS_NFSSTATS;
179 if (sysctl(name, 3, stp, &buflen, (void *)0, (size_t)0) < 0) {
186 * Print a description of the nfs stats.
189 intpr(int clientOnly, int serverOnly)
191 struct nfsstats nfsstats;
193 readstats(&nfsstats);
196 printf("Client Info:\n");
197 printf("Rpc Counts:\n");
198 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
199 "Getattr", "Setattr", "Lookup", "Readlink", "Read",
200 "Write", "Create", "Remove");
201 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
202 nfsstats.rpccnt[NFSPROC_GETATTR],
203 nfsstats.rpccnt[NFSPROC_SETATTR],
204 nfsstats.rpccnt[NFSPROC_LOOKUP],
205 nfsstats.rpccnt[NFSPROC_READLINK],
206 nfsstats.rpccnt[NFSPROC_READ],
207 nfsstats.rpccnt[NFSPROC_WRITE],
208 nfsstats.rpccnt[NFSPROC_CREATE],
209 nfsstats.rpccnt[NFSPROC_REMOVE]);
210 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
211 "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
212 "Readdir", "RdirPlus", "Access");
213 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
214 nfsstats.rpccnt[NFSPROC_RENAME],
215 nfsstats.rpccnt[NFSPROC_LINK],
216 nfsstats.rpccnt[NFSPROC_SYMLINK],
217 nfsstats.rpccnt[NFSPROC_MKDIR],
218 nfsstats.rpccnt[NFSPROC_RMDIR],
219 nfsstats.rpccnt[NFSPROC_READDIR],
220 nfsstats.rpccnt[NFSPROC_READDIRPLUS],
221 nfsstats.rpccnt[NFSPROC_ACCESS]);
222 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
223 "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
224 "GLease", "Vacate", "Evict");
225 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
226 nfsstats.rpccnt[NFSPROC_MKNOD],
227 nfsstats.rpccnt[NFSPROC_FSSTAT],
228 nfsstats.rpccnt[NFSPROC_FSINFO],
229 nfsstats.rpccnt[NFSPROC_PATHCONF],
230 nfsstats.rpccnt[NFSPROC_COMMIT],
231 nfsstats.rpccnt[NQNFSPROC_GETLEASE],
232 nfsstats.rpccnt[NQNFSPROC_VACATED],
233 nfsstats.rpccnt[NQNFSPROC_EVICTED]);
234 printf("Rpc Info:\n");
235 printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
236 "TimedOut", "Invalid", "X Replies", "Retries",
238 printf("%9d %9d %9d %9d %9d\n",
239 nfsstats.rpctimeouts,
241 nfsstats.rpcunexpected,
243 nfsstats.rpcrequests);
244 printf("Cache Info:\n");
245 printf("%9.9s %9.9s %9.9s %9.9s",
246 "Attr Hits", "Misses", "Lkup Hits", "Misses");
247 printf(" %9.9s %9.9s %9.9s %9.9s\n",
248 "BioR Hits", "Misses", "BioW Hits", "Misses");
249 printf("%9d %9d %9d %9d",
250 nfsstats.attrcache_hits, nfsstats.attrcache_misses,
251 nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
252 printf(" %9d %9d %9d %9d\n",
253 nfsstats.biocache_reads-nfsstats.read_bios,
255 nfsstats.biocache_writes-nfsstats.write_bios,
256 nfsstats.write_bios);
257 printf("%9.9s %9.9s %9.9s %9.9s",
258 "BioRLHits", "Misses", "BioD Hits", "Misses");
259 printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
260 printf("%9d %9d %9d %9d",
261 nfsstats.biocache_readlinks-nfsstats.readlink_bios,
262 nfsstats.readlink_bios,
263 nfsstats.biocache_readdirs-nfsstats.readdir_bios,
264 nfsstats.readdir_bios);
266 nfsstats.direofcache_hits, nfsstats.direofcache_misses);
269 printf("\nServer Info:\n");
270 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
271 "Getattr", "Setattr", "Lookup", "Readlink", "Read",
272 "Write", "Create", "Remove");
273 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
274 nfsstats.srvrpccnt[NFSPROC_GETATTR],
275 nfsstats.srvrpccnt[NFSPROC_SETATTR],
276 nfsstats.srvrpccnt[NFSPROC_LOOKUP],
277 nfsstats.srvrpccnt[NFSPROC_READLINK],
278 nfsstats.srvrpccnt[NFSPROC_READ],
279 nfsstats.srvrpccnt[NFSPROC_WRITE],
280 nfsstats.srvrpccnt[NFSPROC_CREATE],
281 nfsstats.srvrpccnt[NFSPROC_REMOVE]);
282 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
283 "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
284 "Readdir", "RdirPlus", "Access");
285 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
286 nfsstats.srvrpccnt[NFSPROC_RENAME],
287 nfsstats.srvrpccnt[NFSPROC_LINK],
288 nfsstats.srvrpccnt[NFSPROC_SYMLINK],
289 nfsstats.srvrpccnt[NFSPROC_MKDIR],
290 nfsstats.srvrpccnt[NFSPROC_RMDIR],
291 nfsstats.srvrpccnt[NFSPROC_READDIR],
292 nfsstats.srvrpccnt[NFSPROC_READDIRPLUS],
293 nfsstats.srvrpccnt[NFSPROC_ACCESS]);
294 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
295 "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
296 "GLease", "Vacate", "Evict");
297 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
298 nfsstats.srvrpccnt[NFSPROC_MKNOD],
299 nfsstats.srvrpccnt[NFSPROC_FSSTAT],
300 nfsstats.srvrpccnt[NFSPROC_FSINFO],
301 nfsstats.srvrpccnt[NFSPROC_PATHCONF],
302 nfsstats.srvrpccnt[NFSPROC_COMMIT],
303 nfsstats.srvrpccnt[NQNFSPROC_GETLEASE],
304 nfsstats.srvrpccnt[NQNFSPROC_VACATED],
305 nfsstats.srvrpccnt[NQNFSPROC_EVICTED]);
306 printf("Server Ret-Failed\n");
307 printf("%17d\n", nfsstats.srvrpc_errs);
308 printf("Server Faults\n");
309 printf("%13d\n", nfsstats.srv_errs);
310 printf("Server Cache Stats:\n");
311 printf("%9.9s %9.9s %9.9s %9.9s\n",
312 "Inprog", "Idem", "Non-idem", "Misses");
313 printf("%9d %9d %9d %9d\n",
314 nfsstats.srvcache_inproghits,
315 nfsstats.srvcache_idemdonehits,
316 nfsstats.srvcache_nonidemdonehits,
317 nfsstats.srvcache_misses);
318 printf("Server Lease Stats:\n");
319 printf("%9.9s %9.9s %9.9s\n",
320 "Leases", "PeakL", "GLeases");
321 printf("%9d %9d %9d\n",
322 nfsstats.srvnqnfs_leases,
323 nfsstats.srvnqnfs_maxleases,
324 nfsstats.srvnqnfs_getleases);
325 printf("Server Write Gathering:\n");
326 printf("%9.9s %9.9s %9.9s\n",
327 "WriteOps", "WriteRPC", "Opsaved");
328 printf("%9d %9d %9d\n",
329 nfsstats.srvvop_writes,
330 nfsstats.srvrpccnt[NFSPROC_WRITE],
331 nfsstats.srvrpccnt[NFSPROC_WRITE] -
332 nfsstats.srvvop_writes);
336 u_char signalled; /* set if alarm goes off "early" */
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.
345 sidewaysintpr(u_int interval, int clientOnly, int serverOnly)
347 struct nfsstats nfsstats, lastst;
354 readstats(&nfsstats);
357 printhdr(clientOnly, serverOnly);
358 if (clientOnly && serverOnly)
364 printf("%s %6d %6d %6d %6d %6d %6d %6d %6d",
365 ((clientOnly && serverOnly) ? "Client:" : ""),
366 DELTA(attrcache_hits) + DELTA(attrcache_misses),
367 DELTA(lookupcache_hits) + DELTA(lookupcache_misses),
368 DELTA(biocache_readlinks),
369 DELTA(biocache_reads),
370 DELTA(biocache_writes),
371 nfsstats.rpccnt[NFSPROC_RENAME]-lastst.rpccnt[NFSPROC_RENAME],
372 DELTA(accesscache_hits) + DELTA(accesscache_misses),
373 DELTA(biocache_readdirs)
376 printf(" %s %s %s %s %s %s",
377 sperc1(DELTA(attrcache_hits),
378 DELTA(attrcache_misses)),
379 sperc1(DELTA(lookupcache_hits),
380 DELTA(lookupcache_misses)),
381 sperc2(DELTA(biocache_reads),
383 sperc2(DELTA(biocache_writes),
385 sperc1(DELTA(accesscache_hits),
386 DELTA(accesscache_misses)),
387 sperc2(DELTA(biocache_readdirs),
394 printf("%s %6d %6d %6d %6d %6d %6d %6d %6d",
395 ((clientOnly && serverOnly) ? "Server:" : ""),
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]));
415 printhdr(int clientOnly, int serverOnly)
417 printf("%s%6.6s %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s",
418 ((serverOnly && clientOnly) ? " " : " "),
419 "GtAttr", "Lookup", "Rdlink", "Read", "Write", "Rename",
421 if (widemode && clientOnly) {
422 printf(" Attr Lkup BioR BioW Accs BioD");
431 (void)fprintf(stderr,
432 "usage: nfsstat [-csW] [-M core] [-N system] [-w interval]\n");
436 static char SPBuf[64][8];
440 sperc1(int hits, int misses)
442 char *p = SPBuf[SPIndex];
446 (int)(char)((quad_t)hits * 100 / (hits + misses)));
450 SPIndex = (SPIndex + 1) & 63;
455 sperc2(int ttl, int misses)
457 char *p = SPBuf[SPIndex];
461 (int)(char)((quad_t)(ttl - misses) * 100 / ttl));
465 SPIndex = (SPIndex + 1) & 63;