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