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