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