iostat.c revision 1.35 1 1.35 dsl /* $NetBSD: iostat.c,v 1.35 2006/03/18 16:48:44 dsl 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.28 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 jtc * may be used to endorse or promote products derived from this software
17 1.1 jtc * without specific prior written permission.
18 1.1 jtc *
19 1.1 jtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 jtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 jtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 jtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 jtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 jtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 jtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 jtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 jtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 jtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 jtc * SUCH DAMAGE.
30 1.1 jtc */
31 1.1 jtc
32 1.8 mrg #include <sys/cdefs.h>
33 1.1 jtc #ifndef lint
34 1.2 jtc #if 0
35 1.1 jtc static char sccsid[] = "@(#)iostat.c 8.1 (Berkeley) 6/6/93";
36 1.2 jtc #endif
37 1.35 dsl __RCSID("$NetBSD: iostat.c,v 1.35 2006/03/18 16:48:44 dsl Exp $");
38 1.18 cgd #endif /* not lint */
39 1.1 jtc
40 1.1 jtc #include <sys/param.h>
41 1.1 jtc
42 1.1 jtc #include <string.h>
43 1.17 simonb
44 1.1 jtc #include "systat.h"
45 1.1 jtc #include "extern.h"
46 1.5 thorpej #include "dkstats.h"
47 1.33 blymn #include "tpstats.h"
48 1.1 jtc
49 1.1 jtc static int linesperregion;
50 1.1 jtc static double etime;
51 1.1 jtc static int numbers = 0; /* default display bar graphs */
52 1.5 thorpej static int secs = 0; /* default seconds shown */
53 1.23 mrg static int read_write = 0; /* default read/write shown */
54 1.1 jtc
55 1.16 ad static int barlabels(int);
56 1.16 ad static void histogram(double, int, double);
57 1.16 ad static int numlabels(int);
58 1.16 ad static int stats(int, int, int);
59 1.33 blymn static int tpstats(int, int, int);
60 1.16 ad static void stat1(int, int);
61 1.1 jtc
62 1.1 jtc
63 1.1 jtc WINDOW *
64 1.16 ad openiostat(void)
65 1.1 jtc {
66 1.8 mrg
67 1.27 dsl return (subwin(stdscr, -1, 0, 5, 0));
68 1.1 jtc }
69 1.1 jtc
70 1.1 jtc void
71 1.16 ad closeiostat(WINDOW *w)
72 1.1 jtc {
73 1.8 mrg
74 1.1 jtc if (w == NULL)
75 1.1 jtc return;
76 1.1 jtc wclear(w);
77 1.1 jtc wrefresh(w);
78 1.1 jtc delwin(w);
79 1.1 jtc }
80 1.1 jtc
81 1.1 jtc int
82 1.16 ad initiostat(void)
83 1.1 jtc {
84 1.8 mrg
85 1.19 augustss dkinit(1);
86 1.33 blymn tpinit(1);
87 1.34 dsl cpureadstats();
88 1.5 thorpej dkreadstats();
89 1.33 blymn tpreadstats();
90 1.8 mrg return(1);
91 1.1 jtc }
92 1.1 jtc
93 1.1 jtc void
94 1.16 ad fetchiostat(void)
95 1.1 jtc {
96 1.8 mrg
97 1.34 dsl cpureadstats();
98 1.34 dsl
99 1.34 dsl if (dk_ndrive != 0)
100 1.33 blymn dkreadstats();
101 1.33 blymn
102 1.34 dsl if (tp_ndrive != 0)
103 1.33 blymn tpreadstats();
104 1.1 jtc }
105 1.1 jtc
106 1.12 mjl #define INSET 14
107 1.1 jtc
108 1.1 jtc void
109 1.16 ad labeliostat(void)
110 1.1 jtc {
111 1.1 jtc int row;
112 1.1 jtc
113 1.33 blymn if ((dk_ndrive == 0) && (tp_ndrive == 0)) {
114 1.5 thorpej error("No drives defined.");
115 1.1 jtc return;
116 1.1 jtc }
117 1.1 jtc row = 0;
118 1.1 jtc wmove(wnd, row, 0); wclrtobot(wnd);
119 1.1 jtc mvwaddstr(wnd, row++, INSET,
120 1.1 jtc "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
121 1.30 wiz mvwaddstr(wnd, row++, 0, " CPU user|");
122 1.12 mjl mvwaddstr(wnd, row++, 0, " nice|");
123 1.12 mjl mvwaddstr(wnd, row++, 0, " system|");
124 1.12 mjl mvwaddstr(wnd, row++, 0, " interrupt|");
125 1.12 mjl mvwaddstr(wnd, row++, 0, " idle|");
126 1.1 jtc if (numbers)
127 1.1 jtc row = numlabels(row + 1);
128 1.1 jtc else
129 1.1 jtc row = barlabels(row + 1);
130 1.1 jtc }
131 1.1 jtc
132 1.1 jtc static int
133 1.16 ad numlabels(int row)
134 1.1 jtc {
135 1.1 jtc int i, col, regions, ndrives;
136 1.1 jtc
137 1.32 dsl #define COLWIDTH (9 + secs * 5 + 1 + read_write * 9 + 1)
138 1.29 dsl #define DRIVESPERLINE ((getmaxx(wnd) + 1) / COLWIDTH)
139 1.1 jtc for (ndrives = 0, i = 0; i < dk_ndrive; i++)
140 1.5 thorpej if (cur.dk_select[i])
141 1.1 jtc ndrives++;
142 1.33 blymn for (i = 0; i < tp_ndrive; i++)
143 1.33 blymn if (cur_tape.select[i])
144 1.33 blymn ndrives++;
145 1.33 blymn
146 1.1 jtc regions = howmany(ndrives, DRIVESPERLINE);
147 1.1 jtc /*
148 1.1 jtc * Deduct -regions for blank line after each scrolling region.
149 1.1 jtc */
150 1.29 dsl linesperregion = (getmaxy(wnd) - row - regions + 1) / regions;
151 1.1 jtc /*
152 1.1 jtc * Minimum region contains space for two
153 1.1 jtc * label lines and one line of statistics.
154 1.1 jtc */
155 1.1 jtc if (linesperregion < 3)
156 1.1 jtc linesperregion = 3;
157 1.1 jtc col = 0;
158 1.33 blymn for (i = 0; i < (dk_ndrive + tp_ndrive); i++)
159 1.33 blymn if (((i < dk_ndrive) && (cur.dk_select[i])) ||
160 1.33 blymn ((i >= dk_ndrive) && (cur_tape.select[i - dk_ndrive]))) {
161 1.29 dsl if (col + COLWIDTH - 1 > getmaxx(wnd)) {
162 1.1 jtc col = 0, row += linesperregion + 1;
163 1.29 dsl if (row > getmaxy(wnd) - (linesperregion))
164 1.1 jtc break;
165 1.1 jtc }
166 1.33 blymn
167 1.33 blymn if (i < dk_ndrive)
168 1.33 blymn mvwprintw(wnd, row, col + 5, "%s",
169 1.33 blymn cur.dk_name[i]);
170 1.33 blymn else
171 1.33 blymn mvwprintw(wnd, row, col + 5, "%s",
172 1.33 blymn cur_tape.name[i - dk_ndrive]);
173 1.33 blymn
174 1.23 mrg if (read_write)
175 1.32 dsl mvwprintw(wnd, row, col + 11 + secs * 5,
176 1.31 dsl "(write)");
177 1.32 dsl mvwprintw(wnd, row + 1, col, " kBps %s",
178 1.31 dsl read_write ? "r/s" : "tps");
179 1.31 dsl if (secs)
180 1.31 dsl waddstr(wnd, " sec");
181 1.31 dsl if (read_write)
182 1.32 dsl waddstr(wnd, " kBps w/s");
183 1.1 jtc col += COLWIDTH;
184 1.1 jtc }
185 1.1 jtc if (col)
186 1.1 jtc row += linesperregion + 1;
187 1.1 jtc return (row);
188 1.1 jtc }
189 1.1 jtc
190 1.1 jtc static int
191 1.16 ad barlabels(int row)
192 1.1 jtc {
193 1.1 jtc int i;
194 1.1 jtc
195 1.1 jtc mvwaddstr(wnd, row++, INSET,
196 1.5 thorpej "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
197 1.23 mrg linesperregion = 2 + secs + (read_write ? 2 : 0);
198 1.1 jtc for (i = 0; i < dk_ndrive; i++)
199 1.23 mrg if (cur.dk_select[i]) {
200 1.7 jtc if (row > getmaxy(wnd) - linesperregion)
201 1.1 jtc break;
202 1.24 thorpej mvwprintw(wnd, row++, 0, "%7.7s kBps|",
203 1.24 thorpej cur.dk_name[i]);
204 1.12 mjl mvwaddstr(wnd, row++, 0, " tps|");
205 1.23 mrg if (read_write) {
206 1.23 mrg mvwprintw(wnd, row++, 0, " (write) kBps|");
207 1.23 mrg mvwaddstr(wnd, row++, 0, " tps|");
208 1.23 mrg }
209 1.5 thorpej if (secs)
210 1.12 mjl mvwaddstr(wnd, row++, 0, " msec|");
211 1.1 jtc }
212 1.33 blymn for (i = 0; i < tp_ndrive; i++)
213 1.33 blymn if (cur_tape.select[i]) {
214 1.33 blymn if (row > getmaxy(wnd) - linesperregion)
215 1.33 blymn break;
216 1.33 blymn mvwprintw(wnd, row++, 0, "%7.7s kBps|",
217 1.33 blymn cur_tape.name[i]);
218 1.33 blymn mvwaddstr(wnd, row++, 0, " tps|");
219 1.33 blymn if (read_write) {
220 1.33 blymn mvwprintw(wnd, row++, 0, " (write) kBps|");
221 1.33 blymn mvwaddstr(wnd, row++, 0, " tps|");
222 1.33 blymn }
223 1.33 blymn if (secs)
224 1.33 blymn mvwaddstr(wnd, row++, 0, " msec|");
225 1.33 blymn }
226 1.1 jtc return (row);
227 1.1 jtc }
228 1.1 jtc
229 1.1 jtc void
230 1.16 ad showiostat(void)
231 1.1 jtc {
232 1.9 lukem int i, row, col;
233 1.1 jtc
234 1.5 thorpej if (dk_ndrive == 0)
235 1.1 jtc return;
236 1.5 thorpej dkswap();
237 1.34 dsl cpuswap();
238 1.33 blymn tpswap();
239 1.5 thorpej
240 1.21 sommerfe etime = cur.cp_etime;
241 1.1 jtc row = 1;
242 1.1 jtc
243 1.1 jtc /*
244 1.3 mycroft * Interrupt CPU state not calculated yet.
245 1.21 sommerfe */
246 1.3 mycroft for (i = 0; i < CPUSTATES; i++)
247 1.1 jtc stat1(row++, i);
248 1.1 jtc if (!numbers) {
249 1.1 jtc row += 2;
250 1.1 jtc for (i = 0; i < dk_ndrive; i++)
251 1.23 mrg if (cur.dk_select[i]) {
252 1.7 jtc if (row > getmaxy(wnd) - linesperregion)
253 1.1 jtc break;
254 1.1 jtc row = stats(row, INSET, i);
255 1.1 jtc }
256 1.33 blymn for (i = 0; i < tp_ndrive; i++)
257 1.33 blymn if (cur_tape.select[i]) {
258 1.33 blymn if (row > getmaxy(wnd) - linesperregion)
259 1.33 blymn break;
260 1.33 blymn row = tpstats(row, INSET, i);
261 1.33 blymn }
262 1.1 jtc return;
263 1.1 jtc }
264 1.1 jtc col = 0;
265 1.1 jtc wmove(wnd, row + linesperregion, 0);
266 1.1 jtc wdeleteln(wnd);
267 1.1 jtc wmove(wnd, row + 3, 0);
268 1.1 jtc winsertln(wnd);
269 1.1 jtc for (i = 0; i < dk_ndrive; i++)
270 1.23 mrg if (cur.dk_select[i]) {
271 1.29 dsl if (col + COLWIDTH - 1 > getmaxx(wnd)) {
272 1.1 jtc col = 0, row += linesperregion + 1;
273 1.7 jtc if (row > getmaxy(wnd) - (linesperregion + 1))
274 1.1 jtc break;
275 1.1 jtc wmove(wnd, row + linesperregion, 0);
276 1.1 jtc wdeleteln(wnd);
277 1.1 jtc wmove(wnd, row + 3, 0);
278 1.1 jtc winsertln(wnd);
279 1.1 jtc }
280 1.1 jtc (void) stats(row + 3, col, i);
281 1.1 jtc col += COLWIDTH;
282 1.1 jtc }
283 1.33 blymn for (i = 0; i < tp_ndrive; i++)
284 1.33 blymn if (cur_tape.select[i]) {
285 1.33 blymn if (col + COLWIDTH - 1 > getmaxx(wnd)) {
286 1.33 blymn col = 0, row += linesperregion + 1;
287 1.33 blymn if (row > getmaxy(wnd) - (linesperregion + 1))
288 1.33 blymn break;
289 1.33 blymn wmove(wnd, row + linesperregion, 0);
290 1.33 blymn wdeleteln(wnd);
291 1.33 blymn wmove(wnd, row + 3, 0);
292 1.33 blymn winsertln(wnd);
293 1.33 blymn }
294 1.33 blymn (void) stats(row + 3, col, i);
295 1.33 blymn col += COLWIDTH;
296 1.33 blymn }
297 1.1 jtc }
298 1.1 jtc
299 1.1 jtc static int
300 1.16 ad stats(int row, int col, int dn)
301 1.1 jtc {
302 1.23 mrg double atime, rwords, wwords;
303 1.31 dsl uint64_t rxfer;
304 1.5 thorpej
305 1.5 thorpej /* time busy in disk activity */
306 1.5 thorpej atime = (double)cur.dk_time[dn].tv_sec +
307 1.5 thorpej ((double)cur.dk_time[dn].tv_usec / (double)1000000);
308 1.1 jtc
309 1.23 mrg /* # of k transferred */
310 1.23 mrg rwords = cur.dk_rbytes[dn] / 1024.0;
311 1.23 mrg wwords = cur.dk_wbytes[dn] / 1024.0;
312 1.31 dsl rxfer = cur.dk_rxfer[dn];
313 1.31 dsl if (!read_write) {
314 1.35 dsl rwords += wwords;
315 1.31 dsl rxfer += cur.dk_wxfer[dn];
316 1.31 dsl }
317 1.1 jtc if (numbers) {
318 1.32 dsl mvwprintw(wnd, row, col, "%5.0f%4.0f",
319 1.31 dsl rwords / etime, rxfer / etime);
320 1.31 dsl if (secs)
321 1.31 dsl wprintw(wnd, "%5.1f", atime / etime);
322 1.23 mrg if (read_write)
323 1.32 dsl wprintw(wnd, " %5.0f%4.0f",
324 1.23 mrg wwords / etime, cur.dk_wxfer[dn] / etime);
325 1.1 jtc return (row);
326 1.1 jtc }
327 1.23 mrg
328 1.31 dsl wmove(wnd, row++, col);
329 1.31 dsl histogram(rwords / etime, 50, 0.5);
330 1.31 dsl wmove(wnd, row++, col);
331 1.31 dsl histogram(rxfer / etime, 50, 0.5);
332 1.23 mrg if (read_write) {
333 1.23 mrg wmove(wnd, row++, col);
334 1.23 mrg histogram(wwords / etime, 50, 0.5);
335 1.23 mrg wmove(wnd, row++, col);
336 1.23 mrg histogram(cur.dk_wxfer[dn] / etime, 50, 0.5);
337 1.23 mrg }
338 1.23 mrg
339 1.5 thorpej if (secs) {
340 1.1 jtc wmove(wnd, row++, col);
341 1.5 thorpej atime *= 1000; /* In milliseconds */
342 1.5 thorpej histogram(atime / etime, 50, 0.5);
343 1.1 jtc }
344 1.1 jtc return (row);
345 1.1 jtc }
346 1.1 jtc
347 1.33 blymn static int
348 1.33 blymn tpstats(int row, int col, int dn)
349 1.33 blymn {
350 1.33 blymn double atime, rwords, wwords;
351 1.33 blymn uint64_t rxfer;
352 1.33 blymn
353 1.33 blymn /* time busy in disk activity */
354 1.33 blymn atime = (double)cur_tape.time[dn].tv_sec +
355 1.33 blymn ((double)cur_tape.time[dn].tv_usec / (double)1000000);
356 1.33 blymn
357 1.33 blymn /* # of k transferred */
358 1.33 blymn rwords = cur_tape.rbytes[dn] / 1024.0;
359 1.33 blymn wwords = cur_tape.wbytes[dn] / 1024.0;
360 1.33 blymn rxfer = cur_tape.rxfer[dn];
361 1.33 blymn if (!read_write) {
362 1.33 blymn rwords = wwords;
363 1.33 blymn rxfer += cur_tape.wxfer[dn];
364 1.33 blymn }
365 1.33 blymn if (numbers) {
366 1.33 blymn mvwprintw(wnd, row, col, "%5.0f%4.0f",
367 1.33 blymn rwords / etime, rxfer / etime);
368 1.33 blymn if (secs)
369 1.33 blymn wprintw(wnd, "%5.1f", atime / etime);
370 1.33 blymn if (read_write)
371 1.33 blymn wprintw(wnd, " %5.0f%4.0f",
372 1.33 blymn wwords / etime, cur_tape.wxfer[dn] / etime);
373 1.33 blymn return (row);
374 1.33 blymn }
375 1.33 blymn
376 1.33 blymn wmove(wnd, row++, col);
377 1.33 blymn histogram(rwords / etime, 50, 0.5);
378 1.33 blymn wmove(wnd, row++, col);
379 1.33 blymn histogram(rxfer / etime, 50, 0.5);
380 1.33 blymn if (read_write) {
381 1.33 blymn wmove(wnd, row++, col);
382 1.33 blymn histogram(wwords / etime, 50, 0.5);
383 1.33 blymn wmove(wnd, row++, col);
384 1.33 blymn histogram(cur_tape.wxfer[dn] / etime, 50, 0.5);
385 1.33 blymn }
386 1.33 blymn
387 1.33 blymn if (secs) {
388 1.33 blymn wmove(wnd, row++, col);
389 1.33 blymn atime *= 1000; /* In milliseconds */
390 1.33 blymn histogram(atime / etime, 50, 0.5);
391 1.33 blymn }
392 1.33 blymn return (row);
393 1.33 blymn }
394 1.33 blymn
395 1.1 jtc static void
396 1.16 ad stat1(int row, int o)
397 1.1 jtc {
398 1.9 lukem int i;
399 1.31 dsl double total_time;
400 1.1 jtc
401 1.31 dsl total_time = 0;
402 1.1 jtc for (i = 0; i < CPUSTATES; i++)
403 1.31 dsl total_time += cur.cp_time[i];
404 1.31 dsl if (total_time == 0.0)
405 1.31 dsl total_time = 1.0;
406 1.1 jtc wmove(wnd, row, INSET);
407 1.1 jtc #define CPUSCALE 0.5
408 1.31 dsl histogram(100.0 * cur.cp_time[o] / total_time, 50, CPUSCALE);
409 1.1 jtc }
410 1.1 jtc
411 1.1 jtc static void
412 1.16 ad histogram(double val, int colwidth, double scale)
413 1.1 jtc {
414 1.26 dsl int v = (int)(val * scale + 0.5);
415 1.26 dsl int factor = 1;
416 1.26 dsl int y, x;
417 1.26 dsl
418 1.26 dsl while (v > colwidth) {
419 1.26 dsl v = (v + 5) / 10;
420 1.26 dsl factor *= 10;
421 1.1 jtc }
422 1.26 dsl getyx(wnd, y, x);
423 1.1 jtc wclrtoeol(wnd);
424 1.26 dsl whline(wnd, 'X', v);
425 1.26 dsl if (factor != 1)
426 1.26 dsl mvwprintw(wnd, y, x + colwidth + 1, "* %d ", factor);
427 1.1 jtc }
428 1.1 jtc
429 1.13 jwise void
430 1.16 ad iostat_bars(char *args)
431 1.13 jwise {
432 1.13 jwise numbers = 0;
433 1.13 jwise wclear(wnd);
434 1.13 jwise labeliostat();
435 1.13 jwise refresh();
436 1.13 jwise }
437 1.13 jwise
438 1.13 jwise void
439 1.16 ad iostat_numbers(char *args)
440 1.1 jtc {
441 1.13 jwise numbers = 1;
442 1.13 jwise wclear(wnd);
443 1.13 jwise labeliostat();
444 1.13 jwise refresh();
445 1.13 jwise }
446 1.1 jtc
447 1.13 jwise void
448 1.16 ad iostat_secs(char *args)
449 1.13 jwise {
450 1.13 jwise secs = !secs;
451 1.23 mrg wclear(wnd);
452 1.23 mrg labeliostat();
453 1.23 mrg refresh();
454 1.23 mrg }
455 1.23 mrg
456 1.23 mrg void
457 1.23 mrg iostat_rw(char *args)
458 1.23 mrg {
459 1.31 dsl read_write ^= 1;
460 1.23 mrg wclear(wnd);
461 1.23 mrg labeliostat();
462 1.23 mrg refresh();
463 1.23 mrg }
464 1.23 mrg
465 1.23 mrg void
466 1.23 mrg iostat_all(char *args)
467 1.23 mrg {
468 1.23 mrg read_write = 0;
469 1.1 jtc wclear(wnd);
470 1.1 jtc labeliostat();
471 1.1 jtc refresh();
472 1.1 jtc }
473