1 /* $NetBSD: vmstat.c,v 1.75 2009/10/21 21:12:07 rmind Exp $ */ 2 3 /*- 4 * Copyright (c) 1983, 1989, 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 1/12/94"; 36 #endif 37 __RCSID("$NetBSD: vmstat.c,v 1.75 2009/10/21 21:12:07 rmind Exp $"); 38 #endif /* not lint */ 39 40 /* 41 * Cursed vmstat -- from Robert Elz. 42 */ 43 44 #include <sys/param.h> 45 #include <sys/uio.h> 46 #include <sys/namei.h> 47 #include <sys/sysctl.h> 48 #include <sys/evcnt.h> 49 50 #include <uvm/uvm_extern.h> 51 52 #include <errno.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <util.h> 56 57 #include "systat.h" 58 #include "extern.h" 59 #include "drvstats.h" 60 #include "utmpentry.h" 61 #include "vmstat.h" 62 63 static struct Info { 64 struct uvmexp_sysctl uvmexp; 65 struct vmtotal Total; 66 struct nchstats nchstats; 67 long nchcount; 68 long *intrcnt; 69 u_int64_t *evcnt; 70 } s, s1, s2, z; 71 72 enum display_mode display_mode = TIME; 73 74 static void allocinfo(struct Info *); 75 static void copyinfo(struct Info *, struct Info *); 76 static float cputime(int); 77 static void dinfo(int, int, int); 78 static void getinfo(struct Info *); 79 static int ucount(void); 80 81 static char buf[26]; 82 static u_int64_t temp; 83 double etime; 84 static float hertz; 85 static int nintr; 86 static long *intrloc; 87 static char **intrname; 88 static int nextintsrow; 89 static int disk_horiz = 1; 90 91 WINDOW * 92 openvmstat(void) 93 { 94 return (stdscr); 95 } 96 97 void 98 closevmstat(WINDOW *w) 99 { 100 101 if (w == NULL) 102 return; 103 wclear(w); 104 wrefresh(w); 105 } 106 107 108 static struct nlist namelist[] = { 109 #define X_NCHSTATS 0 110 { .n_name = "_nchstats" }, 111 #define X_INTRNAMES 1 112 { .n_name = "_intrnames" }, 113 #define X_EINTRNAMES 2 114 { .n_name = "_eintrnames" }, 115 #define X_INTRCNT 3 116 { .n_name = "_intrcnt" }, 117 #define X_EINTRCNT 4 118 { .n_name = "_eintrcnt" }, 119 #define X_ALLEVENTS 5 120 { .n_name = "_allevents" }, 121 { .n_name = NULL } 122 }; 123 124 /* 125 * These constants define where the major pieces are laid out 126 */ 127 #define STATROW 0 /* uses 1 row and 68 cols */ 128 #define STATCOL 2 129 #define MEMROW 9 /* uses 4 rows and 31 cols */ 130 #define MEMCOL 0 131 #define PAGEROW 2 /* uses 4 rows and 26 cols */ 132 #define PAGECOL 54 133 #define INTSROW 9 /* uses all rows to bottom and 17 cols */ 134 #define INTSCOL 40 135 #define INTSCOLEND (VMSTATCOL - 0) 136 #define PROCSROW 2 /* uses 2 rows and 20 cols */ 137 #define PROCSCOL 0 138 #define GENSTATROW 2 /* uses 2 rows and 30 cols */ 139 #define GENSTATCOL 17 140 #define VMSTATROW 7 /* uses 17 rows and 15 cols */ 141 #define VMSTATCOL 64 142 #define GRAPHROW 5 /* uses 3 rows and 51 cols */ 143 #define GRAPHCOL 0 144 #define NAMEIROW 14 /* uses 3 rows and 38 cols */ 145 #define NAMEICOL 0 146 #define DISKROW 18 /* uses 5 rows and 50 cols (for 9 drives) */ 147 #define DISKCOL 0 148 #define DISKCOLWIDTH 6 149 #define DISKCOLEND INTSCOL 150 151 typedef struct intr_evcnt intr_evcnt_t; 152 struct intr_evcnt { 153 char *ie_group; 154 char *ie_name; 155 u_int64_t *ie_count; /* kernel address... */ 156 int ie_loc; /* screen row */ 157 } *ie_head; 158 int nevcnt; 159 160 static void 161 get_interrupt_events(void) 162 { 163 struct evcntlist allevents; 164 struct evcnt evcnt, *evptr; 165 intr_evcnt_t *ie; 166 intr_evcnt_t *n; 167 168 if (!NREAD(X_ALLEVENTS, &allevents, sizeof allevents)) 169 return; 170 evptr = TAILQ_FIRST(&allevents); 171 for (; evptr != NULL; evptr = TAILQ_NEXT(&evcnt, ev_list)) { 172 if (!KREAD(evptr, &evcnt, sizeof evcnt)) 173 return; 174 if (evcnt.ev_type != EVCNT_TYPE_INTR) 175 continue; 176 n = realloc(ie_head, sizeof *ie * (nevcnt + 1)); 177 if (n == NULL) { 178 error("realloc failed"); 179 die(0); 180 } 181 ie_head = n; 182 ie = ie_head + nevcnt; 183 ie->ie_group = malloc(evcnt.ev_grouplen + 1); 184 ie->ie_name = malloc(evcnt.ev_namelen + 1); 185 if (ie->ie_group == NULL || ie->ie_name == NULL) 186 return; 187 if (!KREAD(evcnt.ev_group, ie->ie_group, evcnt.ev_grouplen + 1)) 188 return; 189 if (!KREAD(evcnt.ev_name, ie->ie_name, evcnt.ev_namelen + 1)) 190 return; 191 ie->ie_count = &evptr->ev_count; 192 ie->ie_loc = 0; 193 nevcnt++; 194 } 195 } 196 197 int 198 initvmstat(void) 199 { 200 static char *intrnamebuf; 201 char *cp; 202 int i; 203 204 if (intrnamebuf) 205 free(intrnamebuf); 206 if (intrname) 207 free(intrname); 208 if (intrloc) 209 free(intrloc); 210 211 if (namelist[0].n_type == 0) { 212 if (kvm_nlist(kd, namelist) && 213 (namelist[X_NCHSTATS].n_type == 0 || 214 namelist[X_ALLEVENTS].n_type == 0)) { 215 nlisterr(namelist); 216 return(0); 217 } 218 if (namelist[0].n_type == 0) { 219 error("No namelist"); 220 return(0); 221 } 222 } 223 hertz = stathz ? stathz : hz; 224 if (!drvinit(1)) 225 return(0); 226 227 /* Old style interrupt counts - deprecated */ 228 nintr = (namelist[X_EINTRCNT].n_value - 229 namelist[X_INTRCNT].n_value) / sizeof (long); 230 if (nintr) { 231 intrloc = calloc(nintr, sizeof (long)); 232 intrname = calloc(nintr, sizeof (long)); 233 intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value - 234 namelist[X_INTRNAMES].n_value); 235 if (intrnamebuf == NULL || intrname == 0 || intrloc == 0) { 236 error("Out of memory\n"); 237 nintr = 0; 238 return(0); 239 } 240 NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) - 241 NVAL(X_INTRNAMES)); 242 for (cp = intrnamebuf, i = 0; i < nintr; i++) { 243 intrname[i] = cp; 244 cp += strlen(cp) + 1; 245 } 246 } 247 248 /* event counter interrupt counts */ 249 get_interrupt_events(); 250 251 nextintsrow = INTSROW + 1; 252 allocinfo(&s); 253 allocinfo(&s1); 254 allocinfo(&s2); 255 allocinfo(&z); 256 257 getinfo(&s2); 258 copyinfo(&s2, &s1); 259 return(1); 260 } 261 262 void 263 fetchvmstat(void) 264 { 265 time_t now; 266 267 time(&now); 268 strlcpy(buf, ctime(&now), sizeof(buf)); 269 buf[19] = '\0'; 270 getinfo(&s); 271 } 272 273 static void 274 print_ie_title(int i) 275 { 276 int width, name_width, group_width; 277 278 width = INTSCOLEND - (INTSCOL + 9); 279 if (width <= 0) 280 return; 281 282 move(ie_head[i].ie_loc, INTSCOL + 9); 283 group_width = strlen(ie_head[i].ie_group); 284 name_width = strlen(ie_head[i].ie_name); 285 width -= group_width + 1 + name_width; 286 if (width < 0) { 287 /* 288 * Screen to narrow for full strings 289 * This is all rather horrid, in some cases there are a lot 290 * of events in the same group, and in others the event 291 * name is "intr". There are also names which need 7 or 8 292 * columns before they become meaningful. 293 * This is a bad compromise. 294 */ 295 width = -width; 296 group_width -= (width + 1) / 2; 297 name_width -= width / 2; 298 /* some have the 'useful' name "intr", display their group */ 299 if (strcasecmp(ie_head[i].ie_name, "intr") == 0) { 300 group_width += name_width + 1; 301 name_width = 0; 302 } else { 303 if (group_width <= 3 || name_width < 0) { 304 /* don't display group */ 305 name_width += group_width + 1; 306 group_width = 0; 307 } 308 } 309 } 310 311 if (group_width != 0) { 312 printw("%-.*s", group_width, ie_head[i].ie_group); 313 if (name_width != 0) 314 printw(" "); 315 } 316 if (name_width != 0) 317 printw("%-.*s", name_width, ie_head[i].ie_name); 318 } 319 320 void 321 labelvmstat_top(void) 322 { 323 324 clear(); 325 326 mvprintw(STATROW, STATCOL + 4, "users Load"); 327 328 mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt"); 329 330 mvprintw(GRAPHROW, GRAPHCOL, 331 " . %% Sy . %% Us . %% Ni . %% In . %% Id"); 332 mvprintw(PROCSROW, PROCSCOL, "Proc:r d s"); 333 mvprintw(GRAPHROW + 1, GRAPHCOL, 334 "| | | | | | | | | | |"); 335 336 mvprintw(PAGEROW, PAGECOL + 8, "PAGING SWAPPING "); 337 mvprintw(PAGEROW + 1, PAGECOL, " in out in out "); 338 mvprintw(PAGEROW + 2, PAGECOL + 2, "ops"); 339 mvprintw(PAGEROW + 3, PAGECOL, "pages"); 340 } 341 342 void 343 labelvmstat(void) 344 { 345 int i; 346 347 /* Top few lines first */ 348 349 labelvmstat_top(); 350 351 /* Left hand column */ 352 353 mvprintw(MEMROW, MEMCOL, " memory totals (in kB)"); 354 mvprintw(MEMROW + 1, MEMCOL, " real virtual free"); 355 mvprintw(MEMROW + 2, MEMCOL, "Active"); 356 mvprintw(MEMROW + 3, MEMCOL, "All"); 357 358 mvprintw(NAMEIROW, NAMEICOL, "Namei Sys-cache Proc-cache"); 359 mvprintw(NAMEIROW + 1, NAMEICOL, 360 " Calls hits %% hits %%"); 361 362 mvprintw(DISKROW, DISKCOL, "Disks:"); 363 if (disk_horiz) { 364 mvprintw(DISKROW + 1, DISKCOL + 1, "seeks"); 365 mvprintw(DISKROW + 2, DISKCOL + 1, "xfers"); 366 mvprintw(DISKROW + 3, DISKCOL + 1, "bytes"); 367 mvprintw(DISKROW + 4, DISKCOL + 1, "%%busy"); 368 } else { 369 mvprintw(DISKROW, DISKCOL + 1 + 1 * DISKCOLWIDTH, "seeks"); 370 mvprintw(DISKROW, DISKCOL + 1 + 2 * DISKCOLWIDTH, "xfers"); 371 mvprintw(DISKROW, DISKCOL + 1 + 3 * DISKCOLWIDTH, "bytes"); 372 mvprintw(DISKROW, DISKCOL + 1 + 4 * DISKCOLWIDTH, "%%busy"); 373 } 374 375 /* Middle column */ 376 377 mvprintw(INTSROW, INTSCOL + 9, "Interrupts"); 378 for (i = 0; i < nintr; i++) { 379 if (intrloc[i] == 0) 380 continue; 381 mvprintw(intrloc[i], INTSCOL + 9, "%-.*s", 382 INTSCOLEND - (INTSCOL + 9), intrname[i]); 383 } 384 for (i = 0; i < nevcnt; i++) { 385 if (ie_head[i].ie_loc == 0) 386 continue; 387 print_ie_title(i); 388 } 389 390 /* Right hand column */ 391 392 mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks"); 393 mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw"); 394 mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm"); 395 mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait"); 396 mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck"); 397 mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok"); 398 mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram"); 399 mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy"); 400 mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp"); 401 mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod"); 402 mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow"); 403 mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin"); 404 mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg"); 405 mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg"); 406 mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired"); 407 mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre"); 408 409 if (LINES - 1 > VMSTATROW + 16) 410 mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn"); 411 } 412 413 #define X(s, s1, fld) {temp = (s).fld[i]; (s).fld[i] -= (s1).fld[i]; \ 414 if (display_mode == TIME) (s1).fld[i] = temp;} 415 #define Z(s, s1, fld) {temp = (s).nchstats.fld; \ 416 (s).nchstats.fld -= (s1).nchstats.fld; \ 417 if (display_mode == TIME) (s1).nchstats.fld = temp;} 418 #define PUTRATE(s, s1, fld, l, c, w) \ 419 {temp = (s).fld; (s).fld -= (s1).fld; \ 420 if (display_mode == TIME) (s1).fld = temp; \ 421 putint((int)((float)(s).fld/etime + 0.5), l, c, w);} 422 #define MAXFAIL 5 423 424 static char cpuchar[CPUSTATES] = { '=' , '>', '-', '%', ' ' }; 425 static char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_INTR, CP_IDLE }; 426 427 void 428 show_vmstat_top(vmtotal_t *Total, uvmexp_sysctl_t *uvm, uvmexp_sysctl_t *uvm1) 429 { 430 float f1, f2; 431 int psiz; 432 int i, l, c; 433 struct { 434 struct uvmexp_sysctl *uvmexp; 435 } us, us1; 436 437 us.uvmexp = uvm; 438 us1.uvmexp = uvm1; 439 440 putint(ucount(), STATROW, STATCOL, 3); 441 putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0); 442 putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0); 443 putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0); 444 mvaddstr(STATROW, STATCOL + 53, buf); 445 446 putint(Total->t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3); 447 putint(Total->t_dw, PROCSROW + 1, PROCSCOL + 6, 3); 448 putint(Total->t_sl, PROCSROW + 1, PROCSCOL + 9, 3); 449 450 PUTRATE(us, us1, uvmexp->swtch, GENSTATROW + 1, GENSTATCOL - 1, 7); 451 PUTRATE(us, us1, uvmexp->traps, GENSTATROW + 1, GENSTATCOL + 7, 6); 452 PUTRATE(us, us1, uvmexp->syscalls, GENSTATROW + 1, GENSTATCOL + 14, 6); 453 PUTRATE(us, us1, uvmexp->intrs, GENSTATROW + 1, GENSTATCOL + 21, 5); 454 PUTRATE(us, us1, uvmexp->softs, GENSTATROW + 1, GENSTATCOL + 27, 6); 455 PUTRATE(us, us1, uvmexp->faults, GENSTATROW + 1, GENSTATCOL + 34, 6); 456 457 /* Last CPU state not calculated yet. */ 458 for (f2 = 0.0, psiz = 0, c = 0; c < CPUSTATES; c++) { 459 i = cpuorder[c]; 460 f1 = cputime(i); 461 f2 += f1; 462 l = (int) ((f2 + 1.0) / 2.0) - psiz; 463 if (c == 0) 464 putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0); 465 else 466 putfloat(f1, GRAPHROW, GRAPHCOL + 10 * c + 1, 5, 1, 0); 467 mvhline(GRAPHROW + 2, psiz, cpuchar[c], l); 468 psiz += l; 469 } 470 471 PUTRATE(us, us1, uvmexp->pageins, PAGEROW + 2, PAGECOL + 5, 5); 472 PUTRATE(us, us1, uvmexp->pdpageouts, PAGEROW + 2, PAGECOL + 10, 5); 473 PUTRATE(us, us1, uvmexp->pgswapin, PAGEROW + 3, PAGECOL + 5, 5); 474 PUTRATE(us, us1, uvmexp->pgswapout, PAGEROW + 3, PAGECOL + 10, 5); 475 } 476 477 void 478 showvmstat(void) 479 { 480 int inttotal; 481 int i, l, r, c; 482 static int failcnt = 0; 483 static int relabel = 0; 484 static int last_disks = 0; 485 static char pigs[] = "pigs"; 486 487 if (relabel) { 488 labelvmstat(); 489 relabel = 0; 490 } 491 492 cpuswap(); 493 if (display_mode == TIME) { 494 drvswap(); 495 etime = cur.cp_etime; 496 /* < 5 ticks - ignore this trash */ 497 if ((etime * hertz) < 1.0) { 498 if (failcnt++ <= MAXFAIL) 499 return; 500 clear(); 501 mvprintw(2, 10, "The alternate system clock has died!"); 502 mvprintw(3, 10, "Reverting to ``pigs'' display."); 503 move(CMDLINE, 0); 504 refresh(); 505 failcnt = 0; 506 sleep(5); 507 command(pigs); 508 return; 509 } 510 } else 511 etime = 1.0; 512 513 show_vmstat_top(&s.Total, &s.uvmexp, &s1.uvmexp); 514 515 /* Memory totals */ 516 #define pgtokb(pg) ((pg) * (s.uvmexp.pagesize / 1024)) 517 putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 6, 8); 518 putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse), /* XXX */ 519 MEMROW + 2, MEMCOL + 15, 8); 520 putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 6, 8); 521 putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse), 522 MEMROW + 3, MEMCOL + 15, 8); 523 putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 24, 8); 524 putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse), 525 MEMROW + 3, MEMCOL + 24, 8); 526 #undef pgtokb 527 528 /* Namei cache */ 529 Z(s, s1, ncs_goodhits); Z(s, s1, ncs_badhits); Z(s, s1, ncs_miss); 530 Z(s, s1, ncs_long); Z(s, s1, ncs_pass2); Z(s, s1, ncs_2passes); 531 s.nchcount = s.nchstats.ncs_goodhits + s.nchstats.ncs_badhits + 532 s.nchstats.ncs_miss + s.nchstats.ncs_long; 533 if (display_mode == TIME) 534 s1.nchcount = s.nchcount; 535 536 putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9); 537 putint(s.nchstats.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9); 538 #define nz(x) ((x) ? (x) : 1) 539 putfloat(s.nchstats.ncs_goodhits * 100.0 / nz(s.nchcount), 540 NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1); 541 putint(s.nchstats.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9); 542 putfloat(s.nchstats.ncs_pass2 * 100.0 / nz(s.nchcount), 543 NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1); 544 #undef nz 545 546 /* Disks */ 547 for (l = 0, i = 0, r = DISKROW, c = DISKCOL; 548 i < (int)ndrive; i++) { 549 if (!drv_select[i]) 550 continue; 551 552 if (disk_horiz) 553 c += DISKCOLWIDTH; 554 else 555 r++; 556 if (c + DISKCOLWIDTH > DISKCOLEND) { 557 if (disk_horiz && LINES - 1 - DISKROW > 558 (DISKCOLEND - DISKCOL) / DISKCOLWIDTH) { 559 disk_horiz = 0; 560 relabel = 1; 561 } 562 break; 563 } 564 if (r >= LINES - 1) { 565 if (!disk_horiz && LINES - 1 - DISKROW < 566 (DISKCOLEND - DISKCOL) / DISKCOLWIDTH) { 567 disk_horiz = 1; 568 relabel = 1; 569 } 570 break; 571 } 572 l++; 573 574 dinfo(i, r, c); 575 } 576 /* blank out if we lost any disks */ 577 for (i = l; i < last_disks; i++) { 578 int j; 579 if (disk_horiz) 580 c += DISKCOLWIDTH; 581 else 582 r++; 583 for (j = 0; j < 5; j++) { 584 if (disk_horiz) 585 mvprintw(r+j, c, "%*s", DISKCOLWIDTH, ""); 586 else 587 mvprintw(r, c+j*DISKCOLWIDTH, "%*s", DISKCOLWIDTH, ""); 588 } 589 } 590 last_disks = l; 591 592 /* Interrupts */ 593 failcnt = 0; 594 inttotal = 0; 595 for (i = 0; i < nintr; i++) { 596 if (s.intrcnt[i] == 0) 597 continue; 598 if (intrloc[i] == 0) { 599 if (nextintsrow == LINES) 600 continue; 601 intrloc[i] = nextintsrow++; 602 mvprintw(intrloc[i], INTSCOL + 9, "%-.*s", 603 INTSCOLEND - (INTSCOL + 9), intrname[i]); 604 } 605 X(s, s1, intrcnt); 606 l = (int)((float)s.intrcnt[i]/etime + 0.5); 607 inttotal += l; 608 putint(l, intrloc[i], INTSCOL, 8); 609 } 610 611 for (i = 0; i < nevcnt; i++) { 612 if (s.evcnt[i] == 0) 613 continue; 614 if (ie_head[i].ie_loc == 0) { 615 if (nextintsrow == LINES) 616 continue; 617 ie_head[i].ie_loc = nextintsrow++; 618 print_ie_title(i); 619 } 620 X(s, s1, evcnt); 621 l = (int)((float)s.evcnt[i]/etime + 0.5); 622 inttotal += l; 623 putint(l, ie_head[i].ie_loc, INTSCOL, 8); 624 } 625 putint(inttotal, INTSROW, INTSCOL, 8); 626 627 PUTRATE(s, s1, uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6); 628 PUTRATE(s, s1, uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6); 629 PUTRATE(s, s1, uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6); 630 PUTRATE(s, s1, uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5); 631 PUTRATE(s, s1, uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6); 632 PUTRATE(s, s1, uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6); 633 PUTRATE(s, s1, uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6); 634 PUTRATE(s, s1, uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6); 635 PUTRATE(s, s1, uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6); 636 PUTRATE(s, s1, uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6); 637 PUTRATE(s, s1, uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9); 638 putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9); 639 putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9); 640 putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9); 641 putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9); 642 PUTRATE(s, s1, uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9); 643 if (LINES - 1 > VMSTATROW + 16) 644 PUTRATE(s, s1, uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9); 645 646 } 647 648 void 649 vmstat_boot(char *args) 650 { 651 copyinfo(&z, &s1); 652 display_mode = BOOT; 653 } 654 655 void 656 vmstat_run(char *args) 657 { 658 copyinfo(&s1, &s2); 659 display_mode = RUN; 660 } 661 662 void 663 vmstat_time(char *args) 664 { 665 display_mode = TIME; 666 } 667 668 void 669 vmstat_zero(char *args) 670 { 671 if (display_mode == RUN) 672 getinfo(&s1); 673 } 674 675 /* calculate number of users on the system */ 676 static int 677 ucount(void) 678 { 679 static int onusers = -1; 680 int nusers = 0; 681 struct utmpentry *ehead; 682 683 nusers = getutentries(NULL, &ehead); 684 685 if (nusers != onusers) { 686 if (nusers == 1) 687 mvprintw(STATROW, STATCOL + 8, " "); 688 else 689 mvprintw(STATROW, STATCOL + 8, "s"); 690 } 691 onusers = nusers; 692 return (nusers); 693 } 694 695 static float 696 cputime(int indx) 697 { 698 double t; 699 int i; 700 701 t = 0; 702 for (i = 0; i < CPUSTATES; i++) 703 t += cur.cp_time[i]; 704 if (t == 0.0) 705 t = 1.0; 706 return (cur.cp_time[indx] * 100.0 / t); 707 } 708 709 void 710 puthumanint(u_int64_t n, int l, int c, int w) 711 { 712 char b[128]; 713 714 if (move(l, c) != OK) 715 return; 716 if (n == 0) { 717 hline(' ', w); 718 return; 719 } 720 if (humanize_number(b, w, n, "", HN_AUTOSCALE, HN_NOSPACE) == -1 ) { 721 hline('*', w); 722 return; 723 } 724 printw("%*s", w, b); 725 } 726 727 void 728 putint(int n, int l, int c, int w) 729 { 730 char b[128]; 731 732 if (move(l, c) != OK) 733 return; 734 if (n == 0) { 735 hline(' ', w); 736 return; 737 } 738 (void)snprintf(b, sizeof b, "%*d", w, n); 739 if ((int)strlen(b) > w) { 740 if (display_mode == TIME) 741 hline('*', w); 742 else 743 puthumanint(n, l, c, w); 744 return; 745 } 746 addstr(b); 747 } 748 749 void 750 putfloat(double f, int l, int c, int w, int d, int nz) 751 { 752 char b[128]; 753 754 if (move(l, c) != OK) 755 return; 756 if (nz && f == 0.0) { 757 hline(' ', w); 758 return; 759 } 760 (void)snprintf(b, sizeof b, "%*.*f", w, d, f); 761 if ((int)strlen(b) > w) { 762 hline('*', w); 763 return; 764 } 765 addstr(b); 766 } 767 768 static void 769 getinfo(struct Info *stats) 770 { 771 int mib[2]; 772 size_t size; 773 int i; 774 775 cpureadstats(); 776 drvreadstats(); 777 NREAD(X_NCHSTATS, &stats->nchstats, sizeof stats->nchstats); 778 if (nintr) 779 NREAD(X_INTRCNT, stats->intrcnt, nintr * LONG); 780 for (i = 0; i < nevcnt; i++) 781 KREAD(ie_head[i].ie_count, &stats->evcnt[i], 782 sizeof stats->evcnt[i]); 783 size = sizeof(stats->uvmexp); 784 mib[0] = CTL_VM; 785 mib[1] = VM_UVMEXP2; 786 if (sysctl(mib, 2, &stats->uvmexp, &size, NULL, 0) < 0) { 787 error("can't get uvmexp: %s\n", strerror(errno)); 788 memset(&stats->uvmexp, 0, sizeof(stats->uvmexp)); 789 } 790 size = sizeof(stats->Total); 791 mib[0] = CTL_VM; 792 mib[1] = VM_METER; 793 if (sysctl(mib, 2, &stats->Total, &size, NULL, 0) < 0) { 794 error("Can't get kernel info: %s\n", strerror(errno)); 795 memset(&stats->Total, 0, sizeof(stats->Total)); 796 } 797 } 798 799 static void 800 allocinfo(struct Info *stats) 801 { 802 803 if (nintr && 804 (stats->intrcnt = calloc(nintr, sizeof(long))) == NULL) { 805 error("calloc failed"); 806 die(0); 807 } 808 if ((stats->evcnt = calloc(nevcnt, sizeof(u_int64_t))) == NULL) { 809 error("calloc failed"); 810 die(0); 811 } 812 } 813 814 static void 815 copyinfo(struct Info *from, struct Info *to) 816 { 817 long *intrcnt; 818 u_int64_t *evcnt; 819 820 intrcnt = to->intrcnt; 821 evcnt = to->evcnt; 822 *to = *from; 823 memmove(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof *intrcnt); 824 memmove(to->evcnt = evcnt, from->evcnt, nevcnt * sizeof *evcnt); 825 } 826 827 static void 828 dinfo(int dn, int r, int c) 829 { 830 double atime; 831 #define ADV if (disk_horiz) r++; else c += DISKCOLWIDTH 832 833 mvprintw(r, c, "%*.*s", DISKCOLWIDTH, DISKCOLWIDTH, dr_name[dn]); 834 ADV; 835 836 putint((int)(cur.seek[dn]/etime+0.5), r, c, DISKCOLWIDTH); 837 ADV; 838 putint((int)((cur.rxfer[dn]+cur.wxfer[dn])/etime+0.5), 839 r, c, DISKCOLWIDTH); 840 ADV; 841 puthumanint((cur.rbytes[dn] + cur.wbytes[dn]) / etime + 0.5, 842 r, c, DISKCOLWIDTH); 843 ADV; 844 845 /* time busy in disk activity */ 846 atime = cur.time[dn].tv_sec + cur.time[dn].tv_usec / 1000000.0; 847 atime = atime * 100.0 / etime; 848 if (atime >= 100) 849 putint(100, r, c, DISKCOLWIDTH); 850 else 851 putfloat(atime, r, c, DISKCOLWIDTH, 1, 1); 852 } 853