iostat.c revision 1.13 1 1.13 jwise /* $NetBSD: iostat.c,v 1.13 1999/12/20 03:45:02 jwise 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.13 jwise __RCSID("$NetBSD: iostat.c,v 1.13 1999/12/20 03:45:02 jwise Exp $");
42 1.1 jtc #endif not lint
43 1.1 jtc
44 1.1 jtc #include <sys/param.h>
45 1.1 jtc #include <sys/dkstat.h>
46 1.1 jtc #include <sys/buf.h>
47 1.5 thorpej #include <sys/time.h>
48 1.1 jtc
49 1.1 jtc #include <string.h>
50 1.1 jtc #include <stdlib.h>
51 1.1 jtc #include <nlist.h>
52 1.1 jtc #include <paths.h>
53 1.1 jtc #include "systat.h"
54 1.1 jtc #include "extern.h"
55 1.1 jtc
56 1.5 thorpej #include "dkstats.h"
57 1.5 thorpej extern struct _disk cur;
58 1.1 jtc
59 1.1 jtc static int linesperregion;
60 1.1 jtc static double etime;
61 1.1 jtc static int numbers = 0; /* default display bar graphs */
62 1.5 thorpej static int secs = 0; /* default seconds shown */
63 1.1 jtc
64 1.1 jtc static int barlabels __P((int));
65 1.1 jtc static void histogram __P((double, int, double));
66 1.1 jtc static int numlabels __P((int));
67 1.1 jtc static int stats __P((int, int, int));
68 1.1 jtc static void stat1 __P((int, int));
69 1.1 jtc
70 1.1 jtc
71 1.1 jtc WINDOW *
72 1.1 jtc openiostat()
73 1.1 jtc {
74 1.8 mrg
75 1.1 jtc return (subwin(stdscr, LINES-1-5, 0, 5, 0));
76 1.1 jtc }
77 1.1 jtc
78 1.1 jtc void
79 1.1 jtc closeiostat(w)
80 1.1 jtc WINDOW *w;
81 1.1 jtc {
82 1.8 mrg
83 1.1 jtc if (w == NULL)
84 1.1 jtc return;
85 1.1 jtc wclear(w);
86 1.1 jtc wrefresh(w);
87 1.1 jtc delwin(w);
88 1.1 jtc }
89 1.1 jtc
90 1.1 jtc int
91 1.1 jtc initiostat()
92 1.1 jtc {
93 1.11 drochner extern gid_t egid;
94 1.8 mrg
95 1.11 drochner dkinit(1, egid);
96 1.5 thorpej dkreadstats();
97 1.8 mrg return(1);
98 1.1 jtc }
99 1.1 jtc
100 1.1 jtc void
101 1.1 jtc fetchiostat()
102 1.1 jtc {
103 1.8 mrg
104 1.5 thorpej if (dk_ndrive == 0)
105 1.1 jtc return;
106 1.5 thorpej dkreadstats();
107 1.1 jtc }
108 1.1 jtc
109 1.12 mjl #define INSET 14
110 1.1 jtc
111 1.1 jtc void
112 1.1 jtc labeliostat()
113 1.1 jtc {
114 1.1 jtc int row;
115 1.1 jtc
116 1.5 thorpej if (dk_ndrive == 0) {
117 1.5 thorpej error("No drives defined.");
118 1.1 jtc return;
119 1.1 jtc }
120 1.1 jtc row = 0;
121 1.1 jtc wmove(wnd, row, 0); wclrtobot(wnd);
122 1.1 jtc mvwaddstr(wnd, row++, INSET,
123 1.1 jtc "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
124 1.12 mjl mvwaddstr(wnd, row++, 0, " cpu user|");
125 1.12 mjl mvwaddstr(wnd, row++, 0, " nice|");
126 1.12 mjl mvwaddstr(wnd, row++, 0, " system|");
127 1.12 mjl mvwaddstr(wnd, row++, 0, " interrupt|");
128 1.12 mjl mvwaddstr(wnd, row++, 0, " idle|");
129 1.1 jtc if (numbers)
130 1.1 jtc row = numlabels(row + 1);
131 1.1 jtc else
132 1.1 jtc row = barlabels(row + 1);
133 1.1 jtc }
134 1.1 jtc
135 1.1 jtc static int
136 1.1 jtc numlabels(row)
137 1.1 jtc int row;
138 1.1 jtc {
139 1.1 jtc int i, col, regions, ndrives;
140 1.1 jtc
141 1.1 jtc #define COLWIDTH 14
142 1.7 jtc #define DRIVESPERLINE ((getmaxx(wnd) - INSET) / COLWIDTH)
143 1.1 jtc for (ndrives = 0, i = 0; i < dk_ndrive; i++)
144 1.5 thorpej if (cur.dk_select[i])
145 1.1 jtc ndrives++;
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.7 jtc linesperregion = (getmaxy(wnd) - row - regions) / 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.1 jtc for (i = 0; i < dk_ndrive; i++)
159 1.5 thorpej if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) {
160 1.7 jtc if (col + COLWIDTH >= getmaxx(wnd) - INSET) {
161 1.1 jtc col = 0, row += linesperregion + 1;
162 1.7 jtc if (row > getmaxy(wnd) - (linesperregion + 1))
163 1.1 jtc break;
164 1.1 jtc }
165 1.5 thorpej mvwaddstr(wnd, row, col + 4, cur.dk_name[i]);
166 1.6 scottr mvwaddstr(wnd, row + 1, col, "KBps tps sec");
167 1.1 jtc col += COLWIDTH;
168 1.1 jtc }
169 1.1 jtc if (col)
170 1.1 jtc row += linesperregion + 1;
171 1.1 jtc return (row);
172 1.1 jtc }
173 1.1 jtc
174 1.1 jtc static int
175 1.1 jtc barlabels(row)
176 1.1 jtc int row;
177 1.1 jtc {
178 1.1 jtc int i;
179 1.1 jtc
180 1.1 jtc mvwaddstr(wnd, row++, INSET,
181 1.5 thorpej "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
182 1.5 thorpej linesperregion = 2 + secs;
183 1.1 jtc for (i = 0; i < dk_ndrive; i++)
184 1.5 thorpej if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) {
185 1.7 jtc if (row > getmaxy(wnd) - linesperregion)
186 1.1 jtc break;
187 1.12 mjl mvwprintw(wnd, row++, 0, "%7.7s KBps|", cur.dk_name[i]);
188 1.12 mjl mvwaddstr(wnd, row++, 0, " tps|");
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
196 1.1 jtc void
197 1.1 jtc showiostat()
198 1.1 jtc {
199 1.9 lukem int i, row, col;
200 1.1 jtc
201 1.5 thorpej if (dk_ndrive == 0)
202 1.1 jtc return;
203 1.5 thorpej dkswap();
204 1.5 thorpej
205 1.1 jtc etime = 0;
206 1.1 jtc for(i = 0; i < CPUSTATES; i++) {
207 1.5 thorpej etime += cur.cp_time[i];
208 1.1 jtc }
209 1.1 jtc if (etime == 0.0)
210 1.1 jtc etime = 1.0;
211 1.1 jtc etime /= (float) hz;
212 1.1 jtc row = 1;
213 1.1 jtc
214 1.1 jtc /*
215 1.3 mycroft * Interrupt CPU state not calculated yet.
216 1.1 jtc */
217 1.3 mycroft for (i = 0; i < CPUSTATES; i++)
218 1.1 jtc stat1(row++, i);
219 1.1 jtc if (!numbers) {
220 1.1 jtc row += 2;
221 1.1 jtc for (i = 0; i < dk_ndrive; i++)
222 1.5 thorpej if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) {
223 1.7 jtc if (row > getmaxy(wnd) - linesperregion)
224 1.1 jtc break;
225 1.1 jtc row = stats(row, INSET, i);
226 1.1 jtc }
227 1.1 jtc return;
228 1.1 jtc }
229 1.1 jtc col = 0;
230 1.1 jtc wmove(wnd, row + linesperregion, 0);
231 1.1 jtc wdeleteln(wnd);
232 1.1 jtc wmove(wnd, row + 3, 0);
233 1.1 jtc winsertln(wnd);
234 1.1 jtc for (i = 0; i < dk_ndrive; i++)
235 1.5 thorpej if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) {
236 1.7 jtc if (col + COLWIDTH >= getmaxx(wnd)) {
237 1.1 jtc col = 0, row += linesperregion + 1;
238 1.7 jtc if (row > getmaxy(wnd) - (linesperregion + 1))
239 1.1 jtc break;
240 1.1 jtc wmove(wnd, row + linesperregion, 0);
241 1.1 jtc wdeleteln(wnd);
242 1.1 jtc wmove(wnd, row + 3, 0);
243 1.1 jtc winsertln(wnd);
244 1.1 jtc }
245 1.1 jtc (void) stats(row + 3, col, i);
246 1.1 jtc col += COLWIDTH;
247 1.1 jtc }
248 1.1 jtc }
249 1.1 jtc
250 1.1 jtc static int
251 1.1 jtc stats(row, col, dn)
252 1.1 jtc int row, col, dn;
253 1.1 jtc {
254 1.5 thorpej double atime, words;
255 1.5 thorpej
256 1.5 thorpej /* time busy in disk activity */
257 1.5 thorpej atime = (double)cur.dk_time[dn].tv_sec +
258 1.5 thorpej ((double)cur.dk_time[dn].tv_usec / (double)1000000);
259 1.1 jtc
260 1.5 thorpej words = cur.dk_bytes[dn] / 1024.0; /* # of K transferred */
261 1.1 jtc if (numbers) {
262 1.6 scottr mvwprintw(wnd, row, col, " %3.0f%4.0f%5.1f",
263 1.5 thorpej words / etime, cur.dk_xfer[dn] / etime, atime / etime);
264 1.1 jtc return (row);
265 1.1 jtc }
266 1.1 jtc wmove(wnd, row++, col);
267 1.5 thorpej histogram(words / etime, 50, 0.5);
268 1.1 jtc wmove(wnd, row++, col);
269 1.5 thorpej histogram(cur.dk_xfer[dn] / etime, 50, 0.5);
270 1.5 thorpej if (secs) {
271 1.1 jtc wmove(wnd, row++, col);
272 1.5 thorpej atime *= 1000; /* In milliseconds */
273 1.5 thorpej histogram(atime / etime, 50, 0.5);
274 1.1 jtc }
275 1.1 jtc return (row);
276 1.1 jtc }
277 1.1 jtc
278 1.1 jtc static void
279 1.1 jtc stat1(row, o)
280 1.1 jtc int row, o;
281 1.1 jtc {
282 1.9 lukem int i;
283 1.1 jtc double time;
284 1.1 jtc
285 1.1 jtc time = 0;
286 1.1 jtc for (i = 0; i < CPUSTATES; i++)
287 1.5 thorpej time += cur.cp_time[i];
288 1.1 jtc if (time == 0.0)
289 1.1 jtc time = 1.0;
290 1.1 jtc wmove(wnd, row, INSET);
291 1.1 jtc #define CPUSCALE 0.5
292 1.5 thorpej histogram(100.0 * cur.cp_time[o] / time, 50, CPUSCALE);
293 1.1 jtc }
294 1.1 jtc
295 1.1 jtc static void
296 1.1 jtc histogram(val, colwidth, scale)
297 1.1 jtc double val;
298 1.1 jtc int colwidth;
299 1.1 jtc double scale;
300 1.1 jtc {
301 1.1 jtc char buf[10];
302 1.9 lukem int k;
303 1.9 lukem int v = (int)(val * scale) + 0.5;
304 1.1 jtc
305 1.1 jtc k = MIN(v, colwidth);
306 1.1 jtc if (v > colwidth) {
307 1.10 mrg snprintf(buf, sizeof buf, "%4.1f", val);
308 1.1 jtc k -= strlen(buf);
309 1.1 jtc while (k--)
310 1.1 jtc waddch(wnd, 'X');
311 1.1 jtc waddstr(wnd, buf);
312 1.5 thorpej wclrtoeol(wnd);
313 1.1 jtc return;
314 1.1 jtc }
315 1.1 jtc while (k--)
316 1.1 jtc waddch(wnd, 'X');
317 1.1 jtc wclrtoeol(wnd);
318 1.1 jtc }
319 1.1 jtc
320 1.13 jwise void
321 1.13 jwise iostat_bars (args)
322 1.13 jwise char *args;
323 1.13 jwise {
324 1.13 jwise numbers = 0;
325 1.13 jwise wclear(wnd);
326 1.13 jwise labeliostat();
327 1.13 jwise refresh();
328 1.13 jwise }
329 1.13 jwise
330 1.13 jwise void
331 1.13 jwise iostat_numbers (args)
332 1.13 jwise char *args;
333 1.1 jtc {
334 1.13 jwise numbers = 1;
335 1.13 jwise wclear(wnd);
336 1.13 jwise labeliostat();
337 1.13 jwise refresh();
338 1.13 jwise }
339 1.1 jtc
340 1.13 jwise void
341 1.13 jwise iostat_secs (args)
342 1.13 jwise char *args;
343 1.13 jwise {
344 1.13 jwise secs = !secs;
345 1.1 jtc wclear(wnd);
346 1.1 jtc labeliostat();
347 1.1 jtc refresh();
348 1.1 jtc }
349