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.5 2005/01/13 13:45:39 y0netan1 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. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 #include <sys/param.h>
78 struct statinfo cur
, last
;
79 static struct kinfo_cputime cp_time
, old_cp_time
;
81 static int linesperregion
;
82 static int numbers
= 0; /* default display bar graphs */
83 static int kbpt
= 0; /* default ms/seek shown */
85 static int barlabels(int);
86 static void histogram(long double, int, double);
87 static int numlabels(int);
88 static int devstats(int, int, int);
89 static void stat1(int, uint64_t, uint64_t);
94 return (subwin(stdscr
, LINES
-1-5, 0, 5, 0));
98 closeiostat(WINDOW
*w
)
110 if (num_devices
= getnumdevs() < 0)
113 cur
.dinfo
= (struct devinfo
*)malloc(sizeof(struct devinfo
));
114 last
.dinfo
= (struct devinfo
*)malloc(sizeof(struct devinfo
));
115 bzero(cur
.dinfo
, sizeof(struct devinfo
));
116 bzero(last
.dinfo
, sizeof(struct devinfo
));
119 * This value for maxshowdevs (100) is bogus. I'm not sure exactly
120 * how to calculate it, though.
122 if (dsinit(100, &cur
, &last
, NULL
) != 1)
131 struct devinfo
*tmp_dinfo
;
133 if (kinfo_get_sched_cputime(&cp_time
))
134 err(1, "kinfo_get_sched_cputime");
135 tmp_dinfo
= last
.dinfo
;
136 last
.dinfo
= cur
.dinfo
;
137 cur
.dinfo
= tmp_dinfo
;
139 last
.busy_time
= cur
.busy_time
;
142 * Here what we want to do is refresh our device stats.
143 * getdevs() returns 1 when the device list has changed.
144 * If the device list has changed, we want to go through
145 * the selection process again, in case a device that we
146 * were previously displaying has gone away.
148 switch (getdevs(&cur
)) {
150 errx(1, "%s", devstat_errbuf
);
153 cmdiostat("refresh", NULL
);
158 num_devices
= cur
.dinfo
->numdevs
;
159 generation
= cur
.dinfo
->generation
;
171 wmove(wnd
, row
, 0); wclrtobot(wnd
);
172 mvwaddstr(wnd
, row
++, INSET
,
173 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
174 mvwaddstr(wnd
, row
++, 0, "cpu user|");
175 mvwaddstr(wnd
, row
++, 0, " nice|");
176 mvwaddstr(wnd
, row
++, 0, " system|");
177 mvwaddstr(wnd
, row
++, 0, "interrupt|");
178 mvwaddstr(wnd
, row
++, 0, " idle|");
180 row
= numlabels(row
+ 1);
182 row
= barlabels(row
+ 1);
188 int i
, col
, regions
, ndrives
;
192 #define DRIVESPERLINE ((wnd->_maxx - INSET) / COLWIDTH)
193 for (ndrives
= 0, i
= 0; i
< num_devices
; i
++)
194 if (dev_select
[i
].selected
)
196 regions
= howmany(ndrives
, DRIVESPERLINE
);
198 * Deduct -regions for blank line after each scrolling region.
200 linesperregion
= (wnd
->_maxy
- row
- regions
) / regions
;
202 * Minimum region contains space for two
203 * label lines and one line of statistics.
205 if (linesperregion
< 3)
208 for (i
= 0; i
< num_devices
; i
++)
209 if (dev_select
[i
].selected
) {
210 if (col
+ COLWIDTH
>= wnd
->_maxx
- INSET
) {
211 col
= INSET
, row
+= linesperregion
+ 1;
212 if (row
> wnd
->_maxy
- (linesperregion
+ 1))
215 sprintf(tmpstr
, "%s%d", dev_select
[i
].device_name
,
216 dev_select
[i
].unit_number
);
217 mvwaddstr(wnd
, row
, col
+ 4, tmpstr
);
218 mvwaddstr(wnd
, row
+ 1, col
, " KB/t tps MB/s ");
222 row
+= linesperregion
+ 1;
232 mvwaddstr(wnd
, row
++, INSET
,
233 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
234 linesperregion
= 2 + kbpt
;
235 for (i
= 0; i
< num_devices
; i
++)
236 if (dev_select
[i
].selected
) {
237 if (row
> wnd
->_maxy
- linesperregion
)
239 sprintf(tmpstr
, "%s%d", dev_select
[i
].device_name
,
240 dev_select
[i
].unit_number
);
241 mvwprintw(wnd
, row
++, 0, "%-5.5s MB/s|",
243 mvwaddstr(wnd
, row
++, 0, " tps|");
245 mvwaddstr(wnd
, row
++, 0, " KB/t|");
255 register int i
, row
, col
;
256 struct kinfo_cputime diff_cp_time
;
259 diff_cp_time
.cp_user
= cp_time
.cp_user
- old_cp_time
.cp_user
;
260 diff_cp_time
.cp_nice
= cp_time
.cp_nice
- old_cp_time
.cp_nice
;
261 diff_cp_time
.cp_sys
= cp_time
.cp_sys
- old_cp_time
.cp_sys
;
262 diff_cp_time
.cp_intr
= cp_time
.cp_intr
- old_cp_time
.cp_intr
;
263 diff_cp_time
.cp_idle
= cp_time
.cp_idle
- old_cp_time
.cp_idle
;
264 old_cp_time
= cp_time
;
267 cp_total
= diff_cp_time
.cp_user
+ diff_cp_time
.cp_nice
+
268 diff_cp_time
.cp_sys
+ diff_cp_time
.cp_intr
+ diff_cp_time
.cp_idle
;
269 stat1(row
++, diff_cp_time
.cp_user
, cp_total
);
270 stat1(row
++, diff_cp_time
.cp_nice
, cp_total
);
271 stat1(row
++, diff_cp_time
.cp_sys
, cp_total
);
272 stat1(row
++, diff_cp_time
.cp_intr
, cp_total
);
273 stat1(row
++, diff_cp_time
.cp_idle
, cp_total
);
276 for (i
= 0; i
< num_devices
; i
++)
277 if (dev_select
[i
].selected
) {
278 if (row
> wnd
->_maxy
- linesperregion
)
280 row
= devstats(row
, INSET
, i
);
285 wmove(wnd
, row
+ linesperregion
, 0);
287 wmove(wnd
, row
+ 3, 0);
289 for (i
= 0; i
< num_devices
; i
++)
290 if (dev_select
[i
].selected
) {
291 if (col
+ COLWIDTH
>= wnd
->_maxx
- INSET
) {
292 col
= INSET
, row
+= linesperregion
+ 1;
293 if (row
> wnd
->_maxy
- (linesperregion
+ 1))
295 wmove(wnd
, row
+ linesperregion
, 0);
297 wmove(wnd
, row
+ 3, 0);
300 (void) devstats(row
+ 3, col
, i
);
306 devstats(int row
, int col
, int dn
)
308 long double transfers_per_second
;
309 long double kb_per_transfer
, mb_per_second
;
310 long double busy_seconds
;
313 di
= dev_select
[dn
].position
;
315 busy_seconds
= compute_etime(cur
.busy_time
, last
.busy_time
);
317 if (compute_stats(&cur
.dinfo
->devices
[di
], &last
.dinfo
->devices
[di
],
318 busy_seconds
, NULL
, NULL
, NULL
,
319 &kb_per_transfer
, &transfers_per_second
,
320 &mb_per_second
, NULL
, NULL
) != 0)
321 errx(1, "%s", devstat_errbuf
);
324 mvwprintw(wnd
, row
, col
, " %5.2Lf %3.0Lf %5.2Lf ",
325 kb_per_transfer
, transfers_per_second
,
329 wmove(wnd
, row
++, col
);
330 histogram(mb_per_second
, 50, .5);
331 wmove(wnd
, row
++, col
);
332 histogram(transfers_per_second
, 50, .5);
334 wmove(wnd
, row
++, col
);
335 histogram(kb_per_transfer
, 50, .5);
343 stat1(int row
, uint64_t difference
, uint64_t total
)
349 time
= 100.0 * difference
/ total
;
352 wmove(wnd
, row
, INSET
);
354 histogram(time
, 50, CPUSCALE
);
358 histogram(long double val
, int colwidth
, double scale
)
362 register int v
= (int)(val
* scale
) + 0.5;
366 k
= MIN(v
, colwidth
);
368 snprintf(buf
, sizeof(buf
), "%5.2Lf", val
);
381 cmdiostat(char *cmd
, char *args
)
384 if (prefix(cmd
, "kbpt"))
386 else if (prefix(cmd
, "numbers"))
388 else if (prefix(cmd
, "bars"))
390 else if (!dscmd(cmd
, args
, 100, &cur
))