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