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