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, 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 16
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, " 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, 'D')
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, 'D');
454 putint(inttotal, INTSROW + 1, INTSCOL + 2, 6, 'D');
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, 'D');
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, 'K');
486 putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 11, 8, 'K');
487 putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 19, 9, 'K');
488 putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 28, 9, 'K');
489 putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 8, 'K');
490 putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 11, 8, 'K');
491 putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 19, 9, 'K');
492 putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 28, 9, 'K');
493 putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 37, 8, 'K');
494 putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 0, 3, 'D');
495 putint(total.t_pw, PROCSROW + 1, PROCSCOL + 3, 3, 'D');
496 putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3, 'D');
497 putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3, 'D');
498 putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3, 'D');
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, 'K');
504 putint(pgtokb(vms.v_active_count), VMSTATROW + 3, VMSTATCOL, 9, 'K');
505 putint(pgtokb(vms.v_inactive_count), VMSTATROW + 4, VMSTATCOL, 9, 'K');
506 putint(pgtokb(vms.v_cache_count), VMSTATROW + 5, VMSTATCOL, 9, 'K');
507 putint(pgtokb(vms.v_free_count), VMSTATROW + 6, VMSTATCOL, 9, 'K');
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 :
528 PUTRATE(Vmm.v_tfree, VMSTATROW + 14, VMSTATCOL - 16, 9);
531 putint(s.bufspace/1024, VMSTATROW + 13, VMSTATCOL, 9, 'K');
532 putint(s.dirtybufspace/1024, VMSTATROW + 14, VMSTATCOL, 9, 'K');
533 putint(s.desiredvnodes, VMSTATROW + 15, VMSTATCOL, 9, 'D');
534 putint(s.numvnodes, VMSTATROW + 16, VMSTATCOL, 9, 'D');
535 putint(s.freevnodes, VMSTATROW + 17, VMSTATCOL, 9, 'D');
536 PUTRATE(Vmm.v_vnodein, PAGEROW + 2, PAGECOL + 5, 5);
537 PUTRATE(Vmm.v_vnodeout, PAGEROW + 2, PAGECOL + 10, 5);
538 PUTRATE(Vmm.v_swapin, PAGEROW + 2, PAGECOL + 17, 5);
539 PUTRATE(Vmm.v_swapout, PAGEROW + 2, PAGECOL + 22, 5);
540 PUTRATE(Vmm.v_vnodepgsin, PAGEROW + 3, PAGECOL + 5, 5);
541 PUTRATE(Vmm.v_vnodepgsout, PAGEROW + 3, PAGECOL + 10, 5);
542 PUTRATE(Vmm.v_swappgsin, PAGEROW + 3, PAGECOL + 17, 5);
543 PUTRATE(Vmm.v_swappgsout, PAGEROW + 3, PAGECOL + 22, 5);
544 PUTRATE(Vmm.v_swtch, GENSTATROW + 1, GENSTATCOL, 5);
545 PUTRATE(Vmm.v_trap, GENSTATROW + 1, GENSTATCOL + 6, 5);
546 PUTRATE(Vmm.v_syscall, GENSTATROW + 1, GENSTATCOL + 12, 5);
547 PUTRATE(Vmm.v_intr, GENSTATROW + 1, GENSTATCOL + 18, 5);
548 PUTRATE(Vmm.v_soft, GENSTATROW + 1, GENSTATCOL + 23, 5);
549 PUTRATE(Vmm.v_vm_faults, GENSTATROW + 1, GENSTATCOL + 29, 5);
550 mvprintw(DISKROW, DISKCOL + 5, " ");
551 for (i = 0, lc = 0; i < num_devices && lc < MAXDRIVES; i++)
552 if (dev_select[i].selected) {
554 sprintf(tmpstr, "%s%d", dev_select[i].device_name,
555 dev_select[i].unit_number);
556 mvprintw(DISKROW, DISKCOL + 5 + 6 * lc,
560 dinfo(i, ++lc, &cur, &last);
563 dinfo(i, ++lc, &cur, &run);
566 dinfo(i, ++lc, &cur, NULL);
570 putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9, 'D');
571 putint((nchtotal.ncs_goodhits + nchtotal.ncs_neghits),
572 NAMEIROW + 2, NAMEICOL + 9, 9, 'D');
573 #define nz(x) ((x) ? (x) : 1)
574 putfloat((nchtotal.ncs_goodhits+nchtotal.ncs_neghits) *
575 100.0 / nz(s.nchcount),
576 NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
577 putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9, 'D');
578 putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
579 NAMEIROW + 2, NAMEICOL + 33, 4, 0, 1);
584 cmdkre(const char *cmd, char *args)
588 if (prefix(cmd, "run")) {
591 switch (getdevs(&run)) {
593 errx(1, "%s", devstat_errbuf);
596 num_devices = run.dinfo->numdevs;
597 generation = run.dinfo->generation;
598 retval = dscmd("refresh", NULL, MAXDRIVES, &cur);
608 if (prefix(cmd, "boot")) {
613 if (prefix(cmd, "time")) {
617 if (prefix(cmd, "zero")) {
621 switch (getdevs(&run)) {
623 errx(1, "%s", devstat_errbuf);
626 num_devices = run.dinfo->numdevs;
627 generation = run.dinfo->generation;
628 retval = dscmd("refresh",NULL, MAXDRIVES, &cur);
638 retval = dscmd(cmd, args, MAXDRIVES, &cur);
646 /* calculate number of users on the system */
654 while (read(ut, &utmp, sizeof(utmp)))
655 if (utmp.ut_name[0] != '\0')
658 lseek(ut, 0L, L_SET);
663 putint(int n, int l, int lc, int w, int type)
674 snprintf(b, sizeof(b), "%*d", w, n);
675 if (strlen(b) > (size_t)w) {
683 snprintf(b, sizeof(b), "%*d%c", w - 1, n, xtype);
684 if (strlen(b) > (size_t)w) {
692 snprintf(b, sizeof(b), "%*d%c", w - 1, n, xtype);
693 if (strlen(b) > (size_t)w) {
704 putfloat(double f, int l, int lc, int w, int d, int nz)
709 if (nz && f == 0.0) {
714 snprintf(b, sizeof(b), "%*.*f", w, d, f);
715 if (strlen(b) > (size_t)w)
716 snprintf(b, sizeof(b), "%*.0f", w, f);
717 if (strlen(b) > (size_t)w) {
726 putlongdouble(long double f, int l, int lc, int w, int d, int nz)
731 if (nz && f == 0.0) {
736 sprintf(b, "%*.*Lf", w, d, f);
737 if (strlen(b) > (size_t)w)
738 sprintf(b, "%*.0Lf", w, f);
739 if (strlen(b) > (size_t)w) {
748 getinfo(struct Info *ls)
750 struct devinfo *tmp_dinfo;
751 struct nchstats *nch_tmp;
753 size_t vms_size = sizeof(ls->Vms);
754 size_t vmm_size = sizeof(ls->Vmm);
755 size_t nch_size = sizeof(ls->nchstats) * SMP_MAXCPU;
757 if (sysctlbyname("vm.vmstats", &ls->Vms, &vms_size, NULL, 0)) {
758 perror("sysctlbyname: vm.vmstats");
761 if (sysctlbyname("vm.vmmeter", &ls->Vmm, &vmm_size, NULL, 0)) {
762 perror("sysctlbyname: vm.vmstats");
766 if (kinfo_get_sched_cputime(&ls->cp_time))
767 err(1, "kinfo_get_sched_cputime");
768 if (kinfo_get_sched_cputime(&cp_time))
769 err(1, "kinfo_get_sched_cputime");
770 NREAD(X_BUFFERSPACE, &ls->bufspace, sizeof(ls->bufspace));
771 NREAD(X_DESIREDVNODES, &ls->desiredvnodes, sizeof(ls->desiredvnodes));
772 NREAD(X_NUMVNODES, &ls->numvnodes, LONG);
773 NREAD(X_FREEVNODES, &ls->freevnodes, LONG);
774 NREAD(X_NUMDIRTYBUFFERS, &ls->dirtybufspace, sizeof(ls->dirtybufspace));
777 size = nintr * sizeof(ls->intrcnt[0]);
778 sysctlbyname("hw.intrcnt", ls->intrcnt, &size, NULL, 0);
780 size = sizeof(ls->Total);
781 if (sysctlbyname("vm.vmtotal", &ls->Total, &size, NULL, 0) < 0) {
782 error("Can't get kernel info: %s\n", strerror(errno));
783 bzero(&ls->Total, sizeof(ls->Total));
786 if ((nch_tmp = malloc(nch_size)) == NULL) {
790 if (sysctlbyname("vfs.cache.nchstats", nch_tmp, &nch_size, NULL, 0)) {
791 perror("sysctlbyname vfs.cache.nchstats");
795 if ((nch_tmp = realloc(nch_tmp, nch_size)) == NULL) {
802 if (kinfo_get_cpus(&ncpu))
803 err(1, "kinfo_get_cpus");
804 kvm_nch_cpuagg(nch_tmp, &ls->nchstats, ncpu);
807 tmp_dinfo = last.dinfo;
808 last.dinfo = cur.dinfo;
809 cur.dinfo = tmp_dinfo;
811 last.busy_time = cur.busy_time;
812 switch (getdevs(&cur)) {
814 errx(1, "%s", devstat_errbuf);
817 num_devices = cur.dinfo->numdevs;
818 generation = cur.dinfo->generation;
819 cmdkre("refresh", NULL);
827 allocinfo(struct Info *ls)
829 ls->intrcnt = (long *) calloc(nintr, sizeof(long));
830 if (ls->intrcnt == NULL)
831 errx(2, "out of memory");
835 copyinfo(struct Info *from, struct Info *to)
840 * time, wds, seek, and xfer are malloc'd so we have to
841 * save the pointers before the structure copy and then
844 intrcnt = to->intrcnt;
847 bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
851 dinfo(int dn, int lc, struct statinfo *now, struct statinfo *then)
853 long double transfers_per_second;
854 long double kb_per_transfer, mb_per_second;
855 long double elapsed_time, device_busy;
858 di = dev_select[dn].position;
860 elapsed_time = compute_etime(now->busy_time, then ?
862 now->dinfo->devices[di].dev_creation_time);
864 device_busy = compute_etime(now->dinfo->devices[di].busy_time, then ?
865 then->dinfo->devices[di].busy_time :
866 now->dinfo->devices[di].dev_creation_time);
868 if (compute_stats(&now->dinfo->devices[di], then ?
869 &then->dinfo->devices[di] : NULL, elapsed_time,
871 &kb_per_transfer, &transfers_per_second,
872 &mb_per_second, NULL, NULL) != 0)
873 errx(1, "%s", devstat_errbuf);
875 if ((device_busy == 0) && (transfers_per_second > 5))
876 /* the device has been 100% busy, fake it because
877 * as long as the device is 100% busy the busy_time
878 * field in the devstat struct is not updated */
879 device_busy = elapsed_time;
880 if (device_busy > elapsed_time)
881 /* this normally happens after one or more periods
882 * where the device has been 100% busy, correct it */
883 device_busy = elapsed_time;
885 lc = DISKCOL + lc * 6;
886 putlongdouble(kb_per_transfer, DISKROW + 1, lc, 5, 2, 0);
887 putlongdouble(transfers_per_second, DISKROW + 2, lc, 5, 0, 0);
888 putlongdouble(mb_per_second, DISKROW + 3, lc, 5, 2, 0);
889 putlongdouble(device_busy * 100 / elapsed_time, DISKROW + 4, lc, 5, 0, 0);