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>
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)
113 * This value for maxshowdevs (100) is bogus. I'm not sure exactly
114 * how to calculate it, though.
125 struct devinfo *tmp_dinfo;
128 len = sizeof(cur_dev.cp_time);
129 if (sysctlbyname("kern.cp_time", &cur_dev.cp_time, &len, NULL, 0)
130 || len != sizeof(cur_dev.cp_time)) {
131 perror("kern.cp_time");
134 tmp_dinfo = last_dev.dinfo;
135 last_dev.dinfo = cur_dev.dinfo;
136 cur_dev.dinfo = tmp_dinfo;
138 last_dev.snap_time = cur_dev.snap_time;
141 * Here what we want to do is refresh our device stats.
142 * getdevs() returns 1 when the device list has changed.
143 * If the device list has changed, we want to go through
144 * the selection process again, in case a device that we
145 * were previously displaying has gone away.
147 switch (devstat_getdevs(NULL, &cur_dev)) {
149 errx(1, "%s", devstat_errbuf);
152 cmdiostat("refresh", NULL);
157 num_devices = cur_dev.dinfo->numdevs;
158 generation = cur_dev.dinfo->generation;
170 wmove(wnd, row, 0); wclrtobot(wnd);
171 mvwaddstr(wnd, row++, INSET,
172 "/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
173 mvwaddstr(wnd, row++, 0, "cpu user|");
174 mvwaddstr(wnd, row++, 0, " nice|");
175 mvwaddstr(wnd, row++, 0, " system|");
176 mvwaddstr(wnd, row++, 0, "interrupt|");
177 mvwaddstr(wnd, row++, 0, " idle|");
179 row = numlabels(row + 1);
181 row = barlabels(row + 1);
187 int i, _col, regions, ndrives;
191 #define DRIVESPERLINE ((getmaxx(wnd) - 1 - INSET) / COLWIDTH)
192 for (ndrives = 0, i = 0; i < num_devices; i++)
193 if (dev_select[i].selected)
195 regions = howmany(ndrives, DRIVESPERLINE);
197 * Deduct -regions for blank line after each scrolling region.
199 linesperregion = (getmaxy(wnd) - 1 - row - regions) / regions;
201 * Minimum region contains space for two
202 * label lines and one line of statistics.
204 if (linesperregion < 3)
207 for (i = 0; i < num_devices; i++)
208 if (dev_select[i].selected) {
209 if (_col + COLWIDTH >= getmaxx(wnd) - 1 - INSET) {
210 _col = INSET, row += linesperregion + 1;
211 if (row > getmaxy(wnd) - 1 - (linesperregion + 1))
214 sprintf(tmpstr, "%s%d", dev_select[i].device_name,
215 dev_select[i].unit_number);
216 mvwaddstr(wnd, row, _col + 4, tmpstr);
217 mvwaddstr(wnd, row + 1, _col, " KB/t tps MB/s ");
221 row += linesperregion + 1;
231 mvwaddstr(wnd, row++, INSET,
232 "/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
233 linesperregion = 2 + kbpt;
234 for (i = 0; i < num_devices; i++)
235 if (dev_select[i].selected) {
236 if (row > getmaxy(wnd) - 1 - linesperregion)
238 sprintf(tmpstr, "%s%d", dev_select[i].device_name,
239 dev_select[i].unit_number);
240 mvwprintw(wnd, row++, 0, "%-5.5s MB/s|",
242 mvwaddstr(wnd, row++, 0, " tps|");
244 mvwaddstr(wnd, row++, 0, " KB/t|");
255 #define X(fld) t = cur_dev.fld[i]; cur_dev.fld[i] -= last_dev.fld[i]; last_dev.fld[i] = t
257 for(i = 0; i < CPUSTATES; i++) {
259 etime += cur_dev.cp_time[i];
265 for (i = 0; i < CPUSTATES; i++)
269 for (i = 0; i < num_devices; i++)
270 if (dev_select[i].selected) {
271 if (row > getmaxy(wnd) - linesperregion)
273 row = devstats(row, INSET, i);
278 wmove(wnd, row + linesperregion, 0);
280 wmove(wnd, row + 3, 0);
282 for (i = 0; i < num_devices; i++)
283 if (dev_select[i].selected) {
284 if (_col + COLWIDTH >= getmaxx(wnd) - 1 - INSET) {
285 _col = INSET, row += linesperregion + 1;
286 if (row > getmaxy(wnd) - 1 - (linesperregion + 1))
288 wmove(wnd, row + linesperregion, 0);
290 wmove(wnd, row + 3, 0);
293 (void) devstats(row + 3, _col, i);
299 devstats(int row, int _col, int dn)
301 long double transfers_per_second;
302 long double kb_per_transfer, mb_per_second;
303 long double busy_seconds;
306 di = dev_select[dn].position;
308 busy_seconds = cur_dev.snap_time - last_dev.snap_time;
310 if (devstat_compute_statistics(&cur_dev.dinfo->devices[di],
311 &last_dev.dinfo->devices[di], busy_seconds,
312 DSM_KB_PER_TRANSFER, &kb_per_transfer,
313 DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
314 DSM_MB_PER_SECOND, &mb_per_second, DSM_NONE) != 0)
315 errx(1, "%s", devstat_errbuf);
318 mvwprintw(wnd, row, _col, " %5.2Lf %3.0Lf %5.2Lf ",
319 kb_per_transfer, transfers_per_second,
323 wmove(wnd, row++, _col);
324 histogram(mb_per_second, 50, .5);
325 wmove(wnd, row++, _col);
326 histogram(transfers_per_second, 50, .5);
328 wmove(wnd, row++, _col);
329 histogram(kb_per_transfer, 50, .5);
337 stat1(int row, int o)
343 for (i = 0; i < CPUSTATES; i++)
344 dtime += cur_dev.cp_time[i];
347 wmove(wnd, row, INSET);
349 histogram(100.0 * cur_dev.cp_time[o] / dtime, 50, CPUSCALE);
353 histogram(long double val, int colwidth, double scale)
357 int v = (int)(val * scale) + 0.5;
359 k = MIN(v, colwidth);
361 snprintf(buf, sizeof(buf), "%5.2Lf", val);
374 cmdiostat(const char *cmd, const char *args)
377 if (prefix(cmd, "kbpt"))
379 else if (prefix(cmd, "numbers"))
381 else if (prefix(cmd, "bars"))
383 else if (!dscmd(cmd, args, 100, &cur_dev))