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