]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/systat/vmstat.c
Add UPDATING entries and bump version.
[FreeBSD/FreeBSD.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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31
32 __FBSDID("$FreeBSD$");
33
34 #ifdef lint
35 static const char sccsid[] = "@(#)vmstat.c      8.2 (Berkeley) 1/12/94";
36 #endif
37
38 /*
39  * Cursed vmstat -- from Robert Elz.
40  */
41
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 #include <sys/time.h>
45 #include <sys/proc.h>
46 #include <sys/uio.h>
47 #include <sys/namei.h>
48 #include <sys/resource.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 <langinfo.h>
58 #include <nlist.h>
59 #include <paths.h>
60 #include <signal.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <time.h>
64 #include <unistd.h>
65 #include <utmpx.h>
66 #include <devstat.h>
67 #include "systat.h"
68 #include "extern.h"
69 #include "devs.h"
70
71 static struct Info {
72         long    time[CPUSTATES];
73         u_int v_swtch;          /* context switches */
74         u_int v_trap;           /* calls to trap */
75         u_int v_syscall;        /* calls to syscall() */
76         u_int v_intr;           /* device interrupts */
77         u_int v_soft;           /* software interrupts */
78         /*
79          * Virtual memory activity.
80          */
81         u_int v_vm_faults;      /* number of address memory faults */
82         u_int v_io_faults;      /* page faults requiring I/O */
83         u_int v_cow_faults;     /* number of copy-on-writes */
84         u_int v_zfod;           /* pages zero filled on demand */
85         u_int v_ozfod;          /* optimized zero fill pages */
86         u_int v_swapin;         /* swap pager pageins */
87         u_int v_swapout;        /* swap pager pageouts */
88         u_int v_swappgsin;      /* swap pager pages paged in */
89         u_int v_swappgsout;     /* swap pager pages paged out */
90         u_int v_vnodein;        /* vnode pager pageins */
91         u_int v_vnodeout;       /* vnode pager pageouts */
92         u_int v_vnodepgsin;     /* vnode_pager pages paged in */
93         u_int v_vnodepgsout;    /* vnode pager pages paged out */
94         u_int v_intrans;        /* intransit blocking page faults */
95         u_int v_reactivated;    /* number of pages reactivated by pagedaemon */
96         u_int v_pdwakeups;      /* number of times daemon has awaken from sleep */
97         u_int v_pdpages;        /* number of pages analyzed by daemon */
98
99         u_int v_dfree;          /* pages freed by daemon */
100         u_int v_pfree;          /* pages freed by exiting processes */
101         u_int v_tfree;          /* total pages freed */
102         /*
103          * Distribution of page usages.
104          */
105         u_int v_page_size;      /* page size in bytes */
106         u_int v_free_count;     /* number of pages free */
107         u_int v_wire_count;     /* number of pages wired down */
108         u_int v_active_count;   /* number of pages active */
109         u_int v_inactive_count; /* number of pages inactive */
110         u_int v_laundry_count;  /* number of pages in laundry queue */
111         u_long v_kmem_map_size; /* Current kmem allocation size */
112         struct  vmtotal Total;
113         struct  nchstats nchstats;
114         long    nchcount;
115         long    *intrcnt;
116         long    bufspace;
117         int     desiredvnodes;
118         long    numvnodes;
119         long    freevnodes;
120         int     numdirtybuffers;
121 } s, s1, s2, z;
122 static u_long kmem_size;
123 static u_int v_page_count;
124
125 struct statinfo cur, last, run;
126
127 #define total s.Total
128 #define nchtotal s.nchstats
129 #define oldnchtotal s1.nchstats
130
131 static  enum state { BOOT, TIME, RUN } state = TIME;
132
133 static void allocinfo(struct Info *);
134 static void copyinfo(struct Info *, struct Info *);
135 static float cputime(int);
136 static void dinfo(int, int, struct statinfo *, struct statinfo *);
137 static void getinfo(struct Info *);
138 static void putint(int, int, int, int);
139 static void putfloat(double, int, int, int, int, int);
140 static void putlongdouble(long double, int, int, int, int, int);
141 static int ucount(void);
142
143 static  int ncpu;
144 static  char buf[26];
145 static  time_t t;
146 static  double etime;
147 static  int nintr;
148 static  long *intrloc;
149 static  char **intrname;
150 static  int nextintsrow;
151
152 WINDOW *
153 openkre(void)
154 {
155
156         return (stdscr);
157 }
158
159 void
160 closekre(WINDOW *w)
161 {
162
163         if (w == NULL)
164                 return;
165         wclear(w);
166         wrefresh(w);
167 }
168
169 /*
170  * These constants define where the major pieces are laid out
171  */
172 #define STATROW          0      /* uses 1 row and 67 cols */
173 #define STATCOL          0
174 #define MEMROW           2      /* uses 4 rows and 45 cols */
175 #define MEMCOL           0
176 #define PAGEROW          2      /* uses 4 rows and 30 cols */
177 #define PAGECOL         47
178 #define INTSROW          6      /* uses all rows to bottom and 16 cols */
179 #define INTSCOL         64
180 #define PROCSROW         6      /* uses 3 rows and 19 cols */
181 #define PROCSCOL         0
182 #define GENSTATROW       7      /* uses 2 rows and 29 cols */
183 #define GENSTATCOL      21
184 #define VMSTATROW        7      /* uses 17 rows and 12-14 cols */
185 #define VMSTATCOL       49      /* actually 50-51 for some fields */
186 #define GRAPHROW        10      /* uses 3 rows and 49-51 cols */
187 #define GRAPHCOL         0
188 #define VNSTATROW       13      /* uses 4 rows and 13 columns */
189 #define VNSTATCOL       35
190 #define NAMEIROW        14      /* uses 3 rows and 32 cols */
191 #define NAMEICOL         0
192 #define DISKROW         18      /* uses 5 rows and 47 cols (for 7 drives) */
193 #define DISKCOL          0
194
195 #define DRIVESPACE       7      /* max # for space */
196
197 #define MAXDRIVES       DRIVESPACE       /* max # to display */
198
199 int
200 initkre(void)
201 {
202         char *cp, *cp1, *cp2, *intrnamebuf, *nextcp;
203         int i;
204         size_t sz;
205
206         if ((num_devices = devstat_getnumdevs(NULL)) < 0) {
207                 warnx("%s", devstat_errbuf);
208                 return(0);
209         }
210
211         cur.dinfo = calloc(1, sizeof(struct devinfo));
212         last.dinfo = calloc(1, sizeof(struct devinfo));
213         run.dinfo = calloc(1, sizeof(struct devinfo));
214
215         if (dsinit(MAXDRIVES, &cur, &last, &run) != 1)
216                 return(0);
217
218         if (nintr == 0) {
219                 if (sysctlbyname("hw.intrcnt", NULL, &sz, NULL, 0) == -1) {
220                         error("sysctl(hw.intrcnt...) failed: %s",
221                               strerror(errno));
222                         return (0);
223                 }
224                 nintr = sz / sizeof(u_long);
225                 intrloc = calloc(nintr, sizeof (long));
226                 intrname = calloc(nintr, sizeof (char *));
227                 intrnamebuf = sysctl_dynread("hw.intrnames", NULL);
228                 if (intrnamebuf == NULL || intrname == NULL ||
229                     intrloc == NULL) {
230                         error("Out of memory");
231                         if (intrnamebuf)
232                                 free(intrnamebuf);
233                         if (intrname)
234                                 free(intrname);
235                         if (intrloc)
236                                 free(intrloc);
237                         nintr = 0;
238                         return(0);
239                 }
240                 for (cp = intrnamebuf, i = 0; i < nintr; i++) {
241                         nextcp = cp + strlen(cp) + 1;
242
243                         /* Discard trailing spaces. */
244                         for (cp1 = nextcp - 1; cp1 > cp && *(cp1 - 1) == ' '; )
245                                 *--cp1 = '\0';
246
247                         /* Convert "irqN: name" to "name irqN". */
248                         if (strncmp(cp, "irq", 3) == 0) {
249                                 cp1 = cp + 3;
250                                 while (isdigit((u_char)*cp1))
251                                         cp1++;
252                                 if (cp1 != cp && *cp1 == ':' &&
253                                     *(cp1 + 1) == ' ') {
254                                         sz = strlen(cp);
255                                         *cp1 = '\0';
256                                         cp1 = cp1 + 2;
257                                         cp2 = strdup(cp);
258                                         bcopy(cp1, cp, sz - (cp1 - cp) + 1);
259                                         if (sz <= 10 + 4) {
260                                                 strcat(cp, " ");
261                                                 strcat(cp, cp2 + 3);
262                                         }
263                                         free(cp2);
264                                 }
265                         }
266
267                         /*
268                          * Convert "name irqN" to "name N" if the former is
269                          * longer than the field width.
270                          */
271                         if ((cp1 = strstr(cp, "irq")) != NULL &&
272                             strlen(cp) > 10)
273                                 bcopy(cp1 + 3, cp1, strlen(cp1 + 3) + 1);
274
275                         intrname[i] = cp;
276                         cp = nextcp;
277                 }
278                 nextintsrow = INTSROW + 2;
279                 allocinfo(&s);
280                 allocinfo(&s1);
281                 allocinfo(&s2);
282                 allocinfo(&z);
283         }
284         GETSYSCTL("vm.kmem_size", kmem_size);
285         GETSYSCTL("vm.stats.vm.v_page_count", v_page_count);
286         getinfo(&s2);
287         copyinfo(&s2, &s1);
288         return(1);
289 }
290
291 void
292 fetchkre(void)
293 {
294         time_t now;
295         struct tm *tp;
296         static int d_first = -1;
297
298         if (d_first < 0)
299                 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
300
301         time(&now);
302         tp = localtime(&now);
303         (void) strftime(buf, sizeof(buf),
304                         d_first ? "%e %b %R" : "%b %e %R", tp);
305         getinfo(&s);
306 }
307
308 void
309 labelkre(void)
310 {
311         int i, j;
312
313         clear();
314         mvprintw(STATROW, STATCOL + 6, "users    Load");
315         mvprintw(STATROW + 1, STATCOL + 3, "Mem usage:    %%Phy   %%Kmem");
316         mvprintw(MEMROW, MEMCOL, "Mem: KB    REAL            VIRTUAL");
317         mvprintw(MEMROW + 1, MEMCOL, "        Tot   Share      Tot    Share");
318         mvprintw(MEMROW + 2, MEMCOL, "Act");
319         mvprintw(MEMROW + 3, MEMCOL, "All");
320
321         mvprintw(MEMROW + 1, MEMCOL + 41, "Free");
322
323         mvprintw(PAGEROW, PAGECOL,     "         VN PAGER   SWAP PAGER");
324         mvprintw(PAGEROW + 1, PAGECOL, "         in   out     in   out");
325         mvprintw(PAGEROW + 2, PAGECOL, "count");
326         mvprintw(PAGEROW + 3, PAGECOL, "pages");
327
328         mvprintw(INTSROW, INTSCOL + 1, "Interrupts");
329         mvprintw(INTSROW + 1, INTSCOL + 6, "total");
330
331         mvprintw(VMSTATROW, VMSTATCOL + 9, "ioflt");
332         mvprintw(VMSTATROW + 1, VMSTATCOL + 9, "cow");
333         mvprintw(VMSTATROW + 2, VMSTATCOL + 9, "zfod");
334         mvprintw(VMSTATROW + 3, VMSTATCOL + 9, "ozfod");
335         mvprintw(VMSTATROW + 4, VMSTATCOL + 9 - 1, "%%ozfod");
336         mvprintw(VMSTATROW + 5, VMSTATCOL + 9, "daefr");
337         mvprintw(VMSTATROW + 6, VMSTATCOL + 9, "prcfr");
338         mvprintw(VMSTATROW + 7, VMSTATCOL + 9, "totfr");
339         mvprintw(VMSTATROW + 8, VMSTATCOL + 9, "react");
340         mvprintw(VMSTATROW + 9, VMSTATCOL + 9, "pdwak");
341         mvprintw(VMSTATROW + 10, VMSTATCOL + 9, "pdpgs");
342         mvprintw(VMSTATROW + 11, VMSTATCOL + 9, "intrn");
343         mvprintw(VMSTATROW + 12, VMSTATCOL + 9, "wire");
344         mvprintw(VMSTATROW + 13, VMSTATCOL + 9, "act");
345         mvprintw(VMSTATROW + 14, VMSTATCOL + 9, "inact");
346         mvprintw(VMSTATROW + 15, VMSTATCOL + 9, "laund");
347         mvprintw(VMSTATROW + 16, VMSTATCOL + 9, "free");
348         if (LINES - 1 > VMSTATROW + 17)
349                 mvprintw(VMSTATROW + 17, VMSTATCOL + 9, "buf");
350
351         mvprintw(GENSTATROW, GENSTATCOL, " Csw  Trp  Sys  Int  Sof  Flt");
352
353         mvprintw(GRAPHROW, GRAPHCOL,
354                 "  . %%Sys    . %%Intr   . %%User   . %%Nice   . %%Idle");
355         mvprintw(PROCSROW, PROCSCOL, "Proc:");
356         mvprintw(PROCSROW + 1, PROCSCOL, "  r   p   d   s   w");
357         mvprintw(GRAPHROW + 1, GRAPHCOL,
358                 "|    |    |    |    |    |    |    |    |    |    |");
359
360         mvprintw(VNSTATROW, VNSTATCOL + 8, "dtbuf");
361         mvprintw(VNSTATROW + 1, VNSTATCOL + 8, "desvn");
362         mvprintw(VNSTATROW + 2, VNSTATCOL + 8, "numvn");
363         mvprintw(VNSTATROW + 3, VNSTATCOL + 8, "frevn");
364
365         mvprintw(NAMEIROW, NAMEICOL, "Namei     Name-cache   Dir-cache");
366         mvprintw(NAMEIROW + 1, NAMEICOL,
367                 "   Calls    hits   %%    hits   %%");
368         mvprintw(DISKROW, DISKCOL, "Disks");
369         mvprintw(DISKROW + 1, DISKCOL, "KB/t");
370         mvprintw(DISKROW + 2, DISKCOL, "tps");
371         mvprintw(DISKROW + 3, DISKCOL, "MB/s");
372         mvprintw(DISKROW + 4, DISKCOL, "%%busy");
373         /*
374          * For now, we don't support a fourth disk statistic.  So there's
375          * no point in providing a label for it.  If someone can think of a
376          * fourth useful disk statistic, there is room to add it.
377          */
378         /* mvprintw(DISKROW + 4, DISKCOL, " msps"); */
379         j = 0;
380         for (i = 0; i < num_devices && j < MAXDRIVES; i++)
381                 if (dev_select[i].selected) {
382                         char tmpstr[80];
383                         sprintf(tmpstr, "%s%d", dev_select[i].device_name,
384                                 dev_select[i].unit_number);
385                         mvprintw(DISKROW, DISKCOL + 5 + 6 * j,
386                                 " %5.5s", tmpstr);
387                         j++;
388                 }
389
390         for (i = 0; i < nintr; i++) {
391                 if (intrloc[i] == 0)
392                         continue;
393                 mvprintw(intrloc[i], INTSCOL + 6, "%-10.10s", intrname[i]);
394         }
395 }
396
397 #define X(fld)  {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
398 #define Q(fld)  {t=cur.fld[i]; cur.fld[i]-=last.fld[i]; if(state==TIME) last.fld[i]=t;}
399 #define Y(fld)  {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
400 #define Z(fld)  {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
401         if(state == TIME) s1.nchstats.fld = t;}
402 #define PUTRATE(fld, l, c, w) \
403 do { \
404         Y(fld); \
405         putint((int)((float)s.fld/etime + 0.5), l, c, w); \
406 } while (0)
407 #define MAXFAIL 5
408
409 static  char cpuchar[CPUSTATES] = { '=' , '+', '>', '-', ' ' };
410 static  char cpuorder[CPUSTATES] = { CP_SYS, CP_INTR, CP_USER, CP_NICE,
411                                      CP_IDLE };
412
413 void
414 showkre(void)
415 {
416         float f1, f2;
417         int psiz, inttotal;
418         int i, l, lc;
419         static int failcnt = 0;
420
421         etime = 0;
422         for(i = 0; i < CPUSTATES; i++) {
423                 X(time);
424                 Q(cp_time);
425                 etime += s.time[i];
426         }
427         if (etime < 5.0) {      /* < 5 ticks - ignore this trash */
428                 if (failcnt++ >= MAXFAIL) {
429                         clear();
430                         mvprintw(2, 10, "The alternate system clock has died!");
431                         mvprintw(3, 10, "Reverting to ``pigs'' display.");
432                         move(CMDLINE, 0);
433                         refresh();
434                         failcnt = 0;
435                         sleep(5);
436                         command("pigs");
437                 }
438                 return;
439         }
440         failcnt = 0;
441         etime /= hertz;
442         etime /= ncpu;
443         inttotal = 0;
444         for (i = 0; i < nintr; i++) {
445                 if (s.intrcnt[i] == 0)
446                         continue;
447                 X(intrcnt);
448                 l = (int)((float)s.intrcnt[i]/etime + 0.5);
449                 inttotal += l;
450                 if (intrloc[i] == 0) {
451                         if (nextintsrow == LINES)
452                                 continue;
453                         intrloc[i] = nextintsrow++;
454                         mvprintw(intrloc[i], INTSCOL + 6, "%-10.10s",
455                                 intrname[i]);
456                 }
457                 putint(l, intrloc[i], INTSCOL, 5);
458         }
459         putint(inttotal, INTSROW + 1, INTSCOL, 5);
460         Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
461         Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes); Z(ncs_neghits);
462         s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
463             nchtotal.ncs_miss + nchtotal.ncs_long + nchtotal.ncs_neghits;
464         if (state == TIME)
465                 s1.nchcount = s.nchcount;
466
467         psiz = 0;
468         f2 = 0.0;
469         for (lc = 0; lc < CPUSTATES; lc++) {
470                 i = cpuorder[lc];
471                 f1 = cputime(i);
472                 f2 += f1;
473                 l = (int) ((f2 + 1.0) / 2.0) - psiz;
474                 putfloat(f1, GRAPHROW, GRAPHCOL + 10 * lc, 4, 1, 0);
475                 move(GRAPHROW + 2, psiz);
476                 psiz += l;
477                 while (l-- > 0)
478                         addch(cpuchar[lc]);
479         }
480
481         putint(ucount(), STATROW, STATCOL, 5);
482         putfloat(avenrun[0], STATROW, STATCOL + 20, 5, 2, 0);
483         putfloat(avenrun[1], STATROW, STATCOL + 26, 5, 2, 0);
484         putfloat(avenrun[2], STATROW, STATCOL + 32, 5, 2, 0);
485         mvaddstr(STATROW, STATCOL + 55, buf);
486 #define pgtokb(pg)      ((pg) * (s.v_page_size / 1024))
487         putfloat(100.0 * (v_page_count - total.t_free) / v_page_count,
488            STATROW + 1, STATCOL + 15, 2, 0, 1);
489         putfloat(100.0 * s.v_kmem_map_size / kmem_size,
490            STATROW + 1, STATCOL + 22, 2, 0, 1);
491
492         putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 4, 7);
493         putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 12, 7);
494         putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 20, 8);
495         putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 29, 8);
496         putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 4, 7);
497         putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 12, 7);
498         putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 20, 8);
499         putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 29, 8);
500         putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 38, 7);
501         putint(total.t_rq - 1, PROCSROW + 2, PROCSCOL, 3);
502         putint(total.t_pw, PROCSROW + 2, PROCSCOL + 4, 3);
503         putint(total.t_dw, PROCSROW + 2, PROCSCOL + 8, 3);
504         putint(total.t_sl, PROCSROW + 2, PROCSCOL + 12, 3);
505         putint(total.t_sw, PROCSROW + 2, PROCSCOL + 16, 3);
506         PUTRATE(v_io_faults, VMSTATROW, VMSTATCOL + 2, 8 - 2);
507         PUTRATE(v_cow_faults, VMSTATROW + 1, VMSTATCOL + 2, 8 - 2);
508         PUTRATE(v_zfod, VMSTATROW + 2, VMSTATCOL + 2, 8 - 2);
509         PUTRATE(v_ozfod, VMSTATROW + 3, VMSTATCOL, 8);
510         putint(s.v_zfod != 0 ? (int)(s.v_ozfod * 100.0 / s.v_zfod) : 0,
511             VMSTATROW + 4, VMSTATCOL + 1, 8 - 1);
512         PUTRATE(v_dfree, VMSTATROW + 5, VMSTATCOL + 2, 8 - 2);
513         PUTRATE(v_pfree, VMSTATROW + 6, VMSTATCOL + 2, 8 - 2);
514         PUTRATE(v_tfree, VMSTATROW + 7, VMSTATCOL, 8);
515         PUTRATE(v_reactivated, VMSTATROW + 8, VMSTATCOL, 8);
516         PUTRATE(v_pdwakeups, VMSTATROW + 9, VMSTATCOL, 8);
517         PUTRATE(v_pdpages, VMSTATROW + 10, VMSTATCOL, 8);
518         PUTRATE(v_intrans, VMSTATROW + 11, VMSTATCOL, 8);
519         putint(pgtokb(s.v_wire_count), VMSTATROW + 12, VMSTATCOL, 8);
520         putint(pgtokb(s.v_active_count), VMSTATROW + 13, VMSTATCOL, 8);
521         putint(pgtokb(s.v_inactive_count), VMSTATROW + 14, VMSTATCOL, 8);
522         putint(pgtokb(s.v_laundry_count), VMSTATROW + 15, VMSTATCOL, 8);
523         putint(pgtokb(s.v_free_count), VMSTATROW + 16, VMSTATCOL, 8);
524         if (LINES - 1 > VMSTATROW + 17)
525                 putint(s.bufspace / 1024, VMSTATROW + 17, VMSTATCOL, 8);
526         PUTRATE(v_vnodein, PAGEROW + 2, PAGECOL + 6, 5);
527         PUTRATE(v_vnodeout, PAGEROW + 2, PAGECOL + 12, 5);
528         PUTRATE(v_swapin, PAGEROW + 2, PAGECOL + 19, 5);
529         PUTRATE(v_swapout, PAGEROW + 2, PAGECOL + 25, 5);
530         PUTRATE(v_vnodepgsin, PAGEROW + 3, PAGECOL + 6, 5);
531         PUTRATE(v_vnodepgsout, PAGEROW + 3, PAGECOL + 12, 5);
532         PUTRATE(v_swappgsin, PAGEROW + 3, PAGECOL + 19, 5);
533         PUTRATE(v_swappgsout, PAGEROW + 3, PAGECOL + 25, 5);
534         PUTRATE(v_swtch, GENSTATROW + 1, GENSTATCOL, 4);
535         PUTRATE(v_trap, GENSTATROW + 1, GENSTATCOL + 5, 4);
536         PUTRATE(v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 4);
537         PUTRATE(v_intr, GENSTATROW + 1, GENSTATCOL + 15, 4);
538         PUTRATE(v_soft, GENSTATROW + 1, GENSTATCOL + 20, 4);
539         PUTRATE(v_vm_faults, GENSTATROW + 1, GENSTATCOL + 25, 4);
540         for (i = 0, lc = 0; i < num_devices && lc < MAXDRIVES; i++)
541                 if (dev_select[i].selected) {
542                         switch(state) {
543                         case TIME:
544                                 dinfo(i, ++lc, &cur, &last);
545                                 break;
546                         case RUN:
547                                 dinfo(i, ++lc, &cur, &run);
548                                 break;
549                         case BOOT:
550                                 dinfo(i, ++lc, &cur, NULL);
551                                 break;
552                         }
553                 }
554         putint(s.numdirtybuffers, VNSTATROW, VNSTATCOL, 7);
555         putint(s.desiredvnodes, VNSTATROW + 1, VNSTATCOL, 7);
556         putint(s.numvnodes, VNSTATROW + 2, VNSTATCOL, 7);
557         putint(s.freevnodes, VNSTATROW + 3, VNSTATCOL, 7);
558         putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 8);
559         putint((nchtotal.ncs_goodhits + nchtotal.ncs_neghits),
560            NAMEIROW + 2, NAMEICOL + 9, 7);
561 #define nz(x)   ((x) ? (x) : 1)
562         putfloat((nchtotal.ncs_goodhits+nchtotal.ncs_neghits) *
563            100.0 / nz(s.nchcount),
564            NAMEIROW + 2, NAMEICOL + 17, 3, 0, 1);
565         putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 21, 7);
566         putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
567            NAMEIROW + 2, NAMEICOL + 29, 3, 0, 1);
568 #undef nz
569 }
570
571 int
572 cmdkre(const char *cmd, const char *args)
573 {
574         int retval;
575
576         if (prefix(cmd, "run")) {
577                 retval = 1;
578                 copyinfo(&s2, &s1);
579                 switch (devstat_getdevs(NULL, &run)) {
580                 case -1:
581                         errx(1, "%s", devstat_errbuf);
582                         break;
583                 case 1:
584                         num_devices = run.dinfo->numdevs;
585                         generation = run.dinfo->generation;
586                         retval = dscmd("refresh", NULL, MAXDRIVES, &cur);
587                         if (retval == 2)
588                                 labelkre();
589                         break;
590                 default:
591                         break;
592                 }
593                 state = RUN;
594                 return (retval);
595         }
596         if (prefix(cmd, "boot")) {
597                 state = BOOT;
598                 copyinfo(&z, &s1);
599                 return (1);
600         }
601         if (prefix(cmd, "time")) {
602                 state = TIME;
603                 return (1);
604         }
605         if (prefix(cmd, "zero")) {
606                 retval = 1;
607                 if (state == RUN) {
608                         getinfo(&s1);
609                         switch (devstat_getdevs(NULL, &run)) {
610                         case -1:
611                                 errx(1, "%s", devstat_errbuf);
612                                 break;
613                         case 1:
614                                 num_devices = run.dinfo->numdevs;
615                                 generation = run.dinfo->generation;
616                                 retval = dscmd("refresh",NULL, MAXDRIVES, &cur);
617                                 if (retval == 2)
618                                         labelkre();
619                                 break;
620                         default:
621                                 break;
622                         }
623                 }
624                 return (retval);
625         }
626         retval = dscmd(cmd, args, MAXDRIVES, &cur);
627
628         if (retval == 2)
629                 labelkre();
630
631         return(retval);
632 }
633
634 /* calculate number of users on the system */
635 static int
636 ucount(void)
637 {
638         int nusers = 0;
639         struct utmpx *ut;
640
641         setutxent();
642         while ((ut = getutxent()) != NULL)
643                 if (ut->ut_type == USER_PROCESS)
644                         nusers++;
645         endutxent();
646
647         return (nusers);
648 }
649
650 static float
651 cputime(int indx)
652 {
653         double lt;
654         int i;
655
656         lt = 0;
657         for (i = 0; i < CPUSTATES; i++)
658                 lt += s.time[i];
659         if (lt == 0.0)
660                 lt = 1.0;
661         return (s.time[indx] * 100.0 / lt);
662 }
663
664 static void
665 putint(int n, int l, int lc, int w)
666 {
667         int snr;
668         char b[128];
669
670         move(l, lc);
671 #ifdef DEBUG
672                 while (w-- > 0)
673                         addch('*');
674                 return;
675 #endif
676         if (n == 0) {
677                 while (w-- > 0)
678                         addch(' ');
679                 return;
680         }
681         snr = snprintf(b, sizeof(b), "%*d", w, n);
682         if (snr != w)
683                 snr = snprintf(b, sizeof(b), "%*dk", w - 1, n / 1000);
684         if (snr != w)
685                 snr = snprintf(b, sizeof(b), "%*dM", w - 1, n / 1000000);
686         if (snr != w) {
687                 while (w-- > 0)
688                         addch('*');
689                 return;
690         }
691         addstr(b);
692 }
693
694 static void
695 putfloat(double f, int l, int lc, int w, int d, int nz)
696 {
697         int snr;
698         char b[128];
699
700         move(l, lc);
701 #ifdef DEBUG
702                 while (--w >= 0)
703                         addch('*');
704                 return;
705 #endif
706         if (nz && f == 0.0) {
707                 while (--w >= 0)
708                         addch(' ');
709                 return;
710         }
711         snr = snprintf(b, sizeof(b), "%*.*f", w, d, f);
712         if (snr != w)
713                 snr = snprintf(b, sizeof(b), "%*.0f", w, f);
714         if (snr != w)
715                 snr = snprintf(b, sizeof(b), "%*.0fk", w - 1, f / 1000);
716         if (snr != w)
717                 snr = snprintf(b, sizeof(b), "%*.0fM", w - 1, f / 1000000);
718         if (snr != w) {
719                 while (--w >= 0)
720                         addch('*');
721                 return;
722         }
723         addstr(b);
724 }
725
726 static void
727 putlongdouble(long double f, int l, int lc, int w, int d, int nz)
728 {
729         int snr;
730         char b[128];
731
732         move(l, lc);
733 #ifdef DEBUG
734                 while (--w >= 0)
735                         addch('*');
736                 return;
737 #endif
738         if (nz && f == 0.0) {
739                 while (--w >= 0)
740                         addch(' ');
741                 return;
742         }
743         snr = snprintf(b, sizeof(b), "%*.*Lf", w, d, f);
744         if (snr != w)
745                 snr = snprintf(b, sizeof(b), "%*.0Lf", w, f);
746         if (snr != w)
747                 snr = snprintf(b, sizeof(b), "%*.0Lfk", w - 1, f / 1000);
748         if (snr != w)
749                 snr = snprintf(b, sizeof(b), "%*.0LfM", w - 1, f / 1000000);
750         if (snr != w) {
751                 while (--w >= 0)
752                         addch('*');
753                 return;
754         }
755         addstr(b);
756 }
757
758 static void
759 getinfo(struct Info *ls)
760 {
761         struct devinfo *tmp_dinfo;
762         size_t size;
763         int mib[2];
764
765         GETSYSCTL("kern.cp_time", ls->time);
766         GETSYSCTL("kern.cp_time", cur.cp_time);
767         GETSYSCTL("vm.stats.sys.v_swtch", ls->v_swtch);
768         GETSYSCTL("vm.stats.sys.v_trap", ls->v_trap);
769         GETSYSCTL("vm.stats.sys.v_syscall", ls->v_syscall);
770         GETSYSCTL("vm.stats.sys.v_intr", ls->v_intr);
771         GETSYSCTL("vm.stats.sys.v_soft", ls->v_soft);
772         GETSYSCTL("vm.stats.vm.v_vm_faults", ls->v_vm_faults);
773         GETSYSCTL("vm.stats.vm.v_io_faults", ls->v_io_faults);
774         GETSYSCTL("vm.stats.vm.v_cow_faults", ls->v_cow_faults);
775         GETSYSCTL("vm.stats.vm.v_zfod", ls->v_zfod);
776         GETSYSCTL("vm.stats.vm.v_ozfod", ls->v_ozfod);
777         GETSYSCTL("vm.stats.vm.v_swapin", ls->v_swapin);
778         GETSYSCTL("vm.stats.vm.v_swapout", ls->v_swapout);
779         GETSYSCTL("vm.stats.vm.v_swappgsin", ls->v_swappgsin);
780         GETSYSCTL("vm.stats.vm.v_swappgsout", ls->v_swappgsout);
781         GETSYSCTL("vm.stats.vm.v_vnodein", ls->v_vnodein);
782         GETSYSCTL("vm.stats.vm.v_vnodeout", ls->v_vnodeout);
783         GETSYSCTL("vm.stats.vm.v_vnodepgsin", ls->v_vnodepgsin);
784         GETSYSCTL("vm.stats.vm.v_vnodepgsout", ls->v_vnodepgsout);
785         GETSYSCTL("vm.stats.vm.v_intrans", ls->v_intrans);
786         GETSYSCTL("vm.stats.vm.v_reactivated", ls->v_reactivated);
787         GETSYSCTL("vm.stats.vm.v_pdwakeups", ls->v_pdwakeups);
788         GETSYSCTL("vm.stats.vm.v_pdpages", ls->v_pdpages);
789         GETSYSCTL("vm.stats.vm.v_dfree", ls->v_dfree);
790         GETSYSCTL("vm.stats.vm.v_pfree", ls->v_pfree);
791         GETSYSCTL("vm.stats.vm.v_tfree", ls->v_tfree);
792         GETSYSCTL("vm.stats.vm.v_page_size", ls->v_page_size);
793         GETSYSCTL("vm.stats.vm.v_free_count", ls->v_free_count);
794         GETSYSCTL("vm.stats.vm.v_wire_count", ls->v_wire_count);
795         GETSYSCTL("vm.stats.vm.v_active_count", ls->v_active_count);
796         GETSYSCTL("vm.stats.vm.v_inactive_count", ls->v_inactive_count);
797         GETSYSCTL("vm.stats.vm.v_laundry_count", ls->v_laundry_count);
798         GETSYSCTL("vfs.bufspace", ls->bufspace);
799         GETSYSCTL("kern.maxvnodes", ls->desiredvnodes);
800         GETSYSCTL("vfs.numvnodes", ls->numvnodes);
801         GETSYSCTL("vfs.freevnodes", ls->freevnodes);
802         GETSYSCTL("vfs.cache.nchstats", ls->nchstats);
803         GETSYSCTL("vfs.numdirtybuffers", ls->numdirtybuffers);
804         GETSYSCTL("vm.kmem_map_size", ls->v_kmem_map_size);
805         getsysctl("hw.intrcnt", ls->intrcnt, nintr * sizeof(u_long));
806
807         size = sizeof(ls->Total);
808         mib[0] = CTL_VM;
809         mib[1] = VM_TOTAL;
810         if (sysctl(mib, 2, &ls->Total, &size, NULL, 0) < 0) {
811                 error("Can't get kernel info: %s\n", strerror(errno));
812                 bzero(&ls->Total, sizeof(ls->Total));
813         }
814         size = sizeof(ncpu);
815         if (sysctlbyname("hw.ncpu", &ncpu, &size, NULL, 0) < 0 ||
816             size != sizeof(ncpu))
817                 ncpu = 1;
818
819         tmp_dinfo = last.dinfo;
820         last.dinfo = cur.dinfo;
821         cur.dinfo = tmp_dinfo;
822
823         last.snap_time = cur.snap_time;
824         switch (devstat_getdevs(NULL, &cur)) {
825         case -1:
826                 errx(1, "%s", devstat_errbuf);
827                 break;
828         case 1:
829                 num_devices = cur.dinfo->numdevs;
830                 generation = cur.dinfo->generation;
831                 cmdkre("refresh", NULL);
832                 break;
833         default:
834                 break;
835         }
836 }
837
838 static void
839 allocinfo(struct Info *ls)
840 {
841
842         ls->intrcnt = (long *) calloc(nintr, sizeof(long));
843         if (ls->intrcnt == NULL)
844                 errx(2, "out of memory");
845 }
846
847 static void
848 copyinfo(struct Info *from, struct Info *to)
849 {
850         long *intrcnt;
851
852         /*
853          * time, wds, seek, and xfer are malloc'd so we have to
854          * save the pointers before the structure copy and then
855          * copy by hand.
856          */
857         intrcnt = to->intrcnt;
858         *to = *from;
859
860         bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
861 }
862
863 static void
864 dinfo(int dn, int lc, struct statinfo *now, struct statinfo *then)
865 {
866         long double transfers_per_second;
867         long double kb_per_transfer, mb_per_second;
868         long double elapsed_time, device_busy;
869         int di;
870
871         di = dev_select[dn].position;
872
873         if (then != NULL) {
874                 /* Calculate relative to previous sample */
875                 elapsed_time = now->snap_time - then->snap_time;
876         } else {
877                 /* Calculate relative to device creation */
878                 elapsed_time = now->snap_time - devstat_compute_etime(
879                     &now->dinfo->devices[di].creation_time, NULL);
880         }
881
882         if (devstat_compute_statistics(&now->dinfo->devices[di], then ?
883             &then->dinfo->devices[di] : NULL, elapsed_time,
884             DSM_KB_PER_TRANSFER, &kb_per_transfer,
885             DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
886             DSM_MB_PER_SECOND, &mb_per_second,
887             DSM_BUSY_PCT, &device_busy,
888             DSM_NONE) != 0)
889                 errx(1, "%s", devstat_errbuf);
890
891         lc = DISKCOL + lc * 6;
892         putlongdouble(kb_per_transfer, DISKROW + 1, lc, 5, 2, 0);
893         putlongdouble(transfers_per_second, DISKROW + 2, lc, 5, 0, 0);
894         putlongdouble(mb_per_second, DISKROW + 3, lc, 5, 2, 0);
895         putlongdouble(device_busy, DISKROW + 4, lc, 5, 0, 0);
896 }