xref: /freebsd/usr.bin/systat/iostat.c (revision 380a989b3223d455375b4fae70fd0b9bdd43bafb)
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  *	$Id: iostat.c,v 1.6 1998/09/15 08:16:40 gibbs 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/buf.h>
69 #include <sys/dkstat.h>
70 
71 #include <string.h>
72 #include <stdlib.h>
73 #include <nlist.h>
74 #include <paths.h>
75 #include <devstat.h>
76 #include <err.h>
77 #include "systat.h"
78 #include "extern.h"
79 #include "devs.h"
80 
81 static struct nlist namelist[] = {
82 #define X_CP_TIME	0
83 	{ "_cp_time" },
84 #ifdef vax
85 #define X_MBDINIT	(X_CP_TIME+1)
86 	{ "_mbdinit" },
87 #define X_UBDINIT	(X_CP_TIME+2)
88 	{ "_ubdinit" },
89 #endif
90 #ifdef tahoe
91 #define	X_VBDINIT	(X_CP_TIME+1)
92 	{ "_vbdinit" },
93 #endif
94 	{ "" },
95 };
96 
97 struct statinfo cur, last;
98 
99 static  int linesperregion;
100 static  double etime;
101 static  int numbers = 0;		/* default display bar graphs */
102 static  int kbpt = 0;			/* default ms/seek shown */
103 
104 static int barlabels __P((int));
105 static void histogram __P((long double, int, double));
106 static int numlabels __P((int));
107 static int devstats __P((int, int, int));
108 static void stat1 __P((int, int));
109 
110 WINDOW *
111 openiostat()
112 {
113 	return (subwin(stdscr, LINES-1-5, 0, 5, 0));
114 }
115 
116 void
117 closeiostat(w)
118 	WINDOW *w;
119 {
120 	if (w == NULL)
121 		return;
122 	wclear(w);
123 	wrefresh(w);
124 	delwin(w);
125 }
126 
127 int
128 initiostat()
129 {
130 	if (num_devices = getnumdevs() < 0)
131 		return(0);
132 
133 	cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
134 	last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
135 	bzero(cur.dinfo, sizeof(struct devinfo));
136 	bzero(last.dinfo, sizeof(struct devinfo));
137 
138 	/*
139 	 * This value for maxshowdevs (100) is bogus.  I'm not sure exactly
140 	 * how to calculate it, though.
141 	 */
142 	if (dsinit(100, &cur, &last, NULL) != 1)
143 		return(0);
144 
145 	if (kvm_nlist(kd, namelist)) {
146        		nlisterr(namelist);
147 		return(0);
148 	}
149 
150 	return(1);
151 }
152 
153 void
154 fetchiostat()
155 {
156 	struct devinfo *tmp_dinfo;
157 
158 	NREAD(X_CP_TIME, cur.cp_time, sizeof(cur.cp_time));
159 	tmp_dinfo = last.dinfo;
160 	last.dinfo = cur.dinfo;
161 	cur.dinfo = tmp_dinfo;
162 
163 	last.busy_time = cur.busy_time;
164 
165 	/*
166 	 * Here what we want to do is refresh our device stats.
167 	 * getdevs() returns 1 when the device list has changed.
168 	 * If the device list has changed, we want to go through
169 	 * the selection process again, in case a device that we
170 	 * were previously displaying has gone away.
171 	 */
172 	switch (getdevs(&cur)) {
173 	case -1:
174 		errx(1, "%s", devstat_errbuf);
175 		break;
176 	case 1:
177 		cmdiostat("refresh", NULL);
178 		break;
179 	default:
180 		break;
181 	}
182 	num_devices = cur.dinfo->numdevs;
183 	generation = cur.dinfo->generation;
184 
185 }
186 
187 #define	INSET	10
188 
189 void
190 labeliostat()
191 {
192 	int row;
193 
194 	row = 0;
195 	wmove(wnd, row, 0); wclrtobot(wnd);
196 	mvwaddstr(wnd, row++, INSET,
197 	    "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
198 	mvwaddstr(wnd, row++, 0, "cpu  user|");
199 	mvwaddstr(wnd, row++, 0, "     nice|");
200 	mvwaddstr(wnd, row++, 0, "   system|");
201 	mvwaddstr(wnd, row++, 0, "interrupt|");
202 	mvwaddstr(wnd, row++, 0, "     idle|");
203 	if (numbers)
204 		row = numlabels(row + 1);
205 	else
206 		row = barlabels(row + 1);
207 }
208 
209 static int
210 numlabels(row)
211 	int row;
212 {
213 	int i, col, regions, ndrives;
214 	char tmpstr[10];
215 
216 #define COLWIDTH	17
217 #define DRIVESPERLINE	((wnd->maxx - INSET) / COLWIDTH)
218 	for (ndrives = 0, i = 0; i < num_devices; i++)
219 		if (dev_select[i].selected)
220 			ndrives++;
221 	regions = howmany(ndrives, DRIVESPERLINE);
222 	/*
223 	 * Deduct -regions for blank line after each scrolling region.
224 	 */
225 	linesperregion = (wnd->maxy - row - regions) / regions;
226 	/*
227 	 * Minimum region contains space for two
228 	 * label lines and one line of statistics.
229 	 */
230 	if (linesperregion < 3)
231 		linesperregion = 3;
232 	col = INSET;
233 	for (i = 0; i < num_devices; i++)
234 		if (dev_select[i].selected) {
235 			if (col + COLWIDTH >= wnd->maxx - INSET) {
236 				col = INSET, row += linesperregion + 1;
237 				if (row > wnd->maxy - (linesperregion + 1))
238 					break;
239 			}
240 			sprintf(tmpstr, "%s%d", dev_select[i].device_name,
241 				dev_select[i].unit_number);
242 			mvwaddstr(wnd, row, col + 4, tmpstr);
243 			mvwaddstr(wnd, row + 1, col, "  KB/t tps  MB/s ");
244 			col += COLWIDTH;
245 		}
246 	if (col)
247 		row += linesperregion + 1;
248 	return (row);
249 }
250 
251 static int
252 barlabels(row)
253 	int row;
254 {
255 	int i;
256 	char tmpstr[10];
257 
258 	mvwaddstr(wnd, row++, INSET,
259 	    "/0   /5   /10  /15  /20  /25  /30  /35  /40  /45  /50");
260 	linesperregion = 2 + kbpt;
261 	for (i = 0; i < num_devices; i++)
262 		if (dev_select[i].selected) {
263 			if (row > wnd->maxy - linesperregion)
264 				break;
265 			sprintf(tmpstr, "%s%d", dev_select[i].device_name,
266 				dev_select[i].unit_number);
267 			mvwprintw(wnd, row++, 0, "%-5.5s MB/s|",
268 				  tmpstr);
269 			mvwaddstr(wnd, row++, 0, "      tps|");
270 			if (kbpt)
271 				mvwaddstr(wnd, row++, 0, "     KB/t|");
272 		}
273 	return (row);
274 }
275 
276 
277 void
278 showiostat()
279 {
280 	register long t;
281 	register int i, row, col;
282 
283 #define X(fld)	t = cur.fld[i]; cur.fld[i] -= last.fld[i]; last.fld[i] = t
284 	etime = 0;
285 	for(i = 0; i < CPUSTATES; i++) {
286 		X(cp_time);
287 		etime += cur.cp_time[i];
288 	}
289 	if (etime == 0.0)
290 		etime = 1.0;
291 	etime /= hertz;
292 	row = 1;
293 	for (i = 0; i < CPUSTATES; i++)
294 		stat1(row++, i);
295 	if (!numbers) {
296 		row += 2;
297 		for (i = 0; i < num_devices; i++)
298 			if (dev_select[i].selected) {
299 				if (row > wnd->maxy - linesperregion)
300 					break;
301 				row = devstats(row, INSET, i);
302 			}
303 		return;
304 	}
305 	col = INSET;
306 	wmove(wnd, row + linesperregion, 0);
307 	wdeleteln(wnd);
308 	wmove(wnd, row + 3, 0);
309 	winsertln(wnd);
310 	for (i = 0; i < num_devices; i++)
311 		if (dev_select[i].selected) {
312 			if (col + COLWIDTH >= wnd->maxx - INSET) {
313 				col = INSET, row += linesperregion + 1;
314 				if (row > wnd->maxy - (linesperregion + 1))
315 					break;
316 				wmove(wnd, row + linesperregion, 0);
317 				wdeleteln(wnd);
318 				wmove(wnd, row + 3, 0);
319 				winsertln(wnd);
320 			}
321 			(void) devstats(row + 3, col, i);
322 			col += COLWIDTH;
323 		}
324 }
325 
326 static int
327 devstats(row, col, dn)
328 	int row, col, dn;
329 {
330 	long double transfers_per_second;
331 	long double kb_per_transfer, mb_per_second;
332 	long double busy_seconds;
333 	int di;
334 
335 	di = dev_select[dn].position;
336 
337 	busy_seconds = compute_etime(cur.busy_time, last.busy_time);
338 
339 	if (compute_stats(&cur.dinfo->devices[di], &last.dinfo->devices[di],
340 			  busy_seconds, NULL, NULL, NULL,
341 			  &kb_per_transfer, &transfers_per_second,
342 			  &mb_per_second, NULL, NULL) != 0)
343 		errx(1, "%s", devstat_errbuf);
344 
345 	if (numbers) {
346 		mvwprintw(wnd, row, col, " %5.2Lf %3.0Lf %5.2Lf ",
347 			 kb_per_transfer, transfers_per_second,
348 			 mb_per_second);
349 		return(row);
350 	}
351 	wmove(wnd, row++, col);
352 	histogram(mb_per_second, 50, 1.0);
353 	wmove(wnd, row++, col);
354 	histogram(transfers_per_second, 50, 1.0);
355 	if (kbpt) {
356 		wmove(wnd, row++, col);
357 		histogram(kb_per_transfer, 50, 1.0);
358 	}
359 
360 	return(row);
361 
362 }
363 
364 static void
365 stat1(row, o)
366 	int row, o;
367 {
368 	register int i;
369 	double time;
370 
371 	time = 0;
372 	for (i = 0; i < CPUSTATES; i++)
373 		time += cur.cp_time[i];
374 	if (time == 0.0)
375 		time = 1.0;
376 	wmove(wnd, row, INSET);
377 #define CPUSCALE	0.5
378 	histogram(100.0 * cur.cp_time[o] / time, 50, CPUSCALE);
379 }
380 
381 static void
382 histogram(val, colwidth, scale)
383 	long double val;
384 	int colwidth;
385 	double scale;
386 {
387 	char buf[10];
388 	register int k;
389 	register int v = (int)(val * scale) + 0.5;
390 
391 	k = MIN(v, colwidth);
392 	if (v > colwidth) {
393 		snprintf(buf, sizeof(buf), "%5.2Lf", val);
394 		k -= strlen(buf);
395 		while (k--)
396 			waddch(wnd, 'X');
397 		waddstr(wnd, buf);
398 		return;
399 	}
400 	while (k--)
401 		waddch(wnd, 'X');
402 	wclrtoeol(wnd);
403 }
404 
405 int
406 cmdiostat(cmd, args)
407 	char *cmd, *args;
408 {
409 
410 	if (prefix(cmd, "kbpt"))
411 		kbpt = !kbpt;
412 	else if (prefix(cmd, "numbers"))
413 		numbers = 1;
414 	else if (prefix(cmd, "bars"))
415 		numbers = 0;
416 	else if (!dscmd(cmd, args, 100, &cur))
417 		return (0);
418 	wclear(wnd);
419 	labeliostat();
420 	refresh();
421 	return (1);
422 }
423