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
71 struct statinfo cur
, last
;
72 static struct kinfo_cputime cp_time
;
74 static int linesperregion
;
75 static int numbers
= 0; /* default display bar graphs */
76 static int kbpt
= 0; /* default ms/seek shown */
78 static int barlabels(int);
79 static void histogram(long double, int, double);
80 static int numlabels(int);
81 static int devstats(int, int, int);
82 static void stat1(int, uint64_t, uint64_t);
87 return (subwin(stdscr
, LINES
-1-5, 0, 5, 0));
91 closeiostat(WINDOW
*w
)
103 if ((num_devices
= getnumdevs()) < 0)
106 cur
.dinfo
= (struct devinfo
*)malloc(sizeof(struct devinfo
));
107 last
.dinfo
= (struct devinfo
*)malloc(sizeof(struct devinfo
));
108 bzero(cur
.dinfo
, sizeof(struct devinfo
));
109 bzero(last
.dinfo
, sizeof(struct devinfo
));
112 * This value for maxshowdevs (100) is bogus. I'm not sure exactly
113 * how to calculate it, though.
115 if (dsinit(100, &cur
, &last
, NULL
) != 1)
124 struct devinfo
*tmp_dinfo
;
126 if (kinfo_get_sched_cputime(&cp_time
))
127 err(1, "kinfo_get_sched_cputime");
128 tmp_dinfo
= last
.dinfo
;
129 last
.dinfo
= cur
.dinfo
;
130 cur
.dinfo
= tmp_dinfo
;
132 last
.busy_time
= cur
.busy_time
;
135 * Here what we want to do is refresh our device stats.
136 * getdevs() returns 1 when the device list has changed.
137 * If the device list has changed, we want to go through
138 * the selection process again, in case a device that we
139 * were previously displaying has gone away.
141 switch (getdevs(&cur
)) {
143 errx(1, "%s", devstat_errbuf
);
146 cmdiostat("refresh", NULL
);
151 num_devices
= cur
.dinfo
->numdevs
;
152 generation
= cur
.dinfo
->generation
;
164 wmove(wnd
, row
, 0); wclrtobot(wnd
);
165 mvwaddstr(wnd
, row
++, INSET
,
166 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
167 mvwaddstr(wnd
, row
++, 0, "cpu user|");
168 mvwaddstr(wnd
, row
++, 0, " nice|");
169 mvwaddstr(wnd
, row
++, 0, " system|");
170 mvwaddstr(wnd
, row
++, 0, "interrupt|");
171 mvwaddstr(wnd
, row
++, 0, " idle|");
173 row
= numlabels(row
+ 1);
175 row
= barlabels(row
+ 1);
181 int i
, _col
, regions
, ndrives
;
185 #define DRIVESPERLINE ((wnd->_maxx - INSET) / COLWIDTH)
186 for (ndrives
= 0, i
= 0; i
< num_devices
; i
++)
187 if (dev_select
[i
].selected
)
189 regions
= howmany(ndrives
, DRIVESPERLINE
);
191 * Deduct -regions for blank line after each scrolling region.
193 linesperregion
= (wnd
->_maxy
- row
- regions
) / regions
;
195 * Minimum region contains space for two
196 * label lines and one line of statistics.
198 if (linesperregion
< 3)
201 for (i
= 0; i
< num_devices
; i
++)
202 if (dev_select
[i
].selected
) {
203 if (_col
+ COLWIDTH
>= wnd
->_maxx
- INSET
) {
204 _col
= INSET
, row
+= linesperregion
+ 1;
205 if (row
> wnd
->_maxy
- (linesperregion
+ 1))
208 sprintf(tmpstr
, "%.6s%d", dev_select
[i
].device_name
,
209 dev_select
[i
].unit_number
);
210 mvwaddstr(wnd
, row
, _col
+ 4, tmpstr
);
211 mvwaddstr(wnd
, row
+ 1, _col
, " KB/t tps MB/s ");
215 row
+= linesperregion
+ 1;
225 mvwaddstr(wnd
, row
++, INSET
,
226 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
227 linesperregion
= 2 + kbpt
;
228 for (i
= 0; i
< num_devices
; i
++)
229 if (dev_select
[i
].selected
) {
230 if (row
> wnd
->_maxy
- linesperregion
)
232 sprintf(tmpstr
, "%.4s%d", dev_select
[i
].device_name
,
233 dev_select
[i
].unit_number
);
234 mvwprintw(wnd
, row
++, 0, "%-5.5s MB/s|",
236 mvwaddstr(wnd
, row
++, 0, " tps|");
238 mvwaddstr(wnd
, row
++, 0, " KB/t|");
248 struct kinfo_cputime diff_cp_time
;
251 diff_cp_time
.cp_user
= cp_time
.cp_user
- old_cp_time
.cp_user
;
252 diff_cp_time
.cp_nice
= cp_time
.cp_nice
- old_cp_time
.cp_nice
;
253 diff_cp_time
.cp_sys
= cp_time
.cp_sys
- old_cp_time
.cp_sys
;
254 diff_cp_time
.cp_intr
= cp_time
.cp_intr
- old_cp_time
.cp_intr
;
255 diff_cp_time
.cp_idle
= cp_time
.cp_idle
- old_cp_time
.cp_idle
;
256 old_cp_time
= cp_time
;
259 cp_total
= diff_cp_time
.cp_user
+ diff_cp_time
.cp_nice
+
260 diff_cp_time
.cp_sys
+ diff_cp_time
.cp_intr
+ diff_cp_time
.cp_idle
;
261 stat1(row
++, diff_cp_time
.cp_user
, cp_total
);
262 stat1(row
++, diff_cp_time
.cp_nice
, cp_total
);
263 stat1(row
++, diff_cp_time
.cp_sys
, cp_total
);
264 stat1(row
++, diff_cp_time
.cp_intr
, cp_total
);
265 stat1(row
++, diff_cp_time
.cp_idle
, cp_total
);
268 for (i
= 0; i
< num_devices
; i
++)
269 if (dev_select
[i
].selected
) {
270 if (row
> wnd
->_maxy
- linesperregion
)
272 row
= devstats(row
, INSET
, i
);
277 wmove(wnd
, row
+ linesperregion
, 0);
279 wmove(wnd
, row
+ 3, 0);
281 for (i
= 0; i
< num_devices
; i
++)
282 if (dev_select
[i
].selected
) {
283 if (_col
+ COLWIDTH
>= wnd
->_maxx
- INSET
) {
284 _col
= INSET
, row
+= linesperregion
+ 1;
285 if (row
> wnd
->_maxy
- (linesperregion
+ 1))
287 wmove(wnd
, row
+ linesperregion
, 0);
289 wmove(wnd
, row
+ 3, 0);
292 (void) devstats(row
+ 3, _col
, i
);
298 devstats(int row
, int _col
, int dn
)
300 long double transfers_per_second
;
301 long double kb_per_transfer
, mb_per_second
;
302 long double busy_seconds
;
305 di
= dev_select
[dn
].position
;
307 busy_seconds
= compute_etime(cur
.busy_time
, last
.busy_time
);
309 if (compute_stats(&cur
.dinfo
->devices
[di
], &last
.dinfo
->devices
[di
],
310 busy_seconds
, NULL
, NULL
, NULL
,
311 &kb_per_transfer
, &transfers_per_second
,
312 &mb_per_second
, NULL
, NULL
) != 0)
313 errx(1, "%s", devstat_errbuf
);
316 mvwprintw(wnd
, row
, _col
, " %5.2Lf %3.0Lf %5.2Lf ",
317 kb_per_transfer
, transfers_per_second
,
321 wmove(wnd
, row
++, _col
);
322 histogram(mb_per_second
, 50, .5);
323 wmove(wnd
, row
++, _col
);
324 histogram(transfers_per_second
, 50, .5);
326 wmove(wnd
, row
++, _col
);
327 histogram(kb_per_transfer
, 50, .5);
335 stat1(int row
, uint64_t difference
, uint64_t total
)
340 dtime
= 100.0 * difference
/ total
;
343 wmove(wnd
, row
, INSET
);
345 histogram(dtime
, 50, CPUSCALE
);
349 histogram(long double val
, int colwidth
, double scale
)
353 int v
= (int)(val
* scale
) + 0.5;
357 k
= MIN(v
, colwidth
);
359 snprintf(buf
, sizeof(buf
), "%5.2Lf", val
);
372 cmdiostat(const char *cmd
, char *args
)
374 if (prefix(cmd
, "kbpt"))
376 else if (prefix(cmd
, "numbers"))
378 else if (prefix(cmd
, "bars"))
380 else if (!dscmd(cmd
, args
, 100, &cur
))