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 = calloc(1, sizeof(struct devinfo));
116 last.dinfo = calloc(1, sizeof(struct devinfo));
119 * This value for maxshowdevs (100) is bogus. I'm not sure exactly
120 * how to calculate it, though.
122 if (dsinit(100, &cur, &last, NULL) != 1)
131 struct devinfo *tmp_dinfo;
134 len = sizeof(cur.cp_time);
135 if (sysctlbyname("kern.cp_time", &cur.cp_time, &len, NULL, 0)
136 || len != sizeof(cur.cp_time)) {
137 perror("kern.cp_time");
140 tmp_dinfo = last.dinfo;
141 last.dinfo = cur.dinfo;
142 cur.dinfo = tmp_dinfo;
144 last.snap_time = cur.snap_time;
147 * Here what we want to do is refresh our device stats.
148 * getdevs() returns 1 when the device list has changed.
149 * If the device list has changed, we want to go through
150 * the selection process again, in case a device that we
151 * were previously displaying has gone away.
153 switch (devstat_getdevs(NULL, &cur)) {
155 errx(1, "%s", devstat_errbuf);
158 cmdiostat("refresh", NULL);
163 num_devices = cur.dinfo->numdevs;
164 generation = cur.dinfo->generation;
176 wmove(wnd, row, 0); wclrtobot(wnd);
177 mvwaddstr(wnd, row++, INSET,
178 "/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
179 mvwaddstr(wnd, row++, 0, "cpu user|");
180 mvwaddstr(wnd, row++, 0, " nice|");
181 mvwaddstr(wnd, row++, 0, " system|");
182 mvwaddstr(wnd, row++, 0, "interrupt|");
183 mvwaddstr(wnd, row++, 0, " idle|");
185 row = numlabels(row + 1);
187 row = barlabels(row + 1);
193 int i, _col, regions, ndrives;
197 #define DRIVESPERLINE ((getmaxx(wnd) - 1 - INSET) / COLWIDTH)
198 for (ndrives = 0, i = 0; i < num_devices; i++)
199 if (dev_select[i].selected)
201 regions = howmany(ndrives, DRIVESPERLINE);
203 * Deduct -regions for blank line after each scrolling region.
205 linesperregion = (getmaxy(wnd) - 1 - row - regions) / regions;
207 * Minimum region contains space for two
208 * label lines and one line of statistics.
210 if (linesperregion < 3)
213 for (i = 0; i < num_devices; i++)
214 if (dev_select[i].selected) {
215 if (_col + COLWIDTH >= getmaxx(wnd) - 1 - INSET) {
216 _col = INSET, row += linesperregion + 1;
217 if (row > getmaxy(wnd) - 1 - (linesperregion + 1))
220 sprintf(tmpstr, "%s%d", dev_select[i].device_name,
221 dev_select[i].unit_number);
222 mvwaddstr(wnd, row, _col + 4, tmpstr);
223 mvwaddstr(wnd, row + 1, _col, " KB/t tps MB/s ");
227 row += linesperregion + 1;
237 mvwaddstr(wnd, row++, INSET,
238 "/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
239 linesperregion = 2 + kbpt;
240 for (i = 0; i < num_devices; i++)
241 if (dev_select[i].selected) {
242 if (row > getmaxy(wnd) - 1 - linesperregion)
244 sprintf(tmpstr, "%s%d", dev_select[i].device_name,
245 dev_select[i].unit_number);
246 mvwprintw(wnd, row++, 0, "%-5.5s MB/s|",
248 mvwaddstr(wnd, row++, 0, " tps|");
250 mvwaddstr(wnd, row++, 0, " KB/t|");
261 #define X(fld) t = cur.fld[i]; cur.fld[i] -= last.fld[i]; last.fld[i] = t
263 for(i = 0; i < CPUSTATES; i++) {
265 etime += cur.cp_time[i];
271 for (i = 0; i < CPUSTATES; i++)
275 for (i = 0; i < num_devices; i++)
276 if (dev_select[i].selected) {
277 if (row > getmaxy(wnd) - linesperregion)
279 row = devstats(row, INSET, i);
284 wmove(wnd, row + linesperregion, 0);
286 wmove(wnd, row + 3, 0);
288 for (i = 0; i < num_devices; i++)
289 if (dev_select[i].selected) {
290 if (_col + COLWIDTH >= getmaxx(wnd) - 1 - INSET) {
291 _col = INSET, row += linesperregion + 1;
292 if (row > getmaxy(wnd) - 1 - (linesperregion + 1))
294 wmove(wnd, row + linesperregion, 0);
296 wmove(wnd, row + 3, 0);
299 (void) devstats(row + 3, _col, i);
305 devstats(int row, int _col, int dn)
307 long double transfers_per_second;
308 long double kb_per_transfer, mb_per_second;
309 long double busy_seconds;
312 di = dev_select[dn].position;
314 busy_seconds = cur.snap_time - last.snap_time;
316 if (devstat_compute_statistics(&cur.dinfo->devices[di],
317 &last.dinfo->devices[di], busy_seconds,
318 DSM_KB_PER_TRANSFER, &kb_per_transfer,
319 DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
320 DSM_MB_PER_SECOND, &mb_per_second, DSM_NONE) != 0)
321 errx(1, "%s", devstat_errbuf);
324 mvwprintw(wnd, row, _col, " %5.2Lf %3.0Lf %5.2Lf ",
325 kb_per_transfer, transfers_per_second,
329 wmove(wnd, row++, _col);
330 histogram(mb_per_second, 50, .5);
331 wmove(wnd, row++, _col);
332 histogram(transfers_per_second, 50, .5);
334 wmove(wnd, row++, _col);
335 histogram(kb_per_transfer, 50, .5);
343 stat1(int row, int o)
349 for (i = 0; i < CPUSTATES; i++)
350 dtime += cur.cp_time[i];
353 wmove(wnd, row, INSET);
355 histogram(100.0 * cur.cp_time[o] / dtime, 50, CPUSCALE);
359 histogram(long double val, int colwidth, double scale)
363 int v = (int)(val * scale) + 0.5;
365 k = MIN(v, colwidth);
367 snprintf(buf, sizeof(buf), "%5.2Lf", val);
380 cmdiostat(const char *cmd, const char *args)
383 if (prefix(cmd, "kbpt"))
385 else if (prefix(cmd, "numbers"))
387 else if (prefix(cmd, "bars"))
389 else if (!dscmd(cmd, args, 100, &cur))