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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)vmstat.c 8.2 (Berkeley) 1/12/94
34 * $FreeBSD: src/usr.bin/systat/vmstat.c,v 1.38.2.4 2002/03/12 19:50:23 phantom Exp $
35 * $DragonFly: src/usr.bin/systat/vmstat.c,v 1.12 2008/11/10 04:59:45 swildner Exp $
39 * Cursed vmstat -- from Robert Elz.
42 #define _KERNEL_STRUCTURES
43 #include <sys/param.h>
48 #include <sys/namei.h>
49 #include <sys/sysctl.h>
50 #include <sys/vmmeter.h>
52 #include <vm/vm_param.h>
74 struct kinfo_cputime cp_time
;
78 struct nchstats nchstats
;
88 struct kinfo_cputime cp_time
, old_cp_time
;
89 struct statinfo cur
, last
, run
;
96 #define nchtotal s.nchstats
97 #define oldnchtotal s1.nchstats
99 static enum state
{ BOOT
, TIME
, RUN
} state
= TIME
;
101 static void allocinfo(struct Info
*);
102 static void copyinfo(struct Info
*, struct Info
*);
103 static void dinfo(int, int, struct statinfo
*, struct statinfo
*);
104 static void getinfo(struct Info
*);
105 static void putint(int, int, int, int);
106 static void putfloat(double, int, int, int, int, int);
107 static void putlongdouble(long double, int, int, int, int, int);
108 static int ucount(void);
116 static long *intrloc
;
117 static char **intrname
;
118 static int nextintsrow
;
119 static int extended_vm_stats
;
128 ut
= open(_PATH_UTMP
, O_RDONLY
);
146 static struct nlist namelist
[] = {
147 #define X_BUFFERSPACE 0
148 { .n_name
= "_bufspace" },
150 { .n_name
= "_nchstats" },
151 #define X_DESIREDVNODES 2
152 { .n_name
= "_desiredvnodes" },
153 #define X_NUMVNODES 3
154 { .n_name
= "_numvnodes" },
155 #define X_FREEVNODES 4
156 { .n_name
= "_freevnodes" },
157 #define X_NUMDIRTYBUFFERS 5
158 { .n_name
= "_dirtybufspace" },
163 * These constants define where the major pieces are laid out
165 #define STATROW 0 /* uses 1 row and 68 cols */
167 #define MEMROW 2 /* uses 4 rows and 31 cols */
169 #define PAGEROW 2 /* uses 4 rows and 26 cols */
171 #define INTSROW 6 /* uses all rows to bottom and 17 cols */
173 #define PROCSROW 7 /* uses 2 rows and 20 cols */
175 #define GENSTATROW 7 /* uses 2 rows and 30 cols */
176 #define GENSTATCOL 20
177 #define VMSTATROW 6 /* uses 17 rows and 12 cols */
179 #define GRAPHROW 10 /* uses 3 rows and 51 cols */
181 #define NAMEIROW 14 /* uses 3 rows and 38 cols */
183 #define DISKROW 18 /* uses 5 rows and 50 cols (for 9 drives) */
186 #define DRIVESPACE 7 /* max # for space */
188 #define MAXDRIVES DRIVESPACE /* max # to display */
198 if (namelist
[0].n_type
== 0) {
199 if (kvm_nlist(kd
, namelist
)) {
203 if (namelist
[0].n_type
== 0) {
204 error("No namelist");
209 if ((num_devices
= getnumdevs()) < 0) {
210 warnx("%s", devstat_errbuf
);
214 cur
.dinfo
= (struct devinfo
*)malloc(sizeof(struct devinfo
));
215 last
.dinfo
= (struct devinfo
*)malloc(sizeof(struct devinfo
));
216 run
.dinfo
= (struct devinfo
*)malloc(sizeof(struct devinfo
));
217 bzero(cur
.dinfo
, sizeof(struct devinfo
));
218 bzero(last
.dinfo
, sizeof(struct devinfo
));
219 bzero(run
.dinfo
, sizeof(struct devinfo
));
221 if (dsinit(MAXDRIVES
, &cur
, &last
, &run
) != 1)
225 if (sysctlbyname("hw.intrnames", NULL
, &bytes
, NULL
, 0) == 0) {
226 intrnamebuf
= malloc(bytes
);
227 sysctlbyname("hw.intrnames", intrnamebuf
, &bytes
,
229 for (i
= 0; i
< bytes
; ++i
) {
230 if (intrnamebuf
[i
] == 0)
233 intrname
= malloc(nintr
* sizeof(char *));
234 intrloc
= malloc(nintr
* sizeof(*intrloc
));
236 for (b
= i
= 0; i
< bytes
; ++i
) {
237 if (intrnamebuf
[i
] == 0) {
238 intrname
[nintr
] = intrnamebuf
+ b
;
245 nextintsrow
= INTSROW
+ 2;
261 static int d_first
= -1;
264 d_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
267 tp
= localtime(&now
);
268 (void) strftime(buf
, sizeof(buf
),
269 d_first
? "%e %b %R" : "%b %e %R", tp
);
279 mvprintw(STATROW
, STATCOL
+ 4, "users Load");
280 mvprintw(MEMROW
, MEMCOL
, "Mem:KB REAL VIRTUAL");
281 mvprintw(MEMROW
+ 1, MEMCOL
, " Tot Share Tot Share");
282 mvprintw(MEMROW
+ 2, MEMCOL
, "Act");
283 mvprintw(MEMROW
+ 3, MEMCOL
, "All");
285 mvprintw(MEMROW
+ 1, MEMCOL
+ 41, "Free");
287 mvprintw(PAGEROW
, PAGECOL
, " VN PAGER SWAP PAGER ");
288 mvprintw(PAGEROW
+ 1, PAGECOL
, " in out in out ");
289 mvprintw(PAGEROW
+ 2, PAGECOL
, "count");
290 mvprintw(PAGEROW
+ 3, PAGECOL
, "pages");
292 mvprintw(INTSROW
, INTSCOL
+ 3, " Interrupts");
293 mvprintw(INTSROW
+ 1, INTSCOL
+ 9, "total");
295 mvprintw(VMSTATROW
+ 1, VMSTATCOL
+ 10, "cow");
296 mvprintw(VMSTATROW
+ 2, VMSTATCOL
+ 10, "wire");
297 mvprintw(VMSTATROW
+ 3, VMSTATCOL
+ 10, "act");
298 mvprintw(VMSTATROW
+ 4, VMSTATCOL
+ 10, "inact");
299 mvprintw(VMSTATROW
+ 5, VMSTATCOL
+ 10, "cache");
300 mvprintw(VMSTATROW
+ 6, VMSTATCOL
+ 10, "free");
301 mvprintw(VMSTATROW
+ 7, VMSTATCOL
+ 10, "daefr");
302 mvprintw(VMSTATROW
+ 8, VMSTATCOL
+ 10, "prcfr");
303 mvprintw(VMSTATROW
+ 9, VMSTATCOL
+ 10, "react");
304 mvprintw(VMSTATROW
+ 10, VMSTATCOL
+ 10, "pdwake");
305 mvprintw(VMSTATROW
+ 11, VMSTATCOL
+ 10, "pdpgs");
306 mvprintw(VMSTATROW
+ 12, VMSTATCOL
+ 10, "intrn");
307 mvprintw(VMSTATROW
+ 13, VMSTATCOL
+ 10, "buf");
308 mvprintw(VMSTATROW
+ 14, VMSTATCOL
+ 10, "dirtybuf");
310 mvprintw(VMSTATROW
+ 15, VMSTATCOL
+ 10, "desiredvnodes");
311 mvprintw(VMSTATROW
+ 16, VMSTATCOL
+ 10, "numvnodes");
312 mvprintw(VMSTATROW
+ 17, VMSTATCOL
+ 10, "freevnodes");
314 mvprintw(GENSTATROW
, GENSTATCOL
, " Csw Trp Sys Int Sof Flt");
316 mvprintw(GRAPHROW
, GRAPHCOL
,
317 " . %%Sys . %%Intr . %%User . %%Nice . %%Idle");
318 mvprintw(PROCSROW
, PROCSCOL
, "Proc:r p d s w");
319 mvprintw(GRAPHROW
+ 1, GRAPHCOL
,
320 "| | | | | | | | | | |");
322 mvprintw(NAMEIROW
, NAMEICOL
, "Namei Name-cache Dir-cache");
323 mvprintw(NAMEIROW
+ 1, NAMEICOL
,
324 " Calls hits %% hits %%");
325 mvprintw(DISKROW
, DISKCOL
, "Disks");
326 mvprintw(DISKROW
+ 1, DISKCOL
, "KB/t");
327 mvprintw(DISKROW
+ 2, DISKCOL
, "tps");
328 mvprintw(DISKROW
+ 3, DISKCOL
, "MB/s");
329 mvprintw(DISKROW
+ 4, DISKCOL
, "%% busy");
331 * For now, we don't support a fourth disk statistic. So there's
332 * no point in providing a label for it. If someone can think of a
333 * fourth useful disk statistic, there is room to add it.
335 /* mvprintw(DISKROW + 4, DISKCOL, " msps"); */
337 for (i
= 0; i
< num_devices
&& j
< MAXDRIVES
; i
++)
338 if (dev_select
[i
].selected
) {
340 sprintf(tmpstr
, "%s%d", dev_select
[i
].device_name
,
341 dev_select
[i
].unit_number
);
342 mvprintw(DISKROW
, DISKCOL
+ 5 + 6 * j
,
349 * room for extended VM stats
351 mvprintw(VMSTATROW
+ 11, VMSTATCOL
- 6, "zfod");
352 mvprintw(VMSTATROW
+ 12, VMSTATCOL
- 6, "ofod");
353 mvprintw(VMSTATROW
+ 13, VMSTATCOL
- 6, "%%slo-z");
354 mvprintw(VMSTATROW
+ 14, VMSTATCOL
- 6, "tfree");
355 extended_vm_stats
= 1;
357 extended_vm_stats
= 0;
358 mvprintw(VMSTATROW
+ 0, VMSTATCOL
+ 10, "zfod");
361 for (i
= 0; i
< nintr
; i
++) {
364 mvprintw(intrloc
[i
], INTSCOL
+ 9, "%-10.10s", intrname
[i
]);
368 #define CP_UPDATE(fld) do { \
380 #define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
381 #define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
382 #define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
383 if(state == TIME) s1.nchstats.fld = t;}
384 #define PUTRATE(fld, l, c, w) \
386 putint((int)((float)s.fld/etime + 0.5), l, c, w)
390 static const char cpuchar
[5] = { '=' , '+', '>', '-', ' ' };
392 static const size_t cpuoffsets
[] = {
393 offsetof(struct kinfo_cputime
, cp_sys
),
394 offsetof(struct kinfo_cputime
, cp_intr
),
395 offsetof(struct kinfo_cputime
, cp_user
),
396 offsetof(struct kinfo_cputime
, cp_nice
),
397 offsetof(struct kinfo_cputime
, cp_idle
)
406 static int failcnt
= 0;
410 CP_UPDATE(cp_time
.cp_user
);
411 CP_UPDATE(cp_time
.cp_nice
);
412 CP_UPDATE(cp_time
.cp_sys
);
413 CP_UPDATE(cp_time
.cp_intr
);
414 CP_UPDATE(cp_time
.cp_idle
);
417 if (total_time
== 0.0)
420 if (etime
< 100000.0) { /* < 100ms ignore this trash */
421 if (failcnt
++ >= MAXFAIL
) {
423 mvprintw(2, 10, "The alternate system clock has died!");
424 mvprintw(3, 10, "Reverting to ``pigs'' display.");
439 for (i
= 0; i
< nintr
; i
++) {
440 if (s
.intrcnt
[i
] == 0)
442 if (intrloc
[i
] == 0) {
443 if (nextintsrow
== LINES
)
445 intrloc
[i
] = nextintsrow
++;
446 mvprintw(intrloc
[i
], INTSCOL
+ 9, "%-10.10s",
450 l
= (int)((float)s
.intrcnt
[i
]/etime
+ 0.5);
452 putint(l
, intrloc
[i
], INTSCOL
+ 2, 6);
454 putint(inttotal
, INTSROW
+ 1, INTSCOL
+ 2, 6);
455 Z(ncs_goodhits
); Z(ncs_badhits
); Z(ncs_miss
);
456 Z(ncs_long
); Z(ncs_pass2
); Z(ncs_2passes
); Z(ncs_neghits
);
457 s
.nchcount
= nchtotal
.ncs_goodhits
+ nchtotal
.ncs_badhits
+
458 nchtotal
.ncs_miss
+ nchtotal
.ncs_long
+ nchtotal
.ncs_neghits
;
460 s1
.nchcount
= s
.nchcount
;
464 for (lc
= 0; lc
< CPUSTATES
; lc
++) {
465 uint64_t val
= *(uint64_t *)(((uint8_t *)&s
.cp_time
) +
467 f1
= 100.0 * val
/ total_time
;
469 l
= (int) ((f2
+ 1.0) / 2.0) - psiz
;
471 f1
= 99.9; /* no room to display 100.0 */
472 putfloat(f1
, GRAPHROW
, GRAPHCOL
+ 10 * lc
, 4, 1, 0);
473 move(GRAPHROW
+ 2, psiz
);
479 putint(ucount(), STATROW
, STATCOL
, 3);
480 putfloat(avenrun
[0], STATROW
, STATCOL
+ 17, 6, 2, 0);
481 putfloat(avenrun
[1], STATROW
, STATCOL
+ 23, 6, 2, 0);
482 putfloat(avenrun
[2], STATROW
, STATCOL
+ 29, 6, 2, 0);
483 mvaddstr(STATROW
, STATCOL
+ 53, buf
);
484 #define pgtokb(pg) ((pg) * vms.v_page_size / 1024)
485 putint(pgtokb(total
.t_arm
), MEMROW
+ 2, MEMCOL
+ 3, 8);
486 putint(pgtokb(total
.t_armshr
), MEMROW
+ 2, MEMCOL
+ 11, 8);
487 putint(pgtokb(total
.t_avm
), MEMROW
+ 2, MEMCOL
+ 19, 9);
488 putint(pgtokb(total
.t_avmshr
), MEMROW
+ 2, MEMCOL
+ 28, 9);
489 putint(pgtokb(total
.t_rm
), MEMROW
+ 3, MEMCOL
+ 3, 8);
490 putint(pgtokb(total
.t_rmshr
), MEMROW
+ 3, MEMCOL
+ 11, 8);
491 putint(pgtokb(total
.t_vm
), MEMROW
+ 3, MEMCOL
+ 19, 9);
492 putint(pgtokb(total
.t_vmshr
), MEMROW
+ 3, MEMCOL
+ 28, 9);
493 putint(pgtokb(total
.t_free
), MEMROW
+ 2, MEMCOL
+ 37, 8);
494 putint(total
.t_rq
- 1, PROCSROW
+ 1, PROCSCOL
+ 3, 3);
495 putint(total
.t_pw
, PROCSROW
+ 1, PROCSCOL
+ 6, 3);
496 putint(total
.t_dw
, PROCSROW
+ 1, PROCSCOL
+ 9, 3);
497 putint(total
.t_sl
, PROCSROW
+ 1, PROCSCOL
+ 12, 3);
498 putint(total
.t_sw
, PROCSROW
+ 1, PROCSCOL
+ 15, 3);
499 if (extended_vm_stats
== 0) {
500 PUTRATE(Vmm
.v_zfod
, VMSTATROW
+ 0, VMSTATCOL
+ 4, 5);
502 PUTRATE(Vmm
.v_cow_faults
, VMSTATROW
+ 1, VMSTATCOL
+ 3, 6);
503 putint(pgtokb(vms
.v_wire_count
), VMSTATROW
+ 2, VMSTATCOL
, 9);
504 putint(pgtokb(vms
.v_active_count
), VMSTATROW
+ 3, VMSTATCOL
, 9);
505 putint(pgtokb(vms
.v_inactive_count
), VMSTATROW
+ 4, VMSTATCOL
, 9);
506 putint(pgtokb(vms
.v_cache_count
), VMSTATROW
+ 5, VMSTATCOL
, 9);
507 putint(pgtokb(vms
.v_free_count
), VMSTATROW
+ 6, VMSTATCOL
, 9);
508 PUTRATE(Vmm
.v_dfree
, VMSTATROW
+ 7, VMSTATCOL
, 9);
509 PUTRATE(Vmm
.v_pfree
, VMSTATROW
+ 8, VMSTATCOL
, 9);
510 PUTRATE(Vmm
.v_reactivated
, VMSTATROW
+ 9, VMSTATCOL
, 9);
511 PUTRATE(Vmm
.v_pdwakeups
, VMSTATROW
+ 10, VMSTATCOL
, 9);
512 PUTRATE(Vmm
.v_pdpages
, VMSTATROW
+ 11, VMSTATCOL
, 9);
513 PUTRATE(Vmm
.v_intrans
, VMSTATROW
+ 12, VMSTATCOL
, 9);
515 if (extended_vm_stats
) {
516 PUTRATE(Vmm
.v_zfod
, VMSTATROW
+ 11, VMSTATCOL
- 16, 9);
517 PUTRATE(Vmm
.v_ozfod
, VMSTATROW
+ 12, VMSTATCOL
- 16, 9);
519 ((s
.Vmm
.v_ozfod
< s
.Vmm
.v_zfod
) ?
520 s
.Vmm
.v_ozfod
* 100 / s
.Vmm
.v_zfod
:
527 PUTRATE(Vmm
.v_tfree
, VMSTATROW
+ 14, VMSTATCOL
- 16, 9);
530 putint(s
.bufspace
/1024, VMSTATROW
+ 13, VMSTATCOL
, 9);
531 putint(s
.dirtybufspace
/1024, VMSTATROW
+ 14, VMSTATCOL
, 9);
532 putint(s
.desiredvnodes
, VMSTATROW
+ 15, VMSTATCOL
, 9);
533 putint(s
.numvnodes
, VMSTATROW
+ 16, VMSTATCOL
, 9);
534 putint(s
.freevnodes
, VMSTATROW
+ 17, VMSTATCOL
, 9);
535 PUTRATE(Vmm
.v_vnodein
, PAGEROW
+ 2, PAGECOL
+ 5, 5);
536 PUTRATE(Vmm
.v_vnodeout
, PAGEROW
+ 2, PAGECOL
+ 10, 5);
537 PUTRATE(Vmm
.v_swapin
, PAGEROW
+ 2, PAGECOL
+ 17, 5);
538 PUTRATE(Vmm
.v_swapout
, PAGEROW
+ 2, PAGECOL
+ 22, 5);
539 PUTRATE(Vmm
.v_vnodepgsin
, PAGEROW
+ 3, PAGECOL
+ 5, 5);
540 PUTRATE(Vmm
.v_vnodepgsout
, PAGEROW
+ 3, PAGECOL
+ 10, 5);
541 PUTRATE(Vmm
.v_swappgsin
, PAGEROW
+ 3, PAGECOL
+ 17, 5);
542 PUTRATE(Vmm
.v_swappgsout
, PAGEROW
+ 3, PAGECOL
+ 22, 5);
543 PUTRATE(Vmm
.v_swtch
, GENSTATROW
+ 1, GENSTATCOL
, 5);
544 PUTRATE(Vmm
.v_trap
, GENSTATROW
+ 1, GENSTATCOL
+ 5, 5);
545 PUTRATE(Vmm
.v_syscall
, GENSTATROW
+ 1, GENSTATCOL
+ 10, 5);
546 PUTRATE(Vmm
.v_intr
, GENSTATROW
+ 1, GENSTATCOL
+ 15, 5);
547 PUTRATE(Vmm
.v_soft
, GENSTATROW
+ 1, GENSTATCOL
+ 20, 5);
548 PUTRATE(Vmm
.v_vm_faults
, GENSTATROW
+ 1, GENSTATCOL
+ 25, 5);
549 mvprintw(DISKROW
, DISKCOL
+ 5, " ");
550 for (i
= 0, lc
= 0; i
< num_devices
&& lc
< MAXDRIVES
; i
++)
551 if (dev_select
[i
].selected
) {
553 sprintf(tmpstr
, "%s%d", dev_select
[i
].device_name
,
554 dev_select
[i
].unit_number
);
555 mvprintw(DISKROW
, DISKCOL
+ 5 + 6 * lc
,
559 dinfo(i
, ++lc
, &cur
, &last
);
562 dinfo(i
, ++lc
, &cur
, &run
);
565 dinfo(i
, ++lc
, &cur
, NULL
);
569 putint(s
.nchcount
, NAMEIROW
+ 2, NAMEICOL
, 9);
570 putint((nchtotal
.ncs_goodhits
+ nchtotal
.ncs_neghits
),
571 NAMEIROW
+ 2, NAMEICOL
+ 9, 9);
572 #define nz(x) ((x) ? (x) : 1)
573 putfloat((nchtotal
.ncs_goodhits
+nchtotal
.ncs_neghits
) *
574 100.0 / nz(s
.nchcount
),
575 NAMEIROW
+ 2, NAMEICOL
+ 19, 4, 0, 1);
576 putint(nchtotal
.ncs_pass2
, NAMEIROW
+ 2, NAMEICOL
+ 23, 9);
577 putfloat(nchtotal
.ncs_pass2
* 100.0 / nz(s
.nchcount
),
578 NAMEIROW
+ 2, NAMEICOL
+ 33, 4, 0, 1);
583 cmdkre(const char *cmd
, char *args
)
587 if (prefix(cmd
, "run")) {
590 switch (getdevs(&run
)) {
592 errx(1, "%s", devstat_errbuf
);
595 num_devices
= run
.dinfo
->numdevs
;
596 generation
= run
.dinfo
->generation
;
597 retval
= dscmd("refresh", NULL
, MAXDRIVES
, &cur
);
607 if (prefix(cmd
, "boot")) {
612 if (prefix(cmd
, "time")) {
616 if (prefix(cmd
, "zero")) {
620 switch (getdevs(&run
)) {
622 errx(1, "%s", devstat_errbuf
);
625 num_devices
= run
.dinfo
->numdevs
;
626 generation
= run
.dinfo
->generation
;
627 retval
= dscmd("refresh",NULL
, MAXDRIVES
, &cur
);
637 retval
= dscmd(cmd
, args
, MAXDRIVES
, &cur
);
645 /* calculate number of users on the system */
653 while (read(ut
, &utmp
, sizeof(utmp
)))
654 if (utmp
.ut_name
[0] != '\0')
657 lseek(ut
, 0L, L_SET
);
662 putint(int n
, int l
, int lc
, int w
)
672 snprintf(b
, sizeof(b
), "%*d", w
, n
);
673 if (strlen(b
) > (size_t)w
) {
682 putfloat(double f
, int l
, int lc
, int w
, int d
, int nz
)
687 if (nz
&& f
== 0.0) {
692 snprintf(b
, sizeof(b
), "%*.*f", w
, d
, f
);
693 if (strlen(b
) > (size_t)w
)
694 snprintf(b
, sizeof(b
), "%*.0f", w
, f
);
695 if (strlen(b
) > (size_t)w
) {
704 putlongdouble(long double f
, int l
, int lc
, int w
, int d
, int nz
)
709 if (nz
&& f
== 0.0) {
714 sprintf(b
, "%*.*Lf", w
, d
, f
);
715 if (strlen(b
) > (size_t)w
)
716 sprintf(b
, "%*.0Lf", w
, f
);
717 if (strlen(b
) > (size_t)w
) {
726 getinfo(struct Info
*ls
)
728 struct devinfo
*tmp_dinfo
;
729 struct nchstats
*nch_tmp
;
731 size_t vms_size
= sizeof(ls
->Vms
);
732 size_t vmm_size
= sizeof(ls
->Vmm
);
733 size_t nch_size
= sizeof(ls
->nchstats
) * SMP_MAXCPU
;
735 if (sysctlbyname("vm.vmstats", &ls
->Vms
, &vms_size
, NULL
, 0)) {
736 perror("sysctlbyname: vm.vmstats");
739 if (sysctlbyname("vm.vmmeter", &ls
->Vmm
, &vmm_size
, NULL
, 0)) {
740 perror("sysctlbyname: vm.vmstats");
744 if (kinfo_get_sched_cputime(&ls
->cp_time
))
745 err(1, "kinfo_get_sched_cputime");
746 if (kinfo_get_sched_cputime(&cp_time
))
747 err(1, "kinfo_get_sched_cputime");
748 NREAD(X_BUFFERSPACE
, &ls
->bufspace
, sizeof(ls
->bufspace
));
749 NREAD(X_DESIREDVNODES
, &ls
->desiredvnodes
, sizeof(ls
->desiredvnodes
));
750 NREAD(X_NUMVNODES
, &ls
->numvnodes
, LONG
);
751 NREAD(X_FREEVNODES
, &ls
->freevnodes
, LONG
);
752 NREAD(X_NUMDIRTYBUFFERS
, &ls
->dirtybufspace
, sizeof(ls
->dirtybufspace
));
755 size
= nintr
* sizeof(ls
->intrcnt
[0]);
756 sysctlbyname("hw.intrcnt", ls
->intrcnt
, &size
, NULL
, 0);
758 size
= sizeof(ls
->Total
);
759 if (sysctlbyname("vm.vmtotal", &ls
->Total
, &size
, NULL
, 0) < 0) {
760 error("Can't get kernel info: %s\n", strerror(errno
));
761 bzero(&ls
->Total
, sizeof(ls
->Total
));
764 if ((nch_tmp
= malloc(nch_size
)) == NULL
) {
768 if (sysctlbyname("vfs.cache.nchstats", nch_tmp
, &nch_size
, NULL
, 0)) {
769 perror("sysctlbyname vfs.cache.nchstats");
773 if ((nch_tmp
= realloc(nch_tmp
, nch_size
)) == NULL
) {
780 if (kinfo_get_cpus(&ncpu
))
781 err(1, "kinfo_get_cpus");
782 kvm_nch_cpuagg(nch_tmp
, &ls
->nchstats
, ncpu
);
785 tmp_dinfo
= last
.dinfo
;
786 last
.dinfo
= cur
.dinfo
;
787 cur
.dinfo
= tmp_dinfo
;
789 last
.busy_time
= cur
.busy_time
;
790 switch (getdevs(&cur
)) {
792 errx(1, "%s", devstat_errbuf
);
795 num_devices
= cur
.dinfo
->numdevs
;
796 generation
= cur
.dinfo
->generation
;
797 cmdkre("refresh", NULL
);
805 allocinfo(struct Info
*ls
)
807 ls
->intrcnt
= (long *) calloc(nintr
, sizeof(long));
808 if (ls
->intrcnt
== NULL
)
809 errx(2, "out of memory");
813 copyinfo(struct Info
*from
, struct Info
*to
)
818 * time, wds, seek, and xfer are malloc'd so we have to
819 * save the pointers before the structure copy and then
822 intrcnt
= to
->intrcnt
;
825 bcopy(from
->intrcnt
, to
->intrcnt
= intrcnt
, nintr
* sizeof (int));
829 dinfo(int dn
, int lc
, struct statinfo
*now
, struct statinfo
*then
)
831 long double transfers_per_second
;
832 long double kb_per_transfer
, mb_per_second
;
833 long double elapsed_time
, device_busy
;
836 di
= dev_select
[dn
].position
;
838 elapsed_time
= compute_etime(now
->busy_time
, then
?
840 now
->dinfo
->devices
[di
].dev_creation_time
);
842 device_busy
= compute_etime(now
->dinfo
->devices
[di
].busy_time
, then
?
843 then
->dinfo
->devices
[di
].busy_time
:
844 now
->dinfo
->devices
[di
].dev_creation_time
);
846 if (compute_stats(&now
->dinfo
->devices
[di
], then
?
847 &then
->dinfo
->devices
[di
] : NULL
, elapsed_time
,
849 &kb_per_transfer
, &transfers_per_second
,
850 &mb_per_second
, NULL
, NULL
) != 0)
851 errx(1, "%s", devstat_errbuf
);
853 if ((device_busy
== 0) && (transfers_per_second
> 5))
854 /* the device has been 100% busy, fake it because
855 * as long as the device is 100% busy the busy_time
856 * field in the devstat struct is not updated */
857 device_busy
= elapsed_time
;
858 if (device_busy
> elapsed_time
)
859 /* this normally happens after one or more periods
860 * where the device has been 100% busy, correct it */
861 device_busy
= elapsed_time
;
863 lc
= DISKCOL
+ lc
* 6;
864 putlongdouble(kb_per_transfer
, DISKROW
+ 1, lc
, 5, 2, 0);
865 putlongdouble(transfers_per_second
, DISKROW
+ 2, lc
, 5, 0, 0);
866 putlongdouble(mb_per_second
, DISKROW
+ 3, lc
, 5, 2, 0);
867 putlongdouble(device_busy
* 100 / elapsed_time
, DISKROW
+ 4, lc
, 5, 0, 0);