xref: /dragonfly/usr.bin/systat/iostat.c (revision 984263bc)
1 /*
2  * Copyright (c) 1998 Kenneth D. Merry.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD: src/usr.bin/systat/iostat.c,v 1.9.2.1 2000/07/02 10:03:17 ps Exp $
29  */
30 /*
31  * Copyright (c) 1980, 1992, 1993
32  *	The Regents of the University of California.  All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
37  * 1. Redistributions of source code must retain the above copyright
38  *    notice, this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright
40  *    notice, this list of conditions and the following disclaimer in the
41  *    documentation and/or other materials provided with the distribution.
42  * 3. All advertising materials mentioning features or use of this software
43  *    must display the following acknowledgement:
44  *	This product includes software developed by the University of
45  *	California, Berkeley and its contributors.
46  * 4. Neither the name of the University nor the names of its contributors
47  *    may be used to endorse or promote products derived from this software
48  *    without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  */
62 
63 #ifndef lint
64 static char sccsid[] = "@(#)iostat.c	8.1 (Berkeley) 6/6/93";
65 #endif not lint
66 
67 #include <sys/param.h>
68 #include <sys/dkstat.h>
69 
70 #include <string.h>
71 #include <stdlib.h>
72 #include <nlist.h>
73 #include <paths.h>
74 #include <devstat.h>
75 #include <err.h>
76 #include "systat.h"
77 #include "extern.h"
78 #include "devs.h"
79 
80 static struct nlist namelist[] = {
81 #define X_CP_TIME	0
82 	{ "_cp_time" },
83 #ifdef vax
84 #define X_MBDINIT	(X_CP_TIME+1)
85 	{ "_mbdinit" },
86 #define X_UBDINIT	(X_CP_TIME+2)
87 	{ "_ubdinit" },
88 #endif
89 #ifdef tahoe
90 #define	X_VBDINIT	(X_CP_TIME+1)
91 	{ "_vbdinit" },
92 #endif
93 	{ "" },
94 };
95 
96 struct statinfo cur, last;
97 
98 static  int linesperregion;
99 static  double etime;
100 static  int numbers = 0;		/* default display bar graphs */
101 static  int kbpt = 0;			/* default ms/seek shown */
102 
103 static int barlabels __P((int));
104 static void histogram __P((long double, int, double));
105 static int numlabels __P((int));
106 static int devstats __P((int, int, int));
107 static void stat1 __P((int, int));
108 
109 WINDOW *
110 openiostat()
111 {
112 	return (subwin(stdscr, LINES-1-5, 0, 5, 0));
113 }
114 
115 void
116 closeiostat(w)
117 	WINDOW *w;
118 {
119 	if (w == NULL)
120 		return;
121 	wclear(w);
122 	wrefresh(w);
123 	delwin(w);
124 }
125 
126 int
127 initiostat()
128 {
129 	if (num_devices = getnumdevs() < 0)
130 		return(0);
131 
132 	cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
133 	last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
134 	bzero(cur.dinfo, sizeof(struct devinfo));
135 	bzero(last.dinfo, sizeof(struct devinfo));
136 
137 	/*
138 	 * This value for maxshowdevs (100) is bogus.  I'm not sure exactly
139 	 * how to calculate it, though.
140 	 */
141 	if (dsinit(100, &cur, &last, NULL) != 1)
142 		return(0);
143 
144 	if (kvm_nlist(kd, namelist)) {
145        		nlisterr(namelist);
146 		return(0);
147 	}
148 
149 	return(1);
150 }
151 
152 void
153 fetchiostat()
154 {
155 	struct devinfo *tmp_dinfo;
156 
157 	NREAD(X_CP_TIME, cur.cp_time, sizeof(cur.cp_time));
158 	tmp_dinfo = last.dinfo;
159 	last.dinfo = cur.dinfo;
160 	cur.dinfo = tmp_dinfo;
161 
162 	last.busy_time = cur.busy_time;
163 
164 	/*
165 	 * Here what we want to do is refresh our device stats.
166 	 * getdevs() returns 1 when the device list has changed.
167 	 * If the device list has changed, we want to go through
168 	 * the selection process again, in case a device that we
169 	 * were previously displaying has gone away.
170 	 */
171 	switch (getdevs(&cur)) {
172 	case -1:
173 		errx(1, "%s", devstat_errbuf);
174 		break;
175 	case 1:
176 		cmdiostat("refresh", NULL);
177 		break;
178 	default:
179 		break;
180 	}
181 	num_devices = cur.dinfo->numdevs;
182 	generation = cur.dinfo->generation;
183 
184 }
185 
186 #define	INSET	10
187 
188 void
189 labeliostat()
190 {
191 	int row;
192 
193 	row = 0;
194 	wmove(wnd, row, 0); wclrtobot(wnd);
195 	mvwaddstr(wnd, row++, INSET,
196 	    "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
197 	mvwaddstr(wnd, row++, 0, "cpu  user|");
198 	mvwaddstr(wnd, row++, 0, "     nice|");
199 	mvwaddstr(wnd, row++, 0, "   system|");
200 	mvwaddstr(wnd, row++, 0, "interrupt|");
201 	mvwaddstr(wnd, row++, 0, "     idle|");
202 	if (numbers)
203 		row = numlabels(row + 1);
204 	else
205 		row = barlabels(row + 1);
206 }
207 
208 static int
209 numlabels(row)
210 	int row;
211 {
212 	int i, col, regions, ndrives;
213 	char tmpstr[10];
214 
215 #define COLWIDTH	17
216 #define DRIVESPERLINE	((wnd->_maxx - INSET) / COLWIDTH)
217 	for (ndrives = 0, i = 0; i < num_devices; i++)
218 		if (dev_select[i].selected)
219 			ndrives++;
220 	regions = howmany(ndrives, DRIVESPERLINE);
221 	/*
222 	 * Deduct -regions for blank line after each scrolling region.
223 	 */
224 	linesperregion = (wnd->_maxy - row - regions) / regions;
225 	/*
226 	 * Minimum region contains space for two
227 	 * label lines and one line of statistics.
228 	 */
229 	if (linesperregion < 3)
230 		linesperregion = 3;
231 	col = INSET;
232 	for (i = 0; i < num_devices; i++)
233 		if (dev_select[i].selected) {
234 			if (col + COLWIDTH >= wnd->_maxx - INSET) {
235 				col = INSET, row += linesperregion + 1;
236 				if (row > wnd->_maxy - (linesperregion + 1))
237 					break;
238 			}
239 			sprintf(tmpstr, "%s%d", dev_select[i].device_name,
240 				dev_select[i].unit_number);
241 			mvwaddstr(wnd, row, col + 4, tmpstr);
242 			mvwaddstr(wnd, row + 1, col, "  KB/t tps  MB/s ");
243 			col += COLWIDTH;
244 		}
245 	if (col)
246 		row += linesperregion + 1;
247 	return (row);
248 }
249 
250 static int
251 barlabels(row)
252 	int row;
253 {
254 	int i;
255 	char tmpstr[10];
256 
257 	mvwaddstr(wnd, row++, INSET,
258 	    "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
259 	linesperregion = 2 + kbpt;
260 	for (i = 0; i < num_devices; i++)
261 		if (dev_select[i].selected) {
262 			if (row > wnd->_maxy - linesperregion)
263 				break;
264 			sprintf(tmpstr, "%s%d", dev_select[i].device_name,
265 				dev_select[i].unit_number);
266 			mvwprintw(wnd, row++, 0, "%-5.5s MB/s|",
267 				  tmpstr);
268 			mvwaddstr(wnd, row++, 0, "      tps|");
269 			if (kbpt)
270 				mvwaddstr(wnd, row++, 0, "     KB/t|");
271 		}
272 	return (row);
273 }
274 
275 
276 void
277 showiostat()
278 {
279 	register long t;
280 	register int i, row, col;
281 
282 #define X(fld)	t = cur.fld[i]; cur.fld[i] -= last.fld[i]; last.fld[i] = t
283 	etime = 0;
284 	for(i = 0; i < CPUSTATES; i++) {
285 		X(cp_time);
286 		etime += cur.cp_time[i];
287 	}
288 	if (etime == 0.0)
289 		etime = 1.0;
290 	etime /= hertz;
291 	row = 1;
292 	for (i = 0; i < CPUSTATES; i++)
293 		stat1(row++, i);
294 	if (!numbers) {
295 		row += 2;
296 		for (i = 0; i < num_devices; i++)
297 			if (dev_select[i].selected) {
298 				if (row > wnd->_maxy - linesperregion)
299 					break;
300 				row = devstats(row, INSET, i);
301 			}
302 		return;
303 	}
304 	col = INSET;
305 	wmove(wnd, row + linesperregion, 0);
306 	wdeleteln(wnd);
307 	wmove(wnd, row + 3, 0);
308 	winsertln(wnd);
309 	for (i = 0; i < num_devices; i++)
310 		if (dev_select[i].selected) {
311 			if (col + COLWIDTH >= wnd->_maxx - INSET) {
312 				col = INSET, row += linesperregion + 1;
313 				if (row > wnd->_maxy - (linesperregion + 1))
314 					break;
315 				wmove(wnd, row + linesperregion, 0);
316 				wdeleteln(wnd);
317 				wmove(wnd, row + 3, 0);
318 				winsertln(wnd);
319 			}
320 			(void) devstats(row + 3, col, i);
321 			col += COLWIDTH;
322 		}
323 }
324 
325 static int
326 devstats(row, col, dn)
327 	int row, col, dn;
328 {
329 	long double transfers_per_second;
330 	long double kb_per_transfer, mb_per_second;
331 	long double busy_seconds;
332 	int di;
333 
334 	di = dev_select[dn].position;
335 
336 	busy_seconds = compute_etime(cur.busy_time, last.busy_time);
337 
338 	if (compute_stats(&cur.dinfo->devices[di], &last.dinfo->devices[di],
339 			  busy_seconds, NULL, NULL, NULL,
340 			  &kb_per_transfer, &transfers_per_second,
341 			  &mb_per_second, NULL, NULL) != 0)
342 		errx(1, "%s", devstat_errbuf);
343 
344 	if (numbers) {
345 		mvwprintw(wnd, row, col, " %5.2Lf %3.0Lf %5.2Lf ",
346 			 kb_per_transfer, transfers_per_second,
347 			 mb_per_second);
348 		return(row);
349 	}
350 	wmove(wnd, row++, col);
351 	histogram(mb_per_second, 50, .5);
352 	wmove(wnd, row++, col);
353 	histogram(transfers_per_second, 50, .5);
354 	if (kbpt) {
355 		wmove(wnd, row++, col);
356 		histogram(kb_per_transfer, 50, .5);
357 	}
358 
359 	return(row);
360 
361 }
362 
363 static void
364 stat1(row, o)
365 	int row, o;
366 {
367 	register int i;
368 	double time;
369 
370 	time = 0;
371 	for (i = 0; i < CPUSTATES; i++)
372 		time += cur.cp_time[i];
373 	if (time == 0.0)
374 		time = 1.0;
375 	wmove(wnd, row, INSET);
376 #define CPUSCALE	0.5
377 	histogram(100.0 * cur.cp_time[o] / time, 50, CPUSCALE);
378 }
379 
380 static void
381 histogram(val, colwidth, scale)
382 	long double val;
383 	int colwidth;
384 	double scale;
385 {
386 	char buf[10];
387 	register int k;
388 	register int v = (int)(val * scale) + 0.5;
389 
390 	k = MIN(v, colwidth);
391 	if (v > colwidth) {
392 		snprintf(buf, sizeof(buf), "%5.2Lf", val);
393 		k -= strlen(buf);
394 		while (k--)
395 			waddch(wnd, 'X');
396 		waddstr(wnd, buf);
397 		return;
398 	}
399 	while (k--)
400 		waddch(wnd, 'X');
401 	wclrtoeol(wnd);
402 }
403 
404 int
405 cmdiostat(cmd, args)
406 	char *cmd, *args;
407 {
408 
409 	if (prefix(cmd, "kbpt"))
410 		kbpt = !kbpt;
411 	else if (prefix(cmd, "numbers"))
412 		numbers = 1;
413 	else if (prefix(cmd, "bars"))
414 		numbers = 0;
415 	else if (!dscmd(cmd, args, 100, &cur))
416 		return (0);
417 	wclear(wnd);
418 	labeliostat();
419 	refresh();
420 	return (1);
421 }
422