1 /*- 2 * Copyright (c) 1983, 1989, 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 #if 0 36 static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 1/12/94"; 37 #endif 38 static const char rcsid[] = 39 "$Id: vmstat.c,v 1.25 1998/06/09 04:17:29 imp Exp $"; 40 #endif /* not lint */ 41 42 /* 43 * Cursed vmstat -- from Robert Elz. 44 */ 45 46 #include <sys/param.h> 47 #include <sys/dkstat.h> 48 #include <sys/buf.h> 49 #include <sys/stat.h> 50 #include <sys/time.h> 51 #include <sys/proc.h> 52 #include <sys/uio.h> 53 #include <sys/namei.h> 54 #include <sys/sysctl.h> 55 #include <sys/vmmeter.h> 56 57 #include <vm/vm_param.h> 58 59 #include <ctype.h> 60 #include <err.h> 61 #include <nlist.h> 62 #include <paths.h> 63 #include <signal.h> 64 #include <stdlib.h> 65 #include <string.h> 66 #include <time.h> 67 #include <unistd.h> 68 #include <utmp.h> 69 #include "systat.h" 70 #include "extern.h" 71 72 static struct Info { 73 long time[CPUSTATES]; 74 struct vmmeter Cnt; 75 struct vmtotal Total; 76 long *dk_time; 77 long *dk_wds; 78 long *dk_seek; 79 long *dk_xfer; 80 int dk_busy; 81 struct nchstats nchstats; 82 long nchcount; 83 long *intrcnt; 84 int bufspace; 85 int desiredvnodes; 86 long numvnodes; 87 long freevnodes; 88 } s, s1, s2, z; 89 90 #define cnt s.Cnt 91 #define oldcnt s1.Cnt 92 #define total s.Total 93 #define nchtotal s.nchstats 94 #define oldnchtotal s1.nchstats 95 96 static enum state { BOOT, TIME, RUN } state = TIME; 97 98 static void allocinfo __P((struct Info *)); 99 static void copyinfo __P((struct Info *, struct Info *)); 100 static float cputime __P((int)); 101 static void dinfo __P((int, int)); 102 static void getinfo __P((struct Info *, enum state)); 103 static void putint __P((int, int, int, int)); 104 static void putfloat __P((double, int, int, int, int, int)); 105 static int ucount __P((void)); 106 107 static int ncpu; 108 static int ut; 109 static char buf[26]; 110 static time_t t; 111 static double etime; 112 static int nintr; 113 static long *intrloc; 114 static char **intrname; 115 static int nextintsrow; 116 117 struct utmp utmp; 118 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_CPTIME 0 145 { "_cp_time" }, 146 #define X_CNT 1 147 { "_cnt" }, 148 #define X_BUFFERSPACE 2 149 { "_bufspace" }, 150 #define X_DK_BUSY 3 151 { "_dk_busy" }, 152 #define X_DK_TIME 4 153 { "_dk_time" }, 154 #define X_DK_XFER 5 155 { "_dk_xfer" }, 156 #define X_DK_WDS 6 157 { "_dk_wds" }, 158 #define X_DK_SEEK 7 159 { "_dk_seek" }, 160 #define X_NCHSTATS 8 161 { "_nchstats" }, 162 #define X_INTRNAMES 9 163 { "_intrnames" }, 164 #define X_EINTRNAMES 10 165 { "_eintrnames" }, 166 #define X_INTRCNT 11 167 { "_intrcnt" }, 168 #define X_EINTRCNT 12 169 { "_eintrcnt" }, 170 #define X_DESIREDVNODES 13 171 { "_desiredvnodes" }, 172 #define X_NUMVNODES 14 173 { "_numvnodes" }, 174 #define X_FREEVNODES 15 175 { "_freevnodes" }, 176 { "" }, 177 }; 178 179 /* 180 * These constants define where the major pieces are laid out 181 */ 182 #define STATROW 0 /* uses 1 row and 68 cols */ 183 #define STATCOL 2 184 #define MEMROW 2 /* uses 4 rows and 31 cols */ 185 #define MEMCOL 0 186 #define PAGEROW 2 /* uses 4 rows and 26 cols */ 187 #define PAGECOL 46 188 #define INTSROW 6 /* uses all rows to bottom and 17 cols */ 189 #define INTSCOL 61 190 #define PROCSROW 7 /* uses 2 rows and 20 cols */ 191 #define PROCSCOL 0 192 #define GENSTATROW 7 /* uses 2 rows and 30 cols */ 193 #define GENSTATCOL 20 194 #define VMSTATROW 6 /* uses 17 rows and 12 cols */ 195 #define VMSTATCOL 48 196 #define GRAPHROW 10 /* uses 3 rows and 51 cols */ 197 #define GRAPHCOL 0 198 #define NAMEIROW 14 /* uses 3 rows and 38 cols */ 199 #define NAMEICOL 0 200 #define DISKROW 18 /* uses 5 rows and 50 cols (for 9 drives) */ 201 #define DISKCOL 0 202 203 #define DRIVESPACE 9 /* max # for space */ 204 205 #if DK_NDRIVE > DRIVESPACE 206 #define MAXDRIVES DRIVESPACE /* max # to display */ 207 #else 208 #define MAXDRIVES DK_NDRIVE /* max # to display */ 209 #endif 210 211 int 212 initkre() 213 { 214 char *intrnamebuf, *cp; 215 int i; 216 static int once = 0; 217 218 if (namelist[0].n_type == 0) { 219 if (kvm_nlist(kd, namelist)) { 220 nlisterr(namelist); 221 return(0); 222 } 223 if (namelist[0].n_type == 0) { 224 error("No namelist"); 225 return(0); 226 } 227 } 228 if (! dkinit()) 229 return(0); 230 if (dk_ndrive && !once) { 231 #define allocate(e, t) \ 232 s./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \ 233 s1./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \ 234 s2./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \ 235 z./**/e = (t *)calloc(dk_ndrive, sizeof (t)); 236 allocate(dk_time, long); 237 allocate(dk_wds, long); 238 allocate(dk_seek, long); 239 allocate(dk_xfer, long); 240 once = 1; 241 #undef allocate 242 } 243 if (nintr == 0) { 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 nextintsrow = INTSROW + 2; 268 allocinfo(&s); 269 allocinfo(&s1); 270 allocinfo(&s2); 271 allocinfo(&z); 272 } 273 getinfo(&s2, RUN); 274 copyinfo(&s2, &s1); 275 return(1); 276 } 277 278 void 279 fetchkre() 280 { 281 time_t now; 282 struct tm *tp; 283 284 time(&now); 285 tp = localtime(&now); 286 (void) strftime(buf, sizeof(buf), "%c", tp); 287 buf[16] = '\0'; 288 getinfo(&s, state); 289 } 290 291 void 292 labelkre() 293 { 294 register int i, j; 295 296 clear(); 297 mvprintw(STATROW, STATCOL + 4, "users Load"); 298 mvprintw(MEMROW, MEMCOL, "Mem:KB REAL VIRTUAL"); 299 mvprintw(MEMROW + 1, MEMCOL, " Tot Share Tot Share"); 300 mvprintw(MEMROW + 2, MEMCOL, "Act"); 301 mvprintw(MEMROW + 3, MEMCOL, "All"); 302 303 mvprintw(MEMROW + 1, MEMCOL + 41, "Free"); 304 305 mvprintw(PAGEROW, PAGECOL, " VN PAGER SWAP PAGER "); 306 mvprintw(PAGEROW + 1, PAGECOL, " in out in out "); 307 mvprintw(PAGEROW + 2, PAGECOL, "count"); 308 mvprintw(PAGEROW + 3, PAGECOL, "pages"); 309 310 mvprintw(INTSROW, INTSCOL + 3, " Interrupts"); 311 mvprintw(INTSROW + 1, INTSCOL + 9, "total"); 312 313 mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "cow"); 314 mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "zfod"); 315 mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "wire"); 316 mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "act"); 317 mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "inact"); 318 mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "cache"); 319 mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "free"); 320 mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "daefr"); 321 mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "prcfr"); 322 mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "react"); 323 mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "pdwake"); 324 mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "pdpgs"); 325 mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "intrn"); 326 mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "buf"); 327 328 mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "desiredvnodes"); 329 mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "numvnodes"); 330 mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "freevnodes"); 331 332 mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt"); 333 334 mvprintw(GRAPHROW, GRAPHCOL, 335 " . %%Sys . %%Intr . %%User . %%Nice . %%Idle"); 336 mvprintw(PROCSROW, PROCSCOL, "Proc:r p d s w"); 337 mvprintw(GRAPHROW + 1, GRAPHCOL, 338 "| | | | | | | | | | |"); 339 340 mvprintw(NAMEIROW, NAMEICOL, "Namei Name-cache Dir-cache"); 341 mvprintw(NAMEIROW + 1, NAMEICOL, 342 " Calls hits %% hits %%"); 343 mvprintw(DISKROW, DISKCOL, "Discs"); 344 mvprintw(DISKROW + 1, DISKCOL, "seeks"); 345 mvprintw(DISKROW + 2, DISKCOL, "xfers"); 346 mvprintw(DISKROW + 3, DISKCOL, " blks"); 347 mvprintw(DISKROW + 4, DISKCOL, " msps"); 348 j = 0; 349 for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++) 350 if (dk_select[i]) { 351 mvprintw(DISKROW, DISKCOL + 5 + 5 * j, 352 " %4.4s", dr_name[j]); 353 j++; 354 } 355 for (i = 0; i < nintr; i++) { 356 if (intrloc[i] == 0) 357 continue; 358 mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s", intrname[i]); 359 } 360 } 361 362 #define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;} 363 #define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;} 364 #define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \ 365 if(state == TIME) s1.nchstats.fld = t;} 366 #define PUTRATE(fld, l, c, w) \ 367 Y(fld); \ 368 putint((int)((float)s.fld/etime + 0.5), l, c, w) 369 #define MAXFAIL 5 370 371 static char cpuchar[CPUSTATES] = { '=' , '+', '>', '-', ' ' }; 372 static char cpuorder[CPUSTATES] = { CP_SYS, CP_INTR, CP_USER, CP_NICE, 373 CP_IDLE }; 374 375 void 376 showkre() 377 { 378 float f1, f2; 379 int psiz, inttotal; 380 int i, l, c; 381 static int failcnt = 0; 382 383 for (i = 0; i < dk_ndrive; i++) { 384 X(dk_xfer); X(dk_seek); X(dk_wds); X(dk_time); 385 } 386 etime = 0; 387 for(i = 0; i < CPUSTATES; i++) { 388 X(time); 389 etime += s.time[i]; 390 } 391 if (etime < 5.0) { /* < 5 ticks - ignore this trash */ 392 if (failcnt++ >= MAXFAIL) { 393 clear(); 394 mvprintw(2, 10, "The alternate system clock has died!"); 395 mvprintw(3, 10, "Reverting to ``pigs'' display."); 396 move(CMDLINE, 0); 397 refresh(); 398 failcnt = 0; 399 sleep(5); 400 command("pigs"); 401 } 402 return; 403 } 404 failcnt = 0; 405 etime /= hertz; 406 etime /= ncpu; 407 inttotal = 0; 408 for (i = 0; i < nintr; i++) { 409 if (s.intrcnt[i] == 0) 410 continue; 411 if (intrloc[i] == 0) { 412 if (nextintsrow == LINES) 413 continue; 414 intrloc[i] = nextintsrow++; 415 mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s", 416 intrname[i]); 417 } 418 X(intrcnt); 419 l = (int)((float)s.intrcnt[i]/etime + 0.5); 420 inttotal += l; 421 putint(l, intrloc[i], INTSCOL + 2, 6); 422 } 423 putint(inttotal, INTSROW + 1, INTSCOL + 2, 6); 424 Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss); 425 Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes); Z(ncs_neghits); 426 s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits + 427 nchtotal.ncs_miss + nchtotal.ncs_long + nchtotal.ncs_neghits; 428 if (state == TIME) 429 s1.nchcount = s.nchcount; 430 431 psiz = 0; 432 f2 = 0.0; 433 for (c = 0; c < CPUSTATES; c++) { 434 i = cpuorder[c]; 435 f1 = cputime(i); 436 f2 += f1; 437 l = (int) ((f2 + 1.0) / 2.0) - psiz; 438 if (f1 > 99.9) 439 f1 = 99.9; /* no room to display 100.0 */ 440 putfloat(f1, GRAPHROW, GRAPHCOL + 10 * c, 4, 1, 0); 441 move(GRAPHROW + 2, psiz); 442 psiz += l; 443 while (l-- > 0) 444 addch(cpuchar[c]); 445 } 446 447 putint(ucount(), STATROW, STATCOL, 3); 448 putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0); 449 putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0); 450 putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0); 451 mvaddstr(STATROW, STATCOL + 53, buf); 452 #define pgtokb(pg) ((pg) * cnt.v_page_size / 1024) 453 putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 8); 454 putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 11, 8); 455 putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 19, 9); 456 putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 28, 9); 457 putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 8); 458 putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 11, 8); 459 putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 19, 9); 460 putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 28, 9); 461 putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 37, 8); 462 putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3); 463 putint(total.t_pw, PROCSROW + 1, PROCSCOL + 6, 3); 464 putint(total.t_dw, PROCSROW + 1, PROCSCOL + 9, 3); 465 putint(total.t_sl, PROCSROW + 1, PROCSCOL + 12, 3); 466 putint(total.t_sw, PROCSROW + 1, PROCSCOL + 15, 3); 467 PUTRATE(Cnt.v_cow_faults, VMSTATROW + 0, VMSTATCOL + 3, 6); 468 PUTRATE(Cnt.v_zfod, VMSTATROW + 1, VMSTATCOL + 4, 5); 469 putint(pgtokb(cnt.v_wire_count), VMSTATROW + 2, VMSTATCOL, 9); 470 putint(pgtokb(cnt.v_active_count), VMSTATROW + 3, VMSTATCOL, 9); 471 putint(pgtokb(cnt.v_inactive_count), VMSTATROW + 4, VMSTATCOL, 9); 472 putint(pgtokb(cnt.v_cache_count), VMSTATROW + 5, VMSTATCOL, 9); 473 putint(pgtokb(cnt.v_free_count), VMSTATROW + 6, VMSTATCOL, 9); 474 PUTRATE(Cnt.v_dfree, VMSTATROW + 7, VMSTATCOL, 9); 475 PUTRATE(Cnt.v_pfree, VMSTATROW + 8, VMSTATCOL, 9); 476 PUTRATE(Cnt.v_reactivated, VMSTATROW + 9, VMSTATCOL, 9); 477 PUTRATE(Cnt.v_pdwakeups, VMSTATROW + 10, VMSTATCOL, 9); 478 PUTRATE(Cnt.v_pdpages, VMSTATROW + 11, VMSTATCOL, 9); 479 PUTRATE(Cnt.v_intrans, VMSTATROW + 12, VMSTATCOL, 9); 480 putint(s.bufspace/1024, VMSTATROW + 13, VMSTATCOL, 9); 481 putint(s.desiredvnodes, VMSTATROW + 14, VMSTATCOL, 9); 482 putint(s.numvnodes, VMSTATROW + 15, VMSTATCOL, 9); 483 putint(s.freevnodes, VMSTATROW + 16, VMSTATCOL, 9); 484 PUTRATE(Cnt.v_vnodein, PAGEROW + 2, PAGECOL + 5, 5); 485 PUTRATE(Cnt.v_vnodeout, PAGEROW + 2, PAGECOL + 10, 5); 486 PUTRATE(Cnt.v_swapin, PAGEROW + 2, PAGECOL + 17, 5); 487 PUTRATE(Cnt.v_swapout, PAGEROW + 2, PAGECOL + 22, 5); 488 PUTRATE(Cnt.v_vnodepgsin, PAGEROW + 3, PAGECOL + 5, 5); 489 PUTRATE(Cnt.v_vnodepgsout, PAGEROW + 3, PAGECOL + 10, 5); 490 PUTRATE(Cnt.v_swappgsin, PAGEROW + 3, PAGECOL + 17, 5); 491 PUTRATE(Cnt.v_swappgsout, PAGEROW + 3, PAGECOL + 22, 5); 492 PUTRATE(Cnt.v_swtch, GENSTATROW + 1, GENSTATCOL, 5); 493 PUTRATE(Cnt.v_trap, GENSTATROW + 1, GENSTATCOL + 5, 5); 494 PUTRATE(Cnt.v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 5); 495 PUTRATE(Cnt.v_intr, GENSTATROW + 1, GENSTATCOL + 15, 5); 496 PUTRATE(Cnt.v_soft, GENSTATROW + 1, GENSTATCOL + 20, 5); 497 PUTRATE(Cnt.v_vm_faults, GENSTATROW + 1, GENSTATCOL + 25, 5); 498 mvprintw(DISKROW, DISKCOL + 5, " "); 499 for (i = 0, c = 0; i < dk_ndrive && c < MAXDRIVES; i++) 500 if (dk_select[i]) { 501 mvprintw(DISKROW, DISKCOL + 5 + 5 * c, 502 " %4.4s", dr_name[i]); 503 dinfo(i, ++c); 504 } 505 putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9); 506 putint((nchtotal.ncs_goodhits + nchtotal.ncs_neghits), 507 NAMEIROW + 2, NAMEICOL + 9, 9); 508 #define nz(x) ((x) ? (x) : 1) 509 putfloat((nchtotal.ncs_goodhits+nchtotal.ncs_neghits) * 510 100.0 / nz(s.nchcount), 511 NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1); 512 putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9); 513 putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount), 514 NAMEIROW + 2, NAMEICOL + 33, 4, 0, 1); 515 #undef nz 516 } 517 518 int 519 cmdkre(cmd, args) 520 char *cmd, *args; 521 { 522 523 if (prefix(cmd, "run")) { 524 copyinfo(&s2, &s1); 525 state = RUN; 526 return (1); 527 } 528 if (prefix(cmd, "boot")) { 529 state = BOOT; 530 copyinfo(&z, &s1); 531 return (1); 532 } 533 if (prefix(cmd, "time")) { 534 state = TIME; 535 return (1); 536 } 537 if (prefix(cmd, "zero")) { 538 if (state == RUN) 539 getinfo(&s1, RUN); 540 return (1); 541 } 542 return (dkcmd(cmd, args)); 543 } 544 545 /* calculate number of users on the system */ 546 static int 547 ucount() 548 { 549 register int nusers = 0; 550 551 if (ut < 0) 552 return (0); 553 while (read(ut, &utmp, sizeof(utmp))) 554 if (utmp.ut_name[0] != '\0') 555 nusers++; 556 557 lseek(ut, 0L, L_SET); 558 return (nusers); 559 } 560 561 static float 562 cputime(indx) 563 int indx; 564 { 565 double t; 566 register int i; 567 568 t = 0; 569 for (i = 0; i < CPUSTATES; i++) 570 t += s.time[i]; 571 if (t == 0.0) 572 t = 1.0; 573 return (s.time[indx] * 100.0 / t); 574 } 575 576 static void 577 putint(n, l, c, w) 578 int n, l, c, w; 579 { 580 char b[128]; 581 582 move(l, c); 583 if (n == 0) { 584 while (w-- > 0) 585 addch(' '); 586 return; 587 } 588 snprintf(b, sizeof(b), "%*d", w, n); 589 if (strlen(b) > w) { 590 while (w-- > 0) 591 addch('*'); 592 return; 593 } 594 addstr(b); 595 } 596 597 static void 598 putfloat(f, l, c, w, d, nz) 599 double f; 600 int l, c, w, d, nz; 601 { 602 char b[128]; 603 604 move(l, c); 605 if (nz && f == 0.0) { 606 while (--w >= 0) 607 addch(' '); 608 return; 609 } 610 snprintf(b, sizeof(b), "%*.*f", w, d, f); 611 if (strlen(b) > w) { 612 while (--w >= 0) 613 addch('*'); 614 return; 615 } 616 addstr(b); 617 } 618 619 static void 620 getinfo(s, st) 621 struct Info *s; 622 enum state st; 623 { 624 int mib[2], size; 625 extern int errno; 626 627 NREAD(X_CPTIME, s->time, sizeof s->time); 628 NREAD(X_CNT, &s->Cnt, sizeof s->Cnt); 629 NREAD(X_BUFFERSPACE, &s->bufspace, sizeof(s->bufspace)); 630 NREAD(X_DESIREDVNODES, &s->desiredvnodes, sizeof(s->desiredvnodes)); 631 NREAD(X_NUMVNODES, &s->numvnodes, LONG); 632 NREAD(X_FREEVNODES, &s->freevnodes, LONG); 633 NREAD(X_DK_BUSY, &s->dk_busy, sizeof(s->dk_busy)); 634 NREAD(X_DK_TIME, s->dk_time, dk_ndrive * LONG); 635 NREAD(X_DK_XFER, s->dk_xfer, dk_ndrive * LONG); 636 NREAD(X_DK_WDS, s->dk_wds, dk_ndrive * LONG); 637 NREAD(X_DK_SEEK, s->dk_seek, dk_ndrive * LONG); 638 NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats); 639 NREAD(X_INTRCNT, s->intrcnt, nintr * LONG); 640 size = sizeof(s->Total); 641 mib[0] = CTL_VM; 642 mib[1] = VM_METER; 643 if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) { 644 error("Can't get kernel info: %s\n", strerror(errno)); 645 bzero(&s->Total, sizeof(s->Total)); 646 } 647 size = sizeof(ncpu); 648 if (sysctlbyname("hw.ncpu", &ncpu, &size, NULL, 0) < 0) 649 ncpu = 1; 650 } 651 652 static void 653 allocinfo(s) 654 struct Info *s; 655 { 656 657 s->intrcnt = (long *) calloc(nintr, sizeof(long)); 658 if (s->intrcnt == NULL) 659 errx(2, "out of memory"); 660 } 661 662 static void 663 copyinfo(from, to) 664 register struct Info *from, *to; 665 { 666 long *time, *wds, *seek, *xfer; 667 long *intrcnt; 668 669 /* 670 * time, wds, seek, and xfer are malloc'd so we have to 671 * save the pointers before the structure copy and then 672 * copy by hand. 673 */ 674 time = to->dk_time; wds = to->dk_wds; seek = to->dk_seek; 675 xfer = to->dk_xfer; intrcnt = to->intrcnt; 676 *to = *from; 677 bcopy(from->dk_time, to->dk_time = time, dk_ndrive * sizeof (long)); 678 bcopy(from->dk_wds, to->dk_wds = wds, dk_ndrive * sizeof (long)); 679 bcopy(from->dk_seek, to->dk_seek = seek, dk_ndrive * sizeof (long)); 680 bcopy(from->dk_xfer, to->dk_xfer = xfer, dk_ndrive * sizeof (long)); 681 bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int)); 682 } 683 684 static void 685 dinfo(dn, c) 686 int dn, c; 687 { 688 double words, atime, itime, xtime; 689 690 c = DISKCOL + c * 5; 691 atime = s.dk_time[dn]; 692 atime /= hertz; 693 words = s.dk_wds[dn]*32.0; /* number of words transferred */ 694 xtime = dk_mspw[dn]*words; /* transfer time */ 695 itime = atime - xtime; /* time not transferring */ 696 if (xtime < 0) 697 itime += xtime, xtime = 0; 698 if (itime < 0) 699 xtime += itime, itime = 0; 700 putint((int)((float)s.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5); 701 putint((int)((float)s.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5); 702 putint((int)(words/etime/512.0 + 0.5), DISKROW + 3, c, 5); 703 if (s.dk_seek[dn]) 704 putfloat(itime*1000.0/s.dk_seek[dn], DISKROW + 4, c, 5, 1, 1); 705 else 706 putint(0, DISKROW + 4, c, 5); 707 } 708