iostat.c revision 1.3 1 1.3 mycroft /* $NetBSD: iostat.c,v 1.3 1995/05/17 15:51:47 mycroft Exp $ */
2 1.2 jtc
3 1.1 jtc /*
4 1.1 jtc * Copyright (c) 1980, 1992, 1993
5 1.1 jtc * The Regents of the University of California. All rights reserved.
6 1.1 jtc *
7 1.1 jtc * Redistribution and use in source and binary forms, with or without
8 1.1 jtc * modification, are permitted provided that the following conditions
9 1.1 jtc * are met:
10 1.1 jtc * 1. Redistributions of source code must retain the above copyright
11 1.1 jtc * notice, this list of conditions and the following disclaimer.
12 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jtc * notice, this list of conditions and the following disclaimer in the
14 1.1 jtc * documentation and/or other materials provided with the distribution.
15 1.1 jtc * 3. All advertising materials mentioning features or use of this software
16 1.1 jtc * must display the following acknowledgement:
17 1.1 jtc * This product includes software developed by the University of
18 1.1 jtc * California, Berkeley and its contributors.
19 1.1 jtc * 4. Neither the name of the University nor the names of its contributors
20 1.1 jtc * may be used to endorse or promote products derived from this software
21 1.1 jtc * without specific prior written permission.
22 1.1 jtc *
23 1.1 jtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 jtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 jtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 jtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 jtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 jtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 jtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 jtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 jtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 jtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 jtc * SUCH DAMAGE.
34 1.1 jtc */
35 1.1 jtc
36 1.1 jtc #ifndef lint
37 1.2 jtc #if 0
38 1.1 jtc static char sccsid[] = "@(#)iostat.c 8.1 (Berkeley) 6/6/93";
39 1.2 jtc #endif
40 1.3 mycroft static char rcsid[] = "$NetBSD: iostat.c,v 1.3 1995/05/17 15:51:47 mycroft Exp $";
41 1.1 jtc #endif not lint
42 1.1 jtc
43 1.1 jtc #include <sys/param.h>
44 1.1 jtc #include <sys/dkstat.h>
45 1.1 jtc #include <sys/buf.h>
46 1.1 jtc
47 1.1 jtc #include <string.h>
48 1.1 jtc #include <stdlib.h>
49 1.1 jtc #include <nlist.h>
50 1.1 jtc #include <paths.h>
51 1.1 jtc #include "systat.h"
52 1.1 jtc #include "extern.h"
53 1.1 jtc
54 1.1 jtc static struct nlist namelist[] = {
55 1.1 jtc #define X_DK_BUSY 0
56 1.1 jtc { "_dk_busy" },
57 1.1 jtc #define X_DK_TIME 1
58 1.1 jtc { "_dk_time" },
59 1.1 jtc #define X_DK_XFER 2
60 1.1 jtc { "_dk_xfer" },
61 1.1 jtc #define X_DK_WDS 3
62 1.1 jtc { "_dk_wds" },
63 1.1 jtc #define X_DK_SEEK 4
64 1.1 jtc { "_dk_seek" },
65 1.1 jtc #define X_CP_TIME 5
66 1.1 jtc { "_cp_time" },
67 1.1 jtc #ifdef vax
68 1.1 jtc #define X_MBDINIT (X_CP_TIME+1)
69 1.1 jtc { "_mbdinit" },
70 1.1 jtc #define X_UBDINIT (X_CP_TIME+2)
71 1.1 jtc { "_ubdinit" },
72 1.1 jtc #endif
73 1.1 jtc #ifdef tahoe
74 1.1 jtc #define X_VBDINIT (X_CP_TIME+1)
75 1.1 jtc { "_vbdinit" },
76 1.1 jtc #endif
77 1.1 jtc { "" },
78 1.1 jtc };
79 1.1 jtc
80 1.1 jtc static struct {
81 1.1 jtc int dk_busy;
82 1.1 jtc long cp_time[CPUSTATES];
83 1.1 jtc long *dk_time;
84 1.1 jtc long *dk_wds;
85 1.1 jtc long *dk_seek;
86 1.1 jtc long *dk_xfer;
87 1.1 jtc } s, s1;
88 1.1 jtc
89 1.1 jtc static int linesperregion;
90 1.1 jtc static double etime;
91 1.1 jtc static int numbers = 0; /* default display bar graphs */
92 1.1 jtc static int msps = 0; /* default ms/seek shown */
93 1.1 jtc
94 1.1 jtc static int barlabels __P((int));
95 1.1 jtc static void histogram __P((double, int, double));
96 1.1 jtc static int numlabels __P((int));
97 1.1 jtc static int stats __P((int, int, int));
98 1.1 jtc static void stat1 __P((int, int));
99 1.1 jtc
100 1.1 jtc
101 1.1 jtc WINDOW *
102 1.1 jtc openiostat()
103 1.1 jtc {
104 1.1 jtc return (subwin(stdscr, LINES-1-5, 0, 5, 0));
105 1.1 jtc }
106 1.1 jtc
107 1.1 jtc void
108 1.1 jtc closeiostat(w)
109 1.1 jtc WINDOW *w;
110 1.1 jtc {
111 1.1 jtc if (w == NULL)
112 1.1 jtc return;
113 1.1 jtc wclear(w);
114 1.1 jtc wrefresh(w);
115 1.1 jtc delwin(w);
116 1.1 jtc }
117 1.1 jtc
118 1.1 jtc int
119 1.1 jtc initiostat()
120 1.1 jtc {
121 1.1 jtc if (namelist[X_DK_BUSY].n_type == 0) {
122 1.1 jtc if (kvm_nlist(kd, namelist)) {
123 1.1 jtc nlisterr(namelist);
124 1.1 jtc return(0);
125 1.1 jtc }
126 1.1 jtc if (namelist[X_DK_BUSY].n_type == 0) {
127 1.1 jtc error("Disk init information isn't in namelist");
128 1.1 jtc return(0);
129 1.1 jtc }
130 1.1 jtc }
131 1.1 jtc if (! dkinit())
132 1.1 jtc return(0);
133 1.1 jtc if (dk_ndrive) {
134 1.1 jtc #define allocate(e, t) \
135 1.1 jtc s./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
136 1.1 jtc s1./**/e = (t *)calloc(dk_ndrive, sizeof (t));
137 1.1 jtc allocate(dk_time, long);
138 1.1 jtc allocate(dk_wds, long);
139 1.1 jtc allocate(dk_seek, long);
140 1.1 jtc allocate(dk_xfer, long);
141 1.1 jtc #undef allocate
142 1.1 jtc }
143 1.1 jtc return(1);
144 1.1 jtc }
145 1.1 jtc
146 1.1 jtc void
147 1.1 jtc fetchiostat()
148 1.1 jtc {
149 1.1 jtc if (namelist[X_DK_BUSY].n_type == 0)
150 1.1 jtc return;
151 1.1 jtc NREAD(X_DK_BUSY, &s.dk_busy, LONG);
152 1.1 jtc NREAD(X_DK_TIME, s.dk_time, dk_ndrive * LONG);
153 1.1 jtc NREAD(X_DK_XFER, s.dk_xfer, dk_ndrive * LONG);
154 1.1 jtc NREAD(X_DK_WDS, s.dk_wds, dk_ndrive * LONG);
155 1.1 jtc NREAD(X_DK_SEEK, s.dk_seek, dk_ndrive * LONG);
156 1.1 jtc NREAD(X_CP_TIME, s.cp_time, sizeof s.cp_time);
157 1.1 jtc }
158 1.1 jtc
159 1.1 jtc #define INSET 10
160 1.1 jtc
161 1.1 jtc void
162 1.1 jtc labeliostat()
163 1.1 jtc {
164 1.1 jtc int row;
165 1.1 jtc
166 1.1 jtc if (namelist[X_DK_BUSY].n_type == 0) {
167 1.1 jtc error("No dk_busy defined.");
168 1.1 jtc return;
169 1.1 jtc }
170 1.1 jtc row = 0;
171 1.1 jtc wmove(wnd, row, 0); wclrtobot(wnd);
172 1.1 jtc mvwaddstr(wnd, row++, INSET,
173 1.1 jtc "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
174 1.1 jtc mvwaddstr(wnd, row++, 0, "cpu user|");
175 1.1 jtc mvwaddstr(wnd, row++, 0, " nice|");
176 1.1 jtc mvwaddstr(wnd, row++, 0, " system|");
177 1.3 mycroft mvwaddstr(wnd, row++, 0, "interrupt|");
178 1.1 jtc mvwaddstr(wnd, row++, 0, " idle|");
179 1.1 jtc if (numbers)
180 1.1 jtc row = numlabels(row + 1);
181 1.1 jtc else
182 1.1 jtc row = barlabels(row + 1);
183 1.1 jtc }
184 1.1 jtc
185 1.1 jtc static int
186 1.1 jtc numlabels(row)
187 1.1 jtc int row;
188 1.1 jtc {
189 1.1 jtc int i, col, regions, ndrives;
190 1.1 jtc
191 1.1 jtc #define COLWIDTH 14
192 1.1 jtc #define DRIVESPERLINE ((wnd->maxx - INSET) / COLWIDTH)
193 1.1 jtc for (ndrives = 0, i = 0; i < dk_ndrive; i++)
194 1.1 jtc if (dk_select[i])
195 1.1 jtc ndrives++;
196 1.1 jtc regions = howmany(ndrives, DRIVESPERLINE);
197 1.1 jtc /*
198 1.1 jtc * Deduct -regions for blank line after each scrolling region.
199 1.1 jtc */
200 1.1 jtc linesperregion = (wnd->maxy - row - regions) / regions;
201 1.1 jtc /*
202 1.1 jtc * Minimum region contains space for two
203 1.1 jtc * label lines and one line of statistics.
204 1.1 jtc */
205 1.1 jtc if (linesperregion < 3)
206 1.1 jtc linesperregion = 3;
207 1.1 jtc col = 0;
208 1.1 jtc for (i = 0; i < dk_ndrive; i++)
209 1.1 jtc if (dk_select[i] && dk_mspw[i] != 0.0) {
210 1.1 jtc if (col + COLWIDTH >= wnd->maxx - INSET) {
211 1.1 jtc col = 0, row += linesperregion + 1;
212 1.1 jtc if (row > wnd->maxy - (linesperregion + 1))
213 1.1 jtc break;
214 1.1 jtc }
215 1.1 jtc mvwaddstr(wnd, row, col + 4, dr_name[i]);
216 1.1 jtc mvwaddstr(wnd, row + 1, col, "bps tps msps");
217 1.1 jtc col += COLWIDTH;
218 1.1 jtc }
219 1.1 jtc if (col)
220 1.1 jtc row += linesperregion + 1;
221 1.1 jtc return (row);
222 1.1 jtc }
223 1.1 jtc
224 1.1 jtc static int
225 1.1 jtc barlabels(row)
226 1.1 jtc int row;
227 1.1 jtc {
228 1.1 jtc int i;
229 1.1 jtc
230 1.1 jtc mvwaddstr(wnd, row++, INSET,
231 1.1 jtc "/0 /5 /10 /15 /20 /25 /30 /35 /40 /45 /50");
232 1.1 jtc linesperregion = 2 + msps;
233 1.1 jtc for (i = 0; i < dk_ndrive; i++)
234 1.1 jtc if (dk_select[i] && dk_mspw[i] != 0.0) {
235 1.1 jtc if (row > wnd->maxy - linesperregion)
236 1.1 jtc break;
237 1.1 jtc mvwprintw(wnd, row++, 0, "%3.3s bps|", dr_name[i]);
238 1.1 jtc mvwaddstr(wnd, row++, 0, " tps|");
239 1.1 jtc if (msps)
240 1.1 jtc mvwaddstr(wnd, row++, 0, " msps|");
241 1.1 jtc }
242 1.1 jtc return (row);
243 1.1 jtc }
244 1.1 jtc
245 1.1 jtc
246 1.1 jtc void
247 1.1 jtc showiostat()
248 1.1 jtc {
249 1.1 jtc register long t;
250 1.1 jtc register int i, row, col;
251 1.1 jtc
252 1.1 jtc if (namelist[X_DK_BUSY].n_type == 0)
253 1.1 jtc return;
254 1.1 jtc for (i = 0; i < dk_ndrive; i++) {
255 1.1 jtc #define X(fld) t = s.fld[i]; s.fld[i] -= s1.fld[i]; s1.fld[i] = t
256 1.1 jtc X(dk_xfer); X(dk_seek); X(dk_wds); X(dk_time);
257 1.1 jtc }
258 1.1 jtc etime = 0;
259 1.1 jtc for(i = 0; i < CPUSTATES; i++) {
260 1.1 jtc X(cp_time);
261 1.1 jtc etime += s.cp_time[i];
262 1.1 jtc }
263 1.1 jtc if (etime == 0.0)
264 1.1 jtc etime = 1.0;
265 1.1 jtc etime /= (float) hz;
266 1.1 jtc row = 1;
267 1.1 jtc
268 1.1 jtc /*
269 1.3 mycroft * Interrupt CPU state not calculated yet.
270 1.1 jtc */
271 1.3 mycroft for (i = 0; i < CPUSTATES; i++)
272 1.1 jtc stat1(row++, i);
273 1.1 jtc if (!numbers) {
274 1.1 jtc row += 2;
275 1.1 jtc for (i = 0; i < dk_ndrive; i++)
276 1.1 jtc if (dk_select[i] && dk_mspw[i] != 0.0) {
277 1.1 jtc if (row > wnd->maxy - linesperregion)
278 1.1 jtc break;
279 1.1 jtc row = stats(row, INSET, i);
280 1.1 jtc }
281 1.1 jtc return;
282 1.1 jtc }
283 1.1 jtc col = 0;
284 1.1 jtc wmove(wnd, row + linesperregion, 0);
285 1.1 jtc wdeleteln(wnd);
286 1.1 jtc wmove(wnd, row + 3, 0);
287 1.1 jtc winsertln(wnd);
288 1.1 jtc for (i = 0; i < dk_ndrive; i++)
289 1.1 jtc if (dk_select[i] && dk_mspw[i] != 0.0) {
290 1.1 jtc if (col + COLWIDTH >= wnd->maxx) {
291 1.1 jtc col = 0, row += linesperregion + 1;
292 1.1 jtc if (row > wnd->maxy - (linesperregion + 1))
293 1.1 jtc break;
294 1.1 jtc wmove(wnd, row + linesperregion, 0);
295 1.1 jtc wdeleteln(wnd);
296 1.1 jtc wmove(wnd, row + 3, 0);
297 1.1 jtc winsertln(wnd);
298 1.1 jtc }
299 1.1 jtc (void) stats(row + 3, col, i);
300 1.1 jtc col += COLWIDTH;
301 1.1 jtc }
302 1.1 jtc }
303 1.1 jtc
304 1.1 jtc static int
305 1.1 jtc stats(row, col, dn)
306 1.1 jtc int row, col, dn;
307 1.1 jtc {
308 1.1 jtc double atime, words, xtime, itime;
309 1.1 jtc
310 1.1 jtc atime = s.dk_time[dn];
311 1.1 jtc atime /= (float) hz;
312 1.1 jtc words = s.dk_wds[dn]*32.0; /* number of words transferred */
313 1.1 jtc xtime = dk_mspw[dn]*words; /* transfer time */
314 1.1 jtc itime = atime - xtime; /* time not transferring */
315 1.1 jtc if (xtime < 0)
316 1.1 jtc itime += xtime, xtime = 0;
317 1.1 jtc if (itime < 0)
318 1.1 jtc xtime += itime, itime = 0;
319 1.1 jtc if (numbers) {
320 1.1 jtc mvwprintw(wnd, row, col, "%3.0f%4.0f%5.1f",
321 1.1 jtc words / 512 / etime, s.dk_xfer[dn] / etime,
322 1.1 jtc s.dk_seek[dn] ? itime * 1000. / s.dk_seek[dn] : 0.0);
323 1.1 jtc return (row);
324 1.1 jtc }
325 1.1 jtc wmove(wnd, row++, col);
326 1.1 jtc histogram(words / 512 / etime, 50, 1.0);
327 1.1 jtc wmove(wnd, row++, col);
328 1.1 jtc histogram(s.dk_xfer[dn] / etime, 50, 1.0);
329 1.1 jtc if (msps) {
330 1.1 jtc wmove(wnd, row++, col);
331 1.1 jtc histogram(s.dk_seek[dn] ? itime * 1000. / s.dk_seek[dn] : 0,
332 1.1 jtc 50, 1.0);
333 1.1 jtc }
334 1.1 jtc return (row);
335 1.1 jtc }
336 1.1 jtc
337 1.1 jtc static void
338 1.1 jtc stat1(row, o)
339 1.1 jtc int row, o;
340 1.1 jtc {
341 1.1 jtc register int i;
342 1.1 jtc double time;
343 1.1 jtc
344 1.1 jtc time = 0;
345 1.1 jtc for (i = 0; i < CPUSTATES; i++)
346 1.1 jtc time += s.cp_time[i];
347 1.1 jtc if (time == 0.0)
348 1.1 jtc time = 1.0;
349 1.1 jtc wmove(wnd, row, INSET);
350 1.1 jtc #define CPUSCALE 0.5
351 1.1 jtc histogram(100.0 * s.cp_time[o] / time, 50, CPUSCALE);
352 1.1 jtc }
353 1.1 jtc
354 1.1 jtc static void
355 1.1 jtc histogram(val, colwidth, scale)
356 1.1 jtc double val;
357 1.1 jtc int colwidth;
358 1.1 jtc double scale;
359 1.1 jtc {
360 1.1 jtc char buf[10];
361 1.1 jtc register int k;
362 1.1 jtc register int v = (int)(val * scale) + 0.5;
363 1.1 jtc
364 1.1 jtc k = MIN(v, colwidth);
365 1.1 jtc if (v > colwidth) {
366 1.1 jtc sprintf(buf, "%4.1f", val);
367 1.1 jtc k -= strlen(buf);
368 1.1 jtc while (k--)
369 1.1 jtc waddch(wnd, 'X');
370 1.1 jtc waddstr(wnd, buf);
371 1.1 jtc return;
372 1.1 jtc }
373 1.1 jtc while (k--)
374 1.1 jtc waddch(wnd, 'X');
375 1.1 jtc wclrtoeol(wnd);
376 1.1 jtc }
377 1.1 jtc
378 1.1 jtc int
379 1.1 jtc cmdiostat(cmd, args)
380 1.1 jtc char *cmd, *args;
381 1.1 jtc {
382 1.1 jtc
383 1.1 jtc if (prefix(cmd, "msps"))
384 1.1 jtc msps = !msps;
385 1.1 jtc else if (prefix(cmd, "numbers"))
386 1.1 jtc numbers = 1;
387 1.1 jtc else if (prefix(cmd, "bars"))
388 1.1 jtc numbers = 0;
389 1.1 jtc else if (!dkcmd(cmd, args))
390 1.1 jtc return (0);
391 1.1 jtc wclear(wnd);
392 1.1 jtc labeliostat();
393 1.1 jtc refresh();
394 1.1 jtc return (1);
395 1.1 jtc }
396