1 /*- 2 * Copyright (c) 1983, 1989, 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 /* 31 * Cursed vmstat -- from Robert Elz. 32 */ 33 34 #include <sys/user.h> 35 #include <sys/param.h> 36 #include <sys/stat.h> 37 #include <sys/time.h> 38 #include <sys/uio.h> 39 #include <sys/namei.h> 40 #include <sys/sysctl.h> 41 #include <sys/vmmeter.h> 42 43 #include <vm/vm_param.h> 44 45 #include <ctype.h> 46 #include <err.h> 47 #include <errno.h> 48 #include <kinfo.h> 49 #include <langinfo.h> 50 #include <nlist.h> 51 #include <paths.h> 52 #include <signal.h> 53 #include <stddef.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <time.h> 57 #include <unistd.h> 58 #include "utmpentry.h" 59 #include <devstat.h> 60 #include "systat.h" 61 #include "extern.h" 62 #include "devs.h" 63 64 static struct Info { 65 struct kinfo_cputime cp_time; 66 struct vmmeter Vmm; 67 struct vmtotal Total; 68 struct vmstats Vms; 69 struct nchstats nchstats; 70 long nchcount; 71 long nchpathcount; 72 long *intrcnt; 73 long bufspace; 74 int desiredvnodes; 75 int cachedvnodes; 76 int inactivevnodes; 77 int activevnodes; 78 long dirtybufspace; 79 } s, s1, s2, z; 80 81 struct kinfo_cputime cp_time, old_cp_time; 82 struct statinfo cur, last, run; 83 84 #define vmm s.Vmm 85 #define vms s.Vms 86 #define oldvmm s1.Vmm 87 #define oldvms s1.Vms 88 #define total s.Total 89 #define nchtotal s.nchstats 90 #define oldnchtotal s1.nchstats 91 92 static enum state { BOOT, TIME, RUN } state = TIME; 93 94 static void allocinfo(struct Info *); 95 static void copyinfo(struct Info *, struct Info *); 96 static void dinfo(int, int, struct statinfo *, struct statinfo *); 97 static void getinfo(struct Info *); 98 static void put64(int64_t, int, int, int, int); 99 static void putfloat(double, int, int, int, int, int); 100 static void putlongdouble(long double, int, int, int, int, int); 101 static void putlongdoublez(long double, int, int, int, int, int); 102 static int ucount(void); 103 104 static int ncpu; 105 static char buf[26]; 106 static time_t t; 107 static double etime; 108 static int nintr; 109 static long *intrloc; 110 static char **intrname; 111 static int nextintsrow; 112 static int extended_vm_stats; 113 114 115 116 WINDOW * 117 openkre(void) 118 { 119 120 return (stdscr); 121 } 122 123 void 124 closekre(WINDOW *w) 125 { 126 127 if (w == NULL) 128 return; 129 wclear(w); 130 wrefresh(w); 131 } 132 133 134 static struct nlist namelist[] = { 135 #define X_BUFFERSPACE 0 136 { .n_name = "_bufspace" }, 137 #define X_NCHSTATS 1 138 { .n_name = "_nchstats" }, 139 #define X_DESIREDVNODES 2 140 { .n_name = "_desiredvnodes" }, 141 #define X_CACHEDVNODES 3 142 { .n_name = "_cachedvnodes" }, 143 #define X_INACTIVEVNODES 4 144 { .n_name = "_inactivevnodes" }, 145 #define X_ACTIVEVNODES 5 146 { .n_name = "_activevnodes" }, 147 #define X_NUMDIRTYBUFFERS 6 148 { .n_name = "_dirtybufspace" }, 149 { .n_name = "" }, 150 }; 151 152 /* 153 * These constants define where the major pieces are laid out 154 */ 155 #define STATROW 0 /* uses 1 row and 68 cols */ 156 #define STATCOL 2 157 #define MEMROW 2 /* uses 4 rows and 31 cols */ 158 #define MEMCOL 0 159 #define PAGEROW 2 /* uses 4 rows and 26 cols */ 160 #define PAGECOL 46 161 #define INTSROW 6 /* uses all rows to bottom and 17 cols */ 162 #define INTSCOL 61 163 #define PROCSROW 7 /* uses 2 rows and 20 cols */ 164 #define PROCSCOL 0 165 #define GENSTATROW 7 /* uses 2 rows and 30 cols */ 166 #define GENSTATCOL 16 167 #define VMSTATROW 6 /* uses 17 rows and 12 cols */ 168 #define VMSTATCOL 50 169 #define GRAPHROW 10 /* uses 3 rows and 51 cols */ 170 #define GRAPHCOL 0 171 #define NAMEIROW 14 /* uses 3 rows and 38 cols */ 172 #define NAMEICOL 0 173 #define DISKROW 17 /* uses 6 rows and 50 cols (for 9 drives) */ 174 #define DISKCOL 0 175 176 #define DRIVESPACE 7 /* max # for space */ 177 178 #define MAXDRIVES DRIVESPACE /* max # to display */ 179 180 int 181 initkre(void) 182 { 183 char *intrnamebuf; 184 size_t bytes; 185 size_t b; 186 size_t i; 187 188 if (namelist[0].n_type == 0) { 189 if (kvm_nlist(kd, namelist)) { 190 nlisterr(namelist); 191 return(0); 192 } 193 if (namelist[0].n_type == 0) { 194 error("No namelist"); 195 return(0); 196 } 197 } 198 199 if ((num_devices = getnumdevs()) < 0) { 200 warnx("%s", devstat_errbuf); 201 return(0); 202 } 203 204 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 205 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 206 run.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 207 bzero(cur.dinfo, sizeof(struct devinfo)); 208 bzero(last.dinfo, sizeof(struct devinfo)); 209 bzero(run.dinfo, sizeof(struct devinfo)); 210 211 if (dsinit(MAXDRIVES, &cur, &last, &run) != 1) 212 return(0); 213 214 if (nintr == 0) { 215 if (sysctlbyname("hw.intrnames", NULL, &bytes, NULL, 0) == 0) { 216 intrnamebuf = malloc(bytes); 217 sysctlbyname("hw.intrnames", intrnamebuf, &bytes, 218 NULL, 0); 219 for (i = 0; i < bytes; ++i) { 220 if (intrnamebuf[i] == 0) 221 ++nintr; 222 } 223 intrname = malloc(nintr * sizeof(char *)); 224 intrloc = malloc(nintr * sizeof(*intrloc)); 225 nintr = 0; 226 for (b = i = 0; i < bytes; ++i) { 227 if (intrnamebuf[i] == 0) { 228 intrname[nintr] = intrnamebuf + b; 229 intrloc[nintr] = 0; 230 b = i + 1; 231 ++nintr; 232 } 233 } 234 } 235 nextintsrow = INTSROW + 2; 236 allocinfo(&s); 237 allocinfo(&s1); 238 allocinfo(&s2); 239 allocinfo(&z); 240 } 241 getinfo(&s2); 242 copyinfo(&s2, &s1); 243 return(1); 244 } 245 246 void 247 fetchkre(void) 248 { 249 time_t now; 250 struct tm *tp; 251 static int d_first = -1; 252 253 if (d_first < 0) 254 d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 255 256 time(&now); 257 tp = localtime(&now); 258 (void) strftime(buf, sizeof(buf), 259 d_first ? "%e %b %R" : "%b %e %R", tp); 260 getinfo(&s); 261 } 262 263 void 264 labelkre(void) 265 { 266 int i, j; 267 268 clear(); 269 mvprintw(STATROW, STATCOL + 4, "users Load"); 270 mvprintw(MEMROW, MEMCOL, "Mem: REAL VIRTUAL"); 271 mvprintw(MEMROW + 1, MEMCOL, " Tot Share Tot Share"); 272 mvprintw(MEMROW + 2, MEMCOL, "Act"); 273 mvprintw(MEMROW + 3, MEMCOL, "All"); 274 275 mvprintw(MEMROW + 1, MEMCOL + 36, "Free"); 276 277 mvprintw(PAGEROW, PAGECOL, " VN PAGER SWAP PAGER "); 278 mvprintw(PAGEROW + 1, PAGECOL, " in out in out "); 279 mvprintw(PAGEROW + 2, PAGECOL, "count"); 280 mvprintw(PAGEROW + 3, PAGECOL, "pages"); 281 282 mvprintw(INTSROW, INTSCOL + 3, " Interrupts"); 283 mvprintw(INTSROW + 1, INTSCOL + 9, "total"); 284 285 mvprintw(VMSTATROW + 1, VMSTATCOL + 8, "cow"); 286 mvprintw(VMSTATROW + 2, VMSTATCOL + 8, "wire"); 287 mvprintw(VMSTATROW + 3, VMSTATCOL + 8, "act"); 288 mvprintw(VMSTATROW + 4, VMSTATCOL + 8, "inact"); 289 mvprintw(VMSTATROW + 5, VMSTATCOL + 8, "cache"); 290 mvprintw(VMSTATROW + 6, VMSTATCOL + 8, "free"); 291 mvprintw(VMSTATROW + 7, VMSTATCOL + 8, "daefr"); 292 mvprintw(VMSTATROW + 8, VMSTATCOL + 8, "prcfr"); 293 mvprintw(VMSTATROW + 9, VMSTATCOL + 8, "react"); 294 mvprintw(VMSTATROW + 10, VMSTATCOL + 8, "pdwake"); 295 mvprintw(VMSTATROW + 11, VMSTATCOL + 8, "pdpgs"); 296 mvprintw(VMSTATROW + 12, VMSTATCOL + 8, "intrn"); 297 mvprintw(VMSTATROW + 13, VMSTATCOL + 8, "buf"); 298 mvprintw(VMSTATROW + 14, VMSTATCOL + 8, "dirtybuf"); 299 300 mvprintw(VMSTATROW + 15, VMSTATCOL + 8, "activ-vp"); 301 mvprintw(VMSTATROW + 16, VMSTATCOL + 8, "cachd-vp"); 302 mvprintw(VMSTATROW + 17, VMSTATCOL + 8, "inact-vp"); 303 304 mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt"); 305 306 mvprintw(GRAPHROW, GRAPHCOL, 307 " . %%Sys . %%Intr . %%User . %%Nice . %%Idle"); 308 mvprintw(PROCSROW, PROCSCOL, " r p d s w"); 309 mvprintw(GRAPHROW + 1, GRAPHCOL, 310 "| | | | | | | | | | |"); 311 312 mvprintw(NAMEIROW, NAMEICOL, "Path-lookups hits %% Components"); 313 mvprintw(DISKROW, DISKCOL, "Disks"); 314 mvprintw(DISKROW + 1, DISKCOL, "KB/t"); 315 mvprintw(DISKROW + 2, DISKCOL, "tpr/s"); 316 mvprintw(DISKROW + 3, DISKCOL, "MBr/s"); 317 mvprintw(DISKROW + 4, DISKCOL, "tpw/s"); 318 mvprintw(DISKROW + 5, DISKCOL, "MBw/s"); 319 mvprintw(DISKROW + 6, DISKCOL, "%% busy"); 320 /* 321 * For now, we don't support a fourth disk statistic. So there's 322 * no point in providing a label for it. If someone can think of a 323 * fourth useful disk statistic, there is room to add it. 324 */ 325 j = 0; 326 for (i = 0; i < num_devices && j < MAXDRIVES; i++) 327 if (dev_select[i].selected) { 328 char tmpstr[80]; 329 sprintf(tmpstr, "%s%d", dev_select[i].device_name, 330 dev_select[i].unit_number); 331 mvprintw(DISKROW, DISKCOL + 5 + 6 * j, 332 " %5.5s", tmpstr); 333 j++; 334 } 335 336 if (j <= 4) { 337 /* 338 * room for extended VM stats 339 */ 340 mvprintw(VMSTATROW + 11, VMSTATCOL - 6, "zfod"); 341 mvprintw(VMSTATROW + 12, VMSTATCOL - 6, "ozfod"); 342 mvprintw(VMSTATROW + 13, VMSTATCOL - 6, "%%sloz"); 343 mvprintw(VMSTATROW + 14, VMSTATCOL - 6, "tfree"); 344 extended_vm_stats = 1; 345 } else { 346 extended_vm_stats = 0; 347 mvprintw(VMSTATROW + 0, VMSTATCOL + 8, "zfod"); 348 } 349 350 for (i = 0; i < nintr; i++) { 351 if (intrloc[i] == 0) 352 continue; 353 mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s", intrname[i]); 354 } 355 } 356 357 #define CP_UPDATE(fld) do { \ 358 uint64_t lt; \ 359 lt=s.fld; \ 360 s.fld-=s1.fld; \ 361 if(state==TIME) \ 362 s1.fld=lt; \ 363 lt=fld; \ 364 fld-=old_##fld; \ 365 if(state==TIME) \ 366 old_##fld=lt; \ 367 etime += s.fld; \ 368 } while(0) 369 #define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;} 370 #define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;} 371 #define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \ 372 if(state == TIME) s1.nchstats.fld = t;} 373 #define PUTRATE(fld, l, c, w) \ 374 Y(fld); \ 375 put64((int64_t)((float)s.fld/etime + 0.5), l, c, w, 'D') 376 #define MAXFAIL 5 377 378 #define CPUSTATES 5 379 static const char cpuchar[5] = { '=' , '+', '>', '-', ' ' }; 380 381 static const size_t cpuoffsets[] = { 382 offsetof(struct kinfo_cputime, cp_sys), 383 offsetof(struct kinfo_cputime, cp_intr), 384 offsetof(struct kinfo_cputime, cp_user), 385 offsetof(struct kinfo_cputime, cp_nice), 386 offsetof(struct kinfo_cputime, cp_idle) 387 }; 388 389 void 390 showkre(void) 391 { 392 float f1, f2; 393 int psiz; 394 int i, lc; 395 long inttotal; 396 long l; 397 static int failcnt = 0; 398 double total_time; 399 400 etime = 0; 401 CP_UPDATE(cp_time.cp_user); 402 CP_UPDATE(cp_time.cp_nice); 403 CP_UPDATE(cp_time.cp_sys); 404 CP_UPDATE(cp_time.cp_intr); 405 CP_UPDATE(cp_time.cp_idle); 406 407 total_time = etime; 408 if (total_time == 0.0) 409 total_time = 1.0; 410 411 if (etime < 100000.0) { /* < 100ms ignore this trash */ 412 if (failcnt++ >= MAXFAIL) { 413 clear(); 414 mvprintw(2, 10, "The alternate system clock has died!"); 415 mvprintw(3, 10, "Reverting to ``pigs'' display."); 416 move(CMDLINE, 0); 417 refresh(); 418 failcnt = 0; 419 sleep(5); 420 command("pigs"); 421 } 422 return; 423 } 424 failcnt = 0; 425 etime /= 1000000.0; 426 etime /= ncpu; 427 if (etime == 0) 428 etime = 1; 429 inttotal = 0; 430 for (i = 0; i < nintr; i++) { 431 if (s.intrcnt[i] == 0) 432 continue; 433 if (intrloc[i] == 0) { 434 if (nextintsrow == LINES) 435 continue; 436 intrloc[i] = nextintsrow++; 437 mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s", 438 intrname[i]); 439 } 440 X(intrcnt); 441 l = (long)((float)s.intrcnt[i]/etime + 0.5); 442 inttotal += l; 443 put64(l, intrloc[i], INTSCOL + 2, 6, 'D'); 444 } 445 put64(inttotal, INTSROW + 1, INTSCOL + 2, 6, 'D'); 446 Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss); 447 Z(ncs_longhits); Z(ncs_longmiss); Z(ncs_neghits); 448 s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits + 449 nchtotal.ncs_miss + nchtotal.ncs_neghits; 450 s.nchpathcount = nchtotal.ncs_longhits + nchtotal.ncs_longmiss; 451 if (state == TIME) { 452 s1.nchcount = s.nchcount; 453 s1.nchpathcount = s.nchpathcount; 454 } 455 456 psiz = 0; 457 f2 = 0.0; 458 for (lc = 0; lc < CPUSTATES; lc++) { 459 uint64_t val = *(uint64_t *)(((uint8_t *)&s.cp_time) + 460 cpuoffsets[lc]); 461 f1 = 100.0 * val / total_time; 462 f2 += f1; 463 l = (int) ((f2 + 1.0) / 2.0) - psiz; 464 if (f1 > 99.9) 465 f1 = 99.9; /* no room to display 100.0 */ 466 putfloat(f1, GRAPHROW, GRAPHCOL + 10 * lc, 4, 1, 0); 467 move(GRAPHROW + 2, psiz); 468 psiz += l; 469 while (l-- > 0) 470 addch(cpuchar[lc]); 471 } 472 473 put64(ucount(), STATROW, STATCOL, 3, 'D'); 474 putfloat(avenrun[0], STATROW, STATCOL + 18, 6, 2, 0); 475 putfloat(avenrun[1], STATROW, STATCOL + 25, 6, 2, 0); 476 putfloat(avenrun[2], STATROW, STATCOL + 32, 6, 2, 0); 477 mvaddstr(STATROW, STATCOL + 53, buf); 478 #define pgtokb(pg) (int64_t)((intmax_t)(pg) * vms.v_page_size / 1024) 479 #define pgtomb(pg) (int64_t)((intmax_t)(pg) * vms.v_page_size / (1024 * 1024)) 480 #define pgtob(pg) (int64_t)((intmax_t)(pg) * vms.v_page_size) 481 put64(pgtob(total.t_arm), MEMROW + 2, MEMCOL + 4, 6, 0); 482 put64(pgtob(total.t_armshr), MEMROW + 2, MEMCOL + 11, 6, 0); 483 put64(pgtob(total.t_avm), MEMROW + 2, MEMCOL + 19, 6, 0); 484 put64(pgtob(total.t_avmshr), MEMROW + 2, MEMCOL + 26, 6, 0); 485 put64(pgtob(total.t_rm), MEMROW + 3, MEMCOL + 4, 6, 0); 486 put64(pgtob(total.t_rmshr), MEMROW + 3, MEMCOL + 11, 6, 0); 487 put64(pgtob(total.t_vm), MEMROW + 3, MEMCOL + 19, 6, 0); 488 put64(pgtob(total.t_vmshr), MEMROW + 3, MEMCOL + 26, 6, 0); 489 put64(pgtob(total.t_free), MEMROW + 2, MEMCOL + 34, 6, 0); 490 put64(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 0, 3, 'D'); 491 put64(total.t_pw, PROCSROW + 1, PROCSCOL + 3, 3, 'D'); 492 put64(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3, 'D'); 493 put64(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3, 'D'); 494 put64(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3, 'D'); 495 if (extended_vm_stats == 0) { 496 PUTRATE(Vmm.v_zfod, VMSTATROW + 0, VMSTATCOL, 7); 497 } 498 PUTRATE(Vmm.v_cow_faults, VMSTATROW + 1, VMSTATCOL, 7); 499 put64(pgtob(vms.v_wire_count), VMSTATROW + 2, VMSTATCOL, 7, 0); 500 put64(pgtob(vms.v_active_count), VMSTATROW + 3, VMSTATCOL, 7, 0); 501 put64(pgtob(vms.v_inactive_count), VMSTATROW + 4, VMSTATCOL, 7, 0); 502 put64(pgtob(vms.v_cache_count), VMSTATROW + 5, VMSTATCOL, 7, 0); 503 put64(pgtob(vms.v_free_count), VMSTATROW + 6, VMSTATCOL, 7, 0); 504 PUTRATE(Vmm.v_dfree, VMSTATROW + 7, VMSTATCOL, 7); 505 PUTRATE(Vmm.v_pfree, VMSTATROW + 8, VMSTATCOL, 7); 506 PUTRATE(Vmm.v_reactivated, VMSTATROW + 9, VMSTATCOL, 7); 507 PUTRATE(Vmm.v_pdwakeups, VMSTATROW + 10, VMSTATCOL, 7); 508 PUTRATE(Vmm.v_pdpages, VMSTATROW + 11, VMSTATCOL, 7); 509 PUTRATE(Vmm.v_intrans, VMSTATROW + 12, VMSTATCOL, 7); 510 511 if (extended_vm_stats) { 512 PUTRATE(Vmm.v_zfod, VMSTATROW + 11, VMSTATCOL - 16, 9); 513 PUTRATE(Vmm.v_ozfod, VMSTATROW + 12, VMSTATCOL - 16, 9); 514 #define nz(x) ((x) ? (x) : 1) 515 put64((s.Vmm.v_zfod - s.Vmm.v_ozfod) * 100 / nz(s.Vmm.v_zfod), 516 VMSTATROW + 13, VMSTATCOL - 16, 9, 'D'); 517 #undef nz 518 PUTRATE(Vmm.v_tfree, VMSTATROW + 14, VMSTATCOL - 16, 9); 519 } 520 521 put64(s.bufspace, VMSTATROW + 13, VMSTATCOL, 7, 0); 522 put64(s.dirtybufspace/1024, VMSTATROW + 14, VMSTATCOL, 7, 'k'); 523 put64(s.activevnodes, VMSTATROW + 15, VMSTATCOL, 7, 'D'); 524 put64(s.cachedvnodes, VMSTATROW + 16, VMSTATCOL, 7, 'D'); 525 put64(s.inactivevnodes, VMSTATROW + 17, VMSTATCOL, 7, 'D'); 526 PUTRATE(Vmm.v_vnodein, PAGEROW + 2, PAGECOL + 6, 4); 527 PUTRATE(Vmm.v_vnodeout, PAGEROW + 2, PAGECOL + 11, 4); 528 PUTRATE(Vmm.v_swapin, PAGEROW + 2, PAGECOL + 18, 4); 529 PUTRATE(Vmm.v_swapout, PAGEROW + 2, PAGECOL + 23, 4); 530 PUTRATE(Vmm.v_vnodepgsin, PAGEROW + 3, PAGECOL + 6, 4); 531 PUTRATE(Vmm.v_vnodepgsout, PAGEROW + 3, PAGECOL + 11, 4); 532 PUTRATE(Vmm.v_swappgsin, PAGEROW + 3, PAGECOL + 18, 4); 533 PUTRATE(Vmm.v_swappgsout, PAGEROW + 3, PAGECOL + 23, 4); 534 PUTRATE(Vmm.v_swtch, GENSTATROW + 1, GENSTATCOL + 1, 4); 535 PUTRATE(Vmm.v_trap, GENSTATROW + 1, GENSTATCOL + 6, 4); 536 PUTRATE(Vmm.v_syscall, GENSTATROW + 1, GENSTATCOL + 11, 4); 537 PUTRATE(Vmm.v_intr, GENSTATROW + 1, GENSTATCOL + 16, 4); 538 PUTRATE(Vmm.v_soft, GENSTATROW + 1, GENSTATCOL + 21, 4); 539 PUTRATE(Vmm.v_vm_faults, GENSTATROW + 1, GENSTATCOL + 26, 4); 540 mvprintw(DISKROW, DISKCOL + 5, " "); 541 for (i = 0, lc = 0; i < num_devices && lc < MAXDRIVES; i++) 542 if (dev_select[i].selected) { 543 char tmpstr[80]; 544 sprintf(tmpstr, "%s%d", dev_select[i].device_name, 545 dev_select[i].unit_number); 546 mvprintw(DISKROW, DISKCOL + 5 + 6 * lc, 547 " %5.5s", tmpstr); 548 switch(state) { 549 case TIME: 550 dinfo(i, ++lc, &cur, &last); 551 break; 552 case RUN: 553 dinfo(i, ++lc, &cur, &run); 554 break; 555 case BOOT: 556 dinfo(i, ++lc, &cur, NULL); 557 break; 558 } 559 } 560 #define nz(x) ((x) ? (x) : 1) 561 put64(s.nchpathcount, NAMEIROW + 1, NAMEICOL + 6, 6, 'D'); 562 put64(nchtotal.ncs_longhits, NAMEIROW + 1, NAMEICOL + 13, 6, 'D'); 563 putfloat(nchtotal.ncs_longhits * 100.0 / nz(s.nchpathcount), 564 NAMEIROW + 1, NAMEICOL + 19, 4, 0, 0); 565 566 putfloat((double)s.nchcount / nz(s.nchpathcount), 567 NAMEIROW + 1, NAMEICOL + 27, 5, 2, 1); 568 #undef nz 569 } 570 571 int 572 cmdkre(const char *cmd, char *args) 573 { 574 int retval; 575 576 if (prefix(cmd, "run")) { 577 retval = 1; 578 copyinfo(&s2, &s1); 579 switch (getdevs(&run)) { 580 case -1: 581 errx(1, "%s", devstat_errbuf); 582 break; 583 case 1: 584 num_devices = run.dinfo->numdevs; 585 generation = run.dinfo->generation; 586 retval = dscmd("refresh", NULL, MAXDRIVES, &cur); 587 if (retval == 2) 588 labelkre(); 589 break; 590 default: 591 break; 592 } 593 state = RUN; 594 return (retval); 595 } 596 if (prefix(cmd, "boot")) { 597 state = BOOT; 598 copyinfo(&z, &s1); 599 return (1); 600 } 601 if (prefix(cmd, "time")) { 602 state = TIME; 603 return (1); 604 } 605 if (prefix(cmd, "zero")) { 606 retval = 1; 607 if (state == RUN) { 608 getinfo(&s1); 609 switch (getdevs(&run)) { 610 case -1: 611 errx(1, "%s", devstat_errbuf); 612 break; 613 case 1: 614 num_devices = run.dinfo->numdevs; 615 generation = run.dinfo->generation; 616 retval = dscmd("refresh",NULL, MAXDRIVES, &cur); 617 if (retval == 2) 618 labelkre(); 619 break; 620 default: 621 break; 622 } 623 } 624 return (retval); 625 } 626 retval = dscmd(cmd, args, MAXDRIVES, &cur); 627 628 if (retval == 2) 629 labelkre(); 630 631 return(retval); 632 } 633 634 /* calculate number of users on the system */ 635 static int 636 ucount(void) 637 { 638 struct utmpentry *ep; 639 int nusers = 0; 640 641 getutentries(NULL, &ep); 642 for (; ep; ep = ep->next) 643 nusers++; 644 645 return (nusers); 646 } 647 648 static void 649 put64(intmax_t n, int l, int lc, int w, int type) 650 { 651 char b[128]; 652 int isneg; 653 int i; 654 int64_t d; 655 int64_t u; 656 657 move(l, lc); 658 if (n == 0) { 659 while (w-- > 0) 660 addch(' '); 661 return; 662 } 663 if (type == 0 || type == 'D') 664 snprintf(b, sizeof(b), "%*jd", w, n); 665 else 666 snprintf(b, sizeof(b), "%*jd%c", w - 1, n, type); 667 if (strlen(b) <= (size_t)w) { 668 addstr(b); 669 return; 670 } 671 672 if (type == 'D') 673 u = 1000; 674 else 675 u = 1024; 676 if (n < 0) { 677 n = -n; 678 isneg = 1; 679 } else { 680 isneg = 0; 681 } 682 683 for (d = 1; n / d >= 1000; d *= u) { 684 switch(type) { 685 case 'D': 686 case 0: 687 type = 'k'; 688 break; 689 case 'k': 690 type = 'M'; 691 break; 692 case 'M': 693 type = 'G'; 694 break; 695 case 'G': 696 type = 'T'; 697 break; 698 case 'T': 699 type = 'X'; 700 break; 701 default: 702 type = '?'; 703 break; 704 } 705 } 706 707 i = w - isneg; 708 if (n / d >= 100) 709 i -= 3; 710 else if (n / d >= 10) 711 i -= 2; 712 else 713 i -= 1; 714 if (i > 4) { 715 snprintf(b + 64, sizeof(b) - 64, "%jd.%03jd%c", 716 n / d, n / (d / 1000) % 1000, type); 717 } else if (i > 3) { 718 snprintf(b + 64, sizeof(b) - 64, "%jd.%02jd%c", 719 n / d, n / (d / 100) % 100, type); 720 } else if (i > 2) { 721 snprintf(b + 64, sizeof(b) - 64, "%jd.%01jd%c", 722 n / d, n / (d / 10) % 10, type); 723 } else { 724 snprintf(b + 64, sizeof(b) - 64, "%jd%c", 725 n / d, type); 726 } 727 w -= strlen(b + 64); 728 i = 64; 729 if (isneg) { 730 b[--i] = '-'; 731 --w; 732 } 733 while (w > 0) { 734 --w; 735 b[--i] = ' '; 736 } 737 addstr(b + i); 738 } 739 740 static void 741 putfloat(double f, int l, int lc, int w, int d, int nz) 742 { 743 char b[128]; 744 745 move(l, lc); 746 if (nz && f == 0.0) { 747 while (--w >= 0) 748 addch(' '); 749 return; 750 } 751 snprintf(b, sizeof(b), "%*.*f", w, d, f); 752 if (strlen(b) > (size_t)w) 753 snprintf(b, sizeof(b), "%*.0f", w, f); 754 if (strlen(b) > (size_t)w) { 755 while (--w >= 0) 756 addch('*'); 757 return; 758 } 759 addstr(b); 760 } 761 762 static void 763 putlongdouble(long double f, int l, int lc, int w, int d, int nz) 764 { 765 char b[128]; 766 767 move(l, lc); 768 if (nz && f == 0.0) { 769 while (--w >= 0) 770 addch(' '); 771 return; 772 } 773 sprintf(b, "%*.*Lf", w, d, f); 774 if (strlen(b) > (size_t)w) 775 sprintf(b, "%*.0Lf", w, f); 776 if (strlen(b) > (size_t)w) { 777 while (--w >= 0) 778 addch('*'); 779 return; 780 } 781 addstr(b); 782 } 783 784 static void 785 putlongdoublez(long double f, int l, int lc, int w, int d, int nz) 786 { 787 char b[128]; 788 789 if (f == 0.0) { 790 move(l, lc); 791 sprintf(b, "%*.*s", w, w, ""); 792 addstr(b); 793 } else { 794 putlongdouble(f, l, lc, w, d, nz); 795 } 796 } 797 798 static void 799 getinfo(struct Info *ls) 800 { 801 struct devinfo *tmp_dinfo; 802 struct nchstats *nch_tmp; 803 size_t size; 804 size_t vms_size = sizeof(ls->Vms); 805 size_t vmm_size = sizeof(ls->Vmm); 806 size_t nch_size = sizeof(ls->nchstats) * SMP_MAXCPU; 807 808 if (sysctlbyname("vm.vmstats", &ls->Vms, &vms_size, NULL, 0)) { 809 perror("sysctlbyname: vm.vmstats"); 810 exit(1); 811 } 812 if (sysctlbyname("vm.vmmeter", &ls->Vmm, &vmm_size, NULL, 0)) { 813 perror("sysctlbyname: vm.vmstats"); 814 exit(1); 815 } 816 817 if (kinfo_get_sched_cputime(&ls->cp_time)) 818 err(1, "kinfo_get_sched_cputime"); 819 if (kinfo_get_sched_cputime(&cp_time)) 820 err(1, "kinfo_get_sched_cputime"); 821 NREAD(X_BUFFERSPACE, &ls->bufspace, sizeof(ls->bufspace)); 822 NREAD(X_DESIREDVNODES, &ls->desiredvnodes, sizeof(ls->desiredvnodes)); 823 NREAD(X_CACHEDVNODES, &ls->cachedvnodes, sizeof(ls->cachedvnodes)); 824 NREAD(X_INACTIVEVNODES, &ls->inactivevnodes, 825 sizeof(ls->inactivevnodes)); 826 NREAD(X_ACTIVEVNODES, &ls->activevnodes, sizeof(ls->activevnodes)); 827 NREAD(X_NUMDIRTYBUFFERS, &ls->dirtybufspace, sizeof(ls->dirtybufspace)); 828 829 if (nintr) { 830 size = nintr * sizeof(ls->intrcnt[0]); 831 sysctlbyname("hw.intrcnt_all", ls->intrcnt, &size, NULL, 0); 832 } 833 size = sizeof(ls->Total); 834 if (sysctlbyname("vm.vmtotal", &ls->Total, &size, NULL, 0) < 0) { 835 error("Can't get kernel info: %s\n", strerror(errno)); 836 bzero(&ls->Total, sizeof(ls->Total)); 837 } 838 839 if ((nch_tmp = malloc(nch_size)) == NULL) { 840 perror("malloc"); 841 exit(1); 842 } else { 843 if (sysctlbyname("vfs.cache.nchstats", nch_tmp, &nch_size, NULL, 0)) { 844 perror("sysctlbyname vfs.cache.nchstats"); 845 free(nch_tmp); 846 exit(1); 847 } else { 848 if ((nch_tmp = realloc(nch_tmp, nch_size)) == NULL) { 849 perror("realloc"); 850 exit(1); 851 } 852 } 853 } 854 855 if (kinfo_get_cpus(&ncpu)) 856 err(1, "kinfo_get_cpus"); 857 kvm_nch_cpuagg(nch_tmp, &ls->nchstats, ncpu); 858 free(nch_tmp); 859 860 tmp_dinfo = last.dinfo; 861 last.dinfo = cur.dinfo; 862 cur.dinfo = tmp_dinfo; 863 864 last.busy_time = cur.busy_time; 865 switch (getdevs(&cur)) { 866 case -1: 867 errx(1, "%s", devstat_errbuf); 868 break; 869 case 1: 870 num_devices = cur.dinfo->numdevs; 871 generation = cur.dinfo->generation; 872 cmdkre("refresh", NULL); 873 break; 874 default: 875 break; 876 } 877 } 878 879 static void 880 allocinfo(struct Info *ls) 881 { 882 ls->intrcnt = (long *) calloc(nintr, sizeof(long)); 883 if (ls->intrcnt == NULL) 884 errx(2, "out of memory"); 885 } 886 887 static void 888 copyinfo(struct Info *from, struct Info *to) 889 { 890 long *intrcnt; 891 892 /* 893 * time, wds, seek, and xfer are malloc'd so we have to 894 * save the pointers before the structure copy and then 895 * copy by hand. 896 */ 897 intrcnt = to->intrcnt; 898 *to = *from; 899 900 bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int)); 901 } 902 903 static void 904 dinfo(int dn, int lc, struct statinfo *now, struct statinfo *then) 905 { 906 long double kb_per_transfer; 907 long double transfers_per_secondr; 908 long double transfers_per_secondw; 909 long double mb_per_secondr; 910 long double mb_per_secondw; 911 long double elapsed_time, device_busy; 912 int di; 913 914 di = dev_select[dn].position; 915 916 elapsed_time = compute_etime(now->busy_time, then ? 917 then->busy_time : 918 now->dinfo->devices[di].dev_creation_time); 919 920 device_busy = compute_etime(now->dinfo->devices[di].busy_time, then ? 921 then->dinfo->devices[di].busy_time : 922 now->dinfo->devices[di].dev_creation_time); 923 924 if (compute_stats( 925 &now->dinfo->devices[di], 926 (then ? &then->dinfo->devices[di] : NULL), 927 elapsed_time, 928 NULL, NULL, NULL, 929 &kb_per_transfer, 930 NULL, 931 NULL, 932 NULL, NULL) != 0) 933 errx(1, "%s", devstat_errbuf); 934 935 if (compute_stats_read( 936 &now->dinfo->devices[di], 937 (then ? &then->dinfo->devices[di] : NULL), 938 elapsed_time, 939 NULL, NULL, NULL, 940 NULL, 941 &transfers_per_secondr, 942 &mb_per_secondr, 943 NULL, NULL) != 0) 944 errx(1, "%s", devstat_errbuf); 945 946 if (compute_stats_write( 947 &now->dinfo->devices[di], 948 (then ? &then->dinfo->devices[di] : NULL), 949 elapsed_time, 950 NULL, NULL, NULL, 951 NULL, 952 &transfers_per_secondw, 953 &mb_per_secondw, 954 NULL, NULL) != 0) 955 errx(1, "%s", devstat_errbuf); 956 957 #if 0 958 /* 959 * Remove this hack, it no longer works properly and will 960 * report 100% busy in situations where the device is able 961 * to respond to the requests faster than the busy counter's 962 * granularity. 963 */ 964 if ((device_busy == 0) && 965 (transfers_per_secondr > 5 || transfers_per_secondw > 5)) { 966 /* the device has been 100% busy, fake it because 967 * as long as the device is 100% busy the busy_time 968 * field in the devstat struct is not updated */ 969 device_busy = elapsed_time; 970 } 971 #endif 972 if (device_busy > elapsed_time) { 973 /* this normally happens after one or more periods 974 * where the device has been 100% busy, correct it */ 975 device_busy = elapsed_time; 976 } 977 978 lc = DISKCOL + lc * 6; 979 putlongdoublez(kb_per_transfer, DISKROW + 1, lc, 5, 2, 0); 980 putlongdoublez(transfers_per_secondr, DISKROW + 2, lc, 5, 0, 0); 981 putlongdoublez(mb_per_secondr, DISKROW + 3, lc, 5, 2, 0); 982 putlongdoublez(transfers_per_secondw, DISKROW + 4, lc, 5, 0, 0); 983 putlongdoublez(mb_per_secondw, DISKROW + 5, lc, 5, 2, 0); 984 putlongdouble(device_busy * 100 / elapsed_time, 985 DISKROW + 6, lc, 5, 0, 0); 986 } 987