2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1989, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 static const char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 1/12/94";
41 * Cursed vmstat -- from Robert Elz.
44 #include <sys/param.h>
49 #include <sys/namei.h>
50 #include <sys/resource.h>
51 #include <sys/sysctl.h>
52 #include <sys/vmmeter.h>
54 #include <vm/vm_param.h>
76 uint64_t v_swtch; /* context switches */
77 uint64_t v_trap; /* calls to trap */
78 uint64_t v_syscall; /* calls to syscall() */
79 uint64_t v_intr; /* device interrupts */
80 uint64_t v_soft; /* software interrupts */
82 * Virtual memory activity.
84 uint64_t v_vm_faults; /* number of address memory faults */
85 uint64_t v_io_faults; /* page faults requiring I/O */
86 uint64_t v_cow_faults; /* number of copy-on-writes */
87 uint64_t v_zfod; /* pages zero filled on demand */
88 uint64_t v_ozfod; /* optimized zero fill pages */
89 uint64_t v_swapin; /* swap pager pageins */
90 uint64_t v_swapout; /* swap pager pageouts */
91 uint64_t v_swappgsin; /* swap pager pages paged in */
92 uint64_t v_swappgsout; /* swap pager pages paged out */
93 uint64_t v_vnodein; /* vnode pager pageins */
94 uint64_t v_vnodeout; /* vnode pager pageouts */
95 uint64_t v_vnodepgsin; /* vnode_pager pages paged in */
96 uint64_t v_vnodepgsout; /* vnode pager pages paged out */
97 uint64_t v_intrans; /* intransit blocking page faults */
98 uint64_t v_reactivated; /* number of pages reactivated by pagedaemon */
99 uint64_t v_pdwakeups; /* number of times daemon has awaken from sleep */
100 uint64_t v_pdpages; /* number of pages analyzed by daemon */
102 uint64_t v_dfree; /* pages freed by daemon */
103 uint64_t v_pfree; /* pages freed by exiting processes */
104 uint64_t v_tfree; /* total pages freed */
106 * Distribution of page usages.
108 u_int v_page_size; /* page size in bytes */
109 u_int v_free_count; /* number of pages free */
110 u_int v_wire_count; /* number of pages wired down */
111 u_int v_active_count; /* number of pages active */
112 u_int v_inactive_count; /* number of pages inactive */
113 u_int v_laundry_count; /* number of pages in laundry queue */
114 u_long v_kmem_map_size; /* Current kmem allocation size */
115 struct vmtotal Total;
116 struct nchstats nchstats;
125 static u_long kmem_size;
126 static u_int v_page_count;
128 struct statinfo cur, last, run;
130 #define total s.Total
131 #define nchtotal s.nchstats
132 #define oldnchtotal s1.nchstats
134 static enum state { BOOT, TIME, RUN } state = TIME;
135 enum divisor { IEC = 0, SI = HN_DIVISOR_1000 };
137 static void allocinfo(struct Info *);
138 static void copyinfo(struct Info *, struct Info *);
139 static float cputime(int);
140 static void dinfo(int, int, struct statinfo *, struct statinfo *);
141 static void do_putuint64(uint64_t, int, int, int, int);
142 static void getinfo(struct Info *);
143 static void putint(int, int, int, int);
144 static void putuint64(uint64_t, int, int, int);
145 static void putfloat(double, int, int, int, int, int);
146 static void putlongdouble(long double, int, int, int, int, int);
147 static int ucount(void);
154 static long *intrloc;
155 static char **intrname;
156 static int nextintsrow;
176 * These constants define where the major pieces are laid out
178 #define STATROW 0 /* uses 1 row and 67 cols */
180 #define MEMROW 2 /* uses 4 rows and 45 cols */
182 #define PAGEROW 2 /* uses 4 rows and 30 cols */
184 #define INTSROW 6 /* uses all rows to bottom and 16 cols */
186 #define PROCSROW 6 /* uses 3 rows and 19 cols */
188 #define GENSTATROW 7 /* uses 2 rows and 29 cols */
189 #define GENSTATCOL 21
190 #define VMSTATROW 7 /* uses 17 rows and 12-14 cols */
191 #define VMSTATCOL 49 /* actually 50-51 for some fields */
192 #define GRAPHROW 10 /* uses 3 rows and 49-51 cols */
194 #define VNSTATROW 13 /* uses 4 rows and 13 columns */
196 #define NAMEIROW 14 /* uses 3 rows and 32 cols */
198 #define DISKROW 18 /* uses 5 rows and 47 cols (for 7 drives) */
201 #define DRIVESPACE 7 /* max # for space */
203 #define MAXDRIVES DRIVESPACE /* max # to display */
208 char *cp, *cp1, *cp2, *intrnamebuf, *nextcp;
212 if ((num_devices = devstat_getnumdevs(NULL)) < 0) {
213 warnx("%s", devstat_errbuf);
217 cur.dinfo = calloc(1, sizeof(struct devinfo));
218 last.dinfo = calloc(1, sizeof(struct devinfo));
219 run.dinfo = calloc(1, sizeof(struct devinfo));
221 if (dsinit(MAXDRIVES, &cur, &last, &run) != 1)
225 if (sysctlbyname("hw.intrcnt", NULL, &sz, NULL, 0) == -1) {
226 error("sysctl(hw.intrcnt...) failed: %s",
230 nintr = sz / sizeof(u_long);
231 intrloc = calloc(nintr, sizeof (long));
232 intrname = calloc(nintr, sizeof (char *));
233 intrnamebuf = sysctl_dynread("hw.intrnames", NULL);
234 if (intrnamebuf == NULL || intrname == NULL ||
236 error("Out of memory");
246 for (cp = intrnamebuf, i = 0; i < nintr; i++) {
247 nextcp = cp + strlen(cp) + 1;
249 /* Discard trailing spaces. */
250 for (cp1 = nextcp - 1; cp1 > cp && *(cp1 - 1) == ' '; )
253 /* Convert "irqN: name" to "name irqN". */
254 if (strncmp(cp, "irq", 3) == 0) {
256 while (isdigit((u_char)*cp1))
258 if (cp1 != cp && *cp1 == ':' &&
264 bcopy(cp1, cp, sz - (cp1 - cp) + 1);
274 * Convert "name irqN" to "name N" if the former is
275 * longer than the field width.
277 if ((cp1 = strstr(cp, "irq")) != NULL &&
279 bcopy(cp1 + 3, cp1, strlen(cp1 + 3) + 1);
284 nextintsrow = INTSROW + 2;
290 GETSYSCTL("vm.kmem_size", kmem_size);
291 GETSYSCTL("vm.stats.vm.v_page_count", v_page_count);
302 static int d_first = -1;
305 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
308 tp = localtime(&now);
309 (void) strftime(buf, sizeof(buf),
310 d_first ? "%e %b %R" : "%b %e %R", tp);
320 mvprintw(STATROW, STATCOL + 6, "users Load");
321 mvprintw(STATROW + 1, STATCOL + 3, "Mem usage: %%Phy %%Kmem");
322 mvprintw(MEMROW, MEMCOL, "Mem: KB REAL VIRTUAL");
323 mvprintw(MEMROW + 1, MEMCOL, " Tot Share Tot Share");
324 mvprintw(MEMROW + 2, MEMCOL, "Act");
325 mvprintw(MEMROW + 3, MEMCOL, "All");
327 mvprintw(MEMROW + 1, MEMCOL + 41, "Free");
329 mvprintw(PAGEROW, PAGECOL, " VN PAGER SWAP PAGER");
330 mvprintw(PAGEROW + 1, PAGECOL, " in out in out");
331 mvprintw(PAGEROW + 2, PAGECOL, "count");
332 mvprintw(PAGEROW + 3, PAGECOL, "pages");
334 mvprintw(INTSROW, INTSCOL + 1, "Interrupts");
335 mvprintw(INTSROW + 1, INTSCOL + 6, "total");
337 mvprintw(VMSTATROW, VMSTATCOL + 9, "ioflt");
338 mvprintw(VMSTATROW + 1, VMSTATCOL + 9, "cow");
339 mvprintw(VMSTATROW + 2, VMSTATCOL + 9, "zfod");
340 mvprintw(VMSTATROW + 3, VMSTATCOL + 9, "ozfod");
341 mvprintw(VMSTATROW + 4, VMSTATCOL + 9 - 1, "%%ozfod");
342 mvprintw(VMSTATROW + 5, VMSTATCOL + 9, "daefr");
343 mvprintw(VMSTATROW + 6, VMSTATCOL + 9, "prcfr");
344 mvprintw(VMSTATROW + 7, VMSTATCOL + 9, "totfr");
345 mvprintw(VMSTATROW + 8, VMSTATCOL + 9, "react");
346 mvprintw(VMSTATROW + 9, VMSTATCOL + 9, "pdwak");
347 mvprintw(VMSTATROW + 10, VMSTATCOL + 9, "pdpgs");
348 mvprintw(VMSTATROW + 11, VMSTATCOL + 9, "intrn");
349 mvprintw(VMSTATROW + 12, VMSTATCOL + 9, "wire");
350 mvprintw(VMSTATROW + 13, VMSTATCOL + 9, "act");
351 mvprintw(VMSTATROW + 14, VMSTATCOL + 9, "inact");
352 mvprintw(VMSTATROW + 15, VMSTATCOL + 9, "laund");
353 mvprintw(VMSTATROW + 16, VMSTATCOL + 9, "free");
354 if (LINES - 1 > VMSTATROW + 17)
355 mvprintw(VMSTATROW + 17, VMSTATCOL + 9, "buf");
357 mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt");
359 mvprintw(GRAPHROW, GRAPHCOL,
360 " . %%Sys . %%Intr . %%User . %%Nice . %%Idle");
361 mvprintw(PROCSROW, PROCSCOL, "Proc:");
362 mvprintw(PROCSROW + 1, PROCSCOL, " r p d s w");
363 mvprintw(GRAPHROW + 1, GRAPHCOL,
364 "| | | | | | | | | | |");
366 mvprintw(VNSTATROW, VNSTATCOL + 8, "dtbuf");
367 mvprintw(VNSTATROW + 1, VNSTATCOL + 8, "desvn");
368 mvprintw(VNSTATROW + 2, VNSTATCOL + 8, "numvn");
369 mvprintw(VNSTATROW + 3, VNSTATCOL + 8, "frevn");
371 mvprintw(NAMEIROW, NAMEICOL, "Namei Name-cache Dir-cache");
372 mvprintw(NAMEIROW + 1, NAMEICOL,
373 " Calls hits %% hits %%");
374 mvprintw(DISKROW, DISKCOL, "Disks");
375 mvprintw(DISKROW + 1, DISKCOL, "KB/t");
376 mvprintw(DISKROW + 2, DISKCOL, "tps");
377 mvprintw(DISKROW + 3, DISKCOL, "MB/s");
378 mvprintw(DISKROW + 4, DISKCOL, "%%busy");
380 * For now, we don't support a fourth disk statistic. So there's
381 * no point in providing a label for it. If someone can think of a
382 * fourth useful disk statistic, there is room to add it.
384 /* mvprintw(DISKROW + 4, DISKCOL, " msps"); */
386 for (i = 0; i < num_devices && j < MAXDRIVES; i++)
387 if (dev_select[i].selected) {
389 sprintf(tmpstr, "%s%d", dev_select[i].device_name,
390 dev_select[i].unit_number);
391 mvprintw(DISKROW, DISKCOL + 5 + 6 * j,
396 for (i = 0; i < nintr; i++) {
399 mvprintw(intrloc[i], INTSCOL + 6, "%-10.10s", intrname[i]);
403 #define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
404 #define Q(fld) {t=cur.fld[i]; cur.fld[i]-=last.fld[i]; if(state==TIME) last.fld[i]=t;}
405 #define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
406 #define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
407 if(state == TIME) s1.nchstats.fld = t;}
408 #define PUTRATE(fld, l, c, w) \
411 putint((int)((float)s.fld/etime + 0.5), l, c, w); \
415 static char cpuchar[CPUSTATES] = { '=' , '+', '>', '-', ' ' };
416 static char cpuorder[CPUSTATES] = { CP_SYS, CP_INTR, CP_USER, CP_NICE,
425 static int failcnt = 0;
428 for(i = 0; i < CPUSTATES; i++) {
433 if (etime < 5.0) { /* < 5 ticks - ignore this trash */
434 if (failcnt++ >= MAXFAIL) {
436 mvprintw(2, 10, "The alternate system clock has died!");
437 mvprintw(3, 10, "Reverting to ``pigs'' display.");
450 for (i = 0; i < nintr; i++) {
451 if (s.intrcnt[i] == 0)
454 l = (int)((float)s.intrcnt[i]/etime + 0.5);
456 if (intrloc[i] == 0) {
457 if (nextintsrow == LINES)
459 intrloc[i] = nextintsrow++;
460 mvprintw(intrloc[i], INTSCOL + 6, "%-10.10s",
463 putint(l, intrloc[i], INTSCOL, 5);
465 putint(inttotal, INTSROW + 1, INTSCOL, 5);
466 Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
467 Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes); Z(ncs_neghits);
468 s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
469 nchtotal.ncs_miss + nchtotal.ncs_long + nchtotal.ncs_neghits;
471 s1.nchcount = s.nchcount;
475 for (lc = 0; lc < CPUSTATES; lc++) {
479 l = (int) ((f2 + 1.0) / 2.0) - psiz;
480 putfloat(f1, GRAPHROW, GRAPHCOL + 10 * lc, 4, 1, 0);
481 move(GRAPHROW + 2, psiz);
487 putint(ucount(), STATROW, STATCOL, 5);
488 putfloat(avenrun[0], STATROW, STATCOL + 20, 5, 2, 0);
489 putfloat(avenrun[1], STATROW, STATCOL + 26, 5, 2, 0);
490 putfloat(avenrun[2], STATROW, STATCOL + 32, 5, 2, 0);
491 mvaddstr(STATROW, STATCOL + 55, buf);
492 #define pgtokb(pg) ((pg) * (s.v_page_size / 1024))
493 putfloat(100.0 * (v_page_count - total.t_free) / v_page_count,
494 STATROW + 1, STATCOL + 15, 2, 0, 1);
495 putfloat(100.0 * s.v_kmem_map_size / kmem_size,
496 STATROW + 1, STATCOL + 22, 2, 0, 1);
498 putuint64(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 4, 7);
499 putuint64(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 12, 7);
500 putuint64(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 20, 8);
501 putuint64(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 29, 8);
502 putuint64(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 4, 7);
503 putuint64(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 12, 7);
504 putuint64(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 20, 8);
505 putuint64(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 29, 8);
506 putuint64(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 38, 7);
507 putint(total.t_rq - 1, PROCSROW + 2, PROCSCOL, 3);
508 putint(total.t_pw, PROCSROW + 2, PROCSCOL + 4, 3);
509 putint(total.t_dw, PROCSROW + 2, PROCSCOL + 8, 3);
510 putint(total.t_sl, PROCSROW + 2, PROCSCOL + 12, 3);
511 putint(total.t_sw, PROCSROW + 2, PROCSCOL + 16, 3);
512 PUTRATE(v_io_faults, VMSTATROW, VMSTATCOL + 2, 8 - 2);
513 PUTRATE(v_cow_faults, VMSTATROW + 1, VMSTATCOL + 2, 8 - 2);
514 PUTRATE(v_zfod, VMSTATROW + 2, VMSTATCOL + 2, 8 - 2);
515 PUTRATE(v_ozfod, VMSTATROW + 3, VMSTATCOL, 8);
516 putint(s.v_zfod != 0 ? (int)(s.v_ozfod * 100.0 / s.v_zfod) : 0,
517 VMSTATROW + 4, VMSTATCOL + 1, 8 - 1);
518 PUTRATE(v_dfree, VMSTATROW + 5, VMSTATCOL + 2, 8 - 2);
519 PUTRATE(v_pfree, VMSTATROW + 6, VMSTATCOL + 2, 8 - 2);
520 PUTRATE(v_tfree, VMSTATROW + 7, VMSTATCOL, 8);
521 PUTRATE(v_reactivated, VMSTATROW + 8, VMSTATCOL, 8);
522 PUTRATE(v_pdwakeups, VMSTATROW + 9, VMSTATCOL, 8);
523 PUTRATE(v_pdpages, VMSTATROW + 10, VMSTATCOL, 8);
524 PUTRATE(v_intrans, VMSTATROW + 11, VMSTATCOL, 8);
525 putuint64(pgtokb(s.v_wire_count), VMSTATROW + 12, VMSTATCOL, 8);
526 putuint64(pgtokb(s.v_active_count), VMSTATROW + 13, VMSTATCOL, 8);
527 putuint64(pgtokb(s.v_inactive_count), VMSTATROW + 14, VMSTATCOL, 8);
528 putuint64(pgtokb(s.v_laundry_count), VMSTATROW + 15, VMSTATCOL, 8);
529 putuint64(pgtokb(s.v_free_count), VMSTATROW + 16, VMSTATCOL, 8);
530 if (LINES - 1 > VMSTATROW + 17)
531 putuint64(s.bufspace / 1024, VMSTATROW + 17, VMSTATCOL, 8);
532 PUTRATE(v_vnodein, PAGEROW + 2, PAGECOL + 6, 5);
533 PUTRATE(v_vnodeout, PAGEROW + 2, PAGECOL + 12, 5);
534 PUTRATE(v_swapin, PAGEROW + 2, PAGECOL + 19, 5);
535 PUTRATE(v_swapout, PAGEROW + 2, PAGECOL + 25, 5);
536 PUTRATE(v_vnodepgsin, PAGEROW + 3, PAGECOL + 6, 5);
537 PUTRATE(v_vnodepgsout, PAGEROW + 3, PAGECOL + 12, 5);
538 PUTRATE(v_swappgsin, PAGEROW + 3, PAGECOL + 19, 5);
539 PUTRATE(v_swappgsout, PAGEROW + 3, PAGECOL + 25, 5);
540 PUTRATE(v_swtch, GENSTATROW + 1, GENSTATCOL, 4);
541 PUTRATE(v_trap, GENSTATROW + 1, GENSTATCOL + 5, 4);
542 PUTRATE(v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 4);
543 PUTRATE(v_intr, GENSTATROW + 1, GENSTATCOL + 15, 4);
544 PUTRATE(v_soft, GENSTATROW + 1, GENSTATCOL + 20, 4);
545 PUTRATE(v_vm_faults, GENSTATROW + 1, GENSTATCOL + 25, 4);
546 for (i = 0, lc = 0; i < num_devices && lc < MAXDRIVES; i++)
547 if (dev_select[i].selected) {
550 dinfo(i, ++lc, &cur, &last);
553 dinfo(i, ++lc, &cur, &run);
556 dinfo(i, ++lc, &cur, NULL);
560 putint(s.numdirtybuffers, VNSTATROW, VNSTATCOL, 7);
561 putint(s.desiredvnodes, VNSTATROW + 1, VNSTATCOL, 7);
562 putint(s.numvnodes, VNSTATROW + 2, VNSTATCOL, 7);
563 putint(s.freevnodes, VNSTATROW + 3, VNSTATCOL, 7);
564 putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 8);
565 putint((nchtotal.ncs_goodhits + nchtotal.ncs_neghits),
566 NAMEIROW + 2, NAMEICOL + 9, 7);
567 #define nz(x) ((x) ? (x) : 1)
568 putfloat((nchtotal.ncs_goodhits+nchtotal.ncs_neghits) *
569 100.0 / nz(s.nchcount),
570 NAMEIROW + 2, NAMEICOL + 17, 3, 0, 1);
571 putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 21, 7);
572 putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
573 NAMEIROW + 2, NAMEICOL + 29, 3, 0, 1);
578 cmdkre(const char *cmd, const char *args)
582 if (prefix(cmd, "run")) {
585 switch (devstat_getdevs(NULL, &run)) {
587 errx(1, "%s", devstat_errbuf);
590 num_devices = run.dinfo->numdevs;
591 generation = run.dinfo->generation;
592 retval = dscmd("refresh", NULL, MAXDRIVES, &cur);
602 if (prefix(cmd, "boot")) {
607 if (prefix(cmd, "time")) {
611 if (prefix(cmd, "zero")) {
615 switch (devstat_getdevs(NULL, &run)) {
617 errx(1, "%s", devstat_errbuf);
620 num_devices = run.dinfo->numdevs;
621 generation = run.dinfo->generation;
622 retval = dscmd("refresh",NULL, MAXDRIVES, &cur);
632 retval = dscmd(cmd, args, MAXDRIVES, &cur);
640 /* calculate number of users on the system */
648 while ((ut = getutxent()) != NULL)
649 if (ut->ut_type == USER_PROCESS)
663 for (i = 0; i < CPUSTATES; i++)
667 return (s.time[indx] * 100.0 / lt);
671 putint(int n, int l, int lc, int w)
674 do_putuint64(n, l, lc, w, SI);
678 putuint64(uint64_t n, int l, int lc, int w)
681 do_putuint64(n, l, lc, w, IEC);
685 do_putuint64(uint64_t n, int l, int lc, int w, int div)
702 snr = snprintf(b, sizeof(b), "%*ju", w, (uintmax_t)n);
704 humanize_number(buf, w, n, "", HN_AUTOSCALE,
705 HN_NOSPACE | HN_DECIMAL | div);
706 snr = snprintf(b, sizeof(b), "%*s", w, buf);
717 putfloat(double f, int l, int lc, int w, int d, int nz)
728 if (nz && f == 0.0) {
733 snr = snprintf(b, sizeof(b), "%*.*f", w, d, f);
735 snr = snprintf(b, sizeof(b), "%*.0f", w, f);
737 snr = snprintf(b, sizeof(b), "%*.0fk", w - 1, f / 1000);
739 snr = snprintf(b, sizeof(b), "%*.0fM", w - 1, f / 1000000);
749 putlongdouble(long double f, int l, int lc, int w, int d, int nz)
760 if (nz && f == 0.0) {
765 snr = snprintf(b, sizeof(b), "%*.*Lf", w, d, f);
767 snr = snprintf(b, sizeof(b), "%*.0Lf", w, f);
769 snr = snprintf(b, sizeof(b), "%*.0Lfk", w - 1, f / 1000);
771 snr = snprintf(b, sizeof(b), "%*.0LfM", w - 1, f / 1000000);
781 getinfo(struct Info *ls)
783 struct devinfo *tmp_dinfo;
787 GETSYSCTL("kern.cp_time", ls->time);
788 GETSYSCTL("kern.cp_time", cur.cp_time);
789 GETSYSCTL("vm.stats.sys.v_swtch", ls->v_swtch);
790 GETSYSCTL("vm.stats.sys.v_trap", ls->v_trap);
791 GETSYSCTL("vm.stats.sys.v_syscall", ls->v_syscall);
792 GETSYSCTL("vm.stats.sys.v_intr", ls->v_intr);
793 GETSYSCTL("vm.stats.sys.v_soft", ls->v_soft);
794 GETSYSCTL("vm.stats.vm.v_vm_faults", ls->v_vm_faults);
795 GETSYSCTL("vm.stats.vm.v_io_faults", ls->v_io_faults);
796 GETSYSCTL("vm.stats.vm.v_cow_faults", ls->v_cow_faults);
797 GETSYSCTL("vm.stats.vm.v_zfod", ls->v_zfod);
798 GETSYSCTL("vm.stats.vm.v_ozfod", ls->v_ozfod);
799 GETSYSCTL("vm.stats.vm.v_swapin", ls->v_swapin);
800 GETSYSCTL("vm.stats.vm.v_swapout", ls->v_swapout);
801 GETSYSCTL("vm.stats.vm.v_swappgsin", ls->v_swappgsin);
802 GETSYSCTL("vm.stats.vm.v_swappgsout", ls->v_swappgsout);
803 GETSYSCTL("vm.stats.vm.v_vnodein", ls->v_vnodein);
804 GETSYSCTL("vm.stats.vm.v_vnodeout", ls->v_vnodeout);
805 GETSYSCTL("vm.stats.vm.v_vnodepgsin", ls->v_vnodepgsin);
806 GETSYSCTL("vm.stats.vm.v_vnodepgsout", ls->v_vnodepgsout);
807 GETSYSCTL("vm.stats.vm.v_intrans", ls->v_intrans);
808 GETSYSCTL("vm.stats.vm.v_reactivated", ls->v_reactivated);
809 GETSYSCTL("vm.stats.vm.v_pdwakeups", ls->v_pdwakeups);
810 GETSYSCTL("vm.stats.vm.v_pdpages", ls->v_pdpages);
811 GETSYSCTL("vm.stats.vm.v_dfree", ls->v_dfree);
812 GETSYSCTL("vm.stats.vm.v_pfree", ls->v_pfree);
813 GETSYSCTL("vm.stats.vm.v_tfree", ls->v_tfree);
814 GETSYSCTL("vm.stats.vm.v_page_size", ls->v_page_size);
815 GETSYSCTL("vm.stats.vm.v_free_count", ls->v_free_count);
816 GETSYSCTL("vm.stats.vm.v_wire_count", ls->v_wire_count);
817 GETSYSCTL("vm.stats.vm.v_active_count", ls->v_active_count);
818 GETSYSCTL("vm.stats.vm.v_inactive_count", ls->v_inactive_count);
819 GETSYSCTL("vm.stats.vm.v_laundry_count", ls->v_laundry_count);
820 GETSYSCTL("vfs.bufspace", ls->bufspace);
821 GETSYSCTL("kern.maxvnodes", ls->desiredvnodes);
822 GETSYSCTL("vfs.numvnodes", ls->numvnodes);
823 GETSYSCTL("vfs.freevnodes", ls->freevnodes);
824 GETSYSCTL("vfs.cache.nchstats", ls->nchstats);
825 GETSYSCTL("vfs.numdirtybuffers", ls->numdirtybuffers);
826 GETSYSCTL("vm.kmem_map_size", ls->v_kmem_map_size);
827 getsysctl("hw.intrcnt", ls->intrcnt, nintr * sizeof(u_long));
829 size = sizeof(ls->Total);
832 if (sysctl(mib, 2, &ls->Total, &size, NULL, 0) < 0) {
833 error("Can't get kernel info: %s\n", strerror(errno));
834 bzero(&ls->Total, sizeof(ls->Total));
837 if (sysctlbyname("hw.ncpu", &ncpu, &size, NULL, 0) < 0 ||
838 size != sizeof(ncpu))
841 tmp_dinfo = last.dinfo;
842 last.dinfo = cur.dinfo;
843 cur.dinfo = tmp_dinfo;
845 last.snap_time = cur.snap_time;
846 switch (devstat_getdevs(NULL, &cur)) {
848 errx(1, "%s", devstat_errbuf);
851 num_devices = cur.dinfo->numdevs;
852 generation = cur.dinfo->generation;
853 cmdkre("refresh", NULL);
861 allocinfo(struct Info *ls)
864 ls->intrcnt = (long *) calloc(nintr, sizeof(long));
865 if (ls->intrcnt == NULL)
866 errx(2, "out of memory");
870 copyinfo(struct Info *from, struct Info *to)
875 * time, wds, seek, and xfer are malloc'd so we have to
876 * save the pointers before the structure copy and then
879 intrcnt = to->intrcnt;
882 bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
886 dinfo(int dn, int lc, struct statinfo *now, struct statinfo *then)
888 long double transfers_per_second;
889 long double kb_per_transfer, mb_per_second;
890 long double elapsed_time, device_busy;
893 di = dev_select[dn].position;
896 /* Calculate relative to previous sample */
897 elapsed_time = now->snap_time - then->snap_time;
899 /* Calculate relative to device creation */
900 elapsed_time = now->snap_time - devstat_compute_etime(
901 &now->dinfo->devices[di].creation_time, NULL);
904 if (devstat_compute_statistics(&now->dinfo->devices[di], then ?
905 &then->dinfo->devices[di] : NULL, elapsed_time,
906 DSM_KB_PER_TRANSFER, &kb_per_transfer,
907 DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
908 DSM_MB_PER_SECOND, &mb_per_second,
909 DSM_BUSY_PCT, &device_busy,
911 errx(1, "%s", devstat_errbuf);
913 lc = DISKCOL + lc * 6;
914 putlongdouble(kb_per_transfer, DISKROW + 1, lc, 5, 2, 0);
915 putlongdouble(transfers_per_second, DISKROW + 2, lc, 5, 0, 0);
916 putlongdouble(mb_per_second, DISKROW + 3, lc, 5, 2, 0);
917 putlongdouble(device_busy, DISKROW + 4, lc, 5, 0, 0);