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