vmstat.c revision 1.79 1 /* $NetBSD: vmstat.c,v 1.79 2014/06/14 20:10:42 joerg 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.79 2014/06/14 20:10:42 joerg 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_sysctl 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
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 4 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 14 /* uses 3 rows and 38 cols */
143 #define NAMEICOL 0
144 #define DISKROW 18 /* uses 5 rows and 50 cols (for 9 drives) */
145 #define DISKCOL 0
146 #define DISKCOLWIDTH 6
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 if (namelist[0].n_type == 0) {
216 error("No namelist");
217 return(0);
218 }
219 }
220 hertz = stathz ? stathz : hz;
221 if (!drvinit(1))
222 return(0);
223
224 /* Old style interrupt counts - deprecated */
225 nintr = (namelist[X_EINTRCNT].n_value -
226 namelist[X_INTRCNT].n_value) / sizeof (long);
227 if (nintr) {
228 intrloc = calloc(nintr, sizeof (long));
229 intrname = calloc(nintr, sizeof (long));
230 intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
231 namelist[X_INTRNAMES].n_value);
232 if (intrnamebuf == NULL || intrname == 0 || intrloc == 0) {
233 error("Out of memory\n");
234 nintr = 0;
235 return(0);
236 }
237 NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
238 NVAL(X_INTRNAMES));
239 for (cp = intrnamebuf, i = 0; i < nintr; i++) {
240 intrname[i] = cp;
241 cp += strlen(cp) + 1;
242 }
243 }
244
245 /* event counter interrupt counts */
246 get_interrupt_events();
247
248 nextintsrow = INTSROW + 1;
249 allocinfo(&s);
250 allocinfo(&s1);
251 allocinfo(&s2);
252 allocinfo(&z);
253
254 getinfo(&s2);
255 copyinfo(&s2, &s1);
256 return(1);
257 }
258
259 void
260 fetchvmstat(void)
261 {
262 time_t now;
263
264 time(&now);
265 strlcpy(buf, ctime(&now), sizeof(buf));
266 buf[19] = '\0';
267 getinfo(&s);
268 }
269
270 static void
271 print_ie_title(int i)
272 {
273 int width, name_width, group_width;
274
275 width = INTSCOLEND - (INTSCOL + 9);
276 if (width <= 0)
277 return;
278
279 move(ie_head[i].ie_loc, INTSCOL + 9);
280 group_width = strlen(ie_head[i].ie_group);
281 name_width = strlen(ie_head[i].ie_name);
282 width -= group_width + 1 + name_width;
283 if (width < 0) {
284 /*
285 * Screen to narrow for full strings
286 * This is all rather horrid, in some cases there are a lot
287 * of events in the same group, and in others the event
288 * name is "intr". There are also names which need 7 or 8
289 * columns before they become meaningful.
290 * This is a bad compromise.
291 */
292 width = -width;
293 group_width -= (width + 1) / 2;
294 name_width -= width / 2;
295 /* some have the 'useful' name "intr", display their group */
296 if (strcasecmp(ie_head[i].ie_name, "intr") == 0) {
297 group_width += name_width + 1;
298 name_width = 0;
299 } else {
300 if (group_width <= 3 || name_width < 0) {
301 /* don't display group */
302 name_width += group_width + 1;
303 group_width = 0;
304 }
305 }
306 }
307
308 if (group_width != 0) {
309 printw("%-.*s", group_width, ie_head[i].ie_group);
310 if (name_width != 0)
311 printw(" ");
312 }
313 if (name_width != 0)
314 printw("%-.*s", name_width, ie_head[i].ie_name);
315 }
316
317 void
318 labelvmstat_top(void)
319 {
320
321 clear();
322
323 mvprintw(STATROW, STATCOL + 4, "users Load");
324
325 mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt");
326
327 mvprintw(GRAPHROW, GRAPHCOL,
328 " . %% Sy . %% Us . %% Ni . %% In . %% Id");
329 mvprintw(PROCSROW, PROCSCOL, "Proc:r d s");
330 mvprintw(GRAPHROW + 1, GRAPHCOL,
331 "| | | | | | | | | | |");
332
333 mvprintw(PAGEROW, PAGECOL + 8, "PAGING SWAPPING ");
334 mvprintw(PAGEROW + 1, PAGECOL, " in out in out ");
335 mvprintw(PAGEROW + 2, PAGECOL + 2, "ops");
336 mvprintw(PAGEROW + 3, PAGECOL, "pages");
337 }
338
339 void
340 labelvmstat(void)
341 {
342 int i;
343
344 /* Top few lines first */
345
346 labelvmstat_top();
347
348 /* Left hand column */
349
350 mvprintw(MEMROW, MEMCOL, " memory totals (in kB)");
351 mvprintw(MEMROW + 1, MEMCOL, " real virtual free");
352 mvprintw(MEMROW + 2, MEMCOL, "Active");
353 mvprintw(MEMROW + 3, MEMCOL, "All");
354
355 mvprintw(NAMEIROW, NAMEICOL, "Namei Sys-cache Proc-cache");
356 mvprintw(NAMEIROW + 1, NAMEICOL,
357 " Calls hits %% hits %%");
358
359 mvprintw(DISKROW, DISKCOL, "Disks:");
360 if (disk_horiz) {
361 mvprintw(DISKROW + 1, DISKCOL + 1, "seeks");
362 mvprintw(DISKROW + 2, DISKCOL + 1, "xfers");
363 mvprintw(DISKROW + 3, DISKCOL + 1, "bytes");
364 mvprintw(DISKROW + 4, DISKCOL + 1, "%%busy");
365 } else {
366 mvprintw(DISKROW, DISKCOL + 1 + 1 * DISKCOLWIDTH, "seeks");
367 mvprintw(DISKROW, DISKCOL + 1 + 2 * DISKCOLWIDTH, "xfers");
368 mvprintw(DISKROW, DISKCOL + 1 + 3 * DISKCOLWIDTH, "bytes");
369 mvprintw(DISKROW, DISKCOL + 1 + 4 * DISKCOLWIDTH, "%%busy");
370 }
371
372 /* Middle column */
373
374 mvprintw(INTSROW, INTSCOL + 9, "Interrupts");
375 for (i = 0; i < nintr; i++) {
376 if (intrloc[i] == 0)
377 continue;
378 mvprintw(intrloc[i], INTSCOL + 9, "%-.*s",
379 INTSCOLEND - (INTSCOL + 9), intrname[i]);
380 }
381 for (i = 0; i < nevcnt; i++) {
382 if (ie_head[i].ie_loc == 0)
383 continue;
384 print_ie_title(i);
385 }
386
387 /* Right hand column */
388
389 mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks");
390 mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw");
391 mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm");
392 mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait");
393 mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck");
394 mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok");
395 mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram");
396 mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy");
397 mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp");
398 mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod");
399 mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow");
400 mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin");
401 mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg");
402 mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg");
403 mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired");
404 mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre");
405
406 if (LINES - 1 > VMSTATROW + 16)
407 mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn");
408 }
409
410 #define X(s, s1, fld) {temp = (s).fld[i]; (s).fld[i] -= (s1).fld[i]; \
411 if (display_mode == TIME) (s1).fld[i] = temp;}
412 #define Z(s, s1, fld) {temp = (s).nchstats.fld; \
413 (s).nchstats.fld -= (s1).nchstats.fld; \
414 if (display_mode == TIME) (s1).nchstats.fld = temp;}
415 #define PUTRATE(s, s1, fld, l, c, w) \
416 {temp = (s).fld; (s).fld -= (s1).fld; \
417 if (display_mode == TIME) (s1).fld = temp; \
418 putint((int)((float)(s).fld/etime + 0.5), l, c, w);}
419 #define MAXFAIL 5
420
421 static char cpuchar[CPUSTATES] = { '=' , '>', '-', '%', ' ' };
422 static char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_INTR, CP_IDLE };
423
424 void
425 show_vmstat_top(vmtotal_t *Total, uvmexp_sysctl_t *uvm, uvmexp_sysctl_t *uvm1)
426 {
427 float f1, f2;
428 int psiz;
429 int i, l, c;
430 struct {
431 struct uvmexp_sysctl *uvmexp;
432 } us, us1;
433
434 us.uvmexp = uvm;
435 us1.uvmexp = uvm1;
436
437 putint(ucount(), STATROW, STATCOL, 3);
438 putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
439 putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
440 putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
441 mvaddstr(STATROW, STATCOL + 53, buf);
442
443 putint(Total->t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
444 putint(Total->t_dw, PROCSROW + 1, PROCSCOL + 6, 3);
445 putint(Total->t_sl, PROCSROW + 1, PROCSCOL + 9, 3);
446
447 PUTRATE(us, us1, uvmexp->swtch, GENSTATROW + 1, GENSTATCOL - 1, 7);
448 PUTRATE(us, us1, uvmexp->traps, GENSTATROW + 1, GENSTATCOL + 7, 6);
449 PUTRATE(us, us1, uvmexp->syscalls, GENSTATROW + 1, GENSTATCOL + 14, 6);
450 PUTRATE(us, us1, uvmexp->intrs, GENSTATROW + 1, GENSTATCOL + 21, 5);
451 PUTRATE(us, us1, uvmexp->softs, GENSTATROW + 1, GENSTATCOL + 27, 6);
452 PUTRATE(us, us1, uvmexp->faults, GENSTATROW + 1, GENSTATCOL + 34, 6);
453
454 /* Last CPU state not calculated yet. */
455 for (f2 = 0.0, psiz = 0, c = 0; c < CPUSTATES; c++) {
456 i = cpuorder[c];
457 f1 = cputime(i);
458 f2 += f1;
459 l = (int) ((f2 + 1.0) / 2.0) - psiz;
460 if (c == 0)
461 putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
462 else
463 putfloat(f1, GRAPHROW, GRAPHCOL + 10 * c + 1, 5, 1, 0);
464 mvhline(GRAPHROW + 2, psiz, cpuchar[c], l);
465 psiz += l;
466 }
467
468 PUTRATE(us, us1, uvmexp->pageins, PAGEROW + 2, PAGECOL + 5, 5);
469 PUTRATE(us, us1, uvmexp->pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
470 PUTRATE(us, us1, uvmexp->pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
471 PUTRATE(us, us1, uvmexp->pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
472 }
473
474 void
475 showvmstat(void)
476 {
477 int inttotal;
478 int i, l, r, c;
479 static int failcnt = 0;
480 static int relabel = 0;
481 static int last_disks = 0;
482 static char pigs[] = "pigs";
483
484 if (relabel) {
485 labelvmstat();
486 relabel = 0;
487 }
488
489 cpuswap();
490 if (display_mode == TIME) {
491 drvswap();
492 etime = cur.cp_etime;
493 /* < 5 ticks - ignore this trash */
494 if ((etime * hertz) < 1.0) {
495 if (failcnt++ <= MAXFAIL)
496 return;
497 clear();
498 mvprintw(2, 10, "The alternate system clock has died!");
499 mvprintw(3, 10, "Reverting to ``pigs'' display.");
500 move(CMDLINE, 0);
501 refresh();
502 failcnt = 0;
503 sleep(5);
504 command(pigs);
505 return;
506 }
507 } else
508 etime = 1.0;
509
510 show_vmstat_top(&s.Total, &s.uvmexp, &s1.uvmexp);
511
512 /* Memory totals */
513 #define pgtokb(pg) ((pg) * (s.uvmexp.pagesize / 1024))
514 putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 6, 9);
515 putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse), /* XXX */
516 MEMROW + 2, MEMCOL + 16, 9);
517 putint(pgtokb(s.uvmexp.npages - s.uvmexp.free),
518 MEMROW + 3, MEMCOL + 6, 9);
519 putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
520 MEMROW + 3, MEMCOL + 16, 9);
521 putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 26, 9);
522 putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
523 MEMROW + 3, MEMCOL + 26, 9);
524 #undef pgtokb
525
526 /* Namei cache */
527 Z(s, s1, ncs_goodhits); Z(s, s1, ncs_badhits); Z(s, s1, ncs_miss);
528 Z(s, s1, ncs_long); Z(s, s1, ncs_pass2); Z(s, s1, ncs_2passes);
529 s.nchcount = s.nchstats.ncs_goodhits + s.nchstats.ncs_badhits +
530 s.nchstats.ncs_miss + s.nchstats.ncs_long +
531 s.nchstats.ncs_pass2 + s.nchstats.ncs_2passes;
532 if (display_mode == TIME)
533 s1.nchcount = s.nchcount;
534
535 putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
536 putint(s.nchstats.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
537 #define nz(x) ((x) ? (x) : 1)
538 putfloat(s.nchstats.ncs_goodhits * 100.0 / nz(s.nchcount),
539 NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
540 putint(s.nchstats.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
541 putfloat(s.nchstats.ncs_pass2 * 100.0 / nz(s.nchcount),
542 NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
543 #undef nz
544
545 /* Disks */
546 for (l = 0, i = 0, r = DISKROW, c = DISKCOL;
547 i < (int)ndrive; i++) {
548 if (!drv_select[i])
549 continue;
550
551 if (disk_horiz)
552 c += DISKCOLWIDTH;
553 else
554 r++;
555 if (c + DISKCOLWIDTH > DISKCOLEND) {
556 if (disk_horiz && LINES - 1 - DISKROW >
557 (DISKCOLEND - DISKCOL) / DISKCOLWIDTH) {
558 disk_horiz = 0;
559 relabel = 1;
560 }
561 break;
562 }
563 if (r >= LINES - 1) {
564 if (!disk_horiz && LINES - 1 - DISKROW <
565 (DISKCOLEND - DISKCOL) / DISKCOLWIDTH) {
566 disk_horiz = 1;
567 relabel = 1;
568 }
569 break;
570 }
571 l++;
572
573 dinfo(i, r, c);
574 }
575 /* blank out if we lost any disks */
576 for (i = l; i < last_disks; i++) {
577 int j;
578 if (disk_horiz)
579 c += DISKCOLWIDTH;
580 else
581 r++;
582 for (j = 0; j < 5; j++) {
583 if (disk_horiz)
584 mvprintw(r+j, c, "%*s", DISKCOLWIDTH, "");
585 else
586 mvprintw(r, c+j*DISKCOLWIDTH, "%*s", DISKCOLWIDTH, "");
587 }
588 }
589 last_disks = l;
590
591 /* Interrupts */
592 failcnt = 0;
593 inttotal = 0;
594 for (i = 0; i < nintr; i++) {
595 if (s.intrcnt[i] == 0)
596 continue;
597 if (intrloc[i] == 0) {
598 if (nextintsrow == LINES)
599 continue;
600 intrloc[i] = nextintsrow++;
601 mvprintw(intrloc[i], INTSCOL + 9, "%-.*s",
602 INTSCOLEND - (INTSCOL + 9), intrname[i]);
603 }
604 X(s, s1, intrcnt);
605 l = (int)((float)s.intrcnt[i]/etime + 0.5);
606 inttotal += l;
607 putint(l, intrloc[i], INTSCOL, 8);
608 }
609
610 for (i = 0; i < nevcnt; i++) {
611 if (s.evcnt[i] == 0)
612 continue;
613 if (ie_head[i].ie_loc == 0) {
614 if (nextintsrow == LINES)
615 continue;
616 ie_head[i].ie_loc = nextintsrow++;
617 print_ie_title(i);
618 }
619 X(s, s1, evcnt);
620 l = (int)((float)s.evcnt[i]/etime + 0.5);
621 inttotal += l;
622 putint(l, ie_head[i].ie_loc, INTSCOL, 8);
623 }
624 putint(inttotal, INTSROW, INTSCOL, 8);
625
626 PUTRATE(s, s1, uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
627 PUTRATE(s, s1, uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
628 PUTRATE(s, s1, uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
629 PUTRATE(s, s1, uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
630 PUTRATE(s, s1, uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
631 PUTRATE(s, s1, uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
632 PUTRATE(s, s1, uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
633 PUTRATE(s, s1, uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
634 PUTRATE(s, s1, uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
635 PUTRATE(s, s1, uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
636 PUTRATE(s, s1, uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
637 putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
638 putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
639 putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
640 putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9);
641 PUTRATE(s, s1, uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
642 if (LINES - 1 > VMSTATROW + 16)
643 PUTRATE(s, s1, uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
644
645 }
646
647 void
648 vmstat_boot(char *args)
649 {
650 copyinfo(&z, &s1);
651 display_mode = BOOT;
652 }
653
654 void
655 vmstat_run(char *args)
656 {
657 copyinfo(&s1, &s2);
658 display_mode = RUN;
659 }
660
661 void
662 vmstat_time(char *args)
663 {
664 display_mode = TIME;
665 }
666
667 void
668 vmstat_zero(char *args)
669 {
670 if (display_mode == RUN)
671 getinfo(&s1);
672 }
673
674 /* calculate number of users on the system */
675 static int
676 ucount(void)
677 {
678 static int onusers = -1;
679 int nusers = 0;
680 struct utmpentry *ehead;
681
682 nusers = getutentries(NULL, &ehead);
683
684 if (nusers != onusers) {
685 if (nusers == 1)
686 mvprintw(STATROW, STATCOL + 8, " ");
687 else
688 mvprintw(STATROW, STATCOL + 8, "s");
689 }
690 onusers = nusers;
691 return (nusers);
692 }
693
694 static float
695 cputime(int indx)
696 {
697 double t;
698 int i;
699
700 t = 0;
701 for (i = 0; i < CPUSTATES; i++)
702 t += cur.cp_time[i];
703 if (t == 0.0)
704 t = 1.0;
705 return (cur.cp_time[indx] * 100.0 / t);
706 }
707
708 void
709 puthumanint(u_int64_t n, int l, int c, int w)
710 {
711 char b[128];
712
713 if (move(l, c) != OK)
714 return;
715 if (n == 0) {
716 hline(' ', w);
717 return;
718 }
719 if (humanize_number(b, w, n, "", HN_AUTOSCALE, HN_NOSPACE) == -1 ) {
720 hline('*', w);
721 return;
722 }
723 printw("%*s", w, b);
724 }
725
726 void
727 putint(int n, int l, int c, int w)
728 {
729 char b[128];
730
731 if (move(l, c) != OK)
732 return;
733 if (n == 0) {
734 hline(' ', w);
735 return;
736 }
737 (void)snprintf(b, sizeof b, "%*d", w, n);
738 if ((int)strlen(b) > w) {
739 if (display_mode == TIME)
740 hline('*', w);
741 else
742 puthumanint(n, l, c, w);
743 return;
744 }
745 addstr(b);
746 }
747
748 void
749 putfloat(double f, int l, int c, int w, int d, int nz)
750 {
751 char b[128];
752
753 if (move(l, c) != OK)
754 return;
755 if (nz && f == 0.0) {
756 hline(' ', w);
757 return;
758 }
759 (void)snprintf(b, sizeof b, "%*.*f", w, d, f);
760 if ((int)strlen(b) > w) {
761 hline('*', w);
762 return;
763 }
764 addstr(b);
765 }
766
767 static void
768 getinfo(struct Info *stats)
769 {
770 int mib[2];
771 size_t size;
772 int i;
773
774 cpureadstats();
775 drvreadstats();
776 size = sizeof(stats->nchstats);
777 if (sysctlbyname("vfs.namecache_stats", &stats->nchstats, &size,
778 NULL, 0) < 0) {
779 error("can't get namecache statistics: %s\n", strerror(errno));
780 memset(&stats->nchstats, 0, sizeof(stats->nchstats));
781 }
782 if (nintr)
783 NREAD(X_INTRCNT, stats->intrcnt, nintr * LONG);
784 for (i = 0; i < nevcnt; i++)
785 KREAD(ie_head[i].ie_count, &stats->evcnt[i],
786 sizeof stats->evcnt[i]);
787 size = sizeof(stats->uvmexp);
788 mib[0] = CTL_VM;
789 mib[1] = VM_UVMEXP2;
790 if (sysctl(mib, 2, &stats->uvmexp, &size, NULL, 0) < 0) {
791 error("can't get uvmexp: %s\n", strerror(errno));
792 memset(&stats->uvmexp, 0, sizeof(stats->uvmexp));
793 }
794 size = sizeof(stats->Total);
795 mib[0] = CTL_VM;
796 mib[1] = VM_METER;
797 if (sysctl(mib, 2, &stats->Total, &size, NULL, 0) < 0) {
798 error("Can't get kernel info: %s\n", strerror(errno));
799 memset(&stats->Total, 0, sizeof(stats->Total));
800 }
801 }
802
803 static void
804 allocinfo(struct Info *stats)
805 {
806
807 if (nintr &&
808 (stats->intrcnt = calloc(nintr, sizeof(long))) == NULL) {
809 error("calloc failed");
810 die(0);
811 }
812 if ((stats->evcnt = calloc(nevcnt, sizeof(u_int64_t))) == NULL) {
813 error("calloc failed");
814 die(0);
815 }
816 }
817
818 static void
819 copyinfo(struct Info *from, struct Info *to)
820 {
821 long *intrcnt;
822 u_int64_t *evcnt;
823
824 intrcnt = to->intrcnt;
825 evcnt = to->evcnt;
826 *to = *from;
827 memmove(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof *intrcnt);
828 memmove(to->evcnt = evcnt, from->evcnt, nevcnt * sizeof *evcnt);
829 }
830
831 static void
832 dinfo(int dn, int r, int c)
833 {
834 double atime;
835 #define ADV if (disk_horiz) r++; else c += DISKCOLWIDTH
836
837 mvprintw(r, c, "%*.*s", DISKCOLWIDTH, DISKCOLWIDTH, dr_name[dn]);
838 ADV;
839
840 putint((int)(cur.seek[dn]/etime+0.5), r, c, DISKCOLWIDTH);
841 ADV;
842 putint((int)((cur.rxfer[dn]+cur.wxfer[dn])/etime+0.5),
843 r, c, DISKCOLWIDTH);
844 ADV;
845 puthumanint((cur.rbytes[dn] + cur.wbytes[dn]) / etime + 0.5,
846 r, c, DISKCOLWIDTH);
847 ADV;
848
849 /* time busy in disk activity */
850 atime = cur.time[dn].tv_sec + cur.time[dn].tv_usec / 1000000.0;
851 atime = atime * 100.0 / etime;
852 if (atime >= 100)
853 putint(100, r, c, DISKCOLWIDTH);
854 else
855 putfloat(atime, r, c, DISKCOLWIDTH, 1, 1);
856 }
857