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 $
30 * @(#)iostat.c 8.1 (Berkeley) 6/6/93
33 * Copyright (c) 1980, 1992, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 struct statinfo cur, last;
74 static struct kinfo_cputime cp_time, old_cp_time;
76 static int linesperregion;
77 static int numbers = 0; /* default display bar graphs */
78 static int kbpt = 0; /* default ms/seek shown */
80 static int barlabels(int);
81 static void histogram(long double, int, double);
82 static int numlabels(int);
83 static int devstats(int, int, int);
84 static void stat1(int, uint64_t, uint64_t);
89 return (subwin(stdscr, LINES-1-5, 0, 5, 0));
93 closeiostat(WINDOW *w)
105 if ((num_devices = getnumdevs()) < 0)
108 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
109 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
110 bzero(cur.dinfo, sizeof(struct devinfo));
111 bzero(last.dinfo, sizeof(struct devinfo));
114 * This value for maxshowdevs (100) is bogus. I'm not sure exactly
115 * how to calculate it, though.
117 if (dsinit(100, &cur, &last, NULL) != 1)
126 struct devinfo *tmp_dinfo;
128 if (kinfo_get_sched_cputime(&cp_time))
129 err(1, "kinfo_get_sched_cputime");
130 tmp_dinfo = last.dinfo;
131 last.dinfo = cur.dinfo;
132 cur.dinfo = tmp_dinfo;
134 last.busy_time = cur.busy_time;
137 * Here what we want to do is refresh our device stats.
138 * getdevs() returns 1 when the device list has changed.
139 * If the device list has changed, we want to go through
140 * the selection process again, in case a device that we
141 * were previously displaying has gone away.
143 switch (getdevs(&cur)) {
145 errx(1, "%s", devstat_errbuf);
148 cmdiostat("refresh", NULL);
153 num_devices = cur.dinfo->numdevs;
154 generation = cur.dinfo->generation;
166 wmove(wnd, row, 0); wclrtobot(wnd);
167 mvwaddstr(wnd, row++, INSET,
168 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
169 mvwaddstr(wnd, row++, 0, "cpu user|");
170 mvwaddstr(wnd, row++, 0, " nice|");
171 mvwaddstr(wnd, row++, 0, " system|");
172 mvwaddstr(wnd, row++, 0, "interrupt|");
173 mvwaddstr(wnd, row++, 0, " idle|");
175 row = numlabels(row + 1);
177 row = barlabels(row + 1);
183 int i, _col, regions, ndrives;
187 #define DRIVESPERLINE ((wnd->_maxx - INSET) / COLWIDTH)
188 for (ndrives = 0, i = 0; i < num_devices; i++)
189 if (dev_select[i].selected)
191 regions = howmany(ndrives, DRIVESPERLINE);
193 * Deduct -regions for blank line after each scrolling region.
195 linesperregion = (wnd->_maxy - row - regions) / regions;
197 * Minimum region contains space for two
198 * label lines and one line of statistics.
200 if (linesperregion < 3)
203 for (i = 0; i < num_devices; i++)
204 if (dev_select[i].selected) {
205 if (_col + COLWIDTH >= wnd->_maxx - INSET) {
206 _col = INSET, row += linesperregion + 1;
207 if (row > wnd->_maxy - (linesperregion + 1))
210 sprintf(tmpstr, "%.6s%d", dev_select[i].device_name,
211 dev_select[i].unit_number);
212 mvwaddstr(wnd, row, _col + 4, tmpstr);
213 mvwaddstr(wnd, row + 1, _col, " KB/t tps MB/s ");
217 row += linesperregion + 1;
227 mvwaddstr(wnd, row++, INSET,
228 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
229 linesperregion = 2 + kbpt;
230 for (i = 0; i < num_devices; i++)
231 if (dev_select[i].selected) {
232 if (row > wnd->_maxy - linesperregion)
234 sprintf(tmpstr, "%.4s%d", dev_select[i].device_name,
235 dev_select[i].unit_number);
236 mvwprintw(wnd, row++, 0, "%-5.5s MB/s|",
238 mvwaddstr(wnd, row++, 0, " tps|");
240 mvwaddstr(wnd, row++, 0, " KB/t|");
250 struct kinfo_cputime diff_cp_time;
253 diff_cp_time.cp_user = cp_time.cp_user - old_cp_time.cp_user;
254 diff_cp_time.cp_nice = cp_time.cp_nice - old_cp_time.cp_nice;
255 diff_cp_time.cp_sys = cp_time.cp_sys - old_cp_time.cp_sys;
256 diff_cp_time.cp_intr = cp_time.cp_intr - old_cp_time.cp_intr;
257 diff_cp_time.cp_idle = cp_time.cp_idle - old_cp_time.cp_idle;
258 old_cp_time = cp_time;
261 cp_total = diff_cp_time.cp_user + diff_cp_time.cp_nice +
262 diff_cp_time.cp_sys + diff_cp_time.cp_intr + diff_cp_time.cp_idle;
263 stat1(row++, diff_cp_time.cp_user, cp_total);
264 stat1(row++, diff_cp_time.cp_nice, cp_total);
265 stat1(row++, diff_cp_time.cp_sys, cp_total);
266 stat1(row++, diff_cp_time.cp_intr, cp_total);
267 stat1(row++, diff_cp_time.cp_idle, cp_total);
270 for (i = 0; i < num_devices; i++)
271 if (dev_select[i].selected) {
272 if (row > wnd->_maxy - 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 >= wnd->_maxx - INSET) {
286 _col = INSET, row += linesperregion + 1;
287 if (row > wnd->_maxy - (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 = compute_etime(cur.busy_time, last.busy_time);
311 if (compute_stats(&cur.dinfo->devices[di], &last.dinfo->devices[di],
312 busy_seconds, NULL, NULL, NULL,
313 &kb_per_transfer, &transfers_per_second,
314 &mb_per_second, NULL, NULL) != 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, uint64_t difference, uint64_t total)
342 dtime = 100.0 * difference / total;
345 wmove(wnd, row, INSET);
347 histogram(dtime, 50, CPUSCALE);
351 histogram(long double val, int colwidth, double scale)
355 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, char *args)
376 if (prefix(cmd, "kbpt"))
378 else if (prefix(cmd, "numbers"))
380 else if (prefix(cmd, "bars"))
382 else if (!dscmd(cmd, args, 100, &cur))