vmstat.c revision 1.47 1 /* $NetBSD: vmstat.c,v 1.47 2003/02/13 08:09:33 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 1/12/94";
40 #endif
41 __RCSID("$NetBSD: vmstat.c,v 1.47 2003/02/13 08:09:33 dsl Exp $");
42 #endif /* not lint */
43
44 /*
45 * Cursed vmstat -- from Robert Elz.
46 */
47
48 #include <sys/param.h>
49 #include <sys/dkstat.h>
50 #include <sys/user.h>
51 #include <sys/namei.h>
52 #include <sys/sysctl.h>
53 #include <sys/device.h>
54
55 #include <uvm/uvm_extern.h>
56
57 #include <stdlib.h>
58 #include <string.h>
59 #include <util.h>
60
61 #include "systat.h"
62 #include "extern.h"
63 #include "dkstats.h"
64 #include "utmpentry.h"
65
66 static struct Info {
67 struct uvmexp_sysctl uvmexp;
68 struct vmtotal Total;
69 struct nchstats nchstats;
70 long nchcount;
71 long *intrcnt;
72 u_int64_t *evcnt;
73 } s, s1, s2, z;
74
75 #define cnt s.Cnt
76 #define oldcnt s1.Cnt
77 #define total s.Total
78 #define nchtotal s.nchstats
79 #define oldnchtotal s1.nchstats
80
81 static enum state { BOOT, TIME, RUN } state = TIME;
82
83 static void allocinfo(struct Info *);
84 static void copyinfo(struct Info *, struct Info *);
85 static float cputime(int);
86 static void dinfo(int, int);
87 static void getinfo(struct Info *, enum state);
88 static void putint(int, int, int, int);
89 static void putfloat(double, int, int, int, int, int);
90 static int ucount(void);
91
92 static char buf[26];
93 static u_int64_t t;
94 static double etime;
95 static float hertz;
96 static int nintr;
97 static long *intrloc;
98 static char **intrname;
99 static int nextintsrow;
100
101 WINDOW *
102 openvmstat(void)
103 {
104 return (stdscr);
105 }
106
107 void
108 closevmstat(WINDOW *w)
109 {
110
111 if (w == NULL)
112 return;
113 wclear(w);
114 wrefresh(w);
115 }
116
117
118 static struct nlist namelist[] = {
119 #define X_NCHSTATS 0
120 { "_nchstats" },
121 #define X_INTRNAMES 1
122 { "_intrnames" },
123 #define X_EINTRNAMES 2
124 { "_eintrnames" },
125 #define X_INTRCNT 3
126 { "_intrcnt" },
127 #define X_EINTRCNT 4
128 { "_eintrcnt" },
129 #define X_ALLEVENTS 5
130 { "_allevents" },
131 { "" },
132 };
133
134 /*
135 * These constants define where the major pieces are laid out
136 */
137 #define STATROW 0 /* uses 1 row and 68 cols */
138 #define STATCOL 2
139 #define MEMROW 2 /* uses 4 rows and 31 cols */
140 #define MEMCOL 0
141 #define PAGEROW 2 /* uses 4 rows and 26 cols */
142 #define PAGECOL 36
143 #define INTSROW 2 /* uses all rows to bottom and 17 cols */
144 #define INTSCOL 63
145 #define PROCSROW 7 /* uses 2 rows and 20 cols */
146 #define PROCSCOL 0
147 #define GENSTATROW 7 /* uses 2 rows and 30 cols */
148 #define GENSTATCOL 18
149 #define VMSTATROW 7 /* uses 17 rows and 12 cols */
150 #define VMSTATCOL 48
151 #define GRAPHROW 10 /* uses 3 rows and 51 cols */
152 #define GRAPHCOL 0
153 #define NAMEIROW 14 /* uses 3 rows and 38 cols */
154 #define NAMEICOL 0
155 #define DISKROW 18 /* uses 5 rows and 50 cols (for 9 drives) */
156 #define DISKCOL 0
157
158 #define DRIVESPACE 9 /* max # for space */
159
160 #if DK_NDRIVE > DRIVESPACE
161 #define MAXDRIVES DRIVESPACE /* max # to display */
162 #else
163 #define MAXDRIVES DK_NDRIVE /* max # to display */
164 #endif
165
166 typedef struct intr_evcnt intr_evcnt_t;
167 struct intr_evcnt {
168 char *ie_group;
169 char *ie_name;
170 u_int64_t *ie_count; /* kernel address... */
171 int ie_loc; /* screen row */
172 } *ie_head;
173 int nevcnt;
174
175 static void
176 get_interrupt_events(void)
177 {
178 struct evcntlist allevents;
179 struct evcnt evcnt, *evptr;
180 intr_evcnt_t *ie;
181
182 if (!NREAD(X_ALLEVENTS, &allevents, sizeof allevents))
183 return;
184 evptr = allevents.tqh_first;
185 for (; evptr != NULL; evptr = evcnt.ev_list.tqe_next) {
186 if (!KREAD(evptr, &evcnt, sizeof evcnt))
187 return;
188 if (evcnt.ev_type != EVCNT_TYPE_INTR)
189 continue;
190 ie_head = realloc(ie_head, sizeof *ie * (nevcnt + 1));
191 if (ie_head == NULL) {
192 error("realloc failed");
193 die(0);
194 }
195 ie = ie_head + nevcnt;
196 ie->ie_group = malloc(evcnt.ev_grouplen + 1);
197 ie->ie_name = malloc(evcnt.ev_namelen + 1);
198 if (ie->ie_group == NULL || ie->ie_name == NULL)
199 return;
200 if (!KREAD(evcnt.ev_group, ie->ie_group, evcnt.ev_grouplen + 1))
201 return;
202 if (!KREAD(evcnt.ev_name, ie->ie_name, evcnt.ev_namelen + 1))
203 return;
204 ie->ie_count = &evptr->ev_count;
205 ie->ie_loc = 0;
206 nevcnt++;
207 }
208 }
209
210 int
211 initvmstat(void)
212 {
213 char *intrnamebuf, *cp;
214 int i;
215
216 if (namelist[0].n_type == 0) {
217 if (kvm_nlist(kd, namelist)) {
218 nlisterr(namelist);
219 return(0);
220 }
221 if (namelist[0].n_type == 0) {
222 error("No namelist");
223 return(0);
224 }
225 }
226 hertz = stathz ? stathz : hz;
227 if (!dkinit(1))
228 return(0);
229
230 /* Old style interrupt counts - deprecated */
231 nintr = (namelist[X_EINTRCNT].n_value -
232 namelist[X_INTRCNT].n_value) / sizeof (long);
233 intrloc = calloc(nintr, sizeof (long));
234 intrname = calloc(nintr, sizeof (long));
235 intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
236 namelist[X_INTRNAMES].n_value);
237 if (intrnamebuf == NULL || intrname == 0 || intrloc == 0) {
238 error("Out of memory\n");
239 if (intrnamebuf)
240 free(intrnamebuf);
241 if (intrname)
242 free(intrname);
243 if (intrloc)
244 free(intrloc);
245 nintr = 0;
246 return(0);
247 }
248 NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
249 NVAL(X_INTRNAMES));
250 for (cp = intrnamebuf, i = 0; i < nintr; i++) {
251 intrname[i] = cp;
252 cp += strlen(cp) + 1;
253 }
254
255 /* event counter interrupt counts */
256 get_interrupt_events();
257
258 nextintsrow = INTSROW + 2;
259 allocinfo(&s);
260 allocinfo(&s1);
261 allocinfo(&s2);
262 allocinfo(&z);
263
264 getinfo(&s2, RUN);
265 copyinfo(&s2, &s1);
266 return(1);
267 }
268
269 void
270 fetchvmstat(void)
271 {
272 time_t now;
273
274 time(&now);
275 strcpy(buf, ctime(&now));
276 buf[19] = '\0';
277 getinfo(&s, state);
278 }
279
280 static void
281 print_ie_title(int i)
282 {
283 int width, name_width, group_width;
284
285 width = COLS - (INTSCOL + 9);
286 if (width <= 0)
287 return;
288
289 move(ie_head[i].ie_loc, INTSCOL + 9);
290 group_width = strlen(ie_head[i].ie_group);
291 name_width = strlen(ie_head[i].ie_name);
292 width -= group_width + 1 + name_width;
293 if (width < 0) {
294 /*
295 * Screen to narrow for full strings
296 * This is all rather horrid, in some cases there are a lot
297 * of events in the same group, and in others the event
298 * name is "intr". There are also names which need 7 or 8
299 * columns before they become meaningful.
300 * This is a bad compromise.
301 */
302 width = -width;
303 group_width -= (width + 1) / 2;
304 name_width -= width / 2;
305 /* some have the 'useful' name "intr", display their group */
306 if (strcasecmp(ie_head[i].ie_name, "intr") == 0) {
307 group_width += name_width + 1;
308 name_width = 0;
309 } else {
310 if (group_width <= 3 || name_width < 0) {
311 /* don't display group */
312 name_width += group_width + 1;
313 group_width = 0;
314 }
315 }
316 }
317
318 if (group_width != 0) {
319 printw("%-.*s", group_width, ie_head[i].ie_group);
320 if (name_width != 0)
321 printw(" ");
322 }
323 if (name_width != 0)
324 printw("%-.*s", name_width, ie_head[i].ie_name);
325 }
326
327 void
328 labelvmstat(void)
329 {
330 int i, j;
331
332 clear();
333 mvprintw(STATROW, STATCOL + 4, "users Load");
334 mvprintw(MEMROW, MEMCOL, " memory totals (in kB)");
335 mvprintw(MEMROW + 1, MEMCOL, " real virtual free");
336 mvprintw(MEMROW + 2, MEMCOL, "Active");
337 mvprintw(MEMROW + 3, MEMCOL, "All");
338
339 mvprintw(PAGEROW, PAGECOL, " PAGING SWAPPING ");
340 mvprintw(PAGEROW + 1, PAGECOL, " in out in out ");
341 mvprintw(PAGEROW + 2, PAGECOL, "ops");
342 mvprintw(PAGEROW + 3, PAGECOL, "pages");
343
344 mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
345 mvprintw(INTSROW + 1, INTSCOL + 9, "total");
346
347 mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks");
348 mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw");
349 mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm");
350 mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait");
351 mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck");
352 mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok");
353 mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram");
354 mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy");
355 mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp");
356 mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod");
357 mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow");
358 mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin");
359 mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg");
360 mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg");
361 mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired");
362 mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre");
363 if (LINES - 1 > VMSTATROW + 16)
364 mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn");
365
366 mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt");
367
368 mvprintw(GRAPHROW, GRAPHCOL,
369 " . %% Sy . %% Us . %% Ni . %% In . %% Id");
370 mvprintw(PROCSROW, PROCSCOL, "Proc:r d s w");
371 mvprintw(GRAPHROW + 1, GRAPHCOL,
372 "| | | | | | | | | | |");
373
374 mvprintw(NAMEIROW, NAMEICOL, "Namei Sys-cache Proc-cache");
375 mvprintw(NAMEIROW + 1, NAMEICOL,
376 " Calls hits %% hits %%");
377 mvprintw(DISKROW, DISKCOL, "Discs");
378 mvprintw(DISKROW + 1, DISKCOL, "seeks");
379 mvprintw(DISKROW + 2, DISKCOL, "xfers");
380 mvprintw(DISKROW + 3, DISKCOL, "bytes");
381 mvprintw(DISKROW + 4, DISKCOL, "%%busy");
382 j = 0;
383 for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++)
384 if (dk_select[i]) {
385 mvprintw(DISKROW, DISKCOL + 5 + 5 * j,
386 " %4.4s", dr_name[j]);
387 j++;
388 }
389 for (i = 0; i < nintr; i++) {
390 if (intrloc[i] == 0)
391 continue;
392 mvprintw(intrloc[i], INTSCOL + 9, "%-.*s",
393 COLS - (INTSCOL + 9), intrname[i]);
394 }
395 for (i = 0; i < nevcnt; i++) {
396 if (ie_head[i].ie_loc == 0)
397 continue;
398 print_ie_title(i);
399 }
400 }
401
402 #define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
403 #define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
404 #define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
405 if(state == TIME) s1.nchstats.fld = t;}
406 #define PUTRATE(fld, l, c, w) {Y(fld); putint((int)((float)s.fld/etime + 0.5), l, c, w);}
407 #define MAXFAIL 5
408
409 static char cpuchar[CPUSTATES] = { '=' , '>', '-', '%', ' ' };
410 static char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_INTR, CP_IDLE };
411
412 void
413 showvmstat(void)
414 {
415 float f1, f2;
416 int psiz, inttotal;
417 int i, l, c;
418 static int failcnt = 0;
419
420 if (state == TIME) {
421 dkswap();
422 etime = cur.cp_etime;
423 /* < 5 ticks - ignore this trash */
424 if ((etime * hertz) < 1.0) {
425 if (failcnt++ > MAXFAIL)
426 return;
427 clear();
428 mvprintw(2, 10, "The alternate system clock has died!");
429 mvprintw(3, 10, "Reverting to ``pigs'' display.");
430 move(CMDLINE, 0);
431 refresh();
432 failcnt = 0;
433 sleep(5);
434 command("pigs");
435 return;
436 }
437 } else
438 etime = 1.0;
439
440 failcnt = 0;
441 inttotal = 0;
442 for (i = 0; i < nintr; i++) {
443 if (s.intrcnt[i] == 0)
444 continue;
445 if (intrloc[i] == 0) {
446 if (nextintsrow == LINES)
447 continue;
448 intrloc[i] = nextintsrow++;
449 mvprintw(intrloc[i], INTSCOL + 9, "%-.*s",
450 COLS - (INTSCOL + 9), intrname[i]);
451 }
452 X(intrcnt);
453 l = (int)((float)s.intrcnt[i]/etime + 0.5);
454 inttotal += l;
455 putint(l, intrloc[i], INTSCOL, 8);
456 }
457 for (i = 0; i < nevcnt; i++) {
458 if (s.evcnt[i] == 0)
459 continue;
460 if (ie_head[i].ie_loc == 0) {
461 if (nextintsrow == LINES)
462 continue;
463 ie_head[i].ie_loc = nextintsrow++;
464 print_ie_title(i);
465 }
466 X(evcnt);
467 l = (int)((float)s.evcnt[i]/etime + 0.5);
468 inttotal += l;
469 putint(l, ie_head[i].ie_loc, INTSCOL, 8);
470 }
471 putint(inttotal, INTSROW + 1, INTSCOL, 8);
472 Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
473 Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
474 s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
475 nchtotal.ncs_miss + nchtotal.ncs_long;
476 if (state == TIME)
477 s1.nchcount = s.nchcount;
478
479 psiz = 0;
480 f2 = 0.0;
481
482 /*
483 * Last CPU state not calculated yet.
484 */
485 for (c = 0; c < CPUSTATES; c++) {
486 i = cpuorder[c];
487 f1 = cputime(i);
488 f2 += f1;
489 l = (int) ((f2 + 1.0) / 2.0) - psiz;
490 if (c == 0)
491 putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
492 else
493 putfloat(f1, GRAPHROW, GRAPHCOL + 10 * c + 1, 5, 1, 0);
494 mvhline(GRAPHROW + 2, psiz, cpuchar[c], l);
495 psiz += l;
496 }
497
498 putint(ucount(), STATROW, STATCOL, 3);
499 putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
500 putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
501 putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
502 mvaddstr(STATROW, STATCOL + 53, buf);
503 #define pgtokb(pg) ((pg) * (s.uvmexp.pagesize / 1024))
504
505 putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 6, 7);
506 putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse), /* XXX */
507 MEMROW + 2, MEMCOL + 16, 7);
508 putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 6, 7);
509 putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
510 MEMROW + 3, MEMCOL + 16, 7);
511 putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 24, 7);
512 putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
513 MEMROW + 3, MEMCOL + 24, 7);
514 putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
515 putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3);
516 putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3);
517 putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3);
518 PUTRATE(uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
519 PUTRATE(uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
520 PUTRATE(uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
521 PUTRATE(uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
522 PUTRATE(uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
523 PUTRATE(uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
524 PUTRATE(uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
525 PUTRATE(uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
526 PUTRATE(uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
527 PUTRATE(uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
528 PUTRATE(uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
529 putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
530 putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
531 putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
532 putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9);
533 PUTRATE(uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
534 if (LINES - 1 > VMSTATROW + 16)
535 PUTRATE(uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
536
537 PUTRATE(uvmexp.pageins, PAGEROW + 2, PAGECOL + 5, 5);
538 PUTRATE(uvmexp.pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
539 PUTRATE(uvmexp.swapins, PAGEROW + 2, PAGECOL + 15, 5);
540 PUTRATE(uvmexp.swapouts, PAGEROW + 2, PAGECOL + 20, 5);
541 PUTRATE(uvmexp.pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
542 PUTRATE(uvmexp.pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
543
544 PUTRATE(uvmexp.swtch, GENSTATROW + 1, GENSTATCOL, 4);
545 PUTRATE(uvmexp.traps, GENSTATROW + 1, GENSTATCOL + 4, 6);
546 PUTRATE(uvmexp.syscalls, GENSTATROW + 1, GENSTATCOL + 10, 6);
547 PUTRATE(uvmexp.intrs, GENSTATROW + 1, GENSTATCOL + 16, 5);
548 PUTRATE(uvmexp.softs, GENSTATROW + 1, GENSTATCOL + 21, 5);
549 PUTRATE(uvmexp.faults, GENSTATROW + 1, GENSTATCOL + 26, 6);
550 mvprintw(DISKROW, DISKCOL + 5, " ");
551 for (i = 0, c = 0; i < dk_ndrive && c < MAXDRIVES; i++)
552 if (dk_select[i]) {
553 mvprintw(DISKROW, DISKCOL + 5 + 5 * c,
554 " %4.4s", dr_name[i]);
555 dinfo(i, ++c);
556 }
557 putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
558 putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
559 #define nz(x) ((x) ? (x) : 1)
560 putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
561 NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
562 putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
563 putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
564 NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
565 #undef nz
566 }
567
568 void
569 vmstat_boot(char *args)
570 {
571 copyinfo(&z, &s1);
572 state = BOOT;
573 }
574
575 void
576 vmstat_run(char *args)
577 {
578 copyinfo(&s1, &s2);
579 state = RUN;
580 }
581
582 void
583 vmstat_time(char *args)
584 {
585 state = TIME;
586 }
587
588 void
589 vmstat_zero(char *args)
590 {
591 if (state == RUN)
592 getinfo(&s1, RUN);
593 }
594
595 /* calculate number of users on the system */
596 static int
597 ucount(void)
598 {
599 static int onusers = -1;
600 static struct utmpentry *oehead = NULL;
601 int nusers = 0;
602 struct utmpentry *ehead;
603
604 nusers = getutentries(NULL, &ehead);
605 if (oehead != ehead) {
606 freeutentries(oehead);
607 oehead = ehead;
608 }
609
610 if (nusers != onusers) {
611 if (nusers == 1)
612 mvprintw(STATROW, STATCOL + 8, " ");
613 else
614 mvprintw(STATROW, STATCOL + 8, "s");
615 }
616 onusers = nusers;
617 return (nusers);
618 }
619
620 static float
621 cputime(int indx)
622 {
623 double t;
624 int i;
625
626 t = 0;
627 for (i = 0; i < CPUSTATES; i++)
628 t += cur.cp_time[i];
629 if (t == 0.0)
630 t = 1.0;
631 return (cur.cp_time[indx] * 100.0 / t);
632 }
633
634 static void
635 puthumanint(u_int64_t n, int l, int c, int w)
636 {
637 char b[128];
638
639 move(l, c);
640 if (n == 0) {
641 hline(' ', w);
642 return;
643 }
644 if (humanize_number(b, w, n, "", HN_AUTOSCALE, HN_NOSPACE) == -1 ) {
645 hline('*', w);
646 return;
647 }
648 hline(' ', w - strlen(b));
649 move(l, c + w - strlen(b));
650 addstr(b);
651 }
652
653 static void
654 putint(int n, int l, int c, int w)
655 {
656 char b[128];
657
658 move(l, c);
659 if (n == 0) {
660 hline(' ', w);
661 return;
662 }
663 (void)snprintf(b, sizeof b, "%*d", w, n);
664 if (strlen(b) > w) {
665 hline('*', w);
666 return;
667 }
668 addstr(b);
669 }
670
671 static void
672 putfloat(double f, int l, int c, int w, int d, int nz)
673 {
674 char b[128];
675
676 move(l, c);
677 if (nz && f == 0.0) {
678 hline(' ', w);
679 return;
680 }
681 (void)snprintf(b, sizeof b, "%*.*f", w, d, f);
682 if (strlen(b) > w) {
683 hline('*', w);
684 return;
685 }
686 addstr(b);
687 }
688
689 static void
690 getinfo(struct Info *s, enum state st)
691 {
692 int mib[2];
693 size_t size;
694 int i;
695
696 dkreadstats();
697 NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
698 NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
699 for (i = 0; i < nevcnt; i++)
700 KREAD(ie_head[i].ie_count, &s->evcnt[i], sizeof s->evcnt[i]);
701 size = sizeof(s->uvmexp);
702 mib[0] = CTL_VM;
703 mib[1] = VM_UVMEXP2;
704 if (sysctl(mib, 2, &s->uvmexp, &size, NULL, 0) < 0) {
705 error("can't get uvmexp: %s\n", strerror(errno));
706 memset(&s->uvmexp, 0, sizeof(s->uvmexp));
707 }
708 size = sizeof(s->Total);
709 mib[0] = CTL_VM;
710 mib[1] = VM_METER;
711 if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
712 error("Can't get kernel info: %s\n", strerror(errno));
713 memset(&s->Total, 0, sizeof(s->Total));
714 }
715 }
716
717 static void
718 allocinfo(struct Info *s)
719 {
720
721 if ((s->intrcnt = calloc(nintr, sizeof(long))) == NULL) {
722 error("calloc failed");
723 die(0);
724 }
725 if ((s->evcnt = calloc(nevcnt, sizeof(u_int64_t))) == NULL) {
726 error("calloc failed");
727 die(0);
728 }
729 }
730
731 static void
732 copyinfo(struct Info *from, struct Info *to)
733 {
734 long *intrcnt;
735 u_int64_t *evcnt;
736
737 intrcnt = to->intrcnt;
738 evcnt = to->evcnt;
739 *to = *from;
740 memmove(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof (int));
741 memmove(to->evcnt = evcnt, from->evcnt, nevcnt * sizeof *evcnt);
742 }
743
744 static void
745 dinfo(int dn, int c)
746 {
747 double atime;
748
749 c = DISKCOL + c * 5;
750
751 /* time busy in disk activity */
752 atime = (double)cur.dk_time[dn].tv_sec +
753 ((double)cur.dk_time[dn].tv_usec / (double)1000000);
754
755 putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
756 putint((int)((float)(cur.dk_rxfer[dn]+cur.dk_wxfer[dn])/etime+0.5),
757 DISKROW + 2, c, 5);
758 puthumanint((cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) / etime + 0.5,
759 DISKROW + 3, c, 5);
760 if (atime*100.0/etime >= 100)
761 putint(100, DISKROW + 4, c, 5);
762 else
763 putfloat(atime*100.0/etime, DISKROW + 4, c, 5, 1, 1);
764 }
765