iostat.c revision 1.24 1 1.24 thorpej /* $NetBSD: iostat.c,v 1.24 2002/12/06 03:13:14 thorpej 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.8 mrg #include <sys/cdefs.h>
37 1.1 jtc #ifndef lint
38 1.2 jtc #if 0
39 1.1 jtc static char sccsid[] = "@(#)iostat.c 8.1 (Berkeley) 6/6/93";
40 1.2 jtc #endif
41 1.24 thorpej __RCSID("$NetBSD: iostat.c,v 1.24 2002/12/06 03:13:14 thorpej Exp $");
42 1.18 cgd #endif /* not lint */
43 1.1 jtc
44 1.1 jtc #include <sys/param.h>
45 1.1 jtc
46 1.1 jtc #include <string.h>
47 1.17 simonb
48 1.1 jtc #include "systat.h"
49 1.1 jtc #include "extern.h"
50 1.5 thorpej #include "dkstats.h"
51 1.1 jtc
52 1.1 jtc static int linesperregion;
53 1.1 jtc static double etime;
54 1.1 jtc static int numbers = 0; /* default display bar graphs */
55 1.5 thorpej static int secs = 0; /* default seconds shown */
56 1.23 mrg static int read_write = 0; /* default read/write shown */
57 1.1 jtc
58 1.16 ad static int barlabels(int);
59 1.16 ad static void histogram(double, int, double);
60 1.16 ad static int numlabels(int);
61 1.16 ad static int stats(int, int, int);
62 1.16 ad static void stat1(int, int);
63 1.1 jtc
64 1.1 jtc
65 1.1 jtc WINDOW *
66 1.16 ad openiostat(void)
67 1.1 jtc {
68 1.8 mrg
69 1.1 jtc return (subwin(stdscr, LINES-1-5, 0, 5, 0));
70 1.1 jtc }
71 1.1 jtc
72 1.1 jtc void
73 1.16 ad closeiostat(WINDOW *w)
74 1.1 jtc {
75 1.8 mrg
76 1.1 jtc if (w == NULL)
77 1.1 jtc return;
78 1.1 jtc wclear(w);
79 1.1 jtc wrefresh(w);
80 1.1 jtc delwin(w);
81 1.1 jtc }
82 1.1 jtc
83 1.1 jtc int
84 1.16 ad initiostat(void)
85 1.1 jtc {
86 1.8 mrg
87 1.19 augustss dkinit(1);
88 1.5 thorpej dkreadstats();
89 1.8 mrg return(1);
90 1.1 jtc }
91 1.1 jtc
92 1.1 jtc void
93 1.16 ad fetchiostat(void)
94 1.1 jtc {
95 1.8 mrg
96 1.5 thorpej if (dk_ndrive == 0)
97 1.1 jtc return;
98 1.5 thorpej dkreadstats();
99 1.1 jtc }
100 1.1 jtc
101 1.12 mjl #define INSET 14
102 1.1 jtc
103 1.1 jtc void
104 1.16 ad labeliostat(void)
105 1.1 jtc {
106 1.1 jtc int row;
107 1.1 jtc
108 1.5 thorpej if (dk_ndrive == 0) {
109 1.5 thorpej error("No drives defined.");
110 1.1 jtc return;
111 1.1 jtc }
112 1.1 jtc row = 0;
113 1.1 jtc wmove(wnd, row, 0); wclrtobot(wnd);
114 1.1 jtc mvwaddstr(wnd, row++, INSET,
115 1.1 jtc "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
116 1.12 mjl mvwaddstr(wnd, row++, 0, " cpu user|");
117 1.12 mjl mvwaddstr(wnd, row++, 0, " nice|");
118 1.12 mjl mvwaddstr(wnd, row++, 0, " system|");
119 1.12 mjl mvwaddstr(wnd, row++, 0, " interrupt|");
120 1.12 mjl mvwaddstr(wnd, row++, 0, " idle|");
121 1.1 jtc if (numbers)
122 1.1 jtc row = numlabels(row + 1);
123 1.1 jtc else
124 1.1 jtc row = barlabels(row + 1);
125 1.1 jtc }
126 1.1 jtc
127 1.1 jtc static int
128 1.16 ad numlabels(int row)
129 1.1 jtc {
130 1.1 jtc int i, col, regions, ndrives;
131 1.1 jtc
132 1.23 mrg #define COLWIDTH (read_write ? 24 : 14)
133 1.20 itojun #define DRIVESPERLINE ((getmaxx(wnd) - 1) / COLWIDTH)
134 1.1 jtc for (ndrives = 0, i = 0; i < dk_ndrive; i++)
135 1.5 thorpej if (cur.dk_select[i])
136 1.1 jtc ndrives++;
137 1.1 jtc regions = howmany(ndrives, DRIVESPERLINE);
138 1.1 jtc /*
139 1.1 jtc * Deduct -regions for blank line after each scrolling region.
140 1.1 jtc */
141 1.7 jtc linesperregion = (getmaxy(wnd) - row - regions) / regions;
142 1.1 jtc /*
143 1.1 jtc * Minimum region contains space for two
144 1.1 jtc * label lines and one line of statistics.
145 1.1 jtc */
146 1.1 jtc if (linesperregion < 3)
147 1.1 jtc linesperregion = 3;
148 1.1 jtc col = 0;
149 1.1 jtc for (i = 0; i < dk_ndrive; i++)
150 1.23 mrg if (cur.dk_select[i]) {
151 1.20 itojun if (col + COLWIDTH > getmaxx(wnd)) {
152 1.1 jtc col = 0, row += linesperregion + 1;
153 1.7 jtc if (row > getmaxy(wnd) - (linesperregion + 1))
154 1.1 jtc break;
155 1.1 jtc }
156 1.23 mrg mvwprintw(wnd, row, col + 4, "%s%s",
157 1.23 mrg cur.dk_name[i], read_write ? " (write)" : "");
158 1.23 mrg if (read_write)
159 1.23 mrg mvwaddstr(wnd, row + 1, col,
160 1.23 mrg "kBps r/s sec kBps w/s");
161 1.23 mrg else
162 1.23 mrg mvwaddstr(wnd, row + 1, col, "kBps tps sec");
163 1.1 jtc col += COLWIDTH;
164 1.1 jtc }
165 1.1 jtc if (col)
166 1.1 jtc row += linesperregion + 1;
167 1.1 jtc return (row);
168 1.1 jtc }
169 1.1 jtc
170 1.1 jtc static int
171 1.16 ad barlabels(int row)
172 1.1 jtc {
173 1.1 jtc int i;
174 1.1 jtc
175 1.1 jtc mvwaddstr(wnd, row++, INSET,
176 1.5 thorpej "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
177 1.23 mrg linesperregion = 2 + secs + (read_write ? 2 : 0);
178 1.1 jtc for (i = 0; i < dk_ndrive; i++)
179 1.23 mrg if (cur.dk_select[i]) {
180 1.7 jtc if (row > getmaxy(wnd) - linesperregion)
181 1.1 jtc break;
182 1.24 thorpej mvwprintw(wnd, row++, 0, "%7.7s kBps|",
183 1.24 thorpej cur.dk_name[i]);
184 1.12 mjl mvwaddstr(wnd, row++, 0, " tps|");
185 1.23 mrg if (read_write) {
186 1.23 mrg mvwprintw(wnd, row++, 0, " (write) kBps|");
187 1.23 mrg mvwaddstr(wnd, row++, 0, " tps|");
188 1.23 mrg }
189 1.5 thorpej if (secs)
190 1.12 mjl mvwaddstr(wnd, row++, 0, " msec|");
191 1.1 jtc }
192 1.1 jtc return (row);
193 1.1 jtc }
194 1.1 jtc
195 1.1 jtc void
196 1.16 ad showiostat(void)
197 1.1 jtc {
198 1.9 lukem int i, row, col;
199 1.1 jtc
200 1.5 thorpej if (dk_ndrive == 0)
201 1.1 jtc return;
202 1.5 thorpej dkswap();
203 1.5 thorpej
204 1.21 sommerfe etime = cur.cp_etime;
205 1.1 jtc row = 1;
206 1.1 jtc
207 1.1 jtc /*
208 1.3 mycroft * Interrupt CPU state not calculated yet.
209 1.21 sommerfe */
210 1.3 mycroft for (i = 0; i < CPUSTATES; i++)
211 1.1 jtc stat1(row++, i);
212 1.1 jtc if (!numbers) {
213 1.1 jtc row += 2;
214 1.1 jtc for (i = 0; i < dk_ndrive; i++)
215 1.23 mrg if (cur.dk_select[i]) {
216 1.7 jtc if (row > getmaxy(wnd) - linesperregion)
217 1.1 jtc break;
218 1.1 jtc row = stats(row, INSET, i);
219 1.1 jtc }
220 1.1 jtc return;
221 1.1 jtc }
222 1.1 jtc col = 0;
223 1.1 jtc wmove(wnd, row + linesperregion, 0);
224 1.1 jtc wdeleteln(wnd);
225 1.1 jtc wmove(wnd, row + 3, 0);
226 1.1 jtc winsertln(wnd);
227 1.1 jtc for (i = 0; i < dk_ndrive; i++)
228 1.23 mrg if (cur.dk_select[i]) {
229 1.20 itojun if (col + COLWIDTH > getmaxx(wnd)) {
230 1.1 jtc col = 0, row += linesperregion + 1;
231 1.7 jtc if (row > getmaxy(wnd) - (linesperregion + 1))
232 1.1 jtc break;
233 1.1 jtc wmove(wnd, row + linesperregion, 0);
234 1.1 jtc wdeleteln(wnd);
235 1.1 jtc wmove(wnd, row + 3, 0);
236 1.1 jtc winsertln(wnd);
237 1.1 jtc }
238 1.1 jtc (void) stats(row + 3, col, i);
239 1.1 jtc col += COLWIDTH;
240 1.1 jtc }
241 1.1 jtc }
242 1.1 jtc
243 1.1 jtc static int
244 1.16 ad stats(int row, int col, int dn)
245 1.1 jtc {
246 1.23 mrg double atime, rwords, wwords;
247 1.5 thorpej
248 1.5 thorpej /* time busy in disk activity */
249 1.5 thorpej atime = (double)cur.dk_time[dn].tv_sec +
250 1.5 thorpej ((double)cur.dk_time[dn].tv_usec / (double)1000000);
251 1.1 jtc
252 1.23 mrg /* # of k transferred */
253 1.23 mrg rwords = cur.dk_rbytes[dn] / 1024.0;
254 1.23 mrg wwords = cur.dk_wbytes[dn] / 1024.0;
255 1.1 jtc if (numbers) {
256 1.23 mrg if (read_write)
257 1.23 mrg mvwprintw(wnd, row, col, " %3.0f%4.0f%5.1f %3.0f%4.0f",
258 1.23 mrg rwords / etime, cur.dk_rxfer[dn] / etime,
259 1.23 mrg atime / etime,
260 1.23 mrg wwords / etime, cur.dk_wxfer[dn] / etime);
261 1.23 mrg else
262 1.23 mrg mvwprintw(wnd, row, col, " %3.0f%4.0f%5.1f",
263 1.23 mrg (rwords + wwords) / etime,
264 1.23 mrg (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]) / etime,
265 1.23 mrg atime / etime);
266 1.1 jtc return (row);
267 1.1 jtc }
268 1.23 mrg
269 1.23 mrg if (read_write) {
270 1.23 mrg wmove(wnd, row++, col);
271 1.23 mrg histogram(rwords / etime, 50, 0.5);
272 1.23 mrg wmove(wnd, row++, col);
273 1.23 mrg histogram(cur.dk_rxfer[dn] / etime, 50, 0.5);
274 1.23 mrg wmove(wnd, row++, col);
275 1.23 mrg histogram(wwords / etime, 50, 0.5);
276 1.23 mrg wmove(wnd, row++, col);
277 1.23 mrg histogram(cur.dk_wxfer[dn] / etime, 50, 0.5);
278 1.23 mrg } else {
279 1.23 mrg wmove(wnd, row++, col);
280 1.23 mrg histogram((rwords + wwords) / etime, 50, 0.5);
281 1.23 mrg wmove(wnd, row++, col);
282 1.23 mrg histogram((cur.dk_rxfer[dn] + cur.dk_wxfer[dn]) / etime, 50, 0.5);
283 1.23 mrg }
284 1.23 mrg
285 1.5 thorpej if (secs) {
286 1.1 jtc wmove(wnd, row++, col);
287 1.5 thorpej atime *= 1000; /* In milliseconds */
288 1.5 thorpej histogram(atime / etime, 50, 0.5);
289 1.1 jtc }
290 1.1 jtc return (row);
291 1.1 jtc }
292 1.1 jtc
293 1.1 jtc static void
294 1.16 ad stat1(int row, int o)
295 1.1 jtc {
296 1.9 lukem int i;
297 1.1 jtc double time;
298 1.1 jtc
299 1.1 jtc time = 0;
300 1.1 jtc for (i = 0; i < CPUSTATES; i++)
301 1.5 thorpej time += cur.cp_time[i];
302 1.1 jtc if (time == 0.0)
303 1.1 jtc time = 1.0;
304 1.1 jtc wmove(wnd, row, INSET);
305 1.1 jtc #define CPUSCALE 0.5
306 1.5 thorpej histogram(100.0 * cur.cp_time[o] / time, 50, CPUSCALE);
307 1.1 jtc }
308 1.1 jtc
309 1.1 jtc static void
310 1.16 ad histogram(double val, int colwidth, double scale)
311 1.1 jtc {
312 1.1 jtc char buf[10];
313 1.9 lukem int k;
314 1.9 lukem int v = (int)(val * scale) + 0.5;
315 1.1 jtc
316 1.1 jtc k = MIN(v, colwidth);
317 1.1 jtc if (v > colwidth) {
318 1.10 mrg snprintf(buf, sizeof buf, "%4.1f", val);
319 1.1 jtc k -= strlen(buf);
320 1.1 jtc while (k--)
321 1.1 jtc waddch(wnd, 'X');
322 1.1 jtc waddstr(wnd, buf);
323 1.5 thorpej wclrtoeol(wnd);
324 1.1 jtc return;
325 1.1 jtc }
326 1.1 jtc wclrtoeol(wnd);
327 1.15 mycroft whline(wnd, 'X', k);
328 1.1 jtc }
329 1.1 jtc
330 1.13 jwise void
331 1.16 ad iostat_bars(char *args)
332 1.13 jwise {
333 1.13 jwise numbers = 0;
334 1.13 jwise wclear(wnd);
335 1.13 jwise labeliostat();
336 1.13 jwise refresh();
337 1.13 jwise }
338 1.13 jwise
339 1.13 jwise void
340 1.16 ad iostat_numbers(char *args)
341 1.1 jtc {
342 1.13 jwise numbers = 1;
343 1.13 jwise wclear(wnd);
344 1.13 jwise labeliostat();
345 1.13 jwise refresh();
346 1.13 jwise }
347 1.1 jtc
348 1.13 jwise void
349 1.16 ad iostat_secs(char *args)
350 1.13 jwise {
351 1.13 jwise secs = !secs;
352 1.23 mrg wclear(wnd);
353 1.23 mrg labeliostat();
354 1.23 mrg refresh();
355 1.23 mrg }
356 1.23 mrg
357 1.23 mrg void
358 1.23 mrg iostat_rw(char *args)
359 1.23 mrg {
360 1.23 mrg read_write = 1;
361 1.23 mrg wclear(wnd);
362 1.23 mrg labeliostat();
363 1.23 mrg refresh();
364 1.23 mrg }
365 1.23 mrg
366 1.23 mrg void
367 1.23 mrg iostat_all(char *args)
368 1.23 mrg {
369 1.23 mrg read_write = 0;
370 1.1 jtc wclear(wnd);
371 1.1 jtc labeliostat();
372 1.1 jtc refresh();
373 1.1 jtc }
374