1 /* $OpenBSD: vmstat.c,v 1.71 2009/08/14 14:41:55 deraadt Exp $ */ 2 /* $NetBSD: vmstat.c,v 1.5 1996/05/10 23:16:40 thorpej Exp $ */ 3 4 /*- 5 * Copyright (c) 1983, 1989, 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 1/12/94"; 36 #endif 37 static char rcsid[] = "$OpenBSD: vmstat.c,v 1.71 2009/08/14 14:41:55 deraadt Exp $"; 38 #endif /* not lint */ 39 40 /* 41 * Cursed vmstat -- from Robert Elz. 42 */ 43 44 #include <sys/param.h> 45 #include <sys/dkstat.h> 46 #include <sys/buf.h> 47 #include <sys/stat.h> 48 #include <sys/time.h> 49 #include <sys/user.h> 50 #include <sys/proc.h> 51 #include <sys/namei.h> 52 #include <sys/sysctl.h> 53 54 #include <uvm/uvm_extern.h> 55 56 #include <ctype.h> 57 #include <err.h> 58 #include <paths.h> 59 #include <signal.h> 60 #include <stdlib.h> 61 #include <string.h> 62 #include <unistd.h> 63 64 #include "systat.h" 65 66 static struct Info { 67 long time[CPUSTATES]; 68 struct uvmexp uvmexp; 69 struct vmtotal Total; 70 struct nchstats nchstats; 71 long nchcount; 72 u_quad_t *intrcnt; 73 } s, s1, s2, s3, z; 74 75 #include "dkstats.h" 76 extern struct _disk cur; 77 78 #define cnt s.Cnt 79 #define oldcnt s1.Cnt 80 #define total s.Total 81 #define nchtotal s.nchstats 82 #define oldnchtotal s1.nchstats 83 84 static enum state { BOOT, TIME, RUN } state = TIME; 85 86 static void allocinfo(struct Info *); 87 static void copyinfo(struct Info *, struct Info *); 88 static float cputime(int); 89 static void dinfo(int, int); 90 static void getinfo(struct Info *); 91 void putint(int, int, int, int); 92 void putintmk(int, int, int, int); 93 void putuint64(u_int64_t, int, int, int); 94 void putfloat(double, int, int, int, int, int); 95 int ucount(void); 96 97 void print_vm(void); 98 int read_vm(void); 99 int select_vm(void); 100 int vm_keyboard_callback(int); 101 102 static time_t t; 103 static double etime; 104 static float hertz; 105 static int nintr; 106 static long *intrloc; 107 static char **intrname; 108 static int nextintsrow; 109 110 WINDOW * 111 openkre(void) 112 { 113 return (subwin(stdscr, LINES-1-1, 0, 1, 0)); 114 } 115 116 void 117 closekre(WINDOW *w) 118 { 119 120 if (w == NULL) 121 return; 122 wclear(w); 123 wrefresh(w); 124 delwin(w); 125 } 126 127 /* 128 * These constants define where the major pieces are laid out 129 */ 130 #define STATROW 0 /* uses 1 row and 68 cols */ 131 #define STATCOL 2 132 #define MEMROW 2 /* uses 4 rows and 34 cols */ 133 #define MEMCOL 0 134 #define PAGEROW 2 /* uses 4 rows and 26 cols */ 135 #define PAGECOL 37 136 #define INTSROW 2 /* uses all rows to bottom and 17 cols */ 137 #define INTSCOL 63 138 #define PROCSROW 7 /* uses 2 rows and 20 cols */ 139 #define PROCSCOL 0 140 #define GENSTATROW 7 /* uses 2 rows and 35 cols */ 141 #define GENSTATCOL 16 142 #define VMSTATROW 7 /* uses 18 rows and 12 cols */ 143 #define VMSTATCOL 48 144 #define GRAPHROW 10 /* uses 3 rows and 51 cols */ 145 #define GRAPHCOL 0 146 #define NAMEIROW 14 /* uses 3 rows and 49 cols */ 147 #define NAMEICOL 0 148 #define DISKROW 18 /* uses 5 rows and 50 cols (for 9 drives) */ 149 #define DISKCOL 0 150 151 #define DRIVESPACE 45 /* max space for drives */ 152 153 154 field_def *view_vm_0[] = { 155 NULL 156 }; 157 158 /* Define view managers */ 159 struct view_manager vmstat_mgr = { 160 "VMstat", select_vm, read_vm, NULL, print_header, 161 print_vm, vm_keyboard_callback, NULL, NULL 162 }; 163 164 field_view views_vm[] = { 165 {view_vm_0, "vmstat", '7', &vmstat_mgr}, 166 {NULL, NULL, 0, NULL} 167 }; 168 169 int ncpu = 1; 170 171 int 172 initvmstat(void) 173 { 174 field_view *v; 175 int mib[4], i; 176 size_t size; 177 178 mib[0] = CTL_HW; 179 mib[1] = HW_NCPU; 180 size = sizeof(ncpu); 181 if (sysctl(mib, 2, &ncpu, &size, NULL, 0) < 0) 182 return (-1); 183 184 hertz = stathz ? stathz : hz; 185 if (!dkinit(1)) 186 return(0); 187 188 mib[0] = CTL_KERN; 189 mib[1] = KERN_INTRCNT; 190 mib[2] = KERN_INTRCNT_NUM; 191 size = sizeof(nintr); 192 if (sysctl(mib, 3, &nintr, &size, NULL, 0) < 0) 193 return (-1); 194 195 intrloc = calloc(nintr, sizeof(long)); 196 intrname = calloc(nintr, sizeof(char *)); 197 198 for (i = 0; i < nintr; i++) { 199 char name[128]; 200 201 mib[0] = CTL_KERN; 202 mib[1] = KERN_INTRCNT; 203 mib[2] = KERN_INTRCNT_NAME; 204 mib[3] = i; 205 size = sizeof(name); 206 if (sysctl(mib, 4, name, &size, NULL, 0) < 0) 207 return (-1); 208 209 intrname[i] = strdup(name); 210 if (intrname[i] == NULL) 211 return (-1); 212 } 213 214 nextintsrow = INTSROW + 2; 215 allocinfo(&s); 216 allocinfo(&s1); 217 allocinfo(&s2); 218 allocinfo(&s3); 219 allocinfo(&z); 220 221 getinfo(&s2); 222 copyinfo(&z, &s1); 223 224 for (v = views_vm; v->name != NULL; v++) 225 add_view(v); 226 227 return(1); 228 } 229 230 void 231 fetchkre(void) 232 { 233 getinfo(&s3); 234 } 235 236 void 237 labelkre(void) 238 { 239 int i, j, l; 240 241 mvprintw(MEMROW, MEMCOL, " memory totals (in KB)"); 242 mvprintw(MEMROW + 1, MEMCOL, " real virtual free"); 243 mvprintw(MEMROW + 2, MEMCOL, "Active"); 244 mvprintw(MEMROW + 3, MEMCOL, "All"); 245 246 mvprintw(PAGEROW, PAGECOL, " PAGING SWAPPING "); 247 mvprintw(PAGEROW + 1, PAGECOL, " in out in out "); 248 mvprintw(PAGEROW + 2, PAGECOL, "ops"); 249 mvprintw(PAGEROW + 3, PAGECOL, "pages"); 250 251 mvprintw(INTSROW, INTSCOL + 3, " Interrupts"); 252 mvprintw(INTSROW + 1, INTSCOL + 9, "total"); 253 254 mvprintw(LINES - 3, INTSCOL + 9, "IPKTS"); 255 mvprintw(LINES - 2, INTSCOL + 9, "OPKTS"); 256 257 mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks"); 258 mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw"); 259 mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm"); 260 mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait"); 261 mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck"); 262 mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok"); 263 mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram"); 264 mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy"); 265 mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp"); 266 mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod"); 267 mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow"); 268 mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin"); 269 mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg"); 270 mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg"); 271 mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired"); 272 mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre"); 273 if (LINES - 1 > VMSTATROW + 16) 274 mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn"); 275 if (LINES - 1 > VMSTATROW + 17) 276 mvprintw(VMSTATROW + 17, VMSTATCOL + 10, "pzidle"); 277 if (LINES - 1 > VMSTATROW + 18) 278 mvprintw(VMSTATROW + 18, VMSTATCOL + 10, "kmapent"); 279 280 mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt"); 281 282 mvprintw(GRAPHROW, GRAPHCOL, 283 " . %%Int . %%Sys . %%Usr . %%Nic . %%Idle"); 284 mvprintw(PROCSROW, PROCSCOL, "Proc:r d s w"); 285 mvprintw(GRAPHROW + 1, GRAPHCOL, 286 "| | | | | | | | | | |"); 287 288 mvprintw(NAMEIROW, NAMEICOL, 289 "Namei Sys-cache Proc-cache No-cache"); 290 mvprintw(NAMEIROW + 1, NAMEICOL, 291 " Calls hits %% hits %% miss %%"); 292 mvprintw(DISKROW, DISKCOL, "Disks"); 293 mvprintw(DISKROW + 1, DISKCOL, "seeks"); 294 mvprintw(DISKROW + 2, DISKCOL, "xfers"); 295 mvprintw(DISKROW + 3, DISKCOL, "speed"); 296 mvprintw(DISKROW + 4, DISKCOL, " sec"); 297 for (i = 0, j = 0; i < cur.dk_ndrive && j < DRIVESPACE; i++) 298 if (cur.dk_select[i] && (j + strlen(dr_name[i])) < DRIVESPACE) { 299 l = MAX(5, strlen(dr_name[i])); 300 mvprintw(DISKROW, DISKCOL + 5 + j, 301 " %*s", l, dr_name[i]); 302 j += 1 + l; 303 } 304 for (i = 0; i < nintr; i++) { 305 if (intrloc[i] == 0) 306 continue; 307 mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]); 308 } 309 } 310 311 #define X(fld) {s.fld[i]; s.fld[i]-=s1.fld[i];} 312 #define Y(fld) {s.fld; s.fld -= s1.fld;} 313 #define Z(fld) {s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld;} 314 #define PUTRATE(fld, l, c, w) \ 315 do { \ 316 Y(fld); \ 317 putint((int)((float)s.fld/etime + 0.5), l, c, w); \ 318 } while (0) 319 #define MAXFAIL 5 320 321 static char cpuchar[CPUSTATES] = { '|', '=', '>', '-', ' ' }; 322 static char cpuorder[CPUSTATES] = { CP_INTR, CP_SYS, CP_USER, CP_NICE, CP_IDLE }; 323 324 void 325 showkre(void) 326 { 327 float f1, f2; 328 int psiz; 329 u_int64_t inttotal, intcnt; 330 int i, l, c; 331 static int failcnt = 0, first_run = 0; 332 333 if (state == TIME) { 334 if (!first_run) { 335 first_run = 1; 336 return; 337 } 338 } 339 etime = 0; 340 for (i = 0; i < CPUSTATES; i++) { 341 X(time); 342 etime += s.time[i]; 343 } 344 if (etime < 5.0) { /* < 5 ticks - ignore this trash */ 345 if (failcnt++ >= MAXFAIL) { 346 error("The alternate system clock has died!"); 347 failcnt = 0; 348 } 349 return; 350 } 351 failcnt = 0; 352 etime /= hertz; 353 etime /= ncpu; 354 inttotal = 0; 355 for (i = 0; i < nintr; i++) { 356 if (s.intrcnt[i] == 0) 357 continue; 358 if (intrloc[i] == 0) { 359 if (nextintsrow == LINES) 360 continue; 361 intrloc[i] = nextintsrow++; 362 mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", 363 intrname[i]); 364 } 365 t = intcnt = s.intrcnt[i]; 366 s.intrcnt[i] -= s1.intrcnt[i]; 367 intcnt = (u_int64_t)((float)s.intrcnt[i]/etime + 0.5); 368 inttotal += intcnt; 369 putuint64(intcnt, intrloc[i], INTSCOL, 8); 370 } 371 putuint64(inttotal, INTSROW + 1, INTSCOL, 8); 372 Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss); 373 Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes); 374 s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits + 375 nchtotal.ncs_miss + nchtotal.ncs_long; 376 377 putint(sum.ifc_ip, LINES - 3, INTSCOL, 8); 378 putint(sum.ifc_op, LINES - 2, INTSCOL, 8); 379 380 psiz = 0; 381 f2 = 0.0; 382 383 for (c = 0; c < CPUSTATES; c++) { 384 i = cpuorder[c]; 385 f1 = cputime(i); 386 f2 += f1; 387 l = (int) ((f2 + 1.0) / 2.0) - psiz; 388 putfloat(f1, GRAPHROW, GRAPHCOL + 1 + (10 * c), 5, 1, 0); 389 move(GRAPHROW + 2, psiz); 390 psiz += l; 391 while (l-- > 0) 392 addch(cpuchar[c]); 393 } 394 395 #define pgtokb(pg) ((pg) * (s.uvmexp.pagesize / 1024)) 396 397 putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 7, 8); 398 putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse), /* XXX */ 399 MEMROW + 2, MEMCOL + 17, 8); 400 putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 7, 8); 401 putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse), 402 MEMROW + 3, MEMCOL + 17, 8); 403 putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 26, 8); 404 putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse), 405 MEMROW + 3, MEMCOL + 26, 8); 406 putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3); 407 408 putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3); 409 putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3); 410 putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3); 411 PUTRATE(uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6); 412 PUTRATE(uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6); 413 PUTRATE(uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6); 414 PUTRATE(uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5); 415 PUTRATE(uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6); 416 PUTRATE(uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6); 417 PUTRATE(uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6); 418 PUTRATE(uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6); 419 PUTRATE(uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6); 420 PUTRATE(uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6); 421 PUTRATE(uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9); 422 putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9); 423 putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9); 424 putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9); 425 putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9); 426 PUTRATE(uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9); 427 if (LINES - 1 > VMSTATROW + 16) 428 PUTRATE(uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9); 429 if (LINES - 1 > VMSTATROW + 17) 430 PUTRATE(uvmexp.zeropages, VMSTATROW + 17, VMSTATCOL, 9); 431 if (LINES - 1 > VMSTATROW + 18) 432 putint(s.uvmexp.kmapent, VMSTATROW + 18, VMSTATCOL, 9); 433 434 PUTRATE(uvmexp.pageins, PAGEROW + 2, PAGECOL + 5, 5); 435 PUTRATE(uvmexp.pdpageouts, PAGEROW + 2, PAGECOL + 10, 5); 436 PUTRATE(uvmexp.swapins, PAGEROW + 2, PAGECOL + 15, 5); 437 PUTRATE(uvmexp.swapouts, PAGEROW + 2, PAGECOL + 20, 5); 438 PUTRATE(uvmexp.pgswapin, PAGEROW + 3, PAGECOL + 5, 5); 439 PUTRATE(uvmexp.pgswapout, PAGEROW + 3, PAGECOL + 10, 5); 440 441 PUTRATE(uvmexp.swtch, GENSTATROW + 1, GENSTATCOL, 6); 442 PUTRATE(uvmexp.traps, GENSTATROW + 1, GENSTATCOL + 6, 6); 443 PUTRATE(uvmexp.syscalls, GENSTATROW + 1, GENSTATCOL + 12, 6); 444 PUTRATE(uvmexp.intrs, GENSTATROW + 1, GENSTATCOL + 18, 6); 445 PUTRATE(uvmexp.softs, GENSTATROW + 1, GENSTATCOL + 24, 6); 446 PUTRATE(uvmexp.faults, GENSTATROW + 1, GENSTATCOL + 30, 5); 447 mvprintw(DISKROW, DISKCOL + 5, " "); 448 for (i = 0, c = 0; i < cur.dk_ndrive && c < DRIVESPACE; i++) 449 if (cur.dk_select[i] && (c + strlen(dr_name[i])) < DRIVESPACE) { 450 l = MAX(5, strlen(dr_name[i])); 451 mvprintw(DISKROW, DISKCOL + 5 + c, 452 " %*s", l, dr_name[i]); 453 c += 1 + l; 454 dinfo(i, c); 455 } 456 /* and pad the DRIVESPACE */ 457 l = DRIVESPACE - c; 458 for (i = 0; i < 5; i++) 459 mvprintw(DISKROW + i, DISKCOL + 5 + c, "%*s", l, ""); 460 461 putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9); 462 putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 10, 8); 463 #define nz(x) ((x) ? (x) : 1) 464 putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount), 465 NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1); 466 putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 24, 7); 467 putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount), 468 NAMEIROW + 2, NAMEICOL + 33, 4, 0, 1); 469 putint(nchtotal.ncs_miss + nchtotal.ncs_long - nchtotal.ncs_pass2, 470 NAMEIROW + 2, NAMEICOL + 38, 7); 471 putfloat((nchtotal.ncs_miss + nchtotal.ncs_long - nchtotal.ncs_pass2) * 472 100.0 / nz(s.nchcount), NAMEIROW + 2, NAMEICOL + 45, 4, 0, 1); 473 #undef nz 474 475 } 476 477 int 478 vm_keyboard_callback(int ch) 479 { 480 switch(ch) { 481 case 'r': 482 copyinfo(&s2, &s1); 483 state = RUN; 484 break; 485 case 'b': 486 state = BOOT; 487 copyinfo(&z, &s1); 488 break; 489 case 't': 490 state = TIME; 491 break; 492 case 'z': 493 if (state == RUN) 494 getinfo(&s1); 495 break; 496 } 497 return (keyboard_callback(ch)); 498 } 499 500 501 static float 502 cputime(int indx) 503 { 504 double tm; 505 int i; 506 507 tm = 0; 508 for (i = 0; i < CPUSTATES; i++) 509 tm += s.time[i]; 510 if (tm == 0.0) 511 tm = 1.0; 512 return (s.time[indx] * 100.0 / tm); 513 } 514 515 void 516 putint(int n, int l, int c, int w) 517 { 518 char b[128]; 519 520 move(l, c); 521 if (n == 0) { 522 while (w-- > 0) 523 addch(' '); 524 return; 525 } 526 snprintf(b, sizeof b, "%*d", w, n); 527 if (strlen(b) > w) { 528 while (w-- > 0) 529 addch('*'); 530 return; 531 } 532 addstr(b); 533 } 534 535 void 536 putintmk(int n, int l, int c, int w) 537 { 538 char b[128]; 539 540 move(l, c); 541 if (n == 0) { 542 while (w-- > 0) 543 addch(' '); 544 return; 545 } 546 if (n > 9999 * 1024) 547 snprintf(b, sizeof b, "%*dG", w - 1, n / 1024 / 1024); 548 else if (n > 9999) 549 snprintf(b, sizeof b, "%*dM", w - 1, n / 1024); 550 else 551 snprintf(b, sizeof b, "%*dK", w - 1, n); 552 if (strlen(b) > w) { 553 while (w-- > 0) 554 addch('*'); 555 return; 556 } 557 addstr(b); 558 } 559 560 void 561 putuint64(u_int64_t n, int l, int c, int w) 562 { 563 char b[128]; 564 565 move(l, c); 566 if (n == 0) { 567 while (w-- > 0) 568 addch(' '); 569 return; 570 } 571 snprintf(b, sizeof b, "%*llu", w, n); 572 if (strlen(b) > w) { 573 while (w-- > 0) 574 addch('*'); 575 return; 576 } 577 addstr(b); 578 } 579 580 void 581 putfloat(double f, int l, int c, int w, int d, int nz) 582 { 583 char b[128]; 584 585 move(l, c); 586 if (nz && f == 0.0) { 587 while (--w >= 0) 588 addch(' '); 589 return; 590 } 591 snprintf(b, sizeof b, "%*.*f", w, d, f); 592 if (strlen(b) > w) { 593 while (--w >= 0) 594 addch('*'); 595 return; 596 } 597 addstr(b); 598 } 599 600 static void 601 getinfo(struct Info *si) 602 { 603 static int cp_time_mib[] = { CTL_KERN, KERN_CPTIME }; 604 static int nchstats_mib[2] = { CTL_KERN, KERN_NCHSTATS }; 605 static int uvmexp_mib[2] = { CTL_VM, VM_UVMEXP }; 606 static int vmtotal_mib[2] = { CTL_VM, VM_METER }; 607 int mib[4], i; 608 size_t size; 609 610 dkreadstats(); 611 612 for (i = 0; i < nintr; i++) { 613 mib[0] = CTL_KERN; 614 mib[1] = KERN_INTRCNT; 615 mib[2] = KERN_INTRCNT_CNT; 616 mib[3] = i; 617 size = sizeof(si->intrcnt[i]); 618 if (sysctl(mib, 4, &si->intrcnt[i], &size, NULL, 0) < 0) { 619 si->intrcnt[i] = 0; 620 } 621 } 622 623 size = sizeof(si->time); 624 if (sysctl(cp_time_mib, 2, &si->time, &size, NULL, 0) < 0) { 625 error("Can't get KERN_CPTIME: %s\n", strerror(errno)); 626 bzero(&si->time, sizeof(si->time)); 627 } 628 629 size = sizeof(si->nchstats); 630 if (sysctl(nchstats_mib, 2, &si->nchstats, &size, NULL, 0) < 0) { 631 error("Can't get KERN_NCHSTATS: %s\n", strerror(errno)); 632 bzero(&si->nchstats, sizeof(si->nchstats)); 633 } 634 635 size = sizeof(si->uvmexp); 636 if (sysctl(uvmexp_mib, 2, &si->uvmexp, &size, NULL, 0) < 0) { 637 error("Can't get VM_UVMEXP: %s\n", strerror(errno)); 638 bzero(&si->uvmexp, sizeof(si->uvmexp)); 639 } 640 641 size = sizeof(si->Total); 642 if (sysctl(vmtotal_mib, 2, &si->Total, &size, NULL, 0) < 0) { 643 error("Can't get VM_METER: %s\n", strerror(errno)); 644 bzero(&si->Total, sizeof(si->Total)); 645 } 646 } 647 648 static void 649 allocinfo(struct Info *si) 650 { 651 memset(si, 0, sizeof(*si)); 652 si->intrcnt = (u_quad_t *) calloc(nintr, sizeof(u_quad_t)); 653 if (si->intrcnt == NULL) 654 errx(2, "out of memory"); 655 } 656 657 static void 658 copyinfo(struct Info *from, struct Info *to) 659 { 660 u_quad_t *intrcnt; 661 662 intrcnt = to->intrcnt; 663 *to = *from; 664 bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (u_quad_t)); 665 } 666 667 static void 668 dinfo(int dn, int c) 669 { 670 double words, atime; 671 672 c += DISKCOL; 673 674 /* time busy in disk activity */ 675 atime = (double)cur.dk_time[dn].tv_sec + 676 ((double)cur.dk_time[dn].tv_usec / (double)1000000); 677 678 /* # of K transferred */ 679 words = (cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) / 1024.0; 680 681 putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5); 682 putint((int)((float)(cur.dk_rxfer[dn] + cur.dk_wxfer[dn])/etime+0.5), 683 DISKROW + 2, c, 5); 684 putintmk((int)(words/etime + 0.5), DISKROW + 3, c, 5); 685 putfloat(atime/etime, DISKROW + 4, c, 5, 1, 1); 686 } 687 688 689 690 int 691 select_vm(void) 692 { 693 num_disp = 0; 694 return (0); 695 } 696 697 int 698 read_vm(void) 699 { 700 if (state == TIME) 701 copyinfo(&s3, &s1); 702 fetchkre(); 703 fetchifstat(); 704 if (state == TIME) 705 dkswap(); 706 num_disp = 0; 707 return 0; 708 } 709 710 711 void 712 print_vm(void) 713 { 714 copyinfo(&s3, &s); 715 labelkre(); 716 showkre(); 717 } 718