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 struct statinfo cur, last;
84 static int linesperregion;
86 static int numbers = 0; /* default display bar graphs */
87 static int kbpt = 0; /* default ms/seek shown */
89 static int barlabels(int);
90 static void histogram(long double, int, double);
91 static int numlabels(int);
92 static int devstats(int, int, int);
93 static void stat1(int, int);
98 return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
102 closeiostat(WINDOW *w)
114 if ((num_devices = devstat_getnumdevs(NULL)) < 0)
117 cur.dinfo = calloc(1, sizeof(struct devinfo));
118 last.dinfo = calloc(1, 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 ((getmaxx(wnd) - 1 - 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 = (getmaxy(wnd) - 1 - 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 >= getmaxx(wnd) - 1 - INSET) {
218 _col = INSET, row += linesperregion + 1;
219 if (row > getmaxy(wnd) - 1 - (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 > getmaxy(wnd) - 1 - 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|");
263 #define X(fld) t = cur.fld[i]; cur.fld[i] -= last.fld[i]; last.fld[i] = t
265 for(i = 0; i < CPUSTATES; i++) {
267 etime += cur.cp_time[i];
273 for (i = 0; i < CPUSTATES; i++)
277 for (i = 0; i < num_devices; i++)
278 if (dev_select[i].selected) {
279 if (row > getmaxy(wnd) - linesperregion)
281 row = devstats(row, INSET, i);
286 wmove(wnd, row + linesperregion, 0);
288 wmove(wnd, row + 3, 0);
290 for (i = 0; i < num_devices; i++)
291 if (dev_select[i].selected) {
292 if (_col + COLWIDTH >= getmaxx(wnd) - 1 - INSET) {
293 _col = INSET, row += linesperregion + 1;
294 if (row > getmaxy(wnd) - 1 - (linesperregion + 1))
296 wmove(wnd, row + linesperregion, 0);
298 wmove(wnd, row + 3, 0);
301 (void) devstats(row + 3, _col, i);
307 devstats(int row, int _col, int dn)
309 long double transfers_per_second;
310 long double kb_per_transfer, mb_per_second;
311 long double busy_seconds;
314 di = dev_select[dn].position;
316 busy_seconds = cur.snap_time - last.snap_time;
318 if (devstat_compute_statistics(&cur.dinfo->devices[di],
319 &last.dinfo->devices[di], busy_seconds,
320 DSM_KB_PER_TRANSFER, &kb_per_transfer,
321 DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
322 DSM_MB_PER_SECOND, &mb_per_second, DSM_NONE) != 0)
323 errx(1, "%s", devstat_errbuf);
326 mvwprintw(wnd, row, _col, " %5.2Lf %3.0Lf %5.2Lf ",
327 kb_per_transfer, transfers_per_second,
331 wmove(wnd, row++, _col);
332 histogram(mb_per_second, 50, .5);
333 wmove(wnd, row++, _col);
334 histogram(transfers_per_second, 50, .5);
336 wmove(wnd, row++, _col);
337 histogram(kb_per_transfer, 50, .5);
345 stat1(int row, int o)
351 for (i = 0; i < CPUSTATES; i++)
352 dtime += cur.cp_time[i];
355 wmove(wnd, row, INSET);
357 histogram(100.0 * cur.cp_time[o] / dtime, 50, CPUSCALE);
361 histogram(long double val, int colwidth, double scale)
365 int v = (int)(val * scale) + 0.5;
367 k = MIN(v, colwidth);
369 snprintf(buf, sizeof(buf), "%5.2Lf", val);
382 cmdiostat(const char *cmd, const char *args)
385 if (prefix(cmd, "kbpt"))
387 else if (prefix(cmd, "numbers"))
389 else if (prefix(cmd, "bars"))
391 else if (!dscmd(cmd, args, 100, &cur))