iostat.c revision 1.21 1 1.21 sommerfe /* $NetBSD: iostat.c,v 1.21 2002/06/30 00:10:33 sommerfeld 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.21 sommerfe __RCSID("$NetBSD: iostat.c,v 1.21 2002/06/30 00:10:33 sommerfeld 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.1 jtc
57 1.16 ad static int barlabels(int);
58 1.16 ad static void histogram(double, int, double);
59 1.16 ad static int numlabels(int);
60 1.16 ad static int stats(int, int, int);
61 1.16 ad static void stat1(int, int);
62 1.1 jtc
63 1.1 jtc
64 1.1 jtc WINDOW *
65 1.16 ad openiostat(void)
66 1.1 jtc {
67 1.8 mrg
68 1.1 jtc return (subwin(stdscr, LINES-1-5, 0, 5, 0));
69 1.1 jtc }
70 1.1 jtc
71 1.1 jtc void
72 1.16 ad closeiostat(WINDOW *w)
73 1.1 jtc {
74 1.8 mrg
75 1.1 jtc if (w == NULL)
76 1.1 jtc return;
77 1.1 jtc wclear(w);
78 1.1 jtc wrefresh(w);
79 1.1 jtc delwin(w);
80 1.1 jtc }
81 1.1 jtc
82 1.1 jtc int
83 1.16 ad initiostat(void)
84 1.1 jtc {
85 1.8 mrg
86 1.19 augustss dkinit(1);
87 1.5 thorpej dkreadstats();
88 1.8 mrg return(1);
89 1.1 jtc }
90 1.1 jtc
91 1.1 jtc void
92 1.16 ad fetchiostat(void)
93 1.1 jtc {
94 1.8 mrg
95 1.5 thorpej if (dk_ndrive == 0)
96 1.1 jtc return;
97 1.5 thorpej dkreadstats();
98 1.1 jtc }
99 1.1 jtc
100 1.12 mjl #define INSET 14
101 1.1 jtc
102 1.1 jtc void
103 1.16 ad labeliostat(void)
104 1.1 jtc {
105 1.1 jtc int row;
106 1.1 jtc
107 1.5 thorpej if (dk_ndrive == 0) {
108 1.5 thorpej error("No drives defined.");
109 1.1 jtc return;
110 1.1 jtc }
111 1.1 jtc row = 0;
112 1.1 jtc wmove(wnd, row, 0); wclrtobot(wnd);
113 1.1 jtc mvwaddstr(wnd, row++, INSET,
114 1.1 jtc "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
115 1.12 mjl mvwaddstr(wnd, row++, 0, " cpu user|");
116 1.12 mjl mvwaddstr(wnd, row++, 0, " nice|");
117 1.12 mjl mvwaddstr(wnd, row++, 0, " system|");
118 1.12 mjl mvwaddstr(wnd, row++, 0, " interrupt|");
119 1.12 mjl mvwaddstr(wnd, row++, 0, " idle|");
120 1.1 jtc if (numbers)
121 1.1 jtc row = numlabels(row + 1);
122 1.1 jtc else
123 1.1 jtc row = barlabels(row + 1);
124 1.1 jtc }
125 1.1 jtc
126 1.1 jtc static int
127 1.16 ad numlabels(int row)
128 1.1 jtc {
129 1.1 jtc int i, col, regions, ndrives;
130 1.1 jtc
131 1.1 jtc #define COLWIDTH 14
132 1.20 itojun #define DRIVESPERLINE ((getmaxx(wnd) - 1) / COLWIDTH)
133 1.1 jtc for (ndrives = 0, i = 0; i < dk_ndrive; i++)
134 1.5 thorpej if (cur.dk_select[i])
135 1.1 jtc ndrives++;
136 1.1 jtc regions = howmany(ndrives, DRIVESPERLINE);
137 1.1 jtc /*
138 1.1 jtc * Deduct -regions for blank line after each scrolling region.
139 1.1 jtc */
140 1.7 jtc linesperregion = (getmaxy(wnd) - row - regions) / regions;
141 1.1 jtc /*
142 1.1 jtc * Minimum region contains space for two
143 1.1 jtc * label lines and one line of statistics.
144 1.1 jtc */
145 1.1 jtc if (linesperregion < 3)
146 1.1 jtc linesperregion = 3;
147 1.1 jtc col = 0;
148 1.1 jtc for (i = 0; i < dk_ndrive; i++)
149 1.5 thorpej if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) {
150 1.20 itojun if (col + COLWIDTH > getmaxx(wnd)) {
151 1.1 jtc col = 0, row += linesperregion + 1;
152 1.7 jtc if (row > getmaxy(wnd) - (linesperregion + 1))
153 1.1 jtc break;
154 1.1 jtc }
155 1.5 thorpej mvwaddstr(wnd, row, col + 4, cur.dk_name[i]);
156 1.6 scottr mvwaddstr(wnd, row + 1, col, "KBps tps sec");
157 1.1 jtc col += COLWIDTH;
158 1.1 jtc }
159 1.1 jtc if (col)
160 1.1 jtc row += linesperregion + 1;
161 1.1 jtc return (row);
162 1.1 jtc }
163 1.1 jtc
164 1.1 jtc static int
165 1.16 ad barlabels(int row)
166 1.1 jtc {
167 1.1 jtc int i;
168 1.1 jtc
169 1.1 jtc mvwaddstr(wnd, row++, INSET,
170 1.5 thorpej "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
171 1.5 thorpej linesperregion = 2 + secs;
172 1.1 jtc for (i = 0; i < dk_ndrive; i++)
173 1.5 thorpej if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) {
174 1.7 jtc if (row > getmaxy(wnd) - linesperregion)
175 1.1 jtc break;
176 1.12 mjl mvwprintw(wnd, row++, 0, "%7.7s KBps|", cur.dk_name[i]);
177 1.12 mjl mvwaddstr(wnd, row++, 0, " tps|");
178 1.5 thorpej if (secs)
179 1.12 mjl mvwaddstr(wnd, row++, 0, " msec|");
180 1.1 jtc }
181 1.1 jtc return (row);
182 1.1 jtc }
183 1.1 jtc
184 1.1 jtc void
185 1.16 ad showiostat(void)
186 1.1 jtc {
187 1.9 lukem int i, row, col;
188 1.1 jtc
189 1.5 thorpej if (dk_ndrive == 0)
190 1.1 jtc return;
191 1.5 thorpej dkswap();
192 1.5 thorpej
193 1.21 sommerfe etime = cur.cp_etime;
194 1.1 jtc row = 1;
195 1.1 jtc
196 1.1 jtc /*
197 1.3 mycroft * Interrupt CPU state not calculated yet.
198 1.21 sommerfe */
199 1.3 mycroft for (i = 0; i < CPUSTATES; i++)
200 1.1 jtc stat1(row++, i);
201 1.1 jtc if (!numbers) {
202 1.1 jtc row += 2;
203 1.1 jtc for (i = 0; i < dk_ndrive; i++)
204 1.5 thorpej if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) {
205 1.7 jtc if (row > getmaxy(wnd) - linesperregion)
206 1.1 jtc break;
207 1.1 jtc row = stats(row, INSET, i);
208 1.1 jtc }
209 1.1 jtc return;
210 1.1 jtc }
211 1.1 jtc col = 0;
212 1.1 jtc wmove(wnd, row + linesperregion, 0);
213 1.1 jtc wdeleteln(wnd);
214 1.1 jtc wmove(wnd, row + 3, 0);
215 1.1 jtc winsertln(wnd);
216 1.1 jtc for (i = 0; i < dk_ndrive; i++)
217 1.5 thorpej if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) {
218 1.20 itojun if (col + COLWIDTH > getmaxx(wnd)) {
219 1.1 jtc col = 0, row += linesperregion + 1;
220 1.7 jtc if (row > getmaxy(wnd) - (linesperregion + 1))
221 1.1 jtc break;
222 1.1 jtc wmove(wnd, row + linesperregion, 0);
223 1.1 jtc wdeleteln(wnd);
224 1.1 jtc wmove(wnd, row + 3, 0);
225 1.1 jtc winsertln(wnd);
226 1.1 jtc }
227 1.1 jtc (void) stats(row + 3, col, i);
228 1.1 jtc col += COLWIDTH;
229 1.1 jtc }
230 1.1 jtc }
231 1.1 jtc
232 1.1 jtc static int
233 1.16 ad stats(int row, int col, int dn)
234 1.1 jtc {
235 1.5 thorpej double atime, words;
236 1.5 thorpej
237 1.5 thorpej /* time busy in disk activity */
238 1.5 thorpej atime = (double)cur.dk_time[dn].tv_sec +
239 1.5 thorpej ((double)cur.dk_time[dn].tv_usec / (double)1000000);
240 1.1 jtc
241 1.5 thorpej words = cur.dk_bytes[dn] / 1024.0; /* # of K transferred */
242 1.1 jtc if (numbers) {
243 1.6 scottr mvwprintw(wnd, row, col, " %3.0f%4.0f%5.1f",
244 1.5 thorpej words / etime, cur.dk_xfer[dn] / etime, atime / etime);
245 1.1 jtc return (row);
246 1.1 jtc }
247 1.1 jtc wmove(wnd, row++, col);
248 1.5 thorpej histogram(words / etime, 50, 0.5);
249 1.1 jtc wmove(wnd, row++, col);
250 1.5 thorpej histogram(cur.dk_xfer[dn] / etime, 50, 0.5);
251 1.5 thorpej if (secs) {
252 1.1 jtc wmove(wnd, row++, col);
253 1.5 thorpej atime *= 1000; /* In milliseconds */
254 1.5 thorpej histogram(atime / etime, 50, 0.5);
255 1.1 jtc }
256 1.1 jtc return (row);
257 1.1 jtc }
258 1.1 jtc
259 1.1 jtc static void
260 1.16 ad stat1(int row, int o)
261 1.1 jtc {
262 1.9 lukem int i;
263 1.1 jtc double time;
264 1.1 jtc
265 1.1 jtc time = 0;
266 1.1 jtc for (i = 0; i < CPUSTATES; i++)
267 1.5 thorpej time += cur.cp_time[i];
268 1.1 jtc if (time == 0.0)
269 1.1 jtc time = 1.0;
270 1.1 jtc wmove(wnd, row, INSET);
271 1.1 jtc #define CPUSCALE 0.5
272 1.5 thorpej histogram(100.0 * cur.cp_time[o] / time, 50, CPUSCALE);
273 1.1 jtc }
274 1.1 jtc
275 1.1 jtc static void
276 1.16 ad histogram(double val, int colwidth, double scale)
277 1.1 jtc {
278 1.1 jtc char buf[10];
279 1.9 lukem int k;
280 1.9 lukem int v = (int)(val * scale) + 0.5;
281 1.1 jtc
282 1.1 jtc k = MIN(v, colwidth);
283 1.1 jtc if (v > colwidth) {
284 1.10 mrg snprintf(buf, sizeof buf, "%4.1f", val);
285 1.1 jtc k -= strlen(buf);
286 1.1 jtc while (k--)
287 1.1 jtc waddch(wnd, 'X');
288 1.1 jtc waddstr(wnd, buf);
289 1.5 thorpej wclrtoeol(wnd);
290 1.1 jtc return;
291 1.1 jtc }
292 1.1 jtc wclrtoeol(wnd);
293 1.15 mycroft whline(wnd, 'X', k);
294 1.1 jtc }
295 1.1 jtc
296 1.13 jwise void
297 1.16 ad iostat_bars(char *args)
298 1.13 jwise {
299 1.13 jwise numbers = 0;
300 1.13 jwise wclear(wnd);
301 1.13 jwise labeliostat();
302 1.13 jwise refresh();
303 1.13 jwise }
304 1.13 jwise
305 1.13 jwise void
306 1.16 ad iostat_numbers(char *args)
307 1.1 jtc {
308 1.13 jwise numbers = 1;
309 1.13 jwise wclear(wnd);
310 1.13 jwise labeliostat();
311 1.13 jwise refresh();
312 1.13 jwise }
313 1.1 jtc
314 1.13 jwise void
315 1.16 ad iostat_secs(char *args)
316 1.13 jwise {
317 1.13 jwise secs = !secs;
318 1.1 jtc wclear(wnd);
319 1.1 jtc labeliostat();
320 1.1 jtc refresh();
321 1.1 jtc }
322