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