xref: /freebsd/usr.bin/systat/devs.c (revision 315ee00f)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1998 Kenneth D. Merry.
5  *               2015 Yoshihiro Ota
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 /*-
32  * Copyright (c) 1980, 1992, 1993
33  *	The Regents of the University of California.  All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 3. Neither the name of the University nor the names of its contributors
44  *    may be used to endorse or promote products derived from this software
45  *    without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57  * SUCH DAMAGE.
58  */
59 
60 
61 #ifdef lint
62 static const char sccsid[] = "@(#)disks.c	8.1 (Berkeley) 6/6/93";
63 #endif
64 
65 #include <sys/types.h>
66 #include <sys/devicestat.h>
67 #include <sys/resource.h>
68 
69 #include <ctype.h>
70 #include <err.h>
71 #include <stdlib.h>
72 #include <string.h>
73 
74 #include "systat.h"
75 #include "extern.h"
76 #include "devs.h"
77 
78 typedef enum {
79 	DS_MATCHTYPE_NONE,
80 	DS_MATCHTYPE_SPEC,
81 	DS_MATCHTYPE_PATTERN
82 } last_match_type;
83 
84 struct statinfo cur_dev, last_dev, run_dev;
85 
86 static last_match_type last_type;
87 struct device_selection *dev_select;
88 long generation;
89 int num_devices, num_selected;
90 int num_selections;
91 long select_generation;
92 static struct devstat_match *matches = NULL;
93 static int num_matches = 0;
94 static char **specified_devices;
95 static int num_devices_specified = 0;
96 
97 static int dsmatchselect(const char *args, devstat_select_mode select_mode,
98 			 int maxshowdevs, struct statinfo *s1);
99 static int dsselect(const char *args, devstat_select_mode select_mode,
100 		    int maxshowdevs, struct statinfo *s1);
101 
102 int
103 dsinit(int maxshowdevs)
104 {
105 	/*
106 	 * Make sure that the userland devstat version matches the kernel
107 	 * devstat version.  If not, exit and print a message informing
108 	 * the user of his mistake.
109 	 */
110 	if (devstat_checkversion(NULL) < 0)
111 		errx(1, "%s", devstat_errbuf);
112 
113 	if( cur_dev.dinfo ) // init was alreay ran
114 		return(1);
115 
116 	if ((num_devices = devstat_getnumdevs(NULL)) < 0) {
117 		warnx("%s", devstat_errbuf);
118 		return(0);
119 	}
120 
121 	cur_dev.dinfo = calloc(1, sizeof(struct devinfo));
122 	last_dev.dinfo = calloc(1, sizeof(struct devinfo));
123 	run_dev.dinfo = calloc(1, sizeof(struct devinfo));
124 
125 	generation = 0;
126 	num_devices = 0;
127 	num_selected = 0;
128 	num_selections = 0;
129 	select_generation = 0;
130 	last_type = DS_MATCHTYPE_NONE;
131 
132 	if (devstat_getdevs(NULL, &cur_dev) == -1)
133 		errx(1, "%s", devstat_errbuf);
134 
135 	num_devices = cur_dev.dinfo->numdevs;
136 	generation = cur_dev.dinfo->generation;
137 
138 	dev_select = NULL;
139 
140 	/*
141 	 * At this point, selectdevs will almost surely indicate that the
142 	 * device list has changed, so we don't look for return values of 0
143 	 * or 1.  If we get back -1, though, there is an error.
144 	 */
145 	if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
146 	    &select_generation, generation, cur_dev.dinfo->devices, num_devices,
147 	    NULL, 0, NULL, 0, DS_SELECT_ADD, maxshowdevs, 0) == -1)
148 		errx(1, "%d %s", __LINE__, devstat_errbuf);
149 
150 	return(1);
151 }
152 
153 
154 void
155 dsgetinfo(struct statinfo* dev)
156 {
157 	switch (devstat_getdevs(NULL, dev)) {
158 	case -1:
159 		errx(1, "%s", devstat_errbuf);
160 		break;
161 	case 1:
162 		num_devices = dev->dinfo->numdevs;
163 		generation = dev->dinfo->generation;
164 		cmdkre("refresh", NULL);
165 		break;
166 	default:
167 		break;
168 	}
169 }
170 
171 int
172 dscmd(const char *cmd, const char *args, int maxshowdevs, struct statinfo *s1)
173 {
174 	int retval;
175 
176 	if (prefix(cmd, "display") || prefix(cmd, "add"))
177 		return(dsselect(args, DS_SELECT_ADDONLY, maxshowdevs, s1));
178 	if (prefix(cmd, "ignore") || prefix(cmd, "delete"))
179 		return(dsselect(args, DS_SELECT_REMOVE, maxshowdevs, s1));
180 	if (prefix(cmd, "show") || prefix(cmd, "only"))
181 		return(dsselect(args, DS_SELECT_ONLY, maxshowdevs, s1));
182 	if (prefix(cmd, "type") || prefix(cmd, "match"))
183 		return(dsmatchselect(args, DS_SELECT_ONLY, maxshowdevs, s1));
184 	if (prefix(cmd, "refresh")) {
185 		retval = devstat_selectdevs(&dev_select, &num_selected,
186 		    &num_selections, &select_generation, generation,
187 		    s1->dinfo->devices, num_devices,
188 		    (last_type ==DS_MATCHTYPE_PATTERN) ?  matches : NULL,
189 		    (last_type ==DS_MATCHTYPE_PATTERN) ?  num_matches : 0,
190 		    (last_type == DS_MATCHTYPE_SPEC) ?specified_devices : NULL,
191 		    (last_type == DS_MATCHTYPE_SPEC) ?num_devices_specified : 0,
192 		    (last_type == DS_MATCHTYPE_NONE) ?  DS_SELECT_ADD :
193 		    DS_SELECT_ADDONLY, maxshowdevs, 0);
194 		if (retval == -1) {
195 			warnx("%s", devstat_errbuf);
196 			return(0);
197 		} else if (retval == 1)
198 			return(2);
199 	}
200 	if (prefix(cmd, "drives")) {
201 		int i;
202 		move(CMDLINE, 0);
203 		clrtoeol();
204 		for (i = 0; i < num_devices; i++) {
205 			printw("%s%d ", s1->dinfo->devices[i].device_name,
206 			       s1->dinfo->devices[i].unit_number);
207 		}
208 		return(1);
209 	}
210 	return(0);
211 }
212 
213 static int
214 dsmatchselect(const char *args, devstat_select_mode select_mode, int maxshowdevs,
215 	      struct statinfo *s1)
216 {
217 	char **tempstr, *tmpstr, *tmpstr1;
218 	char *tstr[100];
219 	int num_args = 0;
220 	int i;
221 	int retval = 0;
222 
223 	if (!args) {
224 		warnx("dsmatchselect: no arguments");
225 		return(1);
226 	}
227 
228 	/*
229 	 * Break the (pipe delimited) input string out into separate
230 	 * strings.
231 	 */
232 	tmpstr = tmpstr1 = strdup(args);
233 	for (tempstr = tstr, num_args  = 0;
234 	     (*tempstr = strsep(&tmpstr1, "|")) != NULL && (num_args < 100);
235 	     num_args++)
236 		if (**tempstr != '\0')
237 			if (++tempstr >= &tstr[100])
238 				break;
239 	free(tmpstr);
240 
241 	if (num_args > 99) {
242 		warnx("dsmatchselect: too many match arguments");
243 		return(0);
244 	}
245 
246 	/*
247 	 * If we've gone through the matching code before, clean out
248 	 * previously used memory.
249 	 */
250 	if (num_matches > 0) {
251 		free(matches);
252 		matches = NULL;
253 		num_matches = 0;
254 	}
255 
256 	for (i = 0; i < num_args; i++) {
257 		if (devstat_buildmatch(tstr[i], &matches, &num_matches) != 0) {
258 			warnx("%s", devstat_errbuf);
259 			return(0);
260 		}
261 	}
262 	if (num_args > 0) {
263 
264 		last_type = DS_MATCHTYPE_PATTERN;
265 
266 		retval = devstat_selectdevs(&dev_select, &num_selected,
267 		    &num_selections, &select_generation, generation,
268 		    s1->dinfo->devices, num_devices, matches, num_matches,
269 		    NULL, 0, select_mode, maxshowdevs, 0);
270 		if (retval == -1)
271 			err(1, "device selection error");
272 		else if (retval == 1)
273 			return(2);
274 	}
275 	return(1);
276 }
277 
278 static int
279 dsselect(const char *args, devstat_select_mode select_mode, int maxshowdevs,
280 	 struct statinfo *s1)
281 {
282 	char *cp, *tmpstr, *tmpstr1, *buffer;
283 	int i;
284 	int retval = 0;
285 
286 	if (!args) {
287 		warnx("dsselect: no argument");
288 		return(1);
289 	}
290 
291 	/*
292 	 * If we've gone through this code before, free previously
293 	 * allocated resources.
294 	 */
295 	if (num_devices_specified > 0) {
296 		for (i = 0; i < num_devices_specified; i++)
297 			free(specified_devices[i]);
298 		free(specified_devices);
299 		specified_devices = NULL;
300 		num_devices_specified = 0;
301 	}
302 
303 	/* do an initial malloc */
304 	specified_devices = (char **)malloc(sizeof(char *));
305 
306 	tmpstr = tmpstr1 = strdup(args);
307 	cp = strchr(tmpstr1, '\n');
308 	if (cp)
309 		*cp = '\0';
310 	for (;;) {
311 		for (cp = tmpstr1; *cp && isspace(*cp); cp++)
312 			;
313 		tmpstr1 = cp;
314 		for (; *cp && !isspace(*cp); cp++)
315 			;
316 		if (*cp)
317 			*cp++ = '\0';
318 		if (cp - tmpstr1 == 0)
319 			break;
320 		for (i = 0; i < num_devices; i++) {
321 			asprintf(&buffer, "%s%d", dev_select[i].device_name,
322 				dev_select[i].unit_number);
323 			if (strcmp(buffer, tmpstr1) == 0) {
324 
325 				num_devices_specified++;
326 
327 				specified_devices =(char **)realloc(
328 						specified_devices,
329 						sizeof(char *) *
330 						num_devices_specified);
331 				specified_devices[num_devices_specified -1]=
332 					strdup(tmpstr1);
333 				free(buffer);
334 
335 				break;
336 			}
337 			else
338 				free(buffer);
339 		}
340 		if (i >= num_devices)
341 			error("%s: unknown drive", args);
342 		tmpstr1 = cp;
343 	}
344 	free(tmpstr);
345 
346 	if (num_devices_specified > 0) {
347 		last_type = DS_MATCHTYPE_SPEC;
348 
349 		retval = devstat_selectdevs(&dev_select, &num_selected,
350 		    &num_selections, &select_generation, generation,
351 		    s1->dinfo->devices, num_devices, NULL, 0,
352 		    specified_devices, num_devices_specified,
353 		    select_mode, maxshowdevs, 0);
354 		if (retval == -1)
355 			err(1, "%s", devstat_errbuf);
356 		else if (retval == 1)
357 			return(2);
358 	}
359 	return(1);
360 }
361 
362 
363 void
364 dslabel(int maxdrives, int diskcol, int diskrow)
365 {
366 	int i, j;
367 
368 	mvprintw(diskrow, diskcol, "Disks");
369 	mvprintw(diskrow + 1, diskcol, "KB/t");
370 	mvprintw(diskrow + 2, diskcol, "tps");
371 	mvprintw(diskrow + 3, diskcol, "MB/s");
372 	mvprintw(diskrow + 4, diskcol, "%%busy");
373 	/*
374 	 * For now, we don't support a fourth disk statistic.  So there's
375 	 * no point in providing a label for it.  If someone can think of a
376 	 * fourth useful disk statistic, there is room to add it.
377 	 */
378 	/* mvprintw(diskrow + 4, diskcol, " msps"); */
379 	j = 0;
380 	for (i = 0; i < num_devices && j < maxdrives; i++)
381 		if (dev_select[i].selected) {
382 			char tmpstr[80];
383 			sprintf(tmpstr, "%s%d", dev_select[i].device_name,
384 				dev_select[i].unit_number);
385 			mvprintw(diskrow, diskcol + 5 + 6 * j,
386 				" %5.5s", tmpstr);
387 			j++;
388 		}
389 }
390 
391 static void
392 dsshow2(int diskcol, int diskrow, int dn, int lc, struct statinfo *now, struct statinfo *then)
393 {
394 	long double transfers_per_second;
395 	long double kb_per_transfer, mb_per_second;
396 	long double elapsed_time, device_busy;
397 	int di;
398 
399 	di = dev_select[dn].position;
400 
401 	if (then != NULL) {
402 		/* Calculate relative to previous sample */
403 		elapsed_time = now->snap_time - then->snap_time;
404 	} else {
405 		/* Calculate relative to device creation */
406 		elapsed_time = now->snap_time - devstat_compute_etime(
407 		    &now->dinfo->devices[di].creation_time, NULL);
408 	}
409 
410 	if (devstat_compute_statistics(&now->dinfo->devices[di], then ?
411 	    &then->dinfo->devices[di] : NULL, elapsed_time,
412 	    DSM_KB_PER_TRANSFER, &kb_per_transfer,
413 	    DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
414 	    DSM_MB_PER_SECOND, &mb_per_second,
415 	    DSM_BUSY_PCT, &device_busy,
416 	    DSM_NONE) != 0)
417 		errx(1, "%s", devstat_errbuf);
418 
419 	lc = diskcol + lc * 6;
420 	putlongdouble(kb_per_transfer, diskrow + 1, lc, 5, 2, 0);
421 	putlongdouble(transfers_per_second, diskrow + 2, lc, 5, 0, 0);
422 	putlongdouble(mb_per_second, diskrow + 3, lc, 5, 2, 0);
423 	putlongdouble(device_busy, diskrow + 4, lc, 5, 0, 0);
424 }
425 
426 void
427 dsshow(int maxdrives, int diskcol, int diskrow, struct statinfo *now, struct statinfo *then)
428 {
429 	int i, lc;
430 
431 	for (i = 0, lc = 0; i < num_devices && lc < maxdrives; i++)
432 		if (dev_select[i].selected)
433 			dsshow2(diskcol, diskrow, i, ++lc, now, then);
434 }
435