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