vmstat.c revision 1.62 1 /* $NetBSD: vmstat.c,v 1.62 2006/02/05 09:54:50 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.62 2006/02/05 09:54:50 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
62 static struct Info {
63 struct uvmexp_sysctl uvmexp;
64 struct vmtotal Total;
65 struct nchstats nchstats;
66 long nchcount;
67 long *intrcnt;
68 u_int64_t *evcnt;
69 } s, s1, s2, z;
70
71 #define cnt s.Cnt
72 #define oldcnt s1.Cnt
73 #define total s.Total
74 #define nchtotal s.nchstats
75 #define oldnchtotal s1.nchstats
76
77 static enum state { BOOT, TIME, RUN } state = TIME;
78
79 static void allocinfo(struct Info *);
80 static void copyinfo(struct Info *, struct Info *);
81 static float cputime(int);
82 static void dinfo(int, int, int);
83 static void tinfo(int, int, int);
84 static void getinfo(struct Info *, enum state);
85 static void putint(int, int, int, int);
86 static void putfloat(double, int, int, int, int, int);
87 static int ucount(void);
88
89 static char buf[26];
90 static u_int64_t temp;
91 static double etime;
92 static float hertz;
93 static int nintr;
94 static long *intrloc;
95 static char **intrname;
96 static int nextintsrow;
97 static int disk_horiz = 1;
98
99 WINDOW *
100 openvmstat(void)
101 {
102 return (stdscr);
103 }
104
105 void
106 closevmstat(WINDOW *w)
107 {
108
109 if (w == NULL)
110 return;
111 wclear(w);
112 wrefresh(w);
113 }
114
115
116 static struct nlist namelist[] = {
117 #define X_NCHSTATS 0
118 { "_nchstats" },
119 #define X_INTRNAMES 1
120 { "_intrnames" },
121 #define X_EINTRNAMES 2
122 { "_eintrnames" },
123 #define X_INTRCNT 3
124 { "_intrcnt" },
125 #define X_EINTRCNT 4
126 { "_eintrcnt" },
127 #define X_ALLEVENTS 5
128 { "_allevents" },
129 { NULL }
130 };
131
132 /*
133 * These constants define where the major pieces are laid out
134 */
135 #define STATROW 0 /* uses 1 row and 68 cols */
136 #define STATCOL 2
137 #define MEMROW 9 /* uses 4 rows and 31 cols */
138 #define MEMCOL 0
139 #define PAGEROW 2 /* uses 4 rows and 26 cols */
140 #define PAGECOL 54
141 #define INTSROW 9 /* uses all rows to bottom and 17 cols */
142 #define INTSCOL 40
143 #define INTSCOLEND (VMSTATCOL - 0)
144 #define PROCSROW 2 /* uses 2 rows and 20 cols */
145 #define PROCSCOL 0
146 #define GENSTATROW 2 /* uses 2 rows and 30 cols */
147 #define GENSTATCOL 17
148 #define VMSTATROW 7 /* uses 17 rows and 15 cols */
149 #define VMSTATCOL 64
150 #define GRAPHROW 5 /* uses 3 rows and 51 cols */
151 #define GRAPHCOL 0
152 #define NAMEIROW 14 /* uses 3 rows and 38 cols */
153 #define NAMEICOL 0
154 #define DISKROW 18 /* uses 5 rows and 50 cols (for 9 drives) */
155 #define DISKCOL 0
156 #define DISKCOLWIDTH 6
157 #define DISKCOLEND INTSCOL
158
159 typedef struct intr_evcnt intr_evcnt_t;
160 struct intr_evcnt {
161 char *ie_group;
162 char *ie_name;
163 u_int64_t *ie_count; /* kernel address... */
164 int ie_loc; /* screen row */
165 } *ie_head;
166 int nevcnt;
167
168 static void
169 get_interrupt_events(void)
170 {
171 struct evcntlist allevents;
172 struct evcnt evcnt, *evptr;
173 intr_evcnt_t *ie;
174 intr_evcnt_t *n;
175
176 if (!NREAD(X_ALLEVENTS, &allevents, sizeof allevents))
177 return;
178 evptr = TAILQ_FIRST(&allevents);
179 for (; evptr != NULL; evptr = TAILQ_NEXT(&evcnt, ev_list)) {
180 if (!KREAD(evptr, &evcnt, sizeof evcnt))
181 return;
182 if (evcnt.ev_type != EVCNT_TYPE_INTR)
183 continue;
184 n = realloc(ie_head, sizeof *ie * (nevcnt + 1));
185 if (n == NULL) {
186 error("realloc failed");
187 die(0);
188 }
189 ie_head = n;
190 ie = ie_head + nevcnt;
191 ie->ie_group = malloc(evcnt.ev_grouplen + 1);
192 ie->ie_name = malloc(evcnt.ev_namelen + 1);
193 if (ie->ie_group == NULL || ie->ie_name == NULL)
194 return;
195 if (!KREAD(evcnt.ev_group, ie->ie_group, evcnt.ev_grouplen + 1))
196 return;
197 if (!KREAD(evcnt.ev_name, ie->ie_name, evcnt.ev_namelen + 1))
198 return;
199 ie->ie_count = &evptr->ev_count;
200 ie->ie_loc = 0;
201 nevcnt++;
202 }
203 }
204
205 int
206 initvmstat(void)
207 {
208 char *intrnamebuf, *cp;
209 int i;
210
211 if (namelist[0].n_type == 0) {
212 if (kvm_nlist(kd, namelist) &&
213 (namelist[X_NCHSTATS].n_type == 0 ||
214 namelist[X_ALLEVENTS].n_type == 0)) {
215 nlisterr(namelist);
216 return(0);
217 }
218 if (namelist[0].n_type == 0) {
219 error("No namelist");
220 return(0);
221 }
222 }
223 hertz = stathz ? stathz : hz;
224 if (!dkinit(1))
225 return(0);
226 if (!tpinit(1))
227 return(0);
228
229 /* Old style interrupt counts - deprecated */
230 nintr = (namelist[X_EINTRCNT].n_value -
231 namelist[X_INTRCNT].n_value) / sizeof (long);
232 if (nintr) {
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
256 /* event counter interrupt counts */
257 get_interrupt_events();
258
259 nextintsrow = INTSROW + 1;
260 allocinfo(&s);
261 allocinfo(&s1);
262 allocinfo(&s2);
263 allocinfo(&z);
264
265 getinfo(&s2, RUN);
266 copyinfo(&s2, &s1);
267 return(1);
268 }
269
270 void
271 fetchvmstat(void)
272 {
273 time_t now;
274
275 time(&now);
276 strlcpy(buf, ctime(&now), sizeof(buf));
277 buf[19] = '\0';
278 getinfo(&s, state);
279 }
280
281 static void
282 print_ie_title(int i)
283 {
284 int width, name_width, group_width;
285
286 width = INTSCOLEND - (INTSCOL + 9);
287 if (width <= 0)
288 return;
289
290 move(ie_head[i].ie_loc, INTSCOL + 9);
291 group_width = strlen(ie_head[i].ie_group);
292 name_width = strlen(ie_head[i].ie_name);
293 width -= group_width + 1 + name_width;
294 if (width < 0) {
295 /*
296 * Screen to narrow for full strings
297 * This is all rather horrid, in some cases there are a lot
298 * of events in the same group, and in others the event
299 * name is "intr". There are also names which need 7 or 8
300 * columns before they become meaningful.
301 * This is a bad compromise.
302 */
303 width = -width;
304 group_width -= (width + 1) / 2;
305 name_width -= width / 2;
306 /* some have the 'useful' name "intr", display their group */
307 if (strcasecmp(ie_head[i].ie_name, "intr") == 0) {
308 group_width += name_width + 1;
309 name_width = 0;
310 } else {
311 if (group_width <= 3 || name_width < 0) {
312 /* don't display group */
313 name_width += group_width + 1;
314 group_width = 0;
315 }
316 }
317 }
318
319 if (group_width != 0) {
320 printw("%-.*s", group_width, ie_head[i].ie_group);
321 if (name_width != 0)
322 printw(" ");
323 }
324 if (name_width != 0)
325 printw("%-.*s", name_width, ie_head[i].ie_name);
326 }
327
328 void
329 labelvmstat(void)
330 {
331 int i;
332
333 clear();
334 mvprintw(STATROW, STATCOL + 4, "users Load");
335 mvprintw(MEMROW, MEMCOL, " memory totals (in kB)");
336 mvprintw(MEMROW + 1, MEMCOL, " real virtual free");
337 mvprintw(MEMROW + 2, MEMCOL, "Active");
338 mvprintw(MEMROW + 3, MEMCOL, "All");
339
340 mvprintw(PAGEROW, PAGECOL, " PAGING SWAPPING ");
341 mvprintw(PAGEROW + 1, PAGECOL, " in out in out ");
342 mvprintw(PAGEROW + 2, PAGECOL, "ops");
343 mvprintw(PAGEROW + 3, PAGECOL, "pages");
344
345 mvprintw(INTSROW, INTSCOL + 9, "Interrupts");
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, "Disks:");
378 if (disk_horiz) {
379 mvprintw(DISKROW + 1, DISKCOL + 1, "seeks");
380 mvprintw(DISKROW + 2, DISKCOL + 1, "xfers");
381 mvprintw(DISKROW + 3, DISKCOL + 1, "bytes");
382 mvprintw(DISKROW + 4, DISKCOL + 1, "%%busy");
383 } else {
384 mvprintw(DISKROW, DISKCOL + 1 + 1 * DISKCOLWIDTH, "seeks");
385 mvprintw(DISKROW, DISKCOL + 1 + 2 * DISKCOLWIDTH, "xfers");
386 mvprintw(DISKROW, DISKCOL + 1 + 3 * DISKCOLWIDTH, "bytes");
387 mvprintw(DISKROW, DISKCOL + 1 + 4 * DISKCOLWIDTH, "%%busy");
388 }
389 for (i = 0; i < nintr; i++) {
390 if (intrloc[i] == 0)
391 continue;
392 mvprintw(intrloc[i], INTSCOL + 9, "%-.*s",
393 INTSCOLEND - (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) {temp=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=temp;}
403 #define Y(fld) {temp = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = temp;}
404 #define Z(fld) {temp = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
405 if(state == TIME) s1.nchstats.fld = temp;}
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, r, c;
418 static int failcnt = 0;
419 static int relabel = 0;
420 static int last_disks = 0;
421 static char pigs[] = "pigs";
422
423 if (relabel) {
424 labelvmstat();
425 relabel = 0;
426 }
427
428 cpuswap();
429 if (state == TIME) {
430 dkswap();
431 tpswap();
432 etime = cur.cp_etime;
433 /* < 5 ticks - ignore this trash */
434 if ((etime * hertz) < 1.0) {
435 if (failcnt++ > MAXFAIL)
436 return;
437 clear();
438 mvprintw(2, 10, "The alternate system clock has died!");
439 mvprintw(3, 10, "Reverting to ``pigs'' display.");
440 move(CMDLINE, 0);
441 refresh();
442 failcnt = 0;
443 sleep(5);
444 command(pigs);
445 return;
446 }
447 } else
448 etime = 1.0;
449
450 failcnt = 0;
451 inttotal = 0;
452 for (i = 0; i < nintr; i++) {
453 if (s.intrcnt[i] == 0)
454 continue;
455 if (intrloc[i] == 0) {
456 if (nextintsrow == LINES)
457 continue;
458 intrloc[i] = nextintsrow++;
459 mvprintw(intrloc[i], INTSCOL + 9, "%-.*s",
460 INTSCOLEND - (INTSCOL + 9), intrname[i]);
461 }
462 X(intrcnt);
463 l = (int)((float)s.intrcnt[i]/etime + 0.5);
464 inttotal += l;
465 putint(l, intrloc[i], INTSCOL, 8);
466 }
467 for (i = 0; i < nevcnt; i++) {
468 if (s.evcnt[i] == 0)
469 continue;
470 if (ie_head[i].ie_loc == 0) {
471 if (nextintsrow == LINES)
472 continue;
473 ie_head[i].ie_loc = nextintsrow++;
474 print_ie_title(i);
475 }
476 X(evcnt);
477 l = (int)((float)s.evcnt[i]/etime + 0.5);
478 inttotal += l;
479 putint(l, ie_head[i].ie_loc, INTSCOL, 8);
480 }
481 putint(inttotal, INTSROW, INTSCOL, 8);
482 Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
483 Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
484 s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
485 nchtotal.ncs_miss + nchtotal.ncs_long;
486 if (state == TIME)
487 s1.nchcount = s.nchcount;
488
489 psiz = 0;
490 f2 = 0.0;
491
492 /*
493 * Last CPU state not calculated yet.
494 */
495 for (c = 0; c < CPUSTATES; c++) {
496 i = cpuorder[c];
497 f1 = cputime(i);
498 f2 += f1;
499 l = (int) ((f2 + 1.0) / 2.0) - psiz;
500 if (c == 0)
501 putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
502 else
503 putfloat(f1, GRAPHROW, GRAPHCOL + 10 * c + 1, 5, 1, 0);
504 mvhline(GRAPHROW + 2, psiz, cpuchar[c], l);
505 psiz += l;
506 }
507
508 putint(ucount(), STATROW, STATCOL, 3);
509 putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
510 putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
511 putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
512 mvaddstr(STATROW, STATCOL + 53, buf);
513 #define pgtokb(pg) ((pg) * (s.uvmexp.pagesize / 1024))
514
515 putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 6, 8);
516 putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse), /* XXX */
517 MEMROW + 2, MEMCOL + 15, 8);
518 putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 6, 8);
519 putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
520 MEMROW + 3, MEMCOL + 15, 8);
521 putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 24, 8);
522 putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
523 MEMROW + 3, MEMCOL + 24, 8);
524 putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
525 putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3);
526 putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3);
527 putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3);
528 PUTRATE(uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
529 PUTRATE(uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
530 PUTRATE(uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
531 PUTRATE(uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
532 PUTRATE(uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
533 PUTRATE(uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
534 PUTRATE(uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
535 PUTRATE(uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
536 PUTRATE(uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
537 PUTRATE(uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
538 PUTRATE(uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
539 putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
540 putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
541 putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
542 putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9);
543 PUTRATE(uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
544 if (LINES - 1 > VMSTATROW + 16)
545 PUTRATE(uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
546
547 PUTRATE(uvmexp.pageins, PAGEROW + 2, PAGECOL + 5, 5);
548 PUTRATE(uvmexp.pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
549 PUTRATE(uvmexp.swapins, PAGEROW + 2, PAGECOL + 15, 5);
550 PUTRATE(uvmexp.swapouts, PAGEROW + 2, PAGECOL + 20, 5);
551 PUTRATE(uvmexp.pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
552 PUTRATE(uvmexp.pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
553
554 PUTRATE(uvmexp.swtch, GENSTATROW + 1, GENSTATCOL, 5);
555 PUTRATE(uvmexp.traps, GENSTATROW + 1, GENSTATCOL + 5, 6);
556 PUTRATE(uvmexp.syscalls, GENSTATROW + 1, GENSTATCOL + 11, 6);
557 PUTRATE(uvmexp.intrs, GENSTATROW + 1, GENSTATCOL + 17, 5);
558 PUTRATE(uvmexp.softs, GENSTATROW + 1, GENSTATCOL + 22, 5);
559 PUTRATE(uvmexp.faults, GENSTATROW + 1, GENSTATCOL + 27, 6);
560 for (l = 0, i = 0, r = DISKROW, c = DISKCOL;
561 i < (dk_ndrive + tp_ndrive); i++) {
562 if (i < dk_ndrive) {
563 if (!dk_select[i])
564 continue;
565 } else {
566 if (!tp_select[i - dk_ndrive])
567 continue;
568 }
569
570 if (disk_horiz)
571 c += DISKCOLWIDTH;
572 else
573 r++;
574 if (c + DISKCOLWIDTH > DISKCOLEND) {
575 if (disk_horiz && LINES - 1 - DISKROW >
576 (DISKCOLEND - DISKCOL) / DISKCOLWIDTH) {
577 disk_horiz = 0;
578 relabel = 1;
579 }
580 break;
581 }
582 if (r >= LINES - 1) {
583 if (!disk_horiz && LINES - 1 - DISKROW <
584 (DISKCOLEND - DISKCOL) / DISKCOLWIDTH) {
585 disk_horiz = 1;
586 relabel = 1;
587 }
588 break;
589 }
590 l++;
591
592 if (i < dk_ndrive)
593 dinfo(i, r, c);
594 else
595 tinfo(i - dk_ndrive, r, c);
596 }
597 /* blank out if we lost any disks */
598 for (i = l; i < last_disks; i++) {
599 int j;
600 if (disk_horiz)
601 c += DISKCOLWIDTH;
602 else
603 r++;
604 for (j = 0; j < 5; j++) {
605 if (disk_horiz)
606 mvprintw(r+j, c, "%*s", DISKCOLWIDTH, "");
607 else
608 mvprintw(r, c+j*DISKCOLWIDTH, "%*s", DISKCOLWIDTH, "");
609 }
610 }
611 last_disks = l;
612 putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
613 putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
614 #define nz(x) ((x) ? (x) : 1)
615 putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
616 NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
617 putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
618 putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
619 NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
620 #undef nz
621 }
622
623 void
624 vmstat_boot(char *args)
625 {
626 copyinfo(&z, &s1);
627 state = BOOT;
628 }
629
630 void
631 vmstat_run(char *args)
632 {
633 copyinfo(&s1, &s2);
634 state = RUN;
635 }
636
637 void
638 vmstat_time(char *args)
639 {
640 state = TIME;
641 }
642
643 void
644 vmstat_zero(char *args)
645 {
646 if (state == RUN)
647 getinfo(&s1, RUN);
648 }
649
650 /* calculate number of users on the system */
651 static int
652 ucount(void)
653 {
654 static int onusers = -1;
655 static struct utmpentry *oehead = NULL;
656 int nusers = 0;
657 struct utmpentry *ehead;
658
659 nusers = getutentries(NULL, &ehead);
660 if (oehead != ehead) {
661 freeutentries(oehead);
662 oehead = ehead;
663 }
664
665 if (nusers != onusers) {
666 if (nusers == 1)
667 mvprintw(STATROW, STATCOL + 8, " ");
668 else
669 mvprintw(STATROW, STATCOL + 8, "s");
670 }
671 onusers = nusers;
672 return (nusers);
673 }
674
675 static float
676 cputime(int indx)
677 {
678 double t;
679 int i;
680
681 t = 0;
682 for (i = 0; i < CPUSTATES; i++)
683 t += cur.cp_time[i];
684 if (t == 0.0)
685 t = 1.0;
686 return (cur.cp_time[indx] * 100.0 / t);
687 }
688
689 static void
690 puthumanint(u_int64_t n, int l, int c, int w)
691 {
692 char b[128];
693
694 if (move(l, c) != OK)
695 return;
696 if (n == 0) {
697 hline(' ', w);
698 return;
699 }
700 if (humanize_number(b, w, n, "", HN_AUTOSCALE, HN_NOSPACE) == -1 ) {
701 hline('*', w);
702 return;
703 }
704 hline(' ', w - strlen(b));
705 mvaddstr(l, c + w - strlen(b), b);
706 }
707
708 static void
709 putint(int 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 (void)snprintf(b, sizeof b, "%*d", w, n);
720 if (strlen(b) > w) {
721 hline('*', w);
722 return;
723 }
724 addstr(b);
725 }
726
727 static void
728 putfloat(double f, int l, int c, int w, int d, int nz)
729 {
730 char b[128];
731
732 if (move(l, c) != OK)
733 return;
734 if (nz && f == 0.0) {
735 hline(' ', w);
736 return;
737 }
738 (void)snprintf(b, sizeof b, "%*.*f", w, d, f);
739 if (strlen(b) > w) {
740 hline('*', w);
741 return;
742 }
743 addstr(b);
744 }
745
746 static void
747 getinfo(struct Info *stats, enum state st)
748 {
749 int mib[2];
750 size_t size;
751 int i;
752
753 cpureadstats();
754 dkreadstats();
755 tpreadstats();
756 NREAD(X_NCHSTATS, &stats->nchstats, sizeof stats->nchstats);
757 if (nintr)
758 NREAD(X_INTRCNT, stats->intrcnt, nintr * LONG);
759 for (i = 0; i < nevcnt; i++)
760 KREAD(ie_head[i].ie_count, &stats->evcnt[i],
761 sizeof stats->evcnt[i]);
762 size = sizeof(stats->uvmexp);
763 mib[0] = CTL_VM;
764 mib[1] = VM_UVMEXP2;
765 if (sysctl(mib, 2, &stats->uvmexp, &size, NULL, 0) < 0) {
766 error("can't get uvmexp: %s\n", strerror(errno));
767 memset(&stats->uvmexp, 0, sizeof(stats->uvmexp));
768 }
769 size = sizeof(stats->Total);
770 mib[0] = CTL_VM;
771 mib[1] = VM_METER;
772 if (sysctl(mib, 2, &stats->Total, &size, NULL, 0) < 0) {
773 error("Can't get kernel info: %s\n", strerror(errno));
774 memset(&stats->Total, 0, sizeof(stats->Total));
775 }
776 }
777
778 static void
779 allocinfo(struct Info *stats)
780 {
781
782 if (nintr &&
783 (stats->intrcnt = calloc(nintr, sizeof(long))) == NULL) {
784 error("calloc failed");
785 die(0);
786 }
787 if ((stats->evcnt = calloc(nevcnt, sizeof(u_int64_t))) == NULL) {
788 error("calloc failed");
789 die(0);
790 }
791 }
792
793 static void
794 copyinfo(struct Info *from, struct Info *to)
795 {
796 long *intrcnt;
797 u_int64_t *evcnt;
798
799 intrcnt = to->intrcnt;
800 evcnt = to->evcnt;
801 *to = *from;
802 memmove(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof *intrcnt);
803 memmove(to->evcnt = evcnt, from->evcnt, nevcnt * sizeof *evcnt);
804 }
805
806 static void
807 dinfo(int dn, int r, int c)
808 {
809 double atime;
810 #define ADV if (disk_horiz) r++; else c += DISKCOLWIDTH
811
812 mvprintw(r, c, "%*.*s", DISKCOLWIDTH, DISKCOLWIDTH, dr_name[dn]);
813 ADV;
814
815 putint((int)(cur.dk_seek[dn]/etime+0.5), r, c, DISKCOLWIDTH);
816 ADV;
817 putint((int)((cur.dk_rxfer[dn]+cur.dk_wxfer[dn])/etime+0.5),
818 r, c, DISKCOLWIDTH);
819 ADV;
820 puthumanint((cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) / etime + 0.5,
821 r, c, DISKCOLWIDTH);
822 ADV;
823
824 /* time busy in disk activity */
825 atime = cur.dk_time[dn].tv_sec + cur.dk_time[dn].tv_usec / 1000000.0;
826 atime = atime * 100.0 / etime;
827 if (atime >= 100)
828 putint(100, r, c, DISKCOLWIDTH);
829 else
830 putfloat(atime, r, c, DISKCOLWIDTH, 1, 1);
831 }
832
833 static void
834 tinfo(int dn, int r, int c)
835 {
836 double atime;
837
838 mvprintw(r, c, "%*.*s", DISKCOLWIDTH, DISKCOLWIDTH, tp_name[dn]);
839 ADV;
840 ADV; /* skip over the seeks column - not relevant for tape drives */
841
842 putint((int)((cur_tape.rxfer[dn]+cur_tape.wxfer[dn])/etime+0.5),
843 r, c, DISKCOLWIDTH);
844 ADV;
845 puthumanint((cur_tape.rbytes[dn] + cur_tape.wbytes[dn]) / etime + 0.5,
846 r, c, DISKCOLWIDTH);
847 ADV;
848
849 /* time busy in disk activity */
850 atime = cur_tape.time[dn].tv_sec + cur_tape.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 #undef ADV
857 }
858
859
860