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