Merge commit 'crater/master'
[dragonfly.git] / usr.bin / systat / vmstat.c
blobc18457f657465dc03d98bbae706331e440be042b
1 /*-
2 * Copyright (c) 1983, 1989, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#)vmstat.c 8.2 (Berkeley) 1/12/94
34 * $FreeBSD: src/usr.bin/systat/vmstat.c,v 1.38.2.4 2002/03/12 19:50:23 phantom Exp $
35 * $DragonFly: src/usr.bin/systat/vmstat.c,v 1.12 2008/11/10 04:59:45 swildner Exp $
39 * Cursed vmstat -- from Robert Elz.
42 #define _KERNEL_STRUCTURES
43 #include <sys/param.h>
44 #include <sys/stat.h>
45 #include <sys/time.h>
46 #include <sys/user.h>
47 #include <sys/uio.h>
48 #include <sys/namei.h>
49 #include <sys/sysctl.h>
50 #include <sys/vmmeter.h>
52 #include <vm/vm_param.h>
54 #include <ctype.h>
55 #include <err.h>
56 #include <errno.h>
57 #include <kinfo.h>
58 #include <langinfo.h>
59 #include <nlist.h>
60 #include <paths.h>
61 #include <signal.h>
62 #include <stddef.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <time.h>
66 #include <unistd.h>
67 #include <utmp.h>
68 #include <devstat.h>
69 #include "systat.h"
70 #include "extern.h"
71 #include "devs.h"
73 static struct Info {
74 struct kinfo_cputime cp_time;
75 struct vmmeter Vmm;
76 struct vmtotal Total;
77 struct vmstats Vms;
78 struct nchstats nchstats;
79 long nchcount;
80 long *intrcnt;
81 int bufspace;
82 int desiredvnodes;
83 long numvnodes;
84 long freevnodes;
85 long dirtybufspace;
86 } s, s1, s2, z;
88 struct kinfo_cputime cp_time, old_cp_time;
89 struct statinfo cur, last, run;
91 #define vmm s.Vmm
92 #define vms s.Vms
93 #define oldvmm s1.Vmm
94 #define oldvms s1.Vms
95 #define total s.Total
96 #define nchtotal s.nchstats
97 #define oldnchtotal s1.nchstats
99 static enum state { BOOT, TIME, RUN } state = TIME;
101 static void allocinfo(struct Info *);
102 static void copyinfo(struct Info *, struct Info *);
103 static void dinfo(int, int, struct statinfo *, struct statinfo *);
104 static void getinfo(struct Info *);
105 static void putint(int, int, int, int, int);
106 static void putfloat(double, int, int, int, int, int);
107 static void putlongdouble(long double, int, int, int, int, int);
108 static int ucount(void);
110 static int ncpu;
111 static int ut;
112 static char buf[26];
113 static time_t t;
114 static double etime;
115 static int nintr;
116 static long *intrloc;
117 static char **intrname;
118 static int nextintsrow;
119 static int extended_vm_stats;
121 struct utmp utmp;
124 WINDOW *
125 openkre(void)
128 ut = open(_PATH_UTMP, O_RDONLY);
129 if (ut < 0)
130 error("No utmp");
131 return (stdscr);
134 void
135 closekre(WINDOW *w)
138 (void) close(ut);
139 if (w == NULL)
140 return;
141 wclear(w);
142 wrefresh(w);
146 static struct nlist namelist[] = {
147 #define X_BUFFERSPACE 0
148 { .n_name = "_bufspace" },
149 #define X_NCHSTATS 1
150 { .n_name = "_nchstats" },
151 #define X_DESIREDVNODES 2
152 { .n_name = "_desiredvnodes" },
153 #define X_NUMVNODES 3
154 { .n_name = "_numvnodes" },
155 #define X_FREEVNODES 4
156 { .n_name = "_freevnodes" },
157 #define X_NUMDIRTYBUFFERS 5
158 { .n_name = "_dirtybufspace" },
159 { .n_name = "" },
163 * These constants define where the major pieces are laid out
165 #define STATROW 0 /* uses 1 row and 68 cols */
166 #define STATCOL 2
167 #define MEMROW 2 /* uses 4 rows and 31 cols */
168 #define MEMCOL 0
169 #define PAGEROW 2 /* uses 4 rows and 26 cols */
170 #define PAGECOL 46
171 #define INTSROW 6 /* uses all rows to bottom and 17 cols */
172 #define INTSCOL 61
173 #define PROCSROW 7 /* uses 2 rows and 20 cols */
174 #define PROCSCOL 0
175 #define GENSTATROW 7 /* uses 2 rows and 30 cols */
176 #define GENSTATCOL 16
177 #define VMSTATROW 6 /* uses 17 rows and 12 cols */
178 #define VMSTATCOL 48
179 #define GRAPHROW 10 /* uses 3 rows and 51 cols */
180 #define GRAPHCOL 0
181 #define NAMEIROW 14 /* uses 3 rows and 38 cols */
182 #define NAMEICOL 0
183 #define DISKROW 18 /* uses 5 rows and 50 cols (for 9 drives) */
184 #define DISKCOL 0
186 #define DRIVESPACE 7 /* max # for space */
188 #define MAXDRIVES DRIVESPACE /* max # to display */
191 initkre(void)
193 char *intrnamebuf;
194 size_t bytes;
195 size_t b;
196 size_t i;
198 if (namelist[0].n_type == 0) {
199 if (kvm_nlist(kd, namelist)) {
200 nlisterr(namelist);
201 return(0);
203 if (namelist[0].n_type == 0) {
204 error("No namelist");
205 return(0);
209 if ((num_devices = getnumdevs()) < 0) {
210 warnx("%s", devstat_errbuf);
211 return(0);
214 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
215 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
216 run.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
217 bzero(cur.dinfo, sizeof(struct devinfo));
218 bzero(last.dinfo, sizeof(struct devinfo));
219 bzero(run.dinfo, sizeof(struct devinfo));
221 if (dsinit(MAXDRIVES, &cur, &last, &run) != 1)
222 return(0);
224 if (nintr == 0) {
225 if (sysctlbyname("hw.intrnames", NULL, &bytes, NULL, 0) == 0) {
226 intrnamebuf = malloc(bytes);
227 sysctlbyname("hw.intrnames", intrnamebuf, &bytes,
228 NULL, 0);
229 for (i = 0; i < bytes; ++i) {
230 if (intrnamebuf[i] == 0)
231 ++nintr;
233 intrname = malloc(nintr * sizeof(char *));
234 intrloc = malloc(nintr * sizeof(*intrloc));
235 nintr = 0;
236 for (b = i = 0; i < bytes; ++i) {
237 if (intrnamebuf[i] == 0) {
238 intrname[nintr] = intrnamebuf + b;
239 intrloc[nintr] = 0;
240 b = i + 1;
241 ++nintr;
245 nextintsrow = INTSROW + 2;
246 allocinfo(&s);
247 allocinfo(&s1);
248 allocinfo(&s2);
249 allocinfo(&z);
251 getinfo(&s2);
252 copyinfo(&s2, &s1);
253 return(1);
256 void
257 fetchkre(void)
259 time_t now;
260 struct tm *tp;
261 static int d_first = -1;
263 if (d_first < 0)
264 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
266 time(&now);
267 tp = localtime(&now);
268 (void) strftime(buf, sizeof(buf),
269 d_first ? "%e %b %R" : "%b %e %R", tp);
270 getinfo(&s);
273 void
274 labelkre(void)
276 int i, j;
278 clear();
279 mvprintw(STATROW, STATCOL + 4, "users Load");
280 mvprintw(MEMROW, MEMCOL, "Mem:KB REAL VIRTUAL");
281 mvprintw(MEMROW + 1, MEMCOL, " Tot Share Tot Share");
282 mvprintw(MEMROW + 2, MEMCOL, "Act");
283 mvprintw(MEMROW + 3, MEMCOL, "All");
285 mvprintw(MEMROW + 1, MEMCOL + 41, "Free");
287 mvprintw(PAGEROW, PAGECOL, " VN PAGER SWAP PAGER ");
288 mvprintw(PAGEROW + 1, PAGECOL, " in out in out ");
289 mvprintw(PAGEROW + 2, PAGECOL, "count");
290 mvprintw(PAGEROW + 3, PAGECOL, "pages");
292 mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
293 mvprintw(INTSROW + 1, INTSCOL + 9, "total");
295 mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "cow");
296 mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "wire");
297 mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "act");
298 mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "inact");
299 mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "cache");
300 mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "free");
301 mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "daefr");
302 mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "prcfr");
303 mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "react");
304 mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "pdwake");
305 mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "pdpgs");
306 mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "intrn");
307 mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "buf");
308 mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "dirtybuf");
310 mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "desiredvnodes");
311 mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "numvnodes");
312 mvprintw(VMSTATROW + 17, VMSTATCOL + 10, "freevnodes");
314 mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt");
316 mvprintw(GRAPHROW, GRAPHCOL,
317 " . %%Sys . %%Intr . %%User . %%Nice . %%Idle");
318 mvprintw(PROCSROW, PROCSCOL, " r p d s w");
319 mvprintw(GRAPHROW + 1, GRAPHCOL,
320 "| | | | | | | | | | |");
322 mvprintw(NAMEIROW, NAMEICOL, "Namei Name-cache Dir-cache");
323 mvprintw(NAMEIROW + 1, NAMEICOL,
324 " Calls hits %% hits %%");
325 mvprintw(DISKROW, DISKCOL, "Disks");
326 mvprintw(DISKROW + 1, DISKCOL, "KB/t");
327 mvprintw(DISKROW + 2, DISKCOL, "tps");
328 mvprintw(DISKROW + 3, DISKCOL, "MB/s");
329 mvprintw(DISKROW + 4, DISKCOL, "%% busy");
331 * For now, we don't support a fourth disk statistic. So there's
332 * no point in providing a label for it. If someone can think of a
333 * fourth useful disk statistic, there is room to add it.
335 /* mvprintw(DISKROW + 4, DISKCOL, " msps"); */
336 j = 0;
337 for (i = 0; i < num_devices && j < MAXDRIVES; i++)
338 if (dev_select[i].selected) {
339 char tmpstr[80];
340 sprintf(tmpstr, "%s%d", dev_select[i].device_name,
341 dev_select[i].unit_number);
342 mvprintw(DISKROW, DISKCOL + 5 + 6 * j,
343 " %5.5s", tmpstr);
344 j++;
347 if (j <= 4) {
349 * room for extended VM stats
351 mvprintw(VMSTATROW + 11, VMSTATCOL - 6, "zfod");
352 mvprintw(VMSTATROW + 12, VMSTATCOL - 6, "ofod");
353 mvprintw(VMSTATROW + 13, VMSTATCOL - 6, "%%slo-z");
354 mvprintw(VMSTATROW + 14, VMSTATCOL - 6, "tfree");
355 extended_vm_stats = 1;
356 } else {
357 extended_vm_stats = 0;
358 mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "zfod");
361 for (i = 0; i < nintr; i++) {
362 if (intrloc[i] == 0)
363 continue;
364 mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s", intrname[i]);
368 #define CP_UPDATE(fld) do { \
369 uint64_t lt; \
370 lt=s.fld; \
371 s.fld-=s1.fld; \
372 if(state==TIME) \
373 s1.fld=lt; \
374 lt=fld; \
375 fld-=old_##fld; \
376 if(state==TIME) \
377 old_##fld=lt; \
378 etime += s.fld; \
379 } while(0)
380 #define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
381 #define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
382 #define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
383 if(state == TIME) s1.nchstats.fld = t;}
384 #define PUTRATE(fld, l, c, w) \
385 Y(fld); \
386 putint((int)((float)s.fld/etime + 0.5), l, c, w, 'D')
387 #define MAXFAIL 5
389 #define CPUSTATES 5
390 static const char cpuchar[5] = { '=' , '+', '>', '-', ' ' };
392 static const size_t cpuoffsets[] = {
393 offsetof(struct kinfo_cputime, cp_sys),
394 offsetof(struct kinfo_cputime, cp_intr),
395 offsetof(struct kinfo_cputime, cp_user),
396 offsetof(struct kinfo_cputime, cp_nice),
397 offsetof(struct kinfo_cputime, cp_idle)
400 void
401 showkre(void)
403 float f1, f2;
404 int psiz, inttotal;
405 int i, l, lc;
406 static int failcnt = 0;
407 double total_time;
409 etime = 0;
410 CP_UPDATE(cp_time.cp_user);
411 CP_UPDATE(cp_time.cp_nice);
412 CP_UPDATE(cp_time.cp_sys);
413 CP_UPDATE(cp_time.cp_intr);
414 CP_UPDATE(cp_time.cp_idle);
416 total_time = etime;
417 if (total_time == 0.0)
418 total_time = 1.0;
420 if (etime < 100000.0) { /* < 100ms ignore this trash */
421 if (failcnt++ >= MAXFAIL) {
422 clear();
423 mvprintw(2, 10, "The alternate system clock has died!");
424 mvprintw(3, 10, "Reverting to ``pigs'' display.");
425 move(CMDLINE, 0);
426 refresh();
427 failcnt = 0;
428 sleep(5);
429 command("pigs");
431 return;
433 failcnt = 0;
434 etime /= 1000000.0;
435 etime /= ncpu;
436 if (etime == 0)
437 etime = 1;
438 inttotal = 0;
439 for (i = 0; i < nintr; i++) {
440 if (s.intrcnt[i] == 0)
441 continue;
442 if (intrloc[i] == 0) {
443 if (nextintsrow == LINES)
444 continue;
445 intrloc[i] = nextintsrow++;
446 mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s",
447 intrname[i]);
449 X(intrcnt);
450 l = (int)((float)s.intrcnt[i]/etime + 0.5);
451 inttotal += l;
452 putint(l, intrloc[i], INTSCOL + 2, 6, 'D');
454 putint(inttotal, INTSROW + 1, INTSCOL + 2, 6, 'D');
455 Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
456 Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes); Z(ncs_neghits);
457 s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
458 nchtotal.ncs_miss + nchtotal.ncs_long + nchtotal.ncs_neghits;
459 if (state == TIME)
460 s1.nchcount = s.nchcount;
462 psiz = 0;
463 f2 = 0.0;
464 for (lc = 0; lc < CPUSTATES; lc++) {
465 uint64_t val = *(uint64_t *)(((uint8_t *)&s.cp_time) +
466 cpuoffsets[lc]);
467 f1 = 100.0 * val / total_time;
468 f2 += f1;
469 l = (int) ((f2 + 1.0) / 2.0) - psiz;
470 if (f1 > 99.9)
471 f1 = 99.9; /* no room to display 100.0 */
472 putfloat(f1, GRAPHROW, GRAPHCOL + 10 * lc, 4, 1, 0);
473 move(GRAPHROW + 2, psiz);
474 psiz += l;
475 while (l-- > 0)
476 addch(cpuchar[lc]);
479 putint(ucount(), STATROW, STATCOL, 3, 'D');
480 putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
481 putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
482 putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
483 mvaddstr(STATROW, STATCOL + 53, buf);
484 #define pgtokb(pg) ((pg) * vms.v_page_size / 1024)
485 putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 8, 'K');
486 putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 11, 8, 'K');
487 putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 19, 9, 'K');
488 putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 28, 9, 'K');
489 putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 8, 'K');
490 putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 11, 8, 'K');
491 putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 19, 9, 'K');
492 putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 28, 9, 'K');
493 putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 37, 8, 'K');
494 putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 0, 3, 'D');
495 putint(total.t_pw, PROCSROW + 1, PROCSCOL + 3, 3, 'D');
496 putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3, 'D');
497 putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3, 'D');
498 putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3, 'D');
499 if (extended_vm_stats == 0) {
500 PUTRATE(Vmm.v_zfod, VMSTATROW + 0, VMSTATCOL + 4, 5);
502 PUTRATE(Vmm.v_cow_faults, VMSTATROW + 1, VMSTATCOL + 3, 6);
503 putint(pgtokb(vms.v_wire_count), VMSTATROW + 2, VMSTATCOL, 9, 'K');
504 putint(pgtokb(vms.v_active_count), VMSTATROW + 3, VMSTATCOL, 9, 'K');
505 putint(pgtokb(vms.v_inactive_count), VMSTATROW + 4, VMSTATCOL, 9, 'K');
506 putint(pgtokb(vms.v_cache_count), VMSTATROW + 5, VMSTATCOL, 9, 'K');
507 putint(pgtokb(vms.v_free_count), VMSTATROW + 6, VMSTATCOL, 9, 'K');
508 PUTRATE(Vmm.v_dfree, VMSTATROW + 7, VMSTATCOL, 9);
509 PUTRATE(Vmm.v_pfree, VMSTATROW + 8, VMSTATCOL, 9);
510 PUTRATE(Vmm.v_reactivated, VMSTATROW + 9, VMSTATCOL, 9);
511 PUTRATE(Vmm.v_pdwakeups, VMSTATROW + 10, VMSTATCOL, 9);
512 PUTRATE(Vmm.v_pdpages, VMSTATROW + 11, VMSTATCOL, 9);
513 PUTRATE(Vmm.v_intrans, VMSTATROW + 12, VMSTATCOL, 9);
515 if (extended_vm_stats) {
516 PUTRATE(Vmm.v_zfod, VMSTATROW + 11, VMSTATCOL - 16, 9);
517 PUTRATE(Vmm.v_ozfod, VMSTATROW + 12, VMSTATCOL - 16, 9);
518 putint(
519 ((s.Vmm.v_ozfod < s.Vmm.v_zfod) ?
520 s.Vmm.v_ozfod * 100 / s.Vmm.v_zfod :
523 VMSTATROW + 13,
524 VMSTATCOL - 16,
528 PUTRATE(Vmm.v_tfree, VMSTATROW + 14, VMSTATCOL - 16, 9);
531 putint(s.bufspace/1024, VMSTATROW + 13, VMSTATCOL, 9, 'K');
532 putint(s.dirtybufspace/1024, VMSTATROW + 14, VMSTATCOL, 9, 'K');
533 putint(s.desiredvnodes, VMSTATROW + 15, VMSTATCOL, 9, 'D');
534 putint(s.numvnodes, VMSTATROW + 16, VMSTATCOL, 9, 'D');
535 putint(s.freevnodes, VMSTATROW + 17, VMSTATCOL, 9, 'D');
536 PUTRATE(Vmm.v_vnodein, PAGEROW + 2, PAGECOL + 5, 5);
537 PUTRATE(Vmm.v_vnodeout, PAGEROW + 2, PAGECOL + 10, 5);
538 PUTRATE(Vmm.v_swapin, PAGEROW + 2, PAGECOL + 17, 5);
539 PUTRATE(Vmm.v_swapout, PAGEROW + 2, PAGECOL + 22, 5);
540 PUTRATE(Vmm.v_vnodepgsin, PAGEROW + 3, PAGECOL + 5, 5);
541 PUTRATE(Vmm.v_vnodepgsout, PAGEROW + 3, PAGECOL + 10, 5);
542 PUTRATE(Vmm.v_swappgsin, PAGEROW + 3, PAGECOL + 17, 5);
543 PUTRATE(Vmm.v_swappgsout, PAGEROW + 3, PAGECOL + 22, 5);
544 PUTRATE(Vmm.v_swtch, GENSTATROW + 1, GENSTATCOL, 5);
545 PUTRATE(Vmm.v_trap, GENSTATROW + 1, GENSTATCOL + 6, 5);
546 PUTRATE(Vmm.v_syscall, GENSTATROW + 1, GENSTATCOL + 12, 5);
547 PUTRATE(Vmm.v_intr, GENSTATROW + 1, GENSTATCOL + 18, 5);
548 PUTRATE(Vmm.v_soft, GENSTATROW + 1, GENSTATCOL + 23, 5);
549 PUTRATE(Vmm.v_vm_faults, GENSTATROW + 1, GENSTATCOL + 29, 5);
550 mvprintw(DISKROW, DISKCOL + 5, " ");
551 for (i = 0, lc = 0; i < num_devices && lc < MAXDRIVES; i++)
552 if (dev_select[i].selected) {
553 char tmpstr[80];
554 sprintf(tmpstr, "%s%d", dev_select[i].device_name,
555 dev_select[i].unit_number);
556 mvprintw(DISKROW, DISKCOL + 5 + 6 * lc,
557 " %5.5s", tmpstr);
558 switch(state) {
559 case TIME:
560 dinfo(i, ++lc, &cur, &last);
561 break;
562 case RUN:
563 dinfo(i, ++lc, &cur, &run);
564 break;
565 case BOOT:
566 dinfo(i, ++lc, &cur, NULL);
567 break;
570 putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9, 'D');
571 putint((nchtotal.ncs_goodhits + nchtotal.ncs_neghits),
572 NAMEIROW + 2, NAMEICOL + 9, 9, 'D');
573 #define nz(x) ((x) ? (x) : 1)
574 putfloat((nchtotal.ncs_goodhits+nchtotal.ncs_neghits) *
575 100.0 / nz(s.nchcount),
576 NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
577 putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9, 'D');
578 putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
579 NAMEIROW + 2, NAMEICOL + 33, 4, 0, 1);
580 #undef nz
584 cmdkre(const char *cmd, char *args)
586 int retval;
588 if (prefix(cmd, "run")) {
589 retval = 1;
590 copyinfo(&s2, &s1);
591 switch (getdevs(&run)) {
592 case -1:
593 errx(1, "%s", devstat_errbuf);
594 break;
595 case 1:
596 num_devices = run.dinfo->numdevs;
597 generation = run.dinfo->generation;
598 retval = dscmd("refresh", NULL, MAXDRIVES, &cur);
599 if (retval == 2)
600 labelkre();
601 break;
602 default:
603 break;
605 state = RUN;
606 return (retval);
608 if (prefix(cmd, "boot")) {
609 state = BOOT;
610 copyinfo(&z, &s1);
611 return (1);
613 if (prefix(cmd, "time")) {
614 state = TIME;
615 return (1);
617 if (prefix(cmd, "zero")) {
618 retval = 1;
619 if (state == RUN) {
620 getinfo(&s1);
621 switch (getdevs(&run)) {
622 case -1:
623 errx(1, "%s", devstat_errbuf);
624 break;
625 case 1:
626 num_devices = run.dinfo->numdevs;
627 generation = run.dinfo->generation;
628 retval = dscmd("refresh",NULL, MAXDRIVES, &cur);
629 if (retval == 2)
630 labelkre();
631 break;
632 default:
633 break;
636 return (retval);
638 retval = dscmd(cmd, args, MAXDRIVES, &cur);
640 if (retval == 2)
641 labelkre();
643 return(retval);
646 /* calculate number of users on the system */
647 static int
648 ucount(void)
650 int nusers = 0;
652 if (ut < 0)
653 return (0);
654 while (read(ut, &utmp, sizeof(utmp)))
655 if (utmp.ut_name[0] != '\0')
656 nusers++;
658 lseek(ut, 0L, L_SET);
659 return (nusers);
662 static void
663 putint(int n, int l, int lc, int w, int type)
665 char b[128];
666 int xtype;
668 move(l, lc);
669 if (n == 0) {
670 while (w-- > 0)
671 addch(' ');
672 return;
674 snprintf(b, sizeof(b), "%*d", w, n);
675 if (strlen(b) > (size_t)w) {
676 if (type == 'D') {
677 n /= 1000;
678 xtype = 'K';
679 } else {
680 n /= 1024;
681 xtype = 'M';
683 snprintf(b, sizeof(b), "%*d%c", w - 1, n, xtype);
684 if (strlen(b) > (size_t)w) {
685 if (type == 'D') {
686 n /= 1000;
687 xtype = 'M';
688 } else {
689 n /= 1024;
690 xtype = 'G';
692 snprintf(b, sizeof(b), "%*d%c", w - 1, n, xtype);
693 if (strlen(b) > (size_t)w) {
694 while (w-- > 0)
695 addch('*');
696 return;
700 addstr(b);
703 static void
704 putfloat(double f, int l, int lc, int w, int d, int nz)
706 char b[128];
708 move(l, lc);
709 if (nz && f == 0.0) {
710 while (--w >= 0)
711 addch(' ');
712 return;
714 snprintf(b, sizeof(b), "%*.*f", w, d, f);
715 if (strlen(b) > (size_t)w)
716 snprintf(b, sizeof(b), "%*.0f", w, f);
717 if (strlen(b) > (size_t)w) {
718 while (--w >= 0)
719 addch('*');
720 return;
722 addstr(b);
725 static void
726 putlongdouble(long double f, int l, int lc, int w, int d, int nz)
728 char b[128];
730 move(l, lc);
731 if (nz && f == 0.0) {
732 while (--w >= 0)
733 addch(' ');
734 return;
736 sprintf(b, "%*.*Lf", w, d, f);
737 if (strlen(b) > (size_t)w)
738 sprintf(b, "%*.0Lf", w, f);
739 if (strlen(b) > (size_t)w) {
740 while (--w >= 0)
741 addch('*');
742 return;
744 addstr(b);
747 static void
748 getinfo(struct Info *ls)
750 struct devinfo *tmp_dinfo;
751 struct nchstats *nch_tmp;
752 size_t size;
753 size_t vms_size = sizeof(ls->Vms);
754 size_t vmm_size = sizeof(ls->Vmm);
755 size_t nch_size = sizeof(ls->nchstats) * SMP_MAXCPU;
757 if (sysctlbyname("vm.vmstats", &ls->Vms, &vms_size, NULL, 0)) {
758 perror("sysctlbyname: vm.vmstats");
759 exit(1);
761 if (sysctlbyname("vm.vmmeter", &ls->Vmm, &vmm_size, NULL, 0)) {
762 perror("sysctlbyname: vm.vmstats");
763 exit(1);
766 if (kinfo_get_sched_cputime(&ls->cp_time))
767 err(1, "kinfo_get_sched_cputime");
768 if (kinfo_get_sched_cputime(&cp_time))
769 err(1, "kinfo_get_sched_cputime");
770 NREAD(X_BUFFERSPACE, &ls->bufspace, sizeof(ls->bufspace));
771 NREAD(X_DESIREDVNODES, &ls->desiredvnodes, sizeof(ls->desiredvnodes));
772 NREAD(X_NUMVNODES, &ls->numvnodes, LONG);
773 NREAD(X_FREEVNODES, &ls->freevnodes, LONG);
774 NREAD(X_NUMDIRTYBUFFERS, &ls->dirtybufspace, sizeof(ls->dirtybufspace));
776 if (nintr) {
777 size = nintr * sizeof(ls->intrcnt[0]);
778 sysctlbyname("hw.intrcnt", ls->intrcnt, &size, NULL, 0);
780 size = sizeof(ls->Total);
781 if (sysctlbyname("vm.vmtotal", &ls->Total, &size, NULL, 0) < 0) {
782 error("Can't get kernel info: %s\n", strerror(errno));
783 bzero(&ls->Total, sizeof(ls->Total));
786 if ((nch_tmp = malloc(nch_size)) == NULL) {
787 perror("malloc");
788 exit(1);
789 } else {
790 if (sysctlbyname("vfs.cache.nchstats", nch_tmp, &nch_size, NULL, 0)) {
791 perror("sysctlbyname vfs.cache.nchstats");
792 free(nch_tmp);
793 exit(1);
794 } else {
795 if ((nch_tmp = realloc(nch_tmp, nch_size)) == NULL) {
796 perror("realloc");
797 exit(1);
802 if (kinfo_get_cpus(&ncpu))
803 err(1, "kinfo_get_cpus");
804 kvm_nch_cpuagg(nch_tmp, &ls->nchstats, ncpu);
805 free(nch_tmp);
807 tmp_dinfo = last.dinfo;
808 last.dinfo = cur.dinfo;
809 cur.dinfo = tmp_dinfo;
811 last.busy_time = cur.busy_time;
812 switch (getdevs(&cur)) {
813 case -1:
814 errx(1, "%s", devstat_errbuf);
815 break;
816 case 1:
817 num_devices = cur.dinfo->numdevs;
818 generation = cur.dinfo->generation;
819 cmdkre("refresh", NULL);
820 break;
821 default:
822 break;
826 static void
827 allocinfo(struct Info *ls)
829 ls->intrcnt = (long *) calloc(nintr, sizeof(long));
830 if (ls->intrcnt == NULL)
831 errx(2, "out of memory");
834 static void
835 copyinfo(struct Info *from, struct Info *to)
837 long *intrcnt;
840 * time, wds, seek, and xfer are malloc'd so we have to
841 * save the pointers before the structure copy and then
842 * copy by hand.
844 intrcnt = to->intrcnt;
845 *to = *from;
847 bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
850 static void
851 dinfo(int dn, int lc, struct statinfo *now, struct statinfo *then)
853 long double transfers_per_second;
854 long double kb_per_transfer, mb_per_second;
855 long double elapsed_time, device_busy;
856 int di;
858 di = dev_select[dn].position;
860 elapsed_time = compute_etime(now->busy_time, then ?
861 then->busy_time :
862 now->dinfo->devices[di].dev_creation_time);
864 device_busy = compute_etime(now->dinfo->devices[di].busy_time, then ?
865 then->dinfo->devices[di].busy_time :
866 now->dinfo->devices[di].dev_creation_time);
868 if (compute_stats(&now->dinfo->devices[di], then ?
869 &then->dinfo->devices[di] : NULL, elapsed_time,
870 NULL, NULL, NULL,
871 &kb_per_transfer, &transfers_per_second,
872 &mb_per_second, NULL, NULL) != 0)
873 errx(1, "%s", devstat_errbuf);
875 if ((device_busy == 0) && (transfers_per_second > 5))
876 /* the device has been 100% busy, fake it because
877 * as long as the device is 100% busy the busy_time
878 * field in the devstat struct is not updated */
879 device_busy = elapsed_time;
880 if (device_busy > elapsed_time)
881 /* this normally happens after one or more periods
882 * where the device has been 100% busy, correct it */
883 device_busy = elapsed_time;
885 lc = DISKCOL + lc * 6;
886 putlongdouble(kb_per_transfer, DISKROW + 1, lc, 5, 2, 0);
887 putlongdouble(transfers_per_second, DISKROW + 2, lc, 5, 0, 0);
888 putlongdouble(mb_per_second, DISKROW + 3, lc, 5, 2, 0);
889 putlongdouble(device_busy * 100 / elapsed_time, DISKROW + 4, lc, 5, 0, 0);