Annotation of src/usr.bin/systat/vmstat.c, Revision 1.93
1.93 ! deraadt 1: /* $OpenBSD: vmstat.c,v 1.92 2022/02/20 00:09:29 deraadt Exp $ */
1.2 deraadt 2: /* $NetBSD: vmstat.c,v 1.5 1996/05/10 23:16:40 thorpej Exp $ */
1.1 deraadt 3:
4: /*-
5: * Copyright (c) 1983, 1989, 1992, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.41 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: /*
34: * Cursed vmstat -- from Robert Elz.
35: */
36:
1.79 deraadt 37: #include <sys/param.h> /* MAXCOMLEN */
38: #include <sys/types.h>
1.77 miod 39: #include <sys/namei.h>
1.92 deraadt 40: #include <sys/signal.h>
1.77 miod 41: #include <sys/proc.h>
42: #include <sys/sched.h>
1.1 deraadt 43: #include <sys/stat.h>
1.77 miod 44: #include <sys/sysctl.h>
1.1 deraadt 45: #include <sys/time.h>
1.76 mpi 46: #include <sys/vmmeter.h>
1.19 art 47:
1.1 deraadt 48: #include <ctype.h>
1.75 deraadt 49: #include <errno.h>
1.1 deraadt 50: #include <err.h>
51: #include <paths.h>
52: #include <signal.h>
53: #include <stdlib.h>
54: #include <string.h>
55: #include <unistd.h>
56:
57: #include "systat.h"
1.79 deraadt 58: #include "dkstats.h"
59:
60: #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
1.86 krw 61: #define MINIMUM(a, b) (((a) < (b)) ? (a) : (b))
1.93 ! deraadt 62: #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
1.1 deraadt 63:
64: static struct Info {
1.89 cheloha 65: struct cpustats cpustats;
1.19 art 66: struct uvmexp uvmexp;
1.1 deraadt 67: struct vmtotal Total;
68: struct nchstats nchstats;
69: long nchcount;
1.81 guenther 70: uint64_t *intrcnt;
1.64 canacar 71: } s, s1, s2, s3, z;
1.1 deraadt 72:
1.89 cheloha 73: static int ncpu;
74:
1.2 deraadt 75: extern struct _disk cur;
76:
1.1 deraadt 77: #define cnt s.Cnt
78: #define oldcnt s1.Cnt
79: #define total s.Total
80: #define nchtotal s.nchstats
81: #define oldnchtotal s1.nchstats
82:
83: static enum state { BOOT, TIME, RUN } state = TIME;
84:
1.33 millert 85: static void allocinfo(struct Info *);
86: static void copyinfo(struct Info *, struct Info *);
87: static float cputime(int);
88: static void dinfo(int, int);
1.57 deraadt 89: static void getinfo(struct Info *);
1.62 deraadt 90: void putint(int, int, int, int);
1.68 deraadt 91: void putintmk(int, int, int, int);
1.62 deraadt 92: void putuint64(u_int64_t, int, int, int);
93: void putfloat(double, int, int, int, int, int);
94: int ucount(void);
1.1 deraadt 95:
1.64 canacar 96: void print_vm(void);
97: int read_vm(void);
98: int select_vm(void);
99: int vm_keyboard_callback(int);
100:
1.1 deraadt 101: static time_t t;
102: static float hertz;
103: static int nintr;
104: static long *intrloc;
105: static char **intrname;
1.86 krw 106: static int ipktsrow;
1.1 deraadt 107:
108: WINDOW *
1.35 deraadt 109: openkre(void)
1.1 deraadt 110: {
1.62 deraadt 111: return (subwin(stdscr, LINES-1-1, 0, 1, 0));
1.1 deraadt 112: }
113:
114: void
1.35 deraadt 115: closekre(WINDOW *w)
1.1 deraadt 116: {
117:
118: if (w == NULL)
119: return;
120: wclear(w);
121: wrefresh(w);
1.62 deraadt 122: delwin(w);
1.1 deraadt 123: }
124:
125: /*
126: * These constants define where the major pieces are laid out
127: */
128: #define STATROW 0 /* uses 1 row and 68 cols */
129: #define STATCOL 2
1.56 mickey 130: #define MEMROW 2 /* uses 4 rows and 34 cols */
1.1 deraadt 131: #define MEMCOL 0
132: #define PAGEROW 2 /* uses 4 rows and 26 cols */
1.17 deraadt 133: #define PAGECOL 37
1.1 deraadt 134: #define INTSROW 2 /* uses all rows to bottom and 17 cols */
135: #define INTSCOL 63
136: #define PROCSROW 7 /* uses 2 rows and 20 cols */
137: #define PROCSCOL 0
1.27 deraadt 138: #define GENSTATROW 7 /* uses 2 rows and 35 cols */
139: #define GENSTATCOL 16
1.55 pedro 140: #define VMSTATROW 7 /* uses 18 rows and 12 cols */
1.1 deraadt 141: #define VMSTATCOL 48
142: #define GRAPHROW 10 /* uses 3 rows and 51 cols */
143: #define GRAPHCOL 0
1.25 weingart 144: #define NAMEIROW 14 /* uses 3 rows and 49 cols */
1.1 deraadt 145: #define NAMEICOL 0
146: #define DISKROW 18 /* uses 5 rows and 50 cols (for 9 drives) */
147: #define DISKCOL 0
148:
1.32 tdeval 149: #define DRIVESPACE 45 /* max space for drives */
1.1 deraadt 150:
1.64 canacar 151:
152: field_def *view_vm_0[] = {
153: NULL
154: };
155:
156: /* Define view managers */
157: struct view_manager vmstat_mgr = {
158: "VMstat", select_vm, read_vm, NULL, print_header,
159: print_vm, vm_keyboard_callback, NULL, NULL
160: };
161:
162: field_view views_vm[] = {
163: {view_vm_0, "vmstat", '7', &vmstat_mgr},
164: {NULL, NULL, 0, NULL}
165: };
166:
1.1 deraadt 167: int
1.64 canacar 168: initvmstat(void)
1.1 deraadt 169: {
1.65 deraadt 170: field_view *v;
1.57 deraadt 171: int mib[4], i;
1.51 aaron 172: size_t size;
1.50 deraadt 173:
1.73 mikeb 174: hertz = stathz;
1.31 deraadt 175: if (!dkinit(1))
1.1 deraadt 176: return(0);
1.51 aaron 177:
1.89 cheloha 178: mib[0] = CTL_HW;
179: mib[1] = HW_NCPU;
180: size = sizeof(ncpu);
1.91 deraadt 181: if (sysctl(mib, 2, &ncpu, &size, NULL, 0) == -1)
1.89 cheloha 182: return (-1);
183:
1.51 aaron 184: mib[0] = CTL_KERN;
185: mib[1] = KERN_INTRCNT;
186: mib[2] = KERN_INTRCNT_NUM;
187: size = sizeof(nintr);
1.91 deraadt 188: if (sysctl(mib, 3, &nintr, &size, NULL, 0) == -1)
1.51 aaron 189: return (-1);
190:
191: intrloc = calloc(nintr, sizeof(long));
192: intrname = calloc(nintr, sizeof(char *));
1.83 otto 193: if (intrloc == NULL || intrname == NULL)
194: err(2, NULL);
1.51 aaron 195:
196: for (i = 0; i < nintr; i++) {
197: char name[128];
1.58 deraadt 198:
1.51 aaron 199: mib[0] = CTL_KERN;
200: mib[1] = KERN_INTRCNT;
201: mib[2] = KERN_INTRCNT_NAME;
202: mib[3] = i;
203: size = sizeof(name);
1.91 deraadt 204: if (sysctl(mib, 4, name, &size, NULL, 0) == -1)
1.51 aaron 205: return (-1);
206:
207: intrname[i] = strdup(name);
208: if (intrname[i] == NULL)
209: return (-1);
210: }
211:
212: allocinfo(&s);
213: allocinfo(&s1);
214: allocinfo(&s2);
1.64 canacar 215: allocinfo(&s3);
1.51 aaron 216: allocinfo(&z);
217:
1.57 deraadt 218: getinfo(&s2);
1.64 canacar 219: copyinfo(&z, &s1);
220:
221: for (v = views_vm; v->name != NULL; v++)
222: add_view(v);
223:
1.1 deraadt 224: return(1);
225: }
226:
227: void
1.35 deraadt 228: fetchkre(void)
1.1 deraadt 229: {
1.64 canacar 230: getinfo(&s3);
1.1 deraadt 231: }
232:
233: void
1.35 deraadt 234: labelkre(void)
1.1 deraadt 235: {
1.32 tdeval 236: int i, j, l;
1.1 deraadt 237:
1.56 mickey 238: mvprintw(MEMROW, MEMCOL, " memory totals (in KB)");
239: mvprintw(MEMROW + 1, MEMCOL, " real virtual free");
1.19 art 240: mvprintw(MEMROW + 2, MEMCOL, "Active");
241: mvprintw(MEMROW + 3, MEMCOL, "All");
242:
243: mvprintw(PAGEROW, PAGECOL, " PAGING SWAPPING ");
244: mvprintw(PAGEROW + 1, PAGECOL, " in out in out ");
245: mvprintw(PAGEROW + 2, PAGECOL, "ops");
246: mvprintw(PAGEROW + 3, PAGECOL, "pages");
1.1 deraadt 247:
1.20 art 248: mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
249: mvprintw(INTSROW + 1, INTSCOL + 9, "total");
250:
1.86 krw 251: j = INTSROW + 2;
252: for (i = 0; i < nintr; i++) {
253: intrloc[i] = 0;
254: if (s.intrcnt[i] == 0 || ipktsrow == LINES)
255: continue;
256: intrloc[i] = j++;
257: mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
258: }
259: ipktsrow = MAXIMUM(j, MINIMUM(LINES - 3, VMSTATROW + 17));
260: if (LINES - 1 > ipktsrow)
261: mvprintw(ipktsrow, INTSCOL + 9, "IPKTS");
262: if (LINES - 1 > ipktsrow + 1)
263: mvprintw(ipktsrow + 1, INTSCOL + 9, "OPKTS");
1.70 deraadt 264:
1.19 art 265: mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks");
266: mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw");
267: mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm");
268: mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait");
269: mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck");
270: mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok");
271: mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram");
272: mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy");
273: mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp");
274: mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod");
275: mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow");
276: mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin");
277: mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg");
278: mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg");
279: mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired");
280: mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre");
281: if (LINES - 1 > VMSTATROW + 16)
282: mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn");
1.42 mickey 283: if (LINES - 1 > VMSTATROW + 17)
1.86 krw 284: mvprintw(VMSTATROW + 17, VMSTATCOL + 10, "pzidl");
1.55 pedro 285: if (LINES - 1 > VMSTATROW + 18)
1.86 krw 286: mvprintw(VMSTATROW + 18, VMSTATCOL + 10, "kmape");
1.1 deraadt 287:
1.27 deraadt 288: mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt");
1.1 deraadt 289:
290: mvprintw(GRAPHROW, GRAPHCOL,
1.85 bluhm 291: " . %%Int . %%Spn . %%Sys . %%Usr . %%Idle");
1.19 art 292: mvprintw(PROCSROW, PROCSCOL, "Proc:r d s w");
1.1 deraadt 293: mvprintw(GRAPHROW + 1, GRAPHCOL,
1.31 deraadt 294: "| | | | | | | | | | |");
1.1 deraadt 295:
1.25 weingart 296: mvprintw(NAMEIROW, NAMEICOL,
1.31 deraadt 297: "Namei Sys-cache Proc-cache No-cache");
1.1 deraadt 298: mvprintw(NAMEIROW + 1, NAMEICOL,
1.31 deraadt 299: " Calls hits %% hits %% miss %%");
1.45 tedu 300: mvprintw(DISKROW, DISKCOL, "Disks");
1.1 deraadt 301: mvprintw(DISKROW + 1, DISKCOL, "seeks");
302: mvprintw(DISKROW + 2, DISKCOL, "xfers");
1.68 deraadt 303: mvprintw(DISKROW + 3, DISKCOL, "speed");
1.2 deraadt 304: mvprintw(DISKROW + 4, DISKCOL, " sec");
1.37 tdeval 305: for (i = 0, j = 0; i < cur.dk_ndrive && j < DRIVESPACE; i++)
306: if (cur.dk_select[i] && (j + strlen(dr_name[i])) < DRIVESPACE) {
1.79 deraadt 307: l = MAXIMUM(5, strlen(dr_name[i]));
1.32 tdeval 308: mvprintw(DISKROW, DISKCOL + 5 + j,
309: " %*s", l, dr_name[i]);
310: j += 1 + l;
1.1 deraadt 311: }
312: for (i = 0; i < nintr; i++) {
313: if (intrloc[i] == 0)
314: continue;
315: mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
316: }
317: }
318:
1.82 krw 319: #define X(fld) {s.fld[i]-=s1.fld[i];}
320: #define Y(fld) {s.fld -= s1.fld;}
321: #define Z(fld) {s.nchstats.fld -= s1.nchstats.fld;}
1.1 deraadt 322: #define PUTRATE(fld, l, c, w) \
1.61 otto 323: do { \
324: Y(fld); \
325: putint((int)((float)s.fld/etime + 0.5), l, c, w); \
326: } while (0)
1.1 deraadt 327: #define MAXFAIL 5
328:
1.85 bluhm 329: static char cpuchar[] = { '|', '@', '=', '>', ' ' };
330: static char cpuorder[] = { CP_INTR, CP_SPIN, CP_SYS, CP_USER, CP_IDLE };
1.1 deraadt 331:
332: void
1.35 deraadt 333: showkre(void)
1.1 deraadt 334: {
335: float f1, f2;
1.53 deraadt 336: int psiz;
337: u_int64_t inttotal, intcnt;
1.1 deraadt 338: int i, l, c;
1.46 tedu 339: static int failcnt = 0, first_run = 0;
1.90 tedu 340: double etime;
1.1 deraadt 341:
1.46 tedu 342: if (state == TIME) {
343: if (!first_run) {
344: first_run = 1;
345: return;
346: }
347: }
1.1 deraadt 348: etime = 0;
1.35 deraadt 349: for (i = 0; i < CPUSTATES; i++) {
1.89 cheloha 350: X(cpustats.cs_time);
351: etime += s.cpustats.cs_time[i];
1.1 deraadt 352: }
353: if (etime < 5.0) { /* < 5 ticks - ignore this trash */
354: if (failcnt++ >= MAXFAIL) {
1.64 canacar 355: error("The alternate system clock has died!");
1.1 deraadt 356: failcnt = 0;
357: }
358: return;
359: }
360: failcnt = 0;
361: etime /= hertz;
362: inttotal = 0;
363: for (i = 0; i < nintr; i++) {
1.53 deraadt 364: t = intcnt = s.intrcnt[i];
365: s.intrcnt[i] -= s1.intrcnt[i];
366: intcnt = (u_int64_t)((float)s.intrcnt[i]/etime + 0.5);
367: inttotal += intcnt;
1.86 krw 368: if (intrloc[i] != 0)
369: putuint64(intcnt, intrloc[i], INTSCOL, 8);
1.1 deraadt 370: }
1.53 deraadt 371: putuint64(inttotal, INTSROW + 1, INTSCOL, 8);
1.1 deraadt 372: Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
373: Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
374: s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
375: nchtotal.ncs_miss + nchtotal.ncs_long;
376:
1.86 krw 377: if (LINES - 1 > ipktsrow)
378: putint(sum.ifc_ip, ipktsrow, INTSCOL, 8);
379: if (LINES - 1 > ipktsrow + 1)
380: putint(sum.ifc_op, ipktsrow + 1, INTSCOL, 8);
1.70 deraadt 381:
1.1 deraadt 382: psiz = 0;
383: f2 = 0.0;
384:
1.84 mpi 385: for (c = 0; c < nitems(cpuorder); c++) {
1.1 deraadt 386: i = cpuorder[c];
387: f1 = cputime(i);
1.85 bluhm 388: if (i == CP_USER)
389: f1 += cputime(CP_NICE);
1.1 deraadt 390: f2 += f1;
391: l = (int) ((f2 + 1.0) / 2.0) - psiz;
1.59 dlg 392: putfloat(f1, GRAPHROW, GRAPHCOL + 1 + (10 * c), 5, 1, 0);
1.1 deraadt 393: move(GRAPHROW + 2, psiz);
394: psiz += l;
395: while (l-- > 0)
396: addch(cpuchar[c]);
397: }
398:
1.34 millert 399: #define pgtokb(pg) ((pg) * (s.uvmexp.pagesize / 1024))
1.19 art 400:
1.56 mickey 401: putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 7, 8);
1.19 art 402: putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse), /* XXX */
1.56 mickey 403: MEMROW + 2, MEMCOL + 17, 8);
404: putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 7, 8);
1.19 art 405: putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
1.56 mickey 406: MEMROW + 3, MEMCOL + 17, 8);
407: putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 26, 8);
1.19 art 408: putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
1.56 mickey 409: MEMROW + 3, MEMCOL + 26, 8);
1.1 deraadt 410: putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
1.19 art 411:
412: putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3);
413: putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3);
414: putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3);
415: PUTRATE(uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
416: PUTRATE(uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
417: PUTRATE(uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
418: PUTRATE(uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
419: PUTRATE(uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
420: PUTRATE(uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
421: PUTRATE(uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
422: PUTRATE(uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
423: PUTRATE(uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
424: PUTRATE(uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
425: PUTRATE(uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
426: putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
427: putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
428: putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
429: putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9);
430: PUTRATE(uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
431: if (LINES - 1 > VMSTATROW + 16)
432: PUTRATE(uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
1.42 mickey 433: if (LINES - 1 > VMSTATROW + 17)
1.43 mickey 434: PUTRATE(uvmexp.zeropages, VMSTATROW + 17, VMSTATCOL, 9);
1.55 pedro 435: if (LINES - 1 > VMSTATROW + 18)
436: putint(s.uvmexp.kmapent, VMSTATROW + 18, VMSTATCOL, 9);
1.19 art 437:
438: PUTRATE(uvmexp.pageins, PAGEROW + 2, PAGECOL + 5, 5);
439: PUTRATE(uvmexp.pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
440: PUTRATE(uvmexp.pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
441: PUTRATE(uvmexp.pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
442:
1.27 deraadt 443: PUTRATE(uvmexp.swtch, GENSTATROW + 1, GENSTATCOL, 6);
444: PUTRATE(uvmexp.traps, GENSTATROW + 1, GENSTATCOL + 6, 6);
445: PUTRATE(uvmexp.syscalls, GENSTATROW + 1, GENSTATCOL + 12, 6);
446: PUTRATE(uvmexp.intrs, GENSTATROW + 1, GENSTATCOL + 18, 6);
447: PUTRATE(uvmexp.softs, GENSTATROW + 1, GENSTATCOL + 24, 6);
448: PUTRATE(uvmexp.faults, GENSTATROW + 1, GENSTATCOL + 30, 5);
1.1 deraadt 449: mvprintw(DISKROW, DISKCOL + 5, " ");
1.37 tdeval 450: for (i = 0, c = 0; i < cur.dk_ndrive && c < DRIVESPACE; i++)
451: if (cur.dk_select[i] && (c + strlen(dr_name[i])) < DRIVESPACE) {
1.79 deraadt 452: l = MAXIMUM(5, strlen(dr_name[i]));
1.32 tdeval 453: mvprintw(DISKROW, DISKCOL + 5 + c,
454: " %*s", l, dr_name[i]);
455: c += 1 + l;
456: dinfo(i, c);
1.1 deraadt 457: }
1.35 deraadt 458: /* and pad the DRIVESPACE */
1.32 tdeval 459: l = DRIVESPACE - c;
460: for (i = 0; i < 5; i++)
461: mvprintw(DISKROW + i, DISKCOL + 5 + c, "%*s", l, "");
462:
1.1 deraadt 463: putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
1.25 weingart 464: putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 10, 8);
1.1 deraadt 465: #define nz(x) ((x) ? (x) : 1)
466: putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
1.31 deraadt 467: NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
1.25 weingart 468: putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 24, 7);
1.1 deraadt 469: putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
1.31 deraadt 470: NAMEIROW + 2, NAMEICOL + 33, 4, 0, 1);
1.66 canacar 471: putint(nchtotal.ncs_miss + nchtotal.ncs_long - nchtotal.ncs_pass2,
1.25 weingart 472: NAMEIROW + 2, NAMEICOL + 38, 7);
1.66 canacar 473: putfloat((nchtotal.ncs_miss + nchtotal.ncs_long - nchtotal.ncs_pass2) *
1.31 deraadt 474: 100.0 / nz(s.nchcount), NAMEIROW + 2, NAMEICOL + 45, 4, 0, 1);
1.1 deraadt 475: #undef nz
1.70 deraadt 476:
1.1 deraadt 477: }
478:
479: int
1.64 canacar 480: vm_keyboard_callback(int ch)
1.1 deraadt 481: {
1.64 canacar 482: switch(ch) {
483: case 'r':
1.1 deraadt 484: copyinfo(&s2, &s1);
485: state = RUN;
1.64 canacar 486: break;
487: case 'b':
1.1 deraadt 488: state = BOOT;
489: copyinfo(&z, &s1);
1.64 canacar 490: break;
491: case 't':
1.1 deraadt 492: state = TIME;
1.64 canacar 493: break;
494: case 'z':
1.1 deraadt 495: if (state == RUN)
1.57 deraadt 496: getinfo(&s1);
1.64 canacar 497: break;
1.1 deraadt 498: }
1.64 canacar 499: return (keyboard_callback(ch));
1.1 deraadt 500: }
501:
1.64 canacar 502:
1.1 deraadt 503: static float
1.35 deraadt 504: cputime(int indx)
1.1 deraadt 505: {
1.67 canacar 506: double tm;
1.28 mpech 507: int i;
1.1 deraadt 508:
1.67 canacar 509: tm = 0;
1.89 cheloha 510: for (i = 0; i < nitems(s.cpustats.cs_time); i++)
511: tm += s.cpustats.cs_time[i];
1.67 canacar 512: if (tm == 0.0)
513: tm = 1.0;
1.89 cheloha 514: return (s.cpustats.cs_time[indx] * 100.0 / tm);
1.1 deraadt 515: }
516:
1.62 deraadt 517: void
1.35 deraadt 518: putint(int n, int l, int c, int w)
1.1 deraadt 519: {
520: char b[128];
521:
522: move(l, c);
523: if (n == 0) {
524: while (w-- > 0)
525: addch(' ');
526: return;
527: }
1.12 deraadt 528: snprintf(b, sizeof b, "%*d", w, n);
1.1 deraadt 529: if (strlen(b) > w) {
530: while (w-- > 0)
531: addch('*');
532: return;
533: }
534: addstr(b);
535: }
536:
1.62 deraadt 537: void
1.68 deraadt 538: putintmk(int n, int l, int c, int w)
539: {
540: char b[128];
541:
542: move(l, c);
543: if (n == 0) {
544: while (w-- > 0)
545: addch(' ');
546: return;
547: }
1.69 drahn 548: if (n > 9999 * 1024)
1.68 deraadt 549: snprintf(b, sizeof b, "%*dG", w - 1, n / 1024 / 1024);
1.69 drahn 550: else if (n > 9999)
1.68 deraadt 551: snprintf(b, sizeof b, "%*dM", w - 1, n / 1024);
552: else
553: snprintf(b, sizeof b, "%*dK", w - 1, n);
554: if (strlen(b) > w) {
555: while (w-- > 0)
556: addch('*');
557: return;
558: }
559: addstr(b);
560: }
561:
562: void
1.53 deraadt 563: putuint64(u_int64_t n, int l, int c, int w)
564: {
565: char b[128];
566:
567: move(l, c);
568: if (n == 0) {
569: while (w-- > 0)
570: addch(' ');
571: return;
572: }
573: snprintf(b, sizeof b, "%*llu", w, n);
574: if (strlen(b) > w) {
575: while (w-- > 0)
576: addch('*');
577: return;
578: }
579: addstr(b);
580: }
581:
1.62 deraadt 582: void
1.35 deraadt 583: putfloat(double f, int l, int c, int w, int d, int nz)
1.1 deraadt 584: {
585: char b[128];
586:
587: move(l, c);
588: if (nz && f == 0.0) {
589: while (--w >= 0)
590: addch(' ');
591: return;
592: }
1.12 deraadt 593: snprintf(b, sizeof b, "%*.*f", w, d, f);
1.1 deraadt 594: if (strlen(b) > w) {
595: while (--w >= 0)
596: addch('*');
597: return;
598: }
599: addstr(b);
600: }
601:
602: static void
1.67 canacar 603: getinfo(struct Info *si)
1.1 deraadt 604: {
1.89 cheloha 605: static int cpustats_mib[3] = { CTL_KERN, KERN_CPUSTATS, 0 };
1.31 deraadt 606: static int nchstats_mib[2] = { CTL_KERN, KERN_NCHSTATS };
607: static int uvmexp_mib[2] = { CTL_VM, VM_UVMEXP };
608: static int vmtotal_mib[2] = { CTL_VM, VM_METER };
1.89 cheloha 609: struct cpustats cs;
610: int mib[4], i, j;
1.1 deraadt 611: size_t size;
612:
1.2 deraadt 613: dkreadstats();
1.51 aaron 614:
615: for (i = 0; i < nintr; i++) {
616: mib[0] = CTL_KERN;
617: mib[1] = KERN_INTRCNT;
618: mib[2] = KERN_INTRCNT_CNT;
619: mib[3] = i;
1.67 canacar 620: size = sizeof(si->intrcnt[i]);
1.91 deraadt 621: if (sysctl(mib, 4, &si->intrcnt[i], &size, NULL, 0) == -1) {
1.67 canacar 622: si->intrcnt[i] = 0;
1.53 deraadt 623: }
1.48 deraadt 624: }
1.51 aaron 625:
1.89 cheloha 626: memset(&si->cpustats.cs_time, 0, sizeof(si->cpustats.cs_time));
627: for (i = 0; i < ncpu; i++) {
628: cpustats_mib[2] = i;
629: size = sizeof(cs);
1.91 deraadt 630: if (sysctl(cpustats_mib, 3, &cs, &size, NULL, 0) == -1) {
1.89 cheloha 631: error("Can't get KERN_CPUSTATS: %s\n", strerror(errno));
632: memset(&si->cpustats, 0, sizeof(si->cpustats));
633: }
634: if ((cs.cs_flags & CPUSTATS_ONLINE) == 0)
635: continue; /* omit totals for offline CPUs */
636: for (j = 0; j < nitems(cs.cs_time); j++)
637: si->cpustats.cs_time[j] += cs.cs_time[j];
1.31 deraadt 638: }
639:
1.67 canacar 640: size = sizeof(si->nchstats);
1.91 deraadt 641: if (sysctl(nchstats_mib, 2, &si->nchstats, &size, NULL, 0) == -1) {
1.31 deraadt 642: error("Can't get KERN_NCHSTATS: %s\n", strerror(errno));
1.81 guenther 643: memset(&si->nchstats, 0, sizeof(si->nchstats));
1.31 deraadt 644: }
645:
1.67 canacar 646: size = sizeof(si->uvmexp);
1.91 deraadt 647: if (sysctl(uvmexp_mib, 2, &si->uvmexp, &size, NULL, 0) == -1) {
1.31 deraadt 648: error("Can't get VM_UVMEXP: %s\n", strerror(errno));
1.81 guenther 649: memset(&si->uvmexp, 0, sizeof(si->uvmexp));
1.31 deraadt 650: }
651:
1.67 canacar 652: size = sizeof(si->Total);
1.91 deraadt 653: if (sysctl(vmtotal_mib, 2, &si->Total, &size, NULL, 0) == -1) {
1.31 deraadt 654: error("Can't get VM_METER: %s\n", strerror(errno));
1.81 guenther 655: memset(&si->Total, 0, sizeof(si->Total));
1.1 deraadt 656: }
657: }
658:
659: static void
1.67 canacar 660: allocinfo(struct Info *si)
1.1 deraadt 661: {
1.67 canacar 662: memset(si, 0, sizeof(*si));
1.81 guenther 663: si->intrcnt = calloc(nintr, sizeof(*si->intrcnt));
1.67 canacar 664: if (si->intrcnt == NULL)
1.83 otto 665: err(2, NULL);
1.1 deraadt 666: }
667:
668: static void
1.35 deraadt 669: copyinfo(struct Info *from, struct Info *to)
1.1 deraadt 670: {
1.81 guenther 671: uint64_t *intrcnt;
1.1 deraadt 672:
1.2 deraadt 673: intrcnt = to->intrcnt;
1.1 deraadt 674: *to = *from;
1.81 guenther 675: memcpy(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof(*intrcnt));
1.1 deraadt 676: }
677:
678: static void
1.35 deraadt 679: dinfo(int dn, int c)
1.1 deraadt 680: {
1.90 tedu 681: double words, atime, etime;
682:
683: etime = naptime;
1.1 deraadt 684:
1.32 tdeval 685: c += DISKCOL;
1.2 deraadt 686:
687: /* time busy in disk activity */
688: atime = (double)cur.dk_time[dn].tv_sec +
1.31 deraadt 689: ((double)cur.dk_time[dn].tv_usec / (double)1000000);
1.2 deraadt 690:
1.44 tedu 691: /* # of K transferred */
692: words = (cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) / 1024.0;
1.2 deraadt 693:
694: putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
1.44 tedu 695: putint((int)((float)(cur.dk_rxfer[dn] + cur.dk_wxfer[dn])/etime+0.5),
696: DISKROW + 2, c, 5);
1.68 deraadt 697: putintmk((int)(words/etime + 0.5), DISKROW + 3, c, 5);
1.2 deraadt 698: putfloat(atime/etime, DISKROW + 4, c, 5, 1, 1);
1.64 canacar 699: }
700:
701:
702:
703: int
704: select_vm(void)
705: {
706: num_disp = 0;
707: return (0);
708: }
709:
710: int
711: read_vm(void)
712: {
713: if (state == TIME)
714: copyinfo(&s3, &s1);
715: fetchkre();
1.70 deraadt 716: fetchifstat();
1.64 canacar 717: if (state == TIME)
718: dkswap();
719: num_disp = 0;
720: return 0;
721: }
722:
723:
724: void
725: print_vm(void)
726: {
727: copyinfo(&s3, &s);
728: labelkre();
729: showkre();
1.1 deraadt 730: }