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