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