Merge branch 'master' into net80211-update
[dragonfly.git] / usr.bin / systat / vmstat.c
1 /*-
2  * Copyright (c) 1983, 1989, 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
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.
20  *
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.
32  *
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 $
36  */
37
38 /*
39  * Cursed vmstat -- from Robert Elz.
40  */
41
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>
51
52 #include <vm/vm_param.h>
53
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"
72
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    nchpathcount;
81         long    *intrcnt;
82         int     bufspace;
83         int     desiredvnodes;
84         long    numvnodes;
85         long    freevnodes;
86         long    dirtybufspace;
87 } s, s1, s2, z;
88
89 struct kinfo_cputime cp_time, old_cp_time;
90 struct statinfo cur, last, run;
91
92 #define vmm s.Vmm
93 #define vms s.Vms
94 #define oldvmm s1.Vmm
95 #define oldvms s1.Vms
96 #define total s.Total
97 #define nchtotal s.nchstats
98 #define oldnchtotal s1.nchstats
99
100 static  enum state { BOOT, TIME, RUN } state = TIME;
101
102 static void allocinfo(struct Info *);
103 static void copyinfo(struct Info *, struct Info *);
104 static void dinfo(int, int, struct statinfo *, struct statinfo *);
105 static void getinfo(struct Info *);
106 static void putint(int, int, int, int, int);
107 static void putfloat(double, int, int, int, int, int);
108 static void putlongdouble(long double, int, int, int, int, int);
109 static void putlongdoublez(long double, int, int, int, int, int);
110 static int ucount(void);
111
112 static  int ncpu;
113 static  int ut;
114 static  char buf[26];
115 static  time_t t;
116 static  double etime;
117 static  int nintr;
118 static  long *intrloc;
119 static  char **intrname;
120 static  int nextintsrow;
121 static  int extended_vm_stats;
122
123 struct  utmp utmp;
124
125
126 WINDOW *
127 openkre(void)
128 {
129
130         ut = open(_PATH_UTMP, O_RDONLY);
131         if (ut < 0)
132                 error("No utmp");
133         return (stdscr);
134 }
135
136 void
137 closekre(WINDOW *w)
138 {
139
140         (void) close(ut);
141         if (w == NULL)
142                 return;
143         wclear(w);
144         wrefresh(w);
145 }
146
147
148 static struct nlist namelist[] = {
149 #define X_BUFFERSPACE   0
150         { .n_name = "_bufspace" },
151 #define X_NCHSTATS      1
152         { .n_name = "_nchstats" },
153 #define X_DESIREDVNODES 2
154         { .n_name = "_desiredvnodes" },
155 #define X_NUMVNODES     3
156         { .n_name = "_numvnodes" },
157 #define X_FREEVNODES    4
158         { .n_name = "_freevnodes" },
159 #define X_NUMDIRTYBUFFERS 5
160         { .n_name = "_dirtybufspace" },
161         { .n_name = "" },
162 };
163
164 /*
165  * These constants define where the major pieces are laid out
166  */
167 #define STATROW          0      /* uses 1 row and 68 cols */
168 #define STATCOL          2
169 #define MEMROW           2      /* uses 4 rows and 31 cols */
170 #define MEMCOL           0
171 #define PAGEROW          2      /* uses 4 rows and 26 cols */
172 #define PAGECOL         46
173 #define INTSROW          6      /* uses all rows to bottom and 17 cols */
174 #define INTSCOL         61
175 #define PROCSROW         7      /* uses 2 rows and 20 cols */
176 #define PROCSCOL         0
177 #define GENSTATROW       7      /* uses 2 rows and 30 cols */
178 #define GENSTATCOL      16
179 #define VMSTATROW        6      /* uses 17 rows and 12 cols */
180 #define VMSTATCOL       48
181 #define GRAPHROW        10      /* uses 3 rows and 51 cols */
182 #define GRAPHCOL         0
183 #define NAMEIROW        14      /* uses 3 rows and 38 cols */
184 #define NAMEICOL         0
185 #define DISKROW         17      /* uses 6 rows and 50 cols (for 9 drives) */
186 #define DISKCOL          0
187
188 #define DRIVESPACE       7      /* max # for space */
189
190 #define MAXDRIVES       DRIVESPACE       /* max # to display */
191
192 int
193 initkre(void)
194 {
195         char *intrnamebuf;
196         size_t bytes;
197         size_t b;
198         size_t i;
199
200         if (namelist[0].n_type == 0) {
201                 if (kvm_nlist(kd, namelist)) {
202                         nlisterr(namelist);
203                         return(0);
204                 }
205                 if (namelist[0].n_type == 0) {
206                         error("No namelist");
207                         return(0);
208                 }
209         }
210
211         if ((num_devices = getnumdevs()) < 0) {
212                 warnx("%s", devstat_errbuf);
213                 return(0);
214         }
215
216         cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
217         last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
218         run.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
219         bzero(cur.dinfo, sizeof(struct devinfo));
220         bzero(last.dinfo, sizeof(struct devinfo));
221         bzero(run.dinfo, sizeof(struct devinfo));
222
223         if (dsinit(MAXDRIVES, &cur, &last, &run) != 1)
224                 return(0);
225
226         if (nintr == 0) {
227                 if (sysctlbyname("hw.intrnames", NULL, &bytes, NULL, 0) == 0) {
228                         intrnamebuf = malloc(bytes);
229                         sysctlbyname("hw.intrnames", intrnamebuf, &bytes, 
230                                         NULL, 0);
231                         for (i = 0; i < bytes; ++i) {
232                             if (intrnamebuf[i] == 0)
233                                 ++nintr;
234                         }
235                         intrname = malloc(nintr * sizeof(char *));
236                         intrloc = malloc(nintr * sizeof(*intrloc));
237                         nintr = 0;
238                         for (b = i = 0; i < bytes; ++i) {
239                             if (intrnamebuf[i] == 0) {
240                                 intrname[nintr] = intrnamebuf + b;
241                                 intrloc[nintr] = 0;
242                                 b = i + 1;
243                                 ++nintr;
244                             }
245                         }
246                 }
247                 nextintsrow = INTSROW + 2;
248                 allocinfo(&s);
249                 allocinfo(&s1);
250                 allocinfo(&s2);
251                 allocinfo(&z);
252         }
253         getinfo(&s2);
254         copyinfo(&s2, &s1);
255         return(1);
256 }
257
258 void
259 fetchkre(void)
260 {
261         time_t now;
262         struct tm *tp;
263         static int d_first = -1;
264
265         if (d_first < 0)
266                 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
267
268         time(&now);
269         tp = localtime(&now);
270         (void) strftime(buf, sizeof(buf),
271                         d_first ? "%e %b %R" : "%b %e %R", tp);
272         getinfo(&s);
273 }
274
275 void
276 labelkre(void)
277 {
278         int i, j;
279
280         clear();
281         mvprintw(STATROW, STATCOL + 4, "users    Load");
282         mvprintw(MEMROW, MEMCOL, "Mem:KB    REAL            VIRTUAL");
283         mvprintw(MEMROW + 1, MEMCOL, "        Tot   Share      Tot    Share");
284         mvprintw(MEMROW + 2, MEMCOL, "Act");
285         mvprintw(MEMROW + 3, MEMCOL, "All");
286
287         mvprintw(MEMROW + 1, MEMCOL + 41, "Free");
288
289         mvprintw(PAGEROW, PAGECOL,     "        VN PAGER  SWAP PAGER ");
290         mvprintw(PAGEROW + 1, PAGECOL, "        in  out     in  out ");
291         mvprintw(PAGEROW + 2, PAGECOL, "count");
292         mvprintw(PAGEROW + 3, PAGECOL, "pages");
293
294         mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
295         mvprintw(INTSROW + 1, INTSCOL + 9, "total");
296
297         mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "cow");
298         mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "wire");
299         mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "act");
300         mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "inact");
301         mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "cache");
302         mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "free");
303         mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "daefr");
304         mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "prcfr");
305         mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "react");
306         mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "pdwake");
307         mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "pdpgs");
308         mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "intrn");
309         mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "buf");
310         mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "dirtybuf");
311
312         mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "desiredvnodes");
313         mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "numvnodes");
314         mvprintw(VMSTATROW + 17, VMSTATCOL + 10, "freevnodes");
315
316         mvprintw(GENSTATROW, GENSTATCOL, "  Csw   Trp   Sys   Int  Sof   Flt");
317
318         mvprintw(GRAPHROW, GRAPHCOL,
319                 "  . %%Sys    . %%Intr   . %%User   . %%Nice   . %%Idle");
320         mvprintw(PROCSROW, PROCSCOL, "  r  p  d  s  w");
321         mvprintw(GRAPHROW + 1, GRAPHCOL,
322                 "|    |    |    |    |    |    |    |    |    |    |");
323
324         mvprintw(NAMEIROW, NAMEICOL, "Path-lookups   hits   %%    Components");
325         mvprintw(DISKROW, DISKCOL, "Disks");
326         mvprintw(DISKROW + 1, DISKCOL, "KB/t");
327         mvprintw(DISKROW + 2, DISKCOL, "tps/r");
328         mvprintw(DISKROW + 3, DISKCOL, "MBs/r");
329         mvprintw(DISKROW + 4, DISKCOL, "tps/w");
330         mvprintw(DISKROW + 5, DISKCOL, "MBs/w");
331         mvprintw(DISKROW + 6, DISKCOL, "%% busy");
332         /*
333          * For now, we don't support a fourth disk statistic.  So there's
334          * no point in providing a label for it.  If someone can think of a
335          * fourth useful disk statistic, there is room to add it.
336          */
337         j = 0;
338         for (i = 0; i < num_devices && j < MAXDRIVES; i++)
339                 if (dev_select[i].selected) {
340                         char tmpstr[80];
341                         sprintf(tmpstr, "%s%d", dev_select[i].device_name,
342                                 dev_select[i].unit_number);
343                         mvprintw(DISKROW, DISKCOL + 5 + 6 * j,
344                                 " %5.5s", tmpstr);
345                         j++;
346                 }
347
348         if (j <= 4) {
349                 /*
350                  * room for extended VM stats
351                  */
352                 mvprintw(VMSTATROW + 11, VMSTATCOL - 6, "zfod");
353                 mvprintw(VMSTATROW + 12, VMSTATCOL - 6, "ofod");
354                 mvprintw(VMSTATROW + 13, VMSTATCOL - 6, "%%slo-z");
355                 mvprintw(VMSTATROW + 14, VMSTATCOL - 6, "tfree");
356                 extended_vm_stats = 1;
357         } else {
358                 extended_vm_stats = 0;
359                 mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "zfod");
360         }
361
362         for (i = 0; i < nintr; i++) {
363                 if (intrloc[i] == 0)
364                         continue;
365                 mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s", intrname[i]);
366         }
367 }
368
369 #define CP_UPDATE(fld)  do {    \
370         uint64_t lt;            \
371         lt=s.fld;               \
372         s.fld-=s1.fld;          \
373         if(state==TIME)         \
374                 s1.fld=lt;      \
375         lt=fld;                 \
376         fld-=old_##fld;         \
377         if(state==TIME)         \
378                 old_##fld=lt;   \
379         etime += s.fld;         \
380 } while(0)
381 #define X(fld)  {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
382 #define Y(fld)  {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
383 #define Z(fld)  {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
384         if(state == TIME) s1.nchstats.fld = t;}
385 #define PUTRATE(fld, l, c, w) \
386         Y(fld); \
387         putint((int)((float)s.fld/etime + 0.5), l, c, w, 'D')
388 #define MAXFAIL 5
389
390 #define CPUSTATES 5
391 static  const char cpuchar[5] = { '=' , '+', '>', '-', ' ' };
392
393 static  const size_t cpuoffsets[] = {
394         offsetof(struct kinfo_cputime, cp_sys),
395         offsetof(struct kinfo_cputime, cp_intr),
396         offsetof(struct kinfo_cputime, cp_user),
397         offsetof(struct kinfo_cputime, cp_nice),
398         offsetof(struct kinfo_cputime, cp_idle)
399 };
400
401 void
402 showkre(void)
403 {
404         float f1, f2;
405         int psiz, inttotal;
406         int i, l, lc;
407         static int failcnt = 0;
408         double total_time;
409
410         etime = 0;
411         CP_UPDATE(cp_time.cp_user);
412         CP_UPDATE(cp_time.cp_nice);
413         CP_UPDATE(cp_time.cp_sys);
414         CP_UPDATE(cp_time.cp_intr);
415         CP_UPDATE(cp_time.cp_idle);
416
417         total_time = etime;
418         if (total_time == 0.0)
419                 total_time = 1.0;
420
421         if (etime < 100000.0) { /* < 100ms ignore this trash */
422                 if (failcnt++ >= MAXFAIL) {
423                         clear();
424                         mvprintw(2, 10, "The alternate system clock has died!");
425                         mvprintw(3, 10, "Reverting to ``pigs'' display.");
426                         move(CMDLINE, 0);
427                         refresh();
428                         failcnt = 0;
429                         sleep(5);
430                         command("pigs");
431                 }
432                 return;
433         }
434         failcnt = 0;
435         etime /= 1000000.0;
436         etime /= ncpu;
437         if (etime == 0)
438                 etime = 1;
439         inttotal = 0;
440         for (i = 0; i < nintr; i++) {
441                 if (s.intrcnt[i] == 0)
442                         continue;
443                 if (intrloc[i] == 0) {
444                         if (nextintsrow == LINES)
445                                 continue;
446                         intrloc[i] = nextintsrow++;
447                         mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s",
448                                 intrname[i]);
449                 }
450                 X(intrcnt);
451                 l = (int)((float)s.intrcnt[i]/etime + 0.5);
452                 inttotal += l;
453                 putint(l, intrloc[i], INTSCOL + 2, 6, 'D');
454         }
455         putint(inttotal, INTSROW + 1, INTSCOL + 2, 6, 'D');
456         Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
457         Z(ncs_longhits); Z(ncs_longmiss); Z(ncs_neghits);
458         s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
459             nchtotal.ncs_miss + nchtotal.ncs_neghits;
460         s.nchpathcount = nchtotal.ncs_longhits + nchtotal.ncs_longmiss;
461         if (state == TIME) {
462                 s1.nchcount = s.nchcount;
463                 s1.nchpathcount = s.nchpathcount;
464         }
465
466         psiz = 0;
467         f2 = 0.0;
468         for (lc = 0; lc < CPUSTATES; lc++) {
469                 uint64_t val = *(uint64_t *)(((uint8_t *)&s.cp_time) +
470                     cpuoffsets[lc]);
471                 f1 = 100.0 * val / total_time;
472                 f2 += f1;
473                 l = (int) ((f2 + 1.0) / 2.0) - psiz;
474                 if (f1 > 99.9)
475                         f1 = 99.9;      /* no room to display 100.0 */
476                 putfloat(f1, GRAPHROW, GRAPHCOL + 10 * lc, 4, 1, 0);
477                 move(GRAPHROW + 2, psiz);
478                 psiz += l;
479                 while (l-- > 0)
480                         addch(cpuchar[lc]);
481         }
482
483         putint(ucount(), STATROW, STATCOL, 3, 'D');
484         putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
485         putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
486         putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
487         mvaddstr(STATROW, STATCOL + 53, buf);
488 #define pgtokb(pg)      ((pg) * vms.v_page_size / 1024)
489         putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 8, 'K');
490         putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 11, 8, 'K');
491         putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 19, 9, 'K');
492         putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 28, 9, 'K');
493         putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 8, 'K');
494         putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 11, 8, 'K');
495         putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 19, 9, 'K');
496         putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 28, 9, 'K');
497         putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 37, 8, 'K');
498         putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 0, 3, 'D');
499         putint(total.t_pw, PROCSROW + 1, PROCSCOL + 3, 3, 'D');
500         putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3, 'D');
501         putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3, 'D');
502         putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3, 'D');
503         if (extended_vm_stats == 0) {
504                 PUTRATE(Vmm.v_zfod, VMSTATROW + 0, VMSTATCOL + 4, 5);
505         }
506         PUTRATE(Vmm.v_cow_faults, VMSTATROW + 1, VMSTATCOL + 3, 6);
507         putint(pgtokb(vms.v_wire_count), VMSTATROW + 2, VMSTATCOL, 9, 'K');
508         putint(pgtokb(vms.v_active_count), VMSTATROW + 3, VMSTATCOL, 9, 'K');
509         putint(pgtokb(vms.v_inactive_count), VMSTATROW + 4, VMSTATCOL, 9, 'K');
510         putint(pgtokb(vms.v_cache_count), VMSTATROW + 5, VMSTATCOL, 9, 'K');
511         putint(pgtokb(vms.v_free_count), VMSTATROW + 6, VMSTATCOL, 9, 'K');
512         PUTRATE(Vmm.v_dfree, VMSTATROW + 7, VMSTATCOL, 9);
513         PUTRATE(Vmm.v_pfree, VMSTATROW + 8, VMSTATCOL, 9);
514         PUTRATE(Vmm.v_reactivated, VMSTATROW + 9, VMSTATCOL, 9);
515         PUTRATE(Vmm.v_pdwakeups, VMSTATROW + 10, VMSTATCOL, 9);
516         PUTRATE(Vmm.v_pdpages, VMSTATROW + 11, VMSTATCOL, 9);
517         PUTRATE(Vmm.v_intrans, VMSTATROW + 12, VMSTATCOL, 9);
518
519         if (extended_vm_stats) {
520             PUTRATE(Vmm.v_zfod, VMSTATROW + 11, VMSTATCOL - 16, 9);
521             PUTRATE(Vmm.v_ozfod, VMSTATROW + 12, VMSTATCOL - 16, 9);
522             putint(
523                 ((s.Vmm.v_ozfod < s.Vmm.v_zfod) ?
524                     s.Vmm.v_ozfod * 100 / s.Vmm.v_zfod : 
525                     0
526                 ),
527                 VMSTATROW + 13, 
528                 VMSTATCOL - 16,
529                 9,
530                 'D'
531             );
532             PUTRATE(Vmm.v_tfree, VMSTATROW + 14, VMSTATCOL - 16, 9);
533         }
534
535         putint(s.bufspace/1024, VMSTATROW + 13, VMSTATCOL, 9, 'K');
536         putint(s.dirtybufspace/1024, VMSTATROW + 14, VMSTATCOL, 9, 'K');
537         putint(s.desiredvnodes, VMSTATROW + 15, VMSTATCOL, 9, 'D');
538         putint(s.numvnodes, VMSTATROW + 16, VMSTATCOL, 9, 'D');
539         putint(s.freevnodes, VMSTATROW + 17, VMSTATCOL, 9, 'D');
540         PUTRATE(Vmm.v_vnodein, PAGEROW + 2, PAGECOL + 5, 5);
541         PUTRATE(Vmm.v_vnodeout, PAGEROW + 2, PAGECOL + 10, 5);
542         PUTRATE(Vmm.v_swapin, PAGEROW + 2, PAGECOL + 17, 5);
543         PUTRATE(Vmm.v_swapout, PAGEROW + 2, PAGECOL + 22, 5);
544         PUTRATE(Vmm.v_vnodepgsin, PAGEROW + 3, PAGECOL + 5, 5);
545         PUTRATE(Vmm.v_vnodepgsout, PAGEROW + 3, PAGECOL + 10, 5);
546         PUTRATE(Vmm.v_swappgsin, PAGEROW + 3, PAGECOL + 17, 5);
547         PUTRATE(Vmm.v_swappgsout, PAGEROW + 3, PAGECOL + 22, 5);
548         PUTRATE(Vmm.v_swtch, GENSTATROW + 1, GENSTATCOL, 5);
549         PUTRATE(Vmm.v_trap, GENSTATROW + 1, GENSTATCOL + 6, 5);
550         PUTRATE(Vmm.v_syscall, GENSTATROW + 1, GENSTATCOL + 12, 5);
551         PUTRATE(Vmm.v_intr, GENSTATROW + 1, GENSTATCOL + 18, 5);
552         PUTRATE(Vmm.v_soft, GENSTATROW + 1, GENSTATCOL + 23, 5);
553         PUTRATE(Vmm.v_vm_faults, GENSTATROW + 1, GENSTATCOL + 29, 5);
554         mvprintw(DISKROW, DISKCOL + 5, "                              ");
555         for (i = 0, lc = 0; i < num_devices && lc < MAXDRIVES; i++)
556                 if (dev_select[i].selected) {
557                         char tmpstr[80];
558                         sprintf(tmpstr, "%s%d", dev_select[i].device_name,
559                                 dev_select[i].unit_number);
560                         mvprintw(DISKROW, DISKCOL + 5 + 6 * lc,
561                                 " %5.5s", tmpstr);
562                         switch(state) {
563                         case TIME:
564                                 dinfo(i, ++lc, &cur, &last);
565                                 break;
566                         case RUN:
567                                 dinfo(i, ++lc, &cur, &run);
568                                 break;
569                         case BOOT:
570                                 dinfo(i, ++lc, &cur, NULL);
571                                 break;
572                         }
573                 }
574 #define nz(x)   ((x) ? (x) : 1)
575         putint(s.nchpathcount, NAMEIROW + 1, NAMEICOL + 3, 9, 'D');
576
577         putint(nchtotal.ncs_longhits, NAMEIROW + 1, NAMEICOL + 12, 7, 'D');
578         putfloat(nchtotal.ncs_longhits * 100.0 / nz(s.nchpathcount),
579            NAMEIROW + 1, NAMEICOL + 19, 4, 0, 0);
580
581         putfloat((double)s.nchcount / nz(s.nchpathcount),
582                 NAMEIROW + 1, NAMEICOL + 27, 5, 2, 1);
583 #undef nz
584 }
585
586 int
587 cmdkre(const char *cmd, char *args)
588 {
589         int retval;
590
591         if (prefix(cmd, "run")) {
592                 retval = 1;
593                 copyinfo(&s2, &s1);
594                 switch (getdevs(&run)) {
595                 case -1:
596                         errx(1, "%s", devstat_errbuf);
597                         break;
598                 case 1:
599                         num_devices = run.dinfo->numdevs;
600                         generation = run.dinfo->generation;
601                         retval = dscmd("refresh", NULL, MAXDRIVES, &cur);
602                         if (retval == 2)
603                                 labelkre();
604                         break;
605                 default:
606                         break;
607                 }
608                 state = RUN;
609                 return (retval);
610         }
611         if (prefix(cmd, "boot")) {
612                 state = BOOT;
613                 copyinfo(&z, &s1);
614                 return (1);
615         }
616         if (prefix(cmd, "time")) {
617                 state = TIME;
618                 return (1);
619         }
620         if (prefix(cmd, "zero")) {
621                 retval = 1;
622                 if (state == RUN) {
623                         getinfo(&s1);
624                         switch (getdevs(&run)) {
625                         case -1:
626                                 errx(1, "%s", devstat_errbuf);
627                                 break;
628                         case 1:
629                                 num_devices = run.dinfo->numdevs;
630                                 generation = run.dinfo->generation;
631                                 retval = dscmd("refresh",NULL, MAXDRIVES, &cur);
632                                 if (retval == 2)
633                                         labelkre();
634                                 break;
635                         default:
636                                 break;
637                         }
638                 }
639                 return (retval);
640         }
641         retval = dscmd(cmd, args, MAXDRIVES, &cur);
642
643         if (retval == 2)
644                 labelkre();
645
646         return(retval);
647 }
648
649 /* calculate number of users on the system */
650 static int
651 ucount(void)
652 {
653         int nusers = 0;
654
655         if (ut < 0)
656                 return (0);
657         while (read(ut, &utmp, sizeof(utmp)))
658                 if (utmp.ut_name[0] != '\0')
659                         nusers++;
660
661         lseek(ut, 0L, L_SET);
662         return (nusers);
663 }
664
665 static void
666 putint(int n, int l, int lc, int w, int type)
667 {
668         char b[128];
669         int xtype;
670
671         move(l, lc);
672         if (n == 0) {
673                 while (w-- > 0)
674                         addch(' ');
675                 return;
676         }
677         snprintf(b, sizeof(b), "%*d", w, n);
678         if (strlen(b) > (size_t)w) {
679                 if (type == 'D') {
680                         n /= 1000;
681                         xtype = 'K';
682                 } else {
683                         n /= 1024;
684                         xtype = 'M';
685                 }
686                 snprintf(b, sizeof(b), "%*d%c", w - 1, n, xtype);
687                 if (strlen(b) > (size_t)w) {
688                         if (type == 'D') {
689                                 n /= 1000;
690                                 xtype = 'M';
691                         } else {
692                                 n /= 1024;
693                                 xtype = 'G';
694                         }
695                         snprintf(b, sizeof(b), "%*d%c", w - 1, n, xtype);
696                         if (strlen(b) > (size_t)w) {
697                                 while (w-- > 0)
698                                         addch('*');
699                                 return;
700                         }
701                 }
702         }
703         addstr(b);
704 }
705
706 static void
707 putfloat(double f, int l, int lc, int w, int d, int nz)
708 {
709         char b[128];
710
711         move(l, lc);
712         if (nz && f == 0.0) {
713                 while (--w >= 0)
714                         addch(' ');
715                 return;
716         }
717         snprintf(b, sizeof(b), "%*.*f", w, d, f);
718         if (strlen(b) > (size_t)w)
719                 snprintf(b, sizeof(b), "%*.0f", w, f);
720         if (strlen(b) > (size_t)w) {
721                 while (--w >= 0)
722                         addch('*');
723                 return;
724         }
725         addstr(b);
726 }
727
728 static void
729 putlongdouble(long double f, int l, int lc, int w, int d, int nz)
730 {
731         char b[128];
732
733         move(l, lc);
734         if (nz && f == 0.0) {
735                 while (--w >= 0)
736                         addch(' ');
737                 return;
738         }
739         sprintf(b, "%*.*Lf", w, d, f);
740         if (strlen(b) > (size_t)w)
741                 sprintf(b, "%*.0Lf", w, f);
742         if (strlen(b) > (size_t)w) {
743                 while (--w >= 0)
744                         addch('*');
745                 return;
746         }
747         addstr(b);
748 }
749
750 static void
751 putlongdoublez(long double f, int l, int lc, int w, int d, int nz)
752 {
753         char b[128];
754
755         if (f == 0.0) {
756                 move(l, lc);
757                 sprintf(b, "%*.*s", w, w, "");
758                 addstr(b);
759         } else {
760                 putlongdouble(f, l, lc, w, d, nz);
761         }
762 }
763
764 static void
765 getinfo(struct Info *ls)
766 {
767         struct devinfo *tmp_dinfo;
768         struct nchstats *nch_tmp;
769         size_t size;
770         size_t vms_size = sizeof(ls->Vms);
771         size_t vmm_size = sizeof(ls->Vmm);
772         size_t nch_size = sizeof(ls->nchstats) * SMP_MAXCPU;
773
774         if (sysctlbyname("vm.vmstats", &ls->Vms, &vms_size, NULL, 0)) {
775                 perror("sysctlbyname: vm.vmstats");
776                 exit(1);
777         }
778         if (sysctlbyname("vm.vmmeter", &ls->Vmm, &vmm_size, NULL, 0)) {
779                 perror("sysctlbyname: vm.vmstats");
780                 exit(1);
781         }
782
783         if (kinfo_get_sched_cputime(&ls->cp_time))
784                 err(1, "kinfo_get_sched_cputime");
785         if (kinfo_get_sched_cputime(&cp_time))
786                 err(1, "kinfo_get_sched_cputime");
787         NREAD(X_BUFFERSPACE, &ls->bufspace, sizeof(ls->bufspace));
788         NREAD(X_DESIREDVNODES, &ls->desiredvnodes, sizeof(ls->desiredvnodes));
789         NREAD(X_NUMVNODES, &ls->numvnodes, LONG);
790         NREAD(X_FREEVNODES, &ls->freevnodes, LONG);
791         NREAD(X_NUMDIRTYBUFFERS, &ls->dirtybufspace, sizeof(ls->dirtybufspace));
792
793         if (nintr) {
794                 size = nintr * sizeof(ls->intrcnt[0]);
795                 sysctlbyname("hw.intrcnt", ls->intrcnt, &size, NULL, 0);
796         }
797         size = sizeof(ls->Total);
798         if (sysctlbyname("vm.vmtotal", &ls->Total, &size, NULL, 0) < 0) {
799                 error("Can't get kernel info: %s\n", strerror(errno));
800                 bzero(&ls->Total, sizeof(ls->Total));
801         }
802         
803         if ((nch_tmp = malloc(nch_size)) == NULL) {
804                 perror("malloc");
805                 exit(1);
806         } else {
807                 if (sysctlbyname("vfs.cache.nchstats", nch_tmp, &nch_size, NULL, 0)) {
808                         perror("sysctlbyname vfs.cache.nchstats");
809                         free(nch_tmp);
810                         exit(1);
811                 } else {
812                         if ((nch_tmp = realloc(nch_tmp, nch_size)) == NULL) {
813                                 perror("realloc");
814                                 exit(1);
815                         }
816                 }
817         }
818
819         if (kinfo_get_cpus(&ncpu))
820                 err(1, "kinfo_get_cpus");
821         kvm_nch_cpuagg(nch_tmp, &ls->nchstats, ncpu);
822         free(nch_tmp);
823
824         tmp_dinfo = last.dinfo;
825         last.dinfo = cur.dinfo;
826         cur.dinfo = tmp_dinfo;
827
828         last.busy_time = cur.busy_time;
829         switch (getdevs(&cur)) {
830         case -1:
831                 errx(1, "%s", devstat_errbuf);
832                 break;
833         case 1:
834                 num_devices = cur.dinfo->numdevs;
835                 generation = cur.dinfo->generation;
836                 cmdkre("refresh", NULL);
837                 break;
838         default:
839                 break;
840         }
841 }
842
843 static void
844 allocinfo(struct Info *ls)
845 {
846         ls->intrcnt = (long *) calloc(nintr, sizeof(long));
847         if (ls->intrcnt == NULL)
848                 errx(2, "out of memory");
849 }
850
851 static void
852 copyinfo(struct Info *from, struct Info *to)
853 {
854         long *intrcnt;
855
856         /*
857          * time, wds, seek, and xfer are malloc'd so we have to
858          * save the pointers before the structure copy and then
859          * copy by hand.
860          */
861         intrcnt = to->intrcnt;
862         *to = *from;
863
864         bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
865 }
866
867 static void
868 dinfo(int dn, int lc, struct statinfo *now, struct statinfo *then)
869 {
870         long double kb_per_transfer;
871         long double transfers_per_secondr;
872         long double transfers_per_secondw;
873         long double mb_per_secondr;
874         long double mb_per_secondw;
875         long double elapsed_time, device_busy;
876         int di;
877
878         di = dev_select[dn].position;
879
880         elapsed_time = compute_etime(now->busy_time, then ?
881                                      then->busy_time :
882                                      now->dinfo->devices[di].dev_creation_time);
883
884         device_busy =  compute_etime(now->dinfo->devices[di].busy_time, then ?
885                                      then->dinfo->devices[di].busy_time :
886                                      now->dinfo->devices[di].dev_creation_time);
887
888         if (compute_stats(
889                           &now->dinfo->devices[di],
890                           (then ? &then->dinfo->devices[di] : NULL),
891                           elapsed_time,
892                           NULL, NULL, NULL,
893                           &kb_per_transfer,
894                           NULL,
895                           NULL,
896                           NULL, NULL) != 0)
897                 errx(1, "%s", devstat_errbuf);
898
899         if (compute_stats_read(
900                           &now->dinfo->devices[di],
901                           (then ? &then->dinfo->devices[di] : NULL),
902                           elapsed_time,
903                           NULL, NULL, NULL,
904                           NULL,
905                           &transfers_per_secondr,
906                           &mb_per_secondr,
907                           NULL, NULL) != 0)
908                 errx(1, "%s", devstat_errbuf);
909
910         if (compute_stats_write(
911                           &now->dinfo->devices[di],
912                           (then ? &then->dinfo->devices[di] : NULL),
913                           elapsed_time,
914                           NULL, NULL, NULL,
915                           NULL,
916                           &transfers_per_secondw,
917                           &mb_per_secondw,
918                           NULL, NULL) != 0)
919                 errx(1, "%s", devstat_errbuf);
920
921         if ((device_busy == 0) &&
922             (transfers_per_secondr > 5 || transfers_per_secondw > 5)) {
923                 /* the device has been 100% busy, fake it because
924                  * as long as the device is 100% busy the busy_time
925                  * field in the devstat struct is not updated */
926                 device_busy = elapsed_time;
927         }
928         if (device_busy > elapsed_time) {
929                 /* this normally happens after one or more periods
930                  * where the device has been 100% busy, correct it */
931                 device_busy = elapsed_time;
932         }
933
934         lc = DISKCOL + lc * 6;
935         putlongdoublez(kb_per_transfer, DISKROW + 1, lc, 5, 2, 0);
936         putlongdoublez(transfers_per_secondr, DISKROW + 2, lc, 5, 0, 0);
937         putlongdoublez(mb_per_secondr, DISKROW + 3, lc, 5, 2, 0);
938         putlongdoublez(transfers_per_secondw, DISKROW + 4, lc, 5, 0, 0);
939         putlongdoublez(mb_per_secondw, DISKROW + 5, lc, 5, 2, 0);
940         putlongdouble(device_busy * 100 / elapsed_time,
941                                       DISKROW + 6, lc, 5, 0, 0);
942 }