xref: /freebsd/lib/libdevstat/devstat.c (revision 6e8394b8baa7d5d9153ab90de6824bcd19b3b4e1)
1 /*
2  * Copyright (c) 1997, 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: devstat.c,v 1.4 1998/10/14 23:28:26 ken Exp $
29  */
30 
31 #include <sys/types.h>
32 #include <sys/sysctl.h>
33 #include <sys/errno.h>
34 #include <sys/dkstat.h>
35 
36 #include <ctype.h>
37 #include <err.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
42 #include "devstat.h"
43 
44 char devstat_errbuf[DEVSTAT_ERRBUF_SIZE];
45 
46 /*
47  * Table to match descriptive strings with device types.  These are in
48  * order from most common to least common to speed search time.
49  */
50 struct devstat_match_table match_table[] = {
51 	{"da",		DEVSTAT_TYPE_DIRECT,	DEVSTAT_MATCH_TYPE},
52 	{"cd",		DEVSTAT_TYPE_CDROM,	DEVSTAT_MATCH_TYPE},
53 	{"scsi",	DEVSTAT_TYPE_IF_SCSI,	DEVSTAT_MATCH_IF},
54 	{"ide",		DEVSTAT_TYPE_IF_IDE,	DEVSTAT_MATCH_IF},
55 	{"other",	DEVSTAT_TYPE_IF_OTHER,	DEVSTAT_MATCH_IF},
56 	{"worm",	DEVSTAT_TYPE_WORM,	DEVSTAT_MATCH_TYPE},
57 	{"sa",		DEVSTAT_TYPE_SEQUENTIAL,DEVSTAT_MATCH_TYPE},
58 	{"pass",	DEVSTAT_TYPE_PASS,	DEVSTAT_MATCH_PASS},
59 	{"optical",	DEVSTAT_TYPE_OPTICAL,	DEVSTAT_MATCH_TYPE},
60 	{"array",	DEVSTAT_TYPE_STORARRAY,	DEVSTAT_MATCH_TYPE},
61 	{"changer",	DEVSTAT_TYPE_CHANGER,	DEVSTAT_MATCH_TYPE},
62 	{"scanner",	DEVSTAT_TYPE_SCANNER,	DEVSTAT_MATCH_TYPE},
63 	{"printer",	DEVSTAT_TYPE_PRINTER,	DEVSTAT_MATCH_TYPE},
64 	{"floppy",	DEVSTAT_TYPE_FLOPPY,	DEVSTAT_MATCH_TYPE},
65 	{"proc",	DEVSTAT_TYPE_PROCESSOR,	DEVSTAT_MATCH_TYPE},
66 	{"comm",	DEVSTAT_TYPE_COMM,	DEVSTAT_MATCH_TYPE},
67 	{"enclosure",	DEVSTAT_TYPE_ENCLOSURE,	DEVSTAT_MATCH_TYPE},
68 	{NULL,		0,			0}
69 };
70 
71 /*
72  * Local function declarations.
73  */
74 static int compare_select(const void *arg1, const void *arg2);
75 
76 int
77 getnumdevs(void)
78 {
79 	size_t numdevsize;
80 	int numdevs;
81 	char *func_name = "getnumdevs";
82 
83 	numdevsize = sizeof(int);
84 
85 	/*
86 	 * Find out how many devices we have in the system.
87 	 */
88 	if (sysctlbyname("kern.devstat.numdevs", &numdevs,
89 			 &numdevsize, NULL, 0) == -1) {
90 		sprintf(devstat_errbuf, "%s: error getting number of devices\n"
91 			"%s: %s", func_name, func_name, strerror(errno));
92 		return(-1);
93 	} else
94 		return(numdevs);
95 }
96 
97 /*
98  * This is an easy way to get the generation number, but the generation is
99  * supplied in a more atmoic manner by the kern.devstat.all sysctl.
100  * Because this generation sysctl is separate from the statistics sysctl,
101  * the device list and the generation could change between the time that
102  * this function is called and the device list is retreived.
103  */
104 long
105 getgeneration(void)
106 {
107 	size_t gensize;
108 	long generation;
109 	char *func_name = "getgeneration";
110 
111 	gensize = sizeof(long);
112 
113 	/*
114 	 * Get the current generation number.
115 	 */
116 	if (sysctlbyname("kern.devstat.generation", &generation,
117 			 &gensize, NULL, 0) == -1) {
118 		sprintf(devstat_errbuf,"%s: error getting devstat generation\n"
119 			"%s: %s", func_name, func_name, strerror(errno));
120 		return(-1);
121 	} else
122 		return(generation);
123 }
124 
125 /*
126  * Get the current devstat version.  The return value of this function
127  * should be compared with DEVSTAT_VERSION, which is defined in
128  * sys/devicestat.h.  This will enable userland programs to determine
129  * whether they are out of sync with the kernel.
130  */
131 int
132 getversion(void)
133 {
134 	size_t versize;
135 	int version;
136 	char *func_name = "getversion";
137 
138 	versize = sizeof(int);
139 
140 	/*
141 	 * Get the current devstat version.
142 	 */
143 	if (sysctlbyname("kern.devstat.version", &version, &versize,
144 			 NULL, 0) == -1) {
145 		sprintf(devstat_errbuf, "%s: error getting devstat version\n"
146 			"%s: %s", func_name, func_name, strerror(errno));
147 		return(-1);
148 	} else
149 		return(version);
150 }
151 
152 /*
153  * Check the devstat version we know about against the devstat version the
154  * kernel knows about.  If they don't match, print an error into the
155  * devstat error buffer, and return -1.  If they match, return 0.
156  */
157 int
158 checkversion(void)
159 {
160 	int retval = 0;
161 	int errlen = 0;
162 	char *func_name = "checkversion";
163 	int version;
164 
165 	version = getversion();
166 
167 	if (version != DEVSTAT_VERSION) {
168 		int buflen = 0;
169 		char tmpstr[256];
170 
171 		/*
172 		 * This is really pretty silly, but basically the idea is
173 		 * that if getversion() returns an error (i.e. -1), then it
174 		 * has printed an error message in the buffer.  Therefore,
175 		 * we need to add a \n to the end of that message before we
176 		 * print our own message in the buffer.
177 		 */
178 		if (version == -1) {
179 			buflen = strlen(devstat_errbuf);
180 			errlen = snprintf(tmpstr, sizeof(tmpstr), "\n");
181 			strncat(devstat_errbuf, tmpstr,
182 				DEVSTAT_ERRBUF_SIZE - buflen - 1);
183 			buflen += errlen;
184 		}
185 
186 		errlen = snprintf(tmpstr, sizeof(tmpstr),
187 				  "%s: userland devstat version %d is not "
188 				  "the same as the kernel\n%s: devstat "
189 				  "version %d\n", func_name, DEVSTAT_VERSION,
190 				  func_name, version);
191 
192 		if (version == -1) {
193 			strncat(devstat_errbuf, tmpstr,
194 				DEVSTAT_ERRBUF_SIZE - buflen - 1);
195 			buflen += errlen;
196 		} else {
197 			strncpy(devstat_errbuf, tmpstr, DEVSTAT_ERRBUF_SIZE);
198 			devstat_errbuf[DEVSTAT_ERRBUF_SIZE - 1] = '\0';
199 		}
200 
201                 if (version < DEVSTAT_VERSION)
202 			snprintf(tmpstr, sizeof(tmpstr),
203 				 "%s: libdevstat newer than kernel\n",
204 				 func_name);
205                 else
206 			snprintf(tmpstr, sizeof(tmpstr),
207 				 "%s: kernel newer than libdevstat\n",
208 				 func_name);
209 
210 		strncat(devstat_errbuf, tmpstr,
211 			DEVSTAT_ERRBUF_SIZE - buflen - 1);
212 
213 		retval = -1;
214 	}
215 
216 	return(retval);
217 }
218 
219 /*
220  * Get the current list of devices and statistics, and the current
221  * generation number.
222  *
223  * Return values:
224  * -1  -- error
225  *  0  -- device list is unchanged
226  *  1  -- device list has changed
227  */
228 int
229 getdevs(struct statinfo *stats)
230 {
231 	int error;
232 	size_t dssize;
233 	int oldnumdevs;
234 	long oldgeneration;
235 	int retval = 0;
236 	struct devinfo *dinfo;
237 	char *func_name = "getdevs";
238 
239 	dinfo = stats->dinfo;
240 
241 	if (dinfo == NULL) {
242 		sprintf(devstat_errbuf, "%s: stats->dinfo was NULL", func_name);
243 		return(-1);
244 	}
245 
246 	oldnumdevs = dinfo->numdevs;
247 	oldgeneration = dinfo->generation;
248 
249 	/*
250 	 * If this is our first time through, mem_ptr will be null.
251 	 */
252 	if (dinfo->mem_ptr == NULL) {
253 		/*
254 		 * Get the number of devices.  If it's negative, it's an
255 		 * error.  Don't bother setting the error string, since
256 		 * getnumdevs() has already done that for us.
257 		 */
258 		if ((dinfo->numdevs = getnumdevs()) < 0)
259 			return(-1);
260 
261 		/*
262 		 * The kern.devstat.all sysctl returns the current generation
263 		 * number, as well as all the devices.  So we need four
264 		 * bytes more.
265 		 */
266 		dssize =(dinfo->numdevs * sizeof(struct devstat)) +sizeof(long);
267 		dinfo->mem_ptr = (u_int8_t *)malloc(dssize);
268 	} else
269 		dssize =(dinfo->numdevs * sizeof(struct devstat)) +sizeof(long);
270 
271 	/* Get the current time when we get the stats */
272 	gettimeofday(&stats->busy_time, NULL);
273 
274 	/*
275 	 * Request all of the devices.  We only really allow for one
276 	 * ENOMEM failure.  It would, of course, be possible to just go in
277 	 * a loop and keep reallocing the device structure until we don't
278 	 * get ENOMEM back.  I'm not sure it's worth it, though.  If
279 	 * devices are being added to the system that quickly, maybe the
280 	 * user can just wait until all devices are added.
281 	 */
282 	if ((error = sysctlbyname("kern.devstat.all", dinfo->mem_ptr,
283 	     &dssize, NULL, 0)) == -1) {
284 		/*
285 		 * If we get ENOMEM back, that means that there are
286 		 * more devices now, so we need to allocate more
287 		 * space for the device array.
288 		 */
289 		if (errno == ENOMEM) {
290 			/*
291 			 * No need to set the error string here, getnumdevs()
292 			 * will do that if it fails.
293 			 */
294 			if ((dinfo->numdevs = getnumdevs()) < 0)
295 				return(-1);
296 
297 			dssize = (dinfo->numdevs * sizeof(struct devstat)) +
298 				sizeof(long);
299 			dinfo->mem_ptr = (u_int8_t *)realloc(dinfo->mem_ptr,
300 							     dssize);
301 			if ((error = sysctlbyname("kern.devstat.all",
302 			    dinfo->mem_ptr, &dssize, NULL, 0)) == -1) {
303 				sprintf(devstat_errbuf,
304 					"%s: error getting device stats\n"
305 					"%s: %s", func_name, func_name,
306 					strerror(errno));
307 				return(-1);
308 			}
309 		} else {
310 			sprintf(devstat_errbuf,
311 				"%s: error getting device stats\n"
312 				"%s: %s", func_name, func_name,
313 				strerror(errno));
314 			return(-1);
315 		}
316 	}
317 
318 	/*
319 	 * The sysctl spits out the generation as the first four bytes,
320 	 * then all of the device statistics structures.
321 	 */
322 	dinfo->generation = *(long *)dinfo->mem_ptr;
323 
324 	/*
325 	 * If the generation has changed, and if the current number of
326 	 * devices is not the same as the number of devices recorded in the
327 	 * devinfo structure, it is likely that the device list has shrunk.
328 	 * The reason that it is likely that the device list has shrunk in
329 	 * this case is that if the device list has grown, the sysctl above
330 	 * will return an ENOMEM error, and we will reset the number of
331 	 * devices and reallocate the device array.  If the second sysctl
332 	 * fails, we will return an error and therefore never get to this
333 	 * point.  If the device list has shrunk, the sysctl will not
334 	 * return an error since we have more space allocated than is
335 	 * necessary.  So, in the shrinkage case, we catch it here and
336 	 * reallocate the array so that we don't use any more space than is
337 	 * necessary.
338 	 */
339 	if (oldgeneration != dinfo->generation) {
340 		if (getnumdevs() != dinfo->numdevs) {
341 			if ((dinfo->numdevs = getnumdevs()) < 0)
342 				return(-1);
343 			dssize = (dinfo->numdevs * sizeof(struct devstat)) +
344 				sizeof(long);
345 			dinfo->mem_ptr = (u_int8_t *)realloc(dinfo->mem_ptr,
346 							     dssize);
347 		}
348 		retval = 1;
349 	}
350 
351 	dinfo->devices = (struct devstat *)(dinfo->mem_ptr + sizeof(long));
352 
353 	return(retval);
354 }
355 
356 /*
357  * selectdevs():
358  *
359  * Devices are selected/deselected based upon the following criteria:
360  * - devices specified by the user on the command line
361  * - devices matching any device type expressions given on the command line
362  * - devices with the highest I/O, if 'top' mode is enabled
363  * - the first n unselected devices in the device list, if maxshowdevs
364  *   devices haven't already been selected and if the user has not
365  *   specified any devices on the command line and if we're in "add" mode.
366  *
367  * Input parameters:
368  * - device selection list (dev_select)
369  * - current number of devices selected (num_selected)
370  * - total number of devices in the selection list (num_selections)
371  * - devstat generation as of the last time selectdevs() was called
372  *   (select_generation)
373  * - current devstat generation (current_generation)
374  * - current list of devices and statistics (devices)
375  * - number of devices in the current device list (numdevs)
376  * - compiled version of the command line device type arguments (matches)
377  *   - This is optional.  If the number of devices is 0, this will be ignored.
378  *   - The matching code pays attention to the current selection mode.  So
379  *     if you pass in a matching expression, it will be evaluated based
380  *     upon the selection mode that is passed in.  See below for details.
381  * - number of device type matching expressions (num_matches)
382  *   - Set to 0 to disable the matching code.
383  * - list of devices specified on the command line by the user (dev_selections)
384  * - number of devices selected on the command line by the user
385  *   (num_dev_selections)
386  * - Our selection mode.  There are four different selection modes:
387  *      - add mode.  (DS_SELECT_ADD) Any devices matching devices explicitly
388  *        selected by the user or devices matching a pattern given by the
389  *        user will be selected in addition to devices that are already
390  *        selected.  Additional devices will be selected, up to maxshowdevs
391  *        number of devices.
392  *      - only mode. (DS_SELECT_ONLY)  Only devices matching devices
393  *        explicitly given by the user or devices matching a pattern
394  *        given by the user will be selected.  No other devices will be
395  *        selected.
396  *      - addonly mode.  (DS_SELECT_ADDONLY)  This is similar to add and
397  *        only.  Basically, this will not de-select any devices that are
398  *        current selected, as only mode would, but it will also not
399  *        gratuitously select up to maxshowdevs devices as add mode would.
400  *      - remove mode.  (DS_SELECT_REMOVE)  Any devices matching devices
401  *        explicitly selected by the user or devices matching a pattern
402  *        given by the user will be de-selected.
403  * - maximum number of devices we can select (maxshowdevs)
404  * - flag indicating whether or not we're in 'top' mode (perf_select)
405  *
406  * Output data:
407  * - the device selection list may be modified and passed back out
408  * - the number of devices selected and the total number of items in the
409  *   device selection list may be changed
410  * - the selection generation may be changed to match the current generation
411  *
412  * Return values:
413  * -1  -- error
414  *  0  -- selected devices are unchanged
415  *  1  -- selected devices changed
416  */
417 int
418 selectdevs(struct device_selection **dev_select, int *num_selected,
419 	   int *num_selections, long *select_generation,
420 	   long current_generation, struct devstat *devices, int numdevs,
421 	   struct devstat_match *matches, int num_matches,
422 	   char **dev_selections, int num_dev_selections,
423 	   devstat_select_mode select_mode, int maxshowdevs, int perf_select)
424 {
425 	register int i, j, k;
426 	int init_selections = 0, init_selected_var = 0;
427 	struct device_selection *old_dev_select = NULL;
428 	int old_num_selections = 0, old_num_selected;
429 	int selection_number = 0;
430 	int changed = 0, found = 0;
431 
432 	if ((dev_select == NULL) || (devices == NULL) || (numdevs <= 0))
433 		return(-1);
434 
435 	/*
436 	 * We always want to make sure that we have as many dev_select
437 	 * entries as there are devices.
438 	 */
439 	/*
440 	 * In this case, we haven't selected devices before.
441 	 */
442 	if (*dev_select == NULL) {
443 		*dev_select = (struct device_selection *)malloc(numdevs *
444 			sizeof(struct device_selection));
445 		*select_generation = current_generation;
446 		init_selections = 1;
447 		changed = 1;
448 	/*
449 	 * In this case, we have selected devices before, but the device
450 	 * list has changed since we last selected devices, so we need to
451 	 * either enlarge or reduce the size of the device selection list.
452 	 */
453 	} else if (*num_selections != numdevs) {
454 		*dev_select = (struct device_selection *)realloc(*dev_select,
455 			numdevs * sizeof(struct device_selection));
456 		*select_generation = current_generation;
457 		init_selections = 1;
458 	/*
459 	 * In this case, we've selected devices before, and the selection
460 	 * list is the same size as it was the last time, but the device
461 	 * list has changed.
462 	 */
463 	} else if (*select_generation < current_generation) {
464 		*select_generation = current_generation;
465 		init_selections = 1;
466 	}
467 
468 	/*
469 	 * If we're in "only" mode, we want to clear out the selected
470 	 * variable since we're going to select exactly what the user wants
471 	 * this time through.
472 	 */
473 	if (select_mode == DS_SELECT_ONLY)
474 		init_selected_var = 1;
475 
476 	/*
477 	 * In all cases, we want to back up the number of selected devices.
478 	 * It is a quick and accurate way to determine whether the selected
479 	 * devices have changed.
480 	 */
481 	old_num_selected = *num_selected;
482 
483 	/*
484 	 * We want to make a backup of the current selection list if
485 	 * the list of devices has changed, or if we're in performance
486 	 * selection mode.  In both cases, we don't want to make a backup
487 	 * if we already know for sure that the list will be different.
488 	 * This is certainly the case if this is our first time through the
489 	 * selection code.
490 	 */
491 	if (((init_selected_var != 0) || (init_selections != 0)
492 	 || (perf_select != 0)) && (changed == 0)){
493 		old_dev_select = (struct device_selection *)malloc(
494 		    *num_selections * sizeof(struct device_selection));
495 		old_num_selections = *num_selections;
496 		bcopy(*dev_select, old_dev_select,
497 		    sizeof(struct device_selection) * *num_selections);
498 	}
499 
500 	if (init_selections != 0) {
501 		bzero(*dev_select, sizeof(struct device_selection) * numdevs);
502 
503 		for (i = 0; i < numdevs; i++) {
504 			(*dev_select)[i].device_number =
505 				devices[i].device_number;
506 			strncpy((*dev_select)[i].device_name,
507 				devices[i].device_name,
508 				DEVSTAT_NAME_LEN);
509 			(*dev_select)[i].device_name[DEVSTAT_NAME_LEN - 1]='\0';
510 			(*dev_select)[i].unit_number = devices[i].unit_number;
511 			(*dev_select)[i].position = i;
512 		}
513 		*num_selections = numdevs;
514 	} else if (init_selected_var != 0) {
515 		for (i = 0; i < numdevs; i++)
516 			(*dev_select)[i].selected = 0;
517 	}
518 
519 	/* we haven't gotten around to selecting anything yet.. */
520 	if ((select_mode == DS_SELECT_ONLY) || (init_selections != 0)
521 	 || (init_selected_var != 0))
522 		*num_selected = 0;
523 
524 	/*
525 	 * Look through any devices the user specified on the command line
526 	 * and see if they match known devices.  If so, select them.
527 	 */
528 	for (i = 0; (i < *num_selections) && (num_dev_selections > 0); i++) {
529 		char tmpstr[80];
530 
531 		snprintf(tmpstr, sizeof(tmpstr), "%s%d",
532 			(*dev_select)[i].device_name,
533 			(*dev_select)[i].unit_number);
534 		for (j = 0; j < num_dev_selections; j++) {
535 			if (strcmp(tmpstr, dev_selections[j]) == 0) {
536 				/*
537 				 * Here we do different things based on the
538 				 * mode we're in.  If we're in add or
539 				 * addonly mode, we only select this device
540 				 * if it hasn't already been selected.
541 				 * Otherwise, we would be unnecessarily
542 				 * changing the selection order and
543 				 * incrementing the selection count.  If
544 				 * we're in only mode, we unconditionally
545 				 * select this device, since in only mode
546 				 * any previous selections are erased and
547 				 * manually specified devices are the first
548 				 * ones to be selected.  If we're in remove
549 				 * mode, we de-select the specified device and
550 				 * decrement the selection count.
551 				 */
552 				switch(select_mode) {
553 				case DS_SELECT_ADD:
554 				case DS_SELECT_ADDONLY:
555 					if ((*dev_select)[i].selected)
556 						break;
557 					/* FALLTHROUGH */
558 				case DS_SELECT_ONLY:
559 					(*dev_select)[i].selected =
560 						++selection_number;
561 					(*num_selected)++;
562 					break;
563 				case DS_SELECT_REMOVE:
564 					(*dev_select)[i].selected = 0;
565 					(*num_selected)--;
566 					/*
567 					 * This isn't passed back out, we
568 					 * just use it to keep track of
569 					 * how many devices we've removed.
570 					 */
571 					num_dev_selections--;
572 					break;
573 				}
574 				break;
575 			}
576 		}
577 	}
578 
579 	/*
580 	 * Go through the user's device type expressions and select devices
581 	 * accordingly.  We only do this if the number of devices already
582 	 * selected is less than the maximum number we can show.
583 	 */
584 	for (i = 0; (i < num_matches) && (*num_selected < maxshowdevs); i++) {
585 		/* We should probably indicate some error here */
586 		if ((matches[i].match_fields == DEVSTAT_MATCH_NONE)
587 		 || (matches[i].num_match_categories <= 0))
588 			continue;
589 
590 		for (j = 0; j < numdevs; j++) {
591 			int num_match_categories;
592 
593 			num_match_categories = matches[i].num_match_categories;
594 
595 			/*
596 			 * Determine whether or not the current device
597 			 * matches the given matching expression.  This if
598 			 * statement consists of three components:
599 			 *   - the device type check
600 			 *   - the device interface check
601 			 *   - the passthrough check
602 			 * If a the matching test is successful, it
603 			 * decrements the number of matching categories,
604 			 * and if we've reached the last element that
605 			 * needed to be matched, the if statement succeeds.
606 			 *
607 			 */
608 			if ((((matches[i].match_fields & DEVSTAT_MATCH_TYPE)!=0)
609 			  && ((devices[j].device_type & DEVSTAT_TYPE_MASK) ==
610 			        (matches[i].device_type & DEVSTAT_TYPE_MASK))
611 			  &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0)
612 			   || (((matches[i].match_fields &
613 				DEVSTAT_MATCH_PASS) == 0)
614 			    && ((devices[j].device_type &
615 			        DEVSTAT_TYPE_PASS) == 0)))
616 			  && (--num_match_categories == 0))
617 			 || (((matches[i].match_fields & DEVSTAT_MATCH_IF) != 0)
618 			  && ((devices[j].device_type & DEVSTAT_TYPE_IF_MASK) ==
619 			        (matches[i].device_type & DEVSTAT_TYPE_IF_MASK))
620 			  &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0)
621 			   || (((matches[i].match_fields &
622 				DEVSTAT_MATCH_PASS) == 0)
623 			    && ((devices[j].device_type &
624 				DEVSTAT_TYPE_PASS) == 0)))
625 			  && (--num_match_categories == 0))
626 			 || (((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0)
627 			  && ((devices[j].device_type & DEVSTAT_TYPE_PASS) != 0)
628 			  && (--num_match_categories == 0))) {
629 
630 				/*
631 				 * This is probably a non-optimal solution
632 				 * to the problem that the devices in the
633 				 * device list will not be in the same
634 				 * order as the devices in the selection
635 				 * array.
636 				 */
637 				for (k = 0; k < numdevs; k++) {
638 					if ((*dev_select)[k].position == j) {
639 						found = 1;
640 						break;
641 					}
642 				}
643 
644 				/*
645 				 * There shouldn't be a case where a device
646 				 * in the device list is not in the
647 				 * selection list...but it could happen.
648 				 */
649 				if (found != 1) {
650 					fprintf(stderr, "selectdevs: couldn't"
651 						" find %s%d in selection "
652 						"list\n",
653 						devices[j].device_name,
654 						devices[j].unit_number);
655 					break;
656 				}
657 
658 				/*
659 				 * We do different things based upon the
660 				 * mode we're in.  If we're in add or only
661 				 * mode, we go ahead and select this device
662 				 * if it hasn't already been selected.  If
663 				 * it has already been selected, we leave
664 				 * it alone so we don't mess up the
665 				 * selection ordering.  Manually specified
666 				 * devices have already been selected, and
667 				 * they have higher priority than pattern
668 				 * matched devices.  If we're in remove
669 				 * mode, we de-select the given device and
670 				 * decrement the selected count.
671 				 */
672 				switch(select_mode) {
673 				case DS_SELECT_ADD:
674 				case DS_SELECT_ADDONLY:
675 				case DS_SELECT_ONLY:
676 					if ((*dev_select)[k].selected != 0)
677 						break;
678 					(*dev_select)[k].selected =
679 						++selection_number;
680 					(*num_selected)++;
681 					break;
682 				case DS_SELECT_REMOVE:
683 					(*dev_select)[k].selected = 0;
684 					(*num_selected)--;
685 					break;
686 				}
687 			}
688 		}
689 	}
690 
691 	/*
692 	 * Here we implement "top" mode.  Devices are sorted in the
693 	 * selection array based on two criteria:  whether or not they are
694 	 * selected (not selection number, just the fact that they are
695 	 * selected!) and the number of bytes in the "bytes" field of the
696 	 * selection structure.  The bytes field generally must be kept up
697 	 * by the user.  In the future, it may be maintained by library
698 	 * functions, but for now the user has to do the work.
699 	 *
700 	 * At first glance, it may seem wrong that we don't go through and
701 	 * select every device in the case where the user hasn't specified
702 	 * any devices or patterns.  In fact, though, it won't make any
703 	 * difference in the device sorting.  In that particular case (i.e.
704 	 * when we're in "add" or "only" mode, and the user hasn't
705 	 * specified anything) the first time through no devices will be
706 	 * selected, so the only criterion used to sort them will be their
707 	 * performance.  The second time through, and every time thereafter,
708 	 * all devices will be selected, so again selection won't matter.
709 	 */
710 	if (perf_select != 0) {
711 
712 		/* Sort the device array by throughput  */
713 		qsort(*dev_select, *num_selections,
714 		      sizeof(struct device_selection),
715 		      compare_select);
716 
717 		if (*num_selected == 0) {
718 			/*
719 			 * Here we select every device in the array, if it
720 			 * isn't already selected.  Because the 'selected'
721 			 * variable in the selection array entries contains
722 			 * the selection order, the devstats routine can show
723 			 * the devices that were selected first.
724 			 */
725 			for (i = 0; i < *num_selections; i++) {
726 				if ((*dev_select)[i].selected == 0) {
727 					(*dev_select)[i].selected =
728 						++selection_number;
729 					(*num_selected)++;
730 				}
731 			}
732 		} else {
733 			selection_number = 0;
734 			for (i = 0; i < *num_selections; i++) {
735 				if ((*dev_select)[i].selected != 0) {
736 					(*dev_select)[i].selected =
737 						++selection_number;
738 				}
739 			}
740 		}
741 	}
742 
743 	/*
744 	 * If we're in the "add" selection mode and if we haven't already
745 	 * selected maxshowdevs number of devices, go through the array and
746 	 * select any unselected devices.  If we're in "only" mode, we
747 	 * obviously don't want to select anything other than what the user
748 	 * specifies.  If we're in "remove" mode, it probably isn't a good
749 	 * idea to go through and select any more devices, since we might
750 	 * end up selecting something that the user wants removed.  Through
751 	 * more complicated logic, we could actually figure this out, but
752 	 * that would probably require combining this loop with the various
753 	 * selections loops above.
754 	 */
755 	if ((select_mode == DS_SELECT_ADD) && (*num_selected < maxshowdevs)) {
756 		for (i = 0; i < *num_selections; i++)
757 			if ((*dev_select)[i].selected == 0) {
758 				(*dev_select)[i].selected = ++selection_number;
759 				(*num_selected)++;
760 			}
761 	}
762 
763 	/*
764 	 * Look at the number of devices that have been selected.  If it
765 	 * has changed, set the changed variable.  Otherwise, if we've
766 	 * made a backup of the selection list, compare it to the current
767 	 * selection list to see if the selected devices have changed.
768 	 */
769 	if ((changed == 0) && (old_num_selected != *num_selected))
770 		changed = 1;
771 	else if ((changed == 0) && (old_dev_select != NULL)) {
772 		/*
773 		 * Now we go through the selection list and we look at
774 		 * it three different ways.
775 		 */
776 		for (i = 0; (i < *num_selections) && (changed == 0) &&
777 		     (i < old_num_selections); i++) {
778 			/*
779 			 * If the device at index i in both the new and old
780 			 * selection arrays has the same device number and
781 			 * selection status, it hasn't changed.  We
782 			 * continue on to the next index.
783 			 */
784 			if (((*dev_select)[i].device_number ==
785 			     old_dev_select[i].device_number)
786 			 && ((*dev_select)[i].selected ==
787 			     old_dev_select[i].selected))
788 				continue;
789 
790 			/*
791 			 * Now, if we're still going through the if
792 			 * statement, the above test wasn't true.  So we
793 			 * check here to see if the device at index i in
794 			 * the current array is the same as the device at
795 			 * index i in the old array.  If it is, that means
796 			 * that its selection number has changed.  Set
797 			 * changed to 1 and exit the loop.
798 			 */
799 			else if ((*dev_select)[i].device_number ==
800 			          old_dev_select[i].device_number) {
801 				changed = 1;
802 				break;
803 			}
804 			/*
805 			 * If we get here, then the device at index i in
806 			 * the current array isn't the same device as the
807 			 * device at index i in the old array.
808 			 */
809 			else {
810 				int found = 0;
811 
812 				/*
813 				 * Search through the old selection array
814 				 * looking for a device with the same
815 				 * device number as the device at index i
816 				 * in the current array.  If the selection
817 				 * status is the same, then we mark it as
818 				 * found.  If the selection status isn't
819 				 * the same, we break out of the loop.
820 				 * Since found isn't set, changed will be
821 				 * set to 1 below.
822 				 */
823 				for (j = 0; j < old_num_selections; j++) {
824 					if (((*dev_select)[i].device_number ==
825 					      old_dev_select[j].device_number)
826 					 && ((*dev_select)[i].selected ==
827 					      old_dev_select[j].selected)){
828 						found = 1;
829 						break;
830 					}
831 					else if ((*dev_select)[i].device_number
832 					    == old_dev_select[j].device_number)
833 						break;
834 				}
835 				if (found == 0)
836 					changed = 1;
837 			}
838 		}
839 	}
840 	if (old_dev_select != NULL)
841 		free(old_dev_select);
842 
843 	return(changed);
844 }
845 
846 /*
847  * Comparison routine for qsort() above.  Note that the comparison here is
848  * backwards -- generally, it should return a value to indicate whether
849  * arg1 is <, =, or > arg2.  Instead, it returns the opposite.  The reason
850  * it returns the opposite is so that the selection array will be sorted in
851  * order of decreasing performance.  We sort on two parameters.  The first
852  * sort key is whether or not one or the other of the devices in question
853  * has been selected.  If one of them has, and the other one has not, the
854  * selected device is automatically more important than the unselected
855  * device.  If neither device is selected, we judge the devices based upon
856  * performance.
857  */
858 static int
859 compare_select(const void *arg1, const void *arg2)
860 {
861 	if ((((struct device_selection *)arg1)->selected)
862 	 && (((struct device_selection *)arg2)->selected == 0))
863 		return(-1);
864 	else if ((((struct device_selection *)arg1)->selected == 0)
865 	      && (((struct device_selection *)arg2)->selected))
866 		return(1);
867 	else if (((struct device_selection *)arg2)->bytes <
868 	         ((struct device_selection *)arg1)->bytes)
869 		return(-1);
870 	else if (((struct device_selection *)arg2)->bytes >
871 		 ((struct device_selection *)arg1)->bytes)
872 		return(1);
873 	else
874 		return(0);
875 }
876 
877 /*
878  * Take a string with the general format "arg1,arg2,arg3", and build a
879  * device matching expression from it.
880  */
881 int
882 buildmatch(char *match_str, struct devstat_match **matches, int *num_matches)
883 {
884 	char *tstr[5];
885 	char **tempstr;
886 	int num_args;
887 	register int i, j;
888 	char *func_name = "buildmatch";
889 
890 	/* We can't do much without a string to parse */
891 	if (match_str == NULL) {
892 		sprintf(devstat_errbuf, "%s: no match expression", func_name);
893 		return(-1);
894 	}
895 
896 	/*
897 	 * Break the (comma delimited) input string out into separate strings.
898 	 */
899 	for (tempstr = tstr, num_args  = 0;
900 	     (*tempstr = strsep(&match_str, ",")) != NULL && (num_args < 5);
901 	     num_args++)
902 		if (**tempstr != '\0')
903 			if (++tempstr >= &tstr[5])
904 				break;
905 
906 	/* The user gave us too many type arguments */
907 	if (num_args > 3) {
908 		sprintf(devstat_errbuf, "%s: too many type arguments",
909 			func_name);
910 		return(-1);
911 	}
912 
913 	/*
914 	 * Since you can't realloc a pointer that hasn't been malloced
915 	 * first, we malloc first and then realloc.
916 	 */
917 	if (*num_matches == 0)
918 		*matches = (struct devstat_match *)malloc(
919 			   sizeof(struct devstat_match));
920 	else
921 		*matches = (struct devstat_match *)realloc(*matches,
922 			  sizeof(struct devstat_match) * (*num_matches + 1));
923 
924 	/* Make sure the current entry is clear */
925 	bzero(&matches[0][*num_matches], sizeof(struct devstat_match));
926 
927 	/*
928 	 * Step through the arguments the user gave us and build a device
929 	 * matching expression from them.
930 	 */
931 	for (i = 0; i < num_args; i++) {
932 		char *tempstr2, *tempstr3;
933 
934 		/*
935 		 * Get rid of leading white space.
936 		 */
937 		tempstr2 = tstr[i];
938 		while (isspace(*tempstr2) && (*tempstr2 != '\0'))
939 			tempstr2++;
940 
941 		/*
942 		 * Get rid of trailing white space.
943 		 */
944 		tempstr3 = &tempstr2[strlen(tempstr2) - 1];
945 
946 		while ((*tempstr3 != '\0') && (tempstr3 > tempstr2)
947 		    && (isspace(*tempstr3))) {
948 			*tempstr3 = '\0';
949 			tempstr3--;
950 		}
951 
952 		/*
953 		 * Go through the match table comparing the user's
954 		 * arguments to known device types, interfaces, etc.
955 		 */
956 		for (j = 0; match_table[j].match_str != NULL; j++) {
957 			/*
958 			 * We do case-insensitive matching, in case someone
959 			 * wants to enter "SCSI" instead of "scsi" or
960 			 * something like that.  Only compare as many
961 			 * characters as are in the string in the match
962 			 * table.  This should help if someone tries to use
963 			 * a super-long match expression.
964 			 */
965 			if (strncasecmp(tempstr2, match_table[j].match_str,
966 			    strlen(match_table[j].match_str)) == 0) {
967 				/*
968 				 * Make sure the user hasn't specified two
969 				 * items of the same type, like "da" and
970 				 * "cd".  One device cannot be both.
971 				 */
972 				if (((*matches)[*num_matches].match_fields &
973 				    match_table[j].match_field) != 0) {
974 					sprintf(devstat_errbuf,
975 						"%s: cannot have more than "
976 						"one match item in a single "
977 						"category", func_name);
978 					return(-1);
979 				}
980 				/*
981 				 * If we've gotten this far, we have a
982 				 * winner.  Set the appropriate fields in
983 				 * the match entry.
984 				 */
985 				(*matches)[*num_matches].match_fields |=
986 					match_table[j].match_field;
987 				(*matches)[*num_matches].device_type |=
988 					match_table[j].type;
989 				(*matches)[*num_matches].num_match_categories++;
990 				break;
991 			}
992 		}
993 		/*
994 		 * We should have found a match in the above for loop.  If
995 		 * not, that means the user entered an invalid device type
996 		 * or interface.
997 		 */
998 		if ((*matches)[*num_matches].num_match_categories != (i + 1)) {
999 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1000 				"%s: unknown match item \"%s\"", func_name,
1001 				tstr[i]);
1002 			return(-1);
1003 		}
1004 	}
1005 
1006 	(*num_matches)++;
1007 
1008 	return(0);
1009 }
1010 
1011 /*
1012  * Compute a number of device statistics.  Only one field is mandatory, and
1013  * that is "current".  Everything else is optional.  The caller passes in
1014  * pointers to variables to hold the various statistics he desires.  If he
1015  * doesn't want a particular staistic, he should pass in a NULL pointer.
1016  * Return values:
1017  * 0   -- success
1018  * -1  -- failure
1019  */
1020 int
1021 compute_stats(struct devstat *current, struct devstat *previous,
1022 	      long double etime, u_int64_t *total_bytes,
1023 	      u_int64_t *total_transfers, u_int64_t *total_blocks,
1024 	      long double *kb_per_transfer, long double *transfers_per_second,
1025 	      long double *mb_per_second, long double *blocks_per_second,
1026 	      long double *ms_per_transaction)
1027 {
1028 	u_int64_t totalbytes, totaltransfers, totalblocks;
1029 	char *func_name = "compute_stats";
1030 
1031 	/*
1032 	 * current is the only mandatory field.
1033 	 */
1034 	if (current == NULL) {
1035 		sprintf(devstat_errbuf, "%s: current stats structure was NULL",
1036 			func_name);
1037 		return(-1);
1038 	}
1039 
1040 	totalbytes = (current->bytes_written + current->bytes_read) -
1041 		     ((previous) ? (previous->bytes_written +
1042 				    previous->bytes_read) : 0);
1043 
1044 	if (total_bytes)
1045 		*total_bytes = totalbytes;
1046 
1047 	totaltransfers = (current->num_reads +
1048 			  current->num_writes +
1049 			  current->num_other) -
1050 			 ((previous) ?
1051 			  (previous->num_reads +
1052 			   previous->num_writes +
1053 			   previous->num_other) : 0);
1054 	if (total_transfers)
1055 		*total_transfers = totaltransfers;
1056 
1057 	if (transfers_per_second) {
1058 		if (etime > 0.0) {
1059 			*transfers_per_second = totaltransfers;
1060 			*transfers_per_second /= etime;
1061 		} else
1062 			*transfers_per_second = 0.0;
1063 	}
1064 
1065 	if (kb_per_transfer) {
1066 		*kb_per_transfer = totalbytes;
1067 		*kb_per_transfer /= 1024;
1068 		if (totaltransfers > 0)
1069 			*kb_per_transfer /= totaltransfers;
1070 		else
1071 			*kb_per_transfer = 0.0;
1072 	}
1073 
1074 	if (mb_per_second) {
1075 		*mb_per_second = totalbytes;
1076 		*mb_per_second /= 1024 * 1024;
1077 		if (etime > 0.0)
1078 			*mb_per_second /= etime;
1079 		else
1080 			*mb_per_second = 0.0;
1081 	}
1082 
1083 	totalblocks = totalbytes;
1084 	if (current->block_size > 0)
1085 		totalblocks /= current->block_size;
1086 	else
1087 		totalblocks /= 512;
1088 
1089 	if (total_blocks)
1090 		*total_blocks = totalblocks;
1091 
1092 	if (blocks_per_second) {
1093 		*blocks_per_second = totalblocks;
1094 		if (etime > 0.0)
1095 			*blocks_per_second /= etime;
1096 		else
1097 			*blocks_per_second = 0.0;
1098 	}
1099 
1100 	if (ms_per_transaction) {
1101 		if (totaltransfers > 0) {
1102 			*ms_per_transaction = etime;
1103 			*ms_per_transaction /= totaltransfers;
1104 			*ms_per_transaction *= 1000;
1105 		} else
1106 			*ms_per_transaction = 0.0;
1107 	}
1108 
1109 	return(0);
1110 }
1111 
1112 long double
1113 compute_etime(struct timeval cur_time, struct timeval prev_time)
1114 {
1115 	struct timeval busy_time;
1116 	u_int64_t busy_usec;
1117 	long double etime;
1118 
1119 	timersub(&cur_time, &prev_time, &busy_time);
1120 
1121         busy_usec = busy_time.tv_sec;
1122         busy_usec *= 1000000;
1123         busy_usec += busy_time.tv_usec;
1124         etime = busy_usec;
1125         etime /= 1000000;
1126 
1127 	return(etime);
1128 }
1129