1 /* 2 * Copyright (c) 1983, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rick Macklem at The University of Guelph. 7 * 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. 23 * 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. 35 */ 36 37 #ifndef lint 38 static char copyright[] = 39 "@(#) Copyright (c) 1983, 1989, 1993\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41 #endif /* not lint */ 42 43 #ifndef lint 44 #if 0 45 static char sccsid[] = "@(#)nfsstat.c 8.2 (Berkeley) 3/31/95"; 46 #endif 47 static const char rcsid[] = 48 "$FreeBSD$"; 49 #endif /* not lint */ 50 51 #include <sys/param.h> 52 #include <sys/mount.h> 53 #include <sys/time.h> 54 #include <sys/sysctl.h> 55 #include <nfs/rpcv2.h> 56 #include <nfs/nfsproto.h> 57 #include <nfs/nfs.h> 58 #include <signal.h> 59 #include <fcntl.h> 60 #include <ctype.h> 61 #include <errno.h> 62 #include <kvm.h> 63 #include <nlist.h> 64 #include <unistd.h> 65 #include <stdio.h> 66 #include <stdlib.h> 67 #include <string.h> 68 #include <paths.h> 69 #include <err.h> 70 71 struct nlist nl[] = { 72 #define N_NFSSTAT 0 73 { "_nfsstats" }, 74 "", 75 }; 76 kvm_t *kd; 77 78 static int deadkernel = 0; 79 static int widemode = 0; 80 81 void intpr __P((int, int)); 82 void printhdr __P((int, int)); 83 void sidewaysintpr __P((u_int, int, int)); 84 void usage __P((void)); 85 char *sperc1 __P((int, int)); 86 char *sperc2 __P((int, int)); 87 88 #define DELTA(field) (nfsstats.field - lastst.field) 89 90 main(argc, argv) 91 int argc; 92 char **argv; 93 { 94 extern int optind; 95 extern char *optarg; 96 u_int interval; 97 int clientOnly = -1; 98 int serverOnly = -1; 99 int ch; 100 char *memf, *nlistf; 101 char errbuf[80]; 102 103 interval = 0; 104 memf = nlistf = NULL; 105 while ((ch = getopt(argc, argv, "csWM:N:w:")) != -1) 106 switch(ch) { 107 case 'M': 108 memf = optarg; 109 break; 110 case 'N': 111 nlistf = optarg; 112 break; 113 case 'W': 114 widemode = 1; 115 break; 116 case 'w': 117 interval = atoi(optarg); 118 break; 119 case 'c': 120 clientOnly = 1; 121 if (serverOnly < 0) 122 serverOnly = 0; 123 break; 124 case 's': 125 serverOnly = 1; 126 if (clientOnly < 0) 127 clientOnly = 0; 128 break; 129 case '?': 130 default: 131 usage(); 132 } 133 argc -= optind; 134 argv += optind; 135 136 #define BACKWARD_COMPATIBILITY 137 #ifdef BACKWARD_COMPATIBILITY 138 if (*argv) { 139 interval = atoi(*argv); 140 if (*++argv) { 141 nlistf = *argv; 142 if (*++argv) 143 memf = *argv; 144 } 145 } 146 #endif 147 /* 148 * Discard setgid privileges if not the running kernel so that bad 149 * guys can't print interesting stuff from kernel memory. 150 */ 151 if (nlistf != NULL || memf != NULL) { 152 setgid(getgid()); 153 deadkernel = 1; 154 155 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, 156 errbuf)) == 0) { 157 errx(1, "kvm_openfiles: %s", errbuf); 158 } 159 if (kvm_nlist(kd, nl) != 0) { 160 errx(1, "kvm_nlist: can't get names"); 161 } 162 } 163 164 if (interval) 165 sidewaysintpr(interval, clientOnly, serverOnly); 166 else 167 intpr(clientOnly, serverOnly); 168 exit(0); 169 } 170 171 /* 172 * Read the nfs stats using sysctl(3) for live kernels, or kvm_read 173 * for dead ones. 174 */ 175 void 176 readstats(stp) 177 struct nfsstats *stp; 178 { 179 if(deadkernel) { 180 if(kvm_read(kd, (u_long)nl[N_NFSSTAT].n_value, stp, 181 sizeof *stp) < 0) { 182 err(1, "kvm_read"); 183 } 184 } else { 185 int name[3]; 186 size_t buflen = sizeof *stp; 187 struct vfsconf vfc; 188 189 if (getvfsbyname("nfs", &vfc) < 0) 190 err(1, "getvfsbyname: NFS not compiled into kernel"); 191 name[0] = CTL_VFS; 192 name[1] = vfc.vfc_typenum; 193 name[2] = NFS_NFSSTATS; 194 if (sysctl(name, 3, stp, &buflen, (void *)0, (size_t)0) < 0) { 195 err(1, "sysctl"); 196 } 197 } 198 } 199 200 /* 201 * Print a description of the nfs stats. 202 */ 203 void 204 intpr(int clientOnly, int serverOnly) 205 { 206 struct nfsstats nfsstats; 207 208 readstats(&nfsstats); 209 210 if (clientOnly) { 211 printf("Client Info:\n"); 212 printf("Rpc Counts:\n"); 213 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n", 214 "Getattr", "Setattr", "Lookup", "Readlink", "Read", 215 "Write", "Create", "Remove"); 216 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n", 217 nfsstats.rpccnt[NFSPROC_GETATTR], 218 nfsstats.rpccnt[NFSPROC_SETATTR], 219 nfsstats.rpccnt[NFSPROC_LOOKUP], 220 nfsstats.rpccnt[NFSPROC_READLINK], 221 nfsstats.rpccnt[NFSPROC_READ], 222 nfsstats.rpccnt[NFSPROC_WRITE], 223 nfsstats.rpccnt[NFSPROC_CREATE], 224 nfsstats.rpccnt[NFSPROC_REMOVE]); 225 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n", 226 "Rename", "Link", "Symlink", "Mkdir", "Rmdir", 227 "Readdir", "RdirPlus", "Access"); 228 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n", 229 nfsstats.rpccnt[NFSPROC_RENAME], 230 nfsstats.rpccnt[NFSPROC_LINK], 231 nfsstats.rpccnt[NFSPROC_SYMLINK], 232 nfsstats.rpccnt[NFSPROC_MKDIR], 233 nfsstats.rpccnt[NFSPROC_RMDIR], 234 nfsstats.rpccnt[NFSPROC_READDIR], 235 nfsstats.rpccnt[NFSPROC_READDIRPLUS], 236 nfsstats.rpccnt[NFSPROC_ACCESS]); 237 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n", 238 "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit", 239 "GLease", "Vacate", "Evict"); 240 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n", 241 nfsstats.rpccnt[NFSPROC_MKNOD], 242 nfsstats.rpccnt[NFSPROC_FSSTAT], 243 nfsstats.rpccnt[NFSPROC_FSINFO], 244 nfsstats.rpccnt[NFSPROC_PATHCONF], 245 nfsstats.rpccnt[NFSPROC_COMMIT], 246 nfsstats.rpccnt[NQNFSPROC_GETLEASE], 247 nfsstats.rpccnt[NQNFSPROC_VACATED], 248 nfsstats.rpccnt[NQNFSPROC_EVICTED]); 249 printf("Rpc Info:\n"); 250 printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n", 251 "TimedOut", "Invalid", "X Replies", "Retries", 252 "Requests"); 253 printf("%9d %9d %9d %9d %9d\n", 254 nfsstats.rpctimeouts, 255 nfsstats.rpcinvalid, 256 nfsstats.rpcunexpected, 257 nfsstats.rpcretries, 258 nfsstats.rpcrequests); 259 printf("Cache Info:\n"); 260 printf("%9.9s %9.9s %9.9s %9.9s", 261 "Attr Hits", "Misses", "Lkup Hits", "Misses"); 262 printf(" %9.9s %9.9s %9.9s %9.9s\n", 263 "BioR Hits", "Misses", "BioW Hits", "Misses"); 264 printf("%9d %9d %9d %9d", 265 nfsstats.attrcache_hits, nfsstats.attrcache_misses, 266 nfsstats.lookupcache_hits, nfsstats.lookupcache_misses); 267 printf(" %9d %9d %9d %9d\n", 268 nfsstats.biocache_reads-nfsstats.read_bios, 269 nfsstats.read_bios, 270 nfsstats.biocache_writes-nfsstats.write_bios, 271 nfsstats.write_bios); 272 printf("%9.9s %9.9s %9.9s %9.9s", 273 "BioRLHits", "Misses", "BioD Hits", "Misses"); 274 printf(" %9.9s %9.9s\n", "DirE Hits", "Misses"); 275 printf("%9d %9d %9d %9d", 276 nfsstats.biocache_readlinks-nfsstats.readlink_bios, 277 nfsstats.readlink_bios, 278 nfsstats.biocache_readdirs-nfsstats.readdir_bios, 279 nfsstats.readdir_bios); 280 printf(" %9d %9d\n", 281 nfsstats.direofcache_hits, nfsstats.direofcache_misses); 282 } 283 if (serverOnly) { 284 printf("\nServer Info:\n"); 285 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n", 286 "Getattr", "Setattr", "Lookup", "Readlink", "Read", 287 "Write", "Create", "Remove"); 288 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n", 289 nfsstats.srvrpccnt[NFSPROC_GETATTR], 290 nfsstats.srvrpccnt[NFSPROC_SETATTR], 291 nfsstats.srvrpccnt[NFSPROC_LOOKUP], 292 nfsstats.srvrpccnt[NFSPROC_READLINK], 293 nfsstats.srvrpccnt[NFSPROC_READ], 294 nfsstats.srvrpccnt[NFSPROC_WRITE], 295 nfsstats.srvrpccnt[NFSPROC_CREATE], 296 nfsstats.srvrpccnt[NFSPROC_REMOVE]); 297 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n", 298 "Rename", "Link", "Symlink", "Mkdir", "Rmdir", 299 "Readdir", "RdirPlus", "Access"); 300 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n", 301 nfsstats.srvrpccnt[NFSPROC_RENAME], 302 nfsstats.srvrpccnt[NFSPROC_LINK], 303 nfsstats.srvrpccnt[NFSPROC_SYMLINK], 304 nfsstats.srvrpccnt[NFSPROC_MKDIR], 305 nfsstats.srvrpccnt[NFSPROC_RMDIR], 306 nfsstats.srvrpccnt[NFSPROC_READDIR], 307 nfsstats.srvrpccnt[NFSPROC_READDIRPLUS], 308 nfsstats.srvrpccnt[NFSPROC_ACCESS]); 309 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n", 310 "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit", 311 "GLease", "Vacate", "Evict"); 312 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n", 313 nfsstats.srvrpccnt[NFSPROC_MKNOD], 314 nfsstats.srvrpccnt[NFSPROC_FSSTAT], 315 nfsstats.srvrpccnt[NFSPROC_FSINFO], 316 nfsstats.srvrpccnt[NFSPROC_PATHCONF], 317 nfsstats.srvrpccnt[NFSPROC_COMMIT], 318 nfsstats.srvrpccnt[NQNFSPROC_GETLEASE], 319 nfsstats.srvrpccnt[NQNFSPROC_VACATED], 320 nfsstats.srvrpccnt[NQNFSPROC_EVICTED]); 321 printf("Server Ret-Failed\n"); 322 printf("%17d\n", nfsstats.srvrpc_errs); 323 printf("Server Faults\n"); 324 printf("%13d\n", nfsstats.srv_errs); 325 printf("Server Cache Stats:\n"); 326 printf("%9.9s %9.9s %9.9s %9.9s\n", 327 "Inprog", "Idem", "Non-idem", "Misses"); 328 printf("%9d %9d %9d %9d\n", 329 nfsstats.srvcache_inproghits, 330 nfsstats.srvcache_idemdonehits, 331 nfsstats.srvcache_nonidemdonehits, 332 nfsstats.srvcache_misses); 333 printf("Server Lease Stats:\n"); 334 printf("%9.9s %9.9s %9.9s\n", 335 "Leases", "PeakL", "GLeases"); 336 printf("%9d %9d %9d\n", 337 nfsstats.srvnqnfs_leases, 338 nfsstats.srvnqnfs_maxleases, 339 nfsstats.srvnqnfs_getleases); 340 printf("Server Write Gathering:\n"); 341 printf("%9.9s %9.9s %9.9s\n", 342 "WriteOps", "WriteRPC", "Opsaved"); 343 printf("%9d %9d %9d\n", 344 nfsstats.srvvop_writes, 345 nfsstats.srvrpccnt[NFSPROC_WRITE], 346 nfsstats.srvrpccnt[NFSPROC_WRITE] - 347 nfsstats.srvvop_writes); 348 } 349 } 350 351 u_char signalled; /* set if alarm goes off "early" */ 352 353 /* 354 * Print a running summary of nfs statistics. 355 * Repeat display every interval seconds, showing statistics 356 * collected over that interval. Assumes that interval is non-zero. 357 * First line printed at top of screen is always cumulative. 358 */ 359 void 360 sidewaysintpr(u_int interval, int clientOnly, int serverOnly) 361 { 362 struct nfsstats nfsstats, lastst; 363 int hdrcnt = 1; 364 365 readstats(&lastst); 366 sleep(interval); 367 368 for (;;) { 369 readstats(&nfsstats); 370 371 if (--hdrcnt == 0) { 372 printhdr(clientOnly, serverOnly); 373 if (clientOnly && serverOnly) 374 hdrcnt = 10; 375 else 376 hdrcnt = 20; 377 } 378 if (clientOnly) { 379 printf("%s %6d %6d %6d %6d %6d %6d %6d %6d", 380 ((clientOnly && serverOnly) ? "Client:" : ""), 381 DELTA(attrcache_hits) + DELTA(attrcache_misses), 382 DELTA(lookupcache_hits) + DELTA(lookupcache_misses), 383 DELTA(biocache_readlinks), 384 DELTA(biocache_reads), 385 DELTA(biocache_writes), 386 nfsstats.rpccnt[NFSPROC_RENAME]-lastst.rpccnt[NFSPROC_RENAME], 387 DELTA(accesscache_hits) + DELTA(accesscache_misses), 388 DELTA(biocache_readdirs) 389 ); 390 if (widemode) { 391 printf(" %s %s %s %s %s %s", 392 sperc1(DELTA(attrcache_hits), 393 DELTA(attrcache_misses)), 394 sperc1(DELTA(lookupcache_hits), 395 DELTA(lookupcache_misses)), 396 sperc2(DELTA(biocache_reads), 397 DELTA(read_bios)), 398 sperc2(DELTA(biocache_writes), 399 DELTA(write_bios)), 400 sperc1(DELTA(accesscache_hits), 401 DELTA(accesscache_misses)), 402 sperc2(DELTA(biocache_readdirs), 403 DELTA(readdir_bios)) 404 ); 405 } 406 printf("\n"); 407 } 408 if (serverOnly) { 409 printf("%s %6d %6d %6d %6d %6d %6d %6d %6d", 410 ((clientOnly && serverOnly) ? "Server:" : ""), 411 nfsstats.srvrpccnt[NFSPROC_GETATTR]-lastst.srvrpccnt[NFSPROC_GETATTR], 412 nfsstats.srvrpccnt[NFSPROC_LOOKUP]-lastst.srvrpccnt[NFSPROC_LOOKUP], 413 nfsstats.srvrpccnt[NFSPROC_READLINK]-lastst.srvrpccnt[NFSPROC_READLINK], 414 nfsstats.srvrpccnt[NFSPROC_READ]-lastst.srvrpccnt[NFSPROC_READ], 415 nfsstats.srvrpccnt[NFSPROC_WRITE]-lastst.srvrpccnt[NFSPROC_WRITE], 416 nfsstats.srvrpccnt[NFSPROC_RENAME]-lastst.srvrpccnt[NFSPROC_RENAME], 417 nfsstats.srvrpccnt[NFSPROC_ACCESS]-lastst.srvrpccnt[NFSPROC_ACCESS], 418 (nfsstats.srvrpccnt[NFSPROC_READDIR]-lastst.srvrpccnt[NFSPROC_READDIR]) 419 +(nfsstats.srvrpccnt[NFSPROC_READDIRPLUS]-lastst.srvrpccnt[NFSPROC_READDIRPLUS])); 420 printf("\n"); 421 } 422 lastst = nfsstats; 423 fflush(stdout); 424 sleep(interval); 425 } 426 /*NOTREACHED*/ 427 } 428 429 void 430 printhdr(int clientOnly, int serverOnly) 431 { 432 printf("%s%6.6s %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s", 433 ((serverOnly && clientOnly) ? " " : " "), 434 "GtAttr", "Lookup", "Rdlink", "Read", "Write", "Rename", 435 "Access", "Rddir"); 436 if (widemode && clientOnly) { 437 printf(" Attr Lkup BioR BioW Accs BioD"); 438 } 439 printf("\n"); 440 fflush(stdout); 441 } 442 443 void 444 usage() 445 { 446 (void)fprintf(stderr, 447 "usage: nfsstat [-M core] [-N system] [-w interval]\n"); 448 exit(1); 449 } 450 451 static char SPBuf[64][8]; 452 static int SPIndex; 453 454 char * 455 sperc1(int hits, int misses) 456 { 457 char *p = SPBuf[SPIndex]; 458 459 if (hits + misses) { 460 sprintf(p, "%3d%%", 461 (int)(char)((quad_t)hits * 100 / (hits + misses))); 462 } else { 463 sprintf(p, " -"); 464 } 465 SPIndex = (SPIndex + 1) & 63; 466 return(p); 467 } 468 469 char * 470 sperc2(int ttl, int misses) 471 { 472 char *p = SPBuf[SPIndex]; 473 474 if (ttl) { 475 sprintf(p, "%3d%%", 476 (int)(char)((quad_t)(ttl - misses) * 100 / ttl)); 477 } else { 478 sprintf(p, " -"); 479 } 480 SPIndex = (SPIndex + 1) & 63; 481 return(p); 482 } 483 484