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