Remove advertising header from all userland binaries.
[dragonfly.git] / usr.bin / systat / vmstat.c
blob6f0916e86474c12ad100ea262febb18474e59518
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
31 * Cursed vmstat -- from Robert Elz.
34 #include <sys/user.h>
35 #include <sys/param.h>
36 #include <sys/stat.h>
37 #include <sys/time.h>
38 #include <sys/uio.h>
39 #include <sys/namei.h>
40 #include <sys/sysctl.h>
41 #include <sys/vmmeter.h>
43 #include <vm/vm_param.h>
45 #include <ctype.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <kinfo.h>
49 #include <langinfo.h>
50 #include <nlist.h>
51 #include <paths.h>
52 #include <signal.h>
53 #include <stddef.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <time.h>
57 #include <unistd.h>
58 #include "utmpentry.h"
59 #include <devstat.h>
60 #include "systat.h"
61 #include "extern.h"
62 #include "devs.h"
64 static struct Info {
65 struct kinfo_cputime cp_time;
66 struct vmmeter Vmm;
67 struct vmtotal Total;
68 struct vmstats Vms;
69 struct nchstats nchstats;
70 long nchcount;
71 long nchpathcount;
72 long *intrcnt;
73 long bufspace;
74 int desiredvnodes;
75 int numvnodes;
76 int freevnodes;
77 long dirtybufspace;
78 } s, s1, s2, z;
80 struct kinfo_cputime cp_time, old_cp_time;
81 struct statinfo cur, last, run;
83 #define vmm s.Vmm
84 #define vms s.Vms
85 #define oldvmm s1.Vmm
86 #define oldvms s1.Vms
87 #define total s.Total
88 #define nchtotal s.nchstats
89 #define oldnchtotal s1.nchstats
91 static enum state { BOOT, TIME, RUN } state = TIME;
93 static void allocinfo(struct Info *);
94 static void copyinfo(struct Info *, struct Info *);
95 static void dinfo(int, int, struct statinfo *, struct statinfo *);
96 static void getinfo(struct Info *);
97 static void put64(int64_t, int, int, int, int);
98 static void putfloat(double, int, int, int, int, int);
99 static void putlongdouble(long double, int, int, int, int, int);
100 static void putlongdoublez(long double, int, int, int, int, int);
101 static int ucount(void);
103 static int ncpu;
104 static char buf[26];
105 static time_t t;
106 static double etime;
107 static int nintr;
108 static long *intrloc;
109 static char **intrname;
110 static int nextintsrow;
111 static int extended_vm_stats;
115 WINDOW *
116 openkre(void)
119 return (stdscr);
122 void
123 closekre(WINDOW *w)
126 if (w == NULL)
127 return;
128 wclear(w);
129 wrefresh(w);
133 static struct nlist namelist[] = {
134 #define X_BUFFERSPACE 0
135 { .n_name = "_bufspace" },
136 #define X_NCHSTATS 1
137 { .n_name = "_nchstats" },
138 #define X_DESIREDVNODES 2
139 { .n_name = "_desiredvnodes" },
140 #define X_NUMVNODES 3
141 { .n_name = "_numvnodes" },
142 #define X_FREEVNODES 4
143 { .n_name = "_freevnodes" },
144 #define X_NUMDIRTYBUFFERS 5
145 { .n_name = "_dirtybufspace" },
146 { .n_name = "" },
150 * These constants define where the major pieces are laid out
152 #define STATROW 0 /* uses 1 row and 68 cols */
153 #define STATCOL 2
154 #define MEMROW 2 /* uses 4 rows and 31 cols */
155 #define MEMCOL 0
156 #define PAGEROW 2 /* uses 4 rows and 26 cols */
157 #define PAGECOL 46
158 #define INTSROW 6 /* uses all rows to bottom and 17 cols */
159 #define INTSCOL 61
160 #define PROCSROW 7 /* uses 2 rows and 20 cols */
161 #define PROCSCOL 0
162 #define GENSTATROW 7 /* uses 2 rows and 30 cols */
163 #define GENSTATCOL 16
164 #define VMSTATROW 6 /* uses 17 rows and 12 cols */
165 #define VMSTATCOL 50
166 #define GRAPHROW 10 /* uses 3 rows and 51 cols */
167 #define GRAPHCOL 0
168 #define NAMEIROW 14 /* uses 3 rows and 38 cols */
169 #define NAMEICOL 0
170 #define DISKROW 17 /* uses 6 rows and 50 cols (for 9 drives) */
171 #define DISKCOL 0
173 #define DRIVESPACE 7 /* max # for space */
175 #define MAXDRIVES DRIVESPACE /* max # to display */
178 initkre(void)
180 char *intrnamebuf;
181 size_t bytes;
182 size_t b;
183 size_t i;
185 if (namelist[0].n_type == 0) {
186 if (kvm_nlist(kd, namelist)) {
187 nlisterr(namelist);
188 return(0);
190 if (namelist[0].n_type == 0) {
191 error("No namelist");
192 return(0);
196 if ((num_devices = getnumdevs()) < 0) {
197 warnx("%s", devstat_errbuf);
198 return(0);
201 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
202 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
203 run.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
204 bzero(cur.dinfo, sizeof(struct devinfo));
205 bzero(last.dinfo, sizeof(struct devinfo));
206 bzero(run.dinfo, sizeof(struct devinfo));
208 if (dsinit(MAXDRIVES, &cur, &last, &run) != 1)
209 return(0);
211 if (nintr == 0) {
212 if (sysctlbyname("hw.intrnames", NULL, &bytes, NULL, 0) == 0) {
213 intrnamebuf = malloc(bytes);
214 sysctlbyname("hw.intrnames", intrnamebuf, &bytes,
215 NULL, 0);
216 for (i = 0; i < bytes; ++i) {
217 if (intrnamebuf[i] == 0)
218 ++nintr;
220 intrname = malloc(nintr * sizeof(char *));
221 intrloc = malloc(nintr * sizeof(*intrloc));
222 nintr = 0;
223 for (b = i = 0; i < bytes; ++i) {
224 if (intrnamebuf[i] == 0) {
225 intrname[nintr] = intrnamebuf + b;
226 intrloc[nintr] = 0;
227 b = i + 1;
228 ++nintr;
232 nextintsrow = INTSROW + 2;
233 allocinfo(&s);
234 allocinfo(&s1);
235 allocinfo(&s2);
236 allocinfo(&z);
238 getinfo(&s2);
239 copyinfo(&s2, &s1);
240 return(1);
243 void
244 fetchkre(void)
246 time_t now;
247 struct tm *tp;
248 static int d_first = -1;
250 if (d_first < 0)
251 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
253 time(&now);
254 tp = localtime(&now);
255 (void) strftime(buf, sizeof(buf),
256 d_first ? "%e %b %R" : "%b %e %R", tp);
257 getinfo(&s);
260 void
261 labelkre(void)
263 int i, j;
265 clear();
266 mvprintw(STATROW, STATCOL + 4, "users Load");
267 mvprintw(MEMROW, MEMCOL, "Mem: REAL VIRTUAL");
268 mvprintw(MEMROW + 1, MEMCOL, " Tot Share Tot Share");
269 mvprintw(MEMROW + 2, MEMCOL, "Act");
270 mvprintw(MEMROW + 3, MEMCOL, "All");
272 mvprintw(MEMROW + 1, MEMCOL + 36, "Free");
274 mvprintw(PAGEROW, PAGECOL, " VN PAGER SWAP PAGER ");
275 mvprintw(PAGEROW + 1, PAGECOL, " in out in out ");
276 mvprintw(PAGEROW + 2, PAGECOL, "count");
277 mvprintw(PAGEROW + 3, PAGECOL, "pages");
279 mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
280 mvprintw(INTSROW + 1, INTSCOL + 9, "total");
282 mvprintw(VMSTATROW + 1, VMSTATCOL + 8, "cow");
283 mvprintw(VMSTATROW + 2, VMSTATCOL + 8, "wire");
284 mvprintw(VMSTATROW + 3, VMSTATCOL + 8, "act");
285 mvprintw(VMSTATROW + 4, VMSTATCOL + 8, "inact");
286 mvprintw(VMSTATROW + 5, VMSTATCOL + 8, "cache");
287 mvprintw(VMSTATROW + 6, VMSTATCOL + 8, "free");
288 mvprintw(VMSTATROW + 7, VMSTATCOL + 8, "daefr");
289 mvprintw(VMSTATROW + 8, VMSTATCOL + 8, "prcfr");
290 mvprintw(VMSTATROW + 9, VMSTATCOL + 8, "react");
291 mvprintw(VMSTATROW + 10, VMSTATCOL + 8, "pdwake");
292 mvprintw(VMSTATROW + 11, VMSTATCOL + 8, "pdpgs");
293 mvprintw(VMSTATROW + 12, VMSTATCOL + 8, "intrn");
294 mvprintw(VMSTATROW + 13, VMSTATCOL + 8, "buf");
295 mvprintw(VMSTATROW + 14, VMSTATCOL + 8, "dirtybuf");
297 mvprintw(VMSTATROW + 15, VMSTATCOL + 8, "desiredvnodes");
298 mvprintw(VMSTATROW + 16, VMSTATCOL + 8, "numvnodes");
299 mvprintw(VMSTATROW + 17, VMSTATCOL + 8, "freevnodes");
301 mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt");
303 mvprintw(GRAPHROW, GRAPHCOL,
304 " . %%Sys . %%Intr . %%User . %%Nice . %%Idle");
305 mvprintw(PROCSROW, PROCSCOL, " r p d s w");
306 mvprintw(GRAPHROW + 1, GRAPHCOL,
307 "| | | | | | | | | | |");
309 mvprintw(NAMEIROW, NAMEICOL, "Path-lookups hits %% Components");
310 mvprintw(DISKROW, DISKCOL, "Disks");
311 mvprintw(DISKROW + 1, DISKCOL, "KB/t");
312 mvprintw(DISKROW + 2, DISKCOL, "tpr/s");
313 mvprintw(DISKROW + 3, DISKCOL, "MBr/s");
314 mvprintw(DISKROW + 4, DISKCOL, "tpw/s");
315 mvprintw(DISKROW + 5, DISKCOL, "MBw/s");
316 mvprintw(DISKROW + 6, DISKCOL, "%% busy");
318 * For now, we don't support a fourth disk statistic. So there's
319 * no point in providing a label for it. If someone can think of a
320 * fourth useful disk statistic, there is room to add it.
322 j = 0;
323 for (i = 0; i < num_devices && j < MAXDRIVES; i++)
324 if (dev_select[i].selected) {
325 char tmpstr[80];
326 sprintf(tmpstr, "%s%d", dev_select[i].device_name,
327 dev_select[i].unit_number);
328 mvprintw(DISKROW, DISKCOL + 5 + 6 * j,
329 " %5.5s", tmpstr);
330 j++;
333 if (j <= 4) {
335 * room for extended VM stats
337 mvprintw(VMSTATROW + 11, VMSTATCOL - 6, "zfod");
338 mvprintw(VMSTATROW + 12, VMSTATCOL - 6, "ozfod");
339 mvprintw(VMSTATROW + 13, VMSTATCOL - 6, "%%sloz");
340 mvprintw(VMSTATROW + 14, VMSTATCOL - 6, "tfree");
341 extended_vm_stats = 1;
342 } else {
343 extended_vm_stats = 0;
344 mvprintw(VMSTATROW + 0, VMSTATCOL + 8, "zfod");
347 for (i = 0; i < nintr; i++) {
348 if (intrloc[i] == 0)
349 continue;
350 mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s", intrname[i]);
354 #define CP_UPDATE(fld) do { \
355 uint64_t lt; \
356 lt=s.fld; \
357 s.fld-=s1.fld; \
358 if(state==TIME) \
359 s1.fld=lt; \
360 lt=fld; \
361 fld-=old_##fld; \
362 if(state==TIME) \
363 old_##fld=lt; \
364 etime += s.fld; \
365 } while(0)
366 #define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
367 #define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
368 #define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
369 if(state == TIME) s1.nchstats.fld = t;}
370 #define PUTRATE(fld, l, c, w) \
371 Y(fld); \
372 put64((int64_t)((float)s.fld/etime + 0.5), l, c, w, 'D')
373 #define MAXFAIL 5
375 #define CPUSTATES 5
376 static const char cpuchar[5] = { '=' , '+', '>', '-', ' ' };
378 static const size_t cpuoffsets[] = {
379 offsetof(struct kinfo_cputime, cp_sys),
380 offsetof(struct kinfo_cputime, cp_intr),
381 offsetof(struct kinfo_cputime, cp_user),
382 offsetof(struct kinfo_cputime, cp_nice),
383 offsetof(struct kinfo_cputime, cp_idle)
386 void
387 showkre(void)
389 float f1, f2;
390 int psiz;
391 int i, lc;
392 long inttotal;
393 long l;
394 static int failcnt = 0;
395 double total_time;
397 etime = 0;
398 CP_UPDATE(cp_time.cp_user);
399 CP_UPDATE(cp_time.cp_nice);
400 CP_UPDATE(cp_time.cp_sys);
401 CP_UPDATE(cp_time.cp_intr);
402 CP_UPDATE(cp_time.cp_idle);
404 total_time = etime;
405 if (total_time == 0.0)
406 total_time = 1.0;
408 if (etime < 100000.0) { /* < 100ms ignore this trash */
409 if (failcnt++ >= MAXFAIL) {
410 clear();
411 mvprintw(2, 10, "The alternate system clock has died!");
412 mvprintw(3, 10, "Reverting to ``pigs'' display.");
413 move(CMDLINE, 0);
414 refresh();
415 failcnt = 0;
416 sleep(5);
417 command("pigs");
419 return;
421 failcnt = 0;
422 etime /= 1000000.0;
423 etime /= ncpu;
424 if (etime == 0)
425 etime = 1;
426 inttotal = 0;
427 for (i = 0; i < nintr; i++) {
428 if (s.intrcnt[i] == 0)
429 continue;
430 if (intrloc[i] == 0) {
431 if (nextintsrow == LINES)
432 continue;
433 intrloc[i] = nextintsrow++;
434 mvprintw(intrloc[i], INTSCOL + 9, "%-10.10s",
435 intrname[i]);
437 X(intrcnt);
438 l = (long)((float)s.intrcnt[i]/etime + 0.5);
439 inttotal += l;
440 put64(l, intrloc[i], INTSCOL + 2, 6, 'D');
442 put64(inttotal, INTSROW + 1, INTSCOL + 2, 6, 'D');
443 Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
444 Z(ncs_longhits); Z(ncs_longmiss); Z(ncs_neghits);
445 s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
446 nchtotal.ncs_miss + nchtotal.ncs_neghits;
447 s.nchpathcount = nchtotal.ncs_longhits + nchtotal.ncs_longmiss;
448 if (state == TIME) {
449 s1.nchcount = s.nchcount;
450 s1.nchpathcount = s.nchpathcount;
453 psiz = 0;
454 f2 = 0.0;
455 for (lc = 0; lc < CPUSTATES; lc++) {
456 uint64_t val = *(uint64_t *)(((uint8_t *)&s.cp_time) +
457 cpuoffsets[lc]);
458 f1 = 100.0 * val / total_time;
459 f2 += f1;
460 l = (int) ((f2 + 1.0) / 2.0) - psiz;
461 if (f1 > 99.9)
462 f1 = 99.9; /* no room to display 100.0 */
463 putfloat(f1, GRAPHROW, GRAPHCOL + 10 * lc, 4, 1, 0);
464 move(GRAPHROW + 2, psiz);
465 psiz += l;
466 while (l-- > 0)
467 addch(cpuchar[lc]);
470 put64(ucount(), STATROW, STATCOL, 3, 'D');
471 putfloat(avenrun[0], STATROW, STATCOL + 18, 6, 2, 0);
472 putfloat(avenrun[1], STATROW, STATCOL + 25, 6, 2, 0);
473 putfloat(avenrun[2], STATROW, STATCOL + 32, 6, 2, 0);
474 mvaddstr(STATROW, STATCOL + 53, buf);
475 #define pgtokb(pg) (int64_t)((intmax_t)(pg) * vms.v_page_size / 1024)
476 #define pgtomb(pg) (int64_t)((intmax_t)(pg) * vms.v_page_size / (1024 * 1024))
477 #define pgtob(pg) (int64_t)((intmax_t)(pg) * vms.v_page_size)
478 put64(pgtob(total.t_arm), MEMROW + 2, MEMCOL + 4, 6, 0);
479 put64(pgtob(total.t_armshr), MEMROW + 2, MEMCOL + 11, 6, 0);
480 put64(pgtob(total.t_avm), MEMROW + 2, MEMCOL + 19, 6, 0);
481 put64(pgtob(total.t_avmshr), MEMROW + 2, MEMCOL + 26, 6, 0);
482 put64(pgtob(total.t_rm), MEMROW + 3, MEMCOL + 4, 6, 0);
483 put64(pgtob(total.t_rmshr), MEMROW + 3, MEMCOL + 11, 6, 0);
484 put64(pgtob(total.t_vm), MEMROW + 3, MEMCOL + 19, 6, 0);
485 put64(pgtob(total.t_vmshr), MEMROW + 3, MEMCOL + 26, 6, 0);
486 put64(pgtob(total.t_free), MEMROW + 2, MEMCOL + 34, 6, 0);
487 put64(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 0, 3, 'D');
488 put64(total.t_pw, PROCSROW + 1, PROCSCOL + 3, 3, 'D');
489 put64(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3, 'D');
490 put64(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3, 'D');
491 put64(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3, 'D');
492 if (extended_vm_stats == 0) {
493 PUTRATE(Vmm.v_zfod, VMSTATROW + 0, VMSTATCOL, 7);
495 PUTRATE(Vmm.v_cow_faults, VMSTATROW + 1, VMSTATCOL, 7);
496 put64(pgtob(vms.v_wire_count), VMSTATROW + 2, VMSTATCOL, 7, 0);
497 put64(pgtob(vms.v_active_count), VMSTATROW + 3, VMSTATCOL, 7, 0);
498 put64(pgtob(vms.v_inactive_count), VMSTATROW + 4, VMSTATCOL, 7, 0);
499 put64(pgtob(vms.v_cache_count), VMSTATROW + 5, VMSTATCOL, 7, 0);
500 put64(pgtob(vms.v_free_count), VMSTATROW + 6, VMSTATCOL, 7, 0);
501 PUTRATE(Vmm.v_dfree, VMSTATROW + 7, VMSTATCOL, 7);
502 PUTRATE(Vmm.v_pfree, VMSTATROW + 8, VMSTATCOL, 7);
503 PUTRATE(Vmm.v_reactivated, VMSTATROW + 9, VMSTATCOL, 7);
504 PUTRATE(Vmm.v_pdwakeups, VMSTATROW + 10, VMSTATCOL, 7);
505 PUTRATE(Vmm.v_pdpages, VMSTATROW + 11, VMSTATCOL, 7);
506 PUTRATE(Vmm.v_intrans, VMSTATROW + 12, VMSTATCOL, 7);
508 if (extended_vm_stats) {
509 PUTRATE(Vmm.v_zfod, VMSTATROW + 11, VMSTATCOL - 16, 9);
510 PUTRATE(Vmm.v_ozfod, VMSTATROW + 12, VMSTATCOL - 16, 9);
511 #define nz(x) ((x) ? (x) : 1)
512 put64((s.Vmm.v_zfod - s.Vmm.v_ozfod) * 100 / nz(s.Vmm.v_zfod),
513 VMSTATROW + 13, VMSTATCOL - 16, 9, 'D');
514 #undef nz
515 PUTRATE(Vmm.v_tfree, VMSTATROW + 14, VMSTATCOL - 16, 9);
518 put64(s.bufspace, VMSTATROW + 13, VMSTATCOL, 7, 0);
519 put64(s.dirtybufspace/1024, VMSTATROW + 14, VMSTATCOL, 7, 'k');
520 put64(s.desiredvnodes, VMSTATROW + 15, VMSTATCOL, 7, 'D');
521 put64(s.numvnodes, VMSTATROW + 16, VMSTATCOL, 7, 'D');
522 put64(s.freevnodes, VMSTATROW + 17, VMSTATCOL, 7, 'D');
523 PUTRATE(Vmm.v_vnodein, PAGEROW + 2, PAGECOL + 6, 4);
524 PUTRATE(Vmm.v_vnodeout, PAGEROW + 2, PAGECOL + 11, 4);
525 PUTRATE(Vmm.v_swapin, PAGEROW + 2, PAGECOL + 18, 4);
526 PUTRATE(Vmm.v_swapout, PAGEROW + 2, PAGECOL + 23, 4);
527 PUTRATE(Vmm.v_vnodepgsin, PAGEROW + 3, PAGECOL + 6, 4);
528 PUTRATE(Vmm.v_vnodepgsout, PAGEROW + 3, PAGECOL + 11, 4);
529 PUTRATE(Vmm.v_swappgsin, PAGEROW + 3, PAGECOL + 18, 4);
530 PUTRATE(Vmm.v_swappgsout, PAGEROW + 3, PAGECOL + 23, 4);
531 PUTRATE(Vmm.v_swtch, GENSTATROW + 1, GENSTATCOL + 1, 4);
532 PUTRATE(Vmm.v_trap, GENSTATROW + 1, GENSTATCOL + 6, 4);
533 PUTRATE(Vmm.v_syscall, GENSTATROW + 1, GENSTATCOL + 11, 4);
534 PUTRATE(Vmm.v_intr, GENSTATROW + 1, GENSTATCOL + 16, 4);
535 PUTRATE(Vmm.v_soft, GENSTATROW + 1, GENSTATCOL + 21, 4);
536 PUTRATE(Vmm.v_vm_faults, GENSTATROW + 1, GENSTATCOL + 26, 4);
537 mvprintw(DISKROW, DISKCOL + 5, " ");
538 for (i = 0, lc = 0; i < num_devices && lc < MAXDRIVES; i++)
539 if (dev_select[i].selected) {
540 char tmpstr[80];
541 sprintf(tmpstr, "%s%d", dev_select[i].device_name,
542 dev_select[i].unit_number);
543 mvprintw(DISKROW, DISKCOL + 5 + 6 * lc,
544 " %5.5s", tmpstr);
545 switch(state) {
546 case TIME:
547 dinfo(i, ++lc, &cur, &last);
548 break;
549 case RUN:
550 dinfo(i, ++lc, &cur, &run);
551 break;
552 case BOOT:
553 dinfo(i, ++lc, &cur, NULL);
554 break;
557 #define nz(x) ((x) ? (x) : 1)
558 put64(s.nchpathcount, NAMEIROW + 1, NAMEICOL + 6, 6, 'D');
559 put64(nchtotal.ncs_longhits, NAMEIROW + 1, NAMEICOL + 13, 6, 'D');
560 putfloat(nchtotal.ncs_longhits * 100.0 / nz(s.nchpathcount),
561 NAMEIROW + 1, NAMEICOL + 19, 4, 0, 0);
563 putfloat((double)s.nchcount / nz(s.nchpathcount),
564 NAMEIROW + 1, NAMEICOL + 27, 5, 2, 1);
565 #undef nz
569 cmdkre(const char *cmd, char *args)
571 int retval;
573 if (prefix(cmd, "run")) {
574 retval = 1;
575 copyinfo(&s2, &s1);
576 switch (getdevs(&run)) {
577 case -1:
578 errx(1, "%s", devstat_errbuf);
579 break;
580 case 1:
581 num_devices = run.dinfo->numdevs;
582 generation = run.dinfo->generation;
583 retval = dscmd("refresh", NULL, MAXDRIVES, &cur);
584 if (retval == 2)
585 labelkre();
586 break;
587 default:
588 break;
590 state = RUN;
591 return (retval);
593 if (prefix(cmd, "boot")) {
594 state = BOOT;
595 copyinfo(&z, &s1);
596 return (1);
598 if (prefix(cmd, "time")) {
599 state = TIME;
600 return (1);
602 if (prefix(cmd, "zero")) {
603 retval = 1;
604 if (state == RUN) {
605 getinfo(&s1);
606 switch (getdevs(&run)) {
607 case -1:
608 errx(1, "%s", devstat_errbuf);
609 break;
610 case 1:
611 num_devices = run.dinfo->numdevs;
612 generation = run.dinfo->generation;
613 retval = dscmd("refresh",NULL, MAXDRIVES, &cur);
614 if (retval == 2)
615 labelkre();
616 break;
617 default:
618 break;
621 return (retval);
623 retval = dscmd(cmd, args, MAXDRIVES, &cur);
625 if (retval == 2)
626 labelkre();
628 return(retval);
631 /* calculate number of users on the system */
632 static int
633 ucount(void)
635 struct utmpentry *ep;
636 int nusers = 0;
638 getutentries(NULL, &ep);
639 for (; ep; ep = ep->next)
640 nusers++;
642 return (nusers);
645 static void
646 put64(intmax_t n, int l, int lc, int w, int type)
648 char b[128];
649 int isneg;
650 int i;
651 int64_t d;
652 int64_t u;
654 move(l, lc);
655 if (n == 0) {
656 while (w-- > 0)
657 addch(' ');
658 return;
660 if (type == 0 || type == 'D')
661 snprintf(b, sizeof(b), "%*jd", w, n);
662 else
663 snprintf(b, sizeof(b), "%*jd%c", w - 1, n, type);
664 if (strlen(b) <= (size_t)w) {
665 addstr(b);
666 return;
669 if (type == 'D')
670 u = 1000;
671 else
672 u = 1024;
673 if (n < 0) {
674 n = -n;
675 isneg = 1;
676 } else {
677 isneg = 0;
680 for (d = 1; n / d >= 1000; d *= u) {
681 switch(type) {
682 case 'D':
683 case 0:
684 type = 'k';
685 break;
686 case 'k':
687 type = 'M';
688 break;
689 case 'M':
690 type = 'G';
691 break;
692 case 'G':
693 type = 'T';
694 break;
695 case 'T':
696 type = 'X';
697 break;
698 default:
699 type = '?';
700 break;
704 i = w - isneg;
705 if (n / d >= 100)
706 i -= 3;
707 else if (n / d >= 10)
708 i -= 2;
709 else
710 i -= 1;
711 if (i > 4) {
712 snprintf(b + 64, sizeof(b) - 64, "%jd.%03jd%c",
713 n / d, n / (d / 1000) % 1000, type);
714 } else if (i > 3) {
715 snprintf(b + 64, sizeof(b) - 64, "%jd.%02jd%c",
716 n / d, n / (d / 100) % 100, type);
717 } else if (i > 2) {
718 snprintf(b + 64, sizeof(b) - 64, "%jd.%01jd%c",
719 n / d, n / (d / 10) % 10, type);
720 } else {
721 snprintf(b + 64, sizeof(b) - 64, "%jd%c",
722 n / d, type);
724 w -= strlen(b + 64);
725 i = 64;
726 if (isneg) {
727 b[--i] = '-';
728 --w;
730 while (w > 0) {
731 --w;
732 b[--i] = ' ';
734 addstr(b + i);
737 static void
738 putfloat(double f, int l, int lc, int w, int d, int nz)
740 char b[128];
742 move(l, lc);
743 if (nz && f == 0.0) {
744 while (--w >= 0)
745 addch(' ');
746 return;
748 snprintf(b, sizeof(b), "%*.*f", w, d, f);
749 if (strlen(b) > (size_t)w)
750 snprintf(b, sizeof(b), "%*.0f", w, f);
751 if (strlen(b) > (size_t)w) {
752 while (--w >= 0)
753 addch('*');
754 return;
756 addstr(b);
759 static void
760 putlongdouble(long double f, int l, int lc, int w, int d, int nz)
762 char b[128];
764 move(l, lc);
765 if (nz && f == 0.0) {
766 while (--w >= 0)
767 addch(' ');
768 return;
770 sprintf(b, "%*.*Lf", w, d, f);
771 if (strlen(b) > (size_t)w)
772 sprintf(b, "%*.0Lf", w, f);
773 if (strlen(b) > (size_t)w) {
774 while (--w >= 0)
775 addch('*');
776 return;
778 addstr(b);
781 static void
782 putlongdoublez(long double f, int l, int lc, int w, int d, int nz)
784 char b[128];
786 if (f == 0.0) {
787 move(l, lc);
788 sprintf(b, "%*.*s", w, w, "");
789 addstr(b);
790 } else {
791 putlongdouble(f, l, lc, w, d, nz);
795 static void
796 getinfo(struct Info *ls)
798 struct devinfo *tmp_dinfo;
799 struct nchstats *nch_tmp;
800 size_t size;
801 size_t vms_size = sizeof(ls->Vms);
802 size_t vmm_size = sizeof(ls->Vmm);
803 size_t nch_size = sizeof(ls->nchstats) * SMP_MAXCPU;
805 if (sysctlbyname("vm.vmstats", &ls->Vms, &vms_size, NULL, 0)) {
806 perror("sysctlbyname: vm.vmstats");
807 exit(1);
809 if (sysctlbyname("vm.vmmeter", &ls->Vmm, &vmm_size, NULL, 0)) {
810 perror("sysctlbyname: vm.vmstats");
811 exit(1);
814 if (kinfo_get_sched_cputime(&ls->cp_time))
815 err(1, "kinfo_get_sched_cputime");
816 if (kinfo_get_sched_cputime(&cp_time))
817 err(1, "kinfo_get_sched_cputime");
818 NREAD(X_BUFFERSPACE, &ls->bufspace, sizeof(ls->bufspace));
819 NREAD(X_DESIREDVNODES, &ls->desiredvnodes, sizeof(ls->desiredvnodes));
820 NREAD(X_NUMVNODES, &ls->numvnodes, sizeof(ls->numvnodes));
821 NREAD(X_FREEVNODES, &ls->freevnodes, sizeof(ls->freevnodes));
822 NREAD(X_NUMDIRTYBUFFERS, &ls->dirtybufspace, sizeof(ls->dirtybufspace));
824 if (nintr) {
825 size = nintr * sizeof(ls->intrcnt[0]);
826 sysctlbyname("hw.intrcnt_all", ls->intrcnt, &size, NULL, 0);
828 size = sizeof(ls->Total);
829 if (sysctlbyname("vm.vmtotal", &ls->Total, &size, NULL, 0) < 0) {
830 error("Can't get kernel info: %s\n", strerror(errno));
831 bzero(&ls->Total, sizeof(ls->Total));
834 if ((nch_tmp = malloc(nch_size)) == NULL) {
835 perror("malloc");
836 exit(1);
837 } else {
838 if (sysctlbyname("vfs.cache.nchstats", nch_tmp, &nch_size, NULL, 0)) {
839 perror("sysctlbyname vfs.cache.nchstats");
840 free(nch_tmp);
841 exit(1);
842 } else {
843 if ((nch_tmp = realloc(nch_tmp, nch_size)) == NULL) {
844 perror("realloc");
845 exit(1);
850 if (kinfo_get_cpus(&ncpu))
851 err(1, "kinfo_get_cpus");
852 kvm_nch_cpuagg(nch_tmp, &ls->nchstats, ncpu);
853 free(nch_tmp);
855 tmp_dinfo = last.dinfo;
856 last.dinfo = cur.dinfo;
857 cur.dinfo = tmp_dinfo;
859 last.busy_time = cur.busy_time;
860 switch (getdevs(&cur)) {
861 case -1:
862 errx(1, "%s", devstat_errbuf);
863 break;
864 case 1:
865 num_devices = cur.dinfo->numdevs;
866 generation = cur.dinfo->generation;
867 cmdkre("refresh", NULL);
868 break;
869 default:
870 break;
874 static void
875 allocinfo(struct Info *ls)
877 ls->intrcnt = (long *) calloc(nintr, sizeof(long));
878 if (ls->intrcnt == NULL)
879 errx(2, "out of memory");
882 static void
883 copyinfo(struct Info *from, struct Info *to)
885 long *intrcnt;
888 * time, wds, seek, and xfer are malloc'd so we have to
889 * save the pointers before the structure copy and then
890 * copy by hand.
892 intrcnt = to->intrcnt;
893 *to = *from;
895 bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
898 static void
899 dinfo(int dn, int lc, struct statinfo *now, struct statinfo *then)
901 long double kb_per_transfer;
902 long double transfers_per_secondr;
903 long double transfers_per_secondw;
904 long double mb_per_secondr;
905 long double mb_per_secondw;
906 long double elapsed_time, device_busy;
907 int di;
909 di = dev_select[dn].position;
911 elapsed_time = compute_etime(now->busy_time, then ?
912 then->busy_time :
913 now->dinfo->devices[di].dev_creation_time);
915 device_busy = compute_etime(now->dinfo->devices[di].busy_time, then ?
916 then->dinfo->devices[di].busy_time :
917 now->dinfo->devices[di].dev_creation_time);
919 if (compute_stats(
920 &now->dinfo->devices[di],
921 (then ? &then->dinfo->devices[di] : NULL),
922 elapsed_time,
923 NULL, NULL, NULL,
924 &kb_per_transfer,
925 NULL,
926 NULL,
927 NULL, NULL) != 0)
928 errx(1, "%s", devstat_errbuf);
930 if (compute_stats_read(
931 &now->dinfo->devices[di],
932 (then ? &then->dinfo->devices[di] : NULL),
933 elapsed_time,
934 NULL, NULL, NULL,
935 NULL,
936 &transfers_per_secondr,
937 &mb_per_secondr,
938 NULL, NULL) != 0)
939 errx(1, "%s", devstat_errbuf);
941 if (compute_stats_write(
942 &now->dinfo->devices[di],
943 (then ? &then->dinfo->devices[di] : NULL),
944 elapsed_time,
945 NULL, NULL, NULL,
946 NULL,
947 &transfers_per_secondw,
948 &mb_per_secondw,
949 NULL, NULL) != 0)
950 errx(1, "%s", devstat_errbuf);
952 #if 0
954 * Remove this hack, it no longer works properly and will
955 * report 100% busy in situations where the device is able
956 * to respond to the requests faster than the busy counter's
957 * granularity.
959 if ((device_busy == 0) &&
960 (transfers_per_secondr > 5 || transfers_per_secondw > 5)) {
961 /* the device has been 100% busy, fake it because
962 * as long as the device is 100% busy the busy_time
963 * field in the devstat struct is not updated */
964 device_busy = elapsed_time;
966 #endif
967 if (device_busy > elapsed_time) {
968 /* this normally happens after one or more periods
969 * where the device has been 100% busy, correct it */
970 device_busy = elapsed_time;
973 lc = DISKCOL + lc * 6;
974 putlongdoublez(kb_per_transfer, DISKROW + 1, lc, 5, 2, 0);
975 putlongdoublez(transfers_per_secondr, DISKROW + 2, lc, 5, 0, 0);
976 putlongdoublez(mb_per_secondr, DISKROW + 3, lc, 5, 2, 0);
977 putlongdoublez(transfers_per_secondw, DISKROW + 4, lc, 5, 0, 0);
978 putlongdoublez(mb_per_secondw, DISKROW + 5, lc, 5, 2, 0);
979 putlongdouble(device_busy * 100 / elapsed_time,
980 DISKROW + 6, lc, 5, 0, 0);