vmstat.c revision 1.33 1 1.33 ad /* $NetBSD: vmstat.c,v 1.33 2000/07/05 11:03:23 ad Exp $ */
2 1.2 jtc
3 1.1 jtc /*-
4 1.1 jtc * Copyright (c) 1983, 1989, 1992, 1993
5 1.1 jtc * The Regents of the University of California. All rights reserved.
6 1.1 jtc *
7 1.1 jtc * Redistribution and use in source and binary forms, with or without
8 1.1 jtc * modification, are permitted provided that the following conditions
9 1.1 jtc * are met:
10 1.1 jtc * 1. Redistributions of source code must retain the above copyright
11 1.1 jtc * notice, this list of conditions and the following disclaimer.
12 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jtc * notice, this list of conditions and the following disclaimer in the
14 1.1 jtc * documentation and/or other materials provided with the distribution.
15 1.1 jtc * 3. All advertising materials mentioning features or use of this software
16 1.1 jtc * must display the following acknowledgement:
17 1.1 jtc * This product includes software developed by the University of
18 1.1 jtc * California, Berkeley and its contributors.
19 1.1 jtc * 4. Neither the name of the University nor the names of its contributors
20 1.1 jtc * may be used to endorse or promote products derived from this software
21 1.1 jtc * without specific prior written permission.
22 1.1 jtc *
23 1.1 jtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 jtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 jtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 jtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 jtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 jtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 jtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 jtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 jtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 jtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 jtc * SUCH DAMAGE.
34 1.1 jtc */
35 1.1 jtc
36 1.8 mrg #include <sys/cdefs.h>
37 1.1 jtc #ifndef lint
38 1.2 jtc #if 0
39 1.1 jtc static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 1/12/94";
40 1.2 jtc #endif
41 1.33 ad __RCSID("$NetBSD: vmstat.c,v 1.33 2000/07/05 11:03:23 ad Exp $");
42 1.1 jtc #endif /* not lint */
43 1.1 jtc
44 1.1 jtc /*
45 1.1 jtc * Cursed vmstat -- from Robert Elz.
46 1.1 jtc */
47 1.1 jtc
48 1.1 jtc #include <sys/param.h>
49 1.1 jtc #include <sys/dkstat.h>
50 1.1 jtc #include <sys/buf.h>
51 1.1 jtc #include <sys/stat.h>
52 1.1 jtc #include <sys/time.h>
53 1.1 jtc #include <sys/user.h>
54 1.1 jtc #include <sys/proc.h>
55 1.1 jtc #include <sys/namei.h>
56 1.26 perry #include <sys/sched.h>
57 1.1 jtc #include <sys/sysctl.h>
58 1.1 jtc
59 1.11 mrg #include <uvm/uvm_extern.h>
60 1.11 mrg
61 1.3 mycroft #include <ctype.h>
62 1.3 mycroft #include <err.h>
63 1.1 jtc #include <nlist.h>
64 1.1 jtc #include <paths.h>
65 1.3 mycroft #include <signal.h>
66 1.3 mycroft #include <stdlib.h>
67 1.1 jtc #include <string.h>
68 1.3 mycroft #include <utmp.h>
69 1.1 jtc #include <unistd.h>
70 1.3 mycroft
71 1.1 jtc #include "systat.h"
72 1.1 jtc #include "extern.h"
73 1.1 jtc
74 1.1 jtc static struct Info {
75 1.25 simonb u_int64_t time[CPUSTATES];
76 1.11 mrg struct uvmexp uvmexp;
77 1.1 jtc struct vmtotal Total;
78 1.1 jtc struct nchstats nchstats;
79 1.1 jtc long nchcount;
80 1.1 jtc long *intrcnt;
81 1.1 jtc } s, s1, s2, z;
82 1.1 jtc
83 1.5 thorpej #include "dkstats.h"
84 1.5 thorpej extern struct _disk cur;
85 1.5 thorpej
86 1.5 thorpej
87 1.1 jtc #define cnt s.Cnt
88 1.1 jtc #define oldcnt s1.Cnt
89 1.1 jtc #define total s.Total
90 1.1 jtc #define nchtotal s.nchstats
91 1.1 jtc #define oldnchtotal s1.nchstats
92 1.1 jtc
93 1.1 jtc static enum state { BOOT, TIME, RUN } state = TIME;
94 1.1 jtc
95 1.33 ad static void allocinfo(struct Info *);
96 1.33 ad static void copyinfo(struct Info *, struct Info *);
97 1.33 ad static float cputime(int);
98 1.33 ad static void dinfo(int, int);
99 1.33 ad static void getinfo(struct Info *, enum state);
100 1.33 ad static void putint(int, int, int, int);
101 1.33 ad static void putfloat(double, int, int, int, int, int);
102 1.33 ad static int ucount(void);
103 1.1 jtc
104 1.1 jtc static int ut;
105 1.1 jtc static char buf[26];
106 1.25 simonb static u_int64_t t;
107 1.1 jtc static double etime;
108 1.1 jtc static float hertz;
109 1.1 jtc static int nintr;
110 1.1 jtc static long *intrloc;
111 1.1 jtc static char **intrname;
112 1.1 jtc static int nextintsrow;
113 1.1 jtc
114 1.1 jtc struct utmp utmp;
115 1.1 jtc
116 1.1 jtc WINDOW *
117 1.33 ad openvmstat(void)
118 1.1 jtc {
119 1.1 jtc
120 1.1 jtc ut = open(_PATH_UTMP, O_RDONLY);
121 1.1 jtc if (ut < 0)
122 1.1 jtc error("No utmp");
123 1.1 jtc return (stdscr);
124 1.1 jtc }
125 1.1 jtc
126 1.1 jtc void
127 1.33 ad closevmstat(WINDOW *w)
128 1.1 jtc {
129 1.1 jtc
130 1.1 jtc (void) close(ut);
131 1.1 jtc if (w == NULL)
132 1.1 jtc return;
133 1.1 jtc wclear(w);
134 1.1 jtc wrefresh(w);
135 1.1 jtc }
136 1.1 jtc
137 1.1 jtc
138 1.1 jtc static struct nlist namelist[] = {
139 1.27 thorpej #define X_TOTAL 0
140 1.1 jtc { "_total" },
141 1.27 thorpej #define X_NCHSTATS 1
142 1.1 jtc { "_nchstats" },
143 1.27 thorpej #define X_INTRNAMES 2
144 1.1 jtc { "_intrnames" },
145 1.27 thorpej #define X_EINTRNAMES 3
146 1.1 jtc { "_eintrnames" },
147 1.27 thorpej #define X_INTRCNT 4
148 1.1 jtc { "_intrcnt" },
149 1.27 thorpej #define X_EINTRCNT 5
150 1.1 jtc { "_eintrcnt" },
151 1.1 jtc { "" },
152 1.1 jtc };
153 1.1 jtc
154 1.1 jtc /*
155 1.1 jtc * These constants define where the major pieces are laid out
156 1.1 jtc */
157 1.1 jtc #define STATROW 0 /* uses 1 row and 68 cols */
158 1.1 jtc #define STATCOL 2
159 1.1 jtc #define MEMROW 2 /* uses 4 rows and 31 cols */
160 1.1 jtc #define MEMCOL 0
161 1.1 jtc #define PAGEROW 2 /* uses 4 rows and 26 cols */
162 1.1 jtc #define PAGECOL 36
163 1.1 jtc #define INTSROW 2 /* uses all rows to bottom and 17 cols */
164 1.1 jtc #define INTSCOL 63
165 1.1 jtc #define PROCSROW 7 /* uses 2 rows and 20 cols */
166 1.1 jtc #define PROCSCOL 0
167 1.1 jtc #define GENSTATROW 7 /* uses 2 rows and 30 cols */
168 1.1 jtc #define GENSTATCOL 20
169 1.1 jtc #define VMSTATROW 7 /* uses 17 rows and 12 cols */
170 1.1 jtc #define VMSTATCOL 48
171 1.1 jtc #define GRAPHROW 10 /* uses 3 rows and 51 cols */
172 1.1 jtc #define GRAPHCOL 0
173 1.1 jtc #define NAMEIROW 14 /* uses 3 rows and 38 cols */
174 1.1 jtc #define NAMEICOL 0
175 1.1 jtc #define DISKROW 18 /* uses 5 rows and 50 cols (for 9 drives) */
176 1.1 jtc #define DISKCOL 0
177 1.1 jtc
178 1.1 jtc #define DRIVESPACE 9 /* max # for space */
179 1.1 jtc
180 1.1 jtc #if DK_NDRIVE > DRIVESPACE
181 1.1 jtc #define MAXDRIVES DRIVESPACE /* max # to display */
182 1.1 jtc #else
183 1.1 jtc #define MAXDRIVES DK_NDRIVE /* max # to display */
184 1.1 jtc #endif
185 1.1 jtc
186 1.1 jtc int
187 1.33 ad initvmstat(void)
188 1.1 jtc {
189 1.1 jtc char *intrnamebuf, *cp;
190 1.1 jtc int i;
191 1.1 jtc static int once = 0;
192 1.15 drochner extern gid_t egid;
193 1.1 jtc
194 1.1 jtc if (namelist[0].n_type == 0) {
195 1.1 jtc if (kvm_nlist(kd, namelist)) {
196 1.1 jtc nlisterr(namelist);
197 1.1 jtc return(0);
198 1.1 jtc }
199 1.1 jtc if (namelist[0].n_type == 0) {
200 1.1 jtc error("No namelist");
201 1.1 jtc return(0);
202 1.1 jtc }
203 1.1 jtc }
204 1.1 jtc hertz = stathz ? stathz : hz;
205 1.15 drochner if (! dkinit(1, egid))
206 1.1 jtc return(0);
207 1.1 jtc if (dk_ndrive && !once) {
208 1.1 jtc #define allocate(e, t) \
209 1.7 scottr s./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
210 1.7 scottr s1./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
211 1.7 scottr s2./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
212 1.7 scottr z./**/e = (t *)calloc(dk_ndrive, sizeof (t));
213 1.1 jtc once = 1;
214 1.1 jtc #undef allocate
215 1.1 jtc }
216 1.1 jtc if (nintr == 0) {
217 1.1 jtc nintr = (namelist[X_EINTRCNT].n_value -
218 1.1 jtc namelist[X_INTRCNT].n_value) / sizeof (long);
219 1.1 jtc intrloc = calloc(nintr, sizeof (long));
220 1.1 jtc intrname = calloc(nintr, sizeof (long));
221 1.1 jtc intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
222 1.1 jtc namelist[X_INTRNAMES].n_value);
223 1.23 jwise if (intrnamebuf == NULL || intrname == 0 || intrloc == 0) {
224 1.1 jtc error("Out of memory\n");
225 1.1 jtc if (intrnamebuf)
226 1.1 jtc free(intrnamebuf);
227 1.1 jtc if (intrname)
228 1.1 jtc free(intrname);
229 1.1 jtc if (intrloc)
230 1.1 jtc free(intrloc);
231 1.1 jtc nintr = 0;
232 1.1 jtc return(0);
233 1.1 jtc }
234 1.1 jtc NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
235 1.1 jtc NVAL(X_INTRNAMES));
236 1.1 jtc for (cp = intrnamebuf, i = 0; i < nintr; i++) {
237 1.1 jtc intrname[i] = cp;
238 1.1 jtc cp += strlen(cp) + 1;
239 1.1 jtc }
240 1.1 jtc nextintsrow = INTSROW + 2;
241 1.1 jtc allocinfo(&s);
242 1.1 jtc allocinfo(&s1);
243 1.1 jtc allocinfo(&s2);
244 1.1 jtc allocinfo(&z);
245 1.1 jtc }
246 1.1 jtc getinfo(&s2, RUN);
247 1.1 jtc copyinfo(&s2, &s1);
248 1.1 jtc return(1);
249 1.1 jtc }
250 1.1 jtc
251 1.1 jtc void
252 1.33 ad fetchvmstat(void)
253 1.1 jtc {
254 1.1 jtc time_t now;
255 1.1 jtc
256 1.1 jtc time(&now);
257 1.1 jtc strcpy(buf, ctime(&now));
258 1.18 marc buf[19] = '\0';
259 1.1 jtc getinfo(&s, state);
260 1.1 jtc }
261 1.1 jtc
262 1.1 jtc void
263 1.33 ad labelvmstat(void)
264 1.1 jtc {
265 1.10 lukem int i, j;
266 1.1 jtc
267 1.1 jtc clear();
268 1.1 jtc mvprintw(STATROW, STATCOL + 4, "users Load");
269 1.13 mrg mvprintw(MEMROW, MEMCOL, " memory totals (in KB)");
270 1.13 mrg mvprintw(MEMROW + 1, MEMCOL, " real virtual free");
271 1.13 mrg mvprintw(MEMROW + 2, MEMCOL, "Active");
272 1.13 mrg mvprintw(MEMROW + 3, MEMCOL, "All");
273 1.13 mrg
274 1.13 mrg mvprintw(PAGEROW, PAGECOL, " PAGING SWAPPING ");
275 1.13 mrg mvprintw(PAGEROW + 1, PAGECOL, " in out in out ");
276 1.13 mrg mvprintw(PAGEROW + 2, PAGECOL, "ops");
277 1.13 mrg mvprintw(PAGEROW + 3, PAGECOL, "pages");
278 1.1 jtc
279 1.1 jtc mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
280 1.1 jtc mvprintw(INTSROW + 1, INTSCOL + 9, "total");
281 1.1 jtc
282 1.12 mrg mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks");
283 1.12 mrg mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw");
284 1.12 mrg mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm");
285 1.12 mrg mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait");
286 1.12 mrg mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck");
287 1.12 mrg mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok");
288 1.12 mrg mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram");
289 1.12 mrg mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy");
290 1.12 mrg mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp");
291 1.12 mrg mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod");
292 1.12 mrg mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow");
293 1.12 mrg mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin");
294 1.12 mrg mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg");
295 1.12 mrg mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg");
296 1.12 mrg mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired");
297 1.12 mrg mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre");
298 1.12 mrg if (LINES - 1 > VMSTATROW + 16)
299 1.12 mrg mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn");
300 1.1 jtc
301 1.1 jtc mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt");
302 1.1 jtc
303 1.1 jtc mvprintw(GRAPHROW, GRAPHCOL,
304 1.28 mycroft " . %% Sy . %% Us . %% Ni . %% In . %% Id");
305 1.13 mrg mvprintw(PROCSROW, PROCSCOL, "Proc:r d s w");
306 1.1 jtc mvprintw(GRAPHROW + 1, GRAPHCOL,
307 1.1 jtc "| | | | | | | | | | |");
308 1.1 jtc
309 1.1 jtc mvprintw(NAMEIROW, NAMEICOL, "Namei Sys-cache Proc-cache");
310 1.1 jtc mvprintw(NAMEIROW + 1, NAMEICOL,
311 1.1 jtc " Calls hits %% hits %%");
312 1.1 jtc mvprintw(DISKROW, DISKCOL, "Discs");
313 1.1 jtc mvprintw(DISKROW + 1, DISKCOL, "seeks");
314 1.1 jtc mvprintw(DISKROW + 2, DISKCOL, "xfers");
315 1.5 thorpej mvprintw(DISKROW + 3, DISKCOL, "Kbyte");
316 1.31 mycroft mvprintw(DISKROW + 4, DISKCOL, "%%busy");
317 1.1 jtc j = 0;
318 1.1 jtc for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++)
319 1.1 jtc if (dk_select[i]) {
320 1.1 jtc mvprintw(DISKROW, DISKCOL + 5 + 5 * j,
321 1.20 soren " %4.4s", dr_name[j]);
322 1.1 jtc j++;
323 1.1 jtc }
324 1.1 jtc for (i = 0; i < nintr; i++) {
325 1.1 jtc if (intrloc[i] == 0)
326 1.1 jtc continue;
327 1.1 jtc mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
328 1.1 jtc }
329 1.1 jtc }
330 1.1 jtc
331 1.1 jtc #define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
332 1.1 jtc #define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
333 1.1 jtc #define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
334 1.1 jtc if(state == TIME) s1.nchstats.fld = t;}
335 1.16 mycroft #define PUTRATE(fld, l, c, w) {Y(fld); putint((int)((float)s.fld/etime + 0.5), l, c, w);}
336 1.1 jtc #define MAXFAIL 5
337 1.1 jtc
338 1.25 simonb static char cpuchar[CPUSTATES] = { '=' , '>', '-', '%', ' ' };
339 1.25 simonb static char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_INTR, CP_IDLE };
340 1.1 jtc
341 1.1 jtc void
342 1.33 ad showvmstat(void)
343 1.1 jtc {
344 1.1 jtc float f1, f2;
345 1.1 jtc int psiz, inttotal;
346 1.1 jtc int i, l, c;
347 1.1 jtc static int failcnt = 0;
348 1.5 thorpej
349 1.5 thorpej if (state == TIME)
350 1.5 thorpej dkswap();
351 1.1 jtc etime = 0;
352 1.1 jtc for(i = 0; i < CPUSTATES; i++) {
353 1.1 jtc X(time);
354 1.1 jtc etime += s.time[i];
355 1.1 jtc }
356 1.29 mycroft if (etime < 1.0) { /* < 5 ticks - ignore this trash */
357 1.1 jtc if (failcnt++ >= MAXFAIL) {
358 1.1 jtc clear();
359 1.1 jtc mvprintw(2, 10, "The alternate system clock has died!");
360 1.1 jtc mvprintw(3, 10, "Reverting to ``pigs'' display.");
361 1.1 jtc move(CMDLINE, 0);
362 1.1 jtc refresh();
363 1.1 jtc failcnt = 0;
364 1.1 jtc sleep(5);
365 1.1 jtc command("pigs");
366 1.1 jtc }
367 1.1 jtc return;
368 1.1 jtc }
369 1.1 jtc failcnt = 0;
370 1.1 jtc etime /= hertz;
371 1.1 jtc inttotal = 0;
372 1.1 jtc for (i = 0; i < nintr; i++) {
373 1.1 jtc if (s.intrcnt[i] == 0)
374 1.1 jtc continue;
375 1.1 jtc if (intrloc[i] == 0) {
376 1.1 jtc if (nextintsrow == LINES)
377 1.1 jtc continue;
378 1.1 jtc intrloc[i] = nextintsrow++;
379 1.1 jtc mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s",
380 1.1 jtc intrname[i]);
381 1.1 jtc }
382 1.1 jtc X(intrcnt);
383 1.1 jtc l = (int)((float)s.intrcnt[i]/etime + 0.5);
384 1.1 jtc inttotal += l;
385 1.1 jtc putint(l, intrloc[i], INTSCOL, 8);
386 1.1 jtc }
387 1.1 jtc putint(inttotal, INTSROW + 1, INTSCOL, 8);
388 1.1 jtc Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
389 1.1 jtc Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
390 1.1 jtc s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
391 1.1 jtc nchtotal.ncs_miss + nchtotal.ncs_long;
392 1.1 jtc if (state == TIME)
393 1.1 jtc s1.nchcount = s.nchcount;
394 1.1 jtc
395 1.1 jtc psiz = 0;
396 1.1 jtc f2 = 0.0;
397 1.1 jtc
398 1.1 jtc /*
399 1.1 jtc * Last CPU state not calculated yet.
400 1.1 jtc */
401 1.28 mycroft for (c = 0; c < CPUSTATES; c++) {
402 1.1 jtc i = cpuorder[c];
403 1.1 jtc f1 = cputime(i);
404 1.1 jtc f2 += f1;
405 1.1 jtc l = (int) ((f2 + 1.0) / 2.0) - psiz;
406 1.1 jtc if (c == 0)
407 1.1 jtc putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
408 1.1 jtc else
409 1.28 mycroft putfloat(f1, GRAPHROW, GRAPHCOL + 10 * c + 1, 5, 1, 0);
410 1.28 mycroft mvhline(GRAPHROW + 2, psiz, cpuchar[c], l);
411 1.1 jtc psiz += l;
412 1.1 jtc }
413 1.1 jtc
414 1.1 jtc putint(ucount(), STATROW, STATCOL, 3);
415 1.1 jtc putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
416 1.1 jtc putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
417 1.1 jtc putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
418 1.1 jtc mvaddstr(STATROW, STATCOL + 53, buf);
419 1.17 drochner #define pgtokb(pg) ((pg) * (s.uvmexp.pagesize / 1024))
420 1.11 mrg
421 1.13 mrg putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 6, 7);
422 1.12 mrg putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse), /* XXX */
423 1.13 mrg MEMROW + 2, MEMCOL + 16, 7);
424 1.13 mrg putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 6, 7);
425 1.12 mrg putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
426 1.13 mrg MEMROW + 3, MEMCOL + 16, 7);
427 1.13 mrg putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 24, 7);
428 1.12 mrg putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
429 1.13 mrg MEMROW + 3, MEMCOL + 24, 7);
430 1.1 jtc putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
431 1.13 mrg putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3);
432 1.13 mrg putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3);
433 1.13 mrg putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3);
434 1.12 mrg PUTRATE(uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
435 1.12 mrg PUTRATE(uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
436 1.12 mrg PUTRATE(uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
437 1.12 mrg PUTRATE(uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
438 1.12 mrg PUTRATE(uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
439 1.12 mrg PUTRATE(uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
440 1.12 mrg PUTRATE(uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
441 1.12 mrg PUTRATE(uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
442 1.12 mrg PUTRATE(uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
443 1.12 mrg PUTRATE(uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
444 1.12 mrg PUTRATE(uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
445 1.12 mrg putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
446 1.12 mrg putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
447 1.12 mrg putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
448 1.12 mrg putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9);
449 1.12 mrg PUTRATE(uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
450 1.11 mrg if (LINES - 1 > VMSTATROW + 16)
451 1.12 mrg PUTRATE(uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
452 1.12 mrg
453 1.12 mrg PUTRATE(uvmexp.pageins, PAGEROW + 2, PAGECOL + 5, 5);
454 1.11 mrg PUTRATE(uvmexp.pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
455 1.12 mrg PUTRATE(uvmexp.swapins, PAGEROW + 2, PAGECOL + 15, 5);
456 1.12 mrg PUTRATE(uvmexp.swapouts, PAGEROW + 2, PAGECOL + 20, 5);
457 1.12 mrg PUTRATE(uvmexp.pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
458 1.12 mrg PUTRATE(uvmexp.pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
459 1.12 mrg
460 1.11 mrg PUTRATE(uvmexp.swtch, GENSTATROW + 1, GENSTATCOL, 5);
461 1.11 mrg PUTRATE(uvmexp.traps, GENSTATROW + 1, GENSTATCOL + 5, 5);
462 1.11 mrg PUTRATE(uvmexp.syscalls, GENSTATROW + 1, GENSTATCOL + 10, 5);
463 1.11 mrg PUTRATE(uvmexp.intrs, GENSTATROW + 1, GENSTATCOL + 15, 5);
464 1.11 mrg PUTRATE(uvmexp.softs, GENSTATROW + 1, GENSTATCOL + 20, 5);
465 1.11 mrg PUTRATE(uvmexp.faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
466 1.1 jtc mvprintw(DISKROW, DISKCOL + 5, " ");
467 1.1 jtc for (i = 0, c = 0; i < dk_ndrive && c < MAXDRIVES; i++)
468 1.1 jtc if (dk_select[i]) {
469 1.1 jtc mvprintw(DISKROW, DISKCOL + 5 + 5 * c,
470 1.20 soren " %4.4s", dr_name[i]);
471 1.1 jtc dinfo(i, ++c);
472 1.1 jtc }
473 1.1 jtc putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
474 1.1 jtc putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
475 1.1 jtc #define nz(x) ((x) ? (x) : 1)
476 1.1 jtc putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
477 1.1 jtc NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
478 1.1 jtc putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
479 1.1 jtc putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
480 1.1 jtc NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
481 1.1 jtc #undef nz
482 1.1 jtc }
483 1.1 jtc
484 1.22 jwise void
485 1.33 ad vmstat_boot(char *args)
486 1.22 jwise {
487 1.22 jwise copyinfo(&z, &s1);
488 1.22 jwise state = BOOT;
489 1.22 jwise }
490 1.22 jwise
491 1.22 jwise void
492 1.33 ad vmstat_run(char *args)
493 1.22 jwise {
494 1.22 jwise copyinfo(&s1, &s2);
495 1.22 jwise state = RUN;
496 1.22 jwise }
497 1.22 jwise
498 1.22 jwise void
499 1.22 jwise vmstat_time (args)
500 1.22 jwise char * args;
501 1.1 jtc {
502 1.22 jwise state = TIME;
503 1.22 jwise }
504 1.1 jtc
505 1.22 jwise void
506 1.33 ad vmstat_zero(char *args)
507 1.22 jwise {
508 1.22 jwise if (state == RUN)
509 1.22 jwise getinfo(&s1, RUN);
510 1.1 jtc }
511 1.1 jtc
512 1.1 jtc /* calculate number of users on the system */
513 1.1 jtc static int
514 1.33 ad ucount(void)
515 1.1 jtc {
516 1.21 mrg int nusers = 0, onusers = -1;
517 1.1 jtc
518 1.1 jtc if (ut < 0)
519 1.1 jtc return (0);
520 1.1 jtc while (read(ut, &utmp, sizeof(utmp)))
521 1.1 jtc if (utmp.ut_name[0] != '\0')
522 1.1 jtc nusers++;
523 1.1 jtc
524 1.21 mrg if (nusers != onusers) {
525 1.21 mrg if (nusers == 1)
526 1.21 mrg mvprintw(STATROW, STATCOL + 8, " ");
527 1.21 mrg else
528 1.21 mrg mvprintw(STATROW, STATCOL + 8, "s");
529 1.21 mrg }
530 1.9 kleink lseek(ut, (off_t)0, SEEK_SET);
531 1.21 mrg onusers = nusers;
532 1.1 jtc return (nusers);
533 1.1 jtc }
534 1.1 jtc
535 1.1 jtc static float
536 1.33 ad cputime(int indx)
537 1.1 jtc {
538 1.1 jtc double t;
539 1.10 lukem int i;
540 1.1 jtc
541 1.1 jtc t = 0;
542 1.1 jtc for (i = 0; i < CPUSTATES; i++)
543 1.1 jtc t += s.time[i];
544 1.1 jtc if (t == 0.0)
545 1.1 jtc t = 1.0;
546 1.1 jtc return (s.time[indx] * 100.0 / t);
547 1.1 jtc }
548 1.1 jtc
549 1.1 jtc static void
550 1.33 ad putint(int n, int l, int c, int w)
551 1.1 jtc {
552 1.1 jtc char b[128];
553 1.1 jtc
554 1.1 jtc move(l, c);
555 1.1 jtc if (n == 0) {
556 1.30 mycroft hline(' ', w);
557 1.1 jtc return;
558 1.1 jtc }
559 1.14 mrg (void)snprintf(b, sizeof b, "%*d", w, n);
560 1.1 jtc if (strlen(b) > w) {
561 1.30 mycroft hline('*', w);
562 1.1 jtc return;
563 1.1 jtc }
564 1.1 jtc addstr(b);
565 1.1 jtc }
566 1.1 jtc
567 1.1 jtc static void
568 1.33 ad putfloat(double f, int l, int c, int w, int d, int nz)
569 1.1 jtc {
570 1.1 jtc char b[128];
571 1.1 jtc
572 1.1 jtc move(l, c);
573 1.1 jtc if (nz && f == 0.0) {
574 1.30 mycroft hline(' ', w);
575 1.1 jtc return;
576 1.1 jtc }
577 1.14 mrg (void)snprintf(b, sizeof b, "%*.*f", w, d, f);
578 1.1 jtc if (strlen(b) > w) {
579 1.30 mycroft hline('*', w);
580 1.1 jtc return;
581 1.1 jtc }
582 1.1 jtc addstr(b);
583 1.1 jtc }
584 1.1 jtc
585 1.1 jtc static void
586 1.33 ad getinfo(struct Info *s, enum state st)
587 1.1 jtc {
588 1.4 cgd int mib[2];
589 1.4 cgd size_t size;
590 1.1 jtc
591 1.5 thorpej dkreadstats();
592 1.27 thorpej (void) fetch_cptime(s->time);
593 1.1 jtc NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
594 1.1 jtc NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
595 1.11 mrg size = sizeof(s->uvmexp);
596 1.11 mrg mib[0] = CTL_VM;
597 1.11 mrg mib[1] = VM_UVMEXP;
598 1.11 mrg if (sysctl(mib, 2, &s->uvmexp, &size, NULL, 0) < 0) {
599 1.11 mrg error("can't get uvmexp: %s\n", strerror(errno));
600 1.11 mrg memset(&s->uvmexp, 0, sizeof(s->uvmexp));
601 1.11 mrg }
602 1.1 jtc size = sizeof(s->Total);
603 1.1 jtc mib[0] = CTL_VM;
604 1.1 jtc mib[1] = VM_METER;
605 1.1 jtc if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
606 1.1 jtc error("Can't get kernel info: %s\n", strerror(errno));
607 1.10 lukem memset(&s->Total, 0, sizeof(s->Total));
608 1.1 jtc }
609 1.1 jtc }
610 1.1 jtc
611 1.1 jtc static void
612 1.33 ad allocinfo(struct Info *s)
613 1.1 jtc {
614 1.1 jtc
615 1.23 jwise if ((s->intrcnt = malloc(nintr * sizeof(long))) == NULL) {
616 1.23 jwise error("malloc failed");
617 1.23 jwise die(0);
618 1.23 jwise }
619 1.1 jtc }
620 1.1 jtc
621 1.1 jtc static void
622 1.33 ad copyinfo(struct Info *from, struct Info *to)
623 1.1 jtc {
624 1.1 jtc long *intrcnt;
625 1.1 jtc
626 1.5 thorpej intrcnt = to->intrcnt;
627 1.1 jtc *to = *from;
628 1.10 lukem memmove(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof (int));
629 1.1 jtc }
630 1.1 jtc
631 1.1 jtc static void
632 1.33 ad dinfo(int dn, int c)
633 1.1 jtc {
634 1.5 thorpej double words, atime;
635 1.1 jtc
636 1.1 jtc c = DISKCOL + c * 5;
637 1.5 thorpej
638 1.5 thorpej /* time busy in disk activity */
639 1.5 thorpej atime = (double)cur.dk_time[dn].tv_sec +
640 1.5 thorpej ((double)cur.dk_time[dn].tv_usec / (double)1000000);
641 1.5 thorpej
642 1.5 thorpej words = cur.dk_bytes[dn] / 1024.0; /* # of K transferred */
643 1.5 thorpej
644 1.5 thorpej putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
645 1.5 thorpej putint((int)((float)cur.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
646 1.5 thorpej putint((int)(words/etime + 0.5), DISKROW + 3, c, 5);
647 1.31 mycroft putfloat(atime*100.0/etime, DISKROW + 4, c, 5, 1, 1);
648 1.1 jtc }
649