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