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 /*static char sccsid[] = "From: @(#)nfsstat.c 8.1 (Berkeley) 6/6/93";*/ 45 static const char rcsid[] = 46 "$Id$"; 47 #endif /* not lint */ 48 49 #include <sys/param.h> 50 #include <sys/mount.h> 51 #include <sys/sysctl.h> 52 #include <nfs/nfsv2.h> 53 #include <nfs/nfs.h> 54 #include <signal.h> 55 #include <fcntl.h> 56 #include <ctype.h> 57 #include <errno.h> 58 #include <kvm.h> 59 #include <nlist.h> 60 #include <unistd.h> 61 #include <stdio.h> 62 #include <stdlib.h> 63 #include <string.h> 64 #include <paths.h> 65 #include <err.h> 66 67 struct nlist nl[] = { 68 #define N_NFSSTAT 0 69 { "_nfsstats" }, 70 "", 71 }; 72 kvm_t *kd; 73 74 static int deadkernel = 0; 75 76 void intpr(void), printhdr(void), sidewaysintpr(u_int), usage(void); 77 78 main(argc, argv) 79 int argc; 80 char **argv; 81 { 82 extern int optind; 83 extern char *optarg; 84 u_int interval; 85 int ch; 86 char *memf, *nlistf; 87 char errbuf[80]; 88 89 interval = 0; 90 memf = nlistf = NULL; 91 while ((ch = getopt(argc, argv, "M:N:w:")) != EOF) 92 switch(ch) { 93 case 'M': 94 memf = optarg; 95 break; 96 case 'N': 97 nlistf = optarg; 98 break; 99 case 'w': 100 interval = atoi(optarg); 101 break; 102 case '?': 103 default: 104 usage(); 105 } 106 argc -= optind; 107 argv += optind; 108 109 #define BACKWARD_COMPATIBILITY 110 #ifdef BACKWARD_COMPATIBILITY 111 if (*argv) { 112 interval = atoi(*argv); 113 if (*++argv) { 114 nlistf = *argv; 115 if (*++argv) 116 memf = *argv; 117 } 118 } 119 #endif 120 /* 121 * Discard setgid privileges if not the running kernel so that bad 122 * guys can't print interesting stuff from kernel memory. 123 */ 124 if (nlistf != NULL || memf != NULL) { 125 setgid(getgid()); 126 deadkernel = 1; 127 128 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, 129 errbuf)) == 0) { 130 errx(1, "kvm_openfiles: %s", errbuf); 131 } 132 if (kvm_nlist(kd, nl) != 0) { 133 errx(1, "kvm_nlist: can't get names"); 134 } 135 } 136 137 if (interval) 138 sidewaysintpr(interval); 139 else 140 intpr(); 141 exit(0); 142 } 143 144 /* 145 * Read the nfs stats using sysctl(3) for live kernels, or kvm_read 146 * for dead ones. 147 */ 148 void 149 readstats(struct nfsstats *stp) 150 { 151 if(deadkernel) { 152 if(kvm_read(kd, (u_long)nl[N_NFSSTAT].n_value, stp, 153 sizeof *stp) < 0) { 154 err(1, "kvm_read"); 155 } 156 } else { 157 int name[3]; 158 size_t buflen = sizeof *stp; 159 160 name[0] = CTL_FS; 161 name[1] = MOUNT_NFS; 162 name[2] = NFS_NFSSTATS; 163 164 if(sysctl(name, 3, stp, &buflen, (void *)0, (size_t)0) < 0) { 165 err(1, "sysctl"); 166 } 167 } 168 } 169 170 /* 171 * Print a description of the nfs stats. 172 */ 173 void 174 intpr(void) 175 { 176 struct nfsstats nfsstats; 177 178 readstats(&nfsstats); 179 180 printf("Client Info:\n"); 181 printf("Rpc Counts:\n"); 182 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n", 183 "Getattr", "Setattr", "Lookup", "Readlink", "Read", 184 "Write", "Create", "Remove"); 185 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n", 186 nfsstats.rpccnt[NFSPROC_GETATTR], 187 nfsstats.rpccnt[NFSPROC_SETATTR], 188 nfsstats.rpccnt[NFSPROC_LOOKUP], 189 nfsstats.rpccnt[NFSPROC_READLINK], 190 nfsstats.rpccnt[NFSPROC_READ], 191 nfsstats.rpccnt[NFSPROC_WRITE], 192 nfsstats.rpccnt[NFSPROC_CREATE], 193 nfsstats.rpccnt[NFSPROC_REMOVE]); 194 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n", 195 "Rename", "Link", "Symlink", "Mkdir", "Rmdir", 196 "Readdir", "Statfs", "RdirLook"); 197 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n", 198 nfsstats.rpccnt[NFSPROC_RENAME], 199 nfsstats.rpccnt[NFSPROC_LINK], 200 nfsstats.rpccnt[NFSPROC_SYMLINK], 201 nfsstats.rpccnt[NFSPROC_MKDIR], 202 nfsstats.rpccnt[NFSPROC_RMDIR], 203 nfsstats.rpccnt[NFSPROC_READDIR], 204 nfsstats.rpccnt[NFSPROC_STATFS], 205 nfsstats.rpccnt[NQNFSPROC_READDIRLOOK]); 206 printf("%9.9s %9.9s %9.9s\n", 207 "GLease", "Vacate", "Evict"); 208 printf("%9d %9d %9d\n", 209 nfsstats.rpccnt[NQNFSPROC_GETLEASE], 210 nfsstats.rpccnt[NQNFSPROC_VACATED], 211 nfsstats.rpccnt[NQNFSPROC_EVICTED]); 212 printf("Rpc Info:\n"); 213 printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n", 214 "TimedOut", "Invalid", "X Replies", "Retries", "Requests"); 215 printf("%9d %9d %9d %9d %9d\n", 216 nfsstats.rpctimeouts, 217 nfsstats.rpcinvalid, 218 nfsstats.rpcunexpected, 219 nfsstats.rpcretries, 220 nfsstats.rpcrequests); 221 printf("Cache Info:\n"); 222 printf("%9.9s %9.9s %9.9s %9.9s", 223 "Attr Hits", "Misses", "Lkup Hits", "Misses"); 224 printf(" %9.9s %9.9s %9.9s %9.9s\n", 225 "BioR Hits", "Misses", "BioW Hits", "Misses"); 226 printf("%9d %9d %9d %9d", 227 nfsstats.attrcache_hits, nfsstats.attrcache_misses, 228 nfsstats.lookupcache_hits, nfsstats.lookupcache_misses); 229 printf(" %9d %9d %9d %9d\n", 230 nfsstats.biocache_reads-nfsstats.read_bios, 231 nfsstats.read_bios, 232 nfsstats.biocache_writes-nfsstats.write_bios, 233 nfsstats.write_bios); 234 printf("%9.9s %9.9s %9.9s %9.9s", 235 "BioRLHits", "Misses", "BioD Hits", "Misses"); 236 printf(" %9.9s %9.9s\n", "DirE Hits", "Misses"); 237 printf("%9d %9d %9d %9d", 238 nfsstats.biocache_readlinks-nfsstats.readlink_bios, 239 nfsstats.readlink_bios, 240 nfsstats.biocache_readdirs-nfsstats.readdir_bios, 241 nfsstats.readdir_bios); 242 printf(" %9d %9d\n", 243 nfsstats.direofcache_hits, nfsstats.direofcache_misses); 244 printf("\nServer Info:\n"); 245 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n", 246 "Getattr", "Setattr", "Lookup", "Readlink", "Read", 247 "Write", "Create", "Remove"); 248 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n", 249 nfsstats.srvrpccnt[NFSPROC_GETATTR], 250 nfsstats.srvrpccnt[NFSPROC_SETATTR], 251 nfsstats.srvrpccnt[NFSPROC_LOOKUP], 252 nfsstats.srvrpccnt[NFSPROC_READLINK], 253 nfsstats.srvrpccnt[NFSPROC_READ], 254 nfsstats.srvrpccnt[NFSPROC_WRITE], 255 nfsstats.srvrpccnt[NFSPROC_CREATE], 256 nfsstats.srvrpccnt[NFSPROC_REMOVE]); 257 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n", 258 "Rename", "Link", "Symlink", "Mkdir", "Rmdir", 259 "Readdir", "Statfs", "RdirLook"); 260 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n", 261 nfsstats.srvrpccnt[NFSPROC_RENAME], 262 nfsstats.srvrpccnt[NFSPROC_LINK], 263 nfsstats.srvrpccnt[NFSPROC_SYMLINK], 264 nfsstats.srvrpccnt[NFSPROC_MKDIR], 265 nfsstats.srvrpccnt[NFSPROC_RMDIR], 266 nfsstats.srvrpccnt[NFSPROC_READDIR], 267 nfsstats.srvrpccnt[NFSPROC_STATFS], 268 nfsstats.srvrpccnt[NQNFSPROC_READDIRLOOK]); 269 printf("%9.9s %9.9s %9.9s\n", 270 "GLease", "Vacate", "Evict"); 271 printf("%9d %9d %9d\n", 272 nfsstats.srvrpccnt[NQNFSPROC_GETLEASE], 273 nfsstats.srvrpccnt[NQNFSPROC_VACATED], 274 nfsstats.srvrpccnt[NQNFSPROC_EVICTED]); 275 printf("Server Ret-Failed\n"); 276 printf("%17d\n", nfsstats.srvrpc_errs); 277 printf("Server Faults\n"); 278 printf("%13d\n", nfsstats.srv_errs); 279 printf("Server Cache Stats:\n"); 280 printf("%9.9s %9.9s %9.9s %9.9s\n", 281 "Inprog", "Idem", "Non-idem", "Misses"); 282 printf("%9d %9d %9d %9d\n", 283 nfsstats.srvcache_inproghits, 284 nfsstats.srvcache_idemdonehits, 285 nfsstats.srvcache_nonidemdonehits, 286 nfsstats.srvcache_misses); 287 printf("Server Lease Stats:\n"); 288 printf("%9.9s %9.9s %9.9s\n", 289 "Leases", "PeakL", "GLeases"); 290 printf("%9d %9d %9d\n", 291 nfsstats.srvnqnfs_leases, 292 nfsstats.srvnqnfs_maxleases, 293 nfsstats.srvnqnfs_getleases); 294 } 295 296 u_char signalled; /* set if alarm goes off "early" */ 297 298 /* 299 * Print a running summary of nfs statistics. 300 * Repeat display every interval seconds, showing statistics 301 * collected over that interval. Assumes that interval is non-zero. 302 * First line printed at top of screen is always cumulative. 303 */ 304 void 305 sidewaysintpr(interval) 306 u_int interval; 307 { 308 struct nfsstats nfsstats, lastst; 309 int hdrcnt, oldmask; 310 void catchalarm(); 311 312 (void)signal(SIGALRM, catchalarm); 313 signalled = 0; 314 (void)alarm(interval); 315 bzero((caddr_t)&lastst, sizeof(lastst)); 316 317 for (hdrcnt = 1;;) { 318 if (!--hdrcnt) { 319 printhdr(); 320 hdrcnt = 20; 321 } 322 readstats(&nfsstats); 323 printf("Client: %8d %8d %8d %8d %8d %8d %8d %8d\n", 324 nfsstats.rpccnt[1]-lastst.rpccnt[1], 325 nfsstats.rpccnt[4]-lastst.rpccnt[4], 326 nfsstats.rpccnt[5]-lastst.rpccnt[5], 327 nfsstats.rpccnt[6]-lastst.rpccnt[6], 328 nfsstats.rpccnt[8]-lastst.rpccnt[8], 329 nfsstats.rpccnt[11]-lastst.rpccnt[11], 330 nfsstats.rpccnt[12]-lastst.rpccnt[12], 331 nfsstats.rpccnt[16]-lastst.rpccnt[16]); 332 printf("Server: %8d %8d %8d %8d %8d %8d %8d %8d\n", 333 nfsstats.srvrpccnt[1]-lastst.srvrpccnt[1], 334 nfsstats.srvrpccnt[4]-lastst.srvrpccnt[4], 335 nfsstats.srvrpccnt[5]-lastst.srvrpccnt[5], 336 nfsstats.srvrpccnt[6]-lastst.srvrpccnt[6], 337 nfsstats.srvrpccnt[8]-lastst.srvrpccnt[8], 338 nfsstats.srvrpccnt[11]-lastst.srvrpccnt[11], 339 nfsstats.srvrpccnt[12]-lastst.srvrpccnt[12], 340 nfsstats.srvrpccnt[16]-lastst.srvrpccnt[16]); 341 lastst = nfsstats; 342 fflush(stdout); 343 oldmask = sigblock(sigmask(SIGALRM)); 344 if (!signalled) 345 sigpause(0); 346 sigsetmask(oldmask); 347 signalled = 0; 348 (void)alarm(interval); 349 } 350 /*NOTREACHED*/ 351 } 352 353 void 354 printhdr() 355 { 356 printf(" %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s\n", 357 "Getattr", "Lookup", "Readlink", "Read", "Write", "Rename", 358 "Link", "Readdir"); 359 fflush(stdout); 360 } 361 362 /* 363 * Called if an interval expires before sidewaysintpr has completed a loop. 364 * Sets a flag to not wait for the alarm. 365 */ 366 void 367 catchalarm() 368 { 369 signalled = 1; 370 } 371 372 void 373 usage() 374 { 375 (void)fprintf(stderr, 376 "usage: nfsstat [-M core] [-N system] [-w interval]\n"); 377 exit(1); 378 } 379