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