vmstat.c revision 1.3 1 1.3 mycroft /* $NetBSD: vmstat.c,v 1.3 1995/03/22 15:25:58 mycroft 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.1 jtc #ifndef lint
37 1.2 jtc #if 0
38 1.1 jtc static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 1/12/94";
39 1.2 jtc #endif
40 1.3 mycroft static char rcsid[] = "$NetBSD: vmstat.c,v 1.3 1995/03/22 15:25:58 mycroft Exp $";
41 1.1 jtc #endif /* not lint */
42 1.1 jtc
43 1.1 jtc /*
44 1.1 jtc * Cursed vmstat -- from Robert Elz.
45 1.1 jtc */
46 1.1 jtc
47 1.1 jtc #include <sys/param.h>
48 1.1 jtc #include <sys/dkstat.h>
49 1.1 jtc #include <sys/buf.h>
50 1.1 jtc #include <sys/stat.h>
51 1.1 jtc #include <sys/time.h>
52 1.1 jtc #include <sys/user.h>
53 1.1 jtc #include <sys/proc.h>
54 1.1 jtc #include <sys/namei.h>
55 1.1 jtc #include <sys/sysctl.h>
56 1.1 jtc #include <vm/vm.h>
57 1.1 jtc
58 1.3 mycroft #include <ctype.h>
59 1.3 mycroft #include <err.h>
60 1.1 jtc #include <nlist.h>
61 1.1 jtc #include <paths.h>
62 1.3 mycroft #include <signal.h>
63 1.3 mycroft #include <stdlib.h>
64 1.1 jtc #include <string.h>
65 1.3 mycroft #include <utmp.h>
66 1.1 jtc #include <unistd.h>
67 1.3 mycroft
68 1.1 jtc #include "systat.h"
69 1.1 jtc #include "extern.h"
70 1.1 jtc
71 1.1 jtc static struct Info {
72 1.1 jtc long time[CPUSTATES];
73 1.1 jtc struct vmmeter Cnt;
74 1.1 jtc struct vmtotal Total;
75 1.1 jtc long *dk_time;
76 1.1 jtc long *dk_wds;
77 1.1 jtc long *dk_seek;
78 1.1 jtc long *dk_xfer;
79 1.1 jtc int dk_busy;
80 1.1 jtc struct nchstats nchstats;
81 1.1 jtc long nchcount;
82 1.1 jtc long *intrcnt;
83 1.1 jtc } s, s1, s2, z;
84 1.1 jtc
85 1.1 jtc #define cnt s.Cnt
86 1.1 jtc #define oldcnt s1.Cnt
87 1.1 jtc #define total s.Total
88 1.1 jtc #define nchtotal s.nchstats
89 1.1 jtc #define oldnchtotal s1.nchstats
90 1.1 jtc
91 1.1 jtc static enum state { BOOT, TIME, RUN } state = TIME;
92 1.1 jtc
93 1.1 jtc static void allocinfo __P((struct Info *));
94 1.1 jtc static void copyinfo __P((struct Info *, struct Info *));
95 1.1 jtc static float cputime __P((int));
96 1.1 jtc static void dinfo __P((int, int));
97 1.1 jtc static void getinfo __P((struct Info *, enum state));
98 1.1 jtc static void putint __P((int, int, int, int));
99 1.1 jtc static void putfloat __P((double, int, int, int, int, int));
100 1.1 jtc static int ucount __P((void));
101 1.1 jtc
102 1.1 jtc static int ut;
103 1.1 jtc static char buf[26];
104 1.1 jtc static time_t t;
105 1.1 jtc static double etime;
106 1.1 jtc static float hertz;
107 1.1 jtc static int nintr;
108 1.1 jtc static long *intrloc;
109 1.1 jtc static char **intrname;
110 1.1 jtc static int nextintsrow;
111 1.1 jtc
112 1.1 jtc struct utmp utmp;
113 1.1 jtc
114 1.1 jtc
115 1.1 jtc WINDOW *
116 1.1 jtc openkre()
117 1.1 jtc {
118 1.1 jtc
119 1.1 jtc ut = open(_PATH_UTMP, O_RDONLY);
120 1.1 jtc if (ut < 0)
121 1.1 jtc error("No utmp");
122 1.1 jtc return (stdscr);
123 1.1 jtc }
124 1.1 jtc
125 1.1 jtc void
126 1.1 jtc closekre(w)
127 1.1 jtc 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.1 jtc #define X_CPTIME 0
140 1.1 jtc { "_cp_time" },
141 1.1 jtc #define X_CNT 1
142 1.1 jtc { "_cnt" },
143 1.1 jtc #define X_TOTAL 2
144 1.1 jtc { "_total" },
145 1.1 jtc #define X_DK_BUSY 3
146 1.1 jtc { "_dk_busy" },
147 1.1 jtc #define X_DK_TIME 4
148 1.1 jtc { "_dk_time" },
149 1.1 jtc #define X_DK_XFER 5
150 1.1 jtc { "_dk_xfer" },
151 1.1 jtc #define X_DK_WDS 6
152 1.1 jtc { "_dk_wds" },
153 1.1 jtc #define X_DK_SEEK 7
154 1.1 jtc { "_dk_seek" },
155 1.1 jtc #define X_NCHSTATS 8
156 1.1 jtc { "_nchstats" },
157 1.1 jtc #define X_INTRNAMES 9
158 1.1 jtc { "_intrnames" },
159 1.1 jtc #define X_EINTRNAMES 10
160 1.1 jtc { "_eintrnames" },
161 1.1 jtc #define X_INTRCNT 11
162 1.1 jtc { "_intrcnt" },
163 1.1 jtc #define X_EINTRCNT 12
164 1.1 jtc { "_eintrcnt" },
165 1.1 jtc { "" },
166 1.1 jtc };
167 1.1 jtc
168 1.1 jtc /*
169 1.1 jtc * These constants define where the major pieces are laid out
170 1.1 jtc */
171 1.1 jtc #define STATROW 0 /* uses 1 row and 68 cols */
172 1.1 jtc #define STATCOL 2
173 1.1 jtc #define MEMROW 2 /* uses 4 rows and 31 cols */
174 1.1 jtc #define MEMCOL 0
175 1.1 jtc #define PAGEROW 2 /* uses 4 rows and 26 cols */
176 1.1 jtc #define PAGECOL 36
177 1.1 jtc #define INTSROW 2 /* uses all rows to bottom and 17 cols */
178 1.1 jtc #define INTSCOL 63
179 1.1 jtc #define PROCSROW 7 /* uses 2 rows and 20 cols */
180 1.1 jtc #define PROCSCOL 0
181 1.1 jtc #define GENSTATROW 7 /* uses 2 rows and 30 cols */
182 1.1 jtc #define GENSTATCOL 20
183 1.1 jtc #define VMSTATROW 7 /* uses 17 rows and 12 cols */
184 1.1 jtc #define VMSTATCOL 48
185 1.1 jtc #define GRAPHROW 10 /* uses 3 rows and 51 cols */
186 1.1 jtc #define GRAPHCOL 0
187 1.1 jtc #define NAMEIROW 14 /* uses 3 rows and 38 cols */
188 1.1 jtc #define NAMEICOL 0
189 1.1 jtc #define DISKROW 18 /* uses 5 rows and 50 cols (for 9 drives) */
190 1.1 jtc #define DISKCOL 0
191 1.1 jtc
192 1.1 jtc #define DRIVESPACE 9 /* max # for space */
193 1.1 jtc
194 1.1 jtc #if DK_NDRIVE > DRIVESPACE
195 1.1 jtc #define MAXDRIVES DRIVESPACE /* max # to display */
196 1.1 jtc #else
197 1.1 jtc #define MAXDRIVES DK_NDRIVE /* max # to display */
198 1.1 jtc #endif
199 1.1 jtc
200 1.1 jtc int
201 1.1 jtc initkre()
202 1.1 jtc {
203 1.1 jtc char *intrnamebuf, *cp;
204 1.1 jtc int i;
205 1.1 jtc static int once = 0;
206 1.1 jtc
207 1.1 jtc if (namelist[0].n_type == 0) {
208 1.1 jtc if (kvm_nlist(kd, namelist)) {
209 1.1 jtc nlisterr(namelist);
210 1.1 jtc return(0);
211 1.1 jtc }
212 1.1 jtc if (namelist[0].n_type == 0) {
213 1.1 jtc error("No namelist");
214 1.1 jtc return(0);
215 1.1 jtc }
216 1.1 jtc }
217 1.1 jtc hertz = stathz ? stathz : hz;
218 1.1 jtc if (! dkinit())
219 1.1 jtc return(0);
220 1.1 jtc if (dk_ndrive && !once) {
221 1.1 jtc #define allocate(e, t) \
222 1.1 jtc s./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
223 1.1 jtc s1./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
224 1.1 jtc s2./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
225 1.1 jtc z./**/e = (t *)calloc(dk_ndrive, sizeof (t));
226 1.1 jtc allocate(dk_time, long);
227 1.1 jtc allocate(dk_wds, long);
228 1.1 jtc allocate(dk_seek, long);
229 1.1 jtc allocate(dk_xfer, long);
230 1.1 jtc once = 1;
231 1.1 jtc #undef allocate
232 1.1 jtc }
233 1.1 jtc if (nintr == 0) {
234 1.1 jtc nintr = (namelist[X_EINTRCNT].n_value -
235 1.1 jtc namelist[X_INTRCNT].n_value) / sizeof (long);
236 1.1 jtc intrloc = calloc(nintr, sizeof (long));
237 1.1 jtc intrname = calloc(nintr, sizeof (long));
238 1.1 jtc intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
239 1.1 jtc namelist[X_INTRNAMES].n_value);
240 1.1 jtc if (intrnamebuf == 0 || intrname == 0 || intrloc == 0) {
241 1.1 jtc error("Out of memory\n");
242 1.1 jtc if (intrnamebuf)
243 1.1 jtc free(intrnamebuf);
244 1.1 jtc if (intrname)
245 1.1 jtc free(intrname);
246 1.1 jtc if (intrloc)
247 1.1 jtc free(intrloc);
248 1.1 jtc nintr = 0;
249 1.1 jtc return(0);
250 1.1 jtc }
251 1.1 jtc NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
252 1.1 jtc NVAL(X_INTRNAMES));
253 1.1 jtc for (cp = intrnamebuf, i = 0; i < nintr; i++) {
254 1.1 jtc intrname[i] = cp;
255 1.1 jtc cp += strlen(cp) + 1;
256 1.1 jtc }
257 1.1 jtc nextintsrow = INTSROW + 2;
258 1.1 jtc allocinfo(&s);
259 1.1 jtc allocinfo(&s1);
260 1.1 jtc allocinfo(&s2);
261 1.1 jtc allocinfo(&z);
262 1.1 jtc }
263 1.1 jtc getinfo(&s2, RUN);
264 1.1 jtc copyinfo(&s2, &s1);
265 1.1 jtc return(1);
266 1.1 jtc }
267 1.1 jtc
268 1.1 jtc void
269 1.1 jtc fetchkre()
270 1.1 jtc {
271 1.1 jtc time_t now;
272 1.1 jtc
273 1.1 jtc time(&now);
274 1.1 jtc strcpy(buf, ctime(&now));
275 1.1 jtc buf[16] = '\0';
276 1.1 jtc getinfo(&s, state);
277 1.1 jtc }
278 1.1 jtc
279 1.1 jtc void
280 1.1 jtc labelkre()
281 1.1 jtc {
282 1.1 jtc register int i, j;
283 1.1 jtc
284 1.1 jtc clear();
285 1.1 jtc mvprintw(STATROW, STATCOL + 4, "users Load");
286 1.1 jtc mvprintw(MEMROW, MEMCOL, "Mem:KB REAL VIRTUAL");
287 1.1 jtc mvprintw(MEMROW + 1, MEMCOL, " Tot Share Tot Share");
288 1.1 jtc mvprintw(MEMROW + 2, MEMCOL, "Act");
289 1.1 jtc mvprintw(MEMROW + 3, MEMCOL, "All");
290 1.1 jtc
291 1.1 jtc mvprintw(MEMROW + 1, MEMCOL + 31, "Free");
292 1.1 jtc
293 1.1 jtc mvprintw(PAGEROW, PAGECOL, " PAGING SWAPPING ");
294 1.1 jtc mvprintw(PAGEROW + 1, PAGECOL, " in out in out ");
295 1.1 jtc mvprintw(PAGEROW + 2, PAGECOL, "count");
296 1.1 jtc mvprintw(PAGEROW + 3, PAGECOL, "pages");
297 1.1 jtc
298 1.1 jtc mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
299 1.1 jtc mvprintw(INTSROW + 1, INTSCOL + 9, "total");
300 1.1 jtc
301 1.1 jtc mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "cow");
302 1.1 jtc mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "objlk");
303 1.1 jtc mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "objht");
304 1.1 jtc mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "zfod");
305 1.1 jtc mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "nzfod");
306 1.1 jtc mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "%%zfod");
307 1.1 jtc mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "kern");
308 1.1 jtc mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "wire");
309 1.1 jtc mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "act");
310 1.1 jtc mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "inact");
311 1.1 jtc mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "free");
312 1.1 jtc mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "daefr");
313 1.1 jtc mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "prcfr");
314 1.1 jtc mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "react");
315 1.1 jtc mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "scan");
316 1.1 jtc mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "hdrev");
317 1.1 jtc if (LINES - 1 > VMSTATROW + 16)
318 1.1 jtc mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "intrn");
319 1.1 jtc
320 1.1 jtc mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt");
321 1.1 jtc
322 1.1 jtc mvprintw(GRAPHROW, GRAPHCOL,
323 1.1 jtc " . %% Sys . %% User . %% Nice . %% Idle");
324 1.1 jtc mvprintw(PROCSROW, PROCSCOL, "Proc:r p d s w");
325 1.1 jtc mvprintw(GRAPHROW + 1, GRAPHCOL,
326 1.1 jtc "| | | | | | | | | | |");
327 1.1 jtc
328 1.1 jtc mvprintw(NAMEIROW, NAMEICOL, "Namei Sys-cache Proc-cache");
329 1.1 jtc mvprintw(NAMEIROW + 1, NAMEICOL,
330 1.1 jtc " Calls hits %% hits %%");
331 1.1 jtc mvprintw(DISKROW, DISKCOL, "Discs");
332 1.1 jtc mvprintw(DISKROW + 1, DISKCOL, "seeks");
333 1.1 jtc mvprintw(DISKROW + 2, DISKCOL, "xfers");
334 1.1 jtc mvprintw(DISKROW + 3, DISKCOL, " blks");
335 1.1 jtc mvprintw(DISKROW + 4, DISKCOL, " msps");
336 1.1 jtc j = 0;
337 1.1 jtc for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++)
338 1.1 jtc if (dk_select[i]) {
339 1.1 jtc mvprintw(DISKROW, DISKCOL + 5 + 5 * j,
340 1.1 jtc " %3.3s", dr_name[j]);
341 1.1 jtc j++;
342 1.1 jtc }
343 1.1 jtc for (i = 0; i < nintr; i++) {
344 1.1 jtc if (intrloc[i] == 0)
345 1.1 jtc continue;
346 1.1 jtc mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
347 1.1 jtc }
348 1.1 jtc }
349 1.1 jtc
350 1.1 jtc #define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
351 1.1 jtc #define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
352 1.1 jtc #define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
353 1.1 jtc if(state == TIME) s1.nchstats.fld = t;}
354 1.1 jtc #define PUTRATE(fld, l, c, w) \
355 1.1 jtc Y(fld); \
356 1.1 jtc putint((int)((float)s.fld/etime + 0.5), l, c, w)
357 1.1 jtc #define MAXFAIL 5
358 1.1 jtc
359 1.1 jtc static char cpuchar[CPUSTATES] = { '=' , '>', '-', ' ' };
360 1.1 jtc static char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_IDLE };
361 1.1 jtc
362 1.1 jtc void
363 1.1 jtc showkre()
364 1.1 jtc {
365 1.1 jtc float f1, f2;
366 1.1 jtc int psiz, inttotal;
367 1.1 jtc int i, l, c;
368 1.1 jtc static int failcnt = 0;
369 1.1 jtc
370 1.1 jtc for (i = 0; i < dk_ndrive; i++) {
371 1.1 jtc X(dk_xfer); X(dk_seek); X(dk_wds); X(dk_time);
372 1.1 jtc }
373 1.1 jtc etime = 0;
374 1.1 jtc for(i = 0; i < CPUSTATES; i++) {
375 1.1 jtc X(time);
376 1.1 jtc etime += s.time[i];
377 1.1 jtc }
378 1.1 jtc if (etime < 5.0) { /* < 5 ticks - ignore this trash */
379 1.1 jtc if (failcnt++ >= MAXFAIL) {
380 1.1 jtc clear();
381 1.1 jtc mvprintw(2, 10, "The alternate system clock has died!");
382 1.1 jtc mvprintw(3, 10, "Reverting to ``pigs'' display.");
383 1.1 jtc move(CMDLINE, 0);
384 1.1 jtc refresh();
385 1.1 jtc failcnt = 0;
386 1.1 jtc sleep(5);
387 1.1 jtc command("pigs");
388 1.1 jtc }
389 1.1 jtc return;
390 1.1 jtc }
391 1.1 jtc failcnt = 0;
392 1.1 jtc etime /= hertz;
393 1.1 jtc inttotal = 0;
394 1.1 jtc for (i = 0; i < nintr; i++) {
395 1.1 jtc if (s.intrcnt[i] == 0)
396 1.1 jtc continue;
397 1.1 jtc if (intrloc[i] == 0) {
398 1.1 jtc if (nextintsrow == LINES)
399 1.1 jtc continue;
400 1.1 jtc intrloc[i] = nextintsrow++;
401 1.1 jtc mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s",
402 1.1 jtc intrname[i]);
403 1.1 jtc }
404 1.1 jtc X(intrcnt);
405 1.1 jtc l = (int)((float)s.intrcnt[i]/etime + 0.5);
406 1.1 jtc inttotal += l;
407 1.1 jtc putint(l, intrloc[i], INTSCOL, 8);
408 1.1 jtc }
409 1.1 jtc putint(inttotal, INTSROW + 1, INTSCOL, 8);
410 1.1 jtc Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
411 1.1 jtc Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
412 1.1 jtc s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
413 1.1 jtc nchtotal.ncs_miss + nchtotal.ncs_long;
414 1.1 jtc if (state == TIME)
415 1.1 jtc s1.nchcount = s.nchcount;
416 1.1 jtc
417 1.1 jtc psiz = 0;
418 1.1 jtc f2 = 0.0;
419 1.1 jtc
420 1.1 jtc /*
421 1.1 jtc * Last CPU state not calculated yet.
422 1.1 jtc */
423 1.1 jtc for (c = 0; c < CPUSTATES - 1; c++) {
424 1.1 jtc i = cpuorder[c];
425 1.1 jtc f1 = cputime(i);
426 1.1 jtc f2 += f1;
427 1.1 jtc l = (int) ((f2 + 1.0) / 2.0) - psiz;
428 1.1 jtc if (c == 0)
429 1.1 jtc putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
430 1.1 jtc else
431 1.1 jtc putfloat(f1, GRAPHROW, GRAPHCOL + 12 * c,
432 1.1 jtc 5, 1, 0);
433 1.1 jtc move(GRAPHROW + 2, psiz);
434 1.1 jtc psiz += l;
435 1.1 jtc while (l-- > 0)
436 1.1 jtc addch(cpuchar[c]);
437 1.1 jtc }
438 1.1 jtc
439 1.1 jtc putint(ucount(), STATROW, STATCOL, 3);
440 1.1 jtc putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
441 1.1 jtc putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
442 1.1 jtc putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
443 1.1 jtc mvaddstr(STATROW, STATCOL + 53, buf);
444 1.1 jtc #define pgtokb(pg) ((pg) * cnt.v_page_size / 1024)
445 1.1 jtc putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 6);
446 1.1 jtc putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 9, 6);
447 1.1 jtc putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 15, 7);
448 1.1 jtc putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 22, 7);
449 1.1 jtc putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 6);
450 1.1 jtc putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 9, 6);
451 1.1 jtc putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 15, 7);
452 1.1 jtc putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 22, 7);
453 1.1 jtc putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 29, 6);
454 1.1 jtc putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
455 1.1 jtc putint(total.t_pw, PROCSROW + 1, PROCSCOL + 6, 3);
456 1.1 jtc putint(total.t_dw, PROCSROW + 1, PROCSCOL + 9, 3);
457 1.1 jtc putint(total.t_sl, PROCSROW + 1, PROCSCOL + 12, 3);
458 1.1 jtc putint(total.t_sw, PROCSROW + 1, PROCSCOL + 15, 3);
459 1.1 jtc PUTRATE(Cnt.v_cow_faults, VMSTATROW + 0, VMSTATCOL + 3, 6);
460 1.1 jtc PUTRATE(Cnt.v_lookups, VMSTATROW + 1, VMSTATCOL + 3, 6);
461 1.1 jtc PUTRATE(Cnt.v_hits, VMSTATROW + 2, VMSTATCOL + 3, 6);
462 1.1 jtc PUTRATE(Cnt.v_zfod, VMSTATROW + 3, VMSTATCOL + 4, 5);
463 1.1 jtc PUTRATE(Cnt.v_nzfod, VMSTATROW + 4, VMSTATCOL + 3, 6);
464 1.1 jtc putfloat(cnt.v_nzfod == 0 ? 0.0 : (100.0 * cnt.v_zfod / cnt.v_nzfod),
465 1.1 jtc VMSTATROW + 5, VMSTATCOL + 2, 7, 2, 1);
466 1.1 jtc putint(pgtokb(cnt.v_kernel_pages), VMSTATROW + 6, VMSTATCOL, 9);
467 1.1 jtc putint(pgtokb(cnt.v_wire_count), VMSTATROW + 7, VMSTATCOL, 9);
468 1.1 jtc putint(pgtokb(cnt.v_active_count), VMSTATROW + 8, VMSTATCOL, 9);
469 1.1 jtc putint(pgtokb(cnt.v_inactive_count), VMSTATROW + 9, VMSTATCOL, 9);
470 1.1 jtc putint(pgtokb(cnt.v_free_count), VMSTATROW + 10, VMSTATCOL, 9);
471 1.1 jtc PUTRATE(Cnt.v_dfree, VMSTATROW + 11, VMSTATCOL, 9);
472 1.1 jtc PUTRATE(Cnt.v_pfree, VMSTATROW + 12, VMSTATCOL, 9);
473 1.1 jtc PUTRATE(Cnt.v_reactivated, VMSTATROW + 13, VMSTATCOL, 9);
474 1.1 jtc PUTRATE(Cnt.v_scan, VMSTATROW + 14, VMSTATCOL, 9);
475 1.1 jtc PUTRATE(Cnt.v_rev, VMSTATROW + 15, VMSTATCOL, 9);
476 1.1 jtc if (LINES - 1 > VMSTATROW + 16)
477 1.1 jtc PUTRATE(Cnt.v_intrans, VMSTATROW + 16, VMSTATCOL, 9);
478 1.1 jtc PUTRATE(Cnt.v_pageins, PAGEROW + 2, PAGECOL + 5, 5);
479 1.1 jtc PUTRATE(Cnt.v_pageouts, PAGEROW + 2, PAGECOL + 10, 5);
480 1.1 jtc PUTRATE(Cnt.v_swpin, PAGEROW + 2, PAGECOL + 15, 5); /* - */
481 1.1 jtc PUTRATE(Cnt.v_swpout, PAGEROW + 2, PAGECOL + 20, 5); /* - */
482 1.1 jtc PUTRATE(Cnt.v_pgpgin, PAGEROW + 3, PAGECOL + 5, 5); /* ? */
483 1.1 jtc PUTRATE(Cnt.v_pgpgout, PAGEROW + 3, PAGECOL + 10, 5); /* ? */
484 1.1 jtc PUTRATE(Cnt.v_pswpin, PAGEROW + 3, PAGECOL + 15, 5); /* - */
485 1.1 jtc PUTRATE(Cnt.v_pswpout, PAGEROW + 3, PAGECOL + 20, 5); /* - */
486 1.1 jtc PUTRATE(Cnt.v_swtch, GENSTATROW + 1, GENSTATCOL, 5);
487 1.1 jtc PUTRATE(Cnt.v_trap, GENSTATROW + 1, GENSTATCOL + 5, 5);
488 1.1 jtc PUTRATE(Cnt.v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 5);
489 1.1 jtc PUTRATE(Cnt.v_intr, GENSTATROW + 1, GENSTATCOL + 15, 5);
490 1.1 jtc PUTRATE(Cnt.v_soft, GENSTATROW + 1, GENSTATCOL + 20, 5);
491 1.1 jtc PUTRATE(Cnt.v_faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
492 1.1 jtc mvprintw(DISKROW, DISKCOL + 5, " ");
493 1.1 jtc for (i = 0, c = 0; i < dk_ndrive && c < MAXDRIVES; i++)
494 1.1 jtc if (dk_select[i]) {
495 1.1 jtc mvprintw(DISKROW, DISKCOL + 5 + 5 * c,
496 1.1 jtc " %3.3s", dr_name[i]);
497 1.1 jtc dinfo(i, ++c);
498 1.1 jtc }
499 1.1 jtc putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
500 1.1 jtc putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
501 1.1 jtc #define nz(x) ((x) ? (x) : 1)
502 1.1 jtc putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
503 1.1 jtc NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
504 1.1 jtc putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
505 1.1 jtc putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
506 1.1 jtc NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
507 1.1 jtc #undef nz
508 1.1 jtc }
509 1.1 jtc
510 1.1 jtc int
511 1.1 jtc cmdkre(cmd, args)
512 1.1 jtc char *cmd, *args;
513 1.1 jtc {
514 1.1 jtc
515 1.1 jtc if (prefix(cmd, "run")) {
516 1.1 jtc copyinfo(&s2, &s1);
517 1.1 jtc state = RUN;
518 1.1 jtc return (1);
519 1.1 jtc }
520 1.1 jtc if (prefix(cmd, "boot")) {
521 1.1 jtc state = BOOT;
522 1.1 jtc copyinfo(&z, &s1);
523 1.1 jtc return (1);
524 1.1 jtc }
525 1.1 jtc if (prefix(cmd, "time")) {
526 1.1 jtc state = TIME;
527 1.1 jtc return (1);
528 1.1 jtc }
529 1.1 jtc if (prefix(cmd, "zero")) {
530 1.1 jtc if (state == RUN)
531 1.1 jtc getinfo(&s1, RUN);
532 1.1 jtc return (1);
533 1.1 jtc }
534 1.1 jtc return (dkcmd(cmd, args));
535 1.1 jtc }
536 1.1 jtc
537 1.1 jtc /* calculate number of users on the system */
538 1.1 jtc static int
539 1.1 jtc ucount()
540 1.1 jtc {
541 1.1 jtc register int nusers = 0;
542 1.1 jtc
543 1.1 jtc if (ut < 0)
544 1.1 jtc return (0);
545 1.1 jtc while (read(ut, &utmp, sizeof(utmp)))
546 1.1 jtc if (utmp.ut_name[0] != '\0')
547 1.1 jtc nusers++;
548 1.1 jtc
549 1.1 jtc lseek(ut, 0L, L_SET);
550 1.1 jtc return (nusers);
551 1.1 jtc }
552 1.1 jtc
553 1.1 jtc static float
554 1.1 jtc cputime(indx)
555 1.1 jtc int indx;
556 1.1 jtc {
557 1.1 jtc double t;
558 1.1 jtc register int i;
559 1.1 jtc
560 1.1 jtc t = 0;
561 1.1 jtc for (i = 0; i < CPUSTATES; i++)
562 1.1 jtc t += s.time[i];
563 1.1 jtc if (t == 0.0)
564 1.1 jtc t = 1.0;
565 1.1 jtc return (s.time[indx] * 100.0 / t);
566 1.1 jtc }
567 1.1 jtc
568 1.1 jtc static void
569 1.1 jtc putint(n, l, c, w)
570 1.1 jtc int n, l, c, w;
571 1.1 jtc {
572 1.1 jtc char b[128];
573 1.1 jtc
574 1.1 jtc move(l, c);
575 1.1 jtc if (n == 0) {
576 1.1 jtc while (w-- > 0)
577 1.1 jtc addch(' ');
578 1.1 jtc return;
579 1.1 jtc }
580 1.1 jtc sprintf(b, "%*d", w, n);
581 1.1 jtc if (strlen(b) > w) {
582 1.1 jtc while (w-- > 0)
583 1.1 jtc addch('*');
584 1.1 jtc return;
585 1.1 jtc }
586 1.1 jtc addstr(b);
587 1.1 jtc }
588 1.1 jtc
589 1.1 jtc static void
590 1.1 jtc putfloat(f, l, c, w, d, nz)
591 1.1 jtc double f;
592 1.1 jtc int l, c, w, d, nz;
593 1.1 jtc {
594 1.1 jtc char b[128];
595 1.1 jtc
596 1.1 jtc move(l, c);
597 1.1 jtc if (nz && f == 0.0) {
598 1.1 jtc while (--w >= 0)
599 1.1 jtc addch(' ');
600 1.1 jtc return;
601 1.1 jtc }
602 1.1 jtc sprintf(b, "%*.*f", w, d, f);
603 1.1 jtc if (strlen(b) > w) {
604 1.1 jtc while (--w >= 0)
605 1.1 jtc addch('*');
606 1.1 jtc return;
607 1.1 jtc }
608 1.1 jtc addstr(b);
609 1.1 jtc }
610 1.1 jtc
611 1.1 jtc static void
612 1.1 jtc getinfo(s, st)
613 1.1 jtc struct Info *s;
614 1.1 jtc enum state st;
615 1.1 jtc {
616 1.1 jtc int mib[2], size;
617 1.1 jtc extern int errno;
618 1.1 jtc
619 1.1 jtc NREAD(X_CPTIME, s->time, sizeof s->time);
620 1.1 jtc NREAD(X_CNT, &s->Cnt, sizeof s->Cnt);
621 1.1 jtc NREAD(X_DK_BUSY, &s->dk_busy, LONG);
622 1.1 jtc NREAD(X_DK_TIME, s->dk_time, dk_ndrive * LONG);
623 1.1 jtc NREAD(X_DK_XFER, s->dk_xfer, dk_ndrive * LONG);
624 1.1 jtc NREAD(X_DK_WDS, s->dk_wds, dk_ndrive * LONG);
625 1.1 jtc NREAD(X_DK_SEEK, s->dk_seek, dk_ndrive * LONG);
626 1.1 jtc NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
627 1.1 jtc NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
628 1.1 jtc size = sizeof(s->Total);
629 1.1 jtc mib[0] = CTL_VM;
630 1.1 jtc mib[1] = VM_METER;
631 1.1 jtc if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
632 1.1 jtc error("Can't get kernel info: %s\n", strerror(errno));
633 1.1 jtc bzero(&s->Total, sizeof(s->Total));
634 1.1 jtc }
635 1.1 jtc }
636 1.1 jtc
637 1.1 jtc static void
638 1.1 jtc allocinfo(s)
639 1.1 jtc struct Info *s;
640 1.1 jtc {
641 1.1 jtc
642 1.1 jtc s->intrcnt = (long *) malloc(nintr * sizeof(long));
643 1.3 mycroft if (s->intrcnt == NULL)
644 1.3 mycroft errx(2, "out of memory");
645 1.1 jtc }
646 1.1 jtc
647 1.1 jtc static void
648 1.1 jtc copyinfo(from, to)
649 1.1 jtc register struct Info *from, *to;
650 1.1 jtc {
651 1.1 jtc long *time, *wds, *seek, *xfer;
652 1.1 jtc long *intrcnt;
653 1.1 jtc
654 1.1 jtc /*
655 1.1 jtc * time, wds, seek, and xfer are malloc'd so we have to
656 1.1 jtc * save the pointers before the structure copy and then
657 1.1 jtc * copy by hand.
658 1.1 jtc */
659 1.1 jtc time = to->dk_time; wds = to->dk_wds; seek = to->dk_seek;
660 1.1 jtc xfer = to->dk_xfer; intrcnt = to->intrcnt;
661 1.1 jtc *to = *from;
662 1.1 jtc bcopy(from->dk_time, to->dk_time = time, dk_ndrive * sizeof (long));
663 1.1 jtc bcopy(from->dk_wds, to->dk_wds = wds, dk_ndrive * sizeof (long));
664 1.1 jtc bcopy(from->dk_seek, to->dk_seek = seek, dk_ndrive * sizeof (long));
665 1.1 jtc bcopy(from->dk_xfer, to->dk_xfer = xfer, dk_ndrive * sizeof (long));
666 1.1 jtc bcopy(from->intrcnt, to->intrcnt = intrcnt, nintr * sizeof (int));
667 1.1 jtc }
668 1.1 jtc
669 1.1 jtc static void
670 1.1 jtc dinfo(dn, c)
671 1.1 jtc int dn, c;
672 1.1 jtc {
673 1.1 jtc double words, atime, itime, xtime;
674 1.1 jtc
675 1.1 jtc c = DISKCOL + c * 5;
676 1.1 jtc atime = s.dk_time[dn];
677 1.1 jtc atime /= hertz;
678 1.1 jtc words = s.dk_wds[dn]*32.0; /* number of words transferred */
679 1.1 jtc xtime = dk_mspw[dn]*words; /* transfer time */
680 1.1 jtc itime = atime - xtime; /* time not transferring */
681 1.1 jtc if (xtime < 0)
682 1.1 jtc itime += xtime, xtime = 0;
683 1.1 jtc if (itime < 0)
684 1.1 jtc xtime += itime, itime = 0;
685 1.1 jtc putint((int)((float)s.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
686 1.1 jtc putint((int)((float)s.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
687 1.1 jtc putint((int)(words/etime/512.0 + 0.5), DISKROW + 3, c, 5);
688 1.1 jtc if (s.dk_seek[dn])
689 1.1 jtc putfloat(itime*1000.0/s.dk_seek[dn], DISKROW + 4, c, 5, 1, 1);
690 1.1 jtc else
691 1.1 jtc putint(0, DISKROW + 4, c, 5);
692 1.1 jtc }
693