Remove dhcpd and dhcrelay remainings.
[dragonfly.git] / usr.bin / systat / vmstat.c
blob90046d584e00bafcc6ee305446e8301cad2eccd6
1 /*-
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
7 * are met:
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
31 * SUCH DAMAGE.
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.10 2008/06/28 23:45:20 dillon Exp $
39 * Cursed vmstat -- from Robert Elz.
42 #define _KERNEL_STRUCTURES
43 #include <sys/param.h>
44 #include <sys/stat.h>
45 #include <sys/time.h>
46 #include <sys/user.h>
47 #include <sys/uio.h>
48 #include <sys/namei.h>
49 #include <sys/sysctl.h>
50 #include <sys/vmmeter.h>
52 #include <vm/vm_param.h>
54 #include <ctype.h>
55 #include <err.h>
56 #include <errno.h>
57 #include <kinfo.h>
58 #include <langinfo.h>
59 #include <nlist.h>
60 #include <paths.h>
61 #include <signal.h>
62 #include <stddef.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <time.h>
66 #include <unistd.h>
67 #include <utmp.h>
68 #include <devstat.h>
69 #include "systat.h"
70 #include "extern.h"
71 #include "devs.h"
73 static struct Info {
74 struct kinfo_cputime cp_time;
75 struct vmmeter Vmm;
76 struct vmtotal Total;
77 struct vmstats Vms;
78 struct nchstats nchstats;
79 long nchcount;
80 long *intrcnt;
81 int bufspace;
82 int desiredvnodes;
83 long numvnodes;
84 long freevnodes;
85 long dirtybufspace;
86 } s, s1, s2, z;
88 struct kinfo_cputime cp_time, old_cp_time;
89 struct statinfo cur, last, run;
91 #define vmm s.Vmm
92 #define vms s.Vms
93 #define oldvmm s1.Vmm
94 #define oldvms s1.Vms
95 #define total s.Total
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 *, enum state);
105 static void putint(int, int, int, int);
106 static void putfloat(double, int, int, int, int, int);
107 static void putlongdouble(long double, int, int, int, int, int);
108 static int ucount(void);
110 static int ncpu;
111 static int ut;
112 static char buf[26];
113 static time_t t;
114 static double etime;
115 static int nintr;
116 static long *intrloc;
117 static char **intrname;
118 static int nextintsrow;
119 static int extended_vm_stats;
121 struct utmp utmp;
124 WINDOW *
125 openkre(void)
128 ut = open(_PATH_UTMP, O_RDONLY);
129 if (ut < 0)
130 error("No utmp");
131 return (stdscr);
134 void
135 closekre(WINDOW *w)
138 (void) close(ut);
139 if (w == NULL)
140 return;
141 wclear(w);
142 wrefresh(w);
146 static struct nlist namelist[] = {
147 #define X_BUFFERSPACE 0
148 { "_bufspace" },
149 #define X_NCHSTATS 1
150 { "_nchstats" },
151 #define X_DESIREDVNODES 2
152 { "_desiredvnodes" },
153 #define X_NUMVNODES 3
154 { "_numvnodes" },
155 #define X_FREEVNODES 4
156 { "_freevnodes" },
157 #define X_NUMDIRTYBUFFERS 5
158 { "_dirtybufspace" },
159 { "" },
163 * These constants define where the major pieces are laid out
165 #define STATROW 0 /* uses 1 row and 68 cols */
166 #define STATCOL 2
167 #define MEMROW 2 /* uses 4 rows and 31 cols */
168 #define MEMCOL 0
169 #define PAGEROW 2 /* uses 4 rows and 26 cols */
170 #define PAGECOL 46
171 #define INTSROW 6 /* uses all rows to bottom and 17 cols */
172 #define INTSCOL 61
173 #define PROCSROW 7 /* uses 2 rows and 20 cols */
174 #define PROCSCOL 0
175 #define GENSTATROW 7 /* uses 2 rows and 30 cols */
176 #define GENSTATCOL 20
177 #define VMSTATROW 6 /* uses 17 rows and 12 cols */
178 #define VMSTATCOL 48
179 #define GRAPHROW 10 /* uses 3 rows and 51 cols */
180 #define GRAPHCOL 0
181 #define NAMEIROW 14 /* uses 3 rows and 38 cols */
182 #define NAMEICOL 0
183 #define DISKROW 18 /* uses 5 rows and 50 cols (for 9 drives) */
184 #define DISKCOL 0
186 #define DRIVESPACE 7 /* max # for space */
188 #define MAXDRIVES DRIVESPACE /* max # to display */
191 initkre(void)
193 char *intrnamebuf;
194 size_t bytes;
195 size_t b;
196 size_t i;
198 if (namelist[0].n_type == 0) {
199 if (kvm_nlist(kd, namelist)) {
200 nlisterr(namelist);
201 return(0);
203 if (namelist[0].n_type == 0) {
204 error("No namelist");
205 return(0);
209 if ((num_devices = getnumdevs()) < 0) {
210 warnx("%s", devstat_errbuf);
211 return(0);
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)
222 return(0);
224 if (nintr == 0) {
225 if (sysctlbyname("hw.intrnames", NULL, &bytes, NULL, 0) == 0) {
226 intrnamebuf = malloc(bytes);
227 sysctlbyname("hw.intrnames", intrnamebuf, &bytes,
228 NULL, 0);
229 for (i = 0; i < bytes; ++i) {
230 if (intrnamebuf[i] == 0)
231 ++nintr;
233 intrname = malloc(nintr * sizeof(char *));
234 intrloc = malloc(nintr * sizeof(*intrloc));
235 nintr = 0;
236 for (b = i = 0; i < bytes; ++i) {
237 if (intrnamebuf[i] == 0) {
238 intrname[nintr] = intrnamebuf + b;
239 intrloc[nintr] = 0;
240 b = i + 1;
241 ++nintr;
245 nextintsrow = INTSROW + 2;
246 allocinfo(&s);
247 allocinfo(&s1);
248 allocinfo(&s2);
249 allocinfo(&z);
251 getinfo(&s2, RUN);
252 copyinfo(&s2, &s1);
253 return(1);
256 void
257 fetchkre(void)
259 time_t now;
260 struct tm *tp;
261 static int d_first = -1;
263 if (d_first < 0)
264 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
266 time(&now);
267 tp = localtime(&now);
268 (void) strftime(buf, sizeof(buf),
269 d_first ? "%e %b %R" : "%b %e %R", tp);
270 getinfo(&s, state);
273 void
274 labelkre(void)
276 register int i, j;
278 clear();
279 mvprintw(STATROW, STATCOL + 4, "users Load");
280 mvprintw(MEMROW, MEMCOL, "Mem:KB REAL VIRTUAL");
281 mvprintw(MEMROW + 1, MEMCOL, " Tot Share Tot Share");
282 mvprintw(MEMROW + 2, MEMCOL, "Act");
283 mvprintw(MEMROW + 3, MEMCOL, "All");
285 mvprintw(MEMROW + 1, MEMCOL + 41, "Free");
287 mvprintw(PAGEROW, PAGECOL, " VN PAGER SWAP PAGER ");
288 mvprintw(PAGEROW + 1, PAGECOL, " in out in out ");
289 mvprintw(PAGEROW + 2, PAGECOL, "count");
290 mvprintw(PAGEROW + 3, PAGECOL, "pages");
292 mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
293 mvprintw(INTSROW + 1, INTSCOL + 9, "total");
295 mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "cow");
296 mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "wire");
297 mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "act");
298 mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "inact");
299 mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "cache");
300 mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "free");
301 mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "daefr");
302 mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "prcfr");
303 mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "react");
304 mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "pdwake");
305 mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "pdpgs");
306 mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "intrn");
307 mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "buf");
308 mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "dirtybuf");
310 mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "desiredvnodes");
311 mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "numvnodes");
312 mvprintw(VMSTATROW + 17, VMSTATCOL + 10, "freevnodes");
314 mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt");
316 mvprintw(GRAPHROW, GRAPHCOL,
317 " . %%Sys . %%Intr . %%User . %%Nice . %%Idle");
318 mvprintw(PROCSROW, PROCSCOL, "Proc:r p d s w");
319 mvprintw(GRAPHROW + 1, GRAPHCOL,
320 "| | | | | | | | | | |");
322 mvprintw(NAMEIROW, NAMEICOL, "Namei Name-cache Dir-cache");
323 mvprintw(NAMEIROW + 1, NAMEICOL,
324 " Calls hits %% hits %%");
325 mvprintw(DISKROW, DISKCOL, "Disks");
326 mvprintw(DISKROW + 1, DISKCOL, "KB/t");
327 mvprintw(DISKROW + 2, DISKCOL, "tps");
328 mvprintw(DISKROW + 3, DISKCOL, "MB/s");
329 mvprintw(DISKROW + 4, DISKCOL, "%% busy");
331 * For now, we don't support a fourth disk statistic. So there's
332 * no point in providing a label for it. If someone can think of a
333 * fourth useful disk statistic, there is room to add it.
335 /* mvprintw(DISKROW + 4, DISKCOL, " msps"); */
336 j = 0;
337 for (i = 0; i < num_devices && j < MAXDRIVES; i++)
338 if (dev_select[i].selected) {
339 char tmpstr[80];
340 sprintf(tmpstr, "%s%d", dev_select[i].device_name,
341 dev_select[i].unit_number);
342 mvprintw(DISKROW, DISKCOL + 5 + 6 * j,
343 " %5.5s", tmpstr);
344 j++;
347 if (j <= 4) {
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;
356 } else {
357 extended_vm_stats = 0;
358 mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "zfod");
361 for (i = 0; i < nintr; i++) {
362 if (intrloc[i] == 0)
363 continue;
364 mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s", intrname[i]);
368 #define CP_UPDATE(fld) do { \
369 uint64_t t; \
370 t=s.fld; \
371 s.fld-=s1.fld; \
372 if(state==TIME) \
373 s1.fld=t; \
374 t=fld; \
375 fld-=old_##fld; \
376 if(state==TIME) \
377 old_##fld=t; \
378 etime += s.fld; \
379 } while(0)
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) \
385 Y(fld); \
386 putint((int)((float)s.fld/etime + 0.5), l, c, w)
387 #define MAXFAIL 5
389 #define CPUSTATES 5
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)
400 void
401 showkre(void)
403 float f1, f2;
404 int psiz, inttotal;
405 int i, l, c;
406 static int failcnt = 0;
407 double total_time;
409 etime = 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);
416 total_time = etime;
417 if (total_time == 0.0)
418 total_time = 1.0;
420 if (etime < 100000.0) { /* < 100ms ignore this trash */
421 if (failcnt++ >= MAXFAIL) {
422 clear();
423 mvprintw(2, 10, "The alternate system clock has died!");
424 mvprintw(3, 10, "Reverting to ``pigs'' display.");
425 move(CMDLINE, 0);
426 refresh();
427 failcnt = 0;
428 sleep(5);
429 command("pigs");
431 return;
433 failcnt = 0;
434 etime /= 1000000.0;
435 etime /= ncpu;
436 if (etime == 0)
437 etime = 1;
438 inttotal = 0;
439 for (i = 0; i < nintr; i++) {
440 if (s.intrcnt[i] == 0)
441 continue;
442 if (intrloc[i] == 0) {
443 if (nextintsrow == LINES)
444 continue;
445 intrloc[i] = nextintsrow++;
446 mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s",
447 intrname[i]);
449 X(intrcnt);
450 l = (int)((float)s.intrcnt[i]/etime + 0.5);
451 inttotal += l;
452 putint(l, intrloc[i], INTSCOL + 2, 6);
454 putint(inttotal, INTSROW + 1, INTSCOL + 2, 6);
455 Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
456 Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes); Z(ncs_neghits);
457 s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
458 nchtotal.ncs_miss + nchtotal.ncs_long + nchtotal.ncs_neghits;
459 if (state == TIME)
460 s1.nchcount = s.nchcount;
462 psiz = 0;
463 f2 = 0.0;
464 for (c = 0; c < CPUSTATES; c++) {
465 uint64_t val = *(uint64_t *)(((uint8_t *)&s.cp_time) +
466 cpuoffsets[c]);
467 f1 = 100.0 * val / total_time;
468 f2 += f1;
469 l = (int) ((f2 + 1.0) / 2.0) - psiz;
470 if (f1 > 99.9)
471 f1 = 99.9; /* no room to display 100.0 */
472 putfloat(f1, GRAPHROW, GRAPHCOL + 10 * c, 4, 1, 0);
473 move(GRAPHROW + 2, psiz);
474 psiz += l;
475 while (l-- > 0)
476 addch(cpuchar[c]);
479 putint(ucount(), STATROW, STATCOL, 3);
480 putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
481 putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
482 putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
483 mvaddstr(STATROW, STATCOL + 53, buf);
484 #define pgtokb(pg) ((pg) * vms.v_page_size / 1024)
485 putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 8);
486 putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 11, 8);
487 putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 19, 9);
488 putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 28, 9);
489 putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 8);
490 putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 11, 8);
491 putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 19, 9);
492 putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 28, 9);
493 putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 37, 8);
494 putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
495 putint(total.t_pw, PROCSROW + 1, PROCSCOL + 6, 3);
496 putint(total.t_dw, PROCSROW + 1, PROCSCOL + 9, 3);
497 putint(total.t_sl, PROCSROW + 1, PROCSCOL + 12, 3);
498 putint(total.t_sw, PROCSROW + 1, PROCSCOL + 15, 3);
499 if (extended_vm_stats == 0) {
500 PUTRATE(Vmm.v_zfod, VMSTATROW + 0, VMSTATCOL + 4, 5);
502 PUTRATE(Vmm.v_cow_faults, VMSTATROW + 1, VMSTATCOL + 3, 6);
503 putint(pgtokb(vms.v_wire_count), VMSTATROW + 2, VMSTATCOL, 9);
504 putint(pgtokb(vms.v_active_count), VMSTATROW + 3, VMSTATCOL, 9);
505 putint(pgtokb(vms.v_inactive_count), VMSTATROW + 4, VMSTATCOL, 9);
506 putint(pgtokb(vms.v_cache_count), VMSTATROW + 5, VMSTATCOL, 9);
507 putint(pgtokb(vms.v_free_count), VMSTATROW + 6, VMSTATCOL, 9);
508 PUTRATE(Vmm.v_dfree, VMSTATROW + 7, VMSTATCOL, 9);
509 PUTRATE(Vmm.v_pfree, VMSTATROW + 8, VMSTATCOL, 9);
510 PUTRATE(Vmm.v_reactivated, VMSTATROW + 9, VMSTATCOL, 9);
511 PUTRATE(Vmm.v_pdwakeups, VMSTATROW + 10, VMSTATCOL, 9);
512 PUTRATE(Vmm.v_pdpages, VMSTATROW + 11, VMSTATCOL, 9);
513 PUTRATE(Vmm.v_intrans, VMSTATROW + 12, VMSTATCOL, 9);
515 if (extended_vm_stats) {
516 PUTRATE(Vmm.v_zfod, VMSTATROW + 11, VMSTATCOL - 16, 9);
517 PUTRATE(Vmm.v_ozfod, VMSTATROW + 12, VMSTATCOL - 16, 9);
518 putint(
519 ((s.Vmm.v_ozfod < s.Vmm.v_zfod) ?
520 s.Vmm.v_ozfod * 100 / s.Vmm.v_zfod :
523 VMSTATROW + 13,
524 VMSTATCOL - 16,
527 PUTRATE(Vmm.v_tfree, VMSTATROW + 14, VMSTATCOL - 16, 9);
530 putint(s.bufspace/1024, VMSTATROW + 13, VMSTATCOL, 9);
531 putint(s.dirtybufspace/1024, VMSTATROW + 14, VMSTATCOL, 9);
532 putint(s.desiredvnodes, VMSTATROW + 15, VMSTATCOL, 9);
533 putint(s.numvnodes, VMSTATROW + 16, VMSTATCOL, 9);
534 putint(s.freevnodes, VMSTATROW + 17, VMSTATCOL, 9);
535 PUTRATE(Vmm.v_vnodein, PAGEROW + 2, PAGECOL + 5, 5);
536 PUTRATE(Vmm.v_vnodeout, PAGEROW + 2, PAGECOL + 10, 5);
537 PUTRATE(Vmm.v_swapin, PAGEROW + 2, PAGECOL + 17, 5);
538 PUTRATE(Vmm.v_swapout, PAGEROW + 2, PAGECOL + 22, 5);
539 PUTRATE(Vmm.v_vnodepgsin, PAGEROW + 3, PAGECOL + 5, 5);
540 PUTRATE(Vmm.v_vnodepgsout, PAGEROW + 3, PAGECOL + 10, 5);
541 PUTRATE(Vmm.v_swappgsin, PAGEROW + 3, PAGECOL + 17, 5);
542 PUTRATE(Vmm.v_swappgsout, PAGEROW + 3, PAGECOL + 22, 5);
543 PUTRATE(Vmm.v_swtch, GENSTATROW + 1, GENSTATCOL, 5);
544 PUTRATE(Vmm.v_trap, GENSTATROW + 1, GENSTATCOL + 5, 5);
545 PUTRATE(Vmm.v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 5);
546 PUTRATE(Vmm.v_intr, GENSTATROW + 1, GENSTATCOL + 15, 5);
547 PUTRATE(Vmm.v_soft, GENSTATROW + 1, GENSTATCOL + 20, 5);
548 PUTRATE(Vmm.v_vm_faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
549 mvprintw(DISKROW, DISKCOL + 5, " ");
550 for (i = 0, c = 0; i < num_devices && c < MAXDRIVES; i++)
551 if (dev_select[i].selected) {
552 char tmpstr[80];
553 sprintf(tmpstr, "%s%d", dev_select[i].device_name,
554 dev_select[i].unit_number);
555 mvprintw(DISKROW, DISKCOL + 5 + 6 * c,
556 " %5.5s", tmpstr);
557 switch(state) {
558 case TIME:
559 dinfo(i, ++c, &cur, &last);
560 break;
561 case RUN:
562 dinfo(i, ++c, &cur, &run);
563 break;
564 case BOOT:
565 dinfo(i, ++c, &cur, NULL);
566 break;
569 putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
570 putint((nchtotal.ncs_goodhits + nchtotal.ncs_neghits),
571 NAMEIROW + 2, NAMEICOL + 9, 9);
572 #define nz(x) ((x) ? (x) : 1)
573 putfloat((nchtotal.ncs_goodhits+nchtotal.ncs_neghits) *
574 100.0 / nz(s.nchcount),
575 NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
576 putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
577 putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
578 NAMEIROW + 2, NAMEICOL + 33, 4, 0, 1);
579 #undef nz
583 cmdkre(char *cmd, char *args)
585 int retval;
587 if (prefix(cmd, "run")) {
588 retval = 1;
589 copyinfo(&s2, &s1);
590 switch (getdevs(&run)) {
591 case -1:
592 errx(1, "%s", devstat_errbuf);
593 break;
594 case 1:
595 num_devices = run.dinfo->numdevs;
596 generation = run.dinfo->generation;
597 retval = dscmd("refresh", NULL, MAXDRIVES, &cur);
598 if (retval == 2)
599 labelkre();
600 break;
601 default:
602 break;
604 state = RUN;
605 return (retval);
607 if (prefix(cmd, "boot")) {
608 state = BOOT;
609 copyinfo(&z, &s1);
610 return (1);
612 if (prefix(cmd, "time")) {
613 state = TIME;
614 return (1);
616 if (prefix(cmd, "zero")) {
617 retval = 1;
618 if (state == RUN) {
619 getinfo(&s1, RUN);
620 switch (getdevs(&run)) {
621 case -1:
622 errx(1, "%s", devstat_errbuf);
623 break;
624 case 1:
625 num_devices = run.dinfo->numdevs;
626 generation = run.dinfo->generation;
627 retval = dscmd("refresh",NULL, MAXDRIVES, &cur);
628 if (retval == 2)
629 labelkre();
630 break;
631 default:
632 break;
635 return (retval);
637 retval = dscmd(cmd, args, MAXDRIVES, &cur);
639 if (retval == 2)
640 labelkre();
642 return(retval);
645 /* calculate number of users on the system */
646 static int
647 ucount(void)
649 register int nusers = 0;
651 if (ut < 0)
652 return (0);
653 while (read(ut, &utmp, sizeof(utmp)))
654 if (utmp.ut_name[0] != '\0')
655 nusers++;
657 lseek(ut, 0L, L_SET);
658 return (nusers);
661 static void
662 putint(int n, int l, int c, int w)
664 char b[128];
666 move(l, c);
667 if (n == 0) {
668 while (w-- > 0)
669 addch(' ');
670 return;
672 snprintf(b, sizeof(b), "%*d", w, n);
673 if (strlen(b) > w) {
674 while (w-- > 0)
675 addch('*');
676 return;
678 addstr(b);
681 static void
682 putfloat(double f, int l, int c, int w, int d, int nz)
684 char b[128];
686 move(l, c);
687 if (nz && f == 0.0) {
688 while (--w >= 0)
689 addch(' ');
690 return;
692 snprintf(b, sizeof(b), "%*.*f", w, d, f);
693 if (strlen(b) > w)
694 snprintf(b, sizeof(b), "%*.0f", w, f);
695 if (strlen(b) > w) {
696 while (--w >= 0)
697 addch('*');
698 return;
700 addstr(b);
703 static void
704 putlongdouble(long double f, int l, int c, int w, int d, int nz)
706 char b[128];
708 move(l, c);
709 if (nz && f == 0.0) {
710 while (--w >= 0)
711 addch(' ');
712 return;
714 sprintf(b, "%*.*Lf", w, d, f);
715 if (strlen(b) > w)
716 sprintf(b, "%*.0Lf", w, f);
717 if (strlen(b) > w) {
718 while (--w >= 0)
719 addch('*');
720 return;
722 addstr(b);
725 static void
726 getinfo(struct Info *s, enum state st)
728 struct devinfo *tmp_dinfo;
729 struct nchstats *nch_tmp;
730 size_t size;
731 int vms_size = sizeof(s->Vms);
732 int vmm_size = sizeof(s->Vmm);
733 size_t nch_size = sizeof(s->nchstats) * SMP_MAXCPU;
735 if (sysctlbyname("vm.vmstats", &s->Vms, &vms_size, NULL, 0)) {
736 perror("sysctlbyname: vm.vmstats");
737 exit(1);
739 if (sysctlbyname("vm.vmmeter", &s->Vmm, &vmm_size, NULL, 0)) {
740 perror("sysctlbyname: vm.vmstats");
741 exit(1);
744 if (kinfo_get_sched_cputime(&s->cp_time))
745 err(1, "kinfo_get_sched_cputime");
746 if (kinfo_get_sched_cputime(&cp_time))
747 err(1, "kinfo_get_sched_cputime");
748 NREAD(X_BUFFERSPACE, &s->bufspace, sizeof(s->bufspace));
749 NREAD(X_DESIREDVNODES, &s->desiredvnodes, sizeof(s->desiredvnodes));
750 NREAD(X_NUMVNODES, &s->numvnodes, LONG);
751 NREAD(X_FREEVNODES, &s->freevnodes, LONG);
752 NREAD(X_NUMDIRTYBUFFERS, &s->dirtybufspace, sizeof(s->dirtybufspace));
754 if (nintr) {
755 size = nintr * sizeof(s->intrcnt[0]);
756 sysctlbyname("hw.intrcnt", s->intrcnt, &size, NULL, 0);
758 size = sizeof(s->Total);
759 if (sysctlbyname("vm.vmtotal", &s->Total, &size, NULL, 0) < 0) {
760 error("Can't get kernel info: %s\n", strerror(errno));
761 bzero(&s->Total, sizeof(s->Total));
764 if ((nch_tmp = malloc(nch_size)) == NULL) {
765 perror("malloc");
766 exit(1);
767 } else {
768 if (sysctlbyname("vfs.cache.nchstats", nch_tmp, &nch_size, NULL, 0)) {
769 perror("sysctlbyname vfs.cache.nchstats");
770 free(nch_tmp);
771 exit(1);
772 } else {
773 if ((nch_tmp = realloc(nch_tmp, nch_size)) == NULL) {
774 perror("realloc");
775 exit(1);
780 if (kinfo_get_cpus(&ncpu))
781 err(1, "kinfo_get_cpus");
782 kvm_nch_cpuagg(nch_tmp, &s->nchstats, ncpu);
783 free(nch_tmp);
785 tmp_dinfo = last.dinfo;
786 last.dinfo = cur.dinfo;
787 cur.dinfo = tmp_dinfo;
789 last.busy_time = cur.busy_time;
790 switch (getdevs(&cur)) {
791 case -1:
792 errx(1, "%s", devstat_errbuf);
793 break;
794 case 1:
795 num_devices = cur.dinfo->numdevs;
796 generation = cur.dinfo->generation;
797 cmdkre("refresh", NULL);
798 break;
799 default:
800 break;
804 static void
805 allocinfo(struct Info *s)
807 s->intrcnt = (long *) calloc(nintr, sizeof(long));
808 if (s->intrcnt == NULL)
809 errx(2, "out of memory");
812 static void
813 copyinfo(register struct Info *from, register struct Info *to)
815 long *intrcnt;
818 * time, wds, seek, and xfer are malloc'd so we have to
819 * save the pointers before the structure copy and then
820 * copy by hand.
822 intrcnt = to->intrcnt;
823 *to = *from;
825 bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
828 static void
829 dinfo(int dn, int c, struct statinfo *now, struct statinfo *then)
831 long double transfers_per_second;
832 long double kb_per_transfer, mb_per_second;
833 long double elapsed_time, device_busy;
834 int di;
836 di = dev_select[dn].position;
838 elapsed_time = compute_etime(now->busy_time, then ?
839 then->busy_time :
840 now->dinfo->devices[di].dev_creation_time);
842 device_busy = compute_etime(now->dinfo->devices[di].busy_time, then ?
843 then->dinfo->devices[di].busy_time :
844 now->dinfo->devices[di].dev_creation_time);
846 if (compute_stats(&now->dinfo->devices[di], then ?
847 &then->dinfo->devices[di] : NULL, elapsed_time,
848 NULL, NULL, NULL,
849 &kb_per_transfer, &transfers_per_second,
850 &mb_per_second, NULL, NULL) != 0)
851 errx(1, "%s", devstat_errbuf);
853 if ((device_busy == 0) && (transfers_per_second > 5))
854 /* the device has been 100% busy, fake it because
855 * as long as the device is 100% busy the busy_time
856 * field in the devstat struct is not updated */
857 device_busy = elapsed_time;
858 if (device_busy > elapsed_time)
859 /* this normally happens after one or more periods
860 * where the device has been 100% busy, correct it */
861 device_busy = elapsed_time;
863 c = DISKCOL + c * 6;
864 putlongdouble(kb_per_transfer, DISKROW + 1, c, 5, 2, 0);
865 putlongdouble(transfers_per_second, DISKROW + 2, c, 5, 0, 0);
866 putlongdouble(mb_per_second, DISKROW + 3, c, 5, 2, 0);
867 putlongdouble(device_busy * 100 / elapsed_time, DISKROW + 4, c, 5, 0, 0);