1 /* 2 * Copyright (c) 1980, 1986, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#) Copyright (c) 1980, 1986, 1991, 1993 The Regents of the University of California. All rights reserved. 34 * @(#)vmstat.c 8.1 (Berkeley) 6/6/93 35 * $FreeBSD: src/usr.bin/vmstat/vmstat.c,v 1.38.2.4 2001/07/31 19:52:41 tmm Exp $ 36 * $DragonFly: src/usr.bin/vmstat/vmstat.c,v 1.2 2003/06/17 04:29:33 dillon Exp $ 37 */ 38 39 #include <sys/param.h> 40 #include <sys/time.h> 41 #include <sys/proc.h> 42 #include <sys/dkstat.h> 43 #include <sys/uio.h> 44 #include <sys/namei.h> 45 #include <sys/malloc.h> 46 #include <sys/signal.h> 47 #include <sys/fcntl.h> 48 #include <sys/ioctl.h> 49 #include <sys/sysctl.h> 50 #include <sys/vmmeter.h> 51 52 #include <vm/vm_param.h> 53 54 #include <ctype.h> 55 #include <err.h> 56 #include <errno.h> 57 #include <kvm.h> 58 #include <limits.h> 59 #include <nlist.h> 60 #include <paths.h> 61 #include <stdio.h> 62 #include <stdlib.h> 63 #include <string.h> 64 #include <sysexits.h> 65 #include <time.h> 66 #include <unistd.h> 67 #include <devstat.h> 68 69 struct nlist namelist[] = { 70 #define X_CPTIME 0 71 { "_cp_time" }, 72 #define X_SUM 1 73 { "_cnt" }, 74 #define X_BOOTTIME 2 75 { "_boottime" }, 76 #define X_HZ 3 77 { "_hz" }, 78 #define X_STATHZ 4 79 { "_stathz" }, 80 #define X_NCHSTATS 5 81 { "_nchstats" }, 82 #define X_INTRNAMES 6 83 { "_intrnames" }, 84 #define X_EINTRNAMES 7 85 { "_eintrnames" }, 86 #define X_INTRCNT 8 87 { "_intrcnt" }, 88 #define X_EINTRCNT 9 89 { "_eintrcnt" }, 90 #define X_KMEMSTATISTICS 10 91 { "_kmemstatistics" }, 92 #define X_KMEMBUCKETS 11 93 { "_bucket" }, 94 #define X_ZLIST 12 95 { "_zlist" }, 96 #ifdef notyet 97 #define X_DEFICIT 13 98 { "_deficit" }, 99 #define X_FORKSTAT 14 100 { "_forkstat" }, 101 #define X_REC 15 102 { "_rectime" }, 103 #define X_PGIN 16 104 { "_pgintime" }, 105 #define X_XSTATS 17 106 { "_xstats" }, 107 #define X_END 18 108 #else 109 #define X_END 13 110 #endif 111 { "" }, 112 }; 113 114 struct statinfo cur, last; 115 int num_devices, maxshowdevs; 116 long generation; 117 struct device_selection *dev_select; 118 int num_selected; 119 struct devstat_match *matches; 120 int num_matches = 0; 121 int num_devices_specified, num_selections; 122 long select_generation; 123 char **specified_devices; 124 devstat_select_mode select_mode; 125 126 struct vmmeter sum, osum; 127 128 int winlines = 20; 129 int nflag = 0; 130 131 kvm_t *kd; 132 133 #define FORKSTAT 0x01 134 #define INTRSTAT 0x02 135 #define MEMSTAT 0x04 136 #define SUMSTAT 0x08 137 #define TIMESTAT 0x10 138 #define VMSTAT 0x20 139 #define ZMEMSTAT 0x40 140 141 void cpustats(), dointr(), domem(), dosum(), dozmem(); 142 void dovmstat(), kread(), usage(); 143 #ifdef notyet 144 void dotimes(), doforkst(); 145 #endif 146 void printhdr __P((void)); 147 static void devstats(); 148 149 int 150 main(argc, argv) 151 register int argc; 152 register char **argv; 153 { 154 register int c, todo; 155 u_int interval; 156 int reps; 157 char *memf, *nlistf; 158 char errbuf[_POSIX2_LINE_MAX]; 159 160 memf = nlistf = NULL; 161 interval = reps = todo = 0; 162 maxshowdevs = 2; 163 while ((c = getopt(argc, argv, "c:fiM:mN:n:p:stw:z")) != -1) { 164 switch (c) { 165 case 'c': 166 reps = atoi(optarg); 167 break; 168 case 'f': 169 #ifdef notyet 170 todo |= FORKSTAT; 171 #else 172 errx(EX_USAGE, "sorry, -f is not (re)implemented yet"); 173 #endif 174 break; 175 case 'i': 176 todo |= INTRSTAT; 177 break; 178 case 'M': 179 memf = optarg; 180 break; 181 case 'm': 182 todo |= MEMSTAT; 183 break; 184 case 'N': 185 nlistf = optarg; 186 break; 187 case 'n': 188 nflag = 1; 189 maxshowdevs = atoi(optarg); 190 if (maxshowdevs < 0) 191 errx(1, "number of devices %d is < 0", 192 maxshowdevs); 193 break; 194 case 'p': 195 if (buildmatch(optarg, &matches, &num_matches) != 0) 196 errx(1, "%s", devstat_errbuf); 197 break; 198 case 's': 199 todo |= SUMSTAT; 200 break; 201 case 't': 202 #ifdef notyet 203 todo |= TIMESTAT; 204 #else 205 errx(EX_USAGE, "sorry, -t is not (re)implemented yet"); 206 #endif 207 break; 208 case 'w': 209 interval = atoi(optarg); 210 break; 211 case 'z': 212 todo |= ZMEMSTAT; 213 break; 214 case '?': 215 default: 216 usage(); 217 } 218 } 219 argc -= optind; 220 argv += optind; 221 222 if (todo == 0) 223 todo = VMSTAT; 224 225 /* 226 * Discard setgid privileges if not the running kernel so that bad 227 * guys can't print interesting stuff from kernel memory. 228 */ 229 if (nlistf != NULL || memf != NULL) 230 setgid(getgid()); 231 232 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 233 if (kd == 0) 234 errx(1, "kvm_openfiles: %s", errbuf); 235 236 if ((c = kvm_nlist(kd, namelist)) != 0) { 237 if (c > 0) { 238 warnx("undefined symbols:"); 239 for (c = 0; 240 c < sizeof(namelist)/sizeof(namelist[0]); c++) 241 if (namelist[c].n_type == 0) 242 fprintf(stderr, " %s", 243 namelist[c].n_name); 244 (void)fputc('\n', stderr); 245 } else 246 warnx("kvm_nlist: %s", kvm_geterr(kd)); 247 exit(1); 248 } 249 250 if (todo & VMSTAT) { 251 char **getdrivedata(); 252 struct winsize winsize; 253 254 /* 255 * Make sure that the userland devstat version matches the 256 * kernel devstat version. If not, exit and print a 257 * message informing the user of his mistake. 258 */ 259 if (checkversion() < 0) 260 errx(1, "%s", devstat_errbuf); 261 262 263 argv = getdrivedata(argv); 264 winsize.ws_row = 0; 265 (void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize); 266 if (winsize.ws_row > 0) 267 winlines = winsize.ws_row; 268 269 } 270 271 #define BACKWARD_COMPATIBILITY 272 #ifdef BACKWARD_COMPATIBILITY 273 if (*argv) { 274 interval = atoi(*argv); 275 if (*++argv) 276 reps = atoi(*argv); 277 } 278 #endif 279 280 if (interval) { 281 if (!reps) 282 reps = -1; 283 } else if (reps) 284 interval = 1; 285 286 #ifdef notyet 287 if (todo & FORKSTAT) 288 doforkst(); 289 #endif 290 if (todo & MEMSTAT) 291 domem(); 292 if (todo & ZMEMSTAT) 293 dozmem(); 294 if (todo & SUMSTAT) 295 dosum(); 296 #ifdef notyet 297 if (todo & TIMESTAT) 298 dotimes(); 299 #endif 300 if (todo & INTRSTAT) 301 dointr(); 302 if (todo & VMSTAT) 303 dovmstat(interval, reps); 304 exit(0); 305 } 306 307 char ** 308 getdrivedata(argv) 309 char **argv; 310 { 311 if ((num_devices = getnumdevs()) < 0) 312 errx(1, "%s", devstat_errbuf); 313 314 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 315 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 316 bzero(cur.dinfo, sizeof(struct devinfo)); 317 bzero(last.dinfo, sizeof(struct devinfo)); 318 319 if (getdevs(&cur) == -1) 320 errx(1, "%s", devstat_errbuf); 321 322 num_devices = cur.dinfo->numdevs; 323 generation = cur.dinfo->generation; 324 325 specified_devices = (char **)malloc(sizeof(char *)); 326 for (num_devices_specified = 0; *argv; ++argv) { 327 if (isdigit(**argv)) 328 break; 329 num_devices_specified++; 330 specified_devices = (char **)realloc(specified_devices, 331 sizeof(char *) * 332 num_devices_specified); 333 specified_devices[num_devices_specified - 1] = *argv; 334 } 335 dev_select = NULL; 336 337 if (nflag == 0 && maxshowdevs < num_devices_specified) 338 maxshowdevs = num_devices_specified; 339 340 /* 341 * People are generally only interested in disk statistics when 342 * they're running vmstat. So, that's what we're going to give 343 * them if they don't specify anything by default. We'll also give 344 * them any other random devices in the system so that we get to 345 * maxshowdevs devices, if that many devices exist. If the user 346 * specifies devices on the command line, either through a pattern 347 * match or by naming them explicitly, we will give the user only 348 * those devices. 349 */ 350 if ((num_devices_specified == 0) && (num_matches == 0)) { 351 if (buildmatch("da", &matches, &num_matches) != 0) 352 errx(1, "%s", devstat_errbuf); 353 354 select_mode = DS_SELECT_ADD; 355 } else 356 select_mode = DS_SELECT_ONLY; 357 358 /* 359 * At this point, selectdevs will almost surely indicate that the 360 * device list has changed, so we don't look for return values of 0 361 * or 1. If we get back -1, though, there is an error. 362 */ 363 if (selectdevs(&dev_select, &num_selected, &num_selections, 364 &select_generation, generation, cur.dinfo->devices, 365 num_devices, matches, num_matches, specified_devices, 366 num_devices_specified, select_mode, 367 maxshowdevs, 0) == -1) 368 errx(1, "%s", devstat_errbuf); 369 370 return(argv); 371 } 372 373 long 374 getuptime() 375 { 376 static time_t now, boottime; 377 time_t uptime; 378 379 if (boottime == 0) 380 kread(X_BOOTTIME, &boottime, sizeof(boottime)); 381 (void)time(&now); 382 uptime = now - boottime; 383 if (uptime <= 0 || uptime > 60*60*24*365*10) 384 errx(1, "time makes no sense; namelist must be wrong"); 385 return(uptime); 386 } 387 388 int hz, hdrcnt; 389 390 void 391 dovmstat(interval, reps) 392 u_int interval; 393 int reps; 394 { 395 struct vmtotal total; 396 time_t uptime, halfuptime; 397 struct devinfo *tmp_dinfo; 398 void needhdr(); 399 int mib[2]; 400 size_t size; 401 402 uptime = getuptime(); 403 halfuptime = uptime / 2; 404 (void)signal(SIGCONT, needhdr); 405 406 if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0) 407 kread(X_STATHZ, &hz, sizeof(hz)); 408 if (!hz) 409 kread(X_HZ, &hz, sizeof(hz)); 410 411 for (hdrcnt = 1;;) { 412 if (!--hdrcnt) 413 printhdr(); 414 kread(X_CPTIME, cur.cp_time, sizeof(cur.cp_time)); 415 416 tmp_dinfo = last.dinfo; 417 last.dinfo = cur.dinfo; 418 cur.dinfo = tmp_dinfo; 419 last.busy_time = cur.busy_time; 420 421 /* 422 * Here what we want to do is refresh our device stats. 423 * getdevs() returns 1 when the device list has changed. 424 * If the device list has changed, we want to go through 425 * the selection process again, in case a device that we 426 * were previously displaying has gone away. 427 */ 428 switch (getdevs(&cur)) { 429 case -1: 430 errx(1, "%s", devstat_errbuf); 431 break; 432 case 1: { 433 int retval; 434 435 num_devices = cur.dinfo->numdevs; 436 generation = cur.dinfo->generation; 437 438 retval = selectdevs(&dev_select, &num_selected, 439 &num_selections, &select_generation, 440 generation, cur.dinfo->devices, 441 num_devices, matches, num_matches, 442 specified_devices, 443 num_devices_specified, select_mode, 444 maxshowdevs, 0); 445 switch (retval) { 446 case -1: 447 errx(1, "%s", devstat_errbuf); 448 break; 449 case 1: 450 printhdr(); 451 break; 452 default: 453 break; 454 } 455 } 456 default: 457 break; 458 } 459 460 kread(X_SUM, &sum, sizeof(sum)); 461 size = sizeof(total); 462 mib[0] = CTL_VM; 463 mib[1] = VM_METER; 464 if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) { 465 printf("Can't get kerninfo: %s\n", strerror(errno)); 466 bzero(&total, sizeof(total)); 467 } 468 (void)printf("%2d %1d %1d", 469 total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw); 470 #define vmstat_pgtok(a) ((a) * sum.v_page_size >> 10) 471 #define rate(x) (((x) + halfuptime) / uptime) /* round */ 472 (void)printf(" %7ld %6ld ", 473 (long)vmstat_pgtok(total.t_avm), (long)vmstat_pgtok(total.t_free)); 474 (void)printf("%4lu ", 475 (u_long)rate(sum.v_vm_faults - osum.v_vm_faults)); 476 (void)printf("%3lu ", 477 (u_long)rate(sum.v_reactivated - osum.v_reactivated)); 478 (void)printf("%3lu ", 479 (u_long)rate(sum.v_swapin + sum.v_vnodein - 480 (osum.v_swapin + osum.v_vnodein))); 481 (void)printf("%3lu ", 482 (u_long)rate(sum.v_swapout + sum.v_vnodeout - 483 (osum.v_swapout + osum.v_vnodeout))); 484 (void)printf("%3lu ", 485 (u_long)rate(sum.v_tfree - osum.v_tfree)); 486 (void)printf("%3lu ", 487 (u_long)rate(sum.v_pdpages - osum.v_pdpages)); 488 devstats(); 489 (void)printf("%4lu %4lu %3lu ", 490 (u_long)rate(sum.v_intr - osum.v_intr), 491 (u_long)rate(sum.v_syscall - osum.v_syscall), 492 (u_long)rate(sum.v_swtch - osum.v_swtch)); 493 cpustats(); 494 (void)printf("\n"); 495 (void)fflush(stdout); 496 if (reps >= 0 && --reps <= 0) 497 break; 498 osum = sum; 499 uptime = interval; 500 /* 501 * We round upward to avoid losing low-frequency events 502 * (i.e., >= 1 per interval but < 1 per second). 503 */ 504 if (interval != 1) 505 halfuptime = (uptime + 1) / 2; 506 else 507 halfuptime = 0; 508 (void)sleep(interval); 509 } 510 } 511 512 void 513 printhdr() 514 { 515 int i, num_shown; 516 517 num_shown = (num_selected < maxshowdevs) ? num_selected : maxshowdevs; 518 (void)printf(" procs memory page%*s", 19, ""); 519 if (num_shown > 1) 520 (void)printf(" disks %*s", num_shown * 4 - 7, ""); 521 else if (num_shown == 1) 522 (void)printf("disk"); 523 (void)printf(" faults cpu\n"); 524 (void)printf(" r b w avm fre flt re pi po fr sr "); 525 for (i = 0; i < num_devices; i++) 526 if ((dev_select[i].selected) 527 && (dev_select[i].selected <= maxshowdevs)) 528 (void)printf("%c%c%d ", dev_select[i].device_name[0], 529 dev_select[i].device_name[1], 530 dev_select[i].unit_number); 531 (void)printf(" in sy cs us sy id\n"); 532 hdrcnt = winlines - 2; 533 } 534 535 /* 536 * Force a header to be prepended to the next output. 537 */ 538 void 539 needhdr() 540 { 541 542 hdrcnt = 1; 543 } 544 545 #ifdef notyet 546 void 547 dotimes() 548 { 549 u_int pgintime, rectime; 550 551 kread(X_REC, &rectime, sizeof(rectime)); 552 kread(X_PGIN, &pgintime, sizeof(pgintime)); 553 kread(X_SUM, &sum, sizeof(sum)); 554 (void)printf("%u reclaims, %u total time (usec)\n", 555 sum.v_pgrec, rectime); 556 (void)printf("average: %u usec / reclaim\n", rectime / sum.v_pgrec); 557 (void)printf("\n"); 558 (void)printf("%u page ins, %u total time (msec)\n", 559 sum.v_pgin, pgintime / 10); 560 (void)printf("average: %8.1f msec / page in\n", 561 pgintime / (sum.v_pgin * 10.0)); 562 } 563 #endif 564 565 long 566 pct(top, bot) 567 long top, bot; 568 { 569 long ans; 570 571 if (bot == 0) 572 return(0); 573 ans = (quad_t)top * 100 / bot; 574 return (ans); 575 } 576 577 #define PCT(top, bot) pct((long)(top), (long)(bot)) 578 579 void 580 dosum() 581 { 582 struct nchstats nchstats; 583 long nchtotal; 584 585 kread(X_SUM, &sum, sizeof(sum)); 586 (void)printf("%9u cpu context switches\n", sum.v_swtch); 587 (void)printf("%9u device interrupts\n", sum.v_intr); 588 (void)printf("%9u software interrupts\n", sum.v_soft); 589 (void)printf("%9u traps\n", sum.v_trap); 590 (void)printf("%9u system calls\n", sum.v_syscall); 591 (void)printf("%9u kernel threads created\n", sum.v_kthreads); 592 (void)printf("%9u fork() calls\n", sum.v_forks); 593 (void)printf("%9u vfork() calls\n", sum.v_vforks); 594 (void)printf("%9u rfork() calls\n", sum.v_rforks); 595 (void)printf("%9u swap pager pageins\n", sum.v_swapin); 596 (void)printf("%9u swap pager pages paged in\n", sum.v_swappgsin); 597 (void)printf("%9u swap pager pageouts\n", sum.v_swapout); 598 (void)printf("%9u swap pager pages paged out\n", sum.v_swappgsout); 599 (void)printf("%9u vnode pager pageins\n", sum.v_vnodein); 600 (void)printf("%9u vnode pager pages paged in\n", sum.v_vnodepgsin); 601 (void)printf("%9u vnode pager pageouts\n", sum.v_vnodeout); 602 (void)printf("%9u vnode pager pages paged out\n", sum.v_vnodepgsout); 603 (void)printf("%9u page daemon wakeups\n", sum.v_pdwakeups); 604 (void)printf("%9u pages examined by the page daemon\n", sum.v_pdpages); 605 (void)printf("%9u pages reactivated\n", sum.v_reactivated); 606 (void)printf("%9u copy-on-write faults\n", sum.v_cow_faults); 607 (void)printf("%9u copy-on-write optimized faults\n", sum.v_cow_optim); 608 (void)printf("%9u zero fill pages zeroed\n", sum.v_zfod); 609 (void)printf("%9u zero fill pages prezeroed\n", sum.v_ozfod); 610 (void)printf("%9u intransit blocking page faults\n", sum.v_intrans); 611 (void)printf("%9u total VM faults taken\n", sum.v_vm_faults); 612 (void)printf("%9u pages affected by kernel thread creation\n", sum.v_kthreadpages); 613 (void)printf("%9u pages affected by fork()\n", sum.v_forkpages); 614 (void)printf("%9u pages affected by vfork()\n", sum.v_vforkpages); 615 (void)printf("%9u pages affected by rfork()\n", sum.v_rforkpages); 616 (void)printf("%9u pages freed\n", sum.v_tfree); 617 (void)printf("%9u pages freed by daemon\n", sum.v_dfree); 618 (void)printf("%9u pages freed by exiting processes\n", sum.v_pfree); 619 (void)printf("%9u pages active\n", sum.v_active_count); 620 (void)printf("%9u pages inactive\n", sum.v_inactive_count); 621 (void)printf("%9u pages in VM cache\n", sum.v_cache_count); 622 (void)printf("%9u pages wired down\n", sum.v_wire_count); 623 (void)printf("%9u pages free\n", sum.v_free_count); 624 (void)printf("%9u bytes per page\n", sum.v_page_size); 625 kread(X_NCHSTATS, &nchstats, sizeof(nchstats)); 626 nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits + 627 nchstats.ncs_badhits + nchstats.ncs_falsehits + 628 nchstats.ncs_miss + nchstats.ncs_long; 629 (void)printf("%9ld total name lookups\n", nchtotal); 630 (void)printf( 631 "%9s cache hits (%ld%% pos + %ld%% neg) system %ld%% per-directory\n", 632 "", PCT(nchstats.ncs_goodhits, nchtotal), 633 PCT(nchstats.ncs_neghits, nchtotal), 634 PCT(nchstats.ncs_pass2, nchtotal)); 635 (void)printf("%9s deletions %ld%%, falsehits %ld%%, toolong %ld%%\n", "", 636 PCT(nchstats.ncs_badhits, nchtotal), 637 PCT(nchstats.ncs_falsehits, nchtotal), 638 PCT(nchstats.ncs_long, nchtotal)); 639 } 640 641 #ifdef notyet 642 void 643 doforkst() 644 { 645 struct forkstat fks; 646 647 kread(X_FORKSTAT, &fks, sizeof(struct forkstat)); 648 (void)printf("%d forks, %d pages, average %.2f\n", 649 fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork); 650 (void)printf("%d vforks, %d pages, average %.2f\n", 651 fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork); 652 } 653 #endif 654 655 static void 656 devstats() 657 { 658 register int dn, state; 659 long double transfers_per_second; 660 long double busy_seconds; 661 long tmp; 662 663 for (state = 0; state < CPUSTATES; ++state) { 664 tmp = cur.cp_time[state]; 665 cur.cp_time[state] -= last.cp_time[state]; 666 last.cp_time[state] = tmp; 667 } 668 669 busy_seconds = compute_etime(cur.busy_time, last.busy_time); 670 671 for (dn = 0; dn < num_devices; dn++) { 672 int di; 673 674 if ((dev_select[dn].selected == 0) 675 || (dev_select[dn].selected > maxshowdevs)) 676 continue; 677 678 di = dev_select[dn].position; 679 680 if (compute_stats(&cur.dinfo->devices[di], 681 &last.dinfo->devices[di], busy_seconds, 682 NULL, NULL, NULL, 683 NULL, &transfers_per_second, NULL, 684 NULL, NULL) != 0) 685 errx(1, "%s", devstat_errbuf); 686 687 printf("%3.0Lf ", transfers_per_second); 688 } 689 } 690 691 void 692 cpustats() 693 { 694 register int state; 695 double pct, total; 696 697 total = 0; 698 for (state = 0; state < CPUSTATES; ++state) 699 total += cur.cp_time[state]; 700 if (total) 701 pct = 100 / total; 702 else 703 pct = 0; 704 (void)printf("%2.0f ", (cur.cp_time[CP_USER] + 705 cur.cp_time[CP_NICE]) * pct); 706 (void)printf("%2.0f ", (cur.cp_time[CP_SYS] + 707 cur.cp_time[CP_INTR]) * pct); 708 (void)printf("%2.0f", cur.cp_time[CP_IDLE] * pct); 709 } 710 711 void 712 dointr() 713 { 714 register u_long *intrcnt, uptime; 715 register u_int64_t inttotal; 716 register int nintr, inamlen; 717 register char *intrname; 718 719 uptime = getuptime(); 720 nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value; 721 inamlen = 722 namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value; 723 intrcnt = malloc((size_t)nintr); 724 intrname = malloc((size_t)inamlen); 725 if (intrcnt == NULL || intrname == NULL) 726 errx(1, "malloc"); 727 kread(X_INTRCNT, intrcnt, (size_t)nintr); 728 kread(X_INTRNAMES, intrname, (size_t)inamlen); 729 (void)printf("interrupt total rate\n"); 730 inttotal = 0; 731 nintr /= sizeof(long); 732 while (--nintr >= 0) { 733 if (*intrcnt) 734 (void)printf("%-12s %20lu %10lu\n", intrname, 735 *intrcnt, *intrcnt / uptime); 736 intrname += strlen(intrname) + 1; 737 inttotal += *intrcnt++; 738 } 739 (void)printf("Total %20llu %10llu\n", inttotal, 740 inttotal / (u_int64_t) uptime); 741 } 742 743 #define MAX_KMSTATS 200 744 745 void 746 domem() 747 { 748 register struct kmembuckets *kp; 749 register struct malloc_type *ks; 750 register int i, j; 751 int len, size, first, nkms; 752 long totuse = 0, totfree = 0, totreq = 0; 753 const char *name; 754 struct malloc_type kmemstats[MAX_KMSTATS], *kmsp; 755 char buf[1024]; 756 struct kmembuckets buckets[MINBUCKET + 16]; 757 758 kread(X_KMEMBUCKETS, buckets, sizeof(buckets)); 759 kread(X_KMEMSTATISTICS, &kmsp, sizeof(kmsp)); 760 for (nkms = 0; nkms < MAX_KMSTATS && kmsp != NULL; nkms++) { 761 if (sizeof(kmemstats[0]) != kvm_read(kd, (u_long)kmsp, 762 &kmemstats[nkms], sizeof(kmemstats[0]))) 763 err(1, "kvm_read(%p)", (void *)kmsp); 764 if (sizeof(buf) != kvm_read(kd, 765 (u_long)kmemstats[nkms].ks_shortdesc, buf, sizeof(buf))) 766 err(1, "kvm_read(%p)", 767 (void *)kmemstats[nkms].ks_shortdesc); 768 buf[sizeof(buf) - 1] = '\0'; 769 kmemstats[nkms].ks_shortdesc = strdup(buf); 770 kmsp = kmemstats[nkms].ks_next; 771 } 772 if (kmsp != NULL) 773 warnx("truncated to the first %d memory types", nkms); 774 (void)printf("Memory statistics by bucket size\n"); 775 (void)printf( 776 "Size In Use Free Requests HighWater Couldfree\n"); 777 for (i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16; i++, kp++) { 778 if (kp->kb_calls == 0) 779 continue; 780 size = 1 << i; 781 if(size < 1024) 782 (void)printf("%4d",size); 783 else 784 (void)printf("%3dK",size>>10); 785 (void)printf(" %8ld %6ld %10lld %7ld %10ld\n", 786 kp->kb_total - kp->kb_totalfree, 787 kp->kb_totalfree, kp->kb_calls, 788 kp->kb_highwat, kp->kb_couldfree); 789 totfree += size * kp->kb_totalfree; 790 } 791 792 (void)printf("\nMemory usage type by bucket size\n"); 793 (void)printf("Size Type(s)\n"); 794 kp = &buckets[MINBUCKET]; 795 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) { 796 if (kp->kb_calls == 0) 797 continue; 798 first = 1; 799 len = 8; 800 for (i = 0, ks = &kmemstats[0]; i < nkms; i++, ks++) { 801 if (ks->ks_calls == 0) 802 continue; 803 if ((ks->ks_size & j) == 0) 804 continue; 805 name = ks->ks_shortdesc; 806 len += 2 + strlen(name); 807 if (first && j < 1024) 808 printf("%4d %s", j, name); 809 else if (first) 810 printf("%3dK %s", j>>10, name); 811 else 812 printf(","); 813 if (len >= 79) { 814 printf("\n\t "); 815 len = 10 + strlen(name); 816 } 817 if (!first) 818 printf(" %s", name); 819 first = 0; 820 } 821 printf("\n"); 822 } 823 824 (void)printf( 825 "\nMemory statistics by type Type Kern\n"); 826 (void)printf( 827 " Type InUse MemUse HighUse Limit Requests Limit Limit Size(s)\n"); 828 for (i = 0, ks = &kmemstats[0]; i < nkms; i++, ks++) { 829 if (ks->ks_calls == 0) 830 continue; 831 (void)printf("%13s%6ld%6ldK%7ldK%6ldK%9lld%5u%6u", 832 ks->ks_shortdesc, 833 ks->ks_inuse, (ks->ks_memuse + 1023) / 1024, 834 (ks->ks_maxused + 1023) / 1024, 835 (ks->ks_limit + 1023) / 1024, ks->ks_calls, 836 ks->ks_limblocks, ks->ks_mapblocks); 837 first = 1; 838 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) { 839 if ((ks->ks_size & j) == 0) 840 continue; 841 if (first) 842 printf(" "); 843 else 844 printf(","); 845 if(j<1024) 846 printf("%d",j); 847 else 848 printf("%dK",j>>10); 849 first = 0; 850 } 851 printf("\n"); 852 totuse += ks->ks_memuse; 853 totreq += ks->ks_calls; 854 } 855 (void)printf("\nMemory Totals: In Use Free Requests\n"); 856 (void)printf(" %7ldK %6ldK %8ld\n", 857 (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq); 858 } 859 860 void 861 dozmem() 862 { 863 char *buf; 864 size_t bufsize; 865 866 buf = NULL; 867 bufsize = 1024; 868 for (;;) { 869 if ((buf = realloc(buf, bufsize)) == NULL) 870 err(1, "realloc()"); 871 if (sysctlbyname("vm.zone", buf, &bufsize, 0, NULL) == 0) 872 break; 873 if (errno != ENOMEM) 874 err(1, "sysctl()"); 875 bufsize *= 2; 876 } 877 buf[bufsize] = '\0'; /* play it safe */ 878 (void)printf("%s\n\n", buf); 879 free(buf); 880 } 881 882 /* 883 * kread reads something from the kernel, given its nlist index. 884 */ 885 void 886 kread(nlx, addr, size) 887 int nlx; 888 void *addr; 889 size_t size; 890 { 891 char *sym; 892 893 if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) { 894 sym = namelist[nlx].n_name; 895 if (*sym == '_') 896 ++sym; 897 errx(1, "symbol %s not defined", sym); 898 } 899 if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) { 900 sym = namelist[nlx].n_name; 901 if (*sym == '_') 902 ++sym; 903 errx(1, "%s: %s", sym, kvm_geterr(kd)); 904 } 905 } 906 907 void 908 usage() 909 { 910 (void)fprintf(stderr, "%s%s", 911 "usage: vmstat [-imsz] [-c count] [-M core] [-N system] [-w wait]\n", 912 " [-n devs] [disks]\n"); 913 exit(1); 914 } 915