1 /* $NetBSD: vmstat.c,v 1.67 2000/06/29 06:29:14 mrg Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Copyright (c) 1980, 1986, 1991, 1993 42 * The Regents of the University of California. All rights reserved. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 3. All advertising materials mentioning features or use of this software 53 * must display the following acknowledgement: 54 * This product includes software developed by the University of 55 * California, Berkeley and its contributors. 56 * 4. Neither the name of the University nor the names of its contributors 57 * may be used to endorse or promote products derived from this software 58 * without specific prior written permission. 59 * 60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 70 * SUCH DAMAGE. 71 */ 72 73 #include <sys/cdefs.h> 74 #ifndef lint 75 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1991, 1993\n\ 76 The Regents of the University of California. All rights reserved.\n"); 77 #endif /* not lint */ 78 79 #ifndef lint 80 #if 0 81 static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 3/1/95"; 82 #else 83 __RCSID("$NetBSD: vmstat.c,v 1.67 2000/06/29 06:29:14 mrg Exp $"); 84 #endif 85 #endif /* not lint */ 86 87 #define __POOL_EXPOSE 88 89 #include <sys/param.h> 90 #include <sys/time.h> 91 #include <sys/proc.h> 92 #include <sys/user.h> 93 #include <sys/dkstat.h> 94 #include <sys/buf.h> 95 #include <sys/namei.h> 96 #include <sys/malloc.h> 97 #include <sys/ioctl.h> 98 #include <sys/sched.h> 99 #include <sys/sysctl.h> 100 #include <sys/device.h> 101 #include <sys/pool.h> 102 103 #include <uvm/uvm_extern.h> 104 #include <uvm/uvm_stat.h> 105 106 #include <err.h> 107 #include <fcntl.h> 108 #include <time.h> 109 #include <nlist.h> 110 #include <kvm.h> 111 #include <errno.h> 112 #include <unistd.h> 113 #include <signal.h> 114 #include <stdio.h> 115 #include <ctype.h> 116 #include <stdlib.h> 117 #include <string.h> 118 #include <paths.h> 119 #include <limits.h> 120 #include "dkstats.h" 121 122 struct nlist namelist[] = { 123 #define X_BOOTTIME 0 124 { "_boottime" }, 125 #define X_HZ 1 126 { "_hz" }, 127 #define X_STATHZ 2 128 { "_stathz" }, 129 #define X_NCHSTATS 3 130 { "_nchstats" }, 131 #define X_INTRNAMES 4 132 { "_intrnames" }, 133 #define X_EINTRNAMES 5 134 { "_eintrnames" }, 135 #define X_INTRCNT 6 136 { "_intrcnt" }, 137 #define X_EINTRCNT 7 138 { "_eintrcnt" }, 139 #define X_KMEMSTAT 8 140 { "_kmemstats" }, 141 #define X_KMEMBUCKETS 9 142 { "_bucket" }, 143 #define X_ALLEVENTS 10 144 { "_allevents" }, 145 #define X_POOLHEAD 11 146 { "_pool_head" }, 147 #define X_UVMEXP 12 148 { "_uvmexp" }, 149 #define X_END 13 150 #if defined(pc532) 151 #define X_IVT (X_END) 152 { "_ivt" }, 153 #endif 154 { "" }, 155 }; 156 157 /* Objects defined in dkstats.c */ 158 extern struct _disk cur; 159 extern char **dr_name; 160 extern int *dk_select, dk_ndrive; 161 162 struct uvmexp uvmexp, ouvmexp; 163 int ndrives; 164 165 int winlines = 20; 166 167 kvm_t *kd; 168 169 #define FORKSTAT 0x01 170 #define INTRSTAT 0x02 171 #define MEMSTAT 0x04 172 #define SUMSTAT 0x08 173 #define EVCNTSTAT 0x10 174 #define VMSTAT 0x20 175 #define HISTLIST 0x40 176 #define HISTDUMP 0x80 177 178 void cpustats __P((void)); 179 void dkstats __P((void)); 180 void doevcnt __P((int verbose)); 181 void dointr __P((int verbose)); 182 void domem __P((void)); 183 void dopool __P((void)); 184 void dosum __P((void)); 185 void dovmstat __P((u_int, int)); 186 void kread __P((int, void *, size_t)); 187 void needhdr __P((int)); 188 long getuptime __P((void)); 189 void printhdr __P((void)); 190 long pct __P((long, long)); 191 void usage __P((void)); 192 void doforkst __P((void)); 193 194 void hist_traverse __P((int, const char *)); 195 void hist_dodump __P((struct uvm_history *)); 196 197 int main __P((int, char **)); 198 char **choosedrives __P((char **)); 199 200 extern int dkinit __P((int, gid_t)); 201 extern void dkreadstats __P((void)); 202 extern void dkswap __P((void)); 203 204 /* Namelist and memory file names. */ 205 char *nlistf, *memf; 206 207 /* allow old usage [vmstat 1] */ 208 #define BACKWARD_COMPATIBILITY 209 210 int 211 main(argc, argv) 212 int argc; 213 char **argv; 214 { 215 int c, todo, verbose; 216 u_int interval; 217 int reps; 218 char errbuf[_POSIX2_LINE_MAX]; 219 gid_t egid = getegid(); 220 const char *histname = NULL; 221 222 (void)setegid(getgid()); 223 memf = nlistf = NULL; 224 interval = reps = todo = verbose = 0; 225 while ((c = getopt(argc, argv, "c:efh:HilM:mN:svw:")) != -1) { 226 switch (c) { 227 case 'c': 228 reps = atoi(optarg); 229 break; 230 case 'e': 231 todo |= EVCNTSTAT; 232 break; 233 case 'f': 234 todo |= FORKSTAT; 235 break; 236 case 'h': 237 histname = optarg; 238 /* FALLTHROUGH */ 239 case 'H': 240 todo |= HISTDUMP; 241 break; 242 case 'i': 243 todo |= INTRSTAT; 244 break; 245 case 'l': 246 todo |= HISTLIST; 247 break; 248 case 'M': 249 memf = optarg; 250 break; 251 case 'm': 252 todo |= MEMSTAT; 253 break; 254 case 'N': 255 nlistf = optarg; 256 break; 257 case 's': 258 todo |= SUMSTAT; 259 break; 260 case 'v': 261 verbose = 1; 262 break; 263 case 'w': 264 interval = atoi(optarg); 265 break; 266 case '?': 267 default: 268 usage(); 269 } 270 } 271 argc -= optind; 272 argv += optind; 273 274 if (todo == 0) 275 todo = VMSTAT; 276 277 /* 278 * Discard setgid privileges. If not the running kernel, we toss 279 * them away totally so that bad guys can't print interesting stuff 280 * from kernel memory, otherwise switch back to kmem for the 281 * duration of the kvm_openfiles() call. 282 */ 283 if (nlistf != NULL || memf != NULL) 284 (void)setgid(getgid()); 285 else 286 (void)setegid(egid); 287 288 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 289 if (kd == 0) 290 errx(1, "kvm_openfiles: %s\n", errbuf); 291 292 if (nlistf == NULL && memf == NULL) { 293 if (todo & VMSTAT) 294 (void)setegid(getgid()); /* XXX: dkinit */ 295 else 296 (void)setgid(getgid()); 297 } 298 299 if ((c = kvm_nlist(kd, namelist)) != 0) { 300 if (c > 0) { 301 (void)fprintf(stderr, 302 "vmstat: undefined symbols:"); 303 for (c = 0; 304 c < sizeof(namelist)/sizeof(namelist[0]); c++) 305 if (namelist[c].n_type == 0) 306 fprintf(stderr, " %s", 307 namelist[c].n_name); 308 (void)fputc('\n', stderr); 309 } else 310 (void)fprintf(stderr, "vmstat: kvm_nlist: %s\n", 311 kvm_geterr(kd)); 312 exit(1); 313 } 314 315 if (todo & VMSTAT) { 316 struct winsize winsize; 317 318 dkinit(0, egid); /* Initialize disk stats, no disks selected. */ 319 320 (void)setgid(getgid()); /* don't need privs anymore */ 321 322 argv = choosedrives(argv); /* Select disks. */ 323 winsize.ws_row = 0; 324 (void)ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize); 325 if (winsize.ws_row > 0) 326 winlines = winsize.ws_row; 327 328 } 329 330 #ifdef BACKWARD_COMPATIBILITY 331 if (*argv) { 332 interval = atoi(*argv); 333 if (*++argv) 334 reps = atoi(*argv); 335 } 336 #endif 337 338 if (interval) { 339 if (!reps) 340 reps = -1; 341 } else if (reps) 342 interval = 1; 343 344 if (todo & (HISTLIST|HISTDUMP)) { 345 if ((todo & (HISTLIST|HISTDUMP)) == (HISTLIST|HISTDUMP)) 346 errx(1, "you may list or dump, but not both!"); 347 hist_traverse(todo, histname); 348 } 349 if (todo & FORKSTAT) 350 doforkst(); 351 if (todo & MEMSTAT) { 352 domem(); 353 dopool(); 354 } 355 if (todo & SUMSTAT) 356 dosum(); 357 if (todo & INTRSTAT) 358 dointr(verbose); 359 if (todo & EVCNTSTAT) 360 doevcnt(verbose); 361 if (todo & VMSTAT) 362 dovmstat(interval, reps); 363 exit(0); 364 } 365 366 char ** 367 choosedrives(argv) 368 char **argv; 369 { 370 int i; 371 372 /* 373 * Choose drives to be displayed. Priority goes to (in order) drives 374 * supplied as arguments, default drives. If everything isn't filled 375 * in and there are drives not taken care of, display the first few 376 * that fit. 377 */ 378 #define BACKWARD_COMPATIBILITY 379 for (ndrives = 0; *argv; ++argv) { 380 #ifdef BACKWARD_COMPATIBILITY 381 if (isdigit(**argv)) 382 break; 383 #endif 384 for (i = 0; i < dk_ndrive; i++) { 385 if (strcmp(dr_name[i], *argv)) 386 continue; 387 dk_select[i] = 1; 388 ++ndrives; 389 break; 390 } 391 } 392 for (i = 0; i < dk_ndrive && ndrives < 4; i++) { 393 if (dk_select[i]) 394 continue; 395 dk_select[i] = 1; 396 ++ndrives; 397 } 398 return(argv); 399 } 400 401 long 402 getuptime() 403 { 404 static time_t now; 405 static struct timeval boottime; 406 time_t uptime; 407 408 if (boottime.tv_sec == 0) 409 kread(X_BOOTTIME, &boottime, sizeof(boottime)); 410 (void)time(&now); 411 uptime = now - boottime.tv_sec; 412 if (uptime <= 0 || uptime > 60*60*24*365*10) { 413 (void)fprintf(stderr, 414 "vmstat: time makes no sense; namelist must be wrong.\n"); 415 exit(1); 416 } 417 return(uptime); 418 } 419 420 int hz, hdrcnt; 421 422 void 423 dovmstat(interval, reps) 424 u_int interval; 425 int reps; 426 { 427 struct vmtotal total; 428 time_t uptime, halfuptime; 429 int mib[2]; 430 size_t size; 431 int pagesize = getpagesize(); 432 433 uptime = getuptime(); 434 halfuptime = uptime / 2; 435 (void)signal(SIGCONT, needhdr); 436 437 if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0) 438 kread(X_STATHZ, &hz, sizeof(hz)); 439 if (!hz) 440 kread(X_HZ, &hz, sizeof(hz)); 441 442 for (hdrcnt = 1;;) { 443 if (!--hdrcnt) 444 printhdr(); 445 /* Read new disk statistics */ 446 dkreadstats(); 447 kread(X_UVMEXP, &uvmexp, sizeof(uvmexp)); 448 if (memf != NULL) { 449 /* 450 * XXX Can't do this if we're reading a crash 451 * XXX dump because they're lazily-calculated. 452 */ 453 printf("Unable to get vmtotals from crash dump.\n"); 454 memset(&total, 0, sizeof(total)); 455 } else { 456 size = sizeof(total); 457 mib[0] = CTL_VM; 458 mib[1] = VM_METER; 459 if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) { 460 printf("Can't get vmtotals: %s\n", 461 strerror(errno)); 462 memset(&total, 0, sizeof(total)); 463 } 464 } 465 (void)printf("%2d%2d%2d", 466 total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw); 467 #define pgtok(a) (long)((a) * (pagesize >> 10)) 468 #define rate(x) (u_long)(((x) + halfuptime) / uptime) /* round */ 469 (void)printf("%6ld%6ld ", 470 pgtok(total.t_avm), pgtok(total.t_free)); 471 (void)printf("%4lu ", rate(uvmexp.faults - ouvmexp.faults)); 472 (void)printf("%3lu ", rate(uvmexp.pdreact - ouvmexp.pdreact)); 473 (void)printf("%3lu ", rate(uvmexp.pageins - ouvmexp.pageins)); 474 (void)printf("%4lu ", 475 rate(uvmexp.pgswapout - ouvmexp.pgswapout)); 476 (void)printf("%4lu ", rate(uvmexp.pdfreed - ouvmexp.pdfreed)); 477 (void)printf("%4lu ", rate(uvmexp.pdscans - ouvmexp.pdscans)); 478 dkstats(); 479 (void)printf("%4lu %4lu %3lu ", 480 rate(uvmexp.intrs - ouvmexp.intrs), 481 rate(uvmexp.syscalls - ouvmexp.syscalls), 482 rate(uvmexp.swtch - ouvmexp.swtch)); 483 cpustats(); 484 (void)printf("\n"); 485 (void)fflush(stdout); 486 if (reps >= 0 && --reps <= 0) 487 break; 488 ouvmexp = uvmexp; 489 uptime = interval; 490 /* 491 * We round upward to avoid losing low-frequency events 492 * (i.e., >= 1 per interval but < 1 per second). 493 */ 494 halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2; 495 (void)sleep(interval); 496 } 497 } 498 499 void 500 printhdr() 501 { 502 int i; 503 504 (void)printf(" procs memory page%*s", 23, ""); 505 if (ndrives > 0) 506 (void)printf("%s %*sfaults cpu\n", 507 ((ndrives > 1) ? "disks" : "disk"), 508 ((ndrives > 1) ? ndrives * 3 - 4 : 0), ""); 509 else 510 (void)printf("%*s faults cpu\n", 511 ndrives * 3, ""); 512 513 (void)printf(" r b w avm fre flt re pi po fr sr "); 514 for (i = 0; i < dk_ndrive; i++) 515 if (dk_select[i]) 516 (void)printf("%c%c ", dr_name[i][0], 517 dr_name[i][strlen(dr_name[i]) - 1]); 518 (void)printf(" in sy cs us sy id\n"); 519 hdrcnt = winlines - 2; 520 } 521 522 /* 523 * Force a header to be prepended to the next output. 524 */ 525 void 526 needhdr(dummy) 527 int dummy; 528 { 529 530 hdrcnt = 1; 531 } 532 533 long 534 pct(top, bot) 535 long top, bot; 536 { 537 long ans; 538 539 if (bot == 0) 540 return(0); 541 ans = (quad_t)top * 100 / bot; 542 return (ans); 543 } 544 545 #define PCT(top, bot) (int)pct((long)(top), (long)(bot)) 546 547 void 548 dosum() 549 { 550 struct nchstats nchstats; 551 long nchtotal; 552 553 kread(X_UVMEXP, &uvmexp, sizeof(uvmexp)); 554 555 (void)printf("%9u bytes per page\n", uvmexp.pagesize); 556 557 (void)printf("%9u pages managed\n", uvmexp.npages); 558 (void)printf("%9u pages free\n", uvmexp.free); 559 (void)printf("%9u pages active\n", uvmexp.active); 560 (void)printf("%9u pages inactive\n", uvmexp.inactive); 561 (void)printf("%9u pages paging\n", uvmexp.paging); 562 (void)printf("%9u pages wired\n", uvmexp.wired); 563 (void)printf("%9u zero pages\n", uvmexp.zeropages); 564 (void)printf("%9u reserve pagedaemon pages\n", 565 uvmexp.reserve_pagedaemon); 566 (void)printf("%9u reserve kernel pages\n", uvmexp.reserve_kernel); 567 568 (void)printf("%9u minimum free pages\n", uvmexp.freemin); 569 (void)printf("%9u target free pages\n", uvmexp.freetarg); 570 (void)printf("%9u target inactive pages\n", uvmexp.inactarg); 571 (void)printf("%9u maximum wired pages\n", uvmexp.wiredmax); 572 573 (void)printf("%9u swap devices\n", uvmexp.nswapdev); 574 (void)printf("%9u swap pages\n", uvmexp.swpages); 575 (void)printf("%9u swap pages in use\n", uvmexp.swpginuse); 576 (void)printf("%9u swap allocations\n", uvmexp.nswget); 577 (void)printf("%9u anons\n", uvmexp.nanon); 578 (void)printf("%9u free anons\n", uvmexp.nfreeanon); 579 580 (void)printf("%9u total faults taken\n", uvmexp.faults); 581 (void)printf("%9u traps\n", uvmexp.traps); 582 (void)printf("%9u device interrupts\n", uvmexp.intrs); 583 (void)printf("%9u cpu context switches\n", uvmexp.swtch); 584 (void)printf("%9u software interrupts\n", uvmexp.softs); 585 (void)printf("%9u system calls\n", uvmexp.syscalls); 586 (void)printf("%9u pagein requests\n", uvmexp.pageins); 587 (void)printf("%9u pageout requests\n", uvmexp.pdpageouts); 588 (void)printf("%9u swap ins\n", uvmexp.swapins); 589 (void)printf("%9u swap outs\n", uvmexp.swapouts); 590 (void)printf("%9u pages swapped in\n", uvmexp.pgswapin); 591 (void)printf("%9u pages swapped out\n", uvmexp.pgswapout); 592 (void)printf("%9u forks total\n", uvmexp.forks); 593 (void)printf("%9u forks blocked parent\n", uvmexp.forks_ppwait); 594 (void)printf("%9u forks shared address space with parent\n", 595 uvmexp.forks_sharevm); 596 (void)printf("%9u pagealloc zero wanted and avail\n", 597 uvmexp.pga_zerohit); 598 (void)printf("%9u pagealloc zero wanted and not avail\n", 599 uvmexp.pga_zeromiss); 600 601 (void)printf("%9u faults with no memory\n", uvmexp.fltnoram); 602 (void)printf("%9u faults with no anons\n", uvmexp.fltnoanon); 603 (void)printf("%9u faults had to wait on pages\n", uvmexp.fltpgwait); 604 (void)printf("%9u faults found released page\n", uvmexp.fltpgrele); 605 (void)printf("%9u faults relock (%u ok)\n", uvmexp.fltrelck, 606 uvmexp.fltrelckok); 607 (void)printf("%9u anon page faults\n", uvmexp.fltanget); 608 (void)printf("%9u anon retry faults\n", uvmexp.fltanretry); 609 (void)printf("%9u amap copy faults\n", uvmexp.fltamcopy); 610 (void)printf("%9u neighbour anon page faults\n", uvmexp.fltnamap); 611 (void)printf("%9u neighbour object page faults\n", uvmexp.fltnomap); 612 (void)printf("%9u locked pager get faults\n", uvmexp.fltlget); 613 (void)printf("%9u unlocked pager get faults\n", uvmexp.fltget); 614 (void)printf("%9u anon faults\n", uvmexp.flt_anon); 615 (void)printf("%9u anon copy on write faults\n", uvmexp.flt_acow); 616 (void)printf("%9u object faults\n", uvmexp.flt_obj); 617 (void)printf("%9u promote copy faults\n", uvmexp.flt_prcopy); 618 (void)printf("%9u promote zero fill faults\n", uvmexp.flt_przero); 619 620 (void)printf("%9u times daemon wokeup\n",uvmexp.pdwoke); 621 (void)printf("%9u revolutions of the clock hand\n", uvmexp.pdrevs); 622 (void)printf("%9u times daemon attempted swapout\n", uvmexp.pdswout); 623 (void)printf("%9u pages freed by daemon\n", uvmexp.pdfreed); 624 (void)printf("%9u pages scanned by daemon\n", uvmexp.pdscans); 625 (void)printf("%9u anonymous pages scanned by daemon\n", uvmexp.pdanscan); 626 (void)printf("%9u object pages scanned by daemon\n", uvmexp.pdobscan); 627 (void)printf("%9u pages reactivated\n", uvmexp.pdreact); 628 (void)printf("%9u pages found busy by daemon\n", uvmexp.pdbusy); 629 (void)printf("%9u total pending pageouts\n", uvmexp.pdpending); 630 (void)printf("%9u pages deactivated\n", uvmexp.pddeact); 631 kread(X_NCHSTATS, &nchstats, sizeof(nchstats)); 632 nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits + 633 nchstats.ncs_badhits + nchstats.ncs_falsehits + 634 nchstats.ncs_miss + nchstats.ncs_long; 635 (void)printf("%9ld total name lookups\n", nchtotal); 636 (void)printf( 637 "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n", 638 "", PCT(nchstats.ncs_goodhits, nchtotal), 639 PCT(nchstats.ncs_neghits, nchtotal), 640 PCT(nchstats.ncs_pass2, nchtotal)); 641 (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "", 642 PCT(nchstats.ncs_badhits, nchtotal), 643 PCT(nchstats.ncs_falsehits, nchtotal), 644 PCT(nchstats.ncs_long, nchtotal)); 645 } 646 647 void 648 doforkst() 649 { 650 651 kread(X_UVMEXP, &uvmexp, sizeof(uvmexp)); 652 653 (void)printf("%u forks total\n", uvmexp.forks); 654 (void)printf("%u forks blocked parent\n", uvmexp.forks_ppwait); 655 (void)printf("%u forks shared address space with parent\n", 656 uvmexp.forks_sharevm); 657 } 658 659 void 660 dkstats() 661 { 662 int dn, state; 663 double etime; 664 665 /* Calculate disk stat deltas. */ 666 dkswap(); 667 etime = 0; 668 for (state = 0; state < CPUSTATES; ++state) { 669 etime += cur.cp_time[state]; 670 } 671 if (etime == 0) 672 etime = 1; 673 etime /= hz; 674 for (dn = 0; dn < dk_ndrive; ++dn) { 675 if (!dk_select[dn]) 676 continue; 677 (void)printf("%2.0f ", cur.dk_xfer[dn] / etime); 678 } 679 } 680 681 void 682 cpustats() 683 { 684 int state; 685 double pct, total; 686 687 total = 0; 688 for (state = 0; state < CPUSTATES; ++state) 689 total += cur.cp_time[state]; 690 if (total) 691 pct = 100 / total; 692 else 693 pct = 0; 694 (void)printf("%2.0f ", (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pct); 695 (void)printf("%2.0f ", (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pct); 696 (void)printf("%2.0f", cur.cp_time[CP_IDLE] * pct); 697 } 698 699 #if defined(pc532) 700 /* To get struct iv ...*/ 701 #define _KERNEL 702 #include <machine/psl.h> 703 #undef _KERNEL 704 void 705 dointr(int verbose) 706 { 707 long i, j, inttotal, uptime; 708 static char iname[64]; 709 struct iv ivt[32], *ivp = ivt; 710 711 iname[63] = '\0'; 712 uptime = getuptime(); 713 kread(X_IVT, ivp, sizeof(ivt)); 714 715 for (i = 0; i < 2; i++) { 716 (void)printf("%sware interrupts:\n", i ? "\nsoft" : "hard"); 717 (void)printf("interrupt total rate\n"); 718 inttotal = 0; 719 for (j = 0; j < 16; j++, ivp++) { 720 if (ivp->iv_vec && ivp->iv_use && 721 (ivp->iv_cnt || verbose)) { 722 if (kvm_read(kd, (u_long)ivp->iv_use, iname, 63) != 63) { 723 (void)fprintf(stderr, "vmstat: iv_use: %s\n", 724 kvm_geterr(kd)); 725 exit(1); 726 } 727 (void)printf("%-12s %8ld %8ld\n", iname, 728 ivp->iv_cnt, ivp->iv_cnt / uptime); 729 inttotal += ivp->iv_cnt; 730 } 731 } 732 (void)printf("Total %8ld %8ld\n", 733 inttotal, inttotal / uptime); 734 } 735 } 736 #else 737 void 738 dointr(int verbose) 739 { 740 long *intrcnt; 741 long long inttotal, uptime; 742 int nintr, inamlen; 743 char *intrname; 744 struct evcntlist allevents; 745 struct evcnt evcnt, *evptr; 746 char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX]; 747 748 uptime = getuptime(); 749 nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value; 750 inamlen = 751 namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value; 752 intrcnt = malloc((size_t)nintr); 753 intrname = malloc((size_t)inamlen); 754 if (intrcnt == NULL || intrname == NULL) { 755 (void)fprintf(stderr, "vmstat: %s.\n", strerror(errno)); 756 exit(1); 757 } 758 kread(X_INTRCNT, intrcnt, (size_t)nintr); 759 kread(X_INTRNAMES, intrname, (size_t)inamlen); 760 (void)printf("%-24s %16s %8s\n", "interrupt", "total", "rate"); 761 inttotal = 0; 762 nintr /= sizeof(long); 763 while (--nintr >= 0) { 764 if (*intrcnt || verbose) 765 (void)printf("%-24s %16lld %8lld\n", intrname, 766 (long long)*intrcnt, 767 (long long)(*intrcnt / uptime)); 768 intrname += strlen(intrname) + 1; 769 inttotal += *intrcnt++; 770 } 771 kread(X_ALLEVENTS, &allevents, sizeof allevents); 772 evptr = allevents.tqh_first; 773 while (evptr) { 774 if (kvm_read(kd, (long)evptr, &evcnt, sizeof evcnt) != sizeof evcnt) { 775 event_chain_trashed: 776 (void)fprintf(stderr, "vmstat: event chain trashed: %s\n", 777 kvm_geterr(kd)); 778 exit(1); 779 } 780 781 evptr = evcnt.ev_list.tqe_next; 782 if (evcnt.ev_type != EVCNT_TYPE_INTR) 783 continue; 784 785 if (evcnt.ev_count == 0 && !verbose) 786 continue; 787 788 if (kvm_read(kd, (long)evcnt.ev_group, evgroup, 789 evcnt.ev_grouplen + 1) != evcnt.ev_grouplen + 1) 790 goto event_chain_trashed; 791 if (kvm_read(kd, (long)evcnt.ev_name, evname, 792 evcnt.ev_namelen + 1) != evcnt.ev_namelen + 1) 793 goto event_chain_trashed; 794 795 (void)printf("%s %s%*s %16lld %8lld\n", evgroup, evname, 796 24 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "", 797 (long long)evcnt.ev_count, 798 (long long)(evcnt.ev_count / uptime)); 799 800 inttotal += evcnt.ev_count++; 801 } 802 (void)printf("%-24s %16lld %8lld\n", "Total", inttotal, 803 (long long)(inttotal / uptime)); 804 } 805 #endif 806 807 void 808 doevcnt(int verbose) 809 { 810 long long uptime; 811 struct evcntlist allevents; 812 struct evcnt evcnt, *evptr; 813 char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX]; 814 815 /* XXX should print type! */ 816 817 uptime = getuptime(); 818 (void)printf("%-24s %16s %8s %s\n", "event", "total", "rate", "type"); 819 kread(X_ALLEVENTS, &allevents, sizeof allevents); 820 evptr = allevents.tqh_first; 821 while (evptr) { 822 if (kvm_read(kd, (long)evptr, &evcnt, sizeof evcnt) != sizeof evcnt) { 823 event_chain_trashed: 824 (void)fprintf(stderr, "vmstat: event chain trashed: %s\n", 825 kvm_geterr(kd)); 826 exit(1); 827 } 828 829 evptr = evcnt.ev_list.tqe_next; 830 if (evcnt.ev_count == 0 && !verbose) 831 continue; 832 833 if (kvm_read(kd, (long)evcnt.ev_group, evgroup, 834 evcnt.ev_grouplen + 1) != evcnt.ev_grouplen + 1) 835 goto event_chain_trashed; 836 if (kvm_read(kd, (long)evcnt.ev_name, evname, 837 evcnt.ev_namelen + 1) != evcnt.ev_namelen + 1) 838 goto event_chain_trashed; 839 840 (void)printf("%s %s%*s %16lld %8lld %s\n", evgroup, evname, 841 24 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "", 842 (long long)evcnt.ev_count, 843 (long long)(evcnt.ev_count / uptime), 844 /* XXX do the following with an array lookup XXX */ 845 (evcnt.ev_type == EVCNT_TYPE_MISC) ? "misc" : 846 ((evcnt.ev_type == EVCNT_TYPE_INTR) ? "intr" : "?")); 847 } 848 } 849 850 /* 851 * These names are defined in <sys/malloc.h>. 852 */ 853 char *kmemnames[] = INITKMEMNAMES; 854 855 void 856 domem() 857 { 858 struct kmembuckets *kp; 859 struct kmemstats *ks; 860 int i, j; 861 int len, size, first; 862 long totuse = 0, totfree = 0, totreq = 0; 863 char *name; 864 struct kmemstats kmemstats[M_LAST]; 865 struct kmembuckets buckets[MINBUCKET + 16]; 866 867 kread(X_KMEMBUCKETS, buckets, sizeof(buckets)); 868 for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16; 869 i++, kp++) { 870 if (kp->kb_calls == 0) 871 continue; 872 if (first) { 873 (void)printf("Memory statistics by bucket size\n"); 874 (void)printf( 875 " Size In Use Free Requests HighWater Couldfree\n"); 876 first = 0; 877 } 878 size = 1 << i; 879 (void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size, 880 kp->kb_total - kp->kb_totalfree, 881 kp->kb_totalfree, kp->kb_calls, 882 kp->kb_highwat, kp->kb_couldfree); 883 totfree += size * kp->kb_totalfree; 884 } 885 886 /* 887 * If kmem statistics are not being gathered by the kernel, 888 * first will still be 1. 889 */ 890 if (first) { 891 printf( 892 "Kmem statistics are not being gathered by the kernel.\n"); 893 return; 894 } 895 896 kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats)); 897 (void)printf("\nMemory usage type by bucket size\n"); 898 (void)printf(" Size Type(s)\n"); 899 kp = &buckets[MINBUCKET]; 900 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) { 901 if (kp->kb_calls == 0) 902 continue; 903 first = 1; 904 len = 8; 905 for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) { 906 if (ks->ks_calls == 0) 907 continue; 908 if ((ks->ks_size & j) == 0) 909 continue; 910 if (kmemnames[i] == 0) { 911 kmemnames[i] = malloc(10); 912 /* strlen("undef/")+3+1);*/ 913 snprintf(kmemnames[i], 10, "undef/%d", i); 914 /* same 10 as above!!! */ 915 } 916 name = kmemnames[i]; 917 len += 2 + strlen(name); 918 if (first) 919 printf("%8d %s", j, name); 920 else 921 printf(","); 922 if (len >= 80) { 923 printf("\n\t "); 924 len = 10 + strlen(name); 925 } 926 if (!first) 927 printf(" %s", name); 928 first = 0; 929 } 930 printf("\n"); 931 } 932 933 (void)printf( 934 "\nMemory statistics by type Type Kern\n"); 935 (void)printf( 936 " Type InUse MemUse HighUse Limit Requests Limit Limit Size(s)\n"); 937 for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) { 938 if (ks->ks_calls == 0) 939 continue; 940 (void)printf("%14s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u", 941 kmemnames[i] ? kmemnames[i] : "undefined", 942 ks->ks_inuse, (ks->ks_memuse + 1023) / 1024, 943 (ks->ks_maxused + 1023) / 1024, 944 (ks->ks_limit + 1023) / 1024, ks->ks_calls, 945 ks->ks_limblocks, ks->ks_mapblocks); 946 first = 1; 947 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) { 948 if ((ks->ks_size & j) == 0) 949 continue; 950 if (first) 951 printf(" %d", j); 952 else 953 printf(",%d", j); 954 first = 0; 955 } 956 printf("\n"); 957 totuse += ks->ks_memuse; 958 totreq += ks->ks_calls; 959 } 960 (void)printf("\nMemory Totals: In Use Free Requests\n"); 961 (void)printf(" %7ldK %6ldK %8ld\n", 962 (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq); 963 } 964 965 void 966 dopool() 967 { 968 int first; 969 long addr; 970 long total = 0, inuse = 0; 971 TAILQ_HEAD(,pool) pool_head; 972 struct pool pool, *pp = &pool; 973 974 kread(X_POOLHEAD, &pool_head, sizeof(pool_head)); 975 addr = (long)TAILQ_FIRST(&pool_head); 976 977 for (first = 1; addr != 0; ) { 978 char name[32], maxp[32]; 979 if (kvm_read(kd, addr, (void *)pp, sizeof *pp) != sizeof *pp) { 980 (void)fprintf(stderr, "vmstat: pool chain trashed: %s\n", 981 kvm_geterr(kd)); 982 exit(1); 983 } 984 if (kvm_read(kd, (long)pp->pr_wchan, name, sizeof name) < 0) { 985 (void)fprintf(stderr, "vmstat: pool name trashed: %s\n", 986 kvm_geterr(kd)); 987 exit(1); 988 } 989 name[31]='\0'; 990 991 if (first) { 992 (void)printf("Memory resource pool statistics\n"); 993 (void)printf( 994 "%-11s%5s%9s%5s%9s%6s%6s%6s%6s%6s%6s%5s\n", 995 "Name", 996 "Size", 997 "Requests", 998 "Fail", 999 "Releases", 1000 "Pgreq", 1001 "Pgrel", 1002 "Npage", 1003 "Hiwat", 1004 "Minpg", 1005 "Maxpg", 1006 "Idle"); 1007 first = 0; 1008 } 1009 if (pp->pr_maxpages == UINT_MAX) 1010 sprintf(maxp, "inf"); 1011 else 1012 sprintf(maxp, "%6u", pp->pr_maxpages); 1013 (void)printf( 1014 "%-11s%5u%9lu%5lu%9lu%6lu%6lu%6d%6d%6d%6s%5lu\n", 1015 name, 1016 pp->pr_size, 1017 pp->pr_nget, 1018 pp->pr_nfail, 1019 pp->pr_nput, 1020 pp->pr_npagealloc, 1021 pp->pr_npagefree, 1022 pp->pr_npages, 1023 pp->pr_hiwat, 1024 pp->pr_minpages, 1025 maxp, 1026 pp->pr_nidle); 1027 1028 inuse += (pp->pr_nget - pp->pr_nput) * pp->pr_size; 1029 total += pp->pr_npages * pp->pr_pagesz; 1030 addr = (long)TAILQ_NEXT(pp, pr_poollist); 1031 } 1032 1033 printf("\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n", 1034 inuse/1024, total/1024, (double)(100 * inuse) / total); 1035 1036 } 1037 1038 /* 1039 * kread reads something from the kernel, given its nlist index. 1040 */ 1041 void 1042 kread(nlx, addr, size) 1043 int nlx; 1044 void *addr; 1045 size_t size; 1046 { 1047 const char *sym; 1048 1049 if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) { 1050 sym = namelist[nlx].n_name; 1051 if (*sym == '_') 1052 ++sym; 1053 (void)fprintf(stderr, 1054 "vmstat: symbol %s not defined\n", sym); 1055 exit(1); 1056 } 1057 if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) { 1058 sym = namelist[nlx].n_name; 1059 if (*sym == '_') 1060 ++sym; 1061 (void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr(kd)); 1062 exit(1); 1063 } 1064 } 1065 1066 struct nlist histnl[] = { 1067 { "_uvm_histories" }, 1068 #define X_UVM_HISTORIES 0 1069 { NULL }, 1070 }; 1071 1072 /* 1073 * Traverse the UVM history buffers, performing the requested action. 1074 * 1075 * Note, we assume that if we're not listing, we're dumping. 1076 */ 1077 void 1078 hist_traverse(todo, histname) 1079 int todo; 1080 const char *histname; 1081 { 1082 struct uvm_history_head histhead; 1083 struct uvm_history hist, *histkva; 1084 char *name = NULL; 1085 size_t namelen = 0; 1086 1087 if (kvm_nlist(kd, histnl) != 0) { 1088 printf("UVM history is not compiled into the kernel.\n"); 1089 return; 1090 } 1091 1092 if (kvm_read(kd, histnl[X_UVM_HISTORIES].n_value, &histhead, 1093 sizeof(histhead)) != sizeof(histhead)) { 1094 warnx("unable to read %s: %s", 1095 histnl[X_UVM_HISTORIES].n_name, kvm_geterr(kd)); 1096 return; 1097 } 1098 1099 if (histhead.lh_first == NULL) { 1100 printf("No active UVM history logs.\n"); 1101 return; 1102 } 1103 1104 if (todo & HISTLIST) 1105 printf("Active UVM histories:"); 1106 1107 for (histkva = histhead.lh_first; histkva != NULL; 1108 histkva = hist.list.le_next) { 1109 if (kvm_read(kd, (u_long)histkva, &hist, sizeof(hist)) != 1110 sizeof(hist)) { 1111 warnx("unable to read history at %p: %s", 1112 histkva, kvm_geterr(kd)); 1113 goto out; 1114 } 1115 1116 if (hist.namelen > namelen) { 1117 if (name != NULL) 1118 free(name); 1119 namelen = hist.namelen; 1120 if ((name = malloc(namelen + 1)) == NULL) 1121 err(1, "malloc history name"); 1122 } 1123 1124 if (kvm_read(kd, (u_long)hist.name, name, namelen) != 1125 namelen) { 1126 warnx("unable to read history name at %p: %s", 1127 hist.name, kvm_geterr(kd)); 1128 goto out; 1129 } 1130 name[namelen] = '\0'; 1131 if (todo & HISTLIST) 1132 printf(" %s", name); 1133 else { 1134 /* 1135 * If we're dumping all histories, do it, else 1136 * check to see if this is the one we want. 1137 */ 1138 if (histname == NULL || strcmp(histname, name) == 0) { 1139 if (histname == NULL) 1140 printf("\nUVM history `%s':\n", name); 1141 hist_dodump(&hist); 1142 } 1143 } 1144 } 1145 1146 if (todo & HISTLIST) 1147 printf("\n"); 1148 1149 out: 1150 if (name != NULL) 1151 free(name); 1152 } 1153 1154 /* 1155 * Actually dump the history buffer at the specified KVA. 1156 */ 1157 void 1158 hist_dodump(histp) 1159 struct uvm_history *histp; 1160 { 1161 struct uvm_history_ent *histents, *e; 1162 size_t histsize; 1163 char *fmt = NULL, *fn = NULL; 1164 size_t fmtlen = 0, fnlen = 0; 1165 int i; 1166 1167 histsize = sizeof(struct uvm_history_ent) * histp->n; 1168 1169 if ((histents = malloc(histsize)) == NULL) 1170 err(1, "malloc history entries"); 1171 1172 memset(histents, 0, histsize); 1173 1174 if (kvm_read(kd, (u_long)histp->e, histents, histsize) != histsize) { 1175 warnx("unable to read history entries at %p: %s", 1176 histp->e, kvm_geterr(kd)); 1177 goto out; 1178 } 1179 1180 i = histp->f; 1181 do { 1182 e = &histents[i]; 1183 if (e->fmt != NULL) { 1184 if (e->fmtlen > fmtlen) { 1185 if (fmt != NULL) 1186 free(fmt); 1187 fmtlen = e->fmtlen; 1188 if ((fmt = malloc(fmtlen + 1)) == NULL) 1189 err(1, "malloc printf format"); 1190 } 1191 if (e->fnlen > fnlen) { 1192 if (fn != NULL) 1193 free(fn); 1194 fnlen = e->fnlen; 1195 if ((fn = malloc(fnlen + 1)) == NULL) 1196 err(1, "malloc function name"); 1197 } 1198 1199 if (kvm_read(kd, (u_long)e->fmt, fmt, fmtlen) 1200 != fmtlen) { 1201 warnx("unable to read printf format " 1202 "at %p: %s", e->fmt, kvm_geterr(kd)); 1203 goto out; 1204 } 1205 fmt[fmtlen] = '\0'; 1206 1207 if (kvm_read(kd, (u_long)e->fn, fn, fnlen) != fnlen) { 1208 warnx("unable to read function name " 1209 "at %p: %s", e->fn, kvm_geterr(kd)); 1210 goto out; 1211 } 1212 fn[fnlen] = '\0'; 1213 1214 printf("%06ld.%06ld ", (long int)e->tv.tv_sec, 1215 (long int)e->tv.tv_usec); 1216 printf("%s#%ld: ", fn, e->call); 1217 printf(fmt, e->v[0], e->v[1], e->v[2], e->v[3]); 1218 printf("\n"); 1219 } 1220 i = (i + 1) % histp->n; 1221 } while (i != histp->f); 1222 1223 out: 1224 free(histents); 1225 if (fmt != NULL) 1226 free(fmt); 1227 if (fn != NULL) 1228 free(fn); 1229 } 1230 1231 void 1232 usage() 1233 { 1234 1235 (void)fprintf(stderr, 1236 "usage: vmstat [-efHilmsv] [-h histname] [-c count] [-M core] \ 1237 [-N system] [-w wait] [disks]\n"); 1238 exit(1); 1239 } 1240