2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1998 Kenneth D. Merry.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * 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. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
64 static const char sccsid[] = "@(#)iostat.c 8.1 (Berkeley) 6/6/93";
67 #include <sys/param.h>
68 #include <sys/sysctl.h>
69 #include <sys/resource.h>
83 static int linesperregion;
85 static bool numbers = false; /* default display bar graphs */
86 static bool kbpt = false; /* default ms/seek shown */
88 static int barlabels(int);
89 static void histogram(long double, int, double);
90 static int numlabels(int);
91 static int devstats(int, int, int);
92 static void stat1(int, int);
97 return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
101 closeiostat(WINDOW *w)
114 * This value for maxshowdevs (100) is bogus. I'm not sure exactly
115 * how to calculate it, though.
126 struct devinfo *tmp_dinfo;
129 len = sizeof(cur_dev.cp_time);
130 if (sysctlbyname("kern.cp_time", &cur_dev.cp_time, &len, NULL, 0)
131 || len != sizeof(cur_dev.cp_time)) {
132 perror("kern.cp_time");
135 tmp_dinfo = last_dev.dinfo;
136 last_dev.dinfo = cur_dev.dinfo;
137 cur_dev.dinfo = tmp_dinfo;
139 last_dev.snap_time = cur_dev.snap_time;
142 * Here what we want to do is refresh our device stats.
143 * getdevs() returns 1 when the device list has changed.
144 * If the device list has changed, we want to go through
145 * the selection process again, in case a device that we
146 * were previously displaying has gone away.
148 switch (devstat_getdevs(NULL, &cur_dev)) {
150 errx(1, "%s", devstat_errbuf);
153 cmdiostat("refresh", NULL);
158 num_devices = cur_dev.dinfo->numdevs;
159 generation = cur_dev.dinfo->generation;
171 wmove(wnd, row, 0); wclrtobot(wnd);
172 mvwaddstr(wnd, row++, INSET,
173 "/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
174 mvwaddstr(wnd, row++, 0, "cpu user|");
175 mvwaddstr(wnd, row++, 0, " nice|");
176 mvwaddstr(wnd, row++, 0, " system|");
177 mvwaddstr(wnd, row++, 0, "interrupt|");
178 mvwaddstr(wnd, row++, 0, " idle|");
180 row = numlabels(row + 1);
182 row = barlabels(row + 1);
188 int i, _col, regions, ndrives;
192 #define DRIVESPERLINE ((getmaxx(wnd) - 1 - INSET) / COLWIDTH)
193 for (ndrives = 0, i = 0; i < num_devices; i++)
194 if (dev_select[i].selected)
196 regions = howmany(ndrives, DRIVESPERLINE);
198 * Deduct -regions for blank line after each scrolling region.
200 linesperregion = (getmaxy(wnd) - 1 - row - regions) / regions;
202 * Minimum region contains space for two
203 * label lines and one line of statistics.
205 if (linesperregion < 3)
208 for (i = 0; i < num_devices; i++)
209 if (dev_select[i].selected) {
210 if (_col + COLWIDTH >= getmaxx(wnd) - 1 - INSET) {
211 _col = INSET, row += linesperregion + 1;
212 if (row > getmaxy(wnd) - 1 - (linesperregion + 1))
215 snprintf(tmpstr, sizeof(tmpstr), "%s%d", dev_select[i].device_name,
216 dev_select[i].unit_number);
217 mvwaddstr(wnd, row, _col + 4, tmpstr);
218 mvwaddstr(wnd, row + 1, _col, " KB/t tps MB/s ");
222 row += linesperregion + 1;
232 mvwaddstr(wnd, row++, INSET,
233 "/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
234 linesperregion = 2 + kbpt;
235 for (i = 0; i < num_devices; i++)
236 if (dev_select[i].selected) {
237 if (row > getmaxy(wnd) - 1 - linesperregion)
239 snprintf(tmpstr, sizeof(tmpstr), "%s%d", dev_select[i].device_name,
240 dev_select[i].unit_number);
241 mvwprintw(wnd, row++, 0, "%-5.5s MB/s|",
243 mvwaddstr(wnd, row++, 0, " tps|");
245 mvwaddstr(wnd, row++, 0, " KB/t|");
256 #define X(fld) t = cur_dev.fld[i]; cur_dev.fld[i] -= last_dev.fld[i]; last_dev.fld[i] = t
258 for(i = 0; i < CPUSTATES; i++) {
260 etime += cur_dev.cp_time[i];
266 for (i = 0; i < CPUSTATES; i++)
270 for (i = 0; i < num_devices; i++)
271 if (dev_select[i].selected) {
272 if (row > getmaxy(wnd) - linesperregion)
274 row = devstats(row, INSET, i);
279 wmove(wnd, row + linesperregion, 0);
281 wmove(wnd, row + 3, 0);
283 for (i = 0; i < num_devices; i++)
284 if (dev_select[i].selected) {
285 if (_col + COLWIDTH >= getmaxx(wnd) - 1 - INSET) {
286 _col = INSET, row += linesperregion + 1;
287 if (row > getmaxy(wnd) - 1 - (linesperregion + 1))
289 wmove(wnd, row + linesperregion, 0);
291 wmove(wnd, row + 3, 0);
294 (void) devstats(row + 3, _col, i);
300 devstats(int row, int _col, int dn)
302 long double transfers_per_second;
303 long double kb_per_transfer, mb_per_second;
304 long double busy_seconds;
307 di = dev_select[dn].position;
309 busy_seconds = cur_dev.snap_time - last_dev.snap_time;
311 if (devstat_compute_statistics(&cur_dev.dinfo->devices[di],
312 &last_dev.dinfo->devices[di], busy_seconds,
313 DSM_KB_PER_TRANSFER, &kb_per_transfer,
314 DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
315 DSM_MB_PER_SECOND, &mb_per_second, DSM_NONE) != 0)
316 errx(1, "%s", devstat_errbuf);
319 mvwprintw(wnd, row, _col, " %5.2Lf %3.0Lf %5.2Lf ",
320 kb_per_transfer, transfers_per_second,
324 wmove(wnd, row++, _col);
325 histogram(mb_per_second, 50, .5);
326 wmove(wnd, row++, _col);
327 histogram(transfers_per_second, 50, .5);
329 wmove(wnd, row++, _col);
330 histogram(kb_per_transfer, 50, .5);
338 stat1(int row, int o)
344 for (i = 0; i < CPUSTATES; i++)
345 dtime += cur_dev.cp_time[i];
348 wmove(wnd, row, INSET);
350 histogram(100.0 * cur_dev.cp_time[o] / dtime, 50, CPUSCALE);
354 histogram(long double val, int colwidth, double scale)
358 int v = (int)(val * scale) + 0.5;
360 k = MIN(v, colwidth);
362 snprintf(buf, sizeof(buf), "%5.2Lf", val);
375 cmdiostat(const char *cmd, const char *args)
378 if (prefix(cmd, "kbpt"))
380 else if (prefix(cmd, "numbers"))
382 else if (prefix(cmd, "bars"))
384 else if (!dscmd(cmd, args, 100, &cur_dev))