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