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