iostat.c revision 1.1 1 1.1 jtc /*
2 1.1 jtc * Copyright (c) 1980, 1992, 1993
3 1.1 jtc * The Regents of the University of California. All rights reserved.
4 1.1 jtc *
5 1.1 jtc * Redistribution and use in source and binary forms, with or without
6 1.1 jtc * modification, are permitted provided that the following conditions
7 1.1 jtc * are met:
8 1.1 jtc * 1. Redistributions of source code must retain the above copyright
9 1.1 jtc * notice, this list of conditions and the following disclaimer.
10 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 jtc * notice, this list of conditions and the following disclaimer in the
12 1.1 jtc * documentation and/or other materials provided with the distribution.
13 1.1 jtc * 3. All advertising materials mentioning features or use of this software
14 1.1 jtc * must display the following acknowledgement:
15 1.1 jtc * This product includes software developed by the University of
16 1.1 jtc * California, Berkeley and its contributors.
17 1.1 jtc * 4. Neither the name of the University nor the names of its contributors
18 1.1 jtc * may be used to endorse or promote products derived from this software
19 1.1 jtc * without specific prior written permission.
20 1.1 jtc *
21 1.1 jtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 jtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 jtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 jtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 jtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 jtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 jtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 jtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 jtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 jtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 jtc * SUCH DAMAGE.
32 1.1 jtc */
33 1.1 jtc
34 1.1 jtc #ifndef lint
35 1.1 jtc static char sccsid[] = "@(#)iostat.c 8.1 (Berkeley) 6/6/93";
36 1.1 jtc #endif not lint
37 1.1 jtc
38 1.1 jtc #include <sys/param.h>
39 1.1 jtc #include <sys/dkstat.h>
40 1.1 jtc #include <sys/buf.h>
41 1.1 jtc
42 1.1 jtc #include <string.h>
43 1.1 jtc #include <stdlib.h>
44 1.1 jtc #include <nlist.h>
45 1.1 jtc #include <paths.h>
46 1.1 jtc #include "systat.h"
47 1.1 jtc #include "extern.h"
48 1.1 jtc
49 1.1 jtc static struct nlist namelist[] = {
50 1.1 jtc #define X_DK_BUSY 0
51 1.1 jtc { "_dk_busy" },
52 1.1 jtc #define X_DK_TIME 1
53 1.1 jtc { "_dk_time" },
54 1.1 jtc #define X_DK_XFER 2
55 1.1 jtc { "_dk_xfer" },
56 1.1 jtc #define X_DK_WDS 3
57 1.1 jtc { "_dk_wds" },
58 1.1 jtc #define X_DK_SEEK 4
59 1.1 jtc { "_dk_seek" },
60 1.1 jtc #define X_CP_TIME 5
61 1.1 jtc { "_cp_time" },
62 1.1 jtc #ifdef vax
63 1.1 jtc #define X_MBDINIT (X_CP_TIME+1)
64 1.1 jtc { "_mbdinit" },
65 1.1 jtc #define X_UBDINIT (X_CP_TIME+2)
66 1.1 jtc { "_ubdinit" },
67 1.1 jtc #endif
68 1.1 jtc #ifdef tahoe
69 1.1 jtc #define X_VBDINIT (X_CP_TIME+1)
70 1.1 jtc { "_vbdinit" },
71 1.1 jtc #endif
72 1.1 jtc { "" },
73 1.1 jtc };
74 1.1 jtc
75 1.1 jtc static struct {
76 1.1 jtc int dk_busy;
77 1.1 jtc long cp_time[CPUSTATES];
78 1.1 jtc long *dk_time;
79 1.1 jtc long *dk_wds;
80 1.1 jtc long *dk_seek;
81 1.1 jtc long *dk_xfer;
82 1.1 jtc } s, s1;
83 1.1 jtc
84 1.1 jtc static int linesperregion;
85 1.1 jtc static double etime;
86 1.1 jtc static int numbers = 0; /* default display bar graphs */
87 1.1 jtc static int msps = 0; /* default ms/seek shown */
88 1.1 jtc
89 1.1 jtc static int barlabels __P((int));
90 1.1 jtc static void histogram __P((double, int, double));
91 1.1 jtc static int numlabels __P((int));
92 1.1 jtc static int stats __P((int, int, int));
93 1.1 jtc static void stat1 __P((int, int));
94 1.1 jtc
95 1.1 jtc
96 1.1 jtc WINDOW *
97 1.1 jtc openiostat()
98 1.1 jtc {
99 1.1 jtc return (subwin(stdscr, LINES-1-5, 0, 5, 0));
100 1.1 jtc }
101 1.1 jtc
102 1.1 jtc void
103 1.1 jtc closeiostat(w)
104 1.1 jtc WINDOW *w;
105 1.1 jtc {
106 1.1 jtc if (w == NULL)
107 1.1 jtc return;
108 1.1 jtc wclear(w);
109 1.1 jtc wrefresh(w);
110 1.1 jtc delwin(w);
111 1.1 jtc }
112 1.1 jtc
113 1.1 jtc int
114 1.1 jtc initiostat()
115 1.1 jtc {
116 1.1 jtc if (namelist[X_DK_BUSY].n_type == 0) {
117 1.1 jtc if (kvm_nlist(kd, namelist)) {
118 1.1 jtc nlisterr(namelist);
119 1.1 jtc return(0);
120 1.1 jtc }
121 1.1 jtc if (namelist[X_DK_BUSY].n_type == 0) {
122 1.1 jtc error("Disk init information isn't in namelist");
123 1.1 jtc return(0);
124 1.1 jtc }
125 1.1 jtc }
126 1.1 jtc if (! dkinit())
127 1.1 jtc return(0);
128 1.1 jtc if (dk_ndrive) {
129 1.1 jtc #define allocate(e, t) \
130 1.1 jtc s./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
131 1.1 jtc s1./**/e = (t *)calloc(dk_ndrive, sizeof (t));
132 1.1 jtc allocate(dk_time, long);
133 1.1 jtc allocate(dk_wds, long);
134 1.1 jtc allocate(dk_seek, long);
135 1.1 jtc allocate(dk_xfer, long);
136 1.1 jtc #undef allocate
137 1.1 jtc }
138 1.1 jtc return(1);
139 1.1 jtc }
140 1.1 jtc
141 1.1 jtc void
142 1.1 jtc fetchiostat()
143 1.1 jtc {
144 1.1 jtc if (namelist[X_DK_BUSY].n_type == 0)
145 1.1 jtc return;
146 1.1 jtc NREAD(X_DK_BUSY, &s.dk_busy, LONG);
147 1.1 jtc NREAD(X_DK_TIME, s.dk_time, dk_ndrive * LONG);
148 1.1 jtc NREAD(X_DK_XFER, s.dk_xfer, dk_ndrive * LONG);
149 1.1 jtc NREAD(X_DK_WDS, s.dk_wds, dk_ndrive * LONG);
150 1.1 jtc NREAD(X_DK_SEEK, s.dk_seek, dk_ndrive * LONG);
151 1.1 jtc NREAD(X_CP_TIME, s.cp_time, sizeof s.cp_time);
152 1.1 jtc }
153 1.1 jtc
154 1.1 jtc #define INSET 10
155 1.1 jtc
156 1.1 jtc void
157 1.1 jtc labeliostat()
158 1.1 jtc {
159 1.1 jtc int row;
160 1.1 jtc
161 1.1 jtc if (namelist[X_DK_BUSY].n_type == 0) {
162 1.1 jtc error("No dk_busy defined.");
163 1.1 jtc return;
164 1.1 jtc }
165 1.1 jtc row = 0;
166 1.1 jtc wmove(wnd, row, 0); wclrtobot(wnd);
167 1.1 jtc mvwaddstr(wnd, row++, INSET,
168 1.1 jtc "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
169 1.1 jtc mvwaddstr(wnd, row++, 0, "cpu user|");
170 1.1 jtc mvwaddstr(wnd, row++, 0, " nice|");
171 1.1 jtc mvwaddstr(wnd, row++, 0, " system|");
172 1.1 jtc mvwaddstr(wnd, row++, 0, " idle|");
173 1.1 jtc if (numbers)
174 1.1 jtc row = numlabels(row + 1);
175 1.1 jtc else
176 1.1 jtc row = barlabels(row + 1);
177 1.1 jtc }
178 1.1 jtc
179 1.1 jtc static int
180 1.1 jtc numlabels(row)
181 1.1 jtc int row;
182 1.1 jtc {
183 1.1 jtc int i, col, regions, ndrives;
184 1.1 jtc
185 1.1 jtc #define COLWIDTH 14
186 1.1 jtc #define DRIVESPERLINE ((wnd->maxx - INSET) / COLWIDTH)
187 1.1 jtc for (ndrives = 0, i = 0; i < dk_ndrive; i++)
188 1.1 jtc if (dk_select[i])
189 1.1 jtc ndrives++;
190 1.1 jtc regions = howmany(ndrives, DRIVESPERLINE);
191 1.1 jtc /*
192 1.1 jtc * Deduct -regions for blank line after each scrolling region.
193 1.1 jtc */
194 1.1 jtc linesperregion = (wnd->maxy - row - regions) / regions;
195 1.1 jtc /*
196 1.1 jtc * Minimum region contains space for two
197 1.1 jtc * label lines and one line of statistics.
198 1.1 jtc */
199 1.1 jtc if (linesperregion < 3)
200 1.1 jtc linesperregion = 3;
201 1.1 jtc col = 0;
202 1.1 jtc for (i = 0; i < dk_ndrive; i++)
203 1.1 jtc if (dk_select[i] && dk_mspw[i] != 0.0) {
204 1.1 jtc if (col + COLWIDTH >= wnd->maxx - INSET) {
205 1.1 jtc col = 0, row += linesperregion + 1;
206 1.1 jtc if (row > wnd->maxy - (linesperregion + 1))
207 1.1 jtc break;
208 1.1 jtc }
209 1.1 jtc mvwaddstr(wnd, row, col + 4, dr_name[i]);
210 1.1 jtc mvwaddstr(wnd, row + 1, col, "bps tps msps");
211 1.1 jtc col += COLWIDTH;
212 1.1 jtc }
213 1.1 jtc if (col)
214 1.1 jtc row += linesperregion + 1;
215 1.1 jtc return (row);
216 1.1 jtc }
217 1.1 jtc
218 1.1 jtc static int
219 1.1 jtc barlabels(row)
220 1.1 jtc int row;
221 1.1 jtc {
222 1.1 jtc int i;
223 1.1 jtc
224 1.1 jtc mvwaddstr(wnd, row++, INSET,
225 1.1 jtc "/0 /5 /10 /15 /20 /25 /30 /35 /40 /45 /50");
226 1.1 jtc linesperregion = 2 + msps;
227 1.1 jtc for (i = 0; i < dk_ndrive; i++)
228 1.1 jtc if (dk_select[i] && dk_mspw[i] != 0.0) {
229 1.1 jtc if (row > wnd->maxy - linesperregion)
230 1.1 jtc break;
231 1.1 jtc mvwprintw(wnd, row++, 0, "%3.3s bps|", dr_name[i]);
232 1.1 jtc mvwaddstr(wnd, row++, 0, " tps|");
233 1.1 jtc if (msps)
234 1.1 jtc mvwaddstr(wnd, row++, 0, " msps|");
235 1.1 jtc }
236 1.1 jtc return (row);
237 1.1 jtc }
238 1.1 jtc
239 1.1 jtc
240 1.1 jtc void
241 1.1 jtc showiostat()
242 1.1 jtc {
243 1.1 jtc register long t;
244 1.1 jtc register int i, row, col;
245 1.1 jtc
246 1.1 jtc if (namelist[X_DK_BUSY].n_type == 0)
247 1.1 jtc return;
248 1.1 jtc for (i = 0; i < dk_ndrive; i++) {
249 1.1 jtc #define X(fld) t = s.fld[i]; s.fld[i] -= s1.fld[i]; s1.fld[i] = t
250 1.1 jtc X(dk_xfer); X(dk_seek); X(dk_wds); X(dk_time);
251 1.1 jtc }
252 1.1 jtc etime = 0;
253 1.1 jtc for(i = 0; i < CPUSTATES; i++) {
254 1.1 jtc X(cp_time);
255 1.1 jtc etime += s.cp_time[i];
256 1.1 jtc }
257 1.1 jtc if (etime == 0.0)
258 1.1 jtc etime = 1.0;
259 1.1 jtc etime /= (float) hz;
260 1.1 jtc row = 1;
261 1.1 jtc
262 1.1 jtc /*
263 1.1 jtc * Last CPU state not calculated yet.
264 1.1 jtc */
265 1.1 jtc for (i = 0; i < CPUSTATES - 1; i++)
266 1.1 jtc stat1(row++, i);
267 1.1 jtc if (!numbers) {
268 1.1 jtc row += 2;
269 1.1 jtc for (i = 0; i < dk_ndrive; i++)
270 1.1 jtc if (dk_select[i] && dk_mspw[i] != 0.0) {
271 1.1 jtc if (row > wnd->maxy - linesperregion)
272 1.1 jtc break;
273 1.1 jtc row = stats(row, INSET, i);
274 1.1 jtc }
275 1.1 jtc return;
276 1.1 jtc }
277 1.1 jtc col = 0;
278 1.1 jtc wmove(wnd, row + linesperregion, 0);
279 1.1 jtc wdeleteln(wnd);
280 1.1 jtc wmove(wnd, row + 3, 0);
281 1.1 jtc winsertln(wnd);
282 1.1 jtc for (i = 0; i < dk_ndrive; i++)
283 1.1 jtc if (dk_select[i] && dk_mspw[i] != 0.0) {
284 1.1 jtc if (col + COLWIDTH >= wnd->maxx) {
285 1.1 jtc col = 0, row += linesperregion + 1;
286 1.1 jtc if (row > wnd->maxy - (linesperregion + 1))
287 1.1 jtc break;
288 1.1 jtc wmove(wnd, row + linesperregion, 0);
289 1.1 jtc wdeleteln(wnd);
290 1.1 jtc wmove(wnd, row + 3, 0);
291 1.1 jtc winsertln(wnd);
292 1.1 jtc }
293 1.1 jtc (void) stats(row + 3, col, i);
294 1.1 jtc col += COLWIDTH;
295 1.1 jtc }
296 1.1 jtc }
297 1.1 jtc
298 1.1 jtc static int
299 1.1 jtc stats(row, col, dn)
300 1.1 jtc int row, col, dn;
301 1.1 jtc {
302 1.1 jtc double atime, words, xtime, itime;
303 1.1 jtc
304 1.1 jtc atime = s.dk_time[dn];
305 1.1 jtc atime /= (float) hz;
306 1.1 jtc words = s.dk_wds[dn]*32.0; /* number of words transferred */
307 1.1 jtc xtime = dk_mspw[dn]*words; /* transfer time */
308 1.1 jtc itime = atime - xtime; /* time not transferring */
309 1.1 jtc if (xtime < 0)
310 1.1 jtc itime += xtime, xtime = 0;
311 1.1 jtc if (itime < 0)
312 1.1 jtc xtime += itime, itime = 0;
313 1.1 jtc if (numbers) {
314 1.1 jtc mvwprintw(wnd, row, col, "%3.0f%4.0f%5.1f",
315 1.1 jtc words / 512 / etime, s.dk_xfer[dn] / etime,
316 1.1 jtc s.dk_seek[dn] ? itime * 1000. / s.dk_seek[dn] : 0.0);
317 1.1 jtc return (row);
318 1.1 jtc }
319 1.1 jtc wmove(wnd, row++, col);
320 1.1 jtc histogram(words / 512 / etime, 50, 1.0);
321 1.1 jtc wmove(wnd, row++, col);
322 1.1 jtc histogram(s.dk_xfer[dn] / etime, 50, 1.0);
323 1.1 jtc if (msps) {
324 1.1 jtc wmove(wnd, row++, col);
325 1.1 jtc histogram(s.dk_seek[dn] ? itime * 1000. / s.dk_seek[dn] : 0,
326 1.1 jtc 50, 1.0);
327 1.1 jtc }
328 1.1 jtc return (row);
329 1.1 jtc }
330 1.1 jtc
331 1.1 jtc static void
332 1.1 jtc stat1(row, o)
333 1.1 jtc int row, o;
334 1.1 jtc {
335 1.1 jtc register int i;
336 1.1 jtc double time;
337 1.1 jtc
338 1.1 jtc time = 0;
339 1.1 jtc for (i = 0; i < CPUSTATES; i++)
340 1.1 jtc time += s.cp_time[i];
341 1.1 jtc if (time == 0.0)
342 1.1 jtc time = 1.0;
343 1.1 jtc wmove(wnd, row, INSET);
344 1.1 jtc #define CPUSCALE 0.5
345 1.1 jtc histogram(100.0 * s.cp_time[o] / time, 50, CPUSCALE);
346 1.1 jtc }
347 1.1 jtc
348 1.1 jtc static void
349 1.1 jtc histogram(val, colwidth, scale)
350 1.1 jtc double val;
351 1.1 jtc int colwidth;
352 1.1 jtc double scale;
353 1.1 jtc {
354 1.1 jtc char buf[10];
355 1.1 jtc register int k;
356 1.1 jtc register int v = (int)(val * scale) + 0.5;
357 1.1 jtc
358 1.1 jtc k = MIN(v, colwidth);
359 1.1 jtc if (v > colwidth) {
360 1.1 jtc sprintf(buf, "%4.1f", val);
361 1.1 jtc k -= strlen(buf);
362 1.1 jtc while (k--)
363 1.1 jtc waddch(wnd, 'X');
364 1.1 jtc waddstr(wnd, buf);
365 1.1 jtc return;
366 1.1 jtc }
367 1.1 jtc while (k--)
368 1.1 jtc waddch(wnd, 'X');
369 1.1 jtc wclrtoeol(wnd);
370 1.1 jtc }
371 1.1 jtc
372 1.1 jtc int
373 1.1 jtc cmdiostat(cmd, args)
374 1.1 jtc char *cmd, *args;
375 1.1 jtc {
376 1.1 jtc
377 1.1 jtc if (prefix(cmd, "msps"))
378 1.1 jtc msps = !msps;
379 1.1 jtc else if (prefix(cmd, "numbers"))
380 1.1 jtc numbers = 1;
381 1.1 jtc else if (prefix(cmd, "bars"))
382 1.1 jtc numbers = 0;
383 1.1 jtc else if (!dkcmd(cmd, args))
384 1.1 jtc return (0);
385 1.1 jtc wclear(wnd);
386 1.1 jtc labeliostat();
387 1.1 jtc refresh();
388 1.1 jtc return (1);
389 1.1 jtc }
390