71#include <devstat.h> 72#include "systat.h" 73#include "extern.h" 74#include "devs.h" 75 76static struct Info { 77 long time[CPUSTATES]; 78 u_int v_swtch; /* context switches */ 79 u_int v_trap; /* calls to trap */ 80 u_int v_syscall; /* calls to syscall() */ 81 u_int v_intr; /* device interrupts */ 82 u_int v_soft; /* software interrupts */ 83 /* 84 * Virtual memory activity. 85 */ 86 u_int v_vm_faults; /* number of address memory faults */ 87 u_int v_cow_faults; /* number of copy-on-writes */ 88 u_int v_zfod; /* pages zero filled on demand */ 89 u_int v_ozfod; /* optimized zero fill pages */ 90 u_int v_swapin; /* swap pager pageins */ 91 u_int v_swapout; /* swap pager pageouts */ 92 u_int v_swappgsin; /* swap pager pages paged in */ 93 u_int v_swappgsout; /* swap pager pages paged out */ 94 u_int v_vnodein; /* vnode pager pageins */ 95 u_int v_vnodeout; /* vnode pager pageouts */ 96 u_int v_vnodepgsin; /* vnode_pager pages paged in */ 97 u_int v_vnodepgsout; /* vnode pager pages paged out */ 98 u_int v_intrans; /* intransit blocking page faults */ 99 u_int v_reactivated; /* number of pages reactivated from free list */ 100 u_int v_pdwakeups; /* number of times daemon has awaken from sleep */ 101 u_int v_pdpages; /* number of pages analyzed by daemon */ 102 103 u_int v_dfree; /* pages freed by daemon */ 104 u_int v_pfree; /* pages freed by exiting processes */ 105 u_int v_tfree; /* total pages freed */ 106 /* 107 * Distribution of page usages. 108 */ 109 u_int v_page_size; /* page size in bytes */ 110 u_int v_free_count; /* number of pages free */ 111 u_int v_wire_count; /* number of pages wired down */ 112 u_int v_active_count; /* number of pages active */ 113 u_int v_inactive_count; /* number of pages inactive */ 114 u_int v_cache_count; /* number of pages on buffer cache queue */ 115 struct vmtotal Total; 116 struct nchstats nchstats; 117 long nchcount; 118 long *intrcnt; 119 long bufspace; 120 int desiredvnodes; 121 long numvnodes; 122 long freevnodes; 123 int numdirtybuffers; 124} s, s1, s2, z; 125 126struct statinfo cur, last, run; 127 128#define total s.Total 129#define nchtotal s.nchstats 130#define oldnchtotal s1.nchstats 131 132static enum state { BOOT, TIME, RUN } state = TIME; 133 134static void allocinfo(struct Info *); 135static void copyinfo(struct Info *, struct Info *); 136static float cputime(int); 137static void dinfo(int, int, struct statinfo *, struct statinfo *); 138static void getinfo(struct Info *); 139static void putint(int, int, int, int); 140static void putfloat(double, int, int, int, int, int); 141static void putlongdouble(long double, int, int, int, int, int); 142static int ucount(void); 143 144static int ncpu; 145static char buf[26]; 146static time_t t; 147static double etime; 148static int nintr; 149static long *intrloc; 150static char **intrname; 151static int nextintsrow; 152 153WINDOW * 154openkre(void) 155{ 156 157 return (stdscr); 158} 159 160void 161closekre(WINDOW *w) 162{ 163 164 if (w == NULL) 165 return; 166 wclear(w); 167 wrefresh(w); 168} 169 170/* 171 * These constants define where the major pieces are laid out 172 */ 173#define STATROW 0 /* uses 1 row and 67 cols */ 174#define STATCOL 0 175#define MEMROW 2 /* uses 4 rows and 45 cols */ 176#define MEMCOL 0 177#define PAGEROW 2 /* uses 4 rows and 30 cols */ 178#define PAGECOL 47 179#define INTSROW 6 /* uses all rows to bottom and 16 cols */ 180#define INTSCOL 64 181#define PROCSROW 6 /* uses 3 rows and 19 cols */ 182#define PROCSCOL 0 183#define GENSTATROW 7 /* uses 2 rows and 29 cols */ 184#define GENSTATCOL 21 185#define VMSTATROW 7 /* uses 17 rows and 12-14 cols */ 186#define VMSTATCOL 49 /* actually 50-51 for some fields */ 187#define GRAPHROW 10 /* uses 3 rows and 49-51 cols */ 188#define GRAPHCOL 0 189#define VNSTATROW 13 /* uses 4 rows and 13 columns */ 190#define VNSTATCOL 35 191#define NAMEIROW 14 /* uses 3 rows and 32 cols */ 192#define NAMEICOL 0 193#define DISKROW 18 /* uses 5 rows and 47 cols (for 7 drives) */ 194#define DISKCOL 0 195 196#define DRIVESPACE 7 /* max # for space */ 197 198#define MAXDRIVES DRIVESPACE /* max # to display */ 199 200int 201initkre(void) 202{ 203 char *cp, *cp1, *cp2, *intrnamebuf, *nextcp; 204 int i; 205 size_t sz; 206 207 if ((num_devices = devstat_getnumdevs(NULL)) < 0) { 208 warnx("%s", devstat_errbuf); 209 return(0); 210 } 211 212 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 213 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 214 run.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 215 bzero(cur.dinfo, sizeof(struct devinfo)); 216 bzero(last.dinfo, sizeof(struct devinfo)); 217 bzero(run.dinfo, sizeof(struct devinfo)); 218 219 if (dsinit(MAXDRIVES, &cur, &last, &run) != 1) 220 return(0); 221 222 if (nintr == 0) { 223 if (sysctlbyname("hw.intrcnt", NULL, &sz, NULL, 0) == -1) { 224 error("sysctl(hw.intrcnt...) failed: %s", 225 strerror(errno)); 226 return (0); 227 } 228 nintr = sz / sizeof(u_long); 229 intrloc = calloc(nintr, sizeof (long)); 230 intrname = calloc(nintr, sizeof (char *)); 231 intrnamebuf = sysctl_dynread("hw.intrnames", NULL); 232 if (intrnamebuf == NULL || intrname == NULL || 233 intrloc == NULL) { 234 error("Out of memory"); 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 for (cp = intrnamebuf, i = 0; i < nintr; i++) { 245 nextcp = cp + strlen(cp) + 1; 246 247 /* Discard trailing spaces. */ 248 for (cp1 = nextcp - 1; cp1 > cp && *(cp1 - 1) == ' '; ) 249 *--cp1 = '\0'; 250 251 /* Convert "irqN: name" to "name irqN". */ 252 if (strncmp(cp, "irq", 3) == 0) { 253 cp1 = cp + 3; 254 while (isdigit((u_char)*cp1)) 255 cp1++; 256 if (cp1 != cp && *cp1 == ':' && 257 *(cp1 + 1) == ' ') { 258 *cp1 = '\0'; 259 cp1 = cp1 + 2; 260 cp2 = strdup(cp); 261 bcopy(cp1, cp, strlen(cp1) + 1); 262 strcat(cp, " "); 263 strcat(cp, cp2); 264 free(cp2); 265 } 266 } 267 268 /* 269 * Convert "name irqN" to "name N" if the former is 270 * longer than the field width. 271 */ 272 if ((cp1 = strstr(cp, "irq")) != NULL && 273 strlen(cp) > 10) 274 bcopy(cp1 + 3, cp1, strlen(cp1 + 3) + 1); 275 276 intrname[i] = cp; 277 cp = nextcp; 278 } 279 nextintsrow = INTSROW + 2; 280 allocinfo(&s); 281 allocinfo(&s1); 282 allocinfo(&s2); 283 allocinfo(&z); 284 } 285 getinfo(&s2); 286 copyinfo(&s2, &s1); 287 return(1); 288} 289 290void 291fetchkre(void) 292{ 293 time_t now; 294 struct tm *tp; 295 static int d_first = -1; 296 297 if (d_first < 0) 298 d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 299 300 time(&now); 301 tp = localtime(&now); 302 (void) strftime(buf, sizeof(buf), 303 d_first ? "%e %b %R" : "%b %e %R", tp); 304 getinfo(&s); 305} 306 307void 308labelkre(void) 309{ 310 int i, j; 311 312 clear(); 313 mvprintw(STATROW, STATCOL + 6, "users Load"); 314 mvprintw(MEMROW, MEMCOL, "Mem:KB REAL VIRTUAL"); 315 mvprintw(MEMROW + 1, MEMCOL, " Tot Share Tot Share"); 316 mvprintw(MEMROW + 2, MEMCOL, "Act"); 317 mvprintw(MEMROW + 3, MEMCOL, "All"); 318 319 mvprintw(MEMROW + 1, MEMCOL + 41, "Free"); 320 321 mvprintw(PAGEROW, PAGECOL, " VN PAGER SWAP PAGER"); 322 mvprintw(PAGEROW + 1, PAGECOL, " in out in out"); 323 mvprintw(PAGEROW + 2, PAGECOL, "count"); 324 mvprintw(PAGEROW + 3, PAGECOL, "pages"); 325 326 mvprintw(INTSROW, INTSCOL + 1, "Interrupts"); 327 mvprintw(INTSROW + 1, INTSCOL + 6, "total"); 328 329 mvprintw(VMSTATROW, VMSTATCOL + 9, "cow"); 330 mvprintw(VMSTATROW + 1, VMSTATCOL + 9, "zfod"); 331 mvprintw(VMSTATROW + 2, VMSTATCOL + 9, "ozfod"); 332 mvprintw(VMSTATROW + 3, VMSTATCOL + 9 - 1, "%%ozfod"); 333 mvprintw(VMSTATROW + 4, VMSTATCOL + 9, "daefr"); 334 mvprintw(VMSTATROW + 5, VMSTATCOL + 9, "prcfr"); 335 mvprintw(VMSTATROW + 6, VMSTATCOL + 9, "totfr"); 336 mvprintw(VMSTATROW + 7, VMSTATCOL + 9, "react"); 337 mvprintw(VMSTATROW + 8, VMSTATCOL + 9, "pdwak"); 338 mvprintw(VMSTATROW + 9, VMSTATCOL + 9, "pdpgs"); 339 mvprintw(VMSTATROW + 10, VMSTATCOL + 9, "intrn"); 340 mvprintw(VMSTATROW + 11, VMSTATCOL + 9, "wire"); 341 mvprintw(VMSTATROW + 12, VMSTATCOL + 9, "act"); 342 mvprintw(VMSTATROW + 13, VMSTATCOL + 9, "inact"); 343 mvprintw(VMSTATROW + 14, VMSTATCOL + 9, "cache"); 344 mvprintw(VMSTATROW + 15, VMSTATCOL + 9, "free"); 345 if (LINES - 1 > VMSTATROW + 16) 346 mvprintw(VMSTATROW + 16, VMSTATCOL + 9, "buf"); 347 348 mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt"); 349 350 mvprintw(GRAPHROW, GRAPHCOL, 351 " . %%Sys . %%Intr . %%User . %%Nice . %%Idle"); 352 mvprintw(PROCSROW, PROCSCOL, "Proc:"); 353 mvprintw(PROCSROW + 1, PROCSCOL, " r p d s w"); 354 mvprintw(GRAPHROW + 1, GRAPHCOL, 355 "| | | | | | | | | | |"); 356 357 mvprintw(VNSTATROW, VNSTATCOL + 8, "dtbuf"); 358 mvprintw(VNSTATROW + 1, VNSTATCOL + 8, "desvn"); 359 mvprintw(VNSTATROW + 2, VNSTATCOL + 8, "numvn"); 360 mvprintw(VNSTATROW + 3, VNSTATCOL + 8, "frevn"); 361 362 mvprintw(NAMEIROW, NAMEICOL, "Namei Name-cache Dir-cache"); 363 mvprintw(NAMEIROW + 1, NAMEICOL, 364 " Calls hits %% hits %%"); 365 mvprintw(DISKROW, DISKCOL, "Disks"); 366 mvprintw(DISKROW + 1, DISKCOL, "KB/t"); 367 mvprintw(DISKROW + 2, DISKCOL, "tps"); 368 mvprintw(DISKROW + 3, DISKCOL, "MB/s"); 369 mvprintw(DISKROW + 4, DISKCOL, "%%busy"); 370 /* 371 * For now, we don't support a fourth disk statistic. So there's 372 * no point in providing a label for it. If someone can think of a 373 * fourth useful disk statistic, there is room to add it. 374 */ 375 /* mvprintw(DISKROW + 4, DISKCOL, " msps"); */ 376 j = 0; 377 for (i = 0; i < num_devices && j < MAXDRIVES; i++) 378 if (dev_select[i].selected) { 379 char tmpstr[80]; 380 sprintf(tmpstr, "%s%d", dev_select[i].device_name, 381 dev_select[i].unit_number); 382 mvprintw(DISKROW, DISKCOL + 5 + 6 * j, 383 " %5.5s", tmpstr); 384 j++; 385 } 386 387 for (i = 0; i < nintr; i++) { 388 if (intrloc[i] == 0) 389 continue; 390 mvprintw(intrloc[i], INTSCOL + 6, "%-10.10s", intrname[i]); 391 } 392} 393 394#define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;} 395#define Q(fld) {t=cur.fld[i]; cur.fld[i]-=last.fld[i]; if(state==TIME) last.fld[i]=t;} 396#define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;} 397#define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \ 398 if(state == TIME) s1.nchstats.fld = t;} 399#define PUTRATE(fld, l, c, w) \ 400do { \ 401 Y(fld); \ 402 putint((int)((float)s.fld/etime + 0.5), l, c, w); \ 403} while (0) 404#define MAXFAIL 5 405 406static char cpuchar[CPUSTATES] = { '=' , '+', '>', '-', ' ' }; 407static char cpuorder[CPUSTATES] = { CP_SYS, CP_INTR, CP_USER, CP_NICE, 408 CP_IDLE }; 409 410void 411showkre(void) 412{ 413 float f1, f2; 414 int psiz, inttotal; 415 int i, l, lc; 416 static int failcnt = 0; 417 418 etime = 0; 419 for(i = 0; i < CPUSTATES; i++) { 420 X(time); 421 Q(cp_time); 422 etime += s.time[i]; 423 } 424 if (etime < 5.0) { /* < 5 ticks - ignore this trash */ 425 if (failcnt++ >= MAXFAIL) { 426 clear(); 427 mvprintw(2, 10, "The alternate system clock has died!"); 428 mvprintw(3, 10, "Reverting to ``pigs'' display."); 429 move(CMDLINE, 0); 430 refresh(); 431 failcnt = 0; 432 sleep(5); 433 command("pigs"); 434 } 435 return; 436 } 437 failcnt = 0; 438 etime /= hertz; 439 etime /= ncpu; 440 inttotal = 0; 441 for (i = 0; i < nintr; i++) { 442 if (s.intrcnt[i] == 0) 443 continue; 444 if (intrloc[i] == 0) { 445 if (nextintsrow == LINES) 446 continue; 447 intrloc[i] = nextintsrow++; 448 mvprintw(intrloc[i], INTSCOL + 6, "%-10.10s", 449 intrname[i]); 450 } 451 X(intrcnt); 452 l = (int)((float)s.intrcnt[i]/etime + 0.5); 453 inttotal += l; 454 putint(l, intrloc[i], INTSCOL, 5); 455 } 456 putint(inttotal, INTSROW + 1, INTSCOL, 5); 457 Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss); 458 Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes); Z(ncs_neghits); 459 s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits + 460 nchtotal.ncs_miss + nchtotal.ncs_long + nchtotal.ncs_neghits; 461 if (state == TIME) 462 s1.nchcount = s.nchcount; 463 464 psiz = 0; 465 f2 = 0.0; 466 for (lc = 0; lc < CPUSTATES; lc++) { 467 i = cpuorder[lc]; 468 f1 = cputime(i); 469 f2 += f1; 470 l = (int) ((f2 + 1.0) / 2.0) - psiz; 471 putfloat(f1, GRAPHROW, GRAPHCOL + 10 * lc, 4, 1, 0); 472 move(GRAPHROW + 2, psiz); 473 psiz += l; 474 while (l-- > 0) 475 addch(cpuchar[lc]); 476 } 477 478 putint(ucount(), STATROW, STATCOL, 5); 479 putfloat(avenrun[0], STATROW, STATCOL + 20, 5, 2, 0); 480 putfloat(avenrun[1], STATROW, STATCOL + 26, 5, 2, 0); 481 putfloat(avenrun[2], STATROW, STATCOL + 32, 5, 2, 0); 482 mvaddstr(STATROW, STATCOL + 55, buf); 483#define pgtokb(pg) ((pg) * (s.v_page_size / 1024)) 484 putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 4, 7); 485 putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 12, 7); 486 putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 20, 8); 487 putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 29, 8); 488 putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 4, 7); 489 putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 12, 7); 490 putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 20, 8); 491 putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 29, 8); 492 putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 38, 7); 493 putint(total.t_rq - 1, PROCSROW + 2, PROCSCOL, 3); 494 putint(total.t_pw, PROCSROW + 2, PROCSCOL + 4, 3); 495 putint(total.t_dw, PROCSROW + 2, PROCSCOL + 8, 3); 496 putint(total.t_sl, PROCSROW + 2, PROCSCOL + 12, 3); 497 putint(total.t_sw, PROCSROW + 2, PROCSCOL + 16, 3); 498 PUTRATE(v_cow_faults, VMSTATROW, VMSTATCOL + 2, 8 - 2); 499 PUTRATE(v_zfod, VMSTATROW + 1, VMSTATCOL + 2, 8 - 2); 500 PUTRATE(v_ozfod, VMSTATROW + 2, VMSTATCOL, 8); 501 putint(s.v_zfod != 0 ? (int)(s.v_ozfod * 100.0 / s.v_zfod) : 0, 502 VMSTATROW + 3, VMSTATCOL + 1, 8 - 1); 503 PUTRATE(v_dfree, VMSTATROW + 4, VMSTATCOL + 2, 8 - 2); 504 PUTRATE(v_pfree, VMSTATROW + 5, VMSTATCOL + 2, 8 - 2); 505 PUTRATE(v_tfree, VMSTATROW + 6, VMSTATCOL, 8); 506 PUTRATE(v_reactivated, VMSTATROW + 7, VMSTATCOL, 8); 507 PUTRATE(v_pdwakeups, VMSTATROW + 8, VMSTATCOL, 8); 508 PUTRATE(v_pdpages, VMSTATROW + 9, VMSTATCOL, 8); 509 PUTRATE(v_intrans, VMSTATROW + 10, VMSTATCOL, 8); 510 putint(pgtokb(s.v_wire_count), VMSTATROW + 11, VMSTATCOL, 8); 511 putint(pgtokb(s.v_active_count), VMSTATROW + 12, VMSTATCOL, 8); 512 putint(pgtokb(s.v_inactive_count), VMSTATROW + 13, VMSTATCOL, 8); 513 putint(pgtokb(s.v_cache_count), VMSTATROW + 14, VMSTATCOL, 8); 514 putint(pgtokb(s.v_free_count), VMSTATROW + 15, VMSTATCOL, 8); 515 if (LINES - 1 > VMSTATROW + 16) 516 putint(s.bufspace / 1024, VMSTATROW + 16, VMSTATCOL, 8); 517 PUTRATE(v_vnodein, PAGEROW + 2, PAGECOL + 6, 5); 518 PUTRATE(v_vnodeout, PAGEROW + 2, PAGECOL + 12, 5); 519 PUTRATE(v_swapin, PAGEROW + 2, PAGECOL + 19, 5); 520 PUTRATE(v_swapout, PAGEROW + 2, PAGECOL + 25, 5); 521 PUTRATE(v_vnodepgsin, PAGEROW + 3, PAGECOL + 6, 5); 522 PUTRATE(v_vnodepgsout, PAGEROW + 3, PAGECOL + 12, 5); 523 PUTRATE(v_swappgsin, PAGEROW + 3, PAGECOL + 19, 5); 524 PUTRATE(v_swappgsout, PAGEROW + 3, PAGECOL + 25, 5); 525 PUTRATE(v_swtch, GENSTATROW + 1, GENSTATCOL, 4); 526 PUTRATE(v_trap, GENSTATROW + 1, GENSTATCOL + 5, 4); 527 PUTRATE(v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 4); 528 PUTRATE(v_intr, GENSTATROW + 1, GENSTATCOL + 15, 4); 529 PUTRATE(v_soft, GENSTATROW + 1, GENSTATCOL + 20, 4); 530 PUTRATE(v_vm_faults, GENSTATROW + 1, GENSTATCOL + 25, 4); 531 for (i = 0, lc = 0; i < num_devices && lc < MAXDRIVES; i++) 532 if (dev_select[i].selected) { 533 switch(state) { 534 case TIME: 535 dinfo(i, ++lc, &cur, &last); 536 break; 537 case RUN: 538 dinfo(i, ++lc, &cur, &run); 539 break; 540 case BOOT: 541 dinfo(i, ++lc, &cur, NULL); 542 break; 543 } 544 } 545 putint(s.numdirtybuffers, VNSTATROW, VNSTATCOL, 7); 546 putint(s.desiredvnodes, VNSTATROW + 1, VNSTATCOL, 7); 547 putint(s.numvnodes, VNSTATROW + 2, VNSTATCOL, 7); 548 putint(s.freevnodes, VNSTATROW + 3, VNSTATCOL, 7); 549 putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 8); 550 putint((nchtotal.ncs_goodhits + nchtotal.ncs_neghits), 551 NAMEIROW + 2, NAMEICOL + 9, 7); 552#define nz(x) ((x) ? (x) : 1) 553 putfloat((nchtotal.ncs_goodhits+nchtotal.ncs_neghits) * 554 100.0 / nz(s.nchcount), 555 NAMEIROW + 2, NAMEICOL + 17, 3, 0, 1); 556 putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 21, 7); 557 putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount), 558 NAMEIROW + 2, NAMEICOL + 29, 3, 0, 1); 559#undef nz 560} 561 562int 563cmdkre(const char *cmd, const char *args) 564{ 565 int retval; 566 567 if (prefix(cmd, "run")) { 568 retval = 1; 569 copyinfo(&s2, &s1); 570 switch (devstat_getdevs(NULL, &run)) { 571 case -1: 572 errx(1, "%s", devstat_errbuf); 573 break; 574 case 1: 575 num_devices = run.dinfo->numdevs; 576 generation = run.dinfo->generation; 577 retval = dscmd("refresh", NULL, MAXDRIVES, &cur); 578 if (retval == 2) 579 labelkre(); 580 break; 581 default: 582 break; 583 } 584 state = RUN; 585 return (retval); 586 } 587 if (prefix(cmd, "boot")) { 588 state = BOOT; 589 copyinfo(&z, &s1); 590 return (1); 591 } 592 if (prefix(cmd, "time")) { 593 state = TIME; 594 return (1); 595 } 596 if (prefix(cmd, "zero")) { 597 retval = 1; 598 if (state == RUN) { 599 getinfo(&s1); 600 switch (devstat_getdevs(NULL, &run)) { 601 case -1: 602 errx(1, "%s", devstat_errbuf); 603 break; 604 case 1: 605 num_devices = run.dinfo->numdevs; 606 generation = run.dinfo->generation; 607 retval = dscmd("refresh",NULL, MAXDRIVES, &cur); 608 if (retval == 2) 609 labelkre(); 610 break; 611 default: 612 break; 613 } 614 } 615 return (retval); 616 } 617 retval = dscmd(cmd, args, MAXDRIVES, &cur); 618 619 if (retval == 2) 620 labelkre(); 621 622 return(retval); 623} 624 625/* calculate number of users on the system */ 626static int 627ucount(void) 628{ 629 int nusers = 0; 630 struct utmpx *ut; 631 632 setutxent(); 633 while ((ut = getutxent()) != NULL) 634 if (ut->ut_type == USER_PROCESS) 635 nusers++; 636 endutxent(); 637 638 return (nusers); 639} 640 641static float 642cputime(int indx) 643{ 644 double lt; 645 int i; 646 647 lt = 0; 648 for (i = 0; i < CPUSTATES; i++) 649 lt += s.time[i]; 650 if (lt == 0.0) 651 lt = 1.0; 652 return (s.time[indx] * 100.0 / lt); 653} 654 655static void 656putint(int n, int l, int lc, int w) 657{ 658 int snr; 659 char b[128]; 660 661 move(l, lc); 662#ifdef DEBUG 663 while (w-- > 0) 664 addch('*'); 665 return; 666#endif 667 if (n == 0) { 668 while (w-- > 0) 669 addch(' '); 670 return; 671 } 672 snr = snprintf(b, sizeof(b), "%*d", w, n); 673 if (snr != w) 674 snr = snprintf(b, sizeof(b), "%*dk", w - 1, n / 1000); 675 if (snr != w) 676 snr = snprintf(b, sizeof(b), "%*dM", w - 1, n / 1000000); 677 if (snr != w) { 678 while (w-- > 0) 679 addch('*'); 680 return; 681 } 682 addstr(b); 683} 684 685static void 686putfloat(double f, int l, int lc, int w, int d, int nz) 687{ 688 int snr; 689 char b[128]; 690 691 move(l, lc); 692#ifdef DEBUG 693 while (--w >= 0) 694 addch('*'); 695 return; 696#endif 697 if (nz && f == 0.0) { 698 while (--w >= 0) 699 addch(' '); 700 return; 701 } 702 snr = snprintf(b, sizeof(b), "%*.*f", w, d, f); 703 if (snr != w) 704 snr = snprintf(b, sizeof(b), "%*.0f", w, f); 705 if (snr != w) { 706 while (--w >= 0) 707 addch('*'); 708 return; 709 } 710 addstr(b); 711} 712 713static void 714putlongdouble(long double f, int l, int lc, int w, int d, int nz) 715{ 716 int snr; 717 char b[128]; 718 719 move(l, lc); 720#ifdef DEBUG 721 while (--w >= 0) 722 addch('*'); 723 return; 724#endif 725 if (nz && f == 0.0) { 726 while (--w >= 0) 727 addch(' '); 728 return; 729 } 730 snr = snprintf(b, sizeof(b), "%*.*Lf", w, d, f); 731 if (snr != w) 732 snr = snprintf(b, sizeof(b), "%*.0Lf", w, f); 733 if (snr != w) { 734 while (--w >= 0) 735 addch('*'); 736 return; 737 } 738 addstr(b); 739} 740 741static void 742getinfo(struct Info *ls) 743{ 744 struct devinfo *tmp_dinfo; 745 size_t size; 746 int mib[2]; 747 748 GETSYSCTL("kern.cp_time", ls->time); 749 GETSYSCTL("kern.cp_time", cur.cp_time); 750 GETSYSCTL("vm.stats.sys.v_swtch", ls->v_swtch); 751 GETSYSCTL("vm.stats.sys.v_trap", ls->v_trap); 752 GETSYSCTL("vm.stats.sys.v_syscall", ls->v_syscall); 753 GETSYSCTL("vm.stats.sys.v_intr", ls->v_intr); 754 GETSYSCTL("vm.stats.sys.v_soft", ls->v_soft); 755 GETSYSCTL("vm.stats.vm.v_vm_faults", ls->v_vm_faults); 756 GETSYSCTL("vm.stats.vm.v_cow_faults", ls->v_cow_faults); 757 GETSYSCTL("vm.stats.vm.v_zfod", ls->v_zfod); 758 GETSYSCTL("vm.stats.vm.v_ozfod", ls->v_ozfod); 759 GETSYSCTL("vm.stats.vm.v_swapin", ls->v_swapin); 760 GETSYSCTL("vm.stats.vm.v_swapout", ls->v_swapout); 761 GETSYSCTL("vm.stats.vm.v_swappgsin", ls->v_swappgsin); 762 GETSYSCTL("vm.stats.vm.v_swappgsout", ls->v_swappgsout); 763 GETSYSCTL("vm.stats.vm.v_vnodein", ls->v_vnodein); 764 GETSYSCTL("vm.stats.vm.v_vnodeout", ls->v_vnodeout); 765 GETSYSCTL("vm.stats.vm.v_vnodepgsin", ls->v_vnodepgsin); 766 GETSYSCTL("vm.stats.vm.v_vnodepgsout", ls->v_vnodepgsout); 767 GETSYSCTL("vm.stats.vm.v_intrans", ls->v_intrans); 768 GETSYSCTL("vm.stats.vm.v_reactivated", ls->v_reactivated); 769 GETSYSCTL("vm.stats.vm.v_pdwakeups", ls->v_pdwakeups); 770 GETSYSCTL("vm.stats.vm.v_pdpages", ls->v_pdpages); 771 GETSYSCTL("vm.stats.vm.v_dfree", ls->v_dfree); 772 GETSYSCTL("vm.stats.vm.v_pfree", ls->v_pfree); 773 GETSYSCTL("vm.stats.vm.v_tfree", ls->v_tfree); 774 GETSYSCTL("vm.stats.vm.v_page_size", ls->v_page_size); 775 GETSYSCTL("vm.stats.vm.v_free_count", ls->v_free_count); 776 GETSYSCTL("vm.stats.vm.v_wire_count", ls->v_wire_count); 777 GETSYSCTL("vm.stats.vm.v_active_count", ls->v_active_count); 778 GETSYSCTL("vm.stats.vm.v_inactive_count", ls->v_inactive_count); 779 GETSYSCTL("vm.stats.vm.v_cache_count", ls->v_cache_count); 780 GETSYSCTL("vfs.bufspace", ls->bufspace); 781 GETSYSCTL("kern.maxvnodes", ls->desiredvnodes); 782 GETSYSCTL("vfs.numvnodes", ls->numvnodes); 783 GETSYSCTL("vfs.freevnodes", ls->freevnodes); 784 GETSYSCTL("vfs.cache.nchstats", ls->nchstats); 785 GETSYSCTL("vfs.numdirtybuffers", ls->numdirtybuffers); 786 getsysctl("hw.intrcnt", ls->intrcnt, nintr * sizeof(u_long)); 787 788 size = sizeof(ls->Total); 789 mib[0] = CTL_VM; 790 mib[1] = VM_TOTAL; 791 if (sysctl(mib, 2, &ls->Total, &size, NULL, 0) < 0) { 792 error("Can't get kernel info: %s\n", strerror(errno)); 793 bzero(&ls->Total, sizeof(ls->Total)); 794 } 795 size = sizeof(ncpu); 796 if (sysctlbyname("hw.ncpu", &ncpu, &size, NULL, 0) < 0 || 797 size != sizeof(ncpu)) 798 ncpu = 1; 799 800 tmp_dinfo = last.dinfo; 801 last.dinfo = cur.dinfo; 802 cur.dinfo = tmp_dinfo; 803 804 last.snap_time = cur.snap_time; 805 switch (devstat_getdevs(NULL, &cur)) { 806 case -1: 807 errx(1, "%s", devstat_errbuf); 808 break; 809 case 1: 810 num_devices = cur.dinfo->numdevs; 811 generation = cur.dinfo->generation; 812 cmdkre("refresh", NULL); 813 break; 814 default: 815 break; 816 } 817} 818 819static void 820allocinfo(struct Info *ls) 821{ 822 823 ls->intrcnt = (long *) calloc(nintr, sizeof(long)); 824 if (ls->intrcnt == NULL) 825 errx(2, "out of memory"); 826} 827 828static void 829copyinfo(struct Info *from, struct Info *to) 830{ 831 long *intrcnt; 832 833 /* 834 * time, wds, seek, and xfer are malloc'd so we have to 835 * save the pointers before the structure copy and then 836 * copy by hand. 837 */ 838 intrcnt = to->intrcnt; 839 *to = *from; 840 841 bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int)); 842} 843 844static void 845dinfo(int dn, int lc, struct statinfo *now, struct statinfo *then) 846{ 847 long double transfers_per_second; 848 long double kb_per_transfer, mb_per_second; 849 long double elapsed_time, device_busy; 850 int di; 851 852 di = dev_select[dn].position; 853 854 if (then != NULL) { 855 /* Calculate relative to previous sample */ 856 elapsed_time = now->snap_time - then->snap_time; 857 } else { 858 /* Calculate relative to device creation */ 859 elapsed_time = now->snap_time - devstat_compute_etime( 860 &now->dinfo->devices[di].creation_time, NULL); 861 } 862 863 if (devstat_compute_statistics(&now->dinfo->devices[di], then ? 864 &then->dinfo->devices[di] : NULL, elapsed_time, 865 DSM_KB_PER_TRANSFER, &kb_per_transfer, 866 DSM_TRANSFERS_PER_SECOND, &transfers_per_second, 867 DSM_MB_PER_SECOND, &mb_per_second, 868 DSM_BUSY_PCT, &device_busy, 869 DSM_NONE) != 0) 870 errx(1, "%s", devstat_errbuf); 871 872 lc = DISKCOL + lc * 6; 873 putlongdouble(kb_per_transfer, DISKROW + 1, lc, 5, 2, 0); 874 putlongdouble(transfers_per_second, DISKROW + 2, lc, 5, 0, 0); 875 putlongdouble(mb_per_second, DISKROW + 3, lc, 5, 2, 0); 876 putlongdouble(device_busy, DISKROW + 4, lc, 5, 0, 0); 877}
| 70#include <devstat.h> 71#include "systat.h" 72#include "extern.h" 73#include "devs.h" 74 75static struct Info { 76 long time[CPUSTATES]; 77 u_int v_swtch; /* context switches */ 78 u_int v_trap; /* calls to trap */ 79 u_int v_syscall; /* calls to syscall() */ 80 u_int v_intr; /* device interrupts */ 81 u_int v_soft; /* software interrupts */ 82 /* 83 * Virtual memory activity. 84 */ 85 u_int v_vm_faults; /* number of address memory faults */ 86 u_int v_cow_faults; /* number of copy-on-writes */ 87 u_int v_zfod; /* pages zero filled on demand */ 88 u_int v_ozfod; /* optimized zero fill pages */ 89 u_int v_swapin; /* swap pager pageins */ 90 u_int v_swapout; /* swap pager pageouts */ 91 u_int v_swappgsin; /* swap pager pages paged in */ 92 u_int v_swappgsout; /* swap pager pages paged out */ 93 u_int v_vnodein; /* vnode pager pageins */ 94 u_int v_vnodeout; /* vnode pager pageouts */ 95 u_int v_vnodepgsin; /* vnode_pager pages paged in */ 96 u_int v_vnodepgsout; /* vnode pager pages paged out */ 97 u_int v_intrans; /* intransit blocking page faults */ 98 u_int v_reactivated; /* number of pages reactivated from free list */ 99 u_int v_pdwakeups; /* number of times daemon has awaken from sleep */ 100 u_int v_pdpages; /* number of pages analyzed by daemon */ 101 102 u_int v_dfree; /* pages freed by daemon */ 103 u_int v_pfree; /* pages freed by exiting processes */ 104 u_int v_tfree; /* total pages freed */ 105 /* 106 * Distribution of page usages. 107 */ 108 u_int v_page_size; /* page size in bytes */ 109 u_int v_free_count; /* number of pages free */ 110 u_int v_wire_count; /* number of pages wired down */ 111 u_int v_active_count; /* number of pages active */ 112 u_int v_inactive_count; /* number of pages inactive */ 113 u_int v_cache_count; /* number of pages on buffer cache queue */ 114 struct vmtotal Total; 115 struct nchstats nchstats; 116 long nchcount; 117 long *intrcnt; 118 long bufspace; 119 int desiredvnodes; 120 long numvnodes; 121 long freevnodes; 122 int numdirtybuffers; 123} s, s1, s2, z; 124 125struct statinfo cur, last, run; 126 127#define total s.Total 128#define nchtotal s.nchstats 129#define oldnchtotal s1.nchstats 130 131static enum state { BOOT, TIME, RUN } state = TIME; 132 133static void allocinfo(struct Info *); 134static void copyinfo(struct Info *, struct Info *); 135static float cputime(int); 136static void dinfo(int, int, struct statinfo *, struct statinfo *); 137static void getinfo(struct Info *); 138static void putint(int, int, int, int); 139static void putfloat(double, int, int, int, int, int); 140static void putlongdouble(long double, int, int, int, int, int); 141static int ucount(void); 142 143static int ncpu; 144static char buf[26]; 145static time_t t; 146static double etime; 147static int nintr; 148static long *intrloc; 149static char **intrname; 150static int nextintsrow; 151 152WINDOW * 153openkre(void) 154{ 155 156 return (stdscr); 157} 158 159void 160closekre(WINDOW *w) 161{ 162 163 if (w == NULL) 164 return; 165 wclear(w); 166 wrefresh(w); 167} 168 169/* 170 * These constants define where the major pieces are laid out 171 */ 172#define STATROW 0 /* uses 1 row and 67 cols */ 173#define STATCOL 0 174#define MEMROW 2 /* uses 4 rows and 45 cols */ 175#define MEMCOL 0 176#define PAGEROW 2 /* uses 4 rows and 30 cols */ 177#define PAGECOL 47 178#define INTSROW 6 /* uses all rows to bottom and 16 cols */ 179#define INTSCOL 64 180#define PROCSROW 6 /* uses 3 rows and 19 cols */ 181#define PROCSCOL 0 182#define GENSTATROW 7 /* uses 2 rows and 29 cols */ 183#define GENSTATCOL 21 184#define VMSTATROW 7 /* uses 17 rows and 12-14 cols */ 185#define VMSTATCOL 49 /* actually 50-51 for some fields */ 186#define GRAPHROW 10 /* uses 3 rows and 49-51 cols */ 187#define GRAPHCOL 0 188#define VNSTATROW 13 /* uses 4 rows and 13 columns */ 189#define VNSTATCOL 35 190#define NAMEIROW 14 /* uses 3 rows and 32 cols */ 191#define NAMEICOL 0 192#define DISKROW 18 /* uses 5 rows and 47 cols (for 7 drives) */ 193#define DISKCOL 0 194 195#define DRIVESPACE 7 /* max # for space */ 196 197#define MAXDRIVES DRIVESPACE /* max # to display */ 198 199int 200initkre(void) 201{ 202 char *cp, *cp1, *cp2, *intrnamebuf, *nextcp; 203 int i; 204 size_t sz; 205 206 if ((num_devices = devstat_getnumdevs(NULL)) < 0) { 207 warnx("%s", devstat_errbuf); 208 return(0); 209 } 210 211 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 212 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 213 run.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 214 bzero(cur.dinfo, sizeof(struct devinfo)); 215 bzero(last.dinfo, sizeof(struct devinfo)); 216 bzero(run.dinfo, sizeof(struct devinfo)); 217 218 if (dsinit(MAXDRIVES, &cur, &last, &run) != 1) 219 return(0); 220 221 if (nintr == 0) { 222 if (sysctlbyname("hw.intrcnt", NULL, &sz, NULL, 0) == -1) { 223 error("sysctl(hw.intrcnt...) failed: %s", 224 strerror(errno)); 225 return (0); 226 } 227 nintr = sz / sizeof(u_long); 228 intrloc = calloc(nintr, sizeof (long)); 229 intrname = calloc(nintr, sizeof (char *)); 230 intrnamebuf = sysctl_dynread("hw.intrnames", NULL); 231 if (intrnamebuf == NULL || intrname == NULL || 232 intrloc == NULL) { 233 error("Out of memory"); 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 for (cp = intrnamebuf, i = 0; i < nintr; i++) { 244 nextcp = cp + strlen(cp) + 1; 245 246 /* Discard trailing spaces. */ 247 for (cp1 = nextcp - 1; cp1 > cp && *(cp1 - 1) == ' '; ) 248 *--cp1 = '\0'; 249 250 /* Convert "irqN: name" to "name irqN". */ 251 if (strncmp(cp, "irq", 3) == 0) { 252 cp1 = cp + 3; 253 while (isdigit((u_char)*cp1)) 254 cp1++; 255 if (cp1 != cp && *cp1 == ':' && 256 *(cp1 + 1) == ' ') { 257 *cp1 = '\0'; 258 cp1 = cp1 + 2; 259 cp2 = strdup(cp); 260 bcopy(cp1, cp, strlen(cp1) + 1); 261 strcat(cp, " "); 262 strcat(cp, cp2); 263 free(cp2); 264 } 265 } 266 267 /* 268 * Convert "name irqN" to "name N" if the former is 269 * longer than the field width. 270 */ 271 if ((cp1 = strstr(cp, "irq")) != NULL && 272 strlen(cp) > 10) 273 bcopy(cp1 + 3, cp1, strlen(cp1 + 3) + 1); 274 275 intrname[i] = cp; 276 cp = nextcp; 277 } 278 nextintsrow = INTSROW + 2; 279 allocinfo(&s); 280 allocinfo(&s1); 281 allocinfo(&s2); 282 allocinfo(&z); 283 } 284 getinfo(&s2); 285 copyinfo(&s2, &s1); 286 return(1); 287} 288 289void 290fetchkre(void) 291{ 292 time_t now; 293 struct tm *tp; 294 static int d_first = -1; 295 296 if (d_first < 0) 297 d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 298 299 time(&now); 300 tp = localtime(&now); 301 (void) strftime(buf, sizeof(buf), 302 d_first ? "%e %b %R" : "%b %e %R", tp); 303 getinfo(&s); 304} 305 306void 307labelkre(void) 308{ 309 int i, j; 310 311 clear(); 312 mvprintw(STATROW, STATCOL + 6, "users Load"); 313 mvprintw(MEMROW, MEMCOL, "Mem:KB REAL VIRTUAL"); 314 mvprintw(MEMROW + 1, MEMCOL, " Tot Share Tot Share"); 315 mvprintw(MEMROW + 2, MEMCOL, "Act"); 316 mvprintw(MEMROW + 3, MEMCOL, "All"); 317 318 mvprintw(MEMROW + 1, MEMCOL + 41, "Free"); 319 320 mvprintw(PAGEROW, PAGECOL, " VN PAGER SWAP PAGER"); 321 mvprintw(PAGEROW + 1, PAGECOL, " in out in out"); 322 mvprintw(PAGEROW + 2, PAGECOL, "count"); 323 mvprintw(PAGEROW + 3, PAGECOL, "pages"); 324 325 mvprintw(INTSROW, INTSCOL + 1, "Interrupts"); 326 mvprintw(INTSROW + 1, INTSCOL + 6, "total"); 327 328 mvprintw(VMSTATROW, VMSTATCOL + 9, "cow"); 329 mvprintw(VMSTATROW + 1, VMSTATCOL + 9, "zfod"); 330 mvprintw(VMSTATROW + 2, VMSTATCOL + 9, "ozfod"); 331 mvprintw(VMSTATROW + 3, VMSTATCOL + 9 - 1, "%%ozfod"); 332 mvprintw(VMSTATROW + 4, VMSTATCOL + 9, "daefr"); 333 mvprintw(VMSTATROW + 5, VMSTATCOL + 9, "prcfr"); 334 mvprintw(VMSTATROW + 6, VMSTATCOL + 9, "totfr"); 335 mvprintw(VMSTATROW + 7, VMSTATCOL + 9, "react"); 336 mvprintw(VMSTATROW + 8, VMSTATCOL + 9, "pdwak"); 337 mvprintw(VMSTATROW + 9, VMSTATCOL + 9, "pdpgs"); 338 mvprintw(VMSTATROW + 10, VMSTATCOL + 9, "intrn"); 339 mvprintw(VMSTATROW + 11, VMSTATCOL + 9, "wire"); 340 mvprintw(VMSTATROW + 12, VMSTATCOL + 9, "act"); 341 mvprintw(VMSTATROW + 13, VMSTATCOL + 9, "inact"); 342 mvprintw(VMSTATROW + 14, VMSTATCOL + 9, "cache"); 343 mvprintw(VMSTATROW + 15, VMSTATCOL + 9, "free"); 344 if (LINES - 1 > VMSTATROW + 16) 345 mvprintw(VMSTATROW + 16, VMSTATCOL + 9, "buf"); 346 347 mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt"); 348 349 mvprintw(GRAPHROW, GRAPHCOL, 350 " . %%Sys . %%Intr . %%User . %%Nice . %%Idle"); 351 mvprintw(PROCSROW, PROCSCOL, "Proc:"); 352 mvprintw(PROCSROW + 1, PROCSCOL, " r p d s w"); 353 mvprintw(GRAPHROW + 1, GRAPHCOL, 354 "| | | | | | | | | | |"); 355 356 mvprintw(VNSTATROW, VNSTATCOL + 8, "dtbuf"); 357 mvprintw(VNSTATROW + 1, VNSTATCOL + 8, "desvn"); 358 mvprintw(VNSTATROW + 2, VNSTATCOL + 8, "numvn"); 359 mvprintw(VNSTATROW + 3, VNSTATCOL + 8, "frevn"); 360 361 mvprintw(NAMEIROW, NAMEICOL, "Namei Name-cache Dir-cache"); 362 mvprintw(NAMEIROW + 1, NAMEICOL, 363 " Calls hits %% hits %%"); 364 mvprintw(DISKROW, DISKCOL, "Disks"); 365 mvprintw(DISKROW + 1, DISKCOL, "KB/t"); 366 mvprintw(DISKROW + 2, DISKCOL, "tps"); 367 mvprintw(DISKROW + 3, DISKCOL, "MB/s"); 368 mvprintw(DISKROW + 4, DISKCOL, "%%busy"); 369 /* 370 * For now, we don't support a fourth disk statistic. So there's 371 * no point in providing a label for it. If someone can think of a 372 * fourth useful disk statistic, there is room to add it. 373 */ 374 /* mvprintw(DISKROW + 4, DISKCOL, " msps"); */ 375 j = 0; 376 for (i = 0; i < num_devices && j < MAXDRIVES; i++) 377 if (dev_select[i].selected) { 378 char tmpstr[80]; 379 sprintf(tmpstr, "%s%d", dev_select[i].device_name, 380 dev_select[i].unit_number); 381 mvprintw(DISKROW, DISKCOL + 5 + 6 * j, 382 " %5.5s", tmpstr); 383 j++; 384 } 385 386 for (i = 0; i < nintr; i++) { 387 if (intrloc[i] == 0) 388 continue; 389 mvprintw(intrloc[i], INTSCOL + 6, "%-10.10s", intrname[i]); 390 } 391} 392 393#define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;} 394#define Q(fld) {t=cur.fld[i]; cur.fld[i]-=last.fld[i]; if(state==TIME) last.fld[i]=t;} 395#define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;} 396#define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \ 397 if(state == TIME) s1.nchstats.fld = t;} 398#define PUTRATE(fld, l, c, w) \ 399do { \ 400 Y(fld); \ 401 putint((int)((float)s.fld/etime + 0.5), l, c, w); \ 402} while (0) 403#define MAXFAIL 5 404 405static char cpuchar[CPUSTATES] = { '=' , '+', '>', '-', ' ' }; 406static char cpuorder[CPUSTATES] = { CP_SYS, CP_INTR, CP_USER, CP_NICE, 407 CP_IDLE }; 408 409void 410showkre(void) 411{ 412 float f1, f2; 413 int psiz, inttotal; 414 int i, l, lc; 415 static int failcnt = 0; 416 417 etime = 0; 418 for(i = 0; i < CPUSTATES; i++) { 419 X(time); 420 Q(cp_time); 421 etime += s.time[i]; 422 } 423 if (etime < 5.0) { /* < 5 ticks - ignore this trash */ 424 if (failcnt++ >= MAXFAIL) { 425 clear(); 426 mvprintw(2, 10, "The alternate system clock has died!"); 427 mvprintw(3, 10, "Reverting to ``pigs'' display."); 428 move(CMDLINE, 0); 429 refresh(); 430 failcnt = 0; 431 sleep(5); 432 command("pigs"); 433 } 434 return; 435 } 436 failcnt = 0; 437 etime /= hertz; 438 etime /= ncpu; 439 inttotal = 0; 440 for (i = 0; i < nintr; i++) { 441 if (s.intrcnt[i] == 0) 442 continue; 443 if (intrloc[i] == 0) { 444 if (nextintsrow == LINES) 445 continue; 446 intrloc[i] = nextintsrow++; 447 mvprintw(intrloc[i], INTSCOL + 6, "%-10.10s", 448 intrname[i]); 449 } 450 X(intrcnt); 451 l = (int)((float)s.intrcnt[i]/etime + 0.5); 452 inttotal += l; 453 putint(l, intrloc[i], INTSCOL, 5); 454 } 455 putint(inttotal, INTSROW + 1, INTSCOL, 5); 456 Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss); 457 Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes); Z(ncs_neghits); 458 s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits + 459 nchtotal.ncs_miss + nchtotal.ncs_long + nchtotal.ncs_neghits; 460 if (state == TIME) 461 s1.nchcount = s.nchcount; 462 463 psiz = 0; 464 f2 = 0.0; 465 for (lc = 0; lc < CPUSTATES; lc++) { 466 i = cpuorder[lc]; 467 f1 = cputime(i); 468 f2 += f1; 469 l = (int) ((f2 + 1.0) / 2.0) - psiz; 470 putfloat(f1, GRAPHROW, GRAPHCOL + 10 * lc, 4, 1, 0); 471 move(GRAPHROW + 2, psiz); 472 psiz += l; 473 while (l-- > 0) 474 addch(cpuchar[lc]); 475 } 476 477 putint(ucount(), STATROW, STATCOL, 5); 478 putfloat(avenrun[0], STATROW, STATCOL + 20, 5, 2, 0); 479 putfloat(avenrun[1], STATROW, STATCOL + 26, 5, 2, 0); 480 putfloat(avenrun[2], STATROW, STATCOL + 32, 5, 2, 0); 481 mvaddstr(STATROW, STATCOL + 55, buf); 482#define pgtokb(pg) ((pg) * (s.v_page_size / 1024)) 483 putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 4, 7); 484 putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 12, 7); 485 putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 20, 8); 486 putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 29, 8); 487 putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 4, 7); 488 putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 12, 7); 489 putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 20, 8); 490 putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 29, 8); 491 putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 38, 7); 492 putint(total.t_rq - 1, PROCSROW + 2, PROCSCOL, 3); 493 putint(total.t_pw, PROCSROW + 2, PROCSCOL + 4, 3); 494 putint(total.t_dw, PROCSROW + 2, PROCSCOL + 8, 3); 495 putint(total.t_sl, PROCSROW + 2, PROCSCOL + 12, 3); 496 putint(total.t_sw, PROCSROW + 2, PROCSCOL + 16, 3); 497 PUTRATE(v_cow_faults, VMSTATROW, VMSTATCOL + 2, 8 - 2); 498 PUTRATE(v_zfod, VMSTATROW + 1, VMSTATCOL + 2, 8 - 2); 499 PUTRATE(v_ozfod, VMSTATROW + 2, VMSTATCOL, 8); 500 putint(s.v_zfod != 0 ? (int)(s.v_ozfod * 100.0 / s.v_zfod) : 0, 501 VMSTATROW + 3, VMSTATCOL + 1, 8 - 1); 502 PUTRATE(v_dfree, VMSTATROW + 4, VMSTATCOL + 2, 8 - 2); 503 PUTRATE(v_pfree, VMSTATROW + 5, VMSTATCOL + 2, 8 - 2); 504 PUTRATE(v_tfree, VMSTATROW + 6, VMSTATCOL, 8); 505 PUTRATE(v_reactivated, VMSTATROW + 7, VMSTATCOL, 8); 506 PUTRATE(v_pdwakeups, VMSTATROW + 8, VMSTATCOL, 8); 507 PUTRATE(v_pdpages, VMSTATROW + 9, VMSTATCOL, 8); 508 PUTRATE(v_intrans, VMSTATROW + 10, VMSTATCOL, 8); 509 putint(pgtokb(s.v_wire_count), VMSTATROW + 11, VMSTATCOL, 8); 510 putint(pgtokb(s.v_active_count), VMSTATROW + 12, VMSTATCOL, 8); 511 putint(pgtokb(s.v_inactive_count), VMSTATROW + 13, VMSTATCOL, 8); 512 putint(pgtokb(s.v_cache_count), VMSTATROW + 14, VMSTATCOL, 8); 513 putint(pgtokb(s.v_free_count), VMSTATROW + 15, VMSTATCOL, 8); 514 if (LINES - 1 > VMSTATROW + 16) 515 putint(s.bufspace / 1024, VMSTATROW + 16, VMSTATCOL, 8); 516 PUTRATE(v_vnodein, PAGEROW + 2, PAGECOL + 6, 5); 517 PUTRATE(v_vnodeout, PAGEROW + 2, PAGECOL + 12, 5); 518 PUTRATE(v_swapin, PAGEROW + 2, PAGECOL + 19, 5); 519 PUTRATE(v_swapout, PAGEROW + 2, PAGECOL + 25, 5); 520 PUTRATE(v_vnodepgsin, PAGEROW + 3, PAGECOL + 6, 5); 521 PUTRATE(v_vnodepgsout, PAGEROW + 3, PAGECOL + 12, 5); 522 PUTRATE(v_swappgsin, PAGEROW + 3, PAGECOL + 19, 5); 523 PUTRATE(v_swappgsout, PAGEROW + 3, PAGECOL + 25, 5); 524 PUTRATE(v_swtch, GENSTATROW + 1, GENSTATCOL, 4); 525 PUTRATE(v_trap, GENSTATROW + 1, GENSTATCOL + 5, 4); 526 PUTRATE(v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 4); 527 PUTRATE(v_intr, GENSTATROW + 1, GENSTATCOL + 15, 4); 528 PUTRATE(v_soft, GENSTATROW + 1, GENSTATCOL + 20, 4); 529 PUTRATE(v_vm_faults, GENSTATROW + 1, GENSTATCOL + 25, 4); 530 for (i = 0, lc = 0; i < num_devices && lc < MAXDRIVES; i++) 531 if (dev_select[i].selected) { 532 switch(state) { 533 case TIME: 534 dinfo(i, ++lc, &cur, &last); 535 break; 536 case RUN: 537 dinfo(i, ++lc, &cur, &run); 538 break; 539 case BOOT: 540 dinfo(i, ++lc, &cur, NULL); 541 break; 542 } 543 } 544 putint(s.numdirtybuffers, VNSTATROW, VNSTATCOL, 7); 545 putint(s.desiredvnodes, VNSTATROW + 1, VNSTATCOL, 7); 546 putint(s.numvnodes, VNSTATROW + 2, VNSTATCOL, 7); 547 putint(s.freevnodes, VNSTATROW + 3, VNSTATCOL, 7); 548 putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 8); 549 putint((nchtotal.ncs_goodhits + nchtotal.ncs_neghits), 550 NAMEIROW + 2, NAMEICOL + 9, 7); 551#define nz(x) ((x) ? (x) : 1) 552 putfloat((nchtotal.ncs_goodhits+nchtotal.ncs_neghits) * 553 100.0 / nz(s.nchcount), 554 NAMEIROW + 2, NAMEICOL + 17, 3, 0, 1); 555 putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 21, 7); 556 putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount), 557 NAMEIROW + 2, NAMEICOL + 29, 3, 0, 1); 558#undef nz 559} 560 561int 562cmdkre(const char *cmd, const char *args) 563{ 564 int retval; 565 566 if (prefix(cmd, "run")) { 567 retval = 1; 568 copyinfo(&s2, &s1); 569 switch (devstat_getdevs(NULL, &run)) { 570 case -1: 571 errx(1, "%s", devstat_errbuf); 572 break; 573 case 1: 574 num_devices = run.dinfo->numdevs; 575 generation = run.dinfo->generation; 576 retval = dscmd("refresh", NULL, MAXDRIVES, &cur); 577 if (retval == 2) 578 labelkre(); 579 break; 580 default: 581 break; 582 } 583 state = RUN; 584 return (retval); 585 } 586 if (prefix(cmd, "boot")) { 587 state = BOOT; 588 copyinfo(&z, &s1); 589 return (1); 590 } 591 if (prefix(cmd, "time")) { 592 state = TIME; 593 return (1); 594 } 595 if (prefix(cmd, "zero")) { 596 retval = 1; 597 if (state == RUN) { 598 getinfo(&s1); 599 switch (devstat_getdevs(NULL, &run)) { 600 case -1: 601 errx(1, "%s", devstat_errbuf); 602 break; 603 case 1: 604 num_devices = run.dinfo->numdevs; 605 generation = run.dinfo->generation; 606 retval = dscmd("refresh",NULL, MAXDRIVES, &cur); 607 if (retval == 2) 608 labelkre(); 609 break; 610 default: 611 break; 612 } 613 } 614 return (retval); 615 } 616 retval = dscmd(cmd, args, MAXDRIVES, &cur); 617 618 if (retval == 2) 619 labelkre(); 620 621 return(retval); 622} 623 624/* calculate number of users on the system */ 625static int 626ucount(void) 627{ 628 int nusers = 0; 629 struct utmpx *ut; 630 631 setutxent(); 632 while ((ut = getutxent()) != NULL) 633 if (ut->ut_type == USER_PROCESS) 634 nusers++; 635 endutxent(); 636 637 return (nusers); 638} 639 640static float 641cputime(int indx) 642{ 643 double lt; 644 int i; 645 646 lt = 0; 647 for (i = 0; i < CPUSTATES; i++) 648 lt += s.time[i]; 649 if (lt == 0.0) 650 lt = 1.0; 651 return (s.time[indx] * 100.0 / lt); 652} 653 654static void 655putint(int n, int l, int lc, int w) 656{ 657 int snr; 658 char b[128]; 659 660 move(l, lc); 661#ifdef DEBUG 662 while (w-- > 0) 663 addch('*'); 664 return; 665#endif 666 if (n == 0) { 667 while (w-- > 0) 668 addch(' '); 669 return; 670 } 671 snr = snprintf(b, sizeof(b), "%*d", w, n); 672 if (snr != w) 673 snr = snprintf(b, sizeof(b), "%*dk", w - 1, n / 1000); 674 if (snr != w) 675 snr = snprintf(b, sizeof(b), "%*dM", w - 1, n / 1000000); 676 if (snr != w) { 677 while (w-- > 0) 678 addch('*'); 679 return; 680 } 681 addstr(b); 682} 683 684static void 685putfloat(double f, int l, int lc, int w, int d, int nz) 686{ 687 int snr; 688 char b[128]; 689 690 move(l, lc); 691#ifdef DEBUG 692 while (--w >= 0) 693 addch('*'); 694 return; 695#endif 696 if (nz && f == 0.0) { 697 while (--w >= 0) 698 addch(' '); 699 return; 700 } 701 snr = snprintf(b, sizeof(b), "%*.*f", w, d, f); 702 if (snr != w) 703 snr = snprintf(b, sizeof(b), "%*.0f", w, f); 704 if (snr != w) { 705 while (--w >= 0) 706 addch('*'); 707 return; 708 } 709 addstr(b); 710} 711 712static void 713putlongdouble(long double f, int l, int lc, int w, int d, int nz) 714{ 715 int snr; 716 char b[128]; 717 718 move(l, lc); 719#ifdef DEBUG 720 while (--w >= 0) 721 addch('*'); 722 return; 723#endif 724 if (nz && f == 0.0) { 725 while (--w >= 0) 726 addch(' '); 727 return; 728 } 729 snr = snprintf(b, sizeof(b), "%*.*Lf", w, d, f); 730 if (snr != w) 731 snr = snprintf(b, sizeof(b), "%*.0Lf", w, f); 732 if (snr != w) { 733 while (--w >= 0) 734 addch('*'); 735 return; 736 } 737 addstr(b); 738} 739 740static void 741getinfo(struct Info *ls) 742{ 743 struct devinfo *tmp_dinfo; 744 size_t size; 745 int mib[2]; 746 747 GETSYSCTL("kern.cp_time", ls->time); 748 GETSYSCTL("kern.cp_time", cur.cp_time); 749 GETSYSCTL("vm.stats.sys.v_swtch", ls->v_swtch); 750 GETSYSCTL("vm.stats.sys.v_trap", ls->v_trap); 751 GETSYSCTL("vm.stats.sys.v_syscall", ls->v_syscall); 752 GETSYSCTL("vm.stats.sys.v_intr", ls->v_intr); 753 GETSYSCTL("vm.stats.sys.v_soft", ls->v_soft); 754 GETSYSCTL("vm.stats.vm.v_vm_faults", ls->v_vm_faults); 755 GETSYSCTL("vm.stats.vm.v_cow_faults", ls->v_cow_faults); 756 GETSYSCTL("vm.stats.vm.v_zfod", ls->v_zfod); 757 GETSYSCTL("vm.stats.vm.v_ozfod", ls->v_ozfod); 758 GETSYSCTL("vm.stats.vm.v_swapin", ls->v_swapin); 759 GETSYSCTL("vm.stats.vm.v_swapout", ls->v_swapout); 760 GETSYSCTL("vm.stats.vm.v_swappgsin", ls->v_swappgsin); 761 GETSYSCTL("vm.stats.vm.v_swappgsout", ls->v_swappgsout); 762 GETSYSCTL("vm.stats.vm.v_vnodein", ls->v_vnodein); 763 GETSYSCTL("vm.stats.vm.v_vnodeout", ls->v_vnodeout); 764 GETSYSCTL("vm.stats.vm.v_vnodepgsin", ls->v_vnodepgsin); 765 GETSYSCTL("vm.stats.vm.v_vnodepgsout", ls->v_vnodepgsout); 766 GETSYSCTL("vm.stats.vm.v_intrans", ls->v_intrans); 767 GETSYSCTL("vm.stats.vm.v_reactivated", ls->v_reactivated); 768 GETSYSCTL("vm.stats.vm.v_pdwakeups", ls->v_pdwakeups); 769 GETSYSCTL("vm.stats.vm.v_pdpages", ls->v_pdpages); 770 GETSYSCTL("vm.stats.vm.v_dfree", ls->v_dfree); 771 GETSYSCTL("vm.stats.vm.v_pfree", ls->v_pfree); 772 GETSYSCTL("vm.stats.vm.v_tfree", ls->v_tfree); 773 GETSYSCTL("vm.stats.vm.v_page_size", ls->v_page_size); 774 GETSYSCTL("vm.stats.vm.v_free_count", ls->v_free_count); 775 GETSYSCTL("vm.stats.vm.v_wire_count", ls->v_wire_count); 776 GETSYSCTL("vm.stats.vm.v_active_count", ls->v_active_count); 777 GETSYSCTL("vm.stats.vm.v_inactive_count", ls->v_inactive_count); 778 GETSYSCTL("vm.stats.vm.v_cache_count", ls->v_cache_count); 779 GETSYSCTL("vfs.bufspace", ls->bufspace); 780 GETSYSCTL("kern.maxvnodes", ls->desiredvnodes); 781 GETSYSCTL("vfs.numvnodes", ls->numvnodes); 782 GETSYSCTL("vfs.freevnodes", ls->freevnodes); 783 GETSYSCTL("vfs.cache.nchstats", ls->nchstats); 784 GETSYSCTL("vfs.numdirtybuffers", ls->numdirtybuffers); 785 getsysctl("hw.intrcnt", ls->intrcnt, nintr * sizeof(u_long)); 786 787 size = sizeof(ls->Total); 788 mib[0] = CTL_VM; 789 mib[1] = VM_TOTAL; 790 if (sysctl(mib, 2, &ls->Total, &size, NULL, 0) < 0) { 791 error("Can't get kernel info: %s\n", strerror(errno)); 792 bzero(&ls->Total, sizeof(ls->Total)); 793 } 794 size = sizeof(ncpu); 795 if (sysctlbyname("hw.ncpu", &ncpu, &size, NULL, 0) < 0 || 796 size != sizeof(ncpu)) 797 ncpu = 1; 798 799 tmp_dinfo = last.dinfo; 800 last.dinfo = cur.dinfo; 801 cur.dinfo = tmp_dinfo; 802 803 last.snap_time = cur.snap_time; 804 switch (devstat_getdevs(NULL, &cur)) { 805 case -1: 806 errx(1, "%s", devstat_errbuf); 807 break; 808 case 1: 809 num_devices = cur.dinfo->numdevs; 810 generation = cur.dinfo->generation; 811 cmdkre("refresh", NULL); 812 break; 813 default: 814 break; 815 } 816} 817 818static void 819allocinfo(struct Info *ls) 820{ 821 822 ls->intrcnt = (long *) calloc(nintr, sizeof(long)); 823 if (ls->intrcnt == NULL) 824 errx(2, "out of memory"); 825} 826 827static void 828copyinfo(struct Info *from, struct Info *to) 829{ 830 long *intrcnt; 831 832 /* 833 * time, wds, seek, and xfer are malloc'd so we have to 834 * save the pointers before the structure copy and then 835 * copy by hand. 836 */ 837 intrcnt = to->intrcnt; 838 *to = *from; 839 840 bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int)); 841} 842 843static void 844dinfo(int dn, int lc, struct statinfo *now, struct statinfo *then) 845{ 846 long double transfers_per_second; 847 long double kb_per_transfer, mb_per_second; 848 long double elapsed_time, device_busy; 849 int di; 850 851 di = dev_select[dn].position; 852 853 if (then != NULL) { 854 /* Calculate relative to previous sample */ 855 elapsed_time = now->snap_time - then->snap_time; 856 } else { 857 /* Calculate relative to device creation */ 858 elapsed_time = now->snap_time - devstat_compute_etime( 859 &now->dinfo->devices[di].creation_time, NULL); 860 } 861 862 if (devstat_compute_statistics(&now->dinfo->devices[di], then ? 863 &then->dinfo->devices[di] : NULL, elapsed_time, 864 DSM_KB_PER_TRANSFER, &kb_per_transfer, 865 DSM_TRANSFERS_PER_SECOND, &transfers_per_second, 866 DSM_MB_PER_SECOND, &mb_per_second, 867 DSM_BUSY_PCT, &device_busy, 868 DSM_NONE) != 0) 869 errx(1, "%s", devstat_errbuf); 870 871 lc = DISKCOL + lc * 6; 872 putlongdouble(kb_per_transfer, DISKROW + 1, lc, 5, 2, 0); 873 putlongdouble(transfers_per_second, DISKROW + 2, lc, 5, 0, 0); 874 putlongdouble(mb_per_second, DISKROW + 3, lc, 5, 2, 0); 875 putlongdouble(device_busy, DISKROW + 4, lc, 5, 0, 0); 876}
|