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 $
29 * $DragonFly: src/usr.bin/systat/iostat.c,v 1.7 2008/11/10 04:59:45 swildner Exp $
31 * @(#)iostat.c 8.1 (Berkeley) 6/6/93
34 * Copyright (c) 1980, 1992, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74 struct statinfo cur
, last
;
75 static struct kinfo_cputime cp_time
, old_cp_time
;
77 static int linesperregion
;
78 static int numbers
= 0; /* default display bar graphs */
79 static int kbpt
= 0; /* default ms/seek shown */
81 static int barlabels(int);
82 static void histogram(long double, int, double);
83 static int numlabels(int);
84 static int devstats(int, int, int);
85 static void stat1(int, uint64_t, uint64_t);
90 return (subwin(stdscr
, LINES
-1-5, 0, 5, 0));
94 closeiostat(WINDOW
*w
)
106 if ((num_devices
= getnumdevs()) < 0)
109 cur
.dinfo
= (struct devinfo
*)malloc(sizeof(struct devinfo
));
110 last
.dinfo
= (struct devinfo
*)malloc(sizeof(struct devinfo
));
111 bzero(cur
.dinfo
, sizeof(struct devinfo
));
112 bzero(last
.dinfo
, sizeof(struct devinfo
));
115 * This value for maxshowdevs (100) is bogus. I'm not sure exactly
116 * how to calculate it, though.
118 if (dsinit(100, &cur
, &last
, NULL
) != 1)
127 struct devinfo
*tmp_dinfo
;
129 if (kinfo_get_sched_cputime(&cp_time
))
130 err(1, "kinfo_get_sched_cputime");
131 tmp_dinfo
= last
.dinfo
;
132 last
.dinfo
= cur
.dinfo
;
133 cur
.dinfo
= tmp_dinfo
;
135 last
.busy_time
= cur
.busy_time
;
138 * Here what we want to do is refresh our device stats.
139 * getdevs() returns 1 when the device list has changed.
140 * If the device list has changed, we want to go through
141 * the selection process again, in case a device that we
142 * were previously displaying has gone away.
144 switch (getdevs(&cur
)) {
146 errx(1, "%s", devstat_errbuf
);
149 cmdiostat("refresh", NULL
);
154 num_devices
= cur
.dinfo
->numdevs
;
155 generation
= cur
.dinfo
->generation
;
167 wmove(wnd
, row
, 0); wclrtobot(wnd
);
168 mvwaddstr(wnd
, row
++, INSET
,
169 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
170 mvwaddstr(wnd
, row
++, 0, "cpu user|");
171 mvwaddstr(wnd
, row
++, 0, " nice|");
172 mvwaddstr(wnd
, row
++, 0, " system|");
173 mvwaddstr(wnd
, row
++, 0, "interrupt|");
174 mvwaddstr(wnd
, row
++, 0, " idle|");
176 row
= numlabels(row
+ 1);
178 row
= barlabels(row
+ 1);
184 int i
, _col
, regions
, ndrives
;
188 #define DRIVESPERLINE ((wnd->_maxx - INSET) / COLWIDTH)
189 for (ndrives
= 0, i
= 0; i
< num_devices
; i
++)
190 if (dev_select
[i
].selected
)
192 regions
= howmany(ndrives
, DRIVESPERLINE
);
194 * Deduct -regions for blank line after each scrolling region.
196 linesperregion
= (wnd
->_maxy
- row
- regions
) / regions
;
198 * Minimum region contains space for two
199 * label lines and one line of statistics.
201 if (linesperregion
< 3)
204 for (i
= 0; i
< num_devices
; i
++)
205 if (dev_select
[i
].selected
) {
206 if (_col
+ COLWIDTH
>= wnd
->_maxx
- INSET
) {
207 _col
= INSET
, row
+= linesperregion
+ 1;
208 if (row
> wnd
->_maxy
- (linesperregion
+ 1))
211 sprintf(tmpstr
, "%s%d", dev_select
[i
].device_name
,
212 dev_select
[i
].unit_number
);
213 mvwaddstr(wnd
, row
, _col
+ 4, tmpstr
);
214 mvwaddstr(wnd
, row
+ 1, _col
, " KB/t tps MB/s ");
218 row
+= linesperregion
+ 1;
228 mvwaddstr(wnd
, row
++, INSET
,
229 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
230 linesperregion
= 2 + kbpt
;
231 for (i
= 0; i
< num_devices
; i
++)
232 if (dev_select
[i
].selected
) {
233 if (row
> wnd
->_maxy
- linesperregion
)
235 sprintf(tmpstr
, "%s%d", dev_select
[i
].device_name
,
236 dev_select
[i
].unit_number
);
237 mvwprintw(wnd
, row
++, 0, "%-5.5s MB/s|",
239 mvwaddstr(wnd
, row
++, 0, " tps|");
241 mvwaddstr(wnd
, row
++, 0, " KB/t|");
251 struct kinfo_cputime diff_cp_time
;
254 diff_cp_time
.cp_user
= cp_time
.cp_user
- old_cp_time
.cp_user
;
255 diff_cp_time
.cp_nice
= cp_time
.cp_nice
- old_cp_time
.cp_nice
;
256 diff_cp_time
.cp_sys
= cp_time
.cp_sys
- old_cp_time
.cp_sys
;
257 diff_cp_time
.cp_intr
= cp_time
.cp_intr
- old_cp_time
.cp_intr
;
258 diff_cp_time
.cp_idle
= cp_time
.cp_idle
- old_cp_time
.cp_idle
;
259 old_cp_time
= cp_time
;
262 cp_total
= diff_cp_time
.cp_user
+ diff_cp_time
.cp_nice
+
263 diff_cp_time
.cp_sys
+ diff_cp_time
.cp_intr
+ diff_cp_time
.cp_idle
;
264 stat1(row
++, diff_cp_time
.cp_user
, cp_total
);
265 stat1(row
++, diff_cp_time
.cp_nice
, cp_total
);
266 stat1(row
++, diff_cp_time
.cp_sys
, cp_total
);
267 stat1(row
++, diff_cp_time
.cp_intr
, cp_total
);
268 stat1(row
++, diff_cp_time
.cp_idle
, cp_total
);
271 for (i
= 0; i
< num_devices
; i
++)
272 if (dev_select
[i
].selected
) {
273 if (row
> wnd
->_maxy
- linesperregion
)
275 row
= devstats(row
, INSET
, i
);
280 wmove(wnd
, row
+ linesperregion
, 0);
282 wmove(wnd
, row
+ 3, 0);
284 for (i
= 0; i
< num_devices
; i
++)
285 if (dev_select
[i
].selected
) {
286 if (_col
+ COLWIDTH
>= wnd
->_maxx
- INSET
) {
287 _col
= INSET
, row
+= linesperregion
+ 1;
288 if (row
> wnd
->_maxy
- (linesperregion
+ 1))
290 wmove(wnd
, row
+ linesperregion
, 0);
292 wmove(wnd
, row
+ 3, 0);
295 (void) devstats(row
+ 3, _col
, i
);
301 devstats(int row
, int _col
, int dn
)
303 long double transfers_per_second
;
304 long double kb_per_transfer
, mb_per_second
;
305 long double busy_seconds
;
308 di
= dev_select
[dn
].position
;
310 busy_seconds
= compute_etime(cur
.busy_time
, last
.busy_time
);
312 if (compute_stats(&cur
.dinfo
->devices
[di
], &last
.dinfo
->devices
[di
],
313 busy_seconds
, NULL
, NULL
, NULL
,
314 &kb_per_transfer
, &transfers_per_second
,
315 &mb_per_second
, NULL
, NULL
) != 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
, uint64_t difference
, uint64_t total
)
343 dtime
= 100.0 * difference
/ total
;
346 wmove(wnd
, row
, INSET
);
348 histogram(dtime
, 50, CPUSCALE
);
352 histogram(long double val
, int colwidth
, double scale
)
356 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
, 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
))