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
28 * $FreeBSD: src/usr.bin/systat/iostat.c,v 1.9.2.1 2000/07/02 10:03:17 ps Exp $
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. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 static char sccsid[] = "@(#)iostat.c 8.1 (Berkeley) 6/6/93";
67 #include <sys/param.h>
68 #include <sys/dkstat.h>
80 static struct nlist namelist[] = {
84 #define X_MBDINIT (X_CP_TIME+1)
86 #define X_UBDINIT (X_CP_TIME+2)
90 #define X_VBDINIT (X_CP_TIME+1)
96 struct statinfo cur, last;
98 static int linesperregion;
100 static int numbers = 0; /* default display bar graphs */
101 static int kbpt = 0; /* default ms/seek shown */
103 static int barlabels __P((int));
104 static void histogram __P((long double, int, double));
105 static int numlabels __P((int));
106 static int devstats __P((int, int, int));
107 static void stat1 __P((int, int));
112 return (subwin(stdscr, LINES-1-5, 0, 5, 0));
129 if (num_devices = getnumdevs() < 0)
132 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
133 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
134 bzero(cur.dinfo, sizeof(struct devinfo));
135 bzero(last.dinfo, sizeof(struct devinfo));
138 * This value for maxshowdevs (100) is bogus. I'm not sure exactly
139 * how to calculate it, though.
141 if (dsinit(100, &cur, &last, NULL) != 1)
144 if (kvm_nlist(kd, namelist)) {
155 struct devinfo *tmp_dinfo;
157 NREAD(X_CP_TIME, cur.cp_time, sizeof(cur.cp_time));
158 tmp_dinfo = last.dinfo;
159 last.dinfo = cur.dinfo;
160 cur.dinfo = tmp_dinfo;
162 last.busy_time = cur.busy_time;
165 * Here what we want to do is refresh our device stats.
166 * getdevs() returns 1 when the device list has changed.
167 * If the device list has changed, we want to go through
168 * the selection process again, in case a device that we
169 * were previously displaying has gone away.
171 switch (getdevs(&cur)) {
173 errx(1, "%s", devstat_errbuf);
176 cmdiostat("refresh", NULL);
181 num_devices = cur.dinfo->numdevs;
182 generation = cur.dinfo->generation;
194 wmove(wnd, row, 0); wclrtobot(wnd);
195 mvwaddstr(wnd, row++, INSET,
196 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
197 mvwaddstr(wnd, row++, 0, "cpu user|");
198 mvwaddstr(wnd, row++, 0, " nice|");
199 mvwaddstr(wnd, row++, 0, " system|");
200 mvwaddstr(wnd, row++, 0, "interrupt|");
201 mvwaddstr(wnd, row++, 0, " idle|");
203 row = numlabels(row + 1);
205 row = barlabels(row + 1);
212 int i, col, regions, ndrives;
216 #define DRIVESPERLINE ((wnd->_maxx - INSET) / COLWIDTH)
217 for (ndrives = 0, i = 0; i < num_devices; i++)
218 if (dev_select[i].selected)
220 regions = howmany(ndrives, DRIVESPERLINE);
222 * Deduct -regions for blank line after each scrolling region.
224 linesperregion = (wnd->_maxy - row - regions) / regions;
226 * Minimum region contains space for two
227 * label lines and one line of statistics.
229 if (linesperregion < 3)
232 for (i = 0; i < num_devices; i++)
233 if (dev_select[i].selected) {
234 if (col + COLWIDTH >= wnd->_maxx - INSET) {
235 col = INSET, row += linesperregion + 1;
236 if (row > wnd->_maxy - (linesperregion + 1))
239 sprintf(tmpstr, "%s%d", dev_select[i].device_name,
240 dev_select[i].unit_number);
241 mvwaddstr(wnd, row, col + 4, tmpstr);
242 mvwaddstr(wnd, row + 1, col, " KB/t tps MB/s ");
246 row += linesperregion + 1;
257 mvwaddstr(wnd, row++, INSET,
258 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
259 linesperregion = 2 + kbpt;
260 for (i = 0; i < num_devices; i++)
261 if (dev_select[i].selected) {
262 if (row > wnd->_maxy - linesperregion)
264 sprintf(tmpstr, "%s%d", dev_select[i].device_name,
265 dev_select[i].unit_number);
266 mvwprintw(wnd, row++, 0, "%-5.5s MB/s|",
268 mvwaddstr(wnd, row++, 0, " tps|");
270 mvwaddstr(wnd, row++, 0, " KB/t|");
280 register int i, row, col;
282 #define X(fld) t = cur.fld[i]; cur.fld[i] -= last.fld[i]; last.fld[i] = t
284 for(i = 0; i < CPUSTATES; i++) {
286 etime += cur.cp_time[i];
292 for (i = 0; i < CPUSTATES; i++)
296 for (i = 0; i < num_devices; i++)
297 if (dev_select[i].selected) {
298 if (row > wnd->_maxy - linesperregion)
300 row = devstats(row, INSET, i);
305 wmove(wnd, row + linesperregion, 0);
307 wmove(wnd, row + 3, 0);
309 for (i = 0; i < num_devices; i++)
310 if (dev_select[i].selected) {
311 if (col + COLWIDTH >= wnd->_maxx - INSET) {
312 col = INSET, row += linesperregion + 1;
313 if (row > wnd->_maxy - (linesperregion + 1))
315 wmove(wnd, row + linesperregion, 0);
317 wmove(wnd, row + 3, 0);
320 (void) devstats(row + 3, col, i);
326 devstats(row, col, dn)
329 long double transfers_per_second;
330 long double kb_per_transfer, mb_per_second;
331 long double busy_seconds;
334 di = dev_select[dn].position;
336 busy_seconds = compute_etime(cur.busy_time, last.busy_time);
338 if (compute_stats(&cur.dinfo->devices[di], &last.dinfo->devices[di],
339 busy_seconds, NULL, NULL, NULL,
340 &kb_per_transfer, &transfers_per_second,
341 &mb_per_second, NULL, NULL) != 0)
342 errx(1, "%s", devstat_errbuf);
345 mvwprintw(wnd, row, col, " %5.2Lf %3.0Lf %5.2Lf ",
346 kb_per_transfer, transfers_per_second,
350 wmove(wnd, row++, col);
351 histogram(mb_per_second, 50, .5);
352 wmove(wnd, row++, col);
353 histogram(transfers_per_second, 50, .5);
355 wmove(wnd, row++, col);
356 histogram(kb_per_transfer, 50, .5);
371 for (i = 0; i < CPUSTATES; i++)
372 time += cur.cp_time[i];
375 wmove(wnd, row, INSET);
377 histogram(100.0 * cur.cp_time[o] / time, 50, CPUSCALE);
381 histogram(val, colwidth, scale)
388 register int v = (int)(val * scale) + 0.5;
390 k = MIN(v, colwidth);
392 snprintf(buf, sizeof(buf), "%5.2Lf", val);
409 if (prefix(cmd, "kbpt"))
411 else if (prefix(cmd, "numbers"))
413 else if (prefix(cmd, "bars"))
415 else if (!dscmd(cmd, args, 100, &cur))