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