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