vmstat.c revision 74595
1218887Sdim/*- 2218887Sdim * Copyright (c) 1983, 1989, 1992, 1993 3218887Sdim * The Regents of the University of California. All rights reserved. 4218887Sdim * 5218887Sdim * Redistribution and use in source and binary forms, with or without 6218887Sdim * modification, are permitted provided that the following conditions 7218887Sdim * are met: 8218887Sdim * 1. Redistributions of source code must retain the above copyright 9218887Sdim * notice, this list of conditions and the following disclaimer. 10218887Sdim * 2. Redistributions in binary form must reproduce the above copyright 11218887Sdim * notice, this list of conditions and the following disclaimer in the 12218887Sdim * documentation and/or other materials provided with the distribution. 13218887Sdim * 3. All advertising materials mentioning features or use of this software 14218887Sdim * must display the following acknowledgement: 15218887Sdim * This product includes software developed by the University of 16219077Sdim * California, Berkeley and its contributors. 17219077Sdim * 4. Neither the name of the University nor the names of its contributors 18218887Sdim * may be used to endorse or promote products derived from this software 19218887Sdim * without specific prior written permission. 20218887Sdim * 21218887Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22218887Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23219077Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24219077Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25219077Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26219077Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27218887Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28218887Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29218887Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30218887Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31218887Sdim * SUCH DAMAGE. 32218887Sdim */ 33218887Sdim 34218887Sdim#ifndef lint 35218887Sdim#if 0 36218887Sdimstatic char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 1/12/94"; 37218887Sdim#endif 38218887Sdimstatic const char rcsid[] = 39218887Sdim "$FreeBSD: head/usr.bin/systat/vmstat.c 74595 2001-03-21 20:32:20Z ache $"; 40218887Sdim#endif /* not lint */ 41218887Sdim 42219077Sdim/* 43218887Sdim * Cursed vmstat -- from Robert Elz. 44218887Sdim */ 45218887Sdim 46218887Sdim#include <sys/param.h> 47218887Sdim#include <sys/stat.h> 48219077Sdim#include <sys/time.h> 49219077Sdim#include <sys/proc.h> 50218887Sdim#include <sys/uio.h> 51218887Sdim#include <sys/namei.h> 52218887Sdim#include <sys/sysctl.h> 53218887Sdim#include <sys/dkstat.h> 54218887Sdim#include <sys/vmmeter.h> 55218887Sdim 56219077Sdim#include <vm/vm_param.h> 57219077Sdim 58219077Sdim#include <ctype.h> 59219077Sdim#include <err.h> 60219077Sdim#include <errno.h> 61219077Sdim#include <langinfo.h> 62219077Sdim#include <nlist.h> 63219077Sdim#include <paths.h> 64219077Sdim#include <signal.h> 65219077Sdim#include <stdlib.h> 66219077Sdim#include <string.h> 67219077Sdim#include <time.h> 68219077Sdim#include <unistd.h> 69219077Sdim#include <utmp.h> 70219077Sdim#include <devstat.h> 71219077Sdim#include "systat.h" 72219077Sdim#include "extern.h" 73219077Sdim#include "devs.h" 74218887Sdim 75219077Sdimstatic struct Info { 76219077Sdim long time[CPUSTATES]; 77219077Sdim struct vmmeter Cnt; 78219077Sdim struct vmtotal Total; 79219077Sdim struct nchstats nchstats; 80219077Sdim long nchcount; 81219077Sdim long *intrcnt; 82219077Sdim int bufspace; 83219077Sdim int desiredvnodes; 84219077Sdim long numvnodes; 85219077Sdim long freevnodes; 86219077Sdim long numdirtybuffers; 87219077Sdim} s, s1, s2, z; 88219077Sdim 89219077Sdimstruct statinfo cur, last, run; 90219077Sdim 91219077Sdim#define cnt s.Cnt 92219077Sdim#define oldcnt s1.Cnt 93219077Sdim#define total s.Total 94219077Sdim#define nchtotal s.nchstats 95218887Sdim#define oldnchtotal s1.nchstats 96218887Sdim 97219077Sdimstatic enum state { BOOT, TIME, RUN } state = TIME; 98219077Sdim 99219077Sdimstatic void allocinfo __P((struct Info *)); 100219077Sdimstatic void copyinfo __P((struct Info *, struct Info *)); 101219077Sdimstatic float cputime __P((int)); 102219077Sdimstatic void dinfo __P((int, int, struct statinfo *, struct statinfo *)); 103219077Sdimstatic void getinfo __P((struct Info *, enum state)); 104219077Sdimstatic void putint __P((int, int, int, int)); 105219077Sdimstatic void putfloat __P((double, int, int, int, int, int)); 106219077Sdimstatic void putlongdouble __P((long double, int, int, int, int, int)); 107219077Sdimstatic int ucount __P((void)); 108219077Sdim 109219077Sdimstatic int ncpu; 110219077Sdimstatic int ut; 111219077Sdimstatic char buf[26]; 112219077Sdimstatic time_t t; 113219077Sdimstatic double etime; 114219077Sdimstatic int nintr; 115219077Sdimstatic long *intrloc; 116219077Sdimstatic char **intrname; 117219077Sdimstatic int nextintsrow; 118219077Sdimstatic int extended_vm_stats; 119219077Sdim 120219077Sdimstruct utmp utmp; 121219077Sdim 122219077Sdim 123219077SdimWINDOW * 124219077Sdimopenkre() 125219077Sdim{ 126219077Sdim 127219077Sdim ut = open(_PATH_UTMP, O_RDONLY); 128219077Sdim if (ut < 0) 129219077Sdim error("No utmp"); 130219077Sdim return (stdscr); 131219077Sdim} 132219077Sdim 133219077Sdimvoid 134219077Sdimclosekre(w) 135219077Sdim WINDOW *w; 136219077Sdim{ 137219077Sdim 138219077Sdim (void) close(ut); 139219077Sdim if (w == NULL) 140219077Sdim return; 141219077Sdim wclear(w); 142219077Sdim wrefresh(w); 143219077Sdim} 144219077Sdim 145219077Sdim 146219077Sdimstatic struct nlist namelist[] = { 147219077Sdim#define X_CNT 0 148219077Sdim { "_cnt" }, 149219077Sdim#define X_INTRNAMES 1 150219077Sdim { "_intrnames" }, 151219077Sdim#define X_EINTRNAMES 2 152219077Sdim { "_eintrnames" }, 153219077Sdim#define X_INTRCNT 3 154219077Sdim { "_intrcnt" }, 155219077Sdim#define X_EINTRCNT 4 156219077Sdim { "_eintrcnt" }, 157219077Sdim { "" }, 158219077Sdim}; 159219077Sdim 160219077Sdim/* 161219077Sdim * These constants define where the major pieces are laid out 162219077Sdim */ 163219077Sdim#define STATROW 0 /* uses 1 row and 68 cols */ 164219077Sdim#define STATCOL 2 165219077Sdim#define MEMROW 2 /* uses 4 rows and 31 cols */ 166219077Sdim#define MEMCOL 0 167219077Sdim#define PAGEROW 2 /* uses 4 rows and 26 cols */ 168219077Sdim#define PAGECOL 46 169219077Sdim#define INTSROW 6 /* uses all rows to bottom and 17 cols */ 170219077Sdim#define INTSCOL 61 171219077Sdim#define PROCSROW 7 /* uses 2 rows and 20 cols */ 172219077Sdim#define PROCSCOL 0 173219077Sdim#define GENSTATROW 7 /* uses 2 rows and 30 cols */ 174219077Sdim#define GENSTATCOL 20 175219077Sdim#define VMSTATROW 6 /* uses 17 rows and 12 cols */ 176219077Sdim#define VMSTATCOL 48 177219077Sdim#define GRAPHROW 10 /* uses 3 rows and 51 cols */ 178219077Sdim#define GRAPHCOL 0 179219077Sdim#define NAMEIROW 14 /* uses 3 rows and 38 cols */ 180219077Sdim#define NAMEICOL 0 181219077Sdim#define DISKROW 18 /* uses 5 rows and 50 cols (for 9 drives) */ 182219077Sdim#define DISKCOL 0 183219077Sdim 184219077Sdim#define DRIVESPACE 7 /* max # for space */ 185219077Sdim 186219077Sdim#define MAXDRIVES DRIVESPACE /* max # to display */ 187219077Sdim 188219077Sdimint 189219077Sdiminitkre() 190219077Sdim{ 191219077Sdim char *intrnamebuf, *cp; 192219077Sdim int i; 193219077Sdim 194219077Sdim if (namelist[0].n_type == 0) { 195219077Sdim if (kvm_nlist(kd, namelist)) { 196219077Sdim nlisterr(namelist); 197219077Sdim return(0); 198219077Sdim } 199219077Sdim if (namelist[0].n_type == 0) { 200219077Sdim error("No namelist"); 201219077Sdim return(0); 202219077Sdim } 203219077Sdim } 204219077Sdim 205219077Sdim if (num_devices = getnumdevs() < 0) { 206219077Sdim warnx("%s", devstat_errbuf); 207219077Sdim return(0); 208219077Sdim } 209219077Sdim 210219077Sdim cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 211219077Sdim last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 212219077Sdim run.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 213219077Sdim bzero(cur.dinfo, sizeof(struct devinfo)); 214219077Sdim bzero(last.dinfo, sizeof(struct devinfo)); 215219077Sdim bzero(run.dinfo, sizeof(struct devinfo)); 216219077Sdim 217219077Sdim if (dsinit(MAXDRIVES, &cur, &last, &run) != 1) 218219077Sdim return(0); 219219077Sdim 220219077Sdim if (nintr == 0) { 221219077Sdim nintr = (namelist[X_EINTRCNT].n_value - 222219077Sdim namelist[X_INTRCNT].n_value) / sizeof (long); 223219077Sdim intrloc = calloc(nintr, sizeof (long)); 224219077Sdim intrname = calloc(nintr, sizeof (long)); 225219077Sdim intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value - 226219077Sdim namelist[X_INTRNAMES].n_value); 227219077Sdim if (intrnamebuf == 0 || intrname == 0 || intrloc == 0) { 228219077Sdim error("Out of memory\n"); 229219077Sdim if (intrnamebuf) 230219077Sdim free(intrnamebuf); 231219077Sdim if (intrname) 232219077Sdim free(intrname); 233219077Sdim if (intrloc) 234219077Sdim free(intrloc); 235219077Sdim nintr = 0; 236219077Sdim return(0); 237219077Sdim } 238219077Sdim NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) - 239219077Sdim NVAL(X_INTRNAMES)); 240219077Sdim for (cp = intrnamebuf, i = 0; i < nintr; i++) { 241219077Sdim intrname[i] = cp; 242219077Sdim cp += strlen(cp) + 1; 243219077Sdim } 244219077Sdim nextintsrow = INTSROW + 2; 245219077Sdim allocinfo(&s); 246219077Sdim allocinfo(&s1); 247219077Sdim allocinfo(&s2); 248219077Sdim allocinfo(&z); 249219077Sdim } 250219077Sdim getinfo(&s2, RUN); 251219077Sdim copyinfo(&s2, &s1); 252219077Sdim return(1); 253219077Sdim} 254219077Sdim 255219077Sdimvoid 256219077Sdimfetchkre() 257219077Sdim{ 258219077Sdim time_t now; 259219077Sdim struct tm *tp; 260219077Sdim static int d_first = -1; 261219077Sdim 262219077Sdim if (d_first < 0) 263219077Sdim d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 264219077Sdim 265219077Sdim time(&now); 266219077Sdim tp = localtime(&now); 267219077Sdim (void) strftime(buf, sizeof(buf), 268219077Sdim d_first ? "%e %b %R" : "%b %e %R", tp); 269219077Sdim getinfo(&s, state); 270219077Sdim} 271219077Sdim 272219077Sdimvoid 273219077Sdimlabelkre() 274219077Sdim{ 275219077Sdim register int i, j; 276219077Sdim 277219077Sdim clear(); 278219077Sdim mvprintw(STATROW, STATCOL + 4, "users Load"); 279219077Sdim mvprintw(MEMROW, MEMCOL, "Mem:KB REAL VIRTUAL"); 280219077Sdim mvprintw(MEMROW + 1, MEMCOL, " Tot Share Tot Share"); 281219077Sdim mvprintw(MEMROW + 2, MEMCOL, "Act"); 282219077Sdim mvprintw(MEMROW + 3, MEMCOL, "All"); 283219077Sdim 284219077Sdim mvprintw(MEMROW + 1, MEMCOL + 41, "Free"); 285219077Sdim 286219077Sdim mvprintw(PAGEROW, PAGECOL, " VN PAGER SWAP PAGER "); 287219077Sdim mvprintw(PAGEROW + 1, PAGECOL, " in out in out "); 288219077Sdim mvprintw(PAGEROW + 2, PAGECOL, "count"); 289219077Sdim mvprintw(PAGEROW + 3, PAGECOL, "pages"); 290219077Sdim 291219077Sdim mvprintw(INTSROW, INTSCOL + 3, " Interrupts"); 292219077Sdim mvprintw(INTSROW + 1, INTSCOL + 9, "total"); 293219077Sdim 294219077Sdim mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "cow"); 295219077Sdim mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "wire"); 296219077Sdim mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "act"); 297219077Sdim mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "inact"); 298219077Sdim mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "cache"); 299219077Sdim mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "free"); 300219077Sdim mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "daefr"); 301219077Sdim mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "prcfr"); 302219077Sdim mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "react"); 303219077Sdim mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "pdwake"); 304219077Sdim mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "pdpgs"); 305219077Sdim mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "intrn"); 306219077Sdim mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "buf"); 307219077Sdim mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "dirtybuf"); 308219077Sdim 309219077Sdim mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "desiredvnodes"); 310219077Sdim mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "numvnodes"); 311219077Sdim mvprintw(VMSTATROW + 17, VMSTATCOL + 10, "freevnodes"); 312219077Sdim 313219077Sdim mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt"); 314219077Sdim 315219077Sdim mvprintw(GRAPHROW, GRAPHCOL, 316219077Sdim " . %%Sys . %%Intr . %%User . %%Nice . %%Idle"); 317219077Sdim mvprintw(PROCSROW, PROCSCOL, "Proc:r p d s w"); 318219077Sdim mvprintw(GRAPHROW + 1, GRAPHCOL, 319219077Sdim "| | | | | | | | | | |"); 320219077Sdim 321219077Sdim mvprintw(NAMEIROW, NAMEICOL, "Namei Name-cache Dir-cache"); 322219077Sdim mvprintw(NAMEIROW + 1, NAMEICOL, 323219077Sdim " Calls hits %% hits %%"); 324219077Sdim mvprintw(DISKROW, DISKCOL, "Disks"); 325219077Sdim mvprintw(DISKROW + 1, DISKCOL, "KB/t"); 326219077Sdim mvprintw(DISKROW + 2, DISKCOL, "tps"); 327219077Sdim mvprintw(DISKROW + 3, DISKCOL, "MB/s"); 328219077Sdim mvprintw(DISKROW + 4, DISKCOL, "%% busy"); 329219077Sdim /* 330219077Sdim * For now, we don't support a fourth disk statistic. So there's 331219077Sdim * no point in providing a label for it. If someone can think of a 332219077Sdim * fourth useful disk statistic, there is room to add it. 333219077Sdim */ 334219077Sdim /* mvprintw(DISKROW + 4, DISKCOL, " msps"); */ 335219077Sdim j = 0; 336219077Sdim for (i = 0; i < num_devices && j < MAXDRIVES; i++) 337219077Sdim if (dev_select[i].selected) { 338219077Sdim char tmpstr[80]; 339218887Sdim sprintf(tmpstr, "%s%d", dev_select[i].device_name, 340219077Sdim dev_select[i].unit_number); 341218887Sdim mvprintw(DISKROW, DISKCOL + 5 + 6 * j, 342218887Sdim " %5.5s", tmpstr); 343218887Sdim j++; 344219077Sdim } 345219077Sdim 346218887Sdim if (j <= 4) { 347218887Sdim /* 348219077Sdim * room for extended VM stats 349219077Sdim */ 350219077Sdim mvprintw(VMSTATROW + 11, VMSTATCOL - 6, "zfod"); 351219077Sdim mvprintw(VMSTATROW + 12, VMSTATCOL - 6, "ofod"); 352219077Sdim mvprintw(VMSTATROW + 13, VMSTATCOL - 6, "%%slo-z"); 353219077Sdim mvprintw(VMSTATROW + 14, VMSTATCOL - 6, "tfree"); 354219077Sdim extended_vm_stats = 1; 355219077Sdim } else { 356218887Sdim extended_vm_stats = 0; 357219077Sdim mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "zfod"); 358219077Sdim } 359219077Sdim 360219077Sdim for (i = 0; i < nintr; i++) { 361219077Sdim if (intrloc[i] == 0) 362218887Sdim continue; 363219077Sdim mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s", intrname[i]); 364219077Sdim } 365219077Sdim} 366219077Sdim 367219077Sdim#define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;} 368219077Sdim#define Q(fld) {t=cur.fld[i]; cur.fld[i]-=last.fld[i]; if(state==TIME) last.fld[i]=t;} 369219077Sdim#define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;} 370219077Sdim#define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \ 371219077Sdim if(state == TIME) s1.nchstats.fld = t;} 372219077Sdim#define PUTRATE(fld, l, c, w) \ 373219077Sdim Y(fld); \ 374219077Sdim putint((int)((float)s.fld/etime + 0.5), l, c, w) 375219077Sdim#define MAXFAIL 5 376219077Sdim 377219077Sdimstatic char cpuchar[CPUSTATES] = { '=' , '+', '>', '-', ' ' }; 378219077Sdimstatic char cpuorder[CPUSTATES] = { CP_SYS, CP_INTR, CP_USER, CP_NICE, 379219077Sdim CP_IDLE }; 380219077Sdim 381219077Sdimvoid 382219077Sdimshowkre() 383219077Sdim{ 384219077Sdim float f1, f2; 385219077Sdim int psiz, inttotal; 386219077Sdim int i, l, c; 387219077Sdim static int failcnt = 0; 388219077Sdim 389219077Sdim etime = 0; 390219077Sdim for(i = 0; i < CPUSTATES; i++) { 391219077Sdim X(time); 392219077Sdim Q(cp_time); 393219077Sdim etime += s.time[i]; 394219077Sdim } 395219077Sdim if (etime < 5.0) { /* < 5 ticks - ignore this trash */ 396219077Sdim if (failcnt++ >= MAXFAIL) { 397219077Sdim clear(); 398219077Sdim mvprintw(2, 10, "The alternate system clock has died!"); 399219077Sdim mvprintw(3, 10, "Reverting to ``pigs'' display."); 400219077Sdim move(CMDLINE, 0); 401219077Sdim refresh(); 402219077Sdim failcnt = 0; 403219077Sdim sleep(5); 404219077Sdim command("pigs"); 405219077Sdim } 406219077Sdim return; 407219077Sdim } 408219077Sdim failcnt = 0; 409219077Sdim etime /= hertz; 410219077Sdim etime /= ncpu; 411219077Sdim inttotal = 0; 412219077Sdim for (i = 0; i < nintr; i++) { 413219077Sdim if (s.intrcnt[i] == 0) 414219077Sdim continue; 415219077Sdim if (intrloc[i] == 0) { 416219077Sdim if (nextintsrow == LINES) 417219077Sdim continue; 418219077Sdim intrloc[i] = nextintsrow++; 419219077Sdim mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s", 420219077Sdim intrname[i]); 421218887Sdim } 422219077Sdim X(intrcnt); 423219077Sdim l = (int)((float)s.intrcnt[i]/etime + 0.5); 424219077Sdim inttotal += l; 425218887Sdim putint(l, intrloc[i], INTSCOL + 2, 6); 426218887Sdim } 427219077Sdim putint(inttotal, INTSROW + 1, INTSCOL + 2, 6); 428219077Sdim Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss); 429219077Sdim Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes); Z(ncs_neghits); 430219077Sdim s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits + 431219077Sdim nchtotal.ncs_miss + nchtotal.ncs_long + nchtotal.ncs_neghits; 432218887Sdim if (state == TIME) 433218887Sdim s1.nchcount = s.nchcount; 434219077Sdim 435219077Sdim psiz = 0; 436219077Sdim f2 = 0.0; 437 for (c = 0; c < CPUSTATES; c++) { 438 i = cpuorder[c]; 439 f1 = cputime(i); 440 f2 += f1; 441 l = (int) ((f2 + 1.0) / 2.0) - psiz; 442 if (f1 > 99.9) 443 f1 = 99.9; /* no room to display 100.0 */ 444 putfloat(f1, GRAPHROW, GRAPHCOL + 10 * c, 4, 1, 0); 445 move(GRAPHROW + 2, psiz); 446 psiz += l; 447 while (l-- > 0) 448 addch(cpuchar[c]); 449 } 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) * cnt.v_page_size / 1024) 457 putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 8); 458 putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 11, 8); 459 putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 19, 9); 460 putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 28, 9); 461 putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 8); 462 putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 11, 8); 463 putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 19, 9); 464 putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 28, 9); 465 putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 37, 8); 466 putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3); 467 putint(total.t_pw, PROCSROW + 1, PROCSCOL + 6, 3); 468 putint(total.t_dw, PROCSROW + 1, PROCSCOL + 9, 3); 469 putint(total.t_sl, PROCSROW + 1, PROCSCOL + 12, 3); 470 putint(total.t_sw, PROCSROW + 1, PROCSCOL + 15, 3); 471 if (extended_vm_stats == 0) { 472 PUTRATE(Cnt.v_zfod, VMSTATROW + 0, VMSTATCOL + 4, 5); 473 } 474 PUTRATE(Cnt.v_cow_faults, VMSTATROW + 1, VMSTATCOL + 3, 6); 475 putint(pgtokb(cnt.v_wire_count), VMSTATROW + 2, VMSTATCOL, 9); 476 putint(pgtokb(cnt.v_active_count), VMSTATROW + 3, VMSTATCOL, 9); 477 putint(pgtokb(cnt.v_inactive_count), VMSTATROW + 4, VMSTATCOL, 9); 478 putint(pgtokb(cnt.v_cache_count), VMSTATROW + 5, VMSTATCOL, 9); 479 putint(pgtokb(cnt.v_free_count), VMSTATROW + 6, VMSTATCOL, 9); 480 PUTRATE(Cnt.v_dfree, VMSTATROW + 7, VMSTATCOL, 9); 481 PUTRATE(Cnt.v_pfree, VMSTATROW + 8, VMSTATCOL, 9); 482 PUTRATE(Cnt.v_reactivated, VMSTATROW + 9, VMSTATCOL, 9); 483 PUTRATE(Cnt.v_pdwakeups, VMSTATROW + 10, VMSTATCOL, 9); 484 PUTRATE(Cnt.v_pdpages, VMSTATROW + 11, VMSTATCOL, 9); 485 PUTRATE(Cnt.v_intrans, VMSTATROW + 12, VMSTATCOL, 9); 486 487 if (extended_vm_stats) { 488 PUTRATE(Cnt.v_zfod, VMSTATROW + 11, VMSTATCOL - 16, 9); 489 PUTRATE(Cnt.v_ozfod, VMSTATROW + 12, VMSTATCOL - 16, 9); 490 putint( 491 ((s.Cnt.v_ozfod < s.Cnt.v_zfod) ? 492 s.Cnt.v_ozfod * 100 / s.Cnt.v_zfod : 493 0 494 ), 495 VMSTATROW + 13, 496 VMSTATCOL - 16, 497 9 498 ); 499 PUTRATE(Cnt.v_tfree, VMSTATROW + 14, VMSTATCOL - 16, 9); 500 } 501 502 putint(s.bufspace/1024, VMSTATROW + 13, VMSTATCOL, 9); 503 putint(s.numdirtybuffers, VMSTATROW + 14, VMSTATCOL, 9); 504 putint(s.desiredvnodes, VMSTATROW + 15, VMSTATCOL, 9); 505 putint(s.numvnodes, VMSTATROW + 16, VMSTATCOL, 9); 506 putint(s.freevnodes, VMSTATROW + 17, VMSTATCOL, 9); 507 PUTRATE(Cnt.v_vnodein, PAGEROW + 2, PAGECOL + 5, 5); 508 PUTRATE(Cnt.v_vnodeout, PAGEROW + 2, PAGECOL + 10, 5); 509 PUTRATE(Cnt.v_swapin, PAGEROW + 2, PAGECOL + 17, 5); 510 PUTRATE(Cnt.v_swapout, PAGEROW + 2, PAGECOL + 22, 5); 511 PUTRATE(Cnt.v_vnodepgsin, PAGEROW + 3, PAGECOL + 5, 5); 512 PUTRATE(Cnt.v_vnodepgsout, PAGEROW + 3, PAGECOL + 10, 5); 513 PUTRATE(Cnt.v_swappgsin, PAGEROW + 3, PAGECOL + 17, 5); 514 PUTRATE(Cnt.v_swappgsout, PAGEROW + 3, PAGECOL + 22, 5); 515 PUTRATE(Cnt.v_swtch, GENSTATROW + 1, GENSTATCOL, 5); 516 PUTRATE(Cnt.v_trap, GENSTATROW + 1, GENSTATCOL + 5, 5); 517 PUTRATE(Cnt.v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 5); 518 PUTRATE(Cnt.v_intr, GENSTATROW + 1, GENSTATCOL + 15, 5); 519 PUTRATE(Cnt.v_soft, GENSTATROW + 1, GENSTATCOL + 20, 5); 520 PUTRATE(Cnt.v_vm_faults, GENSTATROW + 1, GENSTATCOL + 25, 5); 521 mvprintw(DISKROW, DISKCOL + 5, " "); 522 for (i = 0, c = 0; i < num_devices && c < MAXDRIVES; i++) 523 if (dev_select[i].selected) { 524 char tmpstr[80]; 525 sprintf(tmpstr, "%s%d", dev_select[i].device_name, 526 dev_select[i].unit_number); 527 mvprintw(DISKROW, DISKCOL + 5 + 6 * c, 528 " %5.5s", tmpstr); 529 switch(state) { 530 case TIME: 531 dinfo(i, ++c, &cur, &last); 532 break; 533 case RUN: 534 dinfo(i, ++c, &cur, &run); 535 break; 536 case BOOT: 537 dinfo(i, ++c, &cur, NULL); 538 break; 539 } 540 } 541 putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9); 542 putint((nchtotal.ncs_goodhits + nchtotal.ncs_neghits), 543 NAMEIROW + 2, NAMEICOL + 9, 9); 544#define nz(x) ((x) ? (x) : 1) 545 putfloat((nchtotal.ncs_goodhits+nchtotal.ncs_neghits) * 546 100.0 / nz(s.nchcount), 547 NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1); 548 putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9); 549 putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount), 550 NAMEIROW + 2, NAMEICOL + 33, 4, 0, 1); 551#undef nz 552} 553 554int 555cmdkre(cmd, args) 556 char *cmd, *args; 557{ 558 int retval; 559 560 if (prefix(cmd, "run")) { 561 retval = 1; 562 copyinfo(&s2, &s1); 563 switch (getdevs(&run)) { 564 case -1: 565 errx(1, "%s", devstat_errbuf); 566 break; 567 case 1: 568 num_devices = run.dinfo->numdevs; 569 generation = run.dinfo->generation; 570 retval = dscmd("refresh", NULL, MAXDRIVES, &cur); 571 if (retval == 2) 572 labelkre(); 573 break; 574 default: 575 break; 576 } 577 state = RUN; 578 return (retval); 579 } 580 if (prefix(cmd, "boot")) { 581 state = BOOT; 582 copyinfo(&z, &s1); 583 return (1); 584 } 585 if (prefix(cmd, "time")) { 586 state = TIME; 587 return (1); 588 } 589 if (prefix(cmd, "zero")) { 590 retval = 1; 591 if (state == RUN) { 592 getinfo(&s1, RUN); 593 switch (getdevs(&run)) { 594 case -1: 595 errx(1, "%s", devstat_errbuf); 596 break; 597 case 1: 598 num_devices = run.dinfo->numdevs; 599 generation = run.dinfo->generation; 600 retval = dscmd("refresh",NULL, MAXDRIVES, &cur); 601 if (retval == 2) 602 labelkre(); 603 break; 604 default: 605 break; 606 } 607 } 608 return (retval); 609 } 610 retval = dscmd(cmd, args, MAXDRIVES, &cur); 611 612 if (retval == 2) 613 labelkre(); 614 615 return(retval); 616} 617 618/* calculate number of users on the system */ 619static int 620ucount() 621{ 622 register int nusers = 0; 623 624 if (ut < 0) 625 return (0); 626 while (read(ut, &utmp, sizeof(utmp))) 627 if (utmp.ut_name[0] != '\0') 628 nusers++; 629 630 lseek(ut, 0L, L_SET); 631 return (nusers); 632} 633 634static float 635cputime(indx) 636 int indx; 637{ 638 double t; 639 register int i; 640 641 t = 0; 642 for (i = 0; i < CPUSTATES; i++) 643 t += s.time[i]; 644 if (t == 0.0) 645 t = 1.0; 646 return (s.time[indx] * 100.0 / t); 647} 648 649static void 650putint(n, l, c, w) 651 int n, l, c, w; 652{ 653 char b[128]; 654 655 move(l, c); 656 if (n == 0) { 657 while (w-- > 0) 658 addch(' '); 659 return; 660 } 661 snprintf(b, sizeof(b), "%*d", w, n); 662 if (strlen(b) > w) { 663 while (w-- > 0) 664 addch('*'); 665 return; 666 } 667 addstr(b); 668} 669 670static void 671putfloat(f, l, c, w, d, nz) 672 double f; 673 int l, c, w, d, nz; 674{ 675 char b[128]; 676 677 move(l, c); 678 if (nz && f == 0.0) { 679 while (--w >= 0) 680 addch(' '); 681 return; 682 } 683 snprintf(b, sizeof(b), "%*.*f", w, d, f); 684 if (strlen(b) > w) 685 snprintf(b, sizeof(b), "%*.0f", w, f); 686 if (strlen(b) > w) { 687 while (--w >= 0) 688 addch('*'); 689 return; 690 } 691 addstr(b); 692} 693 694static void 695putlongdouble(f, l, c, w, d, nz) 696 long double f; 697 int l, c, w, d, nz; 698{ 699 char b[128]; 700 701 move(l, c); 702 if (nz && f == 0.0) { 703 while (--w >= 0) 704 addch(' '); 705 return; 706 } 707 sprintf(b, "%*.*Lf", w, d, f); 708 if (strlen(b) > w) 709 sprintf(b, "%*.0Lf", w, f); 710 if (strlen(b) > w) { 711 while (--w >= 0) 712 addch('*'); 713 return; 714 } 715 addstr(b); 716} 717 718static void 719getinfo(s, st) 720 struct Info *s; 721 enum state st; 722{ 723 struct devinfo *tmp_dinfo; 724 size_t len, size; 725 int mib[2], err; 726 727 len = sizeof(s->time); 728 err = sysctlbyname("kern.cp_time", &s->time, &len, NULL, 0); 729 if (err || len != sizeof(s->time)) 730 perror("kern.cp_time"); 731 732 len = sizeof(cur.cp_time); 733 err = sysctlbyname("kern.cp_time", &cur.cp_time, &len, NULL, 0); 734 if (err || len != sizeof(cur.cp_time)) 735 perror("kern.cp_time"); 736 737 NREAD(X_CNT, &s->Cnt, sizeof s->Cnt); 738 739 len = sizeof(s->bufspace); 740 err = sysctlbyname("vfs.bufspace", &s->bufspace, &len, NULL, 0); 741 if (err || len != sizeof(s->bufspace)) 742 perror("vfs.bufspace"); 743 744 len = sizeof(s->desiredvnodes); 745 err = sysctlbyname("kern.maxvnodes", &s->desiredvnodes, &len, NULL, 0); 746 if (err || len != sizeof(s->desiredvnodes)) 747 perror("kern.maxvnodes"); 748 749 len = sizeof(s->numvnodes); 750 err = sysctlbyname("debug.numvnodes", &s->numvnodes, &len, NULL, 0); 751 if (err || len != sizeof(s->numvnodes)) 752 perror("debug.numvnodes"); 753 754 len = sizeof(s->freevnodes); 755 err = sysctlbyname("debug.freevnodes", &s->freevnodes, &len, NULL, 0); 756 if (err || len != sizeof(s->freevnodes)) 757 perror("debug.freevnodes"); 758 759 len = sizeof(s->nchstats); 760 err = sysctlbyname("vfs.cache.nchstats", &s->nchstats, &len, NULL, 0); 761 if (err || len != sizeof(s->nchstats)) 762 perror("vfs.cache.nchstats"); 763 764 NREAD(X_INTRCNT, s->intrcnt, nintr * LONG); 765 766 len = sizeof(s->numdirtybuffers); 767 err = sysctlbyname("vfs.numdirtybuffers", &s->numdirtybuffers, &len, 768 NULL, 0); 769 770 size = sizeof(s->Total); 771 mib[0] = CTL_VM; 772 mib[1] = VM_METER; 773 if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) { 774 error("Can't get kernel info: %s\n", strerror(errno)); 775 bzero(&s->Total, sizeof(s->Total)); 776 } 777 size = sizeof(ncpu); 778 if (sysctlbyname("hw.ncpu", &ncpu, &size, NULL, 0) < 0 || 779 size != sizeof(ncpu)) 780 ncpu = 1; 781 782 tmp_dinfo = last.dinfo; 783 last.dinfo = cur.dinfo; 784 cur.dinfo = tmp_dinfo; 785 786 last.busy_time = cur.busy_time; 787 switch (getdevs(&cur)) { 788 case -1: 789 errx(1, "%s", devstat_errbuf); 790 break; 791 case 1: 792 num_devices = cur.dinfo->numdevs; 793 generation = cur.dinfo->generation; 794 cmdkre("refresh", NULL); 795 break; 796 default: 797 break; 798 } 799} 800 801static void 802allocinfo(s) 803 struct Info *s; 804{ 805 806 s->intrcnt = (long *) calloc(nintr, sizeof(long)); 807 if (s->intrcnt == NULL) 808 errx(2, "out of memory"); 809} 810 811static void 812copyinfo(from, to) 813 register struct Info *from, *to; 814{ 815 long *intrcnt; 816 817 /* 818 * time, wds, seek, and xfer are malloc'd so we have to 819 * save the pointers before the structure copy and then 820 * copy by hand. 821 */ 822 intrcnt = to->intrcnt; 823 *to = *from; 824 825 bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int)); 826} 827 828static void 829dinfo(dn, c, now, then) 830 int dn, c; 831 struct statinfo *now, *then; 832{ 833 long double transfers_per_second; 834 long double kb_per_transfer, mb_per_second; 835 long double elapsed_time, device_busy; 836 int di; 837 838 di = dev_select[dn].position; 839 840 elapsed_time = compute_etime(now->busy_time, then ? 841 then->busy_time : 842 now->dinfo->devices[di].dev_creation_time); 843 844 device_busy = compute_etime(now->dinfo->devices[di].busy_time, then ? 845 then->dinfo->devices[di].busy_time : 846 now->dinfo->devices[di].dev_creation_time); 847 848 if (compute_stats(&now->dinfo->devices[di], then ? 849 &then->dinfo->devices[di] : NULL, elapsed_time, 850 NULL, NULL, NULL, 851 &kb_per_transfer, &transfers_per_second, 852 &mb_per_second, NULL, NULL) != 0) 853 errx(1, "%s", devstat_errbuf); 854 855 if ((device_busy == 0) && (transfers_per_second > 5)) 856 /* the device has been 100% busy, fake it because 857 * as long as the device is 100% busy the busy_time 858 * field in the devstat struct is not updated */ 859 device_busy = elapsed_time; 860 if (device_busy > elapsed_time) 861 /* this normally happens after one or more periods 862 * where the device has been 100% busy, correct it */ 863 device_busy = elapsed_time; 864 865 c = DISKCOL + c * 6; 866 putlongdouble(kb_per_transfer, DISKROW + 1, c, 5, 2, 0); 867 putlongdouble(transfers_per_second, DISKROW + 2, c, 5, 0, 0); 868 putlongdouble(mb_per_second, DISKROW + 3, c, 5, 2, 0); 869 putlongdouble(device_busy * 100 / elapsed_time, DISKROW + 4, c, 5, 0, 0); 870} 871