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