1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1983, 1989, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33
34 __FBSDID("$FreeBSD$");
35
36 #ifdef lint
37 static const char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 1/12/94";
38 #endif
39
40 /*
41 * Cursed vmstat -- from Robert Elz.
42 */
43
44 #include <sys/param.h>
45 #include <sys/stat.h>
46 #include <sys/time.h>
47 #include <sys/proc.h>
48 #include <sys/uio.h>
49 #include <sys/namei.h>
50 #include <sys/resource.h>
51 #include <sys/sysctl.h>
52 #include <sys/vmmeter.h>
53
54 #include <vm/vm_param.h>
55
56 #include <ctype.h>
57 #include <err.h>
58 #include <errno.h>
59 #include <langinfo.h>
60 #include <libutil.h>
61 #include <nlist.h>
62 #include <paths.h>
63 #include <signal.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <time.h>
67 #include <unistd.h>
68 #include <utmpx.h>
69 #include <devstat.h>
70 #include "systat.h"
71 #include "extern.h"
72 #include "devs.h"
73
74 static struct Info {
75 long time[CPUSTATES];
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 */
81 /*
82 * Virtual memory activity.
83 */
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 */
101
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 */
105 /*
106 * Distribution of page usages.
107 */
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;
117 long nchcount;
118 long *intrcnt;
119 long bufspace;
120 int desiredvnodes;
121 long numvnodes;
122 long freevnodes;
123 int numdirtybuffers;
124 } s, s1, s2, z;
125 static u_long kmem_size;
126 static u_int v_page_count;
127
128 struct statinfo cur, last, run;
129
130 #define total s.Total
131 #define nchtotal s.nchstats
132 #define oldnchtotal s1.nchstats
133
134 static enum state { BOOT, TIME, RUN } state = TIME;
135 enum divisor { IEC = 0, SI = HN_DIVISOR_1000 };
136
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);
148
149 static int ncpu;
150 static char buf[26];
151 static time_t t;
152 static double etime;
153 static int nintr;
154 static long *intrloc;
155 static char **intrname;
156 static int nextintsrow;
157
158 WINDOW *
openkre(void)159 openkre(void)
160 {
161
162 return (stdscr);
163 }
164
165 void
closekre(WINDOW * w)166 closekre(WINDOW *w)
167 {
168
169 if (w == NULL)
170 return;
171 wclear(w);
172 wrefresh(w);
173 }
174
175 /*
176 * These constants define where the major pieces are laid out
177 */
178 #define STATROW 0 /* uses 1 row and 67 cols */
179 #define STATCOL 0
180 #define MEMROW 2 /* uses 4 rows and 45 cols */
181 #define MEMCOL 0
182 #define PAGEROW 2 /* uses 4 rows and 30 cols */
183 #define PAGECOL 47
184 #define INTSROW 6 /* uses all rows to bottom and 16 cols */
185 #define INTSCOL 64
186 #define PROCSROW 6 /* uses 3 rows and 19 cols */
187 #define PROCSCOL 0
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 */
193 #define GRAPHCOL 0
194 #define VNSTATROW 13 /* uses 4 rows and 13 columns */
195 #define VNSTATCOL 35
196 #define NAMEIROW 14 /* uses 3 rows and 32 cols */
197 #define NAMEICOL 0
198 #define DISKROW 18 /* uses 5 rows and 47 cols (for 7 drives) */
199 #define DISKCOL 0
200
201 #define DRIVESPACE 7 /* max # for space */
202
203 #define MAXDRIVES DRIVESPACE /* max # to display */
204
205 int
initkre(void)206 initkre(void)
207 {
208 char *cp, *cp1, *cp2, *intrnamebuf, *nextcp;
209 int i;
210 size_t sz;
211
212 if ((num_devices = devstat_getnumdevs(NULL)) < 0) {
213 warnx("%s", devstat_errbuf);
214 return(0);
215 }
216
217 cur.dinfo = calloc(1, sizeof(struct devinfo));
218 last.dinfo = calloc(1, sizeof(struct devinfo));
219 run.dinfo = calloc(1, sizeof(struct devinfo));
220
221 if (dsinit(MAXDRIVES, &cur, &last, &run) != 1)
222 return(0);
223
224 if (nintr == 0) {
225 if (sysctlbyname("hw.intrcnt", NULL, &sz, NULL, 0) == -1) {
226 error("sysctl(hw.intrcnt...) failed: %s",
227 strerror(errno));
228 return (0);
229 }
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 ||
235 intrloc == NULL) {
236 error("Out of memory");
237 if (intrnamebuf)
238 free(intrnamebuf);
239 if (intrname)
240 free(intrname);
241 if (intrloc)
242 free(intrloc);
243 nintr = 0;
244 return(0);
245 }
246 for (cp = intrnamebuf, i = 0; i < nintr; i++) {
247 nextcp = cp + strlen(cp) + 1;
248
249 /* Discard trailing spaces. */
250 for (cp1 = nextcp - 1; cp1 > cp && *(cp1 - 1) == ' '; )
251 *--cp1 = '\0';
252
253 /* Convert "irqN: name" to "name irqN". */
254 if (strncmp(cp, "irq", 3) == 0) {
255 cp1 = cp + 3;
256 while (isdigit((u_char)*cp1))
257 cp1++;
258 if (cp1 != cp && *cp1 == ':' &&
259 *(cp1 + 1) == ' ') {
260 sz = strlen(cp);
261 *cp1 = '\0';
262 cp1 = cp1 + 2;
263 cp2 = strdup(cp);
264 bcopy(cp1, cp, sz - (cp1 - cp) + 1);
265 if (sz <= 10 + 4) {
266 strcat(cp, " ");
267 strcat(cp, cp2 + 3);
268 }
269 free(cp2);
270 }
271 }
272
273 /*
274 * Convert "name irqN" to "name N" if the former is
275 * longer than the field width.
276 */
277 if ((cp1 = strstr(cp, "irq")) != NULL &&
278 strlen(cp) > 10)
279 bcopy(cp1 + 3, cp1, strlen(cp1 + 3) + 1);
280
281 intrname[i] = cp;
282 cp = nextcp;
283 }
284 nextintsrow = INTSROW + 2;
285 allocinfo(&s);
286 allocinfo(&s1);
287 allocinfo(&s2);
288 allocinfo(&z);
289 }
290 GETSYSCTL("vm.kmem_size", kmem_size);
291 GETSYSCTL("vm.stats.vm.v_page_count", v_page_count);
292 getinfo(&s2);
293 copyinfo(&s2, &s1);
294 return(1);
295 }
296
297 void
fetchkre(void)298 fetchkre(void)
299 {
300 time_t now;
301 struct tm *tp;
302 static int d_first = -1;
303
304 if (d_first < 0)
305 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
306
307 time(&now);
308 tp = localtime(&now);
309 (void) strftime(buf, sizeof(buf),
310 d_first ? "%e %b %R" : "%b %e %R", tp);
311 getinfo(&s);
312 }
313
314 void
labelkre(void)315 labelkre(void)
316 {
317 int i, j;
318
319 clear();
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");
326
327 mvprintw(MEMROW + 1, MEMCOL + 41, "Free");
328
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");
333
334 mvprintw(INTSROW, INTSCOL + 1, "Interrupts");
335 mvprintw(INTSROW + 1, INTSCOL + 6, "total");
336
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");
356
357 mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt");
358
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 "| | | | | | | | | | |");
365
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");
370
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");
379 /*
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.
383 */
384 /* mvprintw(DISKROW + 4, DISKCOL, " msps"); */
385 j = 0;
386 for (i = 0; i < num_devices && j < MAXDRIVES; i++)
387 if (dev_select[i].selected) {
388 char tmpstr[80];
389 sprintf(tmpstr, "%s%d", dev_select[i].device_name,
390 dev_select[i].unit_number);
391 mvprintw(DISKROW, DISKCOL + 5 + 6 * j,
392 " %5.5s", tmpstr);
393 j++;
394 }
395
396 for (i = 0; i < nintr; i++) {
397 if (intrloc[i] == 0)
398 continue;
399 mvprintw(intrloc[i], INTSCOL + 6, "%-10.10s", intrname[i]);
400 }
401 }
402
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) \
409 do { \
410 Y(fld); \
411 putint((int)((float)s.fld/etime + 0.5), l, c, w); \
412 } while (0)
413 #define MAXFAIL 5
414
415 static char cpuchar[CPUSTATES] = { '=' , '+', '>', '-', ' ' };
416 static char cpuorder[CPUSTATES] = { CP_SYS, CP_INTR, CP_USER, CP_NICE,
417 CP_IDLE };
418
419 void
showkre(void)420 showkre(void)
421 {
422 float f1, f2;
423 int psiz, inttotal;
424 int i, l, lc;
425 static int failcnt = 0;
426
427 etime = 0;
428 for(i = 0; i < CPUSTATES; i++) {
429 X(time);
430 Q(cp_time);
431 etime += s.time[i];
432 }
433 if (etime < 5.0) { /* < 5 ticks - ignore this trash */
434 if (failcnt++ >= MAXFAIL) {
435 clear();
436 mvprintw(2, 10, "The alternate system clock has died!");
437 mvprintw(3, 10, "Reverting to ``pigs'' display.");
438 move(CMDLINE, 0);
439 refresh();
440 failcnt = 0;
441 sleep(5);
442 command("pigs");
443 }
444 return;
445 }
446 failcnt = 0;
447 etime /= hertz;
448 etime /= ncpu;
449 inttotal = 0;
450 for (i = 0; i < nintr; i++) {
451 if (s.intrcnt[i] == 0)
452 continue;
453 X(intrcnt);
454 l = (int)((float)s.intrcnt[i]/etime + 0.5);
455 inttotal += l;
456 if (intrloc[i] == 0) {
457 if (nextintsrow == LINES)
458 continue;
459 intrloc[i] = nextintsrow++;
460 mvprintw(intrloc[i], INTSCOL + 6, "%-10.10s",
461 intrname[i]);
462 }
463 putint(l, intrloc[i], INTSCOL, 5);
464 }
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;
470 if (state == TIME)
471 s1.nchcount = s.nchcount;
472
473 psiz = 0;
474 f2 = 0.0;
475 for (lc = 0; lc < CPUSTATES; lc++) {
476 i = cpuorder[lc];
477 f1 = cputime(i);
478 f2 += f1;
479 l = (int) ((f2 + 1.0) / 2.0) - psiz;
480 putfloat(f1, GRAPHROW, GRAPHCOL + 10 * lc, 4, 1, 0);
481 move(GRAPHROW + 2, psiz);
482 psiz += l;
483 while (l-- > 0)
484 addch(cpuchar[lc]);
485 }
486
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);
497
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) {
548 switch(state) {
549 case TIME:
550 dinfo(i, ++lc, &cur, &last);
551 break;
552 case RUN:
553 dinfo(i, ++lc, &cur, &run);
554 break;
555 case BOOT:
556 dinfo(i, ++lc, &cur, NULL);
557 break;
558 }
559 }
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);
574 #undef nz
575 }
576
577 int
cmdkre(const char * cmd,const char * args)578 cmdkre(const char *cmd, const char *args)
579 {
580 int retval;
581
582 if (prefix(cmd, "run")) {
583 retval = 1;
584 copyinfo(&s2, &s1);
585 switch (devstat_getdevs(NULL, &run)) {
586 case -1:
587 errx(1, "%s", devstat_errbuf);
588 break;
589 case 1:
590 num_devices = run.dinfo->numdevs;
591 generation = run.dinfo->generation;
592 retval = dscmd("refresh", NULL, MAXDRIVES, &cur);
593 if (retval == 2)
594 labelkre();
595 break;
596 default:
597 break;
598 }
599 state = RUN;
600 return (retval);
601 }
602 if (prefix(cmd, "boot")) {
603 state = BOOT;
604 copyinfo(&z, &s1);
605 return (1);
606 }
607 if (prefix(cmd, "time")) {
608 state = TIME;
609 return (1);
610 }
611 if (prefix(cmd, "zero")) {
612 retval = 1;
613 if (state == RUN) {
614 getinfo(&s1);
615 switch (devstat_getdevs(NULL, &run)) {
616 case -1:
617 errx(1, "%s", devstat_errbuf);
618 break;
619 case 1:
620 num_devices = run.dinfo->numdevs;
621 generation = run.dinfo->generation;
622 retval = dscmd("refresh",NULL, MAXDRIVES, &cur);
623 if (retval == 2)
624 labelkre();
625 break;
626 default:
627 break;
628 }
629 }
630 return (retval);
631 }
632 retval = dscmd(cmd, args, MAXDRIVES, &cur);
633
634 if (retval == 2)
635 labelkre();
636
637 return(retval);
638 }
639
640 /* calculate number of users on the system */
641 static int
ucount(void)642 ucount(void)
643 {
644 int nusers = 0;
645 struct utmpx *ut;
646
647 setutxent();
648 while ((ut = getutxent()) != NULL)
649 if (ut->ut_type == USER_PROCESS)
650 nusers++;
651 endutxent();
652
653 return (nusers);
654 }
655
656 static float
cputime(int indx)657 cputime(int indx)
658 {
659 double lt;
660 int i;
661
662 lt = 0;
663 for (i = 0; i < CPUSTATES; i++)
664 lt += s.time[i];
665 if (lt == 0.0)
666 lt = 1.0;
667 return (s.time[indx] * 100.0 / lt);
668 }
669
670 static void
putint(int n,int l,int lc,int w)671 putint(int n, int l, int lc, int w)
672 {
673
674 do_putuint64(n, l, lc, w, SI);
675 }
676
677 static void
putuint64(uint64_t n,int l,int lc,int w)678 putuint64(uint64_t n, int l, int lc, int w)
679 {
680
681 do_putuint64(n, l, lc, w, IEC);
682 }
683
684 static void
do_putuint64(uint64_t n,int l,int lc,int w,int div)685 do_putuint64(uint64_t n, int l, int lc, int w, int div)
686 {
687 int snr;
688 char b[128];
689 char buf[128];
690
691 move(l, lc);
692 #ifdef DEBUG
693 while (w-- > 0)
694 addch('*');
695 return;
696 #endif
697 if (n == 0) {
698 while (w-- > 0)
699 addch(' ');
700 return;
701 }
702 snr = snprintf(b, sizeof(b), "%*ju", w, (uintmax_t)n);
703 if (snr != w) {
704 humanize_number(buf, w, n, "", HN_AUTOSCALE,
705 HN_NOSPACE | HN_DECIMAL | div);
706 snr = snprintf(b, sizeof(b), "%*s", w, buf);
707 }
708 if (snr != w) {
709 while (w-- > 0)
710 addch('*');
711 return;
712 }
713 addstr(b);
714 }
715
716 static void
putfloat(double f,int l,int lc,int w,int d,int nz)717 putfloat(double f, int l, int lc, int w, int d, int nz)
718 {
719 int snr;
720 char b[128];
721
722 move(l, lc);
723 #ifdef DEBUG
724 while (--w >= 0)
725 addch('*');
726 return;
727 #endif
728 if (nz && f == 0.0) {
729 while (--w >= 0)
730 addch(' ');
731 return;
732 }
733 snr = snprintf(b, sizeof(b), "%*.*f", w, d, f);
734 if (snr != w)
735 snr = snprintf(b, sizeof(b), "%*.0f", w, f);
736 if (snr != w)
737 snr = snprintf(b, sizeof(b), "%*.0fk", w - 1, f / 1000);
738 if (snr != w)
739 snr = snprintf(b, sizeof(b), "%*.0fM", w - 1, f / 1000000);
740 if (snr != w) {
741 while (--w >= 0)
742 addch('*');
743 return;
744 }
745 addstr(b);
746 }
747
748 static void
putlongdouble(long double f,int l,int lc,int w,int d,int nz)749 putlongdouble(long double f, int l, int lc, int w, int d, int nz)
750 {
751 int snr;
752 char b[128];
753
754 move(l, lc);
755 #ifdef DEBUG
756 while (--w >= 0)
757 addch('*');
758 return;
759 #endif
760 if (nz && f == 0.0) {
761 while (--w >= 0)
762 addch(' ');
763 return;
764 }
765 snr = snprintf(b, sizeof(b), "%*.*Lf", w, d, f);
766 if (snr != w)
767 snr = snprintf(b, sizeof(b), "%*.0Lf", w, f);
768 if (snr != w)
769 snr = snprintf(b, sizeof(b), "%*.0Lfk", w - 1, f / 1000);
770 if (snr != w)
771 snr = snprintf(b, sizeof(b), "%*.0LfM", w - 1, f / 1000000);
772 if (snr != w) {
773 while (--w >= 0)
774 addch('*');
775 return;
776 }
777 addstr(b);
778 }
779
780 static void
getinfo(struct Info * ls)781 getinfo(struct Info *ls)
782 {
783 struct devinfo *tmp_dinfo;
784 size_t size;
785 int mib[2];
786
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));
828
829 size = sizeof(ls->Total);
830 mib[0] = CTL_VM;
831 mib[1] = VM_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));
835 }
836 size = sizeof(ncpu);
837 if (sysctlbyname("hw.ncpu", &ncpu, &size, NULL, 0) < 0 ||
838 size != sizeof(ncpu))
839 ncpu = 1;
840
841 tmp_dinfo = last.dinfo;
842 last.dinfo = cur.dinfo;
843 cur.dinfo = tmp_dinfo;
844
845 last.snap_time = cur.snap_time;
846 switch (devstat_getdevs(NULL, &cur)) {
847 case -1:
848 errx(1, "%s", devstat_errbuf);
849 break;
850 case 1:
851 num_devices = cur.dinfo->numdevs;
852 generation = cur.dinfo->generation;
853 cmdkre("refresh", NULL);
854 break;
855 default:
856 break;
857 }
858 }
859
860 static void
allocinfo(struct Info * ls)861 allocinfo(struct Info *ls)
862 {
863
864 ls->intrcnt = (long *) calloc(nintr, sizeof(long));
865 if (ls->intrcnt == NULL)
866 errx(2, "out of memory");
867 }
868
869 static void
copyinfo(struct Info * from,struct Info * to)870 copyinfo(struct Info *from, struct Info *to)
871 {
872 long *intrcnt;
873
874 /*
875 * time, wds, seek, and xfer are malloc'd so we have to
876 * save the pointers before the structure copy and then
877 * copy by hand.
878 */
879 intrcnt = to->intrcnt;
880 *to = *from;
881
882 bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
883 }
884
885 static void
dinfo(int dn,int lc,struct statinfo * now,struct statinfo * then)886 dinfo(int dn, int lc, struct statinfo *now, struct statinfo *then)
887 {
888 long double transfers_per_second;
889 long double kb_per_transfer, mb_per_second;
890 long double elapsed_time, device_busy;
891 int di;
892
893 di = dev_select[dn].position;
894
895 if (then != NULL) {
896 /* Calculate relative to previous sample */
897 elapsed_time = now->snap_time - then->snap_time;
898 } else {
899 /* Calculate relative to device creation */
900 elapsed_time = now->snap_time - devstat_compute_etime(
901 &now->dinfo->devices[di].creation_time, NULL);
902 }
903
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,
910 DSM_NONE) != 0)
911 errx(1, "%s", devstat_errbuf);
912
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);
918 }
919