iostat.c revision 1.20
1/*	$OpenBSD: iostat.c,v 1.20 2003/02/02 18:20:41 henning Exp $	*/
2/*	$NetBSD: iostat.c,v 1.5 1996/05/10 23:16:35 thorpej Exp $	*/
3
4/*
5 * Copyright (c) 1980, 1992, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38#if 0
39static char sccsid[] = "@(#)iostat.c	8.1 (Berkeley) 6/6/93";
40#endif
41static char rcsid[] = "$OpenBSD: iostat.c,v 1.20 2003/02/02 18:20:41 henning Exp $";
42#endif /* not lint */
43
44#include <sys/param.h>
45#include <sys/dkstat.h>
46#include <sys/buf.h>
47#include <sys/time.h>
48
49#include <string.h>
50#include <stdlib.h>
51#include <paths.h>
52#include "systat.h"
53#include "extern.h"
54
55#include "dkstats.h"
56extern struct _disk	cur, last;
57
58static  int linesperregion;
59static  double etime;
60static  int numbers = 0;		/* default display bar graphs */
61static  int secs = 0;			/* default seconds shown */
62
63static int barlabels(int);
64static void histogram(double, int, double);
65static int numlabels(int);
66static int stats(int, int, int);
67static void stat1(int, int);
68
69
70WINDOW *
71openiostat(void)
72{
73	return (subwin(stdscr, LINES-1-5, 0, 5, 0));
74}
75
76void
77closeiostat(WINDOW *w)
78{
79	if (w == NULL)
80		return;
81	wclear(w);
82	wrefresh(w);
83	delwin(w);
84}
85
86int
87initiostat(void)
88{
89	dkinit(1);
90	dkreadstats();
91	return (1);
92}
93
94void
95fetchiostat(void)
96{
97	if (cur.dk_ndrive == 0)
98		return;
99	dkreadstats();
100}
101
102#define	INSET	10
103
104void
105labeliostat(void)
106{
107	int row;
108
109	row = 0;
110	wmove(wnd, row, 0); wclrtobot(wnd);
111	mvwaddstr(wnd, row++, INSET,
112	    "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
113	mvwaddstr(wnd, row++, 0, "cpu  user|");
114	mvwaddstr(wnd, row++, 0, "     nice|");
115	mvwaddstr(wnd, row++, 0, "   system|");
116	mvwaddstr(wnd, row++, 0, "interrupt|");
117	mvwaddstr(wnd, row++, 0, "     idle|");
118	if (numbers)
119		row = numlabels(row + 1);
120	else
121		row = barlabels(row + 1);
122}
123
124static int
125numlabels(int row)
126{
127	int i, col, regions, ndrives;
128
129	if (cur.dk_ndrive == 0) {
130		mvwaddstr(wnd, row++, INSET, "No drives attached.");
131		return (row);
132	}
133#define COLWIDTH	17
134#define DRIVESPERLINE	((wnd->_maxx - INSET) / COLWIDTH)
135	for (ndrives = 0, i = 0; i < cur.dk_ndrive; i++)
136		if (cur.dk_select[i])
137			ndrives++;
138	regions = howmany(ndrives, DRIVESPERLINE);
139	/*
140	 * Deduct -regions for blank line after each scrolling region.
141	 */
142	linesperregion = (wnd->_maxy - row - regions) / regions;
143	/*
144	 * Minimum region contains space for two
145	 * label lines and one line of statistics.
146	 */
147	if (linesperregion < 3)
148		linesperregion = 3;
149	col = INSET;
150	for (i = 0; i < cur.dk_ndrive; i++)
151		if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) {
152			if (col + COLWIDTH >= wnd->_maxx) {
153				col = INSET, row += linesperregion + 1;
154				if (row > wnd->_maxy - (linesperregion + 1))
155					break;
156			}
157			mvwaddstr(wnd, row, col + 4, cur.dk_name[i]);
158			mvwaddstr(wnd, row + 1, col, " KBps tps  sec");
159			col += COLWIDTH;
160		}
161	if (col)
162		row += linesperregion + 1;
163	return (row);
164}
165
166static int
167barlabels(int row)
168{
169	int i;
170
171	if (cur.dk_ndrive == 0) {
172		mvwaddstr(wnd, row++, INSET, "No drives attached.");
173		return (row);
174	}
175	mvwaddstr(wnd, row++, INSET,
176	    "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
177	linesperregion = 2 + secs;
178	for (i = 0; i < dk_ndrive; i++)
179		if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) {
180			if (row > wnd->_maxy - linesperregion)
181				break;
182			mvwprintw(wnd, row++, 0, "%4.4s  Kps|", cur.dk_name[i]);
183			mvwaddstr(wnd, row++, 0, "      tps|");
184			if (secs)
185				mvwaddstr(wnd, row++, 0, "     msec|");
186		}
187	return (row);
188}
189
190
191void
192showiostat(void)
193{
194	int i, row, col;
195
196	dkswap();
197
198	etime = 0;
199	for (i = 0; i < CPUSTATES; i++) {
200		etime += cur.cp_time[i];
201	}
202	if (etime == 0.0)
203		etime = 1.0;
204	etime /= (float) hz;
205	row = 1;
206
207	/*
208	 * Interrupt CPU state not calculated yet.
209	 */
210	for (i = 0; i < CPUSTATES; i++)
211		stat1(row++, i);
212
213	if (last.dk_ndrive != cur.dk_ndrive)
214		labeliostat();
215
216	if (cur.dk_ndrive == 0)
217		return;
218
219	if (!numbers) {
220		row += 2;
221		for (i = 0; i < cur.dk_ndrive; i++)
222			if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) {
223				if (row > wnd->_maxy - linesperregion)
224					break;
225				row = stats(row, INSET, i);
226			}
227		return;
228	}
229	col = INSET;
230	wmove(wnd, row + linesperregion, 0);
231	wdeleteln(wnd);
232	wmove(wnd, row + 3, 0);
233	winsertln(wnd);
234	for (i = 0; i < cur.dk_ndrive; i++)
235		if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) {
236			if (col + COLWIDTH >= wnd->_maxx) {
237				col = INSET, row += linesperregion + 1;
238				if (row > wnd->_maxy - (linesperregion + 1))
239					break;
240				wmove(wnd, row + linesperregion, 0);
241				wdeleteln(wnd);
242				wmove(wnd, row + 3, 0);
243				winsertln(wnd);
244			}
245			(void) stats(row + 3, col, i);
246			col += COLWIDTH;
247		}
248}
249
250static int
251stats(int row, int col, int dn)
252{
253	double atime, words;
254
255	/* time busy in disk activity */
256	atime = (double)cur.dk_time[dn].tv_sec +
257		((double)cur.dk_time[dn].tv_usec / (double)1000000);
258
259	words = cur.dk_bytes[dn] / 1024.0;	/* # of K transferred */
260	if (numbers) {
261		mvwprintw(wnd, row, col, "%5.0f%4.0f%5.1f",
262		    words / etime, cur.dk_xfer[dn] / etime, atime / etime);
263		return (row);
264	}
265	wmove(wnd, row++, col);
266	histogram(words / etime, 50, 0.5);
267	wmove(wnd, row++, col);
268	histogram(cur.dk_xfer[dn] / etime, 50, 0.5);
269	if (secs) {
270		wmove(wnd, row++, col);
271		atime *= 1000;	/* In milliseconds */
272		histogram(atime / etime, 50, 0.5);
273	}
274	return (row);
275}
276
277static void
278stat1(int row, int o)
279{
280	int i;
281	double time;
282
283	time = 0;
284	for (i = 0; i < CPUSTATES; i++)
285		time += cur.cp_time[i];
286	if (time == 0.0)
287		time = 1.0;
288	wmove(wnd, row, INSET);
289#define CPUSCALE	0.5
290	histogram(100.0 * cur.cp_time[o] / time, 50, CPUSCALE);
291}
292
293static void
294histogram(double val, int colwidth, double scale)
295{
296	int v = (int)(val * scale + 0.5);
297	int factor = 1;
298	int y, x;
299
300	while (v > colwidth) {
301		v = (v + 5) / 10;
302		factor *= 10;
303	}
304	getyx(wnd, y, x);
305	wclrtoeol(wnd);
306	whline(wnd, 'X', v);
307	if (factor != 1)
308		mvwprintw(wnd, y, x + colwidth + 1, "* %d ", factor);
309}
310
311int
312cmdiostat(char *cmd, char *args)
313{
314
315	if (prefix(cmd, "secs"))
316		secs = !secs;
317	else if (prefix(cmd, "numbers"))
318		numbers = 1;
319	else if (prefix(cmd, "bars"))
320		numbers = 0;
321	else if (!dkcmd(cmd, args))
322		return (0);
323	wclear(wnd);
324	labeliostat();
325	refresh();
326	return (1);
327}
328