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