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
29 * Copyright (c) 1980, 1992, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
62 static const char sccsid[] = "@(#)iostat.c 8.1 (Berkeley) 6/6/93";
65 #include <sys/param.h>
66 #include <sys/sysctl.h>
67 #include <sys/resource.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(int);
88 static void histogram(long double, int, double);
89 static int numlabels(int);
90 static int devstats(int, int, int);
91 static void stat1(int, int);
96 return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
100 closeiostat(WINDOW *w)
112 if ((num_devices = devstat_getnumdevs(NULL)) < 0)
115 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
116 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
117 bzero(cur.dinfo, sizeof(struct devinfo));
118 bzero(last.dinfo, sizeof(struct devinfo));
121 * This value for maxshowdevs (100) is bogus. I'm not sure exactly
122 * how to calculate it, though.
124 if (dsinit(100, &cur, &last, NULL) != 1)
133 struct devinfo *tmp_dinfo;
136 len = sizeof(cur.cp_time);
137 if (sysctlbyname("kern.cp_time", &cur.cp_time, &len, NULL, 0)
138 || len != sizeof(cur.cp_time)) {
139 perror("kern.cp_time");
142 tmp_dinfo = last.dinfo;
143 last.dinfo = cur.dinfo;
144 cur.dinfo = tmp_dinfo;
146 last.snap_time = cur.snap_time;
149 * Here what we want to do is refresh our device stats.
150 * getdevs() returns 1 when the device list has changed.
151 * If the device list has changed, we want to go through
152 * the selection process again, in case a device that we
153 * were previously displaying has gone away.
155 switch (devstat_getdevs(NULL, &cur)) {
157 errx(1, "%s", devstat_errbuf);
160 cmdiostat("refresh", NULL);
165 num_devices = cur.dinfo->numdevs;
166 generation = cur.dinfo->generation;
178 wmove(wnd, row, 0); wclrtobot(wnd);
179 mvwaddstr(wnd, row++, INSET,
180 "/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
181 mvwaddstr(wnd, row++, 0, "cpu user|");
182 mvwaddstr(wnd, row++, 0, " nice|");
183 mvwaddstr(wnd, row++, 0, " system|");
184 mvwaddstr(wnd, row++, 0, "interrupt|");
185 mvwaddstr(wnd, row++, 0, " idle|");
187 row = numlabels(row + 1);
189 row = barlabels(row + 1);
195 int i, _col, regions, ndrives;
199 #define DRIVESPERLINE ((wnd->_maxx - INSET) / COLWIDTH)
200 for (ndrives = 0, i = 0; i < num_devices; i++)
201 if (dev_select[i].selected)
203 regions = howmany(ndrives, DRIVESPERLINE);
205 * Deduct -regions for blank line after each scrolling region.
207 linesperregion = (wnd->_maxy - row - regions) / regions;
209 * Minimum region contains space for two
210 * label lines and one line of statistics.
212 if (linesperregion < 3)
215 for (i = 0; i < num_devices; i++)
216 if (dev_select[i].selected) {
217 if (_col + COLWIDTH >= wnd->_maxx - INSET) {
218 _col = INSET, row += linesperregion + 1;
219 if (row > wnd->_maxy - (linesperregion + 1))
222 sprintf(tmpstr, "%s%d", dev_select[i].device_name,
223 dev_select[i].unit_number);
224 mvwaddstr(wnd, row, _col + 4, tmpstr);
225 mvwaddstr(wnd, row + 1, _col, " KB/t tps MB/s ");
229 row += linesperregion + 1;
239 mvwaddstr(wnd, row++, INSET,
240 "/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
241 linesperregion = 2 + kbpt;
242 for (i = 0; i < num_devices; i++)
243 if (dev_select[i].selected) {
244 if (row > wnd->_maxy - linesperregion)
246 sprintf(tmpstr, "%s%d", dev_select[i].device_name,
247 dev_select[i].unit_number);
248 mvwprintw(wnd, row++, 0, "%-5.5s MB/s|",
250 mvwaddstr(wnd, row++, 0, " tps|");
252 mvwaddstr(wnd, row++, 0, " KB/t|");
264 #define X(fld) t = cur.fld[i]; cur.fld[i] -= last.fld[i]; last.fld[i] = t
266 for(i = 0; i < CPUSTATES; i++) {
268 etime += cur.cp_time[i];
274 for (i = 0; i < CPUSTATES; i++)
278 for (i = 0; i < num_devices; i++)
279 if (dev_select[i].selected) {
280 if (row > wnd->_maxy - linesperregion)
282 row = devstats(row, INSET, i);
287 wmove(wnd, row + linesperregion, 0);
289 wmove(wnd, row + 3, 0);
291 for (i = 0; i < num_devices; i++)
292 if (dev_select[i].selected) {
293 if (_col + COLWIDTH >= wnd->_maxx - INSET) {
294 _col = INSET, row += linesperregion + 1;
295 if (row > wnd->_maxy - (linesperregion + 1))
297 wmove(wnd, row + linesperregion, 0);
299 wmove(wnd, row + 3, 0);
302 (void) devstats(row + 3, _col, i);
308 devstats(int row, int _col, int dn)
310 long double transfers_per_second;
311 long double kb_per_transfer, mb_per_second;
312 long double busy_seconds;
315 di = dev_select[dn].position;
317 busy_seconds = cur.snap_time - last.snap_time;
319 if (devstat_compute_statistics(&cur.dinfo->devices[di],
320 &last.dinfo->devices[di], busy_seconds,
321 DSM_KB_PER_TRANSFER, &kb_per_transfer,
322 DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
323 DSM_MB_PER_SECOND, &mb_per_second, DSM_NONE) != 0)
324 errx(1, "%s", devstat_errbuf);
327 mvwprintw(wnd, row, _col, " %5.2Lf %3.0Lf %5.2Lf ",
328 kb_per_transfer, transfers_per_second,
332 wmove(wnd, row++, _col);
333 histogram(mb_per_second, 50, .5);
334 wmove(wnd, row++, _col);
335 histogram(transfers_per_second, 50, .5);
337 wmove(wnd, row++, _col);
338 histogram(kb_per_transfer, 50, .5);
346 stat1(int row, int o)
352 for (i = 0; i < CPUSTATES; i++)
353 dtime += cur.cp_time[i];
356 wmove(wnd, row, INSET);
358 histogram(100.0 * cur.cp_time[o] / dtime, 50, CPUSCALE);
362 histogram(long double val, int colwidth, double scale)
366 int v = (int)(val * scale) + 0.5;
368 k = MIN(v, colwidth);
370 snprintf(buf, sizeof(buf), "%5.2Lf", val);
383 cmdiostat(const char *cmd, const char *args)
386 if (prefix(cmd, "kbpt"))
388 else if (prefix(cmd, "numbers"))
390 else if (prefix(cmd, "bars"))
392 else if (!dscmd(cmd, args, 100, &cur))