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