iostat.c revision 1.17 1 1.17 simonb /* $NetBSD: iostat.c,v 1.17 2000/12/01 02:19:43 simonb 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.17 simonb __RCSID("$NetBSD: iostat.c,v 1.17 2000/12/01 02:19:43 simonb Exp $");
42 1.1 jtc #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.11 drochner dkinit(1, egid);
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.7 jtc #define DRIVESPERLINE ((getmaxx(wnd) - INSET) / 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.7 jtc if (col + COLWIDTH >= getmaxx(wnd) - INSET) {
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.1 jtc etime = 0;
194 1.1 jtc for(i = 0; i < CPUSTATES; i++) {
195 1.5 thorpej etime += cur.cp_time[i];
196 1.1 jtc }
197 1.1 jtc if (etime == 0.0)
198 1.1 jtc etime = 1.0;
199 1.1 jtc etime /= (float) hz;
200 1.1 jtc row = 1;
201 1.1 jtc
202 1.1 jtc /*
203 1.3 mycroft * Interrupt CPU state not calculated yet.
204 1.1 jtc */
205 1.3 mycroft for (i = 0; i < CPUSTATES; i++)
206 1.1 jtc stat1(row++, i);
207 1.1 jtc if (!numbers) {
208 1.1 jtc row += 2;
209 1.1 jtc for (i = 0; i < dk_ndrive; i++)
210 1.5 thorpej if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) {
211 1.7 jtc if (row > getmaxy(wnd) - linesperregion)
212 1.1 jtc break;
213 1.1 jtc row = stats(row, INSET, i);
214 1.1 jtc }
215 1.1 jtc return;
216 1.1 jtc }
217 1.1 jtc col = 0;
218 1.1 jtc wmove(wnd, row + linesperregion, 0);
219 1.1 jtc wdeleteln(wnd);
220 1.1 jtc wmove(wnd, row + 3, 0);
221 1.1 jtc winsertln(wnd);
222 1.1 jtc for (i = 0; i < dk_ndrive; i++)
223 1.5 thorpej if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) {
224 1.7 jtc if (col + COLWIDTH >= getmaxx(wnd)) {
225 1.1 jtc col = 0, row += linesperregion + 1;
226 1.7 jtc if (row > getmaxy(wnd) - (linesperregion + 1))
227 1.1 jtc break;
228 1.1 jtc wmove(wnd, row + linesperregion, 0);
229 1.1 jtc wdeleteln(wnd);
230 1.1 jtc wmove(wnd, row + 3, 0);
231 1.1 jtc winsertln(wnd);
232 1.1 jtc }
233 1.1 jtc (void) stats(row + 3, col, i);
234 1.1 jtc col += COLWIDTH;
235 1.1 jtc }
236 1.1 jtc }
237 1.1 jtc
238 1.1 jtc static int
239 1.16 ad stats(int row, int col, int dn)
240 1.1 jtc {
241 1.5 thorpej double atime, words;
242 1.5 thorpej
243 1.5 thorpej /* time busy in disk activity */
244 1.5 thorpej atime = (double)cur.dk_time[dn].tv_sec +
245 1.5 thorpej ((double)cur.dk_time[dn].tv_usec / (double)1000000);
246 1.1 jtc
247 1.5 thorpej words = cur.dk_bytes[dn] / 1024.0; /* # of K transferred */
248 1.1 jtc if (numbers) {
249 1.6 scottr mvwprintw(wnd, row, col, " %3.0f%4.0f%5.1f",
250 1.5 thorpej words / etime, cur.dk_xfer[dn] / etime, atime / etime);
251 1.1 jtc return (row);
252 1.1 jtc }
253 1.1 jtc wmove(wnd, row++, col);
254 1.5 thorpej histogram(words / etime, 50, 0.5);
255 1.1 jtc wmove(wnd, row++, col);
256 1.5 thorpej histogram(cur.dk_xfer[dn] / etime, 50, 0.5);
257 1.5 thorpej if (secs) {
258 1.1 jtc wmove(wnd, row++, col);
259 1.5 thorpej atime *= 1000; /* In milliseconds */
260 1.5 thorpej histogram(atime / etime, 50, 0.5);
261 1.1 jtc }
262 1.1 jtc return (row);
263 1.1 jtc }
264 1.1 jtc
265 1.1 jtc static void
266 1.16 ad stat1(int row, int o)
267 1.1 jtc {
268 1.9 lukem int i;
269 1.1 jtc double time;
270 1.1 jtc
271 1.1 jtc time = 0;
272 1.1 jtc for (i = 0; i < CPUSTATES; i++)
273 1.5 thorpej time += cur.cp_time[i];
274 1.1 jtc if (time == 0.0)
275 1.1 jtc time = 1.0;
276 1.1 jtc wmove(wnd, row, INSET);
277 1.1 jtc #define CPUSCALE 0.5
278 1.5 thorpej histogram(100.0 * cur.cp_time[o] / time, 50, CPUSCALE);
279 1.1 jtc }
280 1.1 jtc
281 1.1 jtc static void
282 1.16 ad histogram(double val, int colwidth, double scale)
283 1.1 jtc {
284 1.1 jtc char buf[10];
285 1.9 lukem int k;
286 1.9 lukem int v = (int)(val * scale) + 0.5;
287 1.1 jtc
288 1.1 jtc k = MIN(v, colwidth);
289 1.1 jtc if (v > colwidth) {
290 1.10 mrg snprintf(buf, sizeof buf, "%4.1f", val);
291 1.1 jtc k -= strlen(buf);
292 1.1 jtc while (k--)
293 1.1 jtc waddch(wnd, 'X');
294 1.1 jtc waddstr(wnd, buf);
295 1.5 thorpej wclrtoeol(wnd);
296 1.1 jtc return;
297 1.1 jtc }
298 1.1 jtc wclrtoeol(wnd);
299 1.15 mycroft whline(wnd, 'X', k);
300 1.1 jtc }
301 1.1 jtc
302 1.13 jwise void
303 1.16 ad iostat_bars(char *args)
304 1.13 jwise {
305 1.13 jwise numbers = 0;
306 1.13 jwise wclear(wnd);
307 1.13 jwise labeliostat();
308 1.13 jwise refresh();
309 1.13 jwise }
310 1.13 jwise
311 1.13 jwise void
312 1.16 ad iostat_numbers(char *args)
313 1.1 jtc {
314 1.13 jwise numbers = 1;
315 1.13 jwise wclear(wnd);
316 1.13 jwise labeliostat();
317 1.13 jwise refresh();
318 1.13 jwise }
319 1.1 jtc
320 1.13 jwise void
321 1.16 ad iostat_secs(char *args)
322 1.13 jwise {
323 1.13 jwise secs = !secs;
324 1.1 jtc wclear(wnd);
325 1.1 jtc labeliostat();
326 1.1 jtc refresh();
327 1.1 jtc }
328