vmstat.c revision 1.10 1 /* $NetBSD: vmstat.c,v 1.10 1997/10/19 23:36:34 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1983, 1989, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 1/12/94";
40 #endif
41 __RCSID("$NetBSD: vmstat.c,v 1.10 1997/10/19 23:36:34 lukem Exp $");
42 #endif /* not lint */
43
44 /*
45 * Cursed vmstat -- from Robert Elz.
46 */
47
48 #include <sys/param.h>
49 #include <sys/dkstat.h>
50 #include <sys/buf.h>
51 #include <sys/stat.h>
52 #include <sys/time.h>
53 #include <sys/user.h>
54 #include <sys/proc.h>
55 #include <sys/namei.h>
56 #include <sys/sysctl.h>
57 #include <vm/vm.h>
58
59 #include <ctype.h>
60 #include <err.h>
61 #include <nlist.h>
62 #include <paths.h>
63 #include <signal.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <utmp.h>
67 #include <unistd.h>
68
69 #include "systat.h"
70 #include "extern.h"
71
72 static struct Info {
73 long time[CPUSTATES];
74 struct vmmeter Cnt;
75 struct vmtotal Total;
76 struct nchstats nchstats;
77 long nchcount;
78 long *intrcnt;
79 } s, s1, s2, z;
80
81 #include "dkstats.h"
82 extern struct _disk cur;
83
84
85 #define cnt s.Cnt
86 #define oldcnt s1.Cnt
87 #define total s.Total
88 #define nchtotal s.nchstats
89 #define oldnchtotal s1.nchstats
90
91 static enum state { BOOT, TIME, RUN } state = TIME;
92
93 static void allocinfo __P((struct Info *));
94 static void copyinfo __P((struct Info *, struct Info *));
95 static float cputime __P((int));
96 static void dinfo __P((int, int));
97 static void getinfo __P((struct Info *, enum state));
98 static void putint __P((int, int, int, int));
99 static void putfloat __P((double, int, int, int, int, int));
100 static int ucount __P((void));
101
102 static int ut;
103 static char buf[26];
104 static time_t t;
105 static double etime;
106 static float hertz;
107 static int nintr;
108 static long *intrloc;
109 static char **intrname;
110 static int nextintsrow;
111
112 struct utmp utmp;
113
114
115 WINDOW *
116 openkre()
117 {
118
119 ut = open(_PATH_UTMP, O_RDONLY);
120 if (ut < 0)
121 error("No utmp");
122 return (stdscr);
123 }
124
125 void
126 closekre(w)
127 WINDOW *w;
128 {
129
130 (void) close(ut);
131 if (w == NULL)
132 return;
133 wclear(w);
134 wrefresh(w);
135 }
136
137
138 static struct nlist namelist[] = {
139 #define X_CPTIME 0
140 { "_cp_time" },
141 #define X_CNT 1
142 { "_cnt" },
143 #define X_TOTAL 2
144 { "_total" },
145 #define X_NCHSTATS 3
146 { "_nchstats" },
147 #define X_INTRNAMES 4
148 { "_intrnames" },
149 #define X_EINTRNAMES 5
150 { "_eintrnames" },
151 #define X_INTRCNT 6
152 { "_intrcnt" },
153 #define X_EINTRCNT 7
154 { "_eintrcnt" },
155 { "" },
156 };
157
158 /*
159 * These constants define where the major pieces are laid out
160 */
161 #define STATROW 0 /* uses 1 row and 68 cols */
162 #define STATCOL 2
163 #define MEMROW 2 /* uses 4 rows and 31 cols */
164 #define MEMCOL 0
165 #define PAGEROW 2 /* uses 4 rows and 26 cols */
166 #define PAGECOL 36
167 #define INTSROW 2 /* uses all rows to bottom and 17 cols */
168 #define INTSCOL 63
169 #define PROCSROW 7 /* uses 2 rows and 20 cols */
170 #define PROCSCOL 0
171 #define GENSTATROW 7 /* uses 2 rows and 30 cols */
172 #define GENSTATCOL 20
173 #define VMSTATROW 7 /* uses 17 rows and 12 cols */
174 #define VMSTATCOL 48
175 #define GRAPHROW 10 /* uses 3 rows and 51 cols */
176 #define GRAPHCOL 0
177 #define NAMEIROW 14 /* uses 3 rows and 38 cols */
178 #define NAMEICOL 0
179 #define DISKROW 18 /* uses 5 rows and 50 cols (for 9 drives) */
180 #define DISKCOL 0
181
182 #define DRIVESPACE 9 /* max # for space */
183
184 #if DK_NDRIVE > DRIVESPACE
185 #define MAXDRIVES DRIVESPACE /* max # to display */
186 #else
187 #define MAXDRIVES DK_NDRIVE /* max # to display */
188 #endif
189
190 int
191 initkre()
192 {
193 char *intrnamebuf, *cp;
194 int i;
195 static int once = 0;
196
197 if (namelist[0].n_type == 0) {
198 if (kvm_nlist(kd, namelist)) {
199 nlisterr(namelist);
200 return(0);
201 }
202 if (namelist[0].n_type == 0) {
203 error("No namelist");
204 return(0);
205 }
206 }
207 hertz = stathz ? stathz : hz;
208 if (! dkinit(1))
209 return(0);
210 if (dk_ndrive && !once) {
211 #define allocate(e, t) \
212 s./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
213 s1./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
214 s2./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
215 z./**/e = (t *)calloc(dk_ndrive, sizeof (t));
216 once = 1;
217 #undef allocate
218 }
219 if (nintr == 0) {
220 nintr = (namelist[X_EINTRCNT].n_value -
221 namelist[X_INTRCNT].n_value) / sizeof (long);
222 intrloc = calloc(nintr, sizeof (long));
223 intrname = calloc(nintr, sizeof (long));
224 intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
225 namelist[X_INTRNAMES].n_value);
226 if (intrnamebuf == 0 || intrname == 0 || intrloc == 0) {
227 error("Out of memory\n");
228 if (intrnamebuf)
229 free(intrnamebuf);
230 if (intrname)
231 free(intrname);
232 if (intrloc)
233 free(intrloc);
234 nintr = 0;
235 return(0);
236 }
237 NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
238 NVAL(X_INTRNAMES));
239 for (cp = intrnamebuf, i = 0; i < nintr; i++) {
240 intrname[i] = cp;
241 cp += strlen(cp) + 1;
242 }
243 nextintsrow = INTSROW + 2;
244 allocinfo(&s);
245 allocinfo(&s1);
246 allocinfo(&s2);
247 allocinfo(&z);
248 }
249 getinfo(&s2, RUN);
250 copyinfo(&s2, &s1);
251 return(1);
252 }
253
254 void
255 fetchkre()
256 {
257 time_t now;
258
259 time(&now);
260 strcpy(buf, ctime(&now));
261 buf[16] = '\0';
262 getinfo(&s, state);
263 }
264
265 void
266 labelkre()
267 {
268 int i, j;
269
270 clear();
271 mvprintw(STATROW, STATCOL + 4, "users Load");
272 mvprintw(MEMROW, MEMCOL, "Mem:KB REAL VIRTUAL");
273 mvprintw(MEMROW + 1, MEMCOL, " Tot Share Tot Share");
274 mvprintw(MEMROW + 2, MEMCOL, "Act");
275 mvprintw(MEMROW + 3, MEMCOL, "All");
276
277 mvprintw(MEMROW + 1, MEMCOL + 31, "Free");
278
279 mvprintw(PAGEROW, PAGECOL, " PAGING SWAPPING ");
280 mvprintw(PAGEROW + 1, PAGECOL, " in out in out ");
281 mvprintw(PAGEROW + 2, PAGECOL, "count");
282 mvprintw(PAGEROW + 3, PAGECOL, "pages");
283
284 mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
285 mvprintw(INTSROW + 1, INTSCOL + 9, "total");
286
287 mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "cow");
288 mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "objlk");
289 mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "objht");
290 mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "zfod");
291 mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "nzfod");
292 mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "%%zfod");
293 mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "kern");
294 mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "wire");
295 mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "act");
296 mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "inact");
297 mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "free");
298 mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "daefr");
299 mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "prcfr");
300 mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "react");
301 mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "scan");
302 mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "hdrev");
303 if (LINES - 1 > VMSTATROW + 16)
304 mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "intrn");
305
306 mvprintw(GENSTATROW, GENSTATCOL, " Csw Trp Sys Int Sof Flt");
307
308 mvprintw(GRAPHROW, GRAPHCOL,
309 " . %% Sys . %% User . %% Nice . %% Idle");
310 mvprintw(PROCSROW, PROCSCOL, "Proc:r p d s w");
311 mvprintw(GRAPHROW + 1, GRAPHCOL,
312 "| | | | | | | | | | |");
313
314 mvprintw(NAMEIROW, NAMEICOL, "Namei Sys-cache Proc-cache");
315 mvprintw(NAMEIROW + 1, NAMEICOL,
316 " Calls hits %% hits %%");
317 mvprintw(DISKROW, DISKCOL, "Discs");
318 mvprintw(DISKROW + 1, DISKCOL, "seeks");
319 mvprintw(DISKROW + 2, DISKCOL, "xfers");
320 mvprintw(DISKROW + 3, DISKCOL, "Kbyte");
321 mvprintw(DISKROW + 4, DISKCOL, " sec");
322 j = 0;
323 for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++)
324 if (dk_select[i]) {
325 mvprintw(DISKROW, DISKCOL + 5 + 5 * j,
326 " %3.3s", dr_name[j]);
327 j++;
328 }
329 for (i = 0; i < nintr; i++) {
330 if (intrloc[i] == 0)
331 continue;
332 mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
333 }
334 }
335
336 #define X(fld) {t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
337 #define Y(fld) {t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
338 #define Z(fld) {t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
339 if(state == TIME) s1.nchstats.fld = t;}
340 #define PUTRATE(fld, l, c, w) \
341 Y(fld); \
342 putint((int)((float)s.fld/etime + 0.5), l, c, w)
343 #define MAXFAIL 5
344
345 static char cpuchar[CPUSTATES] = { '=' , '>', '-', ' ' };
346 static char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_IDLE };
347
348 void
349 showkre()
350 {
351 float f1, f2;
352 int psiz, inttotal;
353 int i, l, c;
354 static int failcnt = 0;
355
356
357 if (state == TIME)
358 dkswap();
359 etime = 0;
360 for(i = 0; i < CPUSTATES; i++) {
361 X(time);
362 etime += s.time[i];
363 }
364 if (etime < 5.0) { /* < 5 ticks - ignore this trash */
365 if (failcnt++ >= MAXFAIL) {
366 clear();
367 mvprintw(2, 10, "The alternate system clock has died!");
368 mvprintw(3, 10, "Reverting to ``pigs'' display.");
369 move(CMDLINE, 0);
370 refresh();
371 failcnt = 0;
372 sleep(5);
373 command("pigs");
374 }
375 return;
376 }
377 failcnt = 0;
378 etime /= hertz;
379 inttotal = 0;
380 for (i = 0; i < nintr; i++) {
381 if (s.intrcnt[i] == 0)
382 continue;
383 if (intrloc[i] == 0) {
384 if (nextintsrow == LINES)
385 continue;
386 intrloc[i] = nextintsrow++;
387 mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s",
388 intrname[i]);
389 }
390 X(intrcnt);
391 l = (int)((float)s.intrcnt[i]/etime + 0.5);
392 inttotal += l;
393 putint(l, intrloc[i], INTSCOL, 8);
394 }
395 putint(inttotal, INTSROW + 1, INTSCOL, 8);
396 Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
397 Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
398 s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
399 nchtotal.ncs_miss + nchtotal.ncs_long;
400 if (state == TIME)
401 s1.nchcount = s.nchcount;
402
403 psiz = 0;
404 f2 = 0.0;
405
406 /*
407 * Last CPU state not calculated yet.
408 */
409 for (c = 0; c < CPUSTATES - 1; c++) {
410 i = cpuorder[c];
411 f1 = cputime(i);
412 f2 += f1;
413 l = (int) ((f2 + 1.0) / 2.0) - psiz;
414 if (c == 0)
415 putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
416 else
417 putfloat(f1, GRAPHROW, GRAPHCOL + 12 * c,
418 5, 1, 0);
419 move(GRAPHROW + 2, psiz);
420 psiz += l;
421 while (l-- > 0)
422 addch(cpuchar[c]);
423 }
424
425 putint(ucount(), STATROW, STATCOL, 3);
426 putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
427 putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
428 putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
429 mvaddstr(STATROW, STATCOL + 53, buf);
430 #define pgtokb(pg) ((pg) * cnt.v_page_size / 1024)
431 putint(pgtokb(total.t_arm), MEMROW + 2, MEMCOL + 3, 6);
432 putint(pgtokb(total.t_armshr), MEMROW + 2, MEMCOL + 9, 6);
433 putint(pgtokb(total.t_avm), MEMROW + 2, MEMCOL + 15, 7);
434 putint(pgtokb(total.t_avmshr), MEMROW + 2, MEMCOL + 22, 7);
435 putint(pgtokb(total.t_rm), MEMROW + 3, MEMCOL + 3, 6);
436 putint(pgtokb(total.t_rmshr), MEMROW + 3, MEMCOL + 9, 6);
437 putint(pgtokb(total.t_vm), MEMROW + 3, MEMCOL + 15, 7);
438 putint(pgtokb(total.t_vmshr), MEMROW + 3, MEMCOL + 22, 7);
439 putint(pgtokb(total.t_free), MEMROW + 2, MEMCOL + 29, 6);
440 putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
441 putint(total.t_pw, PROCSROW + 1, PROCSCOL + 6, 3);
442 putint(total.t_dw, PROCSROW + 1, PROCSCOL + 9, 3);
443 putint(total.t_sl, PROCSROW + 1, PROCSCOL + 12, 3);
444 putint(total.t_sw, PROCSROW + 1, PROCSCOL + 15, 3);
445 PUTRATE(Cnt.v_cow_faults, VMSTATROW + 0, VMSTATCOL + 3, 6);
446 PUTRATE(Cnt.v_lookups, VMSTATROW + 1, VMSTATCOL + 3, 6);
447 PUTRATE(Cnt.v_hits, VMSTATROW + 2, VMSTATCOL + 3, 6);
448 PUTRATE(Cnt.v_zfod, VMSTATROW + 3, VMSTATCOL + 4, 5);
449 PUTRATE(Cnt.v_nzfod, VMSTATROW + 4, VMSTATCOL + 3, 6);
450 putfloat(cnt.v_nzfod == 0 ? 0.0 : (100.0 * cnt.v_zfod / cnt.v_nzfod),
451 VMSTATROW + 5, VMSTATCOL + 2, 7, 2, 1);
452 putint(pgtokb(cnt.v_kernel_pages), VMSTATROW + 6, VMSTATCOL, 9);
453 putint(pgtokb(cnt.v_wire_count), VMSTATROW + 7, VMSTATCOL, 9);
454 putint(pgtokb(cnt.v_active_count), VMSTATROW + 8, VMSTATCOL, 9);
455 putint(pgtokb(cnt.v_inactive_count), VMSTATROW + 9, VMSTATCOL, 9);
456 putint(pgtokb(cnt.v_free_count), VMSTATROW + 10, VMSTATCOL, 9);
457 PUTRATE(Cnt.v_dfree, VMSTATROW + 11, VMSTATCOL, 9);
458 PUTRATE(Cnt.v_pfree, VMSTATROW + 12, VMSTATCOL, 9);
459 PUTRATE(Cnt.v_reactivated, VMSTATROW + 13, VMSTATCOL, 9);
460 PUTRATE(Cnt.v_scan, VMSTATROW + 14, VMSTATCOL, 9);
461 PUTRATE(Cnt.v_rev, VMSTATROW + 15, VMSTATCOL, 9);
462 if (LINES - 1 > VMSTATROW + 16)
463 PUTRATE(Cnt.v_intrans, VMSTATROW + 16, VMSTATCOL, 9);
464 PUTRATE(Cnt.v_pageins, PAGEROW + 2, PAGECOL + 5, 5);
465 PUTRATE(Cnt.v_pageouts, PAGEROW + 2, PAGECOL + 10, 5);
466 PUTRATE(Cnt.v_swpin, PAGEROW + 2, PAGECOL + 15, 5); /* - */
467 PUTRATE(Cnt.v_swpout, PAGEROW + 2, PAGECOL + 20, 5); /* - */
468 PUTRATE(Cnt.v_pgpgin, PAGEROW + 3, PAGECOL + 5, 5); /* ? */
469 PUTRATE(Cnt.v_pgpgout, PAGEROW + 3, PAGECOL + 10, 5); /* ? */
470 PUTRATE(Cnt.v_pswpin, PAGEROW + 3, PAGECOL + 15, 5); /* - */
471 PUTRATE(Cnt.v_pswpout, PAGEROW + 3, PAGECOL + 20, 5); /* - */
472 PUTRATE(Cnt.v_swtch, GENSTATROW + 1, GENSTATCOL, 5);
473 PUTRATE(Cnt.v_trap, GENSTATROW + 1, GENSTATCOL + 5, 5);
474 PUTRATE(Cnt.v_syscall, GENSTATROW + 1, GENSTATCOL + 10, 5);
475 PUTRATE(Cnt.v_intr, GENSTATROW + 1, GENSTATCOL + 15, 5);
476 PUTRATE(Cnt.v_soft, GENSTATROW + 1, GENSTATCOL + 20, 5);
477 PUTRATE(Cnt.v_faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
478 mvprintw(DISKROW, DISKCOL + 5, " ");
479 for (i = 0, c = 0; i < dk_ndrive && c < MAXDRIVES; i++)
480 if (dk_select[i]) {
481 mvprintw(DISKROW, DISKCOL + 5 + 5 * c,
482 " %3.3s", dr_name[i]);
483 dinfo(i, ++c);
484 }
485 putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
486 putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
487 #define nz(x) ((x) ? (x) : 1)
488 putfloat(nchtotal.ncs_goodhits * 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
496 int
497 cmdkre(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 */
524 static int
525 ucount()
526 {
527 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, (off_t)0, SEEK_SET);
536 return (nusers);
537 }
538
539 static float
540 cputime(indx)
541 int indx;
542 {
543 double t;
544 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
554 static void
555 putint(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
575 static void
576 putfloat(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
597 static void
598 getinfo(s, st)
599 struct Info *s;
600 enum state st;
601 {
602 int mib[2];
603 size_t size;
604 extern int errno;
605
606 dkreadstats();
607 NREAD(X_CPTIME, s->time, sizeof s->time);
608 NREAD(X_CNT, &s->Cnt, sizeof s->Cnt);
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 memset(&s->Total, 0, sizeof(s->Total));
617 }
618 }
619
620 static void
621 allocinfo(s)
622 struct Info *s;
623 {
624
625 s->intrcnt = (long *) malloc(nintr * sizeof(long));
626 if (s->intrcnt == NULL)
627 errx(2, "out of memory");
628 }
629
630 static void
631 copyinfo(from, to)
632 struct Info *from, *to;
633 {
634 long *intrcnt;
635
636 intrcnt = to->intrcnt;
637 *to = *from;
638 memmove(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof (int));
639 }
640
641 static void
642 dinfo(dn, c)
643 int dn, c;
644 {
645 double words, atime;
646
647 c = DISKCOL + c * 5;
648
649 /* time busy in disk activity */
650 atime = (double)cur.dk_time[dn].tv_sec +
651 ((double)cur.dk_time[dn].tv_usec / (double)1000000);
652
653 words = cur.dk_bytes[dn] / 1024.0; /* # of K transferred */
654
655 putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
656 putint((int)((float)cur.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
657 putint((int)(words/etime + 0.5), DISKROW + 3, c, 5);
658 putfloat(atime/etime, DISKROW + 4, c, 5, 1, 1);
659 }
660