vmstat.c revision 1.32
1/* $OpenBSD: vmstat.c,v 1.32 2002/02/16 00:18:09 tdeval 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 39static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 1/12/94"; 40#endif 41static char rcsid[] = "$OpenBSD: vmstat.c,v 1.32 2002/02/16 00:18:09 tdeval 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 79static 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" 89extern 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 97static enum state { BOOT, TIME, RUN } state = TIME; 98 99static void allocinfo __P((struct Info *)); 100static void copyinfo __P((struct Info *, struct Info *)); 101static float cputime __P((int)); 102static void dinfo __P((int, int)); 103static void getinfo __P((struct Info *, enum state)); 104static void putint __P((int, int, int, int)); 105static void putfloat __P((double, int, int, int, int, int)); 106static int ucount __P((void)); 107 108static int ut; 109static char buf[26]; 110static time_t t; 111static double etime; 112static float hertz; 113static int nintr; 114static long *intrloc; 115static char **intrname; 116static int nextintsrow; 117 118struct utmp utmp; 119 120WINDOW * 121openkre() 122{ 123 124 ut = open(_PATH_UTMP, O_RDONLY); 125 if (ut < 0) 126 error("No utmp"); 127 return (stdscr); 128} 129 130void 131closekre(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 143static 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 45 /* max space for drives */ 186 187int 188initkre() 189{ 190 char *intrnamebuf, *cp; 191 int i, ret; 192 193 if (namelist[0].n_type == 0) { 194 if ((ret = kvm_nlist(kd, namelist)) == -1) 195 errx(1, "%s", kvm_geterr(kd)); 196 else if (ret) 197 nlisterr(namelist); 198 if (namelist[0].n_type == 0) { 199 error("No namelist"); 200 return(0); 201 } 202 } 203 hertz = stathz ? stathz : hz; 204 if (!dkinit(1)) 205 return(0); 206 if (nintr == 0) { 207#if defined(__i386__) 208 struct intrhand *intrhand[16], *ihp, ih; 209 char iname[16]; 210 int namelen, n; 211 212 NREAD(X_INTRHAND, intrhand, sizeof(intrhand)); 213 for (namelen = 0, i = 0; i < 16; i++) { 214 ihp = intrhand[i]; 215 while (ihp) { 216 nintr++; 217 KREAD(ihp, &ih, sizeof(ih)); 218 KREAD(ih.ih_what, iname, 16); 219 namelen += 1 + strlen(iname); 220 ihp = ih.ih_next; 221 } 222 } 223 intrloc = calloc(nintr, sizeof (long)); 224 intrname = calloc(nintr, sizeof (char *)); 225 cp = intrnamebuf = malloc(namelen); 226 for (namelen = 0, i = 0, n = 0; i < 16; i++) { 227 ihp = intrhand[i]; 228 while (ihp) { 229 KREAD(ihp, &ih, sizeof(ih)); 230 KREAD(ih.ih_what, iname, 16); 231 /* XXX strcpy is safe, sized & malloc'd buffer */ 232 strcpy(intrname[n++] = intrnamebuf + namelen, iname); 233 namelen += 1 + strlen(iname); 234 ihp = ih.ih_next; 235 } 236 } 237#else 238 nintr = (namelist[X_EINTRCNT].n_value - 239 namelist[X_INTRCNT].n_value) / sizeof (long); 240 intrloc = calloc(nintr, sizeof (long)); 241 intrname = calloc(nintr, sizeof (long)); 242 intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value - 243 namelist[X_INTRNAMES].n_value); 244 if (intrnamebuf == 0 || intrname == 0 || intrloc == 0) { 245 error("Out of memory\n"); 246 if (intrnamebuf) 247 free(intrnamebuf); 248 if (intrname) 249 free(intrname); 250 if (intrloc) 251 free(intrloc); 252 nintr = 0; 253 return(0); 254 } 255 NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) - 256 NVAL(X_INTRNAMES)); 257 for (cp = intrnamebuf, i = 0; i < nintr; i++) { 258 intrname[i] = cp; 259 cp += strlen(cp) + 1; 260 } 261#endif 262 nextintsrow = INTSROW + 2; 263 allocinfo(&s); 264 allocinfo(&s1); 265 allocinfo(&s2); 266 allocinfo(&z); 267 } 268 getinfo(&s2, RUN); 269 copyinfo(&s2, &s1); 270 return(1); 271} 272 273void 274fetchkre() 275{ 276 time_t now; 277 278 time(&now); 279 strlcpy(buf, ctime(&now), sizeof buf); 280 getinfo(&s, state); 281} 282 283void 284labelkre() 285{ 286 int i, j, l; 287 288 clear(); 289 mvprintw(STATROW, STATCOL + 4, "users Load"); 290 mvprintw(MEMROW, MEMCOL, " memory totals (in KB)"); 291 mvprintw(MEMROW + 1, MEMCOL, " real virtual free"); 292 mvprintw(MEMROW + 2, MEMCOL, "Active"); 293 mvprintw(MEMROW + 3, MEMCOL, "All"); 294 295 mvprintw(PAGEROW, PAGECOL, " PAGING SWAPPING "); 296 mvprintw(PAGEROW + 1, PAGECOL, " in out in out "); 297 mvprintw(PAGEROW + 2, PAGECOL, "ops"); 298 mvprintw(PAGEROW + 3, PAGECOL, "pages"); 299 300 mvprintw(INTSROW, INTSCOL + 3, " Interrupts"); 301 mvprintw(INTSROW + 1, INTSCOL + 9, "total"); 302 303 mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks"); 304 mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw"); 305 mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm"); 306 mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait"); 307 mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck"); 308 mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok"); 309 mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram"); 310 mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy"); 311 mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp"); 312 mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod"); 313 mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow"); 314 mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin"); 315 mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg"); 316 mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg"); 317 mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired"); 318 mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre"); 319 if (LINES - 1 > VMSTATROW + 16) 320 mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn"); 321 322 mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt"); 323 324 mvprintw(GRAPHROW, GRAPHCOL, 325 " . %% Sys . %% User . %% Nice . %% Idle"); 326 mvprintw(PROCSROW, PROCSCOL, "Proc:r d s w"); 327 mvprintw(GRAPHROW + 1, GRAPHCOL, 328 "| | | | | | | | | | |"); 329 330 mvprintw(NAMEIROW, NAMEICOL, 331 "Namei Sys-cache Proc-cache No-cache"); 332 mvprintw(NAMEIROW + 1, NAMEICOL, 333 " Calls hits %% hits %% miss %%"); 334 mvprintw(DISKROW, DISKCOL, "Discs"); 335 mvprintw(DISKROW + 1, DISKCOL, "seeks"); 336 mvprintw(DISKROW + 2, DISKCOL, "xfers"); 337 mvprintw(DISKROW + 3, DISKCOL, "Kbyte"); 338 mvprintw(DISKROW + 4, DISKCOL, " sec"); 339 for (i = 0, j = 0; i < dk_ndrive && j < DRIVESPACE; i++) 340 if (dk_select[i] && (j + strlen(dr_name[i])) < DRIVESPACE) { 341 l = MAX(4, strlen(dr_name[i])); 342 mvprintw(DISKROW, DISKCOL + 5 + j, 343 " %*s", l, dr_name[i]); 344 j += 1 + l; 345 } 346 for (i = 0; i < nintr; i++) { 347 if (intrloc[i] == 0) 348 continue; 349 mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]); 350 } 351} 352 353#define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;} 354#define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;} 355#define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \ 356 if(state == TIME) s1.nchstats.fld = t;} 357#define PUTRATE(fld, l, c, w) \ 358 Y(fld); \ 359 putint((int)((float)s.fld/etime + 0.5), l, c, w) 360#define MAXFAIL 5 361 362static char cpuchar[CPUSTATES] = { '=' , '>', '-', ' ' }; 363static char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_IDLE }; 364 365void 366showkre() 367{ 368 float f1, f2; 369 int psiz, inttotal; 370 int i, l, c; 371 static int failcnt = 0; 372 373 if (state == TIME) 374 dkswap(); 375 etime = 0; 376 for(i = 0; i < CPUSTATES; i++) { 377 X(time); 378 etime += s.time[i]; 379 } 380 if (etime < 5.0) { /* < 5 ticks - ignore this trash */ 381 if (failcnt++ >= MAXFAIL) { 382 clear(); 383 mvprintw(2, 10, "The alternate system clock has died!"); 384 mvprintw(3, 10, "Reverting to ``pigs'' display."); 385 move(CMDLINE, 0); 386 refresh(); 387 failcnt = 0; 388 sleep(5); 389 command("pigs"); 390 } 391 return; 392 } 393 failcnt = 0; 394 etime /= hertz; 395 inttotal = 0; 396 for (i = 0; i < nintr; i++) { 397 if (s.intrcnt[i] == 0) 398 continue; 399 if (intrloc[i] == 0) { 400 if (nextintsrow == LINES) 401 continue; 402 intrloc[i] = nextintsrow++; 403 mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", 404 intrname[i]); 405 } 406 X(intrcnt); 407 l = (int)((float)s.intrcnt[i]/etime + 0.5); 408 inttotal += l; 409 putint(l, intrloc[i], INTSCOL, 8); 410 } 411 putint(inttotal, INTSROW + 1, INTSCOL, 8); 412 Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss); 413 Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes); 414 s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits + 415 nchtotal.ncs_miss + nchtotal.ncs_long; 416 if (state == TIME) 417 s1.nchcount = s.nchcount; 418 419 psiz = 0; 420 f2 = 0.0; 421 422 /* 423 * Last CPU state not calculated yet. 424 */ 425 for (c = 0; c < CPUSTATES - 1; c++) { 426 i = cpuorder[c]; 427 f1 = cputime(i); 428 f2 += f1; 429 l = (int) ((f2 + 1.0) / 2.0) - psiz; 430 if (c == 0) 431 putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0); 432 else 433 putfloat(f1, GRAPHROW, GRAPHCOL + 12 * c, 434 5, 1, 0); 435 move(GRAPHROW + 2, psiz); 436 psiz += l; 437 while (l-- > 0) 438 addch(cpuchar[c]); 439 } 440 441 /* 442 * The above code does not account for time in the CP_INTR state. 443 * Thus the total may be less than 100%. If the total is less than 444 * the previous total old data may be left on the graph. The graph 445 * assumes one character position for every 2 percentage points for 446 * a total of 50 positions. Ensure all positions have been filled. 447 */ 448 while ( psiz++ <= 50 ) 449 addch(' '); 450 451 putint(ucount(), STATROW, STATCOL, 3); 452 putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0); 453 putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0); 454 putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0); 455 mvaddstr(STATROW, STATCOL + 53, buf); 456#define pgtokb(pg) ((pg) * s.uvmexp.pagesize / 1024) 457 458 putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 6, 7); 459 putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse), /* XXX */ 460 MEMROW + 2, MEMCOL + 16, 7); 461 putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 6, 7); 462 putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse), 463 MEMROW + 3, MEMCOL + 16, 7); 464 putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 24, 7); 465 putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse), 466 MEMROW + 3, MEMCOL + 24, 7); 467 putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3); 468 469 putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3); 470 putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3); 471 putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3); 472 PUTRATE(uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6); 473 PUTRATE(uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6); 474 PUTRATE(uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6); 475 PUTRATE(uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5); 476 PUTRATE(uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6); 477 PUTRATE(uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6); 478 PUTRATE(uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6); 479 PUTRATE(uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6); 480 PUTRATE(uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6); 481 PUTRATE(uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6); 482 PUTRATE(uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9); 483 putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9); 484 putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9); 485 putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9); 486 putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9); 487 PUTRATE(uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9); 488 if (LINES - 1 > VMSTATROW + 16) 489 PUTRATE(uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9); 490 491 PUTRATE(uvmexp.pageins, PAGEROW + 2, PAGECOL + 5, 5); 492 PUTRATE(uvmexp.pdpageouts, PAGEROW + 2, PAGECOL + 10, 5); 493 PUTRATE(uvmexp.swapins, PAGEROW + 2, PAGECOL + 15, 5); 494 PUTRATE(uvmexp.swapouts, PAGEROW + 2, PAGECOL + 20, 5); 495 PUTRATE(uvmexp.pgswapin, PAGEROW + 3, PAGECOL + 5, 5); 496 PUTRATE(uvmexp.pgswapout, PAGEROW + 3, PAGECOL + 10, 5); 497 498 PUTRATE(uvmexp.swtch, GENSTATROW + 1, GENSTATCOL, 6); 499 PUTRATE(uvmexp.traps, GENSTATROW + 1, GENSTATCOL + 6, 6); 500 PUTRATE(uvmexp.syscalls, GENSTATROW + 1, GENSTATCOL + 12, 6); 501 PUTRATE(uvmexp.intrs, GENSTATROW + 1, GENSTATCOL + 18, 6); 502 PUTRATE(uvmexp.softs, GENSTATROW + 1, GENSTATCOL + 24, 6); 503 PUTRATE(uvmexp.faults, GENSTATROW + 1, GENSTATCOL + 30, 5); 504 mvprintw(DISKROW, DISKCOL + 5, " "); 505 for (i = 0, c = 0; i < dk_ndrive && c < DRIVESPACE; i++) 506 if (dk_select[i] && (c + strlen(dr_name[i])) < DRIVESPACE) { 507 l = MAX(4, strlen(dr_name[i])); 508 mvprintw(DISKROW, DISKCOL + 5 + c, 509 " %*s", l, dr_name[i]); 510 c += 1 + l; 511 dinfo(i, c); 512 } 513 /* and pad the DRIVESPACE */ 514 l = DRIVESPACE - c; 515 for (i = 0; i < 5; i++) 516 mvprintw(DISKROW + i, DISKCOL + 5 + c, "%*s", l, ""); 517 518 putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9); 519 putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 10, 8); 520#define nz(x) ((x) ? (x) : 1) 521 putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount), 522 NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1); 523 putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 24, 7); 524 putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount), 525 NAMEIROW + 2, NAMEICOL + 33, 4, 0, 1); 526 putint(nchtotal.ncs_miss - nchtotal.ncs_pass2, 527 NAMEIROW + 2, NAMEICOL + 38, 7); 528 putfloat((nchtotal.ncs_miss - nchtotal.ncs_pass2) * 529 100.0 / nz(s.nchcount), NAMEIROW + 2, NAMEICOL + 45, 4, 0, 1); 530#undef nz 531} 532 533int 534cmdkre(cmd, args) 535 char *cmd, *args; 536{ 537 538 if (prefix(cmd, "run")) { 539 copyinfo(&s2, &s1); 540 state = RUN; 541 return (1); 542 } 543 if (prefix(cmd, "boot")) { 544 state = BOOT; 545 copyinfo(&z, &s1); 546 return (1); 547 } 548 if (prefix(cmd, "time")) { 549 state = TIME; 550 return (1); 551 } 552 if (prefix(cmd, "zero")) { 553 if (state == RUN) 554 getinfo(&s1, RUN); 555 return (1); 556 } 557 return (dkcmd(cmd, args)); 558} 559 560/* calculate number of users on the system */ 561static int 562ucount() 563{ 564 int nusers = 0; 565 566 if (ut < 0) 567 return (0); 568 while (read(ut, &utmp, sizeof(utmp))) 569 if (utmp.ut_name[0] != '\0') 570 nusers++; 571 572 lseek(ut, 0, SEEK_SET); 573 return (nusers); 574} 575 576static float 577cputime(indx) 578 int indx; 579{ 580 double t; 581 int i; 582 583 t = 0; 584 for (i = 0; i < CPUSTATES; i++) 585 t += s.time[i]; 586 if (t == 0.0) 587 t = 1.0; 588 return (s.time[indx] * 100.0 / t); 589} 590 591static void 592putint(n, l, c, w) 593 int n, l, c, w; 594{ 595 char b[128]; 596 597 move(l, c); 598 if (n == 0) { 599 while (w-- > 0) 600 addch(' '); 601 return; 602 } 603 snprintf(b, sizeof b, "%*d", w, n); 604 if (strlen(b) > w) { 605 while (w-- > 0) 606 addch('*'); 607 return; 608 } 609 addstr(b); 610} 611 612static void 613putfloat(f, l, c, w, d, nz) 614 double f; 615 int l, c, w, d, nz; 616{ 617 char b[128]; 618 619 move(l, c); 620 if (nz && f == 0.0) { 621 while (--w >= 0) 622 addch(' '); 623 return; 624 } 625 snprintf(b, sizeof b, "%*.*f", w, d, f); 626 if (strlen(b) > w) { 627 while (--w >= 0) 628 addch('*'); 629 return; 630 } 631 addstr(b); 632} 633 634static void 635getinfo(s, st) 636 struct Info *s; 637 enum state st; 638{ 639 static int cp_time_mib[] = { CTL_KERN, KERN_CPTIME }; 640 static int nchstats_mib[2] = { CTL_KERN, KERN_NCHSTATS }; 641 static int uvmexp_mib[2] = { CTL_VM, VM_UVMEXP }; 642 static int vmtotal_mib[2] = { CTL_VM, VM_METER }; 643 size_t size; 644#if defined(__i386__) 645 struct intrhand *intrhand[16], *ihp, ih; 646 int i, n; 647#endif 648 649 dkreadstats(); 650#if defined(__i386__) 651 NREAD(X_INTRHAND, intrhand, sizeof(intrhand)); 652 for (i = 0, n = 0; i < 16; i++) { 653 ihp = intrhand[i]; 654 while (ihp) { 655 KREAD(ihp, &ih, sizeof(ih)); 656 s->intrcnt[n++] = ih.ih_count; 657 ihp = ih.ih_next; 658 } 659 } 660#else 661 NREAD(X_INTRCNT, s->intrcnt, nintr * sizeof(long)); 662#endif 663 size = sizeof(s->time); 664 if (sysctl(cp_time_mib, 2, &s->time, &size, NULL, 0) < 0) { 665 error("Can't get KERN_CPTIME: %s\n", strerror(errno)); 666 bzero(&s->time, sizeof(s->time)); 667 } 668 669 size = sizeof(s->nchstats); 670 if (sysctl(nchstats_mib, 2, &s->nchstats, &size, NULL, 0) < 0) { 671 error("Can't get KERN_NCHSTATS: %s\n", strerror(errno)); 672 bzero(&s->nchstats, sizeof(s->nchstats)); 673 } 674 675 size = sizeof(s->uvmexp); 676 if (sysctl(uvmexp_mib, 2, &s->uvmexp, &size, NULL, 0) < 0) { 677 error("Can't get VM_UVMEXP: %s\n", strerror(errno)); 678 bzero(&s->uvmexp, sizeof(s->uvmexp)); 679 } 680 681 size = sizeof(s->Total); 682 if (sysctl(vmtotal_mib, 2, &s->Total, &size, NULL, 0) < 0) { 683 error("Can't get VM_METER: %s\n", strerror(errno)); 684 bzero(&s->Total, sizeof(s->Total)); 685 } 686} 687 688static void 689allocinfo(s) 690 struct Info *s; 691{ 692 693 s->intrcnt = (long *) malloc(nintr * sizeof(long)); 694 if (s->intrcnt == NULL) 695 errx(2, "out of memory"); 696} 697 698static void 699copyinfo(from, to) 700 struct Info *from, *to; 701{ 702 long *intrcnt; 703 704 intrcnt = to->intrcnt; 705 *to = *from; 706 bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int)); 707} 708 709static void 710dinfo(dn, c) 711 int dn, c; 712{ 713 double words, atime; 714 715 c += DISKCOL; 716 717 /* time busy in disk activity */ 718 atime = (double)cur.dk_time[dn].tv_sec + 719 ((double)cur.dk_time[dn].tv_usec / (double)1000000); 720 721 words = cur.dk_bytes[dn] / 1024.0; /* # of K transferred */ 722 723 putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5); 724 putint((int)((float)cur.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5); 725 putint((int)(words/etime + 0.5), DISKROW + 3, c, 5); 726 putfloat(atime/etime, DISKROW + 4, c, 5, 1, 1); 727} 728