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