vmstat.c revision 1.85 1 /* $NetBSD: vmstat.c,v 1.85 2019/01/25 15:31:11 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1983, 1989, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 1/12/94";
36 #endif
37 __RCSID("$NetBSD: vmstat.c,v 1.85 2019/01/25 15:31:11 christos Exp $");
38 #endif /* not lint */
39
40 /*
41 * Cursed vmstat -- from Robert Elz.
42 */
43
44 #include <sys/param.h>
45 #include <sys/uio.h>
46 #include <sys/namei.h>
47 #include <sys/sysctl.h>
48 #include <sys/evcnt.h>
49
50 #include <uvm/uvm_extern.h>
51
52 #include <errno.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <util.h>
56 #include <curses.h>
57
58 #include "systat.h"
59 #include "extern.h"
60 #include "drvstats.h"
61 #include "utmpentry.h"
62 #include "vmstat.h"
63
64 static struct Info {
65 struct uvmexp_sysctl uvmexp;
66 struct vmtotal Total;
67 struct nchstats nchstats;
68 long nchcount;
69 long *intrcnt;
70 u_int64_t *evcnt;
71 } s, s1, s2, z;
72
73 enum display_mode display_mode = TIME;
74
75 static void allocinfo(struct Info *);
76 static void copyinfo(struct Info *, struct Info *);
77 static float cputime(int);
78 static void dinfo(int, int, int);
79 static void getinfo(struct Info *);
80 static int ucount(void);
81
82 static char buf[26];
83 static u_int64_t temp;
84 static int nintr;
85 static long *intrloc;
86 static char **intrname;
87 static int nextintsrow;
88 static int disk_horiz = 1;
89 static u_int nbuf;
90
91 WINDOW *
92 openvmstat(void)
93 {
94 return (stdscr);
95 }
96
97 void
98 closevmstat(WINDOW *w)
99 {
100
101 if (w == NULL)
102 return;
103 wclear(w);
104 wrefresh(w);
105 }
106
107
108 static struct nlist namelist[] = {
109 #define X_INTRNAMES 0
110 { .n_name = "_intrnames" },
111 #define X_EINTRNAMES 1
112 { .n_name = "_eintrnames" },
113 #define X_INTRCNT 2
114 { .n_name = "_intrcnt" },
115 #define X_EINTRCNT 3
116 { .n_name = "_eintrcnt" },
117 #define X_ALLEVENTS 4
118 { .n_name = "_allevents" },
119 { .n_name = NULL }
120 };
121
122 /*
123 * These constants define where the major pieces are laid out
124 */
125 #define STATROW 0 /* uses 1 row and 68 cols */
126 #define STATCOL 2
127 #define MEMROW 9 /* uses 5 rows and 31 cols */
128 #define MEMCOL 0
129 #define PAGEROW 2 /* uses 4 rows and 26 cols */
130 #define PAGECOL 54
131 #define INTSROW 9 /* uses all rows to bottom and 17 cols */
132 #define INTSCOL 40
133 #define INTSCOLEND (VMSTATCOL - 0)
134 #define PROCSROW 2 /* uses 2 rows and 20 cols */
135 #define PROCSCOL 0
136 #define GENSTATROW 2 /* uses 2 rows and 30 cols */
137 #define GENSTATCOL 17
138 #define VMSTATROW 7 /* uses 17 rows and 15 cols */
139 #define VMSTATCOL 64
140 #define GRAPHROW 5 /* uses 3 rows and 51 cols */
141 #define GRAPHCOL 0
142 #define NAMEIROW 15 /* uses 3 rows and 38 cols (must be MEMROW + 5 + 1) */
143 #define NAMEICOL 0
144 #define DISKROW 19 /* uses 5 rows and 50 cols (for 9 drives) */
145 #define DISKCOL 0
146 #define DISKCOLWIDTH 8
147 #define DISKCOLEND INTSCOL
148
149 typedef struct intr_evcnt intr_evcnt_t;
150 struct intr_evcnt {
151 char *ie_group;
152 char *ie_name;
153 u_int64_t *ie_count; /* kernel address... */
154 int ie_loc; /* screen row */
155 } *ie_head;
156 int nevcnt;
157
158 static void
159 get_interrupt_events(void)
160 {
161 struct evcntlist allevents;
162 struct evcnt evcnt, *evptr;
163 intr_evcnt_t *ie;
164 intr_evcnt_t *n;
165
166 if (!NREAD(X_ALLEVENTS, &allevents, sizeof allevents))
167 return;
168 evptr = TAILQ_FIRST(&allevents);
169 for (; evptr != NULL; evptr = TAILQ_NEXT(&evcnt, ev_list)) {
170 if (!KREAD(evptr, &evcnt, sizeof evcnt))
171 return;
172 if (evcnt.ev_type != EVCNT_TYPE_INTR)
173 continue;
174 n = realloc(ie_head, sizeof *ie * (nevcnt + 1));
175 if (n == NULL) {
176 error("realloc failed");
177 die(0);
178 }
179 ie_head = n;
180 ie = ie_head + nevcnt;
181 ie->ie_group = malloc(evcnt.ev_grouplen + 1);
182 ie->ie_name = malloc(evcnt.ev_namelen + 1);
183 if (ie->ie_group == NULL || ie->ie_name == NULL)
184 return;
185 if (!KREAD(evcnt.ev_group, ie->ie_group, evcnt.ev_grouplen + 1))
186 return;
187 if (!KREAD(evcnt.ev_name, ie->ie_name, evcnt.ev_namelen + 1))
188 return;
189 ie->ie_count = &evptr->ev_count;
190 ie->ie_loc = 0;
191 nevcnt++;
192 }
193 }
194
195 int
196 initvmstat(void)
197 {
198 static char *intrnamebuf;
199 char *cp;
200 int i;
201
202 if (intrnamebuf)
203 free(intrnamebuf);
204 if (intrname)
205 free(intrname);
206 if (intrloc)
207 free(intrloc);
208
209 if (namelist[0].n_type == 0) {
210 if (kvm_nlist(kd, namelist) &&
211 namelist[X_ALLEVENTS].n_type == 0) {
212 nlisterr(namelist);
213 return(0);
214 }
215 }
216 hertz = stathz ? stathz : hz;
217 if (!drvinit(1))
218 return(0);
219
220 /* Old style interrupt counts - deprecated */
221 nintr = (namelist[X_EINTRCNT].n_value -
222 namelist[X_INTRCNT].n_value) / sizeof (long);
223 if (nintr) {
224 intrloc = calloc(nintr, sizeof (long));
225 intrname = calloc(nintr, sizeof (long));
226 intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
227 namelist[X_INTRNAMES].n_value);
228 if (intrnamebuf == NULL || intrname == 0 || intrloc == 0) {
229 error("Out of memory\n");
230 nintr = 0;
231 return(0);
232 }
233 NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
234 NVAL(X_INTRNAMES));
235 for (cp = intrnamebuf, i = 0; i < nintr; i++) {
236 intrname[i] = cp;
237 cp += strlen(cp) + 1;
238 }
239 }
240
241 /* event counter interrupt counts */
242 get_interrupt_events();
243
244 nextintsrow = INTSROW + 1;
245 allocinfo(&s);
246 allocinfo(&s1);
247 allocinfo(&s2);
248 allocinfo(&z);
249
250 getinfo(&s2);
251 copyinfo(&s2, &s1);
252 return(1);
253 }
254
255 void
256 fetchvmstat(void)
257 {
258 time_t now;
259
260 time(&now);
261 strlcpy(buf, ctime(&now), sizeof(buf));
262 buf[19] = '\0';
263 getinfo(&s);
264 }
265
266 static void
267 print_ie_title(int i)
268 {
269 int width, name_width, group_width;
270
271 width = INTSCOLEND - (INTSCOL + 9);
272 if (width <= 0)
273 return;
274
275 move(ie_head[i].ie_loc, INTSCOL + 9);
276 group_width = strlen(ie_head[i].ie_group);
277 name_width = strlen(ie_head[i].ie_name);
278 width -= group_width + 1 + name_width;
279 if (width < 0) {
280 /*
281 * Screen to narrow for full strings
282 * This is all rather horrid, in some cases there are a lot
283 * of events in the same group, and in others the event
284 * name is "intr". There are also names which need 7 or 8
285 * columns before they become meaningful.
286 * This is a bad compromise.
287 */
288 width = -width;
289 group_width -= (width + 1) / 2;
290 name_width -= width / 2;
291 /* some have the 'useful' name "intr", display their group */
292 if (strcasecmp(ie_head[i].ie_name, "intr") == 0) {
293 group_width += name_width + 1;
294 name_width = 0;
295 } else {
296 if (group_width <= 3 || name_width < 0) {
297 /* don't display group */
298 name_width += group_width + 1;
299 group_width = 0;
300 }
301 }
302 }
303
304 if (group_width != 0) {
305 printw("%-.*s", group_width, ie_head[i].ie_group);
306 if (name_width != 0)
307 printw(" ");
308 }
309 if (name_width != 0)
310 printw("%-.*s", name_width, ie_head[i].ie_name);
311 }
312
313 void
314 labelvmstat_top(void)
315 {
316
317 clear();
318
319 mvprintw(STATROW, STATCOL + 4, "users Load");
320
321 mvprintw(GENSTATROW, GENSTATCOL, " Csw Traps SysCal Intr Soft Fault");
322
323 mvprintw(GRAPHROW, GRAPHCOL,
324 " . %% Sy . %% Us . %% Ni . %% In . %% Id");
325 mvprintw(PROCSROW, PROCSCOL, "Proc:r d s");
326 mvprintw(GRAPHROW + 1, GRAPHCOL,
327 "| | | | | | | | | | |");
328
329 mvprintw(PAGEROW, PAGECOL + 8, "PAGING SWAPPING ");
330 mvprintw(PAGEROW + 1, PAGECOL, " in out in out ");
331 mvprintw(PAGEROW + 2, PAGECOL, " ops ");
332 mvprintw(PAGEROW + 3, PAGECOL, "pages ");
333 }
334
335 void
336 labelvmstat(void)
337 {
338 int i;
339
340 /* Top few lines first */
341
342 labelvmstat_top();
343
344 /* Left hand column */
345
346 mvprintw(MEMROW + 0, MEMCOL, "Anon %% zero ");
347 mvprintw(MEMROW + 1, MEMCOL, "Exec %% wired ");
348 mvprintw(MEMROW + 2, MEMCOL, "File %% inact ");
349 mvprintw(MEMROW + 3, MEMCOL, "Meta %% bufs ");
350 mvprintw(MEMROW + 4, MEMCOL, " (kB) real swaponly free");
351 mvprintw(MEMROW + 5, MEMCOL, "Active ");
352
353 mvprintw(NAMEIROW, NAMEICOL, "Namei Sys-cache Proc-cache");
354 mvprintw(NAMEIROW + 1, NAMEICOL,
355 " Calls hits %% hits %%");
356
357 mvprintw(DISKROW, DISKCOL, "%*s", DISKCOLWIDTH, "Disks:");
358 if (disk_horiz) {
359 mvprintw(DISKROW + 1, DISKCOL + 1, "seeks");
360 mvprintw(DISKROW + 2, DISKCOL + 1, "xfers");
361 mvprintw(DISKROW + 3, DISKCOL + 1, "bytes");
362 mvprintw(DISKROW + 4, DISKCOL + 1, "%%busy");
363 } else {
364 mvprintw(DISKROW, DISKCOL + 1 * DISKCOLWIDTH, "%*s", DISKCOLWIDTH, "seeks");
365 mvprintw(DISKROW, DISKCOL + 2 * DISKCOLWIDTH, "%*s", DISKCOLWIDTH, "xfers");
366 mvprintw(DISKROW, DISKCOL + 3 * DISKCOLWIDTH, "%*s", DISKCOLWIDTH, "bytes");
367 mvprintw(DISKROW, DISKCOL + 4 * DISKCOLWIDTH, "%*s", DISKCOLWIDTH, "%busy");
368 }
369
370 /* Middle column */
371
372 mvprintw(INTSROW, INTSCOL + 9, "Interrupts");
373 for (i = 0; i < nintr; i++) {
374 if (intrloc[i] == 0)
375 continue;
376 mvprintw(intrloc[i], INTSCOL + 9, "%-.*s",
377 INTSCOLEND - (INTSCOL + 9), intrname[i]);
378 }
379 for (i = 0; i < nevcnt; i++) {
380 if (ie_head[i].ie_loc == 0)
381 continue;
382 print_ie_title(i);
383 }
384
385 /* Right hand column */
386
387 mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks");
388 mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw");
389 mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm");
390 mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait");
391 mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck");
392 mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok");
393 mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram");
394 mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy");
395 mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp");
396 mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod");
397 mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow");
398 mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin");
399 mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg");
400 mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg");
401 mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "flnan");
402 mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre");
403
404 if (LINES - 1 > VMSTATROW + 16)
405 mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn");
406 }
407
408 #define X(s, s1, fld) {temp = (s).fld[i]; (s).fld[i] -= (s1).fld[i]; \
409 if (display_mode == TIME) (s1).fld[i] = temp;}
410 #define Z(s, s1, fld) {temp = (s).nchstats.fld; \
411 (s).nchstats.fld -= (s1).nchstats.fld; \
412 if (display_mode == TIME) (s1).nchstats.fld = temp;}
413 #define PUTRATE(s, s1, fld, l, c, w) \
414 {temp = (s).fld; (s).fld -= (s1).fld; \
415 if (display_mode == TIME) (s1).fld = temp; \
416 putint((int)((float)(s).fld/etime + 0.5), l, c, w);}
417
418 static char cpuchar[CPUSTATES] = { '=' , '>', '-', '%', ' ' };
419 static char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_INTR, CP_IDLE };
420
421 void
422 show_vmstat_top(vmtotal_t *Total, uvmexp_sysctl_t *uvm, uvmexp_sysctl_t *uvm1)
423 {
424 float f1, f2;
425 int psiz;
426 int i, l, c;
427 struct {
428 struct uvmexp_sysctl *uvmexp;
429 } us, us1;
430
431 us.uvmexp = uvm;
432 us1.uvmexp = uvm1;
433
434 putint(ucount(), STATROW, STATCOL, 3);
435 putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
436 putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
437 putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
438 mvaddstr(STATROW, STATCOL + 53, buf);
439
440 putint(Total->t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
441 putint(Total->t_dw, PROCSROW + 1, PROCSCOL + 6, 3);
442 putint(Total->t_sl, PROCSROW + 1, PROCSCOL + 9, 3);
443
444 PUTRATE(us, us1, uvmexp->swtch, GENSTATROW + 1, GENSTATCOL - 1, 7);
445 PUTRATE(us, us1, uvmexp->traps, GENSTATROW + 1, GENSTATCOL + 7, 6);
446 PUTRATE(us, us1, uvmexp->syscalls, GENSTATROW + 1, GENSTATCOL + 14, 6);
447 PUTRATE(us, us1, uvmexp->intrs, GENSTATROW + 1, GENSTATCOL + 21, 5);
448 PUTRATE(us, us1, uvmexp->softs, GENSTATROW + 1, GENSTATCOL + 27, 6);
449 PUTRATE(us, us1, uvmexp->faults, GENSTATROW + 1, GENSTATCOL + 34, 6);
450
451 /*
452 * XXX it sure would be nice if this did what top(1) does and showed
453 * the utilization of each CPU on a separate line, though perhaps IFF
454 * the screen is tall enough
455 */
456 /* Last CPU state not calculated yet. */
457 for (f2 = 0.0, psiz = 0, c = 0; c < CPUSTATES; c++) {
458 i = cpuorder[c];
459 f1 = cputime(i);
460 f2 += f1;
461 l = (int) ((f2 + 1.0) / 2.0) - psiz;
462 if (c == 0)
463 putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
464 else
465 putfloat(f1, GRAPHROW, GRAPHCOL + 10 * c + 1, 5, 1, 0);
466 mvhline(GRAPHROW + 2, psiz, cpuchar[c], l);
467 psiz += l;
468 }
469
470 PUTRATE(us, us1, uvmexp->pageins, PAGEROW + 2, PAGECOL + 5, 5);
471 PUTRATE(us, us1, uvmexp->pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
472 PUTRATE(us, us1, uvmexp->pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
473 PUTRATE(us, us1, uvmexp->pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
474 }
475
476 void
477 showvmstat(void)
478 {
479 int inttotal;
480 int i, l, r, c;
481 static int failcnt = 0;
482 static int relabel = 0;
483 static int last_disks = 0;
484 static u_long bufmem;
485 struct buf_sysctl *buffers;
486 int mib[6];
487 size_t size;
488 int extraslop = 0;
489
490 if (relabel) {
491 labelvmstat();
492 relabel = 0;
493 }
494
495 cpuswap();
496 if (display_mode == TIME) {
497 drvswap();
498 if (toofast(&failcnt))
499 return;
500 } else
501 etime = 1.0;
502
503 show_vmstat_top(&s.Total, &s.uvmexp, &s1.uvmexp);
504
505 /* Memory totals */
506 #define pgtokb(pg) ((pg) * (s.uvmexp.pagesize / 1024))
507
508 putint(pgtokb(s.uvmexp.anonpages), MEMROW + 0, MEMCOL + 7, 10);
509 putint((s.uvmexp.anonpages * 100 + 0.5) / s.uvmexp.npages, MEMROW + 0, MEMCOL + 17, 4);
510
511 putint(pgtokb(s.uvmexp.zeropages), MEMROW + 0, MEMCOL + 30, 8);
512
513 putint(pgtokb(s.uvmexp.execpages), MEMROW + 1, MEMCOL + 7, 10);
514 putint((s.uvmexp.execpages * 100 + 0.5) / s.uvmexp.npages, MEMROW + 1, MEMCOL + 17, 4);
515
516 putint(pgtokb(s.uvmexp.wired), MEMROW + 1, MEMCOL + 30, 8);
517
518 putint(pgtokb(s.uvmexp.filepages), MEMROW + 2, MEMCOL + 7, 10);
519 putint((s.uvmexp.filepages * 100 + 0.5) / s.uvmexp.npages, MEMROW + 2, MEMCOL + 17, 4);
520
521 putint(pgtokb(s.uvmexp.inactive), MEMROW + 2, MEMCOL + 30, 8);
522
523 /* Get total size of metadata buffers */
524 size = sizeof(bufmem);
525 if (sysctlbyname("vm.bufmem", &bufmem, &size, NULL, 0) < 0) {
526 error("can't get buffers size: %s\n", strerror(errno));
527 return;
528 }
529
530 /* Get number of metadata buffers */
531 size = 0;
532 buffers = NULL;
533 mib[0] = CTL_KERN;
534 mib[1] = KERN_BUF;
535 mib[2] = KERN_BUF_ALL;
536 mib[3] = KERN_BUF_ALL;
537 mib[4] = (int)sizeof(struct buf_sysctl);
538 mib[5] = INT_MAX; /* we want them all */
539 again:
540 if (sysctl(mib, 6, NULL, &size, NULL, 0) < 0) {
541 error("can't get buffers size: %s\n", strerror(errno));
542 return;
543 }
544 if (size == 0) {
545 error("buffers size is zero: %s\n", strerror(errno));
546 return;
547 }
548 size += extraslop * sizeof(struct buf_sysctl);
549 buffers = malloc(size);
550 if (buffers == NULL) {
551 error("can't allocate buffers: %s\n", strerror(errno));
552 return;
553 }
554 if (sysctl(mib, 6, buffers, &size, NULL, 0) < 0) {
555 free(buffers);
556 if (extraslop == 0) {
557 extraslop = 100;
558 goto again;
559 }
560 error("can't get buffers: %s\n", strerror(errno));
561 return;
562 }
563 free(buffers); /* XXX there must be a better way! */
564 nbuf = size / sizeof(struct buf_sysctl);
565
566 putint((int) (bufmem / 1024), MEMROW + 3, MEMCOL + 5, 12);
567 putint((int) ((bufmem * 100) + 0.5) / s.uvmexp.pagesize / s.uvmexp.npages,
568 MEMROW + 3, MEMCOL + 17, 4);
569 putint(nbuf, MEMROW + 3, MEMCOL + 30, 8);
570
571 putint(pgtokb(s.uvmexp.active), MEMROW + 5, MEMCOL + 7, 10);
572 putint(pgtokb(s.uvmexp.swpgonly), MEMROW + 5, MEMCOL + 18, 10);
573 putint(pgtokb(s.uvmexp.free), MEMROW + 5, MEMCOL + 28, 10);
574
575 #undef pgtokb
576
577 /* Namei cache */
578 Z(s, s1, ncs_goodhits); Z(s, s1, ncs_badhits); Z(s, s1, ncs_miss);
579 Z(s, s1, ncs_long); Z(s, s1, ncs_pass2); Z(s, s1, ncs_2passes);
580 s.nchcount = s.nchstats.ncs_goodhits + s.nchstats.ncs_badhits +
581 s.nchstats.ncs_miss + s.nchstats.ncs_long +
582 s.nchstats.ncs_pass2 + s.nchstats.ncs_2passes;
583 if (display_mode == TIME)
584 s1.nchcount = s.nchcount;
585
586 putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
587 putint(s.nchstats.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
588 #define nz(x) ((x) ? (x) : 1)
589 putfloat(s.nchstats.ncs_goodhits * 100.0 / nz(s.nchcount),
590 NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
591 putint(s.nchstats.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
592 putfloat(s.nchstats.ncs_pass2 * 100.0 / nz(s.nchcount),
593 NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
594 #undef nz
595
596 /* Disks */
597 for (l = 0, i = 0, r = DISKROW, c = DISKCOL;
598 i < (int)ndrive; i++) {
599 if (!drv_select[i])
600 continue;
601
602 if (disk_horiz)
603 c += DISKCOLWIDTH;
604 else
605 r++;
606 if (c + DISKCOLWIDTH > DISKCOLEND) {
607 if (disk_horiz && LINES - 1 - DISKROW >
608 (DISKCOLEND - DISKCOL) / DISKCOLWIDTH) {
609 disk_horiz = 0;
610 relabel = 1;
611 }
612 break;
613 }
614 if (r >= LINES - 1) {
615 if (!disk_horiz && LINES - 1 - DISKROW <
616 (DISKCOLEND - DISKCOL) / DISKCOLWIDTH) {
617 disk_horiz = 1;
618 relabel = 1;
619 }
620 break;
621 }
622 l++;
623
624 dinfo(i, r, c);
625 }
626 /* blank out if we lost any disks */
627 for (i = l; i < last_disks; i++) {
628 int j;
629 if (disk_horiz)
630 c += DISKCOLWIDTH;
631 else
632 r++;
633 for (j = 0; j < 5; j++) {
634 if (disk_horiz)
635 mvprintw(r+j, c, "%*s", DISKCOLWIDTH, "");
636 else
637 mvprintw(r, c+j*DISKCOLWIDTH, "%*s", DISKCOLWIDTH, "");
638 }
639 }
640 last_disks = l;
641
642 /* Interrupts */
643 failcnt = 0;
644 inttotal = 0;
645 for (i = 0; i < nintr; i++) {
646 if (s.intrcnt[i] == 0)
647 continue;
648 if (intrloc[i] == 0) {
649 if (nextintsrow == LINES)
650 continue;
651 intrloc[i] = nextintsrow++;
652 mvprintw(intrloc[i], INTSCOL + 9, "%-.*s",
653 INTSCOLEND - (INTSCOL + 9), intrname[i]);
654 }
655 X(s, s1, intrcnt);
656 l = (int)((float)s.intrcnt[i]/etime + 0.5);
657 inttotal += l;
658 putint(l, intrloc[i], INTSCOL, 8);
659 }
660
661 for (i = 0; i < nevcnt; i++) {
662 if (s.evcnt[i] == 0)
663 continue;
664 if (ie_head[i].ie_loc == 0) {
665 if (nextintsrow == LINES)
666 continue;
667 ie_head[i].ie_loc = nextintsrow++;
668 print_ie_title(i);
669 }
670 X(s, s1, evcnt);
671 l = (int)((float)s.evcnt[i]/etime + 0.5);
672 inttotal += l;
673 putint(l, ie_head[i].ie_loc, INTSCOL, 8);
674 }
675 putint(inttotal, INTSROW, INTSCOL, 8);
676
677 PUTRATE(s, s1, uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
678 PUTRATE(s, s1, uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
679 PUTRATE(s, s1, uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
680 PUTRATE(s, s1, uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
681 PUTRATE(s, s1, uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
682 PUTRATE(s, s1, uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
683 PUTRATE(s, s1, uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
684 PUTRATE(s, s1, uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
685 PUTRATE(s, s1, uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
686 PUTRATE(s, s1, uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
687 PUTRATE(s, s1, uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
688 putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
689 putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
690 putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
691 putint(s.uvmexp.fltnoanon, VMSTATROW + 14, VMSTATCOL, 9);
692 PUTRATE(s, s1, uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
693 if (LINES - 1 > VMSTATROW + 16)
694 PUTRATE(s, s1, uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
695
696 }
697
698 void
699 vmstat_boot(char *args)
700 {
701 copyinfo(&z, &s1);
702 display_mode = BOOT;
703 }
704
705 void
706 vmstat_run(char *args)
707 {
708 copyinfo(&s1, &s2);
709 display_mode = RUN;
710 }
711
712 void
713 vmstat_time(char *args)
714 {
715 display_mode = TIME;
716 }
717
718 void
719 vmstat_zero(char *args)
720 {
721 if (display_mode == RUN)
722 getinfo(&s1);
723 }
724
725 /* calculate number of users on the system */
726 static int
727 ucount(void)
728 {
729 static int onusers = -1;
730 int nusers = 0;
731 struct utmpentry *ehead;
732
733 nusers = getutentries(NULL, &ehead);
734
735 if (nusers != onusers) {
736 if (nusers == 1)
737 mvprintw(STATROW, STATCOL + 8, " ");
738 else
739 mvprintw(STATROW, STATCOL + 8, "s");
740 }
741 onusers = nusers;
742 return (nusers);
743 }
744
745 static float
746 cputime(int indx)
747 {
748 double t;
749 int i;
750
751 t = 0;
752 for (i = 0; i < CPUSTATES; i++)
753 t += cur.cp_time[i];
754 if (t == 0.0)
755 t = 1.0;
756 return (cur.cp_time[indx] * 100.0 / t);
757 }
758
759 void
760 puthumanint(u_int64_t n, int l, int c, int w)
761 {
762 char b[128];
763
764 if (move(l, c) != OK)
765 return;
766 if (n == 0) {
767 hline(' ', w);
768 return;
769 }
770 if (humanize_number(b, w, n, "", HN_AUTOSCALE, HN_NOSPACE) == -1 ) {
771 hline('*', w);
772 return;
773 }
774 printw("%*s", w, b);
775 }
776
777 void
778 putint(int n, int l, int c, int w)
779 {
780 char b[128];
781
782 if (move(l, c) != OK)
783 return;
784 if (n == 0) {
785 hline(' ', w);
786 return;
787 }
788 (void)snprintf(b, sizeof b, "%*d", w, n);
789 if ((int)strlen(b) > w) {
790 if (display_mode == TIME)
791 hline('*', w);
792 else
793 puthumanint(n, l, c, w);
794 return;
795 }
796 addstr(b);
797 }
798
799 void
800 putfloat(double f, int l, int c, int w, int d, int nz)
801 {
802 char b[128];
803
804 if (move(l, c) != OK)
805 return;
806 if (nz && f == 0.0) {
807 hline(' ', w);
808 return;
809 }
810 (void)snprintf(b, sizeof b, "%*.*f", w, d, f);
811 if ((int)strlen(b) > w) {
812 hline('*', w);
813 return;
814 }
815 addstr(b);
816 }
817
818 static void
819 getinfo(struct Info *stats)
820 {
821 int mib[2];
822 size_t size;
823 int i;
824
825 cpureadstats();
826 drvreadstats();
827 size = sizeof(stats->nchstats);
828 if (sysctlbyname("vfs.namecache_stats", &stats->nchstats, &size,
829 NULL, 0) < 0) {
830 error("can't get namecache statistics: %s\n", strerror(errno));
831 memset(&stats->nchstats, 0, sizeof(stats->nchstats));
832 }
833 if (nintr)
834 NREAD(X_INTRCNT, stats->intrcnt, nintr * sizeof(long));
835 for (i = 0; i < nevcnt; i++)
836 KREAD(ie_head[i].ie_count, &stats->evcnt[i],
837 sizeof stats->evcnt[i]);
838 size = sizeof(stats->uvmexp);
839 mib[0] = CTL_VM;
840 mib[1] = VM_UVMEXP2;
841 if (sysctl(mib, 2, &stats->uvmexp, &size, NULL, 0) < 0) {
842 error("can't get uvmexp: %s\n", strerror(errno));
843 memset(&stats->uvmexp, 0, sizeof(stats->uvmexp));
844 }
845 size = sizeof(stats->Total);
846 mib[0] = CTL_VM;
847 mib[1] = VM_METER;
848 if (sysctl(mib, 2, &stats->Total, &size, NULL, 0) < 0) {
849 error("Can't get kernel info: %s\n", strerror(errno));
850 memset(&stats->Total, 0, sizeof(stats->Total));
851 }
852 }
853
854 static void
855 allocinfo(struct Info *stats)
856 {
857
858 if (nintr &&
859 (stats->intrcnt = calloc(nintr, sizeof(long))) == NULL) {
860 error("calloc failed");
861 die(0);
862 }
863 if ((stats->evcnt = calloc(nevcnt, sizeof(u_int64_t))) == NULL) {
864 error("calloc failed");
865 die(0);
866 }
867 }
868
869 static void
870 copyinfo(struct Info *from, struct Info *to)
871 {
872 long *intrcnt;
873 u_int64_t *evcnt;
874
875 intrcnt = to->intrcnt;
876 evcnt = to->evcnt;
877 *to = *from;
878 memmove(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof *intrcnt);
879 memmove(to->evcnt = evcnt, from->evcnt, nevcnt * sizeof *evcnt);
880 }
881
882 static void
883 dinfo(int dn, int r, int c)
884 {
885 double atime, dtime;
886 #define ADV if (disk_horiz) r++; else c += DISKCOLWIDTH
887
888 /* elapsed time for disk stats */
889 dtime = etime;
890 if (cur.timestamp[dn].tv_sec || cur.timestamp[dn].tv_usec) {
891 dtime = (double)cur.timestamp[dn].tv_sec +
892 ((double)cur.timestamp[dn].tv_usec / (double)1000000);
893 }
894
895 mvprintw(r, c, "%*.*s", DISKCOLWIDTH, DISKCOLWIDTH, dr_name[dn]);
896 ADV;
897
898 putint((int)(cur.seek[dn]/dtime+0.5), r, c, DISKCOLWIDTH);
899 ADV;
900 putint((int)((cur.rxfer[dn]+cur.wxfer[dn])/dtime+0.5),
901 r, c, DISKCOLWIDTH);
902 ADV;
903 puthumanint((cur.rbytes[dn] + cur.wbytes[dn]) / dtime + 0.5,
904 r, c, DISKCOLWIDTH);
905 ADV;
906
907 /* time busy in disk activity */
908 atime = cur.time[dn].tv_sec + cur.time[dn].tv_usec / 1000000.0;
909 atime = atime * 100.0 / dtime;
910 if (atime >= 100)
911 putint(100, r, c, DISKCOLWIDTH);
912 else
913 putfloat(atime, r, c, DISKCOLWIDTH, 1, 1);
914 }
915