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.
43 #include <sys/param.h>
47 #include <sys/namei.h>
48 #include <sys/sysctl.h>
49 #include <sys/vmmeter.h>
51 #include <vm/vm_param.h>
73 struct kinfo_cputime cp_time;
77 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 putlong(long, 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 void putlongdoublez(long double, int, int, int, int, int);
109 static int ucount(void);
117 static long *intrloc;
118 static char **intrname;
119 static int nextintsrow;
120 static int extended_vm_stats;
129 ut = open(_PATH_UTMP, O_RDONLY);
147 static struct nlist namelist[] = {
148 #define X_BUFFERSPACE 0
149 { .n_name = "_bufspace" },
151 { .n_name = "_nchstats" },
152 #define X_DESIREDVNODES 2
153 { .n_name = "_desiredvnodes" },
154 #define X_NUMVNODES 3
155 { .n_name = "_numvnodes" },
156 #define X_FREEVNODES 4
157 { .n_name = "_freevnodes" },
158 #define X_NUMDIRTYBUFFERS 5
159 { .n_name = "_dirtybufspace" },
164 * These constants define where the major pieces are laid out
166 #define STATROW 0 /* uses 1 row and 68 cols */
168 #define MEMROW 2 /* uses 4 rows and 31 cols */
170 #define PAGEROW 2 /* uses 4 rows and 26 cols */
172 #define INTSROW 6 /* uses all rows to bottom and 17 cols */
174 #define PROCSROW 7 /* uses 2 rows and 20 cols */
176 #define GENSTATROW 7 /* uses 2 rows and 30 cols */
177 #define GENSTATCOL 16
178 #define VMSTATROW 6 /* uses 17 rows and 12 cols */
180 #define GRAPHROW 10 /* uses 3 rows and 51 cols */
182 #define NAMEIROW 14 /* uses 3 rows and 38 cols */
184 #define DISKROW 17 /* uses 6 rows and 50 cols (for 9 drives) */
187 #define DRIVESPACE 7 /* max # for space */
189 #define MAXDRIVES DRIVESPACE /* max # to display */
199 if (namelist[0].n_type == 0) {
200 if (kvm_nlist(kd, namelist)) {
204 if (namelist[0].n_type == 0) {
205 error("No namelist");
210 if ((num_devices = getnumdevs()) < 0) {
211 warnx("%s", devstat_errbuf);
215 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
216 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
217 run.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
218 bzero(cur.dinfo, sizeof(struct devinfo));
219 bzero(last.dinfo, sizeof(struct devinfo));
220 bzero(run.dinfo, sizeof(struct devinfo));
222 if (dsinit(MAXDRIVES, &cur, &last, &run) != 1)
226 if (sysctlbyname("hw.intrnames", NULL, &bytes, NULL, 0) == 0) {
227 intrnamebuf = malloc(bytes);
228 sysctlbyname("hw.intrnames", intrnamebuf, &bytes,
230 for (i = 0; i < bytes; ++i) {
231 if (intrnamebuf[i] == 0)
234 intrname = malloc(nintr * sizeof(char *));
235 intrloc = malloc(nintr * sizeof(*intrloc));
237 for (b = i = 0; i < bytes; ++i) {
238 if (intrnamebuf[i] == 0) {
239 intrname[nintr] = intrnamebuf + b;
246 nextintsrow = INTSROW + 2;
262 static int d_first = -1;
265 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
268 tp = localtime(&now);
269 (void) strftime(buf, sizeof(buf),
270 d_first ? "%e %b %R" : "%b %e %R", tp);
280 mvprintw(STATROW, STATCOL + 4, "users Load");
281 mvprintw(MEMROW, MEMCOL, "Mem:MB REAL VIRTUAL");
282 mvprintw(MEMROW + 1, MEMCOL, " Tot Share Tot Share");
283 mvprintw(MEMROW + 2, MEMCOL, "Act");
284 mvprintw(MEMROW + 3, MEMCOL, "All");
286 mvprintw(MEMROW + 1, MEMCOL + 41, "Free");
288 mvprintw(PAGEROW, PAGECOL, " VN PAGER SWAP PAGER ");
289 mvprintw(PAGEROW + 1, PAGECOL, " in out in out ");
290 mvprintw(PAGEROW + 2, PAGECOL, "count");
291 mvprintw(PAGEROW + 3, PAGECOL, "pages");
293 mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
294 mvprintw(INTSROW + 1, INTSCOL + 9, "total");
296 mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "cow");
297 mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "wire");
298 mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "act");
299 mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "inact");
300 mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "cache");
301 mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "free");
302 mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "daefr");
303 mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "prcfr");
304 mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "react");
305 mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "pdwake");
306 mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "pdpgs");
307 mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "intrn");
308 mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "buf");
309 mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "dirtybuf");
311 mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "desiredvnodes");
312 mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "numvnodes");
313 mvprintw(VMSTATROW + 17, VMSTATCOL + 10, "freevnodes");
315 mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt");
317 mvprintw(GRAPHROW, GRAPHCOL,
318 " . %%Sys . %%Intr . %%User . %%Nice . %%Idle");
319 mvprintw(PROCSROW, PROCSCOL, " r p d s w");
320 mvprintw(GRAPHROW + 1, GRAPHCOL,
321 "| | | | | | | | | | |");
323 mvprintw(NAMEIROW, NAMEICOL, "Path-lookups hits %% Components");
324 mvprintw(DISKROW, DISKCOL, "Disks");
325 mvprintw(DISKROW + 1, DISKCOL, "KB/t");
326 mvprintw(DISKROW + 2, DISKCOL, "tpr/s");
327 mvprintw(DISKROW + 3, DISKCOL, "MBr/s");
328 mvprintw(DISKROW + 4, DISKCOL, "tpw/s");
329 mvprintw(DISKROW + 5, DISKCOL, "MBw/s");
330 mvprintw(DISKROW + 6, DISKCOL, "%% busy");
332 * For now, we don't support a fourth disk statistic. So there's
333 * no point in providing a label for it. If someone can think of a
334 * fourth useful disk statistic, there is room to add it.
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, "ozfod");
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 putlong((long)((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)
408 static int failcnt = 0;
412 CP_UPDATE(cp_time.cp_user);
413 CP_UPDATE(cp_time.cp_nice);
414 CP_UPDATE(cp_time.cp_sys);
415 CP_UPDATE(cp_time.cp_intr);
416 CP_UPDATE(cp_time.cp_idle);
419 if (total_time == 0.0)
422 if (etime < 100000.0) { /* < 100ms ignore this trash */
423 if (failcnt++ >= MAXFAIL) {
425 mvprintw(2, 10, "The alternate system clock has died!");
426 mvprintw(3, 10, "Reverting to ``pigs'' display.");
441 for (i = 0; i < nintr; i++) {
442 if (s.intrcnt[i] == 0)
444 if (intrloc[i] == 0) {
445 if (nextintsrow == LINES)
447 intrloc[i] = nextintsrow++;
448 mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s",
452 l = (long)((float)s.intrcnt[i]/etime + 0.5);
454 putlong(l, intrloc[i], INTSCOL + 2, 6, 'D');
456 putlong(inttotal, INTSROW + 1, INTSCOL + 2, 6, 'D');
457 Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
458 Z(ncs_longhits); Z(ncs_longmiss); Z(ncs_neghits);
459 s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
460 nchtotal.ncs_miss + nchtotal.ncs_neghits;
461 s.nchpathcount = nchtotal.ncs_longhits + nchtotal.ncs_longmiss;
463 s1.nchcount = s.nchcount;
464 s1.nchpathcount = s.nchpathcount;
469 for (lc = 0; lc < CPUSTATES; lc++) {
470 uint64_t val = *(uint64_t *)(((uint8_t *)&s.cp_time) +
472 f1 = 100.0 * val / total_time;
474 l = (int) ((f2 + 1.0) / 2.0) - psiz;
476 f1 = 99.9; /* no room to display 100.0 */
477 putfloat(f1, GRAPHROW, GRAPHCOL + 10 * lc, 4, 1, 0);
478 move(GRAPHROW + 2, psiz);
484 putlong(ucount(), STATROW, STATCOL, 3, 'D');
485 putfloat(avenrun[0], STATROW, STATCOL + 18, 6, 2, 0);
486 putfloat(avenrun[1], STATROW, STATCOL + 25, 6, 2, 0);
487 putfloat(avenrun[2], STATROW, STATCOL + 32, 6, 2, 0);
488 mvaddstr(STATROW, STATCOL + 53, buf);
489 #define pgtokb(pg) (long)((intmax_t)(pg) * vms.v_page_size / 1024)
490 #define pgtomb(pg) (long)((intmax_t)(pg) * vms.v_page_size / (1024 * 1024))
491 putlong(pgtomb(total.t_arm), MEMROW + 2, MEMCOL + 3, 8, 'M');
492 putlong(pgtomb(total.t_armshr), MEMROW + 2, MEMCOL + 11, 8, 'M');
493 putlong(pgtomb(total.t_avm), MEMROW + 2, MEMCOL + 19, 9, 'M');
494 putlong(pgtomb(total.t_avmshr), MEMROW + 2, MEMCOL + 28, 9, 'M');
495 putlong(pgtomb(total.t_rm), MEMROW + 3, MEMCOL + 3, 8, 'M');
496 putlong(pgtomb(total.t_rmshr), MEMROW + 3, MEMCOL + 11, 8, 'M');
497 putlong(pgtomb(total.t_vm), MEMROW + 3, MEMCOL + 19, 9, 'M');
498 putlong(pgtomb(total.t_vmshr), MEMROW + 3, MEMCOL + 28, 9, 'M');
499 putlong(pgtomb(total.t_free), MEMROW + 2, MEMCOL + 37, 8, 'M');
500 putlong(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 0, 3, 'D');
501 putlong(total.t_pw, PROCSROW + 1, PROCSCOL + 3, 3, 'D');
502 putlong(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3, 'D');
503 putlong(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3, 'D');
504 putlong(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3, 'D');
505 if (extended_vm_stats == 0) {
506 PUTRATE(Vmm.v_zfod, VMSTATROW + 0, VMSTATCOL + 4, 5);
508 PUTRATE(Vmm.v_cow_faults, VMSTATROW + 1, VMSTATCOL + 3, 6);
509 putlong(pgtokb(vms.v_wire_count), VMSTATROW + 2, VMSTATCOL, 9, 'K');
510 putlong(pgtokb(vms.v_active_count), VMSTATROW + 3, VMSTATCOL, 9, 'K');
511 putlong(pgtokb(vms.v_inactive_count), VMSTATROW + 4, VMSTATCOL, 9, 'K');
512 putlong(pgtokb(vms.v_cache_count), VMSTATROW + 5, VMSTATCOL, 9, 'K');
513 putlong(pgtokb(vms.v_free_count), VMSTATROW + 6, VMSTATCOL, 9, 'K');
514 PUTRATE(Vmm.v_dfree, VMSTATROW + 7, VMSTATCOL, 9);
515 PUTRATE(Vmm.v_pfree, VMSTATROW + 8, VMSTATCOL, 9);
516 PUTRATE(Vmm.v_reactivated, VMSTATROW + 9, VMSTATCOL, 9);
517 PUTRATE(Vmm.v_pdwakeups, VMSTATROW + 10, VMSTATCOL, 9);
518 PUTRATE(Vmm.v_pdpages, VMSTATROW + 11, VMSTATCOL, 9);
519 PUTRATE(Vmm.v_intrans, VMSTATROW + 12, VMSTATCOL, 9);
521 if (extended_vm_stats) {
522 PUTRATE(Vmm.v_zfod, VMSTATROW + 11, VMSTATCOL - 16, 9);
523 PUTRATE(Vmm.v_ozfod, VMSTATROW + 12, VMSTATCOL - 16, 9);
524 #define nz(x) ((x) ? (x) : 1)
525 putlong((s.Vmm.v_zfod - s.Vmm.v_ozfod) * 100 / nz(s.Vmm.v_zfod),
526 VMSTATROW + 13, VMSTATCOL - 16, 9, 'D');
528 PUTRATE(Vmm.v_tfree, VMSTATROW + 14, VMSTATCOL - 16, 9);
531 putlong(s.bufspace/1024, VMSTATROW + 13, VMSTATCOL, 9, 'K');
532 putlong(s.dirtybufspace/1024, VMSTATROW + 14, VMSTATCOL, 9, 'K');
533 putlong(s.desiredvnodes, VMSTATROW + 15, VMSTATCOL, 9, 'D');
534 putlong(s.numvnodes, VMSTATROW + 16, VMSTATCOL, 9, 'D');
535 putlong(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 #define nz(x) ((x) ? (x) : 1)
571 putlong(s.nchpathcount, NAMEIROW + 1, NAMEICOL + 3, 9, 'D');
573 putlong(nchtotal.ncs_longhits, NAMEIROW + 1, NAMEICOL + 12, 7, 'D');
574 putfloat(nchtotal.ncs_longhits * 100.0 / nz(s.nchpathcount),
575 NAMEIROW + 1, NAMEICOL + 19, 4, 0, 0);
577 putfloat((double)s.nchcount / nz(s.nchpathcount),
578 NAMEIROW + 1, NAMEICOL + 27, 5, 2, 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 putlong(long n, int l, int lc, int w, int type)
673 snprintf(b, sizeof(b), "%*ld", w, n);
674 if (strlen(b) > (size_t)w) {
682 snprintf(b, sizeof(b), "%*ld%c", w - 1, n, xtype);
683 if (strlen(b) > (size_t)w) {
691 snprintf(b, sizeof(b), "%*ld%c", w - 1, n, xtype);
692 if (strlen(b) > (size_t)w) {
703 putfloat(double f, int l, int lc, int w, int d, int nz)
708 if (nz && f == 0.0) {
713 snprintf(b, sizeof(b), "%*.*f", w, d, f);
714 if (strlen(b) > (size_t)w)
715 snprintf(b, sizeof(b), "%*.0f", w, f);
716 if (strlen(b) > (size_t)w) {
725 putlongdouble(long double f, int l, int lc, int w, int d, int nz)
730 if (nz && f == 0.0) {
735 sprintf(b, "%*.*Lf", w, d, f);
736 if (strlen(b) > (size_t)w)
737 sprintf(b, "%*.0Lf", w, f);
738 if (strlen(b) > (size_t)w) {
747 putlongdoublez(long double f, int l, int lc, int w, int d, int nz)
753 sprintf(b, "%*.*s", w, w, "");
756 putlongdouble(f, l, lc, w, d, nz);
761 getinfo(struct Info *ls)
763 struct devinfo *tmp_dinfo;
764 struct nchstats *nch_tmp;
766 size_t vms_size = sizeof(ls->Vms);
767 size_t vmm_size = sizeof(ls->Vmm);
768 size_t nch_size = sizeof(ls->nchstats) * SMP_MAXCPU;
770 if (sysctlbyname("vm.vmstats", &ls->Vms, &vms_size, NULL, 0)) {
771 perror("sysctlbyname: vm.vmstats");
774 if (sysctlbyname("vm.vmmeter", &ls->Vmm, &vmm_size, NULL, 0)) {
775 perror("sysctlbyname: vm.vmstats");
779 if (kinfo_get_sched_cputime(&ls->cp_time))
780 err(1, "kinfo_get_sched_cputime");
781 if (kinfo_get_sched_cputime(&cp_time))
782 err(1, "kinfo_get_sched_cputime");
783 NREAD(X_BUFFERSPACE, &ls->bufspace, sizeof(ls->bufspace));
784 NREAD(X_DESIREDVNODES, &ls->desiredvnodes, sizeof(ls->desiredvnodes));
785 NREAD(X_NUMVNODES, &ls->numvnodes, sizeof(ls->numvnodes));
786 NREAD(X_FREEVNODES, &ls->freevnodes, sizeof(ls->freevnodes));
787 NREAD(X_NUMDIRTYBUFFERS, &ls->dirtybufspace, sizeof(ls->dirtybufspace));
790 size = nintr * sizeof(ls->intrcnt[0]);
791 sysctlbyname("hw.intrcnt", ls->intrcnt, &size, NULL, 0);
793 size = sizeof(ls->Total);
794 if (sysctlbyname("vm.vmtotal", &ls->Total, &size, NULL, 0) < 0) {
795 error("Can't get kernel info: %s\n", strerror(errno));
796 bzero(&ls->Total, sizeof(ls->Total));
799 if ((nch_tmp = malloc(nch_size)) == NULL) {
803 if (sysctlbyname("vfs.cache.nchstats", nch_tmp, &nch_size, NULL, 0)) {
804 perror("sysctlbyname vfs.cache.nchstats");
808 if ((nch_tmp = realloc(nch_tmp, nch_size)) == NULL) {
815 if (kinfo_get_cpus(&ncpu))
816 err(1, "kinfo_get_cpus");
817 kvm_nch_cpuagg(nch_tmp, &ls->nchstats, ncpu);
820 tmp_dinfo = last.dinfo;
821 last.dinfo = cur.dinfo;
822 cur.dinfo = tmp_dinfo;
824 last.busy_time = cur.busy_time;
825 switch (getdevs(&cur)) {
827 errx(1, "%s", devstat_errbuf);
830 num_devices = cur.dinfo->numdevs;
831 generation = cur.dinfo->generation;
832 cmdkre("refresh", NULL);
840 allocinfo(struct Info *ls)
842 ls->intrcnt = (long *) calloc(nintr, sizeof(long));
843 if (ls->intrcnt == NULL)
844 errx(2, "out of memory");
848 copyinfo(struct Info *from, struct Info *to)
853 * time, wds, seek, and xfer are malloc'd so we have to
854 * save the pointers before the structure copy and then
857 intrcnt = to->intrcnt;
860 bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
864 dinfo(int dn, int lc, struct statinfo *now, struct statinfo *then)
866 long double kb_per_transfer;
867 long double transfers_per_secondr;
868 long double transfers_per_secondw;
869 long double mb_per_secondr;
870 long double mb_per_secondw;
871 long double elapsed_time, device_busy;
874 di = dev_select[dn].position;
876 elapsed_time = compute_etime(now->busy_time, then ?
878 now->dinfo->devices[di].dev_creation_time);
880 device_busy = compute_etime(now->dinfo->devices[di].busy_time, then ?
881 then->dinfo->devices[di].busy_time :
882 now->dinfo->devices[di].dev_creation_time);
885 &now->dinfo->devices[di],
886 (then ? &then->dinfo->devices[di] : NULL),
893 errx(1, "%s", devstat_errbuf);
895 if (compute_stats_read(
896 &now->dinfo->devices[di],
897 (then ? &then->dinfo->devices[di] : NULL),
901 &transfers_per_secondr,
904 errx(1, "%s", devstat_errbuf);
906 if (compute_stats_write(
907 &now->dinfo->devices[di],
908 (then ? &then->dinfo->devices[di] : NULL),
912 &transfers_per_secondw,
915 errx(1, "%s", devstat_errbuf);
917 if ((device_busy == 0) &&
918 (transfers_per_secondr > 5 || transfers_per_secondw > 5)) {
919 /* the device has been 100% busy, fake it because
920 * as long as the device is 100% busy the busy_time
921 * field in the devstat struct is not updated */
922 device_busy = elapsed_time;
924 if (device_busy > elapsed_time) {
925 /* this normally happens after one or more periods
926 * where the device has been 100% busy, correct it */
927 device_busy = elapsed_time;
930 lc = DISKCOL + lc * 6;
931 putlongdoublez(kb_per_transfer, DISKROW + 1, lc, 5, 2, 0);
932 putlongdoublez(transfers_per_secondr, DISKROW + 2, lc, 5, 0, 0);
933 putlongdoublez(mb_per_secondr, DISKROW + 3, lc, 5, 2, 0);
934 putlongdoublez(transfers_per_secondw, DISKROW + 4, lc, 5, 0, 0);
935 putlongdoublez(mb_per_secondw, DISKROW + 5, lc, 5, 2, 0);
936 putlongdouble(device_busy * 100 / elapsed_time,
937 DISKROW + 6, lc, 5, 0, 0);