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