2 * Copyright (c) 1998 Kenneth D. Merry.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Copyright (c) 1980, 1992, 1993
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 static char sccsid[] = "@(#)iostat.c 8.1 (Berkeley) 6/6/93";
67 #include <sys/param.h>
68 #include <sys/dkstat.h>
80 struct statinfo cur, last;
82 static int linesperregion;
84 static int numbers = 0; /* default display bar graphs */
85 static int kbpt = 0; /* default ms/seek shown */
87 static int barlabels __P((int));
88 static void histogram __P((long double, int, double));
89 static int numlabels __P((int));
90 static int devstats __P((int, int, int));
91 static void stat1 __P((int, int));
96 return (subwin(stdscr, LINES-1-5, 0, 5, 0));
113 if (num_devices = getnumdevs() < 0)
116 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
117 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
118 bzero(cur.dinfo, sizeof(struct devinfo));
119 bzero(last.dinfo, sizeof(struct devinfo));
122 * This value for maxshowdevs (100) is bogus. I'm not sure exactly
123 * how to calculate it, though.
125 if (dsinit(100, &cur, &last, NULL) != 1)
134 struct devinfo *tmp_dinfo;
138 len = sizeof(cur.cp_time);
139 err = sysctlbyname("kern.cp_time", &cur.cp_time, &len, NULL, 0);
140 if (err || len != sizeof(cur.cp_time)) {
141 perror("kern.cp_time");
144 tmp_dinfo = last.dinfo;
145 last.dinfo = cur.dinfo;
146 cur.dinfo = tmp_dinfo;
148 last.busy_time = cur.busy_time;
151 * Here what we want to do is refresh our device stats.
152 * getdevs() returns 1 when the device list has changed.
153 * If the device list has changed, we want to go through
154 * the selection process again, in case a device that we
155 * were previously displaying has gone away.
157 switch (getdevs(&cur)) {
159 errx(1, "%s", devstat_errbuf);
162 cmdiostat("refresh", NULL);
167 num_devices = cur.dinfo->numdevs;
168 generation = cur.dinfo->generation;
180 wmove(wnd, row, 0); wclrtobot(wnd);
181 mvwaddstr(wnd, row++, INSET,
182 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
183 mvwaddstr(wnd, row++, 0, "cpu user|");
184 mvwaddstr(wnd, row++, 0, " nice|");
185 mvwaddstr(wnd, row++, 0, " system|");
186 mvwaddstr(wnd, row++, 0, "interrupt|");
187 mvwaddstr(wnd, row++, 0, " idle|");
189 row = numlabels(row + 1);
191 row = barlabels(row + 1);
198 int i, col, regions, ndrives;
202 #define DRIVESPERLINE ((wnd->_maxx - INSET) / COLWIDTH)
203 for (ndrives = 0, i = 0; i < num_devices; i++)
204 if (dev_select[i].selected)
206 regions = howmany(ndrives, DRIVESPERLINE);
208 * Deduct -regions for blank line after each scrolling region.
210 linesperregion = (wnd->_maxy - row - regions) / regions;
212 * Minimum region contains space for two
213 * label lines and one line of statistics.
215 if (linesperregion < 3)
218 for (i = 0; i < num_devices; i++)
219 if (dev_select[i].selected) {
220 if (col + COLWIDTH >= wnd->_maxx - INSET) {
221 col = INSET, row += linesperregion + 1;
222 if (row > wnd->_maxy - (linesperregion + 1))
225 sprintf(tmpstr, "%s%d", dev_select[i].device_name,
226 dev_select[i].unit_number);
227 mvwaddstr(wnd, row, col + 4, tmpstr);
228 mvwaddstr(wnd, row + 1, col, " KB/t tps MB/s ");
232 row += linesperregion + 1;
243 mvwaddstr(wnd, row++, INSET,
244 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
245 linesperregion = 2 + kbpt;
246 for (i = 0; i < num_devices; i++)
247 if (dev_select[i].selected) {
248 if (row > wnd->_maxy - linesperregion)
250 sprintf(tmpstr, "%s%d", dev_select[i].device_name,
251 dev_select[i].unit_number);
252 mvwprintw(wnd, row++, 0, "%-5.5s MB/s|",
254 mvwaddstr(wnd, row++, 0, " tps|");
256 mvwaddstr(wnd, row++, 0, " KB/t|");
266 register int i, row, col;
268 #define X(fld) t = cur.fld[i]; cur.fld[i] -= last.fld[i]; last.fld[i] = t
270 for(i = 0; i < CPUSTATES; i++) {
272 etime += cur.cp_time[i];
278 for (i = 0; i < CPUSTATES; i++)
282 for (i = 0; i < num_devices; i++)
283 if (dev_select[i].selected) {
284 if (row > wnd->_maxy - linesperregion)
286 row = devstats(row, INSET, i);
291 wmove(wnd, row + linesperregion, 0);
293 wmove(wnd, row + 3, 0);
295 for (i = 0; i < num_devices; i++)
296 if (dev_select[i].selected) {
297 if (col + COLWIDTH >= wnd->_maxx - INSET) {
298 col = INSET, row += linesperregion + 1;
299 if (row > wnd->_maxy - (linesperregion + 1))
301 wmove(wnd, row + linesperregion, 0);
303 wmove(wnd, row + 3, 0);
306 (void) devstats(row + 3, col, i);
312 devstats(row, col, dn)
315 long double transfers_per_second;
316 long double kb_per_transfer, mb_per_second;
317 long double busy_seconds;
320 di = dev_select[dn].position;
322 busy_seconds = compute_etime(cur.busy_time, last.busy_time);
324 if (compute_stats(&cur.dinfo->devices[di], &last.dinfo->devices[di],
325 busy_seconds, NULL, NULL, NULL,
326 &kb_per_transfer, &transfers_per_second,
327 &mb_per_second, NULL, NULL) != 0)
328 errx(1, "%s", devstat_errbuf);
331 mvwprintw(wnd, row, col, " %5.2Lf %3.0Lf %5.2Lf ",
332 kb_per_transfer, transfers_per_second,
336 wmove(wnd, row++, col);
337 histogram(mb_per_second, 50, .5);
338 wmove(wnd, row++, col);
339 histogram(transfers_per_second, 50, .5);
341 wmove(wnd, row++, col);
342 histogram(kb_per_transfer, 50, .5);
357 for (i = 0; i < CPUSTATES; i++)
358 time += cur.cp_time[i];
361 wmove(wnd, row, INSET);
363 histogram(100.0 * cur.cp_time[o] / time, 50, CPUSCALE);
367 histogram(val, colwidth, scale)
374 register int v = (int)(val * scale) + 0.5;
376 k = MIN(v, colwidth);
378 snprintf(buf, sizeof(buf), "%5.2Lf", val);
395 if (prefix(cmd, "kbpt"))
397 else if (prefix(cmd, "numbers"))
399 else if (prefix(cmd, "bars"))
401 else if (!dscmd(cmd, args, 100, &cur))