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.23 2008/02/19 18:19:15 thomas Exp $ 37 */ 38 39 #define _KERNEL_STRUCTURES 40 41 #include <sys/param.h> 42 #include <sys/time.h> 43 #include <sys/user.h> 44 #include <sys/uio.h> 45 #include <sys/namei.h> 46 #include <sys/malloc.h> 47 #include <sys/signal.h> 48 #include <sys/fcntl.h> 49 #include <sys/ioctl.h> 50 #include <sys/sysctl.h> 51 #include <sys/vmmeter.h> 52 53 #include <vm/vm_param.h> 54 55 #include <ctype.h> 56 #include <err.h> 57 #include <errno.h> 58 #include <kinfo.h> 59 #include <kvm.h> 60 #include <limits.h> 61 #include <nlist.h> 62 #include <paths.h> 63 #include <stdio.h> 64 #include <stdlib.h> 65 #include <string.h> 66 #include <sysexits.h> 67 #include <time.h> 68 #include <unistd.h> 69 #include <devstat.h> 70 71 static struct nlist namelist[] = { 72 #define X_BOOTTIME 0 73 { "_boottime", 0, 0, 0, 0 }, 74 #define X_NCHSTATS 1 75 { "_nchstats", 0, 0, 0, 0 }, 76 #define X_KMEMSTATISTICS 2 77 { "_kmemstatistics", 0, 0, 0, 0 }, 78 #define X_ZLIST 3 79 { "_zlist", 0, 0, 0, 0 }, 80 #ifdef notyet 81 #define X_DEFICIT 4 82 { "_deficit", 0, 0, 0, 0 }, 83 #define X_FORKSTAT 5 84 { "_forkstat", 0, 0, 0, 0 }, 85 #define X_REC 6 86 { "_rectime", 0, 0, 0, 0 }, 87 #define X_PGIN 7 88 { "_pgintime", 0, 0, 0, 0 }, 89 #define X_XSTATS 8 90 { "_xstats", 0, 0, 0, 0 }, 91 #define X_END 9 92 #else 93 #define X_END 4 94 #endif 95 { "", 0, 0, 0, 0 }, 96 }; 97 98 struct statinfo cur, last; 99 int num_devices, maxshowdevs; 100 long generation; 101 struct device_selection *dev_select; 102 int num_selected; 103 struct devstat_match *matches; 104 int num_matches = 0; 105 int num_devices_specified, num_selections; 106 long select_generation; 107 char **specified_devices; 108 devstat_select_mode select_mode; 109 110 struct vmmeter vmm, ovmm; 111 struct vmstats vms, ovms; 112 113 int winlines = 20; 114 int nflag = 0; 115 int verbose = 0; 116 117 kvm_t *kd; 118 119 struct kinfo_cputime cp_time, old_cp_time, diff_cp_time; 120 121 #define FORKSTAT 0x01 122 #define INTRSTAT 0x02 123 #define MEMSTAT 0x04 124 #define SUMSTAT 0x08 125 #define TIMESTAT 0x10 126 #define VMSTAT 0x20 127 #define ZMEMSTAT 0x40 128 129 static void cpustats(void); 130 static void dointr(void); 131 static void domem(void); 132 static void dosum(void); 133 static void dozmem(void); 134 static void dovmstat(u_int, int); 135 static void kread(int, void *, size_t); 136 static void usage(void); 137 static char **getdrivedata(char **); 138 static long getuptime(void); 139 static void needhdr(int); 140 static long pct(long, long); 141 142 #ifdef notyet 143 static void dotimes(void); /* Not implemented */ 144 static void doforkst(void); 145 #endif 146 static void printhdr(void); 147 static void devstats(void); 148 149 int 150 main(int argc, char **argv) 151 { 152 int c, todo; 153 u_int interval; /* milliseconds */ 154 int reps; 155 char *memf, *nlistf; 156 char errbuf[_POSIX2_LINE_MAX]; 157 158 memf = nlistf = NULL; 159 interval = reps = todo = 0; 160 maxshowdevs = 2; 161 while ((c = getopt(argc, argv, "c:fiM:mN:n:p:stvw:z")) != -1) { 162 switch (c) { 163 case 'c': 164 reps = atoi(optarg); 165 break; 166 case 'f': 167 #ifdef notyet 168 todo |= FORKSTAT; 169 #else 170 errx(EX_USAGE, "sorry, -f is not (re)implemented yet"); 171 #endif 172 break; 173 case 'i': 174 todo |= INTRSTAT; 175 break; 176 case 'M': 177 memf = optarg; 178 break; 179 case 'm': 180 todo |= MEMSTAT; 181 break; 182 case 'N': 183 nlistf = optarg; 184 break; 185 case 'n': 186 nflag = 1; 187 maxshowdevs = atoi(optarg); 188 if (maxshowdevs < 0) 189 errx(1, "number of devices %d is < 0", 190 maxshowdevs); 191 break; 192 case 'p': 193 if (buildmatch(optarg, &matches, &num_matches) != 0) 194 errx(1, "%s", devstat_errbuf); 195 break; 196 case 's': 197 todo |= SUMSTAT; 198 break; 199 case 't': 200 #ifdef notyet 201 todo |= TIMESTAT; 202 #else 203 errx(EX_USAGE, "sorry, -t is not (re)implemented yet"); 204 #endif 205 break; 206 case 'v': 207 ++verbose; 208 break; 209 case 'w': 210 interval = (u_int)(strtod(optarg, NULL) * 1000.0); 211 break; 212 case 'z': 213 todo |= ZMEMSTAT; 214 break; 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; c < (int)__arysize(namelist); c++) 240 if (namelist[c].n_type == 0) 241 fprintf(stderr, " %s", 242 namelist[c].n_name); 243 fputc('\n', stderr); 244 } else 245 warnx("kvm_nlist: %s", kvm_geterr(kd)); 246 exit(1); 247 } 248 249 if (todo & VMSTAT) { 250 struct winsize winsize; 251 252 /* 253 * Make sure that the userland devstat version matches the 254 * kernel devstat version. If not, exit and print a 255 * message informing the user of his mistake. 256 */ 257 if (checkversion() < 0) 258 errx(1, "%s", devstat_errbuf); 259 260 261 argv = getdrivedata(argv); 262 winsize.ws_row = 0; 263 ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize); 264 if (winsize.ws_row > 0) 265 winlines = winsize.ws_row; 266 267 } 268 269 #define BACKWARD_COMPATIBILITY 270 #ifdef BACKWARD_COMPATIBILITY 271 if (*argv) { 272 interval = (u_int)(strtod(*argv, NULL) * 1000.0); 273 if (*++argv) 274 reps = atoi(*argv); 275 } 276 #endif 277 278 if (interval) { 279 if (!reps) 280 reps = -1; 281 } else if (reps) { 282 interval = 1000; 283 } 284 285 #ifdef notyet 286 if (todo & FORKSTAT) 287 doforkst(); 288 #endif 289 if (todo & MEMSTAT) 290 domem(); 291 if (todo & ZMEMSTAT) 292 dozmem(); 293 if (todo & SUMSTAT) 294 dosum(); 295 #ifdef notyet 296 if (todo & TIMESTAT) 297 dotimes(); 298 #endif 299 if (todo & INTRSTAT) 300 dointr(); 301 if (todo & VMSTAT) 302 dovmstat(interval, reps); 303 exit(0); 304 } 305 306 static char ** 307 getdrivedata(char **argv) 308 { 309 if ((num_devices = getnumdevs()) < 0) 310 errx(1, "%s", devstat_errbuf); 311 312 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 313 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 314 bzero(cur.dinfo, sizeof(struct devinfo)); 315 bzero(last.dinfo, sizeof(struct devinfo)); 316 317 if (getdevs(&cur) == -1) 318 errx(1, "%s", devstat_errbuf); 319 320 num_devices = cur.dinfo->numdevs; 321 generation = cur.dinfo->generation; 322 323 specified_devices = (char **)malloc(sizeof(char *)); 324 for (num_devices_specified = 0; *argv; ++argv) { 325 if (isdigit(**argv)) 326 break; 327 num_devices_specified++; 328 specified_devices = (char **)realloc(specified_devices, 329 sizeof(char *) * 330 num_devices_specified); 331 specified_devices[num_devices_specified - 1] = *argv; 332 } 333 dev_select = NULL; 334 335 if (nflag == 0 && maxshowdevs < num_devices_specified) 336 maxshowdevs = num_devices_specified; 337 338 /* 339 * People are generally only interested in disk statistics when 340 * they're running vmstat. So, that's what we're going to give 341 * them if they don't specify anything by default. We'll also give 342 * them any other random devices in the system so that we get to 343 * maxshowdevs devices, if that many devices exist. If the user 344 * specifies devices on the command line, either through a pattern 345 * match or by naming them explicitly, we will give the user only 346 * those devices. 347 */ 348 if ((num_devices_specified == 0) && (num_matches == 0)) { 349 if (buildmatch("da", &matches, &num_matches) != 0) 350 errx(1, "%s", devstat_errbuf); 351 352 select_mode = DS_SELECT_ADD; 353 } else 354 select_mode = DS_SELECT_ONLY; 355 356 /* 357 * At this point, selectdevs will almost surely indicate that the 358 * device list has changed, so we don't look for return values of 0 359 * or 1. If we get back -1, though, there is an error. 360 */ 361 if (selectdevs(&dev_select, &num_selected, &num_selections, 362 &select_generation, generation, cur.dinfo->devices, 363 num_devices, matches, num_matches, specified_devices, 364 num_devices_specified, select_mode, 365 maxshowdevs, 0) == -1) 366 errx(1, "%s", devstat_errbuf); 367 368 return(argv); 369 } 370 371 static long 372 getuptime(void) 373 { 374 static time_t now, boottime; 375 time_t uptime; 376 377 if (boottime == 0) 378 kread(X_BOOTTIME, &boottime, sizeof(boottime)); 379 time(&now); 380 uptime = now - boottime; 381 if (uptime <= 0 || uptime > 60*60*24*365*10) 382 errx(1, "time makes no sense; namelist must be wrong"); 383 return(uptime); 384 } 385 386 int hdrcnt; 387 388 static void 389 dovmstat(u_int interval, int reps) 390 { 391 struct vmtotal total; 392 struct devinfo *tmp_dinfo; 393 size_t vmm_size = sizeof(vmm); 394 size_t vms_size = sizeof(vms); 395 size_t vmt_size = sizeof(total); 396 int initial = 1; 397 398 signal(SIGCONT, needhdr); 399 400 for (hdrcnt = 1;;) { 401 if (!--hdrcnt) 402 printhdr(); 403 if (kinfo_get_sched_cputime(&cp_time)) 404 err(1, "kinfo_get_sched_cputime"); 405 406 tmp_dinfo = last.dinfo; 407 last.dinfo = cur.dinfo; 408 cur.dinfo = tmp_dinfo; 409 last.busy_time = cur.busy_time; 410 411 /* 412 * Here what we want to do is refresh our device stats. 413 * getdevs() returns 1 when the device list has changed. 414 * If the device list has changed, we want to go through 415 * the selection process again, in case a device that we 416 * were previously displaying has gone away. 417 */ 418 switch (getdevs(&cur)) { 419 case -1: 420 errx(1, "%s", devstat_errbuf); 421 break; 422 case 1: { 423 int retval; 424 425 num_devices = cur.dinfo->numdevs; 426 generation = cur.dinfo->generation; 427 428 retval = selectdevs(&dev_select, &num_selected, 429 &num_selections, &select_generation, 430 generation, cur.dinfo->devices, 431 num_devices, matches, num_matches, 432 specified_devices, 433 num_devices_specified, select_mode, 434 maxshowdevs, 0); 435 switch (retval) { 436 case -1: 437 errx(1, "%s", devstat_errbuf); 438 break; 439 case 1: 440 printhdr(); 441 break; 442 default: 443 break; 444 } 445 } 446 default: 447 break; 448 } 449 450 if (sysctlbyname("vm.vmstats", &vms, &vms_size, NULL, 0)) { 451 perror("sysctlbyname: vm.vmstats"); 452 exit(1); 453 } 454 if (sysctlbyname("vm.vmmeter", &vmm, &vmm_size, NULL, 0)) { 455 perror("sysctlbyname: vm.vmmeter"); 456 exit(1); 457 } 458 if (sysctlbyname("vm.vmtotal", &total, &vmt_size, NULL, 0)) { 459 perror("sysctlbyname: vm.vmtotal"); 460 exit(1); 461 } 462 printf("%2d %1d %1d", 463 total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw); 464 #define vmstat_pgtok(a) ((a) * vms.v_page_size >> 10) 465 #define rate(x) (initial ? (x) : ((x) * 1000 + interval / 2) / interval) 466 printf(" %7ld %6ld ", 467 (long)vmstat_pgtok(total.t_avm), (long)vmstat_pgtok(total.t_free)); 468 printf("%4lu ", 469 (u_long)rate(vmm.v_vm_faults - ovmm.v_vm_faults)); 470 printf("%3lu ", 471 (u_long)rate(vmm.v_reactivated - ovmm.v_reactivated)); 472 printf("%3lu ", 473 (u_long)rate(vmm.v_swapin + vmm.v_vnodein - 474 (ovmm.v_swapin + ovmm.v_vnodein))); 475 printf("%3lu ", 476 (u_long)rate(vmm.v_swapout + vmm.v_vnodeout - 477 (ovmm.v_swapout + ovmm.v_vnodeout))); 478 printf("%3lu ", 479 (u_long)rate(vmm.v_tfree - ovmm.v_tfree)); 480 printf("%3lu ", 481 (u_long)rate(vmm.v_pdpages - ovmm.v_pdpages)); 482 devstats(); 483 printf("%4lu %4lu %3lu ", 484 (u_long)rate(vmm.v_intr - ovmm.v_intr), 485 (u_long)rate(vmm.v_syscall - ovmm.v_syscall), 486 (u_long)rate(vmm.v_swtch - ovmm.v_swtch)); 487 cpustats(); 488 printf("\n"); 489 fflush(stdout); 490 if (reps >= 0 && --reps <= 0) 491 break; 492 ovmm = vmm; 493 usleep(interval * 1000); 494 initial = 0; 495 } 496 } 497 498 static void 499 printhdr(void) 500 { 501 int i, num_shown; 502 503 num_shown = (num_selected < maxshowdevs) ? num_selected : maxshowdevs; 504 printf(" procs memory page%*s", 19, ""); 505 if (num_shown > 1) 506 printf(" disks %*s", num_shown * 4 - 7, ""); 507 else if (num_shown == 1) 508 printf("disk"); 509 printf(" faults cpu\n"); 510 printf(" r b w avm fre flt re pi po fr sr "); 511 for (i = 0; i < num_devices; i++) 512 if ((dev_select[i].selected) 513 && (dev_select[i].selected <= maxshowdevs)) 514 printf("%c%c%d ", dev_select[i].device_name[0], 515 dev_select[i].device_name[1], 516 dev_select[i].unit_number); 517 printf(" in sy cs us sy id\n"); 518 hdrcnt = winlines - 2; 519 } 520 521 /* 522 * Force a header to be prepended to the next output. 523 */ 524 static void 525 needhdr(__unused int signo) 526 { 527 528 hdrcnt = 1; 529 } 530 531 static long 532 pct(long top, long bot) 533 { 534 long ans; 535 536 if (bot == 0) 537 return(0); 538 ans = (quad_t)top * 100 / bot; 539 return (ans); 540 } 541 542 #define PCT(top, bot) pct((long)(top), (long)(bot)) 543 544 static void 545 dosum(void) 546 { 547 struct nchstats *nch_tmp, nchstats; 548 size_t vms_size = sizeof(vms); 549 size_t vmm_size = sizeof(vmm); 550 int cpucnt; 551 u_long nchtotal; 552 u_long nchpathtotal; 553 size_t nch_size = sizeof(struct nchstats) * SMP_MAXCPU; 554 555 if (sysctlbyname("vm.vmstats", &vms, &vms_size, NULL, 0)) { 556 perror("sysctlbyname: vm.vmstats"); 557 exit(1); 558 } 559 if (sysctlbyname("vm.vmmeter", &vmm, &vmm_size, NULL, 0)) { 560 perror("sysctlbyname: vm.vmstats"); 561 exit(1); 562 } 563 printf("%9u cpu context switches\n", vmm.v_swtch); 564 printf("%9u device interrupts\n", vmm.v_intr); 565 printf("%9u software interrupts\n", vmm.v_soft); 566 printf("%9u traps\n", vmm.v_trap); 567 printf("%9u system calls\n", vmm.v_syscall); 568 printf("%9u kernel threads created\n", vmm.v_kthreads); 569 printf("%9u fork() calls\n", vmm.v_forks); 570 printf("%9u vfork() calls\n", vmm.v_vforks); 571 printf("%9u rfork() calls\n", vmm.v_rforks); 572 printf("%9u exec() calls\n", vmm.v_exec); 573 printf("%9u swap pager pageins\n", vmm.v_swapin); 574 printf("%9u swap pager pages paged in\n", vmm.v_swappgsin); 575 printf("%9u swap pager pageouts\n", vmm.v_swapout); 576 printf("%9u swap pager pages paged out\n", vmm.v_swappgsout); 577 printf("%9u vnode pager pageins\n", vmm.v_vnodein); 578 printf("%9u vnode pager pages paged in\n", vmm.v_vnodepgsin); 579 printf("%9u vnode pager pageouts\n", vmm.v_vnodeout); 580 printf("%9u vnode pager pages paged out\n", vmm.v_vnodepgsout); 581 printf("%9u page daemon wakeups\n", vmm.v_pdwakeups); 582 printf("%9u pages examined by the page daemon\n", vmm.v_pdpages); 583 printf("%9u pages reactivated\n", vmm.v_reactivated); 584 printf("%9u copy-on-write faults\n", vmm.v_cow_faults); 585 printf("%9u copy-on-write optimized faults\n", vmm.v_cow_optim); 586 printf("%9u zero fill pages zeroed\n", vmm.v_zfod); 587 printf("%9u zero fill pages prezeroed\n", vmm.v_ozfod); 588 printf("%9u intransit blocking page faults\n", vmm.v_intrans); 589 printf("%9u total VM faults taken\n", vmm.v_vm_faults); 590 printf("%9u pages affected by kernel thread creation\n", vmm.v_kthreadpages); 591 printf("%9u pages affected by fork()\n", vmm.v_forkpages); 592 printf("%9u pages affected by vfork()\n", vmm.v_vforkpages); 593 printf("%9u pages affected by rfork()\n", vmm.v_rforkpages); 594 printf("%9u pages freed\n", vmm.v_tfree); 595 printf("%9u pages freed by daemon\n", vmm.v_dfree); 596 printf("%9u pages freed by exiting processes\n", vmm.v_pfree); 597 printf("%9u pages active\n", vms.v_active_count); 598 printf("%9u pages inactive\n", vms.v_inactive_count); 599 printf("%9u pages in VM cache\n", vms.v_cache_count); 600 printf("%9u pages wired down\n", vms.v_wire_count); 601 printf("%9u pages free\n", vms.v_free_count); 602 printf("%9u bytes per page\n", vms.v_page_size); 603 604 if ((nch_tmp = malloc(nch_size)) == NULL) { 605 perror("malloc"); 606 exit(1); 607 } else { 608 if (sysctlbyname("vfs.cache.nchstats", nch_tmp, &nch_size, NULL, 0)) { 609 perror("sysctlbyname vfs.cache.nchstats"); 610 free(nch_tmp); 611 exit(1); 612 } else { 613 if ((nch_tmp = realloc(nch_tmp, nch_size)) == NULL) { 614 perror("realloc"); 615 exit(1); 616 } 617 } 618 } 619 620 cpucnt = nch_size / sizeof(struct nchstats); 621 kvm_nch_cpuagg(nch_tmp, &nchstats, cpucnt); 622 623 nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits + 624 nchstats.ncs_badhits + nchstats.ncs_falsehits + 625 nchstats.ncs_miss; 626 nchpathtotal = nchstats.ncs_longhits + nchstats.ncs_longmiss; 627 printf("%9ld total path lookups\n", nchpathtotal); 628 printf("%9ld total component lookups\n", nchtotal); 629 printf( 630 "%9s cache hits (%ld%% pos + %ld%% neg)\n", 631 "", PCT(nchstats.ncs_goodhits, nchtotal), 632 PCT(nchstats.ncs_neghits, nchtotal)); 633 printf("%9s deletions %ld%%, falsehits %ld%%\n", "", 634 PCT(nchstats.ncs_badhits, nchtotal), 635 PCT(nchstats.ncs_falsehits, nchtotal)); 636 free(nch_tmp); 637 } 638 639 #ifdef notyet 640 void 641 doforkst(void) 642 { 643 struct forkstat fks; 644 645 kread(X_FORKSTAT, &fks, sizeof(struct forkstat)); 646 printf("%d forks, %d pages, average %.2f\n", 647 fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork); 648 printf("%d vforks, %d pages, average %.2f\n", 649 fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork); 650 } 651 #endif 652 653 static void 654 devstats(void) 655 { 656 int dn; 657 long double transfers_per_second; 658 long double busy_seconds; 659 660 diff_cp_time.cp_user = cp_time.cp_user - old_cp_time.cp_user; 661 diff_cp_time.cp_nice = cp_time.cp_nice - old_cp_time.cp_nice; 662 diff_cp_time.cp_sys = cp_time.cp_sys - old_cp_time.cp_sys; 663 diff_cp_time.cp_intr = cp_time.cp_intr - old_cp_time.cp_intr; 664 diff_cp_time.cp_idle = cp_time.cp_idle - old_cp_time.cp_idle; 665 old_cp_time = cp_time; 666 667 busy_seconds = compute_etime(cur.busy_time, last.busy_time); 668 669 for (dn = 0; dn < num_devices; dn++) { 670 int di; 671 672 if ((dev_select[dn].selected == 0) 673 || (dev_select[dn].selected > maxshowdevs)) 674 continue; 675 676 di = dev_select[dn].position; 677 678 if (compute_stats(&cur.dinfo->devices[di], 679 &last.dinfo->devices[di], busy_seconds, 680 NULL, NULL, NULL, 681 NULL, &transfers_per_second, NULL, 682 NULL, NULL) != 0) 683 errx(1, "%s", devstat_errbuf); 684 685 printf("%3.0Lf ", transfers_per_second); 686 } 687 } 688 689 static void 690 cpustats(void) 691 { 692 uint64_t total; 693 double totusage; 694 695 total = diff_cp_time.cp_user + diff_cp_time.cp_nice + 696 diff_cp_time.cp_sys + diff_cp_time.cp_intr + diff_cp_time.cp_idle; 697 698 if (total) 699 totusage = 100.0 / total; 700 else 701 totusage = 0; 702 printf("%2.0f ", 703 (diff_cp_time.cp_user + diff_cp_time.cp_nice) * totusage); 704 printf("%2.0f ", 705 (diff_cp_time.cp_sys + diff_cp_time.cp_intr) * totusage); 706 printf("%2.0f", 707 diff_cp_time.cp_idle * totusage); 708 } 709 710 static void 711 dointr(void) 712 { 713 u_long *intrcnt, uptime; 714 u_int64_t inttotal; 715 size_t nintr, inamlen, i, size; 716 int nwidth; 717 char *intrstr; 718 char **intrname; 719 720 uptime = getuptime(); 721 if (sysctlbyname("hw.intrnames", NULL, &inamlen, NULL, 0) != 0) 722 errx(1, "sysctlbyname"); 723 intrstr = malloc(inamlen); 724 if (intrstr == NULL) 725 err(1, "malloc"); 726 sysctlbyname("hw.intrnames", intrstr, &inamlen, NULL, 0); 727 for (nintr = 0, i = 0; i < inamlen; ++i) { 728 if (intrstr[i] == 0) 729 nintr++; 730 } 731 intrname = malloc(nintr * sizeof(char *)); 732 for (i = 0; i < nintr; ++i) { 733 intrname[i] = intrstr; 734 intrstr += strlen(intrstr) + 1; 735 } 736 737 size = nintr * sizeof(*intrcnt); 738 intrcnt = calloc(nintr, sizeof(*intrcnt)); 739 if (intrcnt == NULL) 740 err(1, "malloc"); 741 sysctlbyname("hw.intrcnt", intrcnt, &size, NULL, 0); 742 743 nwidth = 21; 744 for (i = 0; i < nintr; ++i) { 745 if (nwidth < (int)strlen(intrname[i])) 746 nwidth = (int)strlen(intrname[i]); 747 } 748 if (verbose) nwidth += 8; 749 750 printf("%-*.*s %11s %10s\n", 751 nwidth, nwidth, "interrupt", "total", "rate"); 752 inttotal = 0; 753 for (i = 0; i < nintr; ++i) { 754 int named; 755 char *infop, irqinfo[72]; 756 757 if ((named = strncmp(intrname[i], "irq", 3)) != 0 || 758 intrcnt[i] > 0) { 759 infop = intrname[i]; 760 if (verbose && named) { 761 snprintf(irqinfo, sizeof(irqinfo), 762 "irq%zd: %s", i, intrname[i]); 763 infop = irqinfo; 764 } 765 printf("%-*.*s %11lu %10lu\n", 766 nwidth, nwidth, infop, 767 intrcnt[i], intrcnt[i] / uptime); 768 } 769 inttotal += intrcnt[i]; 770 } 771 printf("%-*.*s %11llu %10llu\n", 772 nwidth, nwidth, "Total", 773 (long long)inttotal, (long long)(inttotal / uptime)); 774 } 775 776 #define MAX_KMSTATS 1024 777 778 static long 779 cpuagg(size_t *ary) 780 { 781 int i; 782 long ttl; 783 784 for (i = ttl = 0; i < SMP_MAXCPU; ++i) 785 ttl += ary[i]; 786 return(ttl); 787 } 788 789 static void 790 domem(void) 791 { 792 struct malloc_type *ks; 793 int i, j; 794 int first, nkms; 795 long totuse = 0, totfree = 0, totreq = 0; 796 struct malloc_type kmemstats[MAX_KMSTATS], *kmsp; 797 char buf[1024]; 798 799 kread(X_KMEMSTATISTICS, &kmsp, sizeof(kmsp)); 800 for (nkms = 0; nkms < MAX_KMSTATS && kmsp != NULL; nkms++) { 801 if (sizeof(kmemstats[0]) != kvm_read(kd, (u_long)kmsp, 802 &kmemstats[nkms], sizeof(kmemstats[0]))) 803 err(1, "kvm_read(%p)", (void *)kmsp); 804 if (sizeof(buf) != kvm_read(kd, 805 (u_long)kmemstats[nkms].ks_shortdesc, buf, sizeof(buf))) 806 err(1, "kvm_read(%p)", 807 kmemstats[nkms].ks_shortdesc); 808 buf[sizeof(buf) - 1] = '\0'; 809 kmemstats[nkms].ks_shortdesc = strdup(buf); 810 kmsp = kmemstats[nkms].ks_next; 811 } 812 if (kmsp != NULL) 813 warnx("truncated to the first %d memory types", nkms); 814 815 printf( 816 "\nMemory statistics by type Type Kern\n"); 817 printf( 818 " Type InUse MemUse HighUse Limit Requests Limit Limit\n"); 819 for (i = 0, ks = &kmemstats[0]; i < nkms; i++, ks++) { 820 if (ks->ks_calls == 0) 821 continue; 822 printf("%19s%7ld%7ldK%7ldK%8zdK%10jd%5u%6u", 823 ks->ks_shortdesc, 824 cpuagg(ks->ks_inuse), (cpuagg(ks->ks_memuse) + 1023) / 1024, 825 (ks->ks_maxused + 1023) / 1024, 826 (ks->ks_limit + 1023) / 1024, (intmax_t)ks->ks_calls, 827 ks->ks_limblocks, ks->ks_mapblocks); 828 first = 1; 829 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) { 830 if ((ks->ks_size & j) == 0) 831 continue; 832 if (first) 833 printf(" "); 834 else 835 printf(","); 836 if(j<1024) 837 printf("%d",j); 838 else 839 printf("%dK",j>>10); 840 first = 0; 841 } 842 printf("\n"); 843 totuse += cpuagg(ks->ks_memuse); 844 totreq += ks->ks_calls; 845 } 846 printf("\nMemory Totals: In Use Free Requests\n"); 847 printf(" %7ldK %6ldK %8ld\n", 848 (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq); 849 } 850 851 static void 852 dozmem(void) 853 { 854 char *buf; 855 size_t bufsize; 856 857 buf = NULL; 858 bufsize = 1024; 859 for (;;) { 860 if ((buf = realloc(buf, bufsize)) == NULL) 861 err(1, "realloc()"); 862 if (sysctlbyname("vm.zone", buf, &bufsize, NULL, 0) == 0) 863 break; 864 if (errno != ENOMEM) 865 err(1, "sysctl()"); 866 bufsize *= 2; 867 } 868 buf[bufsize] = '\0'; /* play it safe */ 869 printf("%s\n\n", buf); 870 free(buf); 871 } 872 873 /* 874 * kread reads something from the kernel, given its nlist index. 875 */ 876 static void 877 kread(int nlx, void *addr, size_t size) 878 { 879 const char *sym; 880 881 if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) { 882 sym = namelist[nlx].n_name; 883 if (*sym == '_') 884 ++sym; 885 errx(1, "symbol %s not defined", sym); 886 } 887 if (kvm_read(kd, namelist[nlx].n_value, addr, size) != (ssize_t)size) { 888 sym = namelist[nlx].n_name; 889 if (*sym == '_') 890 ++sym; 891 errx(1, "%s: %s", sym, kvm_geterr(kd)); 892 } 893 } 894 895 static void 896 usage(void) 897 { 898 fprintf(stderr, "%s%s", 899 "usage: vmstat [-imsvz] [-c count] [-M core] [-N system] [-w wait]\n", 900 " [-n devs] [disks]\n"); 901 exit(1); 902 } 903