vmstat.c revision 121291
1139804Simp/*- 23332Sphk * Copyright (c) 1983, 1989, 1992, 1993 33332Sphk * The Regents of the University of California. All rights reserved. 493149Sphk * 53332Sphk * Redistribution and use in source and binary forms, with or without 63332Sphk * modification, are permitted provided that the following conditions 73332Sphk * are met: 8139804Simp * 1. Redistributions of source code must retain the above copyright 9139804Simp * notice, this list of conditions and the following disclaimer. 10139804Simp * 2. Redistributions in binary form must reproduce the above copyright 113332Sphk * notice, this list of conditions and the following disclaimer in the 123784Sphk * documentation and/or other materials provided with the distribution. 133332Sphk * 3. All advertising materials mentioning features or use of this software 143332Sphk * must display the following acknowledgement: 153332Sphk * This product includes software developed by the University of 163332Sphk * California, Berkeley and its contributors. 173332Sphk * 4. Neither the name of the University nor the names of its contributors 183332Sphk * may be used to endorse or promote products derived from this software 193332Sphk * without specific prior written permission. 203332Sphk * 213417Scsgr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 223332Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 233332Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24116182Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25116182Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26116182Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 273784Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 283332Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 293332Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 303332Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 313332Sphk * SUCH DAMAGE. 3276166Smarkm */ 333507Scsgr 3476166Smarkm#include <sys/cdefs.h> 3515494Sbde 36220373Strasz__FBSDID("$FreeBSD: head/usr.bin/systat/vmstat.c 121291 2003-10-20 20:13:50Z phk $"); 373507Scsgr 383332Sphk#ifdef lint 393507Scsgrstatic const char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 1/12/94"; 4015494Sbde#endif 413784Sphk 423332Sphk/* 433332Sphk * Cursed vmstat -- from Robert Elz. 4412662Sdg */ 4512662Sdg 4612662Sdg#include <sys/param.h> 473332Sphk#include <sys/stat.h> 4812662Sdg#include <sys/time.h> 493332Sphk#include <sys/proc.h> 503784Sphk#include <sys/uio.h> 513784Sphk#include <sys/namei.h> 523784Sphk#include <sys/resource.h> 533784Sphk#include <sys/sysctl.h> 5448079Shoek#include <sys/vmmeter.h> 553784Sphk 563784Sphk#include <vm/vm_param.h> 573784Sphk 583784Sphk#include <ctype.h> 593784Sphk#include <err.h> 603784Sphk#include <errno.h> 613784Sphk#include <langinfo.h> 623784Sphk#include <nlist.h> 633784Sphk#include <paths.h> 6492723Salfred#include <signal.h> 6592723Salfred#include <stdlib.h> 6692723Salfred#include <string.h> 6792723Salfred#include <time.h> 683784Sphk#include <unistd.h> 6912568Sbde#include <utmp.h> 7012130Sdg#include <devstat.h> 7112130Sdg#include "systat.h" 723332Sphk#include "extern.h" 73231885Skib#include "devs.h" 7417974Sbde 753784Sphkstatic struct Info { 763784Sphk long time[CPUSTATES]; 7717386Sphk u_int v_swtch; /* context switches */ 783332Sphk u_int v_trap; /* calls to trap */ 793348Sphk u_int v_syscall; /* calls to syscall() */ 803784Sphk u_int v_intr; /* device interrupts */ 813784Sphk u_int v_soft; /* software interrupts */ 823784Sphk /* 833784Sphk * Virtual memory activity. 843784Sphk */ 853784Sphk u_int v_vm_faults; /* number of address memory faults */ 863784Sphk u_int v_cow_faults; /* number of copy-on-writes */ 873784Sphk u_int v_zfod; /* pages zero filled on demand */ 883332Sphk u_int v_ozfod; /* optimized zero fill pages */ 893784Sphk u_int v_swapin; /* swap pager pageins */ 903784Sphk u_int v_swapout; /* swap pager pageouts */ 913784Sphk u_int v_swappgsin; /* swap pager pages paged in */ 923348Sphk u_int v_swappgsout; /* swap pager pages paged out */ 933784Sphk u_int v_vnodein; /* vnode pager pageins */ 943784Sphk u_int v_vnodeout; /* vnode pager pageouts */ 953332Sphk u_int v_vnodepgsin; /* vnode_pager pages paged in */ 963348Sphk u_int v_vnodepgsout; /* vnode pager pages paged out */ 973784Sphk u_int v_intrans; /* intransit blocking page faults */ 983784Sphk u_int v_reactivated; /* number of pages reactivated from free list */ 993348Sphk u_int v_pdwakeups; /* number of times daemon has awaken from sleep */ 1003784Sphk u_int v_pdpages; /* number of pages analyzed by daemon */ 1013784Sphk 1023784Sphk u_int v_dfree; /* pages freed by daemon */ 1033784Sphk u_int v_pfree; /* pages freed by exiting processes */ 1043784Sphk u_int v_tfree; /* total pages freed */ 1053332Sphk /* 10612130Sdg * Distribution of page usages. 1073784Sphk */ 1083353Sphk u_int v_page_size; /* page size in bytes */ 1093784Sphk u_int v_free_count; /* number of pages free */ 1103784Sphk u_int v_wire_count; /* number of pages wired down */ 1113784Sphk u_int v_active_count; /* number of pages active */ 1123784Sphk u_int v_inactive_count; /* number of pages inactive */ 1133348Sphk u_int v_cache_count; /* number of pages on buffer cache queue */ 1143784Sphk struct vmtotal Total; 1153784Sphk struct nchstats nchstats; 1163784Sphk long nchcount; 1173784Sphk long *intrcnt; 1183348Sphk int bufspace; 11917386Sphk int desiredvnodes; 1203332Sphk long numvnodes; 1213784Sphk long freevnodes; 1223348Sphk int numdirtybuffers; 12348079Shoek} s, s1, s2, z; 12448079Shoek 12548079Shoekstruct statinfo cur, last, run; 12648079Shoek 12748079Shoek#define total s.Total 12848079Shoek#define nchtotal s.nchstats 12948079Shoek#define oldnchtotal s1.nchstats 13017386Sphk 13117386Sphkstatic enum state { BOOT, TIME, RUN } state = TIME; 13217386Sphk 13317386Sphkstatic void allocinfo(struct Info *); 13417386Sphkstatic void copyinfo(struct Info *, struct Info *); 13517386Sphkstatic float cputime(int); 13617386Sphkstatic void dinfo(int, int, struct statinfo *, struct statinfo *); 13717386Sphkstatic void getinfo(struct Info *); 13817386Sphkstatic void putint(int, int, int, int); 139231885Skibstatic void putfloat(double, int, int, int, int, int); 140231885Skibstatic void putlongdouble(long double, int, int, int, int, int); 141231885Skibstatic int ucount(void); 1423784Sphk 143231885Skibstatic int ncpu; 144231885Skibstatic int ut; 1453348Sphkstatic char buf[26]; 1463784Sphkstatic time_t t; 1473784Sphkstatic double etime; 1483784Sphkstatic int nintr; 1493784Sphkstatic long *intrloc; 1503784Sphkstatic char **intrname; 1513332Sphkstatic int nextintsrow; 1523332Sphkstatic int extended_vm_stats; 1533784Sphk 1543784Sphkstruct utmp utmp; 1553332Sphk 1563784Sphk 15724848SdysonWINDOW * 15814703Sbdeopenkre() 1593332Sphk{ 1603784Sphk 1613784Sphk ut = open(_PATH_UTMP, O_RDONLY); 1623784Sphk if (ut < 0) 1633784Sphk error("No utmp"); 164237694Simp return (stdscr); 1653332Sphk} 1663784Sphk 1673784Sphkvoid 16815538Sphkclosekre(w) 1693784Sphk WINDOW *w; 1703784Sphk{ 1713784Sphk 1723784Sphk (void) close(ut); 1733784Sphk if (w == NULL) 1743332Sphk return; 17515538Sphk wclear(w); 1763784Sphk wrefresh(w); 1773784Sphk} 1783332Sphk 1793784Sphk/* 180237694Simp * These constants define where the major pieces are laid out 1813784Sphk */ 1823784Sphk#define STATROW 0 /* uses 1 row and 68 cols */ 18315538Sphk#define STATCOL 2 1843784Sphk#define MEMROW 2 /* uses 4 rows and 31 cols */ 1853784Sphk#define MEMCOL 0 1863784Sphk#define PAGEROW 2 /* uses 4 rows and 26 cols */ 1873784Sphk#define PAGECOL 46 1883784Sphk#define INTSROW 6 /* uses all rows to bottom and 17 cols */ 1893784Sphk#define INTSCOL 61 1903332Sphk#define PROCSROW 7 /* uses 2 rows and 20 cols */ 1913332Sphk#define PROCSCOL 0 19215538Sphk#define GENSTATROW 7 /* uses 2 rows and 30 cols */ 1933332Sphk#define GENSTATCOL 20 1943784Sphk#define VMSTATROW 6 /* uses 17 rows and 12 cols */ 1953784Sphk#define VMSTATCOL 48 1963784Sphk#define GRAPHROW 10 /* uses 3 rows and 51 cols */ 1973784Sphk#define GRAPHCOL 0 1983784Sphk#define NAMEIROW 14 /* uses 3 rows and 38 cols */ 1993784Sphk#define NAMEICOL 0 2003332Sphk#define DISKROW 18 /* uses 5 rows and 50 cols (for 9 drives) */ 2013332Sphk#define DISKCOL 0 20215538Sphk 2033784Sphk#define DRIVESPACE 7 /* max # for space */ 2043784Sphk 2053784Sphk#define MAXDRIVES DRIVESPACE /* max # to display */ 2063784Sphk 2073784Sphkint 2083784Sphkinitkre() 209125454Sjhb{ 2103784Sphk char *intrnamebuf, *cp; 21184783Sps int i; 2123332Sphk size_t sz; 2133332Sphk 2143784Sphk if ((num_devices = devstat_getnumdevs(NULL)) < 0) { 215220373Strasz warnx("%s", devstat_errbuf); 216220373Strasz return(0); 217220373Strasz } 218125454Sjhb 2193784Sphk cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 2203784Sphk last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 2213784Sphk run.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 222125454Sjhb bzero(cur.dinfo, sizeof(struct devinfo)); 2233784Sphk bzero(last.dinfo, sizeof(struct devinfo)); 2243784Sphk bzero(run.dinfo, sizeof(struct devinfo)); 2253332Sphk 2263784Sphk if (dsinit(MAXDRIVES, &cur, &last, &run) != 1) 227153698Salc return(0); 228153698Salc 229153698Salc if (nintr == 0) { 230153698Salc if (sysctlbyname("hw.intrcnt", NULL, &sz, NULL, 0) == -1) { 231153698Salc error("sysctl(hw.intrcnt...) failed: %s", 232153698Salc strerror(errno)); 233153698Salc return (0); 234175294Sattilio } 235153698Salc nintr = sz / sizeof(u_long); 236153698Salc intrloc = calloc(nintr, sizeof (long)); 2373784Sphk intrname = calloc(nintr, sizeof (char *)); 2383784Sphk intrnamebuf = sysctl_dynread("hw.intrnames", NULL); 239173361Skib if (intrnamebuf == NULL || intrname == NULL || 2403348Sphk intrloc == NULL) { 241175202Sattilio error("Out of memory"); 242173361Skib if (intrnamebuf) 243173361Skib free(intrnamebuf); 244173361Skib if (intrname) 245173361Skib free(intrname); 246153698Salc if (intrloc) 24724848Sdyson free(intrloc); 24824848Sdyson nintr = 0; 2493784Sphk return(0); 2503332Sphk } 2513784Sphk for (cp = intrnamebuf, i = 0; i < nintr; i++) { 2523784Sphk intrname[i] = cp; 25317386Sphk cp += strlen(cp) + 1; 25417386Sphk } 255144501Sjhb nextintsrow = INTSROW + 2; 256144501Sjhb allocinfo(&s); 2573784Sphk allocinfo(&s1); 2583332Sphk allocinfo(&s2); 2593784Sphk allocinfo(&z); 2603784Sphk } 2613784Sphk getinfo(&s2); 2623784Sphk copyinfo(&s2, &s1); 26317386Sphk return(1); 2646579Sdg} 2656579Sdg 26614087Sphkvoid 26714087Sphkfetchkre() 26814087Sphk{ 2696579Sdg time_t now; 27014087Sphk struct tm *tp; 27114087Sphk static int d_first = -1; 27217386Sphk 27317386Sphk if (d_first < 0) 27417386Sphk d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 27517386Sphk 27617386Sphk time(&now); 27717386Sphk tp = localtime(&now); 2786579Sdg (void) strftime(buf, sizeof(buf), 2796579Sdg d_first ? "%e %b %R" : "%b %e %R", tp); 2806579Sdg getinfo(&s); 2816579Sdg} 2823784Sphk 2833784Sphkvoid 2843784Sphklabelkre() 2853784Sphk{ 28637656Sbde int i, j; 28737656Sbde 28837656Sbde clear(); 2893332Sphk mvprintw(STATROW, STATCOL + 4, "users Load"); 2903784Sphk mvprintw(MEMROW, MEMCOL, "Mem:KB REAL VIRTUAL"); 2913784Sphk mvprintw(MEMROW + 1, MEMCOL, " Tot Share Tot Share"); 2923784Sphk mvprintw(MEMROW + 2, MEMCOL, "Act"); 2933332Sphk mvprintw(MEMROW + 3, MEMCOL, "All"); 2943784Sphk 2953332Sphk mvprintw(MEMROW + 1, MEMCOL + 41, "Free"); 2963784Sphk 2973784Sphk mvprintw(PAGEROW, PAGECOL, " VN PAGER SWAP PAGER "); 2983332Sphk mvprintw(PAGEROW + 1, PAGECOL, " in out in out "); 2993784Sphk mvprintw(PAGEROW + 2, PAGECOL, "count"); 3003784Sphk mvprintw(PAGEROW + 3, PAGECOL, "pages"); 3013784Sphk 3023784Sphk mvprintw(INTSROW, INTSCOL + 3, " Interrupts"); 3033784Sphk mvprintw(INTSROW + 1, INTSCOL + 9, "total"); 3043332Sphk 3053784Sphk mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "cow"); 3063784Sphk mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "wire"); 3073784Sphk mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "act"); 3083784Sphk mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "inact"); 3093784Sphk mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "cache"); 3103784Sphk mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "free"); 3113784Sphk mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "daefr"); 312231885Skib mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "prcfr"); 313231885Skib mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "react"); 3146342Sphk mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "pdwake"); 3153332Sphk mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "pdpgs"); 316231885Skib mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "intrn"); 3173784Sphk mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "buf"); 3183784Sphk mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "dirtybuf"); 3193784Sphk 3203784Sphk mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "desiredvnodes"); 3213784Sphk mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "numvnodes"); 322144501Sjhb mvprintw(VMSTATROW + 17, VMSTATCOL + 10, "freevnodes"); 323144501Sjhb 3243784Sphk mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt"); 3253784Sphk 3263784Sphk mvprintw(GRAPHROW, GRAPHCOL, 3273784Sphk " . %%Sys . %%Intr . %%User . %%Nice . %%Idle"); 3283784Sphk mvprintw(PROCSROW, PROCSCOL, "Proc:r p d s w"); 3293784Sphk mvprintw(GRAPHROW + 1, GRAPHCOL, 3303784Sphk "| | | | | | | | | | |"); 3313784Sphk 3323332Sphk mvprintw(NAMEIROW, NAMEICOL, "Namei Name-cache Dir-cache"); 3333784Sphk mvprintw(NAMEIROW + 1, NAMEICOL, 3343784Sphk " Calls hits %% hits %%"); 3353784Sphk mvprintw(DISKROW, DISKCOL, "Disks"); 3363784Sphk mvprintw(DISKROW + 1, DISKCOL, "KB/t"); 3373784Sphk mvprintw(DISKROW + 2, DISKCOL, "tps"); 3383784Sphk mvprintw(DISKROW + 3, DISKCOL, "MB/s"); 3393348Sphk mvprintw(DISKROW + 4, DISKCOL, "%% busy"); 340108533Sschweikh /* 3413784Sphk * For now, we don't support a fourth disk statistic. So there's 3423784Sphk * no point in providing a label for it. If someone can think of a 3433784Sphk * fourth useful disk statistic, there is room to add it. 3443784Sphk */ 3453784Sphk /* mvprintw(DISKROW + 4, DISKCOL, " msps"); */ 3463784Sphk j = 0; 3473784Sphk for (i = 0; i < num_devices && j < MAXDRIVES; i++) 3483784Sphk if (dev_select[i].selected) { 34948079Shoek char tmpstr[80]; 3503784Sphk sprintf(tmpstr, "%s%d", dev_select[i].device_name, 3513784Sphk dev_select[i].unit_number); 3523785Sphk mvprintw(DISKROW, DISKCOL + 5 + 6 * j, 3533785Sphk " %5.5s", tmpstr); 3543784Sphk j++; 3553784Sphk } 3563784Sphk 3573784Sphk if (j <= 4) { 3583784Sphk /* 3593784Sphk * room for extended VM stats 3603784Sphk */ 36131718Sjdp mvprintw(VMSTATROW + 11, VMSTATCOL - 6, "zfod"); 36237656Sbde mvprintw(VMSTATROW + 12, VMSTATCOL - 6, "ofod"); 36337015Sbde mvprintw(VMSTATROW + 13, VMSTATCOL - 6, "%%slo-z"); 3643784Sphk mvprintw(VMSTATROW + 14, VMSTATCOL - 6, "tfree"); 3653784Sphk extended_vm_stats = 1; 3663784Sphk } else { 3673784Sphk extended_vm_stats = 0; 36837015Sbde mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "zfod"); 36937015Sbde } 3703784Sphk 3713784Sphk for (i = 0; i < nintr; i++) { 3723784Sphk if (intrloc[i] == 0) 3733784Sphk continue; 3743784Sphk mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s", intrname[i]); 3753784Sphk } 3763784Sphk} 37737656Sbde 37837656Sbde#define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;} 37937015Sbde#define Q(fld) {t=cur.fld[i]; cur.fld[i]-=last.fld[i]; if(state==TIME) last.fld[i]=t;} 3803784Sphk#define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;} 3813784Sphk#define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \ 3823784Sphk if(state == TIME) s1.nchstats.fld = t;} 3833784Sphk#define PUTRATE(fld, l, c, w) \ 3843784Sphk Y(fld); \ 3853784Sphk putint((int)((float)s.fld/etime + 0.5), l, c, w) 3863332Sphk#define MAXFAIL 5 3873332Sphk 3883784Sphkstatic char cpuchar[CPUSTATES] = { '=' , '+', '>', '-', ' ' }; 3893332Sphkstatic char cpuorder[CPUSTATES] = { CP_SYS, CP_INTR, CP_USER, CP_NICE, 3903332Sphk CP_IDLE }; 3913332Sphk 39243402Sdillonvoid 39340435Spetershowkre() 394{ 395 float f1, f2; 396 int psiz, inttotal; 397 int i, j, k, l, lc; 398 static int failcnt = 0; 399 char intrbuffer[10]; 400 401 etime = 0; 402 for(i = 0; i < CPUSTATES; i++) { 403 X(time); 404 Q(cp_time); 405 etime += s.time[i]; 406 } 407 if (etime < 5.0) { /* < 5 ticks - ignore this trash */ 408 if (failcnt++ >= MAXFAIL) { 409 clear(); 410 mvprintw(2, 10, "The alternate system clock has died!"); 411 mvprintw(3, 10, "Reverting to ``pigs'' display."); 412 move(CMDLINE, 0); 413 refresh(); 414 failcnt = 0; 415 sleep(5); 416 command("pigs"); 417 } 418 return; 419 } 420 failcnt = 0; 421 etime /= hertz; 422 etime /= ncpu; 423 inttotal = 0; 424 for (i = 0; i < nintr; i++) { 425 if (s.intrcnt[i] == 0) 426 continue; 427 if (intrloc[i] == 0) { 428 if (nextintsrow == LINES) 429 continue; 430 intrloc[i] = nextintsrow++; 431 k = 0; 432 for (j = 0; j < (int)sizeof(intrbuffer); j++) { 433 if (strncmp(&intrname[i][j], "irq", 3) == 0) 434 j += 3; 435 intrbuffer[k++] = intrname[i][j]; 436 } 437 intrbuffer[k] = '\0'; 438 mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s", 439 intrbuffer); 440 } 441 X(intrcnt); 442 l = (int)((float)s.intrcnt[i]/etime + 0.5); 443 inttotal += l; 444 putint(l, intrloc[i], INTSCOL + 2, 6); 445 } 446 putint(inttotal, INTSROW + 1, INTSCOL + 2, 6); 447 Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss); 448 Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes); Z(ncs_neghits); 449 s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits + 450 nchtotal.ncs_miss + nchtotal.ncs_long + nchtotal.ncs_neghits; 451 if (state == TIME) 452 s1.nchcount = s.nchcount; 453 454 psiz = 0; 455 f2 = 0.0; 456 for (lc = 0; lc < CPUSTATES; lc++) { 457 i = cpuorder[lc]; 458 f1 = cputime(i); 459 f2 += f1; 460 l = (int) ((f2 + 1.0) / 2.0) - psiz; 461 if (f1 > 99.9) 462 f1 = 99.9; /* no room to display 100.0 */ 463 putfloat(f1, GRAPHROW, GRAPHCOL + 10 * lc, 4, 1, 0); 464 move(GRAPHROW + 2, psiz); 465 psiz += l; 466 while (l-- > 0) 467 addch(cpuchar[lc]); 468 } 469 470 putint(ucount(), STATROW, STATCOL, 3); 471 putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0); 472 putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0); 473 putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0); 474 mvaddstr(STATROW, STATCOL + 53, buf); 475#define pgtokb(pg) ((pg) * s.v_page_size / 1024) 476 putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 8); 477 putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 11, 8); 478 putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 19, 9); 479 putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 28, 9); 480 putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 8); 481 putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 11, 8); 482 putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 19, 9); 483 putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 28, 9); 484 putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 37, 8); 485 putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3); 486 putint(total.t_pw, PROCSROW + 1, PROCSCOL + 6, 3); 487 putint(total.t_dw, PROCSROW + 1, PROCSCOL + 9, 3); 488 putint(total.t_sl, PROCSROW + 1, PROCSCOL + 12, 3); 489 putint(total.t_sw, PROCSROW + 1, PROCSCOL + 15, 3); 490 if (extended_vm_stats == 0) { 491 PUTRATE(v_zfod, VMSTATROW + 0, VMSTATCOL + 4, 5); 492 } 493 PUTRATE(v_cow_faults, VMSTATROW + 1, VMSTATCOL + 3, 6); 494 putint(pgtokb(s.v_wire_count), VMSTATROW + 2, VMSTATCOL, 9); 495 putint(pgtokb(s.v_active_count), VMSTATROW + 3, VMSTATCOL, 9); 496 putint(pgtokb(s.v_inactive_count), VMSTATROW + 4, VMSTATCOL, 9); 497 putint(pgtokb(s.v_cache_count), VMSTATROW + 5, VMSTATCOL, 9); 498 putint(pgtokb(s.v_free_count), VMSTATROW + 6, VMSTATCOL, 9); 499 PUTRATE(v_dfree, VMSTATROW + 7, VMSTATCOL, 9); 500 PUTRATE(v_pfree, VMSTATROW + 8, VMSTATCOL, 9); 501 PUTRATE(v_reactivated, VMSTATROW + 9, VMSTATCOL, 9); 502 PUTRATE(v_pdwakeups, VMSTATROW + 10, VMSTATCOL, 9); 503 PUTRATE(v_pdpages, VMSTATROW + 11, VMSTATCOL, 9); 504 PUTRATE(v_intrans, VMSTATROW + 12, VMSTATCOL, 9); 505 506 if (extended_vm_stats) { 507 PUTRATE(v_zfod, VMSTATROW + 11, VMSTATCOL - 16, 9); 508 PUTRATE(v_ozfod, VMSTATROW + 12, VMSTATCOL - 16, 9); 509 putint( 510 ((s.v_ozfod < s.v_zfod) ? 511 s.v_ozfod * 100 / s.v_zfod : 512 0 513 ), 514 VMSTATROW + 13, 515 VMSTATCOL - 16, 516 9 517 ); 518 PUTRATE(v_tfree, VMSTATROW + 14, VMSTATCOL - 16, 9); 519 } 520 521 putint(s.bufspace/1024, VMSTATROW + 13, VMSTATCOL, 9); 522 putint(s.numdirtybuffers, VMSTATROW + 14, VMSTATCOL, 9); 523 putint(s.desiredvnodes, VMSTATROW + 15, VMSTATCOL, 9); 524 putint(s.numvnodes, VMSTATROW + 16, VMSTATCOL, 9); 525 putint(s.freevnodes, VMSTATROW + 17, VMSTATCOL, 9); 526 PUTRATE(v_vnodein, PAGEROW + 2, PAGECOL + 5, 5); 527 PUTRATE(v_vnodeout, PAGEROW + 2, PAGECOL + 10, 5); 528 PUTRATE(v_swapin, PAGEROW + 2, PAGECOL + 17, 5); 529 PUTRATE(v_swapout, PAGEROW + 2, PAGECOL + 22, 5); 530 PUTRATE(v_vnodepgsin, PAGEROW + 3, PAGECOL + 5, 5); 531 PUTRATE(v_vnodepgsout, PAGEROW + 3, PAGECOL + 10, 5); 532 PUTRATE(v_swappgsin, PAGEROW + 3, PAGECOL + 17, 5); 533 PUTRATE(v_swappgsout, PAGEROW + 3, PAGECOL + 22, 5); 534 PUTRATE(v_swtch, GENSTATROW + 1, GENSTATCOL, 5); 535 PUTRATE(v_trap, GENSTATROW + 1, GENSTATCOL + 5, 5); 536 PUTRATE(v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 5); 537 PUTRATE(v_intr, GENSTATROW + 1, GENSTATCOL + 15, 5); 538 PUTRATE(v_soft, GENSTATROW + 1, GENSTATCOL + 20, 5); 539 PUTRATE(v_vm_faults, GENSTATROW + 1, GENSTATCOL + 25, 5); 540 mvprintw(DISKROW, DISKCOL + 5, " "); 541 for (i = 0, lc = 0; i < num_devices && lc < MAXDRIVES; i++) 542 if (dev_select[i].selected) { 543 char tmpstr[80]; 544 sprintf(tmpstr, "%s%d", dev_select[i].device_name, 545 dev_select[i].unit_number); 546 mvprintw(DISKROW, DISKCOL + 5 + 6 * lc, 547 " %5.5s", tmpstr); 548 switch(state) { 549 case TIME: 550 dinfo(i, ++lc, &cur, &last); 551 break; 552 case RUN: 553 dinfo(i, ++lc, &cur, &run); 554 break; 555 case BOOT: 556 dinfo(i, ++lc, &cur, NULL); 557 break; 558 } 559 } 560 putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9); 561 putint((nchtotal.ncs_goodhits + nchtotal.ncs_neghits), 562 NAMEIROW + 2, NAMEICOL + 9, 9); 563#define nz(x) ((x) ? (x) : 1) 564 putfloat((nchtotal.ncs_goodhits+nchtotal.ncs_neghits) * 565 100.0 / nz(s.nchcount), 566 NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1); 567 putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9); 568 putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount), 569 NAMEIROW + 2, NAMEICOL + 33, 4, 0, 1); 570#undef nz 571} 572 573int 574cmdkre(cmd, args) 575 const char *cmd, *args; 576{ 577 int retval; 578 579 if (prefix(cmd, "run")) { 580 retval = 1; 581 copyinfo(&s2, &s1); 582 switch (devstat_getdevs(NULL, &run)) { 583 case -1: 584 errx(1, "%s", devstat_errbuf); 585 break; 586 case 1: 587 num_devices = run.dinfo->numdevs; 588 generation = run.dinfo->generation; 589 retval = dscmd("refresh", NULL, MAXDRIVES, &cur); 590 if (retval == 2) 591 labelkre(); 592 break; 593 default: 594 break; 595 } 596 state = RUN; 597 return (retval); 598 } 599 if (prefix(cmd, "boot")) { 600 state = BOOT; 601 copyinfo(&z, &s1); 602 return (1); 603 } 604 if (prefix(cmd, "time")) { 605 state = TIME; 606 return (1); 607 } 608 if (prefix(cmd, "zero")) { 609 retval = 1; 610 if (state == RUN) { 611 getinfo(&s1); 612 switch (devstat_getdevs(NULL, &run)) { 613 case -1: 614 errx(1, "%s", devstat_errbuf); 615 break; 616 case 1: 617 num_devices = run.dinfo->numdevs; 618 generation = run.dinfo->generation; 619 retval = dscmd("refresh",NULL, MAXDRIVES, &cur); 620 if (retval == 2) 621 labelkre(); 622 break; 623 default: 624 break; 625 } 626 } 627 return (retval); 628 } 629 retval = dscmd(cmd, args, MAXDRIVES, &cur); 630 631 if (retval == 2) 632 labelkre(); 633 634 return(retval); 635} 636 637/* calculate number of users on the system */ 638static int 639ucount() 640{ 641 int nusers = 0; 642 643 if (ut < 0) 644 return (0); 645 while (read(ut, &utmp, sizeof(utmp))) 646 if (utmp.ut_name[0] != '\0') 647 nusers++; 648 649 lseek(ut, 0L, L_SET); 650 return (nusers); 651} 652 653static float 654cputime(indx) 655 int indx; 656{ 657 double lt; 658 int i; 659 660 lt = 0; 661 for (i = 0; i < CPUSTATES; i++) 662 lt += s.time[i]; 663 if (lt == 0.0) 664 lt = 1.0; 665 return (s.time[indx] * 100.0 / lt); 666} 667 668static void 669putint(n, l, lc, w) 670 int n, l, lc, w; 671{ 672 char b[128]; 673 674 move(l, lc); 675 if (n == 0) { 676 while (w-- > 0) 677 addch(' '); 678 return; 679 } 680 snprintf(b, sizeof(b), "%*d", w, n); 681 if ((int)strlen(b) > w) 682 snprintf(b, sizeof(b), "%*dK", w - 1, n / 1000); 683 if ((int)strlen(b) > w) 684 snprintf(b, sizeof(b), "%*dM", w - 1, n / 1000000); 685 addstr(b); 686} 687 688static void 689putfloat(f, l, lc, w, d, nz) 690 double f; 691 int l, lc, w, d, nz; 692{ 693 char b[128]; 694 695 move(l, lc); 696 if (nz && f == 0.0) { 697 while (--w >= 0) 698 addch(' '); 699 return; 700 } 701 snprintf(b, sizeof(b), "%*.*f", w, d, f); 702 if ((int)strlen(b) > w) 703 snprintf(b, sizeof(b), "%*.0f", w, f); 704 if ((int)strlen(b) > w) { 705 while (--w >= 0) 706 addch('*'); 707 return; 708 } 709 addstr(b); 710} 711 712static void 713putlongdouble(f, l, lc, w, d, nz) 714 long double f; 715 int l, lc, w, d, nz; 716{ 717 char b[128]; 718 719 move(l, lc); 720 if (nz && f == 0.0) { 721 while (--w >= 0) 722 addch(' '); 723 return; 724 } 725 sprintf(b, "%*.*Lf", w, d, f); 726 if ((int)strlen(b) > w) 727 sprintf(b, "%*.0Lf", w, f); 728 if ((int)strlen(b) > w) { 729 while (--w >= 0) 730 addch('*'); 731 return; 732 } 733 addstr(b); 734} 735 736static void 737getinfo(ls) 738 struct Info *ls; 739{ 740 struct devinfo *tmp_dinfo; 741 size_t size; 742 int mib[2]; 743 744 GETSYSCTL("kern.cp_time", ls->time); 745 GETSYSCTL("kern.cp_time", cur.cp_time); 746 GETSYSCTL("vm.stats.sys.v_swtch", ls->v_swtch); 747 GETSYSCTL("vm.stats.sys.v_trap", ls->v_trap); 748 GETSYSCTL("vm.stats.sys.v_syscall", ls->v_syscall); 749 GETSYSCTL("vm.stats.sys.v_intr", ls->v_intr); 750 GETSYSCTL("vm.stats.sys.v_soft", ls->v_soft); 751 GETSYSCTL("vm.stats.vm.v_vm_faults", ls->v_vm_faults); 752 GETSYSCTL("vm.stats.vm.v_cow_faults", ls->v_cow_faults); 753 GETSYSCTL("vm.stats.vm.v_zfod", ls->v_zfod); 754 GETSYSCTL("vm.stats.vm.v_ozfod", ls->v_ozfod); 755 GETSYSCTL("vm.stats.vm.v_swapin", ls->v_swapin); 756 GETSYSCTL("vm.stats.vm.v_swapout", ls->v_swapout); 757 GETSYSCTL("vm.stats.vm.v_swappgsin", ls->v_swappgsin); 758 GETSYSCTL("vm.stats.vm.v_swappgsout", ls->v_swappgsout); 759 GETSYSCTL("vm.stats.vm.v_vnodein", ls->v_vnodein); 760 GETSYSCTL("vm.stats.vm.v_vnodeout", ls->v_vnodeout); 761 GETSYSCTL("vm.stats.vm.v_vnodepgsin", ls->v_vnodepgsin); 762 GETSYSCTL("vm.stats.vm.v_vnodepgsout", ls->v_vnodepgsout); 763 GETSYSCTL("vm.stats.vm.v_intrans", ls->v_intrans); 764 GETSYSCTL("vm.stats.vm.v_reactivated", ls->v_reactivated); 765 GETSYSCTL("vm.stats.vm.v_pdwakeups", ls->v_pdwakeups); 766 GETSYSCTL("vm.stats.vm.v_pdpages", ls->v_pdpages); 767 GETSYSCTL("vm.stats.vm.v_dfree", ls->v_dfree); 768 GETSYSCTL("vm.stats.vm.v_pfree", ls->v_pfree); 769 GETSYSCTL("vm.stats.vm.v_tfree", ls->v_tfree); 770 GETSYSCTL("vm.stats.vm.v_page_size", ls->v_page_size); 771 GETSYSCTL("vm.stats.vm.v_free_count", ls->v_free_count); 772 GETSYSCTL("vm.stats.vm.v_wire_count", ls->v_wire_count); 773 GETSYSCTL("vm.stats.vm.v_active_count", ls->v_active_count); 774 GETSYSCTL("vm.stats.vm.v_inactive_count", ls->v_inactive_count); 775 GETSYSCTL("vm.stats.vm.v_cache_count", ls->v_cache_count); 776 GETSYSCTL("vfs.bufspace", ls->bufspace); 777 GETSYSCTL("kern.maxvnodes", ls->desiredvnodes); 778 GETSYSCTL("vfs.numvnodes", ls->numvnodes); 779 GETSYSCTL("vfs.freevnodes", ls->freevnodes); 780 GETSYSCTL("vfs.cache.nchstats", ls->nchstats); 781 GETSYSCTL("vfs.numdirtybuffers", ls->numdirtybuffers); 782 getsysctl("hw.intrcnt", ls->intrcnt, nintr * sizeof(u_long)); 783 784 size = sizeof(ls->Total); 785 mib[0] = CTL_VM; 786 mib[1] = VM_TOTAL; 787 if (sysctl(mib, 2, &ls->Total, &size, NULL, 0) < 0) { 788 error("Can't get kernel info: %s\n", strerror(errno)); 789 bzero(&ls->Total, sizeof(ls->Total)); 790 } 791 size = sizeof(ncpu); 792 if (sysctlbyname("hw.ncpu", &ncpu, &size, NULL, 0) < 0 || 793 size != sizeof(ncpu)) 794 ncpu = 1; 795 796 tmp_dinfo = last.dinfo; 797 last.dinfo = cur.dinfo; 798 cur.dinfo = tmp_dinfo; 799 800 last.snap_time = cur.snap_time; 801 switch (devstat_getdevs(NULL, &cur)) { 802 case -1: 803 errx(1, "%s", devstat_errbuf); 804 break; 805 case 1: 806 num_devices = cur.dinfo->numdevs; 807 generation = cur.dinfo->generation; 808 cmdkre("refresh", NULL); 809 break; 810 default: 811 break; 812 } 813} 814 815static void 816allocinfo(ls) 817 struct Info *ls; 818{ 819 820 ls->intrcnt = (long *) calloc(nintr, sizeof(long)); 821 if (ls->intrcnt == NULL) 822 errx(2, "out of memory"); 823} 824 825static void 826copyinfo(from, to) 827 struct Info *from, *to; 828{ 829 long *intrcnt; 830 831 /* 832 * time, wds, seek, and xfer are malloc'd so we have to 833 * save the pointers before the structure copy and then 834 * copy by hand. 835 */ 836 intrcnt = to->intrcnt; 837 *to = *from; 838 839 bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int)); 840} 841 842static void 843dinfo(dn, lc, now, then) 844 int dn, lc; 845 struct statinfo *now, *then; 846{ 847 long double transfers_per_second; 848 long double kb_per_transfer, mb_per_second; 849 long double elapsed_time, device_busy; 850 int di; 851 852 di = dev_select[dn].position; 853 854 if (then != NULL) { 855 /* Calculate relative to previous sample */ 856 elapsed_time = now->snap_time - then->snap_time; 857 } else { 858 /* Calculate relative to device creation */ 859 elapsed_time = now->snap_time - devstat_compute_etime( 860 &now->dinfo->devices[di].creation_time, NULL); 861 } 862 863 if (devstat_compute_statistics(&now->dinfo->devices[di], then ? 864 &then->dinfo->devices[di] : NULL, elapsed_time, 865 DSM_KB_PER_TRANSFER, &kb_per_transfer, 866 DSM_TRANSFERS_PER_SECOND, &transfers_per_second, 867 DSM_MB_PER_SECOND, &mb_per_second, 868 DSM_BUSY_PCT, &device_busy, 869 DSM_NONE) != 0) 870 errx(1, "%s", devstat_errbuf); 871 872 lc = DISKCOL + lc * 6; 873 putlongdouble(kb_per_transfer, DISKROW + 1, lc, 5, 2, 0); 874 putlongdouble(transfers_per_second, DISKROW + 2, lc, 5, 0, 0); 875 putlongdouble(mb_per_second, DISKROW + 3, lc, 5, 2, 0); 876 putlongdouble(device_busy, DISKROW + 4, lc, 5, 0, 0); 877} 878