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