1 /* $NetBSD: iostat.c,v 1.36 2006/04/14 13:14:06 blymn Exp $ */
4 * Copyright (c) 1980, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)iostat.c 8.1 (Berkeley) 6/6/93";
37 __RCSID("$NetBSD: iostat.c,v 1.36 2006/04/14 13:14:06 blymn Exp $");
40 #include <sys/param.h>
48 static int linesperregion
;
50 static int numbers
= 0; /* default display bar graphs */
51 static int secs
= 0; /* default seconds shown */
52 static int read_write
= 0; /* default read/write shown */
54 static int barlabels(int);
55 static void histogram(double, int, double);
56 static int numlabels(int);
57 static int stats(int, int, int);
58 static void stat1(int, int);
65 return (subwin(stdscr
, -1, 0, 5, 0));
69 closeiostat(WINDOW
*w
)
107 error("No drives defined.");
111 wmove(wnd
, row
, 0); wclrtobot(wnd
);
112 mvwaddstr(wnd
, row
++, INSET
,
113 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
114 mvwaddstr(wnd
, row
++, 0, " CPU user|");
115 mvwaddstr(wnd
, row
++, 0, " nice|");
116 mvwaddstr(wnd
, row
++, 0, " system|");
117 mvwaddstr(wnd
, row
++, 0, " interrupt|");
118 mvwaddstr(wnd
, row
++, 0, " idle|");
120 row
= numlabels(row
+ 1);
122 row
= barlabels(row
+ 1);
131 #define COLWIDTH (9 + secs * 5 + 1 + read_write * 9 + 1)
132 #define DRIVESPERLINE ((getmaxx(wnd) + 1) / COLWIDTH)
133 for (ndrives
= 0, i
= 0; i
< ndrive
; i
++)
137 regions
= howmany(ndrives
, DRIVESPERLINE
);
139 * Deduct -regions for blank line after each scrolling region.
141 linesperregion
= (getmaxy(wnd
) - row
- regions
+ 1) / regions
;
143 * Minimum region contains space for two
144 * label lines and one line of statistics.
146 if (linesperregion
< 3)
149 for (i
= 0; i
< ndrive
; i
++)
151 if (col
+ COLWIDTH
- 1 > getmaxx(wnd
)) {
152 col
= 0, row
+= linesperregion
+ 1;
153 if (row
> getmaxy(wnd
) - (linesperregion
))
157 mvwprintw(wnd
, row
, col
+ 5, "%s", cur
.name
[i
]);
160 mvwprintw(wnd
, row
, col
+ 11 + secs
* 5,
162 mvwprintw(wnd
, row
+ 1, col
, " kBps %s",
163 read_write
? "r/s" : "tps");
165 waddstr(wnd
, " sec");
167 waddstr(wnd
, " kBps w/s");
171 row
+= linesperregion
+ 1;
180 mvwaddstr(wnd
, row
++, INSET
,
181 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
182 linesperregion
= 2 + secs
+ (read_write
? 2 : 0);
183 for (i
= 0; i
< ndrive
; i
++) {
185 if (row
> getmaxy(wnd
) - linesperregion
)
187 mvwprintw(wnd
, row
++, 0, "%7.7s kBps|",
189 mvwaddstr(wnd
, row
++, 0, " tps|");
191 mvwprintw(wnd
, row
++, 0, " (write) kBps|");
192 mvwaddstr(wnd
, row
++, 0, " tps|");
195 mvwaddstr(wnd
, row
++, 0, " msec|");
213 etime
= cur
.cp_etime
;
217 * Interrupt CPU state not calculated yet.
219 for (i
= 0; i
< CPUSTATES
; i
++)
223 for (i
= 0; i
< ndrive
; i
++)
225 if (row
> getmaxy(wnd
) - linesperregion
)
227 row
= stats(row
, INSET
, i
);
232 wmove(wnd
, row
+ linesperregion
, 0);
234 wmove(wnd
, row
+ 3, 0);
236 for (i
= 0; i
< ndrive
; i
++)
238 if (col
+ COLWIDTH
- 1 > getmaxx(wnd
)) {
239 col
= 0, row
+= linesperregion
+ 1;
240 if (row
> getmaxy(wnd
) - (linesperregion
+ 1))
242 wmove(wnd
, row
+ linesperregion
, 0);
244 wmove(wnd
, row
+ 3, 0);
247 (void) stats(row
+ 3, col
, i
);
253 stats(int row
, int col
, int dn
)
255 double atime
, rwords
, wwords
;
258 /* time busy in disk activity */
259 atime
= (double)cur
.time
[dn
].tv_sec
+
260 ((double)cur
.time
[dn
].tv_usec
/ (double)1000000);
262 /* # of k transferred */
263 rwords
= cur
.rbytes
[dn
] / 1024.0;
264 wwords
= cur
.wbytes
[dn
] / 1024.0;
265 rxfer
= cur
.rxfer
[dn
];
268 rxfer
+= cur
.wxfer
[dn
];
271 mvwprintw(wnd
, row
, col
, "%5.0f%4.0f",
272 rwords
/ etime
, rxfer
/ etime
);
274 wprintw(wnd
, "%5.1f", atime
/ etime
);
276 wprintw(wnd
, " %5.0f%4.0f",
277 wwords
/ etime
, cur
.wxfer
[dn
] / etime
);
281 wmove(wnd
, row
++, col
);
282 histogram(rwords
/ etime
, 50, 0.5);
283 wmove(wnd
, row
++, col
);
284 histogram(rxfer
/ etime
, 50, 0.5);
286 wmove(wnd
, row
++, col
);
287 histogram(wwords
/ etime
, 50, 0.5);
288 wmove(wnd
, row
++, col
);
289 histogram(cur
.wxfer
[dn
] / etime
, 50, 0.5);
293 wmove(wnd
, row
++, col
);
294 atime
*= 1000; /* In milliseconds */
295 histogram(atime
/ etime
, 50, 0.5);
301 stat1(int row
, int o
)
307 for (i
= 0; i
< CPUSTATES
; i
++)
308 total_time
+= cur
.cp_time
[i
];
309 if (total_time
== 0.0)
311 wmove(wnd
, row
, INSET
);
313 histogram(100.0 * cur
.cp_time
[o
] / total_time
, 50, CPUSCALE
);
317 histogram(double val
, int colwidth
, double scale
)
319 int v
= (int)(val
* scale
+ 0.5);
323 while (v
> colwidth
) {
331 mvwprintw(wnd
, y
, x
+ colwidth
+ 1, "* %d ", factor
);
335 iostat_bars(char *args
)
344 iostat_numbers(char *args
)
353 iostat_secs(char *args
)
362 iostat_rw(char *args
)
371 iostat_all(char *args
)