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
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
31 * Cursed vmstat -- from Robert Elz.
35 #include <sys/param.h>
39 #include <sys/namei.h>
40 #include <sys/sysctl.h>
41 #include <sys/vmmeter.h>
43 #include <vm/vm_param.h>
58 #include "utmpentry.h"
65 struct kinfo_cputime cp_time
;
69 struct nchstats nchstats
;
81 struct kinfo_cputime cp_time
, old_cp_time
;
82 struct statinfo cur
, last
, run
;
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);
109 static int *intralias
;
111 static long *intrloc
;
113 static char **intrname
;
114 static int nextintsrow
;
115 static int extended_vm_stats
;
137 static struct nlist namelist
[] = {
138 #define X_BUFFERSPACE 0
139 { .n_name
= "_bufspace" },
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" },
156 * These constants define where the major pieces are laid out
158 #define STATROW 0 /* uses 1 row and 68 cols */
160 #define MEMROW 2 /* uses 4 rows and 31 cols */
162 #define PAGEROW 2 /* uses 4 rows and 26 cols */
164 #define INTSROW 6 /* uses all rows to bottom and 17 cols */
166 #define PROCSROW 7 /* uses 2 rows and 20 cols */
168 #define GENSTATROW 7 /* uses 2 rows and 30 cols */
169 #define GENSTATCOL 16
170 #define VMSTATROW 6 /* uses 17 rows and 12 cols */
172 #define GRAPHROW 10 /* uses 3 rows and 51 cols */
174 #define NAMEIROW 14 /* uses 3 rows and 38 cols */
176 #define EXECROW 14 /* uses 2 rows and 5 cols */
178 #define DISKROW 17 /* uses 6 rows and 50 cols (for 9 drives) */
181 #define DRIVESPACE 7 /* max # for space */
183 #define MAXDRIVES DRIVESPACE /* max # to display */
187 findintralias(const char *name
, int limit
)
194 for (i
= 0; i
< limit
; ++i
) {
195 if (strcmp(name
, intrname
[i
]) == 0)
197 ilen
= strlen(intrname
[i
]);
200 strncmp(name
, intrname
[i
], nlen
- 1) == 0 &&
202 isdigit(name
[nlen
- 1]) &&
203 (isdigit(intrname
[i
][nlen
- 1]) ||
204 intrname
[i
][nlen
- 1] == '*')) {
205 intrname
[i
][nlen
- 1] = '*';
220 if (namelist
[0].n_type
== 0) {
221 if (kvm_nlist(kd
, namelist
)) {
225 if (namelist
[0].n_type
== 0) {
226 error("No namelist");
231 if ((num_devices
= getnumdevs()) < 0) {
232 warnx("%s", devstat_errbuf
);
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)
247 if (sysctlbyname("hw.intrnames", NULL
, &bytes
, NULL
, 0) == 0) {
248 intrnamebuf
= malloc(bytes
);
249 sysctlbyname("hw.intrnames", intrnamebuf
, &bytes
,
251 for (i
= 0; i
< bytes
; ++i
) {
252 if (intrnamebuf
[i
] == 0)
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
));
263 for (b
= i
= 0; i
< bytes
; ++i
) {
264 if (intrnamebuf
[i
] == 0) {
265 intrname
[nintr
] = intrnamebuf
+ b
;
268 findintralias(intrname
[nintr
], nintr
);
269 ++intrsmp
[intralias
[nintr
]];
275 nextintsrow
= INTSROW
+ 2;
291 static int d_first
= -1;
294 d_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
297 tp
= localtime(&now
);
298 (void) strftime(buf
, sizeof(buf
),
299 d_first
? "%e %b %R" : "%b %e %R", tp
);
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.
367 for (i
= 0; i
< num_devices
&& j
< MAXDRIVES
; i
++)
368 if (dev_select
[i
].selected
) {
370 sprintf(tmpstr
, "%s%d", dev_select
[i
].device_name
,
371 dev_select
[i
].unit_number
);
372 mvprintw(DISKROW
, DISKCOL
+ 5 + 6 * j
,
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;
387 extended_vm_stats
= 0;
388 mvprintw(VMSTATROW
+ 0, VMSTATCOL
+ 8, "zfod");
391 for (i
= 0; i
< nintr
; i
++) {
394 mvprintw(intrloc
[i
], INTSCOL
+ 9, "%-10.10s", intrname
[i
]);
398 #define CP_UPDATE(fld) do { \
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) \
416 put64((int64_t)((float)s.fld/etime + 0.5), l, c, w, 'D')
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
)
438 static int failcnt
= 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
);
449 if (total_time
== 0.0)
452 if (etime
< 100000.0) { /* < 100ms ignore this trash */
453 if (failcnt
++ >= MAXFAIL
) {
455 mvprintw(2, 10, "The alternate system clock has died!");
456 mvprintw(3, 10, "Reverting to ``pigs'' display.");
471 bzero(lacc
, nintr
* sizeof(*lacc
));
473 for (i
= 0; i
< nintr
; i
++) {
474 if (s
.intrcnt
[i
] == 0)
477 if (intrloc
[j
] == 0) {
478 if (nextintsrow
== LINES
)
480 intrloc
[j
] = nextintsrow
++;
481 mvprintw(intrloc
[j
], INTSCOL
+ 9, "%-10.10s",
485 l
= (long)((float)s
.intrcnt
[i
]/etime
+ 0.5);
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
;
497 s1
.nchcount
= s
.nchcount
;
498 s1
.nchpathcount
= s
.nchpathcount
;
503 for (lc
= 0; lc
< CPUSTATES
; lc
++) {
504 uint64_t val
= *(uint64_t *)(((uint8_t *)&s
.cp_time
) +
506 f1
= 100.0 * val
/ total_time
;
508 l
= (int) ((f2
+ 1.0) / 2.0) - psiz
;
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
);
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');
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
) {
589 sprintf(tmpstr
, "%s%d", dev_select
[i
].device_name
,
590 dev_select
[i
].unit_number
);
591 mvprintw(DISKROW
, DISKCOL
+ 5 + 6 * lc
,
595 dinfo(i
, ++lc
, &cur
, &last
);
598 dinfo(i
, ++lc
, &cur
, &run
);
601 dinfo(i
, ++lc
, &cur
, NULL
);
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);
618 cmdkre(const char *cmd
, char *args
)
622 if (prefix(cmd
, "run")) {
625 switch (getdevs(&run
)) {
627 errx(1, "%s", devstat_errbuf
);
630 num_devices
= run
.dinfo
->numdevs
;
631 generation
= run
.dinfo
->generation
;
632 retval
= dscmd("refresh", NULL
, MAXDRIVES
, &cur
);
642 if (prefix(cmd
, "boot")) {
647 if (prefix(cmd
, "time")) {
651 if (prefix(cmd
, "zero")) {
655 switch (getdevs(&run
)) {
657 errx(1, "%s", devstat_errbuf
);
660 num_devices
= run
.dinfo
->numdevs
;
661 generation
= run
.dinfo
->generation
;
662 retval
= dscmd("refresh",NULL
, MAXDRIVES
, &cur
);
672 retval
= dscmd(cmd
, args
, MAXDRIVES
, &cur
);
680 /* calculate number of users on the system */
684 struct utmpentry
*ep
;
687 getutentries(NULL
, &ep
);
688 for (; ep
; ep
= ep
->next
)
695 put64(intmax_t n
, int l
, int lc
, int w
, int type
)
709 if (type
== 0 || type
== 'D')
710 snprintf(b
, sizeof(b
), "%*jd", w
, n
);
712 snprintf(b
, sizeof(b
), "%*jd%c", w
- 1, n
, type
);
713 if (strlen(b
) <= (size_t)w
) {
729 for (d
= 1; n
/ d
>= 1000; d
*= u
) {
756 else if (n
/ d
>= 10)
761 snprintf(b
+ 64, sizeof(b
) - 64, "%jd.%03jd%c",
762 n
/ d
, n
/ (d
/ 1000) % 1000, type
);
764 snprintf(b
+ 64, sizeof(b
) - 64, "%jd.%02jd%c",
765 n
/ d
, n
/ (d
/ 100) % 100, type
);
767 snprintf(b
+ 64, sizeof(b
) - 64, "%jd.%01jd%c",
768 n
/ d
, n
/ (d
/ 10) % 10, type
);
770 snprintf(b
+ 64, sizeof(b
) - 64, "%jd%c",
787 putfloat(double f
, int l
, int lc
, int w
, int d
, int nz
)
792 if (nz
&& f
== 0.0) {
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
) {
809 putlongdouble(long double f
, int l
, int lc
, int w
, int d
, int nz
)
814 if (nz
&& f
== 0.0) {
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
) {
831 putlongdoublez(long double f
, int l
, int lc
, int w
, int d
, int nz
)
837 sprintf(b
, "%*.*s", w
, w
, "");
840 putlongdouble(f
, l
, lc
, w
, d
, nz
);
845 getinfo(struct Info
*ls
)
847 struct devinfo
*tmp_dinfo
;
848 struct nchstats
*nch_tmp
;
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");
858 if (sysctlbyname("vm.vmmeter", &ls
->Vmm
, &vmm_size
, NULL
, 0)) {
859 perror("sysctlbyname: vm.vmstats");
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
));
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
) {
889 if (sysctlbyname("vfs.cache.nchstats", nch_tmp
, &nch_size
, NULL
, 0)) {
890 perror("sysctlbyname vfs.cache.nchstats");
894 if ((nch_tmp
= realloc(nch_tmp
, nch_size
)) == NULL
) {
901 if (kinfo_get_cpus(&ncpu
))
902 err(1, "kinfo_get_cpus");
903 kvm_nch_cpuagg(nch_tmp
, &ls
->nchstats
, ncpu
);
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
)) {
913 errx(1, "%s", devstat_errbuf
);
916 num_devices
= cur
.dinfo
->numdevs
;
917 generation
= cur
.dinfo
->generation
;
918 cmdkre("refresh", NULL
);
926 allocinfo(struct Info
*ls
)
928 ls
->intrcnt
= (long *) calloc(nintr
, sizeof(long));
929 if (ls
->intrcnt
== NULL
)
930 errx(2, "out of memory");
934 copyinfo(struct Info
*from
, struct Info
*to
)
939 * time, wds, seek, and xfer are malloc'd so we have to
940 * save the pointers before the structure copy and then
943 intrcnt
= to
->intrcnt
;
946 bcopy(from
->intrcnt
, to
->intrcnt
= intrcnt
, nintr
* sizeof (int));
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
;
960 di
= dev_select
[dn
].position
;
962 elapsed_time
= compute_etime(now
->busy_time
, then
?
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
);
971 &now
->dinfo
->devices
[di
],
972 (then
? &then
->dinfo
->devices
[di
] : NULL
),
979 errx(1, "%s", devstat_errbuf
);
981 if (compute_stats_read(
982 &now
->dinfo
->devices
[di
],
983 (then
? &then
->dinfo
->devices
[di
] : NULL
),
987 &transfers_per_secondr
,
990 errx(1, "%s", devstat_errbuf
);
992 if (compute_stats_write(
993 &now
->dinfo
->devices
[di
],
994 (then
? &then
->dinfo
->devices
[di
] : NULL
),
998 &transfers_per_secondw
,
1001 errx(1, "%s", devstat_errbuf
);
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
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
;
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);