1 /* $NetBSD: vmstat.c,v 1.90 2021/08/21 13:22:19 christos 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.90 2021/08/21 13:22:19 christos 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 static int nintr; 84 static long *intrloc; 85 static char **intrname; 86 static int nextintsrow; 87 static int disk_horiz = 1; 88 static u_int nbuf; 89 90 WINDOW * 91 openvmstat(void) 92 { 93 return (stdscr); 94 } 95 96 void 97 closevmstat(WINDOW *w) 98 { 99 100 if (w == NULL) 101 return; 102 wclear(w); 103 wrefresh(w); 104 } 105 106 107 static struct nlist namelist[] = { 108 #define X_INTRNAMES 0 109 { .n_name = "_intrnames" }, 110 #define X_EINTRNAMES 1 111 { .n_name = "_eintrnames" }, 112 #define X_INTRCNT 2 113 { .n_name = "_intrcnt" }, 114 #define X_EINTRCNT 3 115 { .n_name = "_eintrcnt" }, 116 #define X_ALLEVENTS 4 117 { .n_name = "_allevents" }, 118 { .n_name = NULL } 119 }; 120 121 /* 122 * These constants define where the major pieces are laid out 123 */ 124 #define STATROW 0 /* uses 1 row and 68 cols */ 125 #define STATCOL 2 126 #define MEMROW 9 /* uses 5 rows and 31 cols */ 127 #define MEMCOL 0 128 #define PAGEROW 2 /* uses 4 rows and 26 cols */ 129 #define PAGECOL 54 130 #define INTSROW 9 /* uses all rows to bottom and 17 cols */ 131 #define INTSCOL 40 132 #define INTSCOLEND (VMSTATCOL - 0) 133 #define PROCSROW 2 /* uses 2 rows and 20 cols */ 134 #define PROCSCOL 0 135 #define GENSTATROW 2 /* uses 2 rows and 30 cols */ 136 #define GENSTATCOL 17 137 #define VMSTATROW 7 /* uses 17 rows and 15 cols */ 138 #define VMSTATCOL 64 139 #define GRAPHROW 5 /* uses 3 rows and 51 cols */ 140 #define GRAPHCOL 0 141 #define NAMEIROW 15 /* uses 3 rows and 38 cols (must be MEMROW + 5 + 1) */ 142 #define NAMEICOL 0 143 #define DISKROW 19 /* uses 5 rows and 50 cols (for 9 drives) */ 144 #define DISKCOL 0 145 #define DISKCOLWIDTH 8 146 #define DISKCOLEND INTSCOL 147 148 typedef struct intr_evcnt intr_evcnt_t; 149 struct intr_evcnt { 150 char *ie_group; 151 char *ie_name; 152 u_int64_t *ie_count; /* kernel address... */ 153 int ie_loc; /* screen row */ 154 } *ie_head; 155 int nevcnt; 156 157 static void 158 get_interrupt_events(void) 159 { 160 struct evcntlist allevents; 161 struct evcnt evcnt, *evptr; 162 intr_evcnt_t *ie; 163 intr_evcnt_t *n; 164 165 if (!NREAD(X_ALLEVENTS, &allevents, sizeof allevents)) 166 return; 167 evptr = TAILQ_FIRST(&allevents); 168 for (; evptr != NULL; evptr = TAILQ_NEXT(&evcnt, ev_list)) { 169 if (!KREAD(evptr, &evcnt, sizeof evcnt)) 170 return; 171 if (evcnt.ev_type != EVCNT_TYPE_INTR) 172 continue; 173 n = realloc(ie_head, sizeof *ie * (nevcnt + 1)); 174 if (n == NULL) { 175 error("realloc failed"); 176 die(0); 177 } 178 ie_head = n; 179 ie = ie_head + nevcnt; 180 ie->ie_group = malloc(evcnt.ev_grouplen + 1); 181 ie->ie_name = malloc(evcnt.ev_namelen + 1); 182 if (ie->ie_group == NULL || ie->ie_name == NULL) 183 return; 184 if (!KREAD(evcnt.ev_group, ie->ie_group, evcnt.ev_grouplen + 1)) 185 return; 186 if (!KREAD(evcnt.ev_name, ie->ie_name, evcnt.ev_namelen + 1)) 187 return; 188 ie->ie_count = &evptr->ev_count; 189 ie->ie_loc = 0; 190 nevcnt++; 191 } 192 } 193 194 int 195 initvmstat(void) 196 { 197 static char *intrnamebuf; 198 char *cp; 199 int i; 200 201 if (intrnamebuf) 202 free(intrnamebuf); 203 if (intrname) 204 free(intrname); 205 if (intrloc) 206 free(intrloc); 207 208 if (namelist[0].n_type == 0) { 209 if (kvm_nlist(kd, namelist) && 210 namelist[X_ALLEVENTS].n_type == 0) { 211 nlisterr(namelist); 212 return(0); 213 } 214 } 215 hertz = stathz ? stathz : hz; 216 if (!drvinit(1)) 217 return(0); 218 219 /* Old style interrupt counts - deprecated */ 220 nintr = (namelist[X_EINTRCNT].n_value - 221 namelist[X_INTRCNT].n_value) / sizeof (long); 222 if (nintr) { 223 intrloc = calloc(nintr, sizeof (long)); 224 intrname = calloc(nintr, sizeof (long)); 225 intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value - 226 namelist[X_INTRNAMES].n_value); 227 if (intrnamebuf == NULL || intrname == 0 || intrloc == 0) { 228 error("Out of memory\n"); 229 nintr = 0; 230 return(0); 231 } 232 NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) - 233 NVAL(X_INTRNAMES)); 234 for (cp = intrnamebuf, i = 0; i < nintr; i++) { 235 intrname[i] = cp; 236 cp += strlen(cp) + 1; 237 } 238 } 239 240 /* event counter interrupt counts */ 241 get_interrupt_events(); 242 243 nextintsrow = INTSROW + 1; 244 allocinfo(&s); 245 allocinfo(&s1); 246 allocinfo(&s2); 247 allocinfo(&z); 248 249 getinfo(&s2); 250 copyinfo(&s2, &s1); 251 return(1); 252 } 253 254 void 255 fetchvmstat(void) 256 { 257 time_t now; 258 259 time(&now); 260 strlcpy(buf, ctime(&now), sizeof(buf)); 261 buf[19] = '\0'; 262 getinfo(&s); 263 } 264 265 static void 266 print_ie_title(int i) 267 { 268 int width, name_width, group_width; 269 270 width = INTSCOLEND - (INTSCOL + 9); 271 if (width <= 0) 272 return; 273 274 move(ie_head[i].ie_loc, INTSCOL + 9); 275 group_width = strlen(ie_head[i].ie_group); 276 name_width = strlen(ie_head[i].ie_name); 277 width -= group_width + 1 + name_width; 278 if (width < 0) { 279 /* 280 * Screen to narrow for full strings 281 * This is all rather horrid, in some cases there are a lot 282 * of events in the same group, and in others the event 283 * name is "intr". There are also names which need 7 or 8 284 * columns before they become meaningful. 285 * This is a bad compromise. 286 */ 287 width = -width; 288 group_width -= (width + 1) / 2; 289 name_width -= width / 2; 290 /* some have the 'useful' name "intr", display their group */ 291 if (strcasecmp(ie_head[i].ie_name, "intr") == 0) { 292 group_width += name_width + 1; 293 name_width = 0; 294 } else { 295 if (group_width <= 3 || name_width < 0) { 296 /* don't display group */ 297 name_width += group_width + 1; 298 group_width = 0; 299 } 300 } 301 } 302 303 if (group_width != 0) { 304 printw("%-.*s", group_width, ie_head[i].ie_group); 305 if (name_width != 0) 306 printw(" "); 307 } 308 if (name_width != 0) 309 printw("%-.*s", name_width, ie_head[i].ie_name); 310 } 311 312 void 313 labelvmstat_top(void) 314 { 315 316 clear(); 317 318 mvprintw(STATROW, STATCOL + 4, "users Load"); 319 320 mvprintw(GENSTATROW, GENSTATCOL, " Csw Traps SysCal Intr Soft Fault"); 321 322 mvprintw(GRAPHROW, GRAPHCOL, 323 " . %% Sy . %% Us . %% Ni . %% In . %% Id"); 324 mvprintw(PROCSROW, PROCSCOL, "Proc:r d s"); 325 mvprintw(GRAPHROW + 1, GRAPHCOL, 326 "| | | | | | | | | | |"); 327 328 mvprintw(PAGEROW, PAGECOL + 8, "PAGING SWAPPING "); 329 mvprintw(PAGEROW + 1, PAGECOL, " in out in out "); 330 mvprintw(PAGEROW + 2, PAGECOL, " ops "); 331 mvprintw(PAGEROW + 3, PAGECOL, "pages "); 332 } 333 334 void 335 labelvmstat(void) 336 { 337 int i; 338 339 /* Top few lines first */ 340 341 labelvmstat_top(); 342 343 /* Left hand column */ 344 345 mvprintw(MEMROW + 0, MEMCOL, "Anon %% zero "); 346 mvprintw(MEMROW + 1, MEMCOL, "Exec %% wired "); 347 mvprintw(MEMROW + 2, MEMCOL, "File %% inact "); 348 mvprintw(MEMROW + 3, MEMCOL, "Meta %% bufs "); 349 mvprintw(MEMROW + 4, MEMCOL, " (kB) real swaponly free"); 350 mvprintw(MEMROW + 5, MEMCOL, "Active "); 351 352 mvprintw(NAMEIROW, NAMEICOL, "Namei Sys-cache Proc-cache"); 353 mvprintw(NAMEIROW + 1, NAMEICOL, 354 " Calls hits %% hits %%"); 355 356 mvprintw(DISKROW, DISKCOL, "%*s", DISKCOLWIDTH, "Disks:"); 357 if (disk_horiz) { 358 mvprintw(DISKROW + 1, DISKCOL + 1, "seeks"); 359 mvprintw(DISKROW + 2, DISKCOL + 1, "xfers"); 360 mvprintw(DISKROW + 3, DISKCOL + 1, "bytes"); 361 mvprintw(DISKROW + 4, DISKCOL + 1, "%%busy"); 362 } else { 363 mvprintw(DISKROW, DISKCOL + 1 * DISKCOLWIDTH, "%*s", DISKCOLWIDTH, "seeks"); 364 mvprintw(DISKROW, DISKCOL + 2 * DISKCOLWIDTH, "%*s", DISKCOLWIDTH, "xfers"); 365 mvprintw(DISKROW, DISKCOL + 3 * DISKCOLWIDTH, "%*s", DISKCOLWIDTH, "bytes"); 366 mvprintw(DISKROW, DISKCOL + 4 * DISKCOLWIDTH, "%*s", DISKCOLWIDTH, "%busy"); 367 } 368 369 /* Middle column */ 370 371 mvprintw(INTSROW, INTSCOL + 9, "Interrupts"); 372 for (i = 0; i < nintr; i++) { 373 if (intrloc[i] == 0) 374 continue; 375 mvprintw(intrloc[i], INTSCOL + 9, "%-.*s", 376 INTSCOLEND - (INTSCOL + 9), intrname[i]); 377 } 378 for (i = 0; i < nevcnt; i++) { 379 if (ie_head[i].ie_loc == 0) 380 continue; 381 print_ie_title(i); 382 } 383 384 /* Right hand column */ 385 386 mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks"); 387 mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw"); 388 mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm"); 389 mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait"); 390 mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck"); 391 mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok"); 392 mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram"); 393 mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy"); 394 mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp"); 395 mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod"); 396 mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow"); 397 mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin"); 398 mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg"); 399 mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg"); 400 mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "flnan"); 401 mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre"); 402 403 if (LINES - 1 > VMSTATROW + 16) 404 mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn"); 405 } 406 407 #define X(s, s1, fld) {temp = (s).fld[i]; (s).fld[i] -= (s1).fld[i]; \ 408 if (display_mode == TIME) (s1).fld[i] = temp;} 409 #define Z(s, s1, fld) {temp = (s).nchstats.fld; \ 410 (s).nchstats.fld -= (s1).nchstats.fld; \ 411 if (display_mode == TIME) (s1).nchstats.fld = temp;} 412 #define PUTRATE(s, s1, fld, l, c, w) \ 413 {temp = (s).fld; (s).fld -= (s1).fld; \ 414 if (display_mode == TIME) (s1).fld = temp; \ 415 putint((int)((float)(s).fld/etime + 0.5), l, c, w);} 416 417 static char cpuchar[CPUSTATES] = { '=' , '>', '-', '%', ' ' }; 418 static char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_INTR, CP_IDLE }; 419 420 void 421 show_vmstat_top(vmtotal_t *Total, uvmexp_sysctl_t *uvm, uvmexp_sysctl_t *uvm1) 422 { 423 float f1, f2; 424 int psiz; 425 int i, l, c; 426 struct { 427 struct uvmexp_sysctl *uvmexp; 428 } us, us1; 429 430 us.uvmexp = uvm; 431 us1.uvmexp = uvm1; 432 433 putint(ucount(), STATROW, STATCOL, 3); 434 putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0); 435 putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0); 436 putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0); 437 mvaddstr(STATROW, STATCOL + 53, buf); 438 439 putint(Total->t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3); 440 putint(Total->t_dw, PROCSROW + 1, PROCSCOL + 6, 3); 441 putint(Total->t_sl, PROCSROW + 1, PROCSCOL + 9, 3); 442 443 PUTRATE(us, us1, uvmexp->swtch, GENSTATROW + 1, GENSTATCOL - 1, 7); 444 PUTRATE(us, us1, uvmexp->traps, GENSTATROW + 1, GENSTATCOL + 7, 6); 445 PUTRATE(us, us1, uvmexp->syscalls, GENSTATROW + 1, GENSTATCOL + 14, 6); 446 PUTRATE(us, us1, uvmexp->intrs, GENSTATROW + 1, GENSTATCOL + 21, 5); 447 PUTRATE(us, us1, uvmexp->softs, GENSTATROW + 1, GENSTATCOL + 27, 6); 448 PUTRATE(us, us1, uvmexp->faults, GENSTATROW + 1, GENSTATCOL + 34, 6); 449 450 /* 451 * XXX it sure would be nice if this did what top(1) does and showed 452 * the utilization of each CPU on a separate line, though perhaps IFF 453 * the screen is tall enough 454 */ 455 /* Last CPU state not calculated yet. */ 456 for (f2 = 0.0, psiz = 0, c = 0; c < CPUSTATES; c++) { 457 i = cpuorder[c]; 458 f1 = cputime(i); 459 f2 += f1; 460 l = (int) ((f2 + 1.0) / 2.0) - psiz; 461 if (c == 0) 462 putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0); 463 else 464 putfloat(f1, GRAPHROW, GRAPHCOL + 10 * c + 1, 5, 1, 0); 465 mvhline(GRAPHROW + 2, psiz, cpuchar[c], l); 466 psiz += l; 467 } 468 469 PUTRATE(us, us1, uvmexp->pageins, PAGEROW + 2, PAGECOL + 5, 5); 470 PUTRATE(us, us1, uvmexp->pdpageouts, PAGEROW + 2, PAGECOL + 10, 5); 471 PUTRATE(us, us1, uvmexp->pgswapin, PAGEROW + 3, PAGECOL + 5, 5); 472 PUTRATE(us, us1, uvmexp->pgswapout, PAGEROW + 3, PAGECOL + 10, 5); 473 } 474 475 void 476 showvmstat(void) 477 { 478 int inttotal; 479 int i, l, r, c; 480 static int failcnt = 0; 481 static int relabel = 0; 482 static int last_disks = 0; 483 static u_long bufmem; 484 int mib[6]; 485 size_t size; 486 487 if (relabel) { 488 labelvmstat(); 489 relabel = 0; 490 } 491 492 cpuswap(); 493 if (display_mode == TIME) { 494 drvswap(); 495 if (toofast(&failcnt)) 496 return; 497 } else 498 etime = 1.0; 499 500 show_vmstat_top(&s.Total, &s.uvmexp, &s1.uvmexp); 501 502 /* Memory totals */ 503 #define pgtokb(pg) ((pg) * (s.uvmexp.pagesize / 1024)) 504 505 putint(pgtokb(s.uvmexp.anonpages), MEMROW + 0, MEMCOL + 7, 10); 506 putint((s.uvmexp.anonpages * 100 + 0.5) / s.uvmexp.npages, MEMROW + 0, MEMCOL + 17, 4); 507 508 putint(pgtokb(s.uvmexp.zeropages), MEMROW + 0, MEMCOL + 30, 8); 509 510 putint(pgtokb(s.uvmexp.execpages), MEMROW + 1, MEMCOL + 7, 10); 511 putint((s.uvmexp.execpages * 100 + 0.5) / s.uvmexp.npages, MEMROW + 1, MEMCOL + 17, 4); 512 513 putint(pgtokb(s.uvmexp.wired), MEMROW + 1, MEMCOL + 30, 8); 514 515 putint(pgtokb(s.uvmexp.filepages), MEMROW + 2, MEMCOL + 7, 10); 516 putint((s.uvmexp.filepages * 100 + 0.5) / s.uvmexp.npages, MEMROW + 2, MEMCOL + 17, 4); 517 518 putint(pgtokb(s.uvmexp.inactive), MEMROW + 2, MEMCOL + 30, 8); 519 520 /* Get total size of metadata buffers */ 521 size = sizeof(bufmem); 522 if (sysctlbyname("vm.bufmem", &bufmem, &size, NULL, 0) < 0) { 523 error("can't get buffers size: %s\n", strerror(errno)); 524 return; 525 } 526 527 /* Get number of metadata buffers */ 528 size = 0; 529 mib[0] = CTL_KERN; 530 mib[1] = KERN_BUF; 531 mib[2] = KERN_BUF_ALL; 532 mib[3] = KERN_BUF_ALL; 533 mib[4] = (int)sizeof(struct buf_sysctl); 534 mib[5] = INT_MAX; /* we want them all */ 535 if (sysctl(mib, 6, NULL, &size, NULL, 0) < 0) { 536 error("can't get buffers size: %s\n", strerror(errno)); 537 return; 538 } 539 if (size == 0) { 540 error("buffers size is zero: %s\n", strerror(errno)); 541 return; 542 } 543 nbuf = size / sizeof(struct buf_sysctl); 544 nbuf -= KERN_BUFSLOP; 545 546 putint((int) (bufmem / 1024), MEMROW + 3, MEMCOL + 5, 12); 547 putint((int) ((bufmem * 100) + 0.5) / s.uvmexp.pagesize / s.uvmexp.npages, 548 MEMROW + 3, MEMCOL + 17, 4); 549 putint(nbuf, MEMROW + 3, MEMCOL + 30, 8); 550 551 putint(pgtokb(s.uvmexp.active), MEMROW + 5, MEMCOL + 7, 10); 552 putint(pgtokb(s.uvmexp.swpgonly), MEMROW + 5, MEMCOL + 18, 10); 553 putint(pgtokb(s.uvmexp.free), MEMROW + 5, MEMCOL + 28, 10); 554 555 #undef pgtokb 556 557 /* Namei cache */ 558 Z(s, s1, ncs_goodhits); Z(s, s1, ncs_badhits); Z(s, s1, ncs_miss); 559 Z(s, s1, ncs_long); Z(s, s1, ncs_pass2); Z(s, s1, ncs_2passes); 560 s.nchcount = s.nchstats.ncs_goodhits + s.nchstats.ncs_badhits + 561 s.nchstats.ncs_miss + s.nchstats.ncs_long + 562 s.nchstats.ncs_pass2 + s.nchstats.ncs_2passes; 563 if (display_mode == TIME) 564 s1.nchcount = s.nchcount; 565 566 putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9); 567 putint(s.nchstats.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9); 568 #define nz(x) ((x) ? (x) : 1) 569 putfloat(s.nchstats.ncs_goodhits * 100.0 / nz(s.nchcount), 570 NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1); 571 putint(s.nchstats.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9); 572 putfloat(s.nchstats.ncs_pass2 * 100.0 / nz(s.nchcount), 573 NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1); 574 #undef nz 575 576 /* Disks */ 577 for (l = 0, i = 0, r = DISKROW, c = DISKCOL; 578 i < (int)ndrive; i++) { 579 if (!drv_select[i]) 580 continue; 581 582 if (disk_horiz) 583 c += DISKCOLWIDTH; 584 else 585 r++; 586 if (c + DISKCOLWIDTH > DISKCOLEND) { 587 if (disk_horiz && LINES - 1 - DISKROW > 588 (DISKCOLEND - DISKCOL) / DISKCOLWIDTH) { 589 disk_horiz = 0; 590 relabel = 1; 591 } 592 break; 593 } 594 if (r >= LINES - 1) { 595 if (!disk_horiz && LINES - 1 - DISKROW < 596 (DISKCOLEND - DISKCOL) / DISKCOLWIDTH) { 597 disk_horiz = 1; 598 relabel = 1; 599 } 600 break; 601 } 602 l++; 603 604 dinfo(i, r, c); 605 } 606 /* blank out if we lost any disks */ 607 for (i = l; i < last_disks; i++) { 608 int j; 609 if (disk_horiz) 610 c += DISKCOLWIDTH; 611 else 612 r++; 613 for (j = 0; j < 5; j++) { 614 if (disk_horiz) 615 mvprintw(r+j, c, "%*s", DISKCOLWIDTH, ""); 616 else 617 mvprintw(r, c+j*DISKCOLWIDTH, "%*s", DISKCOLWIDTH, ""); 618 } 619 } 620 last_disks = l; 621 622 /* Interrupts */ 623 failcnt = 0; 624 inttotal = 0; 625 for (i = 0; i < nintr; i++) { 626 if (s.intrcnt[i] == 0) 627 continue; 628 if (intrloc[i] == 0) { 629 if (nextintsrow == LINES) 630 continue; 631 intrloc[i] = nextintsrow++; 632 mvprintw(intrloc[i], INTSCOL + 9, "%-.*s", 633 INTSCOLEND - (INTSCOL + 9), intrname[i]); 634 } 635 X(s, s1, intrcnt); 636 l = (int)((float)s.intrcnt[i]/etime + 0.5); 637 inttotal += l; 638 putint(l, intrloc[i], INTSCOL, 8); 639 } 640 641 for (i = 0; i < nevcnt; i++) { 642 if (s.evcnt[i] == 0) 643 continue; 644 if (ie_head[i].ie_loc == 0) { 645 if (nextintsrow == LINES) 646 continue; 647 ie_head[i].ie_loc = nextintsrow++; 648 print_ie_title(i); 649 } 650 X(s, s1, evcnt); 651 l = (int)((float)s.evcnt[i]/etime + 0.5); 652 inttotal += l; 653 putint(l, ie_head[i].ie_loc, INTSCOL, 8); 654 } 655 putint(inttotal, INTSROW, INTSCOL, 8); 656 657 PUTRATE(s, s1, uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6); 658 PUTRATE(s, s1, uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6); 659 PUTRATE(s, s1, uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6); 660 PUTRATE(s, s1, uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5); 661 PUTRATE(s, s1, uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6); 662 PUTRATE(s, s1, uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6); 663 PUTRATE(s, s1, uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6); 664 PUTRATE(s, s1, uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6); 665 PUTRATE(s, s1, uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6); 666 PUTRATE(s, s1, uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6); 667 PUTRATE(s, s1, uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9); 668 putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9); 669 putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9); 670 putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9); 671 putint(s.uvmexp.fltnoanon, VMSTATROW + 14, VMSTATCOL, 9); 672 PUTRATE(s, s1, uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9); 673 if (LINES - 1 > VMSTATROW + 16) 674 PUTRATE(s, s1, uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9); 675 676 } 677 678 void 679 vmstat_boot(char *args) 680 { 681 copyinfo(&z, &s1); 682 display_mode = BOOT; 683 } 684 685 void 686 vmstat_run(char *args) 687 { 688 copyinfo(&s1, &s2); 689 display_mode = RUN; 690 } 691 692 void 693 vmstat_time(char *args) 694 { 695 display_mode = TIME; 696 } 697 698 void 699 vmstat_zero(char *args) 700 { 701 if (display_mode == RUN) 702 getinfo(&s1); 703 } 704 705 /* calculate number of users on the system */ 706 static int 707 ucount(void) 708 { 709 static int onusers = -1; 710 int nusers = 0; 711 struct utmpentry *ehead; 712 713 nusers = getutentries(NULL, &ehead); 714 715 if (nusers != onusers) { 716 if (nusers == 1) 717 mvprintw(STATROW, STATCOL + 8, " "); 718 else 719 mvprintw(STATROW, STATCOL + 8, "s"); 720 } 721 onusers = nusers; 722 return (nusers); 723 } 724 725 static float 726 cputime(int indx) 727 { 728 double t; 729 int i; 730 731 t = 0; 732 for (i = 0; i < CPUSTATES; i++) 733 t += cur.cp_time[i]; 734 if (t == 0.0) 735 t = 1.0; 736 return (cur.cp_time[indx] * 100.0 / t); 737 } 738 739 void 740 puthumanint_scale(u_int64_t n, int l, int c, int w, int scale) 741 { 742 char b[128]; 743 744 if (move(l, c) != OK) 745 return; 746 if (n == 0 && !showzero) { 747 hline(' ', w); 748 return; 749 } 750 if (humanize_number(b, w, n, "", scale, HN_NOSPACE) == -1 ) { 751 hline('*', w); 752 return; 753 } 754 printw("%*s", w, b); 755 } 756 757 void 758 puthumanint_sticky(u_int64_t n, int l, int c, int w, int *scale) 759 { 760 char b[128]; 761 int sc; 762 763 sc = humanize_number(b, w, n, "", HN_GETSCALE, HN_NOSPACE); 764 if (sc > *scale) 765 *scale = sc; 766 else 767 sc = *scale; 768 769 puthumanint_scale(n, l, c, w, sc); 770 } 771 772 void 773 puthumanint(u_int64_t n, int l, int c, int w) 774 { 775 776 puthumanint_scale(n, l, c, w, HN_AUTOSCALE); 777 } 778 779 void 780 putint(int n, int l, int c, int w) 781 { 782 char b[128]; 783 784 if (move(l, c) != OK) 785 return; 786 if (n == 0 && !showzero) { 787 hline(' ', w); 788 return; 789 } 790 (void)snprintf(b, sizeof b, "%*d", w, n); 791 if ((int)strlen(b) > w) { 792 if (display_mode == TIME) 793 hline('*', w); 794 else 795 puthumanint(n, l, c, w); 796 return; 797 } 798 addstr(b); 799 } 800 801 void 802 putfloat(double f, int l, int c, int w, int d, int nz) 803 { 804 char b[128]; 805 806 if (move(l, c) != OK) 807 return; 808 if (nz && f == 0.0 && !showzero) { 809 hline(' ', w); 810 return; 811 } 812 (void)snprintf(b, sizeof b, "%*.*f", w, d, f); 813 if ((int)strlen(b) > w) { 814 hline('*', w); 815 return; 816 } 817 addstr(b); 818 } 819 820 static void 821 getinfo(struct Info *stats) 822 { 823 int mib[2]; 824 size_t size; 825 int i; 826 827 cpureadstats(); 828 drvreadstats(); 829 size = sizeof(stats->nchstats); 830 if (sysctlbyname("vfs.namecache_stats", &stats->nchstats, &size, 831 NULL, 0) < 0) { 832 error("can't get namecache statistics: %s\n", strerror(errno)); 833 memset(&stats->nchstats, 0, sizeof(stats->nchstats)); 834 } 835 if (nintr) 836 NREAD(X_INTRCNT, stats->intrcnt, nintr * sizeof(long)); 837 for (i = 0; i < nevcnt; i++) 838 KREAD(ie_head[i].ie_count, &stats->evcnt[i], 839 sizeof stats->evcnt[i]); 840 size = sizeof(stats->uvmexp); 841 mib[0] = CTL_VM; 842 mib[1] = VM_UVMEXP2; 843 if (sysctl(mib, 2, &stats->uvmexp, &size, NULL, 0) < 0) { 844 error("can't get uvmexp: %s\n", strerror(errno)); 845 memset(&stats->uvmexp, 0, sizeof(stats->uvmexp)); 846 } 847 size = sizeof(stats->Total); 848 mib[0] = CTL_VM; 849 mib[1] = VM_METER; 850 if (sysctl(mib, 2, &stats->Total, &size, NULL, 0) < 0) { 851 error("Can't get kernel info: %s\n", strerror(errno)); 852 memset(&stats->Total, 0, sizeof(stats->Total)); 853 } 854 } 855 856 static void 857 allocinfo(struct Info *stats) 858 { 859 860 if (nintr && 861 (stats->intrcnt = calloc(nintr, sizeof(long))) == NULL) { 862 error("calloc failed"); 863 die(0); 864 } 865 if ((stats->evcnt = calloc(nevcnt, sizeof(u_int64_t))) == NULL) { 866 error("calloc failed"); 867 die(0); 868 } 869 } 870 871 static void 872 copyinfo(struct Info *from, struct Info *to) 873 { 874 long *intrcnt; 875 u_int64_t *evcnt; 876 877 intrcnt = to->intrcnt; 878 evcnt = to->evcnt; 879 *to = *from; 880 memmove(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof *intrcnt); 881 memmove(to->evcnt = evcnt, from->evcnt, nevcnt * sizeof *evcnt); 882 } 883 884 static void 885 dinfo(int dn, int r, int c) 886 { 887 double atime, dtime; 888 #define ADV if (disk_horiz) r++; else c += DISKCOLWIDTH 889 890 /* elapsed time for disk stats */ 891 dtime = etime; 892 if (cur.timestamp[dn].tv_sec || cur.timestamp[dn].tv_usec) { 893 dtime = (double)cur.timestamp[dn].tv_sec + 894 ((double)cur.timestamp[dn].tv_usec / (double)1000000); 895 } 896 897 mvprintw(r, c, "%*.*s", DISKCOLWIDTH, DISKCOLWIDTH, dr_name[dn]); 898 ADV; 899 900 putint((int)(cur.seek[dn]/dtime+0.5), r, c, DISKCOLWIDTH); 901 ADV; 902 putint((int)((cur.rxfer[dn]+cur.wxfer[dn])/dtime+0.5), 903 r, c, DISKCOLWIDTH); 904 ADV; 905 puthumanint_sticky((cur.rbytes[dn] + cur.wbytes[dn]) / dtime + 0.5, 906 r, c, DISKCOLWIDTH, &cur.scale[dn]); 907 ADV; 908 909 /* time busy in disk activity */ 910 atime = cur.time[dn].tv_sec + cur.time[dn].tv_usec / 1000000.0; 911 atime = atime * 100.0 / dtime; 912 if (atime >= 100) 913 putint(100, r, c, DISKCOLWIDTH); 914 else 915 putfloat(atime, r, c, DISKCOLWIDTH, 1, 1); 916 } 917