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