xref: /freebsd/lib/libdevstat/devstat.c (revision 6419bb52)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1997, 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 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/types.h>
35 #include <sys/sysctl.h>
36 #include <sys/errno.h>
37 #include <sys/resource.h>
38 #include <sys/queue.h>
39 
40 #include <ctype.h>
41 #include <err.h>
42 #include <fcntl.h>
43 #include <limits.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <stdarg.h>
48 #include <kvm.h>
49 #include <nlist.h>
50 
51 #include "devstat.h"
52 
53 int
54 compute_stats(struct devstat *current, struct devstat *previous,
55 	      long double etime, u_int64_t *total_bytes,
56 	      u_int64_t *total_transfers, u_int64_t *total_blocks,
57 	      long double *kb_per_transfer, long double *transfers_per_second,
58 	      long double *mb_per_second, long double *blocks_per_second,
59 	      long double *ms_per_transaction);
60 
61 typedef enum {
62 	DEVSTAT_ARG_NOTYPE,
63 	DEVSTAT_ARG_UINT64,
64 	DEVSTAT_ARG_LD,
65 	DEVSTAT_ARG_SKIP
66 } devstat_arg_type;
67 
68 char devstat_errbuf[DEVSTAT_ERRBUF_SIZE];
69 
70 /*
71  * Table to match descriptive strings with device types.  These are in
72  * order from most common to least common to speed search time.
73  */
74 struct devstat_match_table match_table[] = {
75 	{"da",		DEVSTAT_TYPE_DIRECT,	DEVSTAT_MATCH_TYPE},
76 	{"cd",		DEVSTAT_TYPE_CDROM,	DEVSTAT_MATCH_TYPE},
77 	{"scsi",	DEVSTAT_TYPE_IF_SCSI,	DEVSTAT_MATCH_IF},
78 	{"ide",		DEVSTAT_TYPE_IF_IDE,	DEVSTAT_MATCH_IF},
79 	{"other",	DEVSTAT_TYPE_IF_OTHER,	DEVSTAT_MATCH_IF},
80 	{"worm",	DEVSTAT_TYPE_WORM,	DEVSTAT_MATCH_TYPE},
81 	{"sa",		DEVSTAT_TYPE_SEQUENTIAL,DEVSTAT_MATCH_TYPE},
82 	{"pass",	DEVSTAT_TYPE_PASS,	DEVSTAT_MATCH_PASS},
83 	{"optical",	DEVSTAT_TYPE_OPTICAL,	DEVSTAT_MATCH_TYPE},
84 	{"array",	DEVSTAT_TYPE_STORARRAY,	DEVSTAT_MATCH_TYPE},
85 	{"changer",	DEVSTAT_TYPE_CHANGER,	DEVSTAT_MATCH_TYPE},
86 	{"scanner",	DEVSTAT_TYPE_SCANNER,	DEVSTAT_MATCH_TYPE},
87 	{"printer",	DEVSTAT_TYPE_PRINTER,	DEVSTAT_MATCH_TYPE},
88 	{"floppy",	DEVSTAT_TYPE_FLOPPY,	DEVSTAT_MATCH_TYPE},
89 	{"proc",	DEVSTAT_TYPE_PROCESSOR,	DEVSTAT_MATCH_TYPE},
90 	{"comm",	DEVSTAT_TYPE_COMM,	DEVSTAT_MATCH_TYPE},
91 	{"enclosure",	DEVSTAT_TYPE_ENCLOSURE,	DEVSTAT_MATCH_TYPE},
92 	{NULL,		0,			0}
93 };
94 
95 struct devstat_args {
96 	devstat_metric 		metric;
97 	devstat_arg_type	argtype;
98 } devstat_arg_list[] = {
99 	{ DSM_NONE, DEVSTAT_ARG_NOTYPE },
100 	{ DSM_TOTAL_BYTES, DEVSTAT_ARG_UINT64 },
101 	{ DSM_TOTAL_BYTES_READ, DEVSTAT_ARG_UINT64 },
102 	{ DSM_TOTAL_BYTES_WRITE, DEVSTAT_ARG_UINT64 },
103 	{ DSM_TOTAL_TRANSFERS, DEVSTAT_ARG_UINT64 },
104 	{ DSM_TOTAL_TRANSFERS_READ, DEVSTAT_ARG_UINT64 },
105 	{ DSM_TOTAL_TRANSFERS_WRITE, DEVSTAT_ARG_UINT64 },
106 	{ DSM_TOTAL_TRANSFERS_OTHER, DEVSTAT_ARG_UINT64 },
107 	{ DSM_TOTAL_BLOCKS, DEVSTAT_ARG_UINT64 },
108 	{ DSM_TOTAL_BLOCKS_READ, DEVSTAT_ARG_UINT64 },
109 	{ DSM_TOTAL_BLOCKS_WRITE, DEVSTAT_ARG_UINT64 },
110 	{ DSM_KB_PER_TRANSFER, DEVSTAT_ARG_LD },
111 	{ DSM_KB_PER_TRANSFER_READ, DEVSTAT_ARG_LD },
112 	{ DSM_KB_PER_TRANSFER_WRITE, DEVSTAT_ARG_LD },
113 	{ DSM_TRANSFERS_PER_SECOND, DEVSTAT_ARG_LD },
114 	{ DSM_TRANSFERS_PER_SECOND_READ, DEVSTAT_ARG_LD },
115 	{ DSM_TRANSFERS_PER_SECOND_WRITE, DEVSTAT_ARG_LD },
116 	{ DSM_TRANSFERS_PER_SECOND_OTHER, DEVSTAT_ARG_LD },
117 	{ DSM_MB_PER_SECOND, DEVSTAT_ARG_LD },
118 	{ DSM_MB_PER_SECOND_READ, DEVSTAT_ARG_LD },
119 	{ DSM_MB_PER_SECOND_WRITE, DEVSTAT_ARG_LD },
120 	{ DSM_BLOCKS_PER_SECOND, DEVSTAT_ARG_LD },
121 	{ DSM_BLOCKS_PER_SECOND_READ, DEVSTAT_ARG_LD },
122 	{ DSM_BLOCKS_PER_SECOND_WRITE, DEVSTAT_ARG_LD },
123 	{ DSM_MS_PER_TRANSACTION, DEVSTAT_ARG_LD },
124 	{ DSM_MS_PER_TRANSACTION_READ, DEVSTAT_ARG_LD },
125 	{ DSM_MS_PER_TRANSACTION_WRITE, DEVSTAT_ARG_LD },
126 	{ DSM_SKIP, DEVSTAT_ARG_SKIP },
127 	{ DSM_TOTAL_BYTES_FREE, DEVSTAT_ARG_UINT64 },
128 	{ DSM_TOTAL_TRANSFERS_FREE, DEVSTAT_ARG_UINT64 },
129 	{ DSM_TOTAL_BLOCKS_FREE, DEVSTAT_ARG_UINT64 },
130 	{ DSM_KB_PER_TRANSFER_FREE, DEVSTAT_ARG_LD },
131 	{ DSM_MB_PER_SECOND_FREE, DEVSTAT_ARG_LD },
132 	{ DSM_TRANSFERS_PER_SECOND_FREE, DEVSTAT_ARG_LD },
133 	{ DSM_BLOCKS_PER_SECOND_FREE, DEVSTAT_ARG_LD },
134 	{ DSM_MS_PER_TRANSACTION_OTHER, DEVSTAT_ARG_LD },
135 	{ DSM_MS_PER_TRANSACTION_FREE, DEVSTAT_ARG_LD },
136 	{ DSM_BUSY_PCT, DEVSTAT_ARG_LD },
137 	{ DSM_QUEUE_LENGTH, DEVSTAT_ARG_UINT64 },
138 	{ DSM_TOTAL_DURATION, DEVSTAT_ARG_LD },
139 	{ DSM_TOTAL_DURATION_READ, DEVSTAT_ARG_LD },
140 	{ DSM_TOTAL_DURATION_WRITE, DEVSTAT_ARG_LD },
141 	{ DSM_TOTAL_DURATION_FREE, DEVSTAT_ARG_LD },
142 	{ DSM_TOTAL_DURATION_OTHER, DEVSTAT_ARG_LD },
143 	{ DSM_TOTAL_BUSY_TIME, DEVSTAT_ARG_LD },
144 };
145 
146 static const char *namelist[] = {
147 #define X_NUMDEVS	0
148 	"_devstat_num_devs",
149 #define X_GENERATION	1
150 	"_devstat_generation",
151 #define X_VERSION	2
152 	"_devstat_version",
153 #define X_DEVICE_STATQ	3
154 	"_device_statq",
155 #define X_TIME_UPTIME	4
156 	"_time_uptime",
157 #define X_END		5
158 };
159 
160 /*
161  * Local function declarations.
162  */
163 static int compare_select(const void *arg1, const void *arg2);
164 static int readkmem(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes);
165 static int readkmem_nl(kvm_t *kd, const char *name, void *buf, size_t nbytes);
166 static char *get_devstat_kvm(kvm_t *kd);
167 
168 #define KREADNL(kd, var, val) \
169 	readkmem_nl(kd, namelist[var], &val, sizeof(val))
170 
171 int
172 devstat_getnumdevs(kvm_t *kd)
173 {
174 	size_t numdevsize;
175 	int numdevs;
176 
177 	numdevsize = sizeof(int);
178 
179 	/*
180 	 * Find out how many devices we have in the system.
181 	 */
182 	if (kd == NULL) {
183 		if (sysctlbyname("kern.devstat.numdevs", &numdevs,
184 				 &numdevsize, NULL, 0) == -1) {
185 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
186 				 "%s: error getting number of devices\n"
187 				 "%s: %s", __func__, __func__,
188 				 strerror(errno));
189 			return(-1);
190 		} else
191 			return(numdevs);
192 	} else {
193 
194 		if (KREADNL(kd, X_NUMDEVS, numdevs) == -1)
195 			return(-1);
196 		else
197 			return(numdevs);
198 	}
199 }
200 
201 /*
202  * This is an easy way to get the generation number, but the generation is
203  * supplied in a more atmoic manner by the kern.devstat.all sysctl.
204  * Because this generation sysctl is separate from the statistics sysctl,
205  * the device list and the generation could change between the time that
206  * this function is called and the device list is retrieved.
207  */
208 long
209 devstat_getgeneration(kvm_t *kd)
210 {
211 	size_t gensize;
212 	long generation;
213 
214 	gensize = sizeof(long);
215 
216 	/*
217 	 * Get the current generation number.
218 	 */
219 	if (kd == NULL) {
220 		if (sysctlbyname("kern.devstat.generation", &generation,
221 				 &gensize, NULL, 0) == -1) {
222 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
223 				 "%s: error getting devstat generation\n%s: %s",
224 				 __func__, __func__, strerror(errno));
225 			return(-1);
226 		} else
227 			return(generation);
228 	} else {
229 		if (KREADNL(kd, X_GENERATION, generation) == -1)
230 			return(-1);
231 		else
232 			return(generation);
233 	}
234 }
235 
236 /*
237  * Get the current devstat version.  The return value of this function
238  * should be compared with DEVSTAT_VERSION, which is defined in
239  * sys/devicestat.h.  This will enable userland programs to determine
240  * whether they are out of sync with the kernel.
241  */
242 int
243 devstat_getversion(kvm_t *kd)
244 {
245 	size_t versize;
246 	int version;
247 
248 	versize = sizeof(int);
249 
250 	/*
251 	 * Get the current devstat version.
252 	 */
253 	if (kd == NULL) {
254 		if (sysctlbyname("kern.devstat.version", &version, &versize,
255 				 NULL, 0) == -1) {
256 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
257 				 "%s: error getting devstat version\n%s: %s",
258 				 __func__, __func__, strerror(errno));
259 			return(-1);
260 		} else
261 			return(version);
262 	} else {
263 		if (KREADNL(kd, X_VERSION, version) == -1)
264 			return(-1);
265 		else
266 			return(version);
267 	}
268 }
269 
270 /*
271  * Check the devstat version we know about against the devstat version the
272  * kernel knows about.  If they don't match, print an error into the
273  * devstat error buffer, and return -1.  If they match, return 0.
274  */
275 int
276 devstat_checkversion(kvm_t *kd)
277 {
278 	int buflen, res, retval = 0, version;
279 
280 	version = devstat_getversion(kd);
281 
282 	if (version != DEVSTAT_VERSION) {
283 		/*
284 		 * If getversion() returns an error (i.e. -1), then it
285 		 * has printed an error message in the buffer.  Therefore,
286 		 * we need to add a \n to the end of that message before we
287 		 * print our own message in the buffer.
288 		 */
289 		if (version == -1)
290 			buflen = strlen(devstat_errbuf);
291 		else
292 			buflen = 0;
293 
294 		res = snprintf(devstat_errbuf + buflen,
295 			       DEVSTAT_ERRBUF_SIZE - buflen,
296 			       "%s%s: userland devstat version %d is not "
297 			       "the same as the kernel\n%s: devstat "
298 			       "version %d\n", version == -1 ? "\n" : "",
299 			       __func__, DEVSTAT_VERSION, __func__, version);
300 
301 		if (res < 0)
302 			devstat_errbuf[buflen] = '\0';
303 
304 		buflen = strlen(devstat_errbuf);
305 		if (version < DEVSTAT_VERSION)
306 			res = snprintf(devstat_errbuf + buflen,
307 				       DEVSTAT_ERRBUF_SIZE - buflen,
308 				       "%s: libdevstat newer than kernel\n",
309 				       __func__);
310 		else
311 			res = snprintf(devstat_errbuf + buflen,
312 				       DEVSTAT_ERRBUF_SIZE - buflen,
313 				       "%s: kernel newer than libdevstat\n",
314 				       __func__);
315 
316 		if (res < 0)
317 			devstat_errbuf[buflen] = '\0';
318 
319 		retval = -1;
320 	}
321 
322 	return(retval);
323 }
324 
325 /*
326  * Get the current list of devices and statistics, and the current
327  * generation number.
328  *
329  * Return values:
330  * -1  -- error
331  *  0  -- device list is unchanged
332  *  1  -- device list has changed
333  */
334 int
335 devstat_getdevs(kvm_t *kd, struct statinfo *stats)
336 {
337 	int error;
338 	size_t dssize;
339 	long oldgeneration;
340 	int retval = 0;
341 	struct devinfo *dinfo;
342 	struct timespec ts;
343 
344 	dinfo = stats->dinfo;
345 
346 	if (dinfo == NULL) {
347 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
348 			 "%s: stats->dinfo was NULL", __func__);
349 		return(-1);
350 	}
351 
352 	oldgeneration = dinfo->generation;
353 
354 	if (kd == NULL) {
355 		clock_gettime(CLOCK_MONOTONIC, &ts);
356 		stats->snap_time = ts.tv_sec + ts.tv_nsec * 1e-9;
357 
358 		/* If this is our first time through, mem_ptr will be null. */
359 		if (dinfo->mem_ptr == NULL) {
360 			/*
361 			 * Get the number of devices.  If it's negative, it's an
362 			 * error.  Don't bother setting the error string, since
363 			 * getnumdevs() has already done that for us.
364 			 */
365 			if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0)
366 				return(-1);
367 
368 			/*
369 			 * The kern.devstat.all sysctl returns the current
370 			 * generation number, as well as all the devices.
371 			 * So we need four bytes more.
372 			 */
373 			dssize = (dinfo->numdevs * sizeof(struct devstat)) +
374 				 sizeof(long);
375 			dinfo->mem_ptr = (u_int8_t *)malloc(dssize);
376 			if (dinfo->mem_ptr == NULL) {
377 				snprintf(devstat_errbuf, sizeof(devstat_errbuf),
378 					 "%s: Cannot allocate memory for mem_ptr element",
379 					 __func__);
380 				return(-1);
381 			}
382 		} else
383 			dssize = (dinfo->numdevs * sizeof(struct devstat)) +
384 				 sizeof(long);
385 
386 		/*
387 		 * Request all of the devices.  We only really allow for one
388 		 * ENOMEM failure.  It would, of course, be possible to just go
389 		 * in a loop and keep reallocing the device structure until we
390 		 * don't get ENOMEM back.  I'm not sure it's worth it, though.
391 		 * If devices are being added to the system that quickly, maybe
392 		 * the user can just wait until all devices are added.
393 		 */
394 		for (;;) {
395 			error = sysctlbyname("kern.devstat.all",
396 					     dinfo->mem_ptr,
397 					     &dssize, NULL, 0);
398 			if (error != -1 || errno != EBUSY)
399 				break;
400 		}
401 		if (error == -1) {
402 			/*
403 			 * If we get ENOMEM back, that means that there are
404 			 * more devices now, so we need to allocate more
405 			 * space for the device array.
406 			 */
407 			if (errno == ENOMEM) {
408 				/*
409 				 * No need to set the error string here,
410 				 * devstat_getnumdevs() will do that if it fails.
411 				 */
412 				if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0)
413 					return(-1);
414 
415 				dssize = (dinfo->numdevs *
416 					sizeof(struct devstat)) + sizeof(long);
417 				dinfo->mem_ptr = (u_int8_t *)
418 					realloc(dinfo->mem_ptr, dssize);
419 				if ((error = sysctlbyname("kern.devstat.all",
420 				    dinfo->mem_ptr, &dssize, NULL, 0)) == -1) {
421 					snprintf(devstat_errbuf,
422 						 sizeof(devstat_errbuf),
423 					    	 "%s: error getting device "
424 					    	 "stats\n%s: %s", __func__,
425 					    	 __func__, strerror(errno));
426 					return(-1);
427 				}
428 			} else {
429 				snprintf(devstat_errbuf, sizeof(devstat_errbuf),
430 					 "%s: error getting device stats\n"
431 					 "%s: %s", __func__, __func__,
432 					 strerror(errno));
433 				return(-1);
434 			}
435 		}
436 
437 	} else {
438 		if (KREADNL(kd, X_TIME_UPTIME, ts.tv_sec) == -1)
439 			return(-1);
440 		else
441 			stats->snap_time = ts.tv_sec;
442 
443 		/*
444 		 * This is of course non-atomic, but since we are working
445 		 * on a core dump, the generation is unlikely to change
446 		 */
447 		if ((dinfo->numdevs = devstat_getnumdevs(kd)) == -1)
448 			return(-1);
449 		if ((dinfo->mem_ptr = (u_int8_t *)get_devstat_kvm(kd)) == NULL)
450 			return(-1);
451 	}
452 	/*
453 	 * The sysctl spits out the generation as the first four bytes,
454 	 * then all of the device statistics structures.
455 	 */
456 	dinfo->generation = *(long *)dinfo->mem_ptr;
457 
458 	/*
459 	 * If the generation has changed, and if the current number of
460 	 * devices is not the same as the number of devices recorded in the
461 	 * devinfo structure, it is likely that the device list has shrunk.
462 	 * The reason that it is likely that the device list has shrunk in
463 	 * this case is that if the device list has grown, the sysctl above
464 	 * will return an ENOMEM error, and we will reset the number of
465 	 * devices and reallocate the device array.  If the second sysctl
466 	 * fails, we will return an error and therefore never get to this
467 	 * point.  If the device list has shrunk, the sysctl will not
468 	 * return an error since we have more space allocated than is
469 	 * necessary.  So, in the shrinkage case, we catch it here and
470 	 * reallocate the array so that we don't use any more space than is
471 	 * necessary.
472 	 */
473 	if (oldgeneration != dinfo->generation) {
474 		if (devstat_getnumdevs(kd) != dinfo->numdevs) {
475 			if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0)
476 				return(-1);
477 			dssize = (dinfo->numdevs * sizeof(struct devstat)) +
478 				sizeof(long);
479 			dinfo->mem_ptr = (u_int8_t *)realloc(dinfo->mem_ptr,
480 							     dssize);
481 		}
482 		retval = 1;
483 	}
484 
485 	dinfo->devices = (struct devstat *)(dinfo->mem_ptr + sizeof(long));
486 
487 	return(retval);
488 }
489 
490 /*
491  * selectdevs():
492  *
493  * Devices are selected/deselected based upon the following criteria:
494  * - devices specified by the user on the command line
495  * - devices matching any device type expressions given on the command line
496  * - devices with the highest I/O, if 'top' mode is enabled
497  * - the first n unselected devices in the device list, if maxshowdevs
498  *   devices haven't already been selected and if the user has not
499  *   specified any devices on the command line and if we're in "add" mode.
500  *
501  * Input parameters:
502  * - device selection list (dev_select)
503  * - current number of devices selected (num_selected)
504  * - total number of devices in the selection list (num_selections)
505  * - devstat generation as of the last time selectdevs() was called
506  *   (select_generation)
507  * - current devstat generation (current_generation)
508  * - current list of devices and statistics (devices)
509  * - number of devices in the current device list (numdevs)
510  * - compiled version of the command line device type arguments (matches)
511  *   - This is optional.  If the number of devices is 0, this will be ignored.
512  *   - The matching code pays attention to the current selection mode.  So
513  *     if you pass in a matching expression, it will be evaluated based
514  *     upon the selection mode that is passed in.  See below for details.
515  * - number of device type matching expressions (num_matches)
516  *   - Set to 0 to disable the matching code.
517  * - list of devices specified on the command line by the user (dev_selections)
518  * - number of devices selected on the command line by the user
519  *   (num_dev_selections)
520  * - Our selection mode.  There are four different selection modes:
521  *      - add mode.  (DS_SELECT_ADD) Any devices matching devices explicitly
522  *        selected by the user or devices matching a pattern given by the
523  *        user will be selected in addition to devices that are already
524  *        selected.  Additional devices will be selected, up to maxshowdevs
525  *        number of devices.
526  *      - only mode. (DS_SELECT_ONLY)  Only devices matching devices
527  *        explicitly given by the user or devices matching a pattern
528  *        given by the user will be selected.  No other devices will be
529  *        selected.
530  *      - addonly mode.  (DS_SELECT_ADDONLY)  This is similar to add and
531  *        only.  Basically, this will not de-select any devices that are
532  *        current selected, as only mode would, but it will also not
533  *        gratuitously select up to maxshowdevs devices as add mode would.
534  *      - remove mode.  (DS_SELECT_REMOVE)  Any devices matching devices
535  *        explicitly selected by the user or devices matching a pattern
536  *        given by the user will be de-selected.
537  * - maximum number of devices we can select (maxshowdevs)
538  * - flag indicating whether or not we're in 'top' mode (perf_select)
539  *
540  * Output data:
541  * - the device selection list may be modified and passed back out
542  * - the number of devices selected and the total number of items in the
543  *   device selection list may be changed
544  * - the selection generation may be changed to match the current generation
545  *
546  * Return values:
547  * -1  -- error
548  *  0  -- selected devices are unchanged
549  *  1  -- selected devices changed
550  */
551 int
552 devstat_selectdevs(struct device_selection **dev_select, int *num_selected,
553 		   int *num_selections, long *select_generation,
554 		   long current_generation, struct devstat *devices,
555 		   int numdevs, struct devstat_match *matches, int num_matches,
556 		   char **dev_selections, int num_dev_selections,
557 		   devstat_select_mode select_mode, int maxshowdevs,
558 		   int perf_select)
559 {
560 	int i, j, k;
561 	int init_selections = 0, init_selected_var = 0;
562 	struct device_selection *old_dev_select = NULL;
563 	int old_num_selections = 0, old_num_selected;
564 	int selection_number = 0;
565 	int changed = 0, found = 0;
566 
567 	if ((dev_select == NULL) || (devices == NULL) || (numdevs < 0))
568 		return(-1);
569 
570 	/*
571 	 * We always want to make sure that we have as many dev_select
572 	 * entries as there are devices.
573 	 */
574 	/*
575 	 * In this case, we haven't selected devices before.
576 	 */
577 	if (*dev_select == NULL) {
578 		*dev_select = (struct device_selection *)malloc(numdevs *
579 			sizeof(struct device_selection));
580 		*select_generation = current_generation;
581 		init_selections = 1;
582 		changed = 1;
583 	/*
584 	 * In this case, we have selected devices before, but the device
585 	 * list has changed since we last selected devices, so we need to
586 	 * either enlarge or reduce the size of the device selection list.
587 	 * But delay the resizing until after copying the data to old_dev_select
588 	 * as to not lose any data in the case of reducing the size.
589 	 */
590 	} else if (*num_selections != numdevs) {
591 		*select_generation = current_generation;
592 		init_selections = 1;
593 	/*
594 	 * In this case, we've selected devices before, and the selection
595 	 * list is the same size as it was the last time, but the device
596 	 * list has changed.
597 	 */
598 	} else if (*select_generation < current_generation) {
599 		*select_generation = current_generation;
600 		init_selections = 1;
601 	}
602 
603 	if (*dev_select == NULL) {
604 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
605 			 "%s: Cannot (re)allocate memory for dev_select argument",
606 			 __func__);
607 		return(-1);
608 	}
609 
610 	/*
611 	 * If we're in "only" mode, we want to clear out the selected
612 	 * variable since we're going to select exactly what the user wants
613 	 * this time through.
614 	 */
615 	if (select_mode == DS_SELECT_ONLY)
616 		init_selected_var = 1;
617 
618 	/*
619 	 * In all cases, we want to back up the number of selected devices.
620 	 * It is a quick and accurate way to determine whether the selected
621 	 * devices have changed.
622 	 */
623 	old_num_selected = *num_selected;
624 
625 	/*
626 	 * We want to make a backup of the current selection list if
627 	 * the list of devices has changed, or if we're in performance
628 	 * selection mode.  In both cases, we don't want to make a backup
629 	 * if we already know for sure that the list will be different.
630 	 * This is certainly the case if this is our first time through the
631 	 * selection code.
632 	 */
633 	if (((init_selected_var != 0) || (init_selections != 0)
634 	 || (perf_select != 0)) && (changed == 0)){
635 		old_dev_select = (struct device_selection *)malloc(
636 		    *num_selections * sizeof(struct device_selection));
637 		if (old_dev_select == NULL) {
638 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
639 				 "%s: Cannot allocate memory for selection list backup",
640 				 __func__);
641 			return(-1);
642 		}
643 		old_num_selections = *num_selections;
644 		bcopy(*dev_select, old_dev_select,
645 		    sizeof(struct device_selection) * *num_selections);
646 	}
647 
648 	if (!changed && *num_selections != numdevs) {
649 		*dev_select = (struct device_selection *)reallocf(*dev_select,
650 			numdevs * sizeof(struct device_selection));
651 	}
652 
653 	if (init_selections != 0) {
654 		bzero(*dev_select, sizeof(struct device_selection) * numdevs);
655 
656 		for (i = 0; i < numdevs; i++) {
657 			(*dev_select)[i].device_number =
658 				devices[i].device_number;
659 			strncpy((*dev_select)[i].device_name,
660 				devices[i].device_name,
661 				DEVSTAT_NAME_LEN);
662 			(*dev_select)[i].device_name[DEVSTAT_NAME_LEN - 1]='\0';
663 			(*dev_select)[i].unit_number = devices[i].unit_number;
664 			(*dev_select)[i].position = i;
665 		}
666 		*num_selections = numdevs;
667 	} else if (init_selected_var != 0) {
668 		for (i = 0; i < numdevs; i++)
669 			(*dev_select)[i].selected = 0;
670 	}
671 
672 	/* we haven't gotten around to selecting anything yet.. */
673 	if ((select_mode == DS_SELECT_ONLY) || (init_selections != 0)
674 	 || (init_selected_var != 0))
675 		*num_selected = 0;
676 
677 	/*
678 	 * Look through any devices the user specified on the command line
679 	 * and see if they match known devices.  If so, select them.
680 	 */
681 	for (i = 0; (i < *num_selections) && (num_dev_selections > 0); i++) {
682 		char tmpstr[80];
683 
684 		snprintf(tmpstr, sizeof(tmpstr), "%s%d",
685 			 (*dev_select)[i].device_name,
686 			 (*dev_select)[i].unit_number);
687 		for (j = 0; j < num_dev_selections; j++) {
688 			if (strcmp(tmpstr, dev_selections[j]) == 0) {
689 				/*
690 				 * Here we do different things based on the
691 				 * mode we're in.  If we're in add or
692 				 * addonly mode, we only select this device
693 				 * if it hasn't already been selected.
694 				 * Otherwise, we would be unnecessarily
695 				 * changing the selection order and
696 				 * incrementing the selection count.  If
697 				 * we're in only mode, we unconditionally
698 				 * select this device, since in only mode
699 				 * any previous selections are erased and
700 				 * manually specified devices are the first
701 				 * ones to be selected.  If we're in remove
702 				 * mode, we de-select the specified device and
703 				 * decrement the selection count.
704 				 */
705 				switch(select_mode) {
706 				case DS_SELECT_ADD:
707 				case DS_SELECT_ADDONLY:
708 					if ((*dev_select)[i].selected)
709 						break;
710 					/* FALLTHROUGH */
711 				case DS_SELECT_ONLY:
712 					(*dev_select)[i].selected =
713 						++selection_number;
714 					(*num_selected)++;
715 					break;
716 				case DS_SELECT_REMOVE:
717 					(*dev_select)[i].selected = 0;
718 					(*num_selected)--;
719 					/*
720 					 * This isn't passed back out, we
721 					 * just use it to keep track of
722 					 * how many devices we've removed.
723 					 */
724 					num_dev_selections--;
725 					break;
726 				}
727 				break;
728 			}
729 		}
730 	}
731 
732 	/*
733 	 * Go through the user's device type expressions and select devices
734 	 * accordingly.  We only do this if the number of devices already
735 	 * selected is less than the maximum number we can show.
736 	 */
737 	for (i = 0; (i < num_matches) && (*num_selected < maxshowdevs); i++) {
738 		/* We should probably indicate some error here */
739 		if ((matches[i].match_fields == DEVSTAT_MATCH_NONE)
740 		 || (matches[i].num_match_categories <= 0))
741 			continue;
742 
743 		for (j = 0; j < numdevs; j++) {
744 			int num_match_categories;
745 
746 			num_match_categories = matches[i].num_match_categories;
747 
748 			/*
749 			 * Determine whether or not the current device
750 			 * matches the given matching expression.  This if
751 			 * statement consists of three components:
752 			 *   - the device type check
753 			 *   - the device interface check
754 			 *   - the passthrough check
755 			 * If a the matching test is successful, it
756 			 * decrements the number of matching categories,
757 			 * and if we've reached the last element that
758 			 * needed to be matched, the if statement succeeds.
759 			 *
760 			 */
761 			if ((((matches[i].match_fields & DEVSTAT_MATCH_TYPE)!=0)
762 			  && ((devices[j].device_type & DEVSTAT_TYPE_MASK) ==
763 			        (matches[i].device_type & DEVSTAT_TYPE_MASK))
764 			  &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0)
765 			   || (((matches[i].match_fields &
766 				DEVSTAT_MATCH_PASS) == 0)
767 			    && ((devices[j].device_type &
768 			        DEVSTAT_TYPE_PASS) == 0)))
769 			  && (--num_match_categories == 0))
770 			 || (((matches[i].match_fields & DEVSTAT_MATCH_IF) != 0)
771 			  && ((devices[j].device_type & DEVSTAT_TYPE_IF_MASK) ==
772 			        (matches[i].device_type & DEVSTAT_TYPE_IF_MASK))
773 			  &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0)
774 			   || (((matches[i].match_fields &
775 				DEVSTAT_MATCH_PASS) == 0)
776 			    && ((devices[j].device_type &
777 				DEVSTAT_TYPE_PASS) == 0)))
778 			  && (--num_match_categories == 0))
779 			 || (((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0)
780 			  && ((devices[j].device_type & DEVSTAT_TYPE_PASS) != 0)
781 			  && (--num_match_categories == 0))) {
782 
783 				/*
784 				 * This is probably a non-optimal solution
785 				 * to the problem that the devices in the
786 				 * device list will not be in the same
787 				 * order as the devices in the selection
788 				 * array.
789 				 */
790 				for (k = 0; k < numdevs; k++) {
791 					if ((*dev_select)[k].position == j) {
792 						found = 1;
793 						break;
794 					}
795 				}
796 
797 				/*
798 				 * There shouldn't be a case where a device
799 				 * in the device list is not in the
800 				 * selection list...but it could happen.
801 				 */
802 				if (found != 1) {
803 					fprintf(stderr, "selectdevs: couldn't"
804 						" find %s%d in selection "
805 						"list\n",
806 						devices[j].device_name,
807 						devices[j].unit_number);
808 					break;
809 				}
810 
811 				/*
812 				 * We do different things based upon the
813 				 * mode we're in.  If we're in add or only
814 				 * mode, we go ahead and select this device
815 				 * if it hasn't already been selected.  If
816 				 * it has already been selected, we leave
817 				 * it alone so we don't mess up the
818 				 * selection ordering.  Manually specified
819 				 * devices have already been selected, and
820 				 * they have higher priority than pattern
821 				 * matched devices.  If we're in remove
822 				 * mode, we de-select the given device and
823 				 * decrement the selected count.
824 				 */
825 				switch(select_mode) {
826 				case DS_SELECT_ADD:
827 				case DS_SELECT_ADDONLY:
828 				case DS_SELECT_ONLY:
829 					if ((*dev_select)[k].selected != 0)
830 						break;
831 					(*dev_select)[k].selected =
832 						++selection_number;
833 					(*num_selected)++;
834 					break;
835 				case DS_SELECT_REMOVE:
836 					(*dev_select)[k].selected = 0;
837 					(*num_selected)--;
838 					break;
839 				}
840 			}
841 		}
842 	}
843 
844 	/*
845 	 * Here we implement "top" mode.  Devices are sorted in the
846 	 * selection array based on two criteria:  whether or not they are
847 	 * selected (not selection number, just the fact that they are
848 	 * selected!) and the number of bytes in the "bytes" field of the
849 	 * selection structure.  The bytes field generally must be kept up
850 	 * by the user.  In the future, it may be maintained by library
851 	 * functions, but for now the user has to do the work.
852 	 *
853 	 * At first glance, it may seem wrong that we don't go through and
854 	 * select every device in the case where the user hasn't specified
855 	 * any devices or patterns.  In fact, though, it won't make any
856 	 * difference in the device sorting.  In that particular case (i.e.
857 	 * when we're in "add" or "only" mode, and the user hasn't
858 	 * specified anything) the first time through no devices will be
859 	 * selected, so the only criterion used to sort them will be their
860 	 * performance.  The second time through, and every time thereafter,
861 	 * all devices will be selected, so again selection won't matter.
862 	 */
863 	if (perf_select != 0) {
864 
865 		/* Sort the device array by throughput  */
866 		qsort(*dev_select, *num_selections,
867 		      sizeof(struct device_selection),
868 		      compare_select);
869 
870 		if (*num_selected == 0) {
871 			/*
872 			 * Here we select every device in the array, if it
873 			 * isn't already selected.  Because the 'selected'
874 			 * variable in the selection array entries contains
875 			 * the selection order, the devstats routine can show
876 			 * the devices that were selected first.
877 			 */
878 			for (i = 0; i < *num_selections; i++) {
879 				if ((*dev_select)[i].selected == 0) {
880 					(*dev_select)[i].selected =
881 						++selection_number;
882 					(*num_selected)++;
883 				}
884 			}
885 		} else {
886 			selection_number = 0;
887 			for (i = 0; i < *num_selections; i++) {
888 				if ((*dev_select)[i].selected != 0) {
889 					(*dev_select)[i].selected =
890 						++selection_number;
891 				}
892 			}
893 		}
894 	}
895 
896 	/*
897 	 * If we're in the "add" selection mode and if we haven't already
898 	 * selected maxshowdevs number of devices, go through the array and
899 	 * select any unselected devices.  If we're in "only" mode, we
900 	 * obviously don't want to select anything other than what the user
901 	 * specifies.  If we're in "remove" mode, it probably isn't a good
902 	 * idea to go through and select any more devices, since we might
903 	 * end up selecting something that the user wants removed.  Through
904 	 * more complicated logic, we could actually figure this out, but
905 	 * that would probably require combining this loop with the various
906 	 * selections loops above.
907 	 */
908 	if ((select_mode == DS_SELECT_ADD) && (*num_selected < maxshowdevs)) {
909 		for (i = 0; i < *num_selections; i++)
910 			if ((*dev_select)[i].selected == 0) {
911 				(*dev_select)[i].selected = ++selection_number;
912 				(*num_selected)++;
913 			}
914 	}
915 
916 	/*
917 	 * Look at the number of devices that have been selected.  If it
918 	 * has changed, set the changed variable.  Otherwise, if we've
919 	 * made a backup of the selection list, compare it to the current
920 	 * selection list to see if the selected devices have changed.
921 	 */
922 	if ((changed == 0) && (old_num_selected != *num_selected))
923 		changed = 1;
924 	else if ((changed == 0) && (old_dev_select != NULL)) {
925 		/*
926 		 * Now we go through the selection list and we look at
927 		 * it three different ways.
928 		 */
929 		for (i = 0; (i < *num_selections) && (changed == 0) &&
930 		     (i < old_num_selections); i++) {
931 			/*
932 			 * If the device at index i in both the new and old
933 			 * selection arrays has the same device number and
934 			 * selection status, it hasn't changed.  We
935 			 * continue on to the next index.
936 			 */
937 			if (((*dev_select)[i].device_number ==
938 			     old_dev_select[i].device_number)
939 			 && ((*dev_select)[i].selected ==
940 			     old_dev_select[i].selected))
941 				continue;
942 
943 			/*
944 			 * Now, if we're still going through the if
945 			 * statement, the above test wasn't true.  So we
946 			 * check here to see if the device at index i in
947 			 * the current array is the same as the device at
948 			 * index i in the old array.  If it is, that means
949 			 * that its selection number has changed.  Set
950 			 * changed to 1 and exit the loop.
951 			 */
952 			else if ((*dev_select)[i].device_number ==
953 			          old_dev_select[i].device_number) {
954 				changed = 1;
955 				break;
956 			}
957 			/*
958 			 * If we get here, then the device at index i in
959 			 * the current array isn't the same device as the
960 			 * device at index i in the old array.
961 			 */
962 			else {
963 				found = 0;
964 
965 				/*
966 				 * Search through the old selection array
967 				 * looking for a device with the same
968 				 * device number as the device at index i
969 				 * in the current array.  If the selection
970 				 * status is the same, then we mark it as
971 				 * found.  If the selection status isn't
972 				 * the same, we break out of the loop.
973 				 * Since found isn't set, changed will be
974 				 * set to 1 below.
975 				 */
976 				for (j = 0; j < old_num_selections; j++) {
977 					if (((*dev_select)[i].device_number ==
978 					      old_dev_select[j].device_number)
979 					 && ((*dev_select)[i].selected ==
980 					      old_dev_select[j].selected)){
981 						found = 1;
982 						break;
983 					}
984 					else if ((*dev_select)[i].device_number
985 					    == old_dev_select[j].device_number)
986 						break;
987 				}
988 				if (found == 0)
989 					changed = 1;
990 			}
991 		}
992 	}
993 	if (old_dev_select != NULL)
994 		free(old_dev_select);
995 
996 	return(changed);
997 }
998 
999 /*
1000  * Comparison routine for qsort() above.  Note that the comparison here is
1001  * backwards -- generally, it should return a value to indicate whether
1002  * arg1 is <, =, or > arg2.  Instead, it returns the opposite.  The reason
1003  * it returns the opposite is so that the selection array will be sorted in
1004  * order of decreasing performance.  We sort on two parameters.  The first
1005  * sort key is whether or not one or the other of the devices in question
1006  * has been selected.  If one of them has, and the other one has not, the
1007  * selected device is automatically more important than the unselected
1008  * device.  If neither device is selected, we judge the devices based upon
1009  * performance.
1010  */
1011 static int
1012 compare_select(const void *arg1, const void *arg2)
1013 {
1014 	if ((((const struct device_selection *)arg1)->selected)
1015 	 && (((const struct device_selection *)arg2)->selected == 0))
1016 		return(-1);
1017 	else if ((((const struct device_selection *)arg1)->selected == 0)
1018 	      && (((const struct device_selection *)arg2)->selected))
1019 		return(1);
1020 	else if (((const struct device_selection *)arg2)->bytes <
1021 	         ((const struct device_selection *)arg1)->bytes)
1022 		return(-1);
1023 	else if (((const struct device_selection *)arg2)->bytes >
1024 		 ((const struct device_selection *)arg1)->bytes)
1025 		return(1);
1026 	else
1027 		return(0);
1028 }
1029 
1030 /*
1031  * Take a string with the general format "arg1,arg2,arg3", and build a
1032  * device matching expression from it.
1033  */
1034 int
1035 devstat_buildmatch(char *match_str, struct devstat_match **matches,
1036 		   int *num_matches)
1037 {
1038 	char *tstr[5];
1039 	char **tempstr;
1040 	int num_args;
1041 	int i, j;
1042 
1043 	/* We can't do much without a string to parse */
1044 	if (match_str == NULL) {
1045 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1046 			 "%s: no match expression", __func__);
1047 		return(-1);
1048 	}
1049 
1050 	/*
1051 	 * Break the (comma delimited) input string out into separate strings.
1052 	 */
1053 	for (tempstr = tstr, num_args  = 0;
1054 	     (*tempstr = strsep(&match_str, ",")) != NULL && (num_args < 5);)
1055 		if (**tempstr != '\0') {
1056 			num_args++;
1057 			if (++tempstr >= &tstr[5])
1058 				break;
1059 		}
1060 
1061 	/* The user gave us too many type arguments */
1062 	if (num_args > 3) {
1063 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1064 			 "%s: too many type arguments", __func__);
1065 		return(-1);
1066 	}
1067 
1068 	if (*num_matches == 0)
1069 		*matches = NULL;
1070 
1071 	*matches = (struct devstat_match *)reallocf(*matches,
1072 		  sizeof(struct devstat_match) * (*num_matches + 1));
1073 
1074 	if (*matches == NULL) {
1075 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1076 			 "%s: Cannot allocate memory for matches list", __func__);
1077 		return(-1);
1078 	}
1079 
1080 	/* Make sure the current entry is clear */
1081 	bzero(&matches[0][*num_matches], sizeof(struct devstat_match));
1082 
1083 	/*
1084 	 * Step through the arguments the user gave us and build a device
1085 	 * matching expression from them.
1086 	 */
1087 	for (i = 0; i < num_args; i++) {
1088 		char *tempstr2, *tempstr3;
1089 
1090 		/*
1091 		 * Get rid of leading white space.
1092 		 */
1093 		tempstr2 = tstr[i];
1094 		while (isspace(*tempstr2) && (*tempstr2 != '\0'))
1095 			tempstr2++;
1096 
1097 		/*
1098 		 * Get rid of trailing white space.
1099 		 */
1100 		tempstr3 = &tempstr2[strlen(tempstr2) - 1];
1101 
1102 		while ((*tempstr3 != '\0') && (tempstr3 > tempstr2)
1103 		    && (isspace(*tempstr3))) {
1104 			*tempstr3 = '\0';
1105 			tempstr3--;
1106 		}
1107 
1108 		/*
1109 		 * Go through the match table comparing the user's
1110 		 * arguments to known device types, interfaces, etc.
1111 		 */
1112 		for (j = 0; match_table[j].match_str != NULL; j++) {
1113 			/*
1114 			 * We do case-insensitive matching, in case someone
1115 			 * wants to enter "SCSI" instead of "scsi" or
1116 			 * something like that.  Only compare as many
1117 			 * characters as are in the string in the match
1118 			 * table.  This should help if someone tries to use
1119 			 * a super-long match expression.
1120 			 */
1121 			if (strncasecmp(tempstr2, match_table[j].match_str,
1122 			    strlen(match_table[j].match_str)) == 0) {
1123 				/*
1124 				 * Make sure the user hasn't specified two
1125 				 * items of the same type, like "da" and
1126 				 * "cd".  One device cannot be both.
1127 				 */
1128 				if (((*matches)[*num_matches].match_fields &
1129 				    match_table[j].match_field) != 0) {
1130 					snprintf(devstat_errbuf,
1131 						 sizeof(devstat_errbuf),
1132 						 "%s: cannot have more than "
1133 						 "one match item in a single "
1134 						 "category", __func__);
1135 					return(-1);
1136 				}
1137 				/*
1138 				 * If we've gotten this far, we have a
1139 				 * winner.  Set the appropriate fields in
1140 				 * the match entry.
1141 				 */
1142 				(*matches)[*num_matches].match_fields |=
1143 					match_table[j].match_field;
1144 				(*matches)[*num_matches].device_type |=
1145 					match_table[j].type;
1146 				(*matches)[*num_matches].num_match_categories++;
1147 				break;
1148 			}
1149 		}
1150 		/*
1151 		 * We should have found a match in the above for loop.  If
1152 		 * not, that means the user entered an invalid device type
1153 		 * or interface.
1154 		 */
1155 		if ((*matches)[*num_matches].num_match_categories != (i + 1)) {
1156 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1157 				 "%s: unknown match item \"%s\"", __func__,
1158 				 tstr[i]);
1159 			return(-1);
1160 		}
1161 	}
1162 
1163 	(*num_matches)++;
1164 
1165 	return(0);
1166 }
1167 
1168 /*
1169  * Compute a number of device statistics.  Only one field is mandatory, and
1170  * that is "current".  Everything else is optional.  The caller passes in
1171  * pointers to variables to hold the various statistics he desires.  If he
1172  * doesn't want a particular staistic, he should pass in a NULL pointer.
1173  * Return values:
1174  * 0   -- success
1175  * -1  -- failure
1176  */
1177 int
1178 compute_stats(struct devstat *current, struct devstat *previous,
1179 	      long double etime, u_int64_t *total_bytes,
1180 	      u_int64_t *total_transfers, u_int64_t *total_blocks,
1181 	      long double *kb_per_transfer, long double *transfers_per_second,
1182 	      long double *mb_per_second, long double *blocks_per_second,
1183 	      long double *ms_per_transaction)
1184 {
1185 	return(devstat_compute_statistics(current, previous, etime,
1186 	       total_bytes ? DSM_TOTAL_BYTES : DSM_SKIP,
1187 	       total_bytes,
1188 	       total_transfers ? DSM_TOTAL_TRANSFERS : DSM_SKIP,
1189 	       total_transfers,
1190 	       total_blocks ? DSM_TOTAL_BLOCKS : DSM_SKIP,
1191 	       total_blocks,
1192 	       kb_per_transfer ? DSM_KB_PER_TRANSFER : DSM_SKIP,
1193 	       kb_per_transfer,
1194 	       transfers_per_second ? DSM_TRANSFERS_PER_SECOND : DSM_SKIP,
1195 	       transfers_per_second,
1196 	       mb_per_second ? DSM_MB_PER_SECOND : DSM_SKIP,
1197 	       mb_per_second,
1198 	       blocks_per_second ? DSM_BLOCKS_PER_SECOND : DSM_SKIP,
1199 	       blocks_per_second,
1200 	       ms_per_transaction ? DSM_MS_PER_TRANSACTION : DSM_SKIP,
1201 	       ms_per_transaction,
1202 	       DSM_NONE));
1203 }
1204 
1205 
1206 /* This is 1/2^64 */
1207 #define BINTIME_SCALE 5.42101086242752217003726400434970855712890625e-20
1208 
1209 long double
1210 devstat_compute_etime(struct bintime *cur_time, struct bintime *prev_time)
1211 {
1212 	long double etime;
1213 
1214 	etime = cur_time->sec;
1215 	etime += cur_time->frac * BINTIME_SCALE;
1216 	if (prev_time != NULL) {
1217 		etime -= prev_time->sec;
1218 		etime -= prev_time->frac * BINTIME_SCALE;
1219 	}
1220 	return(etime);
1221 }
1222 
1223 #define DELTA(field, index)				\
1224 	(current->field[(index)] - (previous ? previous->field[(index)] : 0))
1225 
1226 #define DELTA_T(field)					\
1227 	devstat_compute_etime(&current->field,  	\
1228 	(previous ? &previous->field : NULL))
1229 
1230 int
1231 devstat_compute_statistics(struct devstat *current, struct devstat *previous,
1232 			   long double etime, ...)
1233 {
1234 	u_int64_t totalbytes, totalbytesread, totalbyteswrite, totalbytesfree;
1235 	u_int64_t totaltransfers, totaltransfersread, totaltransferswrite;
1236 	u_int64_t totaltransfersother, totalblocks, totalblocksread;
1237 	u_int64_t totalblockswrite, totaltransfersfree, totalblocksfree;
1238 	long double totalduration, totaldurationread, totaldurationwrite;
1239 	long double totaldurationfree, totaldurationother;
1240 	va_list ap;
1241 	devstat_metric metric;
1242 	u_int64_t *destu64;
1243 	long double *destld;
1244 	int retval;
1245 
1246 	retval = 0;
1247 
1248 	/*
1249 	 * current is the only mandatory field.
1250 	 */
1251 	if (current == NULL) {
1252 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1253 			 "%s: current stats structure was NULL", __func__);
1254 		return(-1);
1255 	}
1256 
1257 	totalbytesread = DELTA(bytes, DEVSTAT_READ);
1258 	totalbyteswrite = DELTA(bytes, DEVSTAT_WRITE);
1259 	totalbytesfree = DELTA(bytes, DEVSTAT_FREE);
1260 	totalbytes = totalbytesread + totalbyteswrite + totalbytesfree;
1261 
1262 	totaltransfersread = DELTA(operations, DEVSTAT_READ);
1263 	totaltransferswrite = DELTA(operations, DEVSTAT_WRITE);
1264 	totaltransfersother = DELTA(operations, DEVSTAT_NO_DATA);
1265 	totaltransfersfree = DELTA(operations, DEVSTAT_FREE);
1266 	totaltransfers = totaltransfersread + totaltransferswrite +
1267 			 totaltransfersother + totaltransfersfree;
1268 
1269 	totalblocks = totalbytes;
1270 	totalblocksread = totalbytesread;
1271 	totalblockswrite = totalbyteswrite;
1272 	totalblocksfree = totalbytesfree;
1273 
1274 	if (current->block_size > 0) {
1275 		totalblocks /= current->block_size;
1276 		totalblocksread /= current->block_size;
1277 		totalblockswrite /= current->block_size;
1278 		totalblocksfree /= current->block_size;
1279 	} else {
1280 		totalblocks /= 512;
1281 		totalblocksread /= 512;
1282 		totalblockswrite /= 512;
1283 		totalblocksfree /= 512;
1284 	}
1285 
1286 	totaldurationread = DELTA_T(duration[DEVSTAT_READ]);
1287 	totaldurationwrite = DELTA_T(duration[DEVSTAT_WRITE]);
1288 	totaldurationfree = DELTA_T(duration[DEVSTAT_FREE]);
1289 	totaldurationother = DELTA_T(duration[DEVSTAT_NO_DATA]);
1290 	totalduration = totaldurationread + totaldurationwrite +
1291 	    totaldurationfree + totaldurationother;
1292 
1293 	va_start(ap, etime);
1294 
1295 	while ((metric = (devstat_metric)va_arg(ap, devstat_metric)) != 0) {
1296 
1297 		if (metric == DSM_NONE)
1298 			break;
1299 
1300 		if (metric >= DSM_MAX) {
1301 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1302 				 "%s: metric %d is out of range", __func__,
1303 				 metric);
1304 			retval = -1;
1305 			goto bailout;
1306 		}
1307 
1308 		switch (devstat_arg_list[metric].argtype) {
1309 		case DEVSTAT_ARG_UINT64:
1310 			destu64 = (u_int64_t *)va_arg(ap, u_int64_t *);
1311 			break;
1312 		case DEVSTAT_ARG_LD:
1313 			destld = (long double *)va_arg(ap, long double *);
1314 			break;
1315 		case DEVSTAT_ARG_SKIP:
1316 			destld = (long double *)va_arg(ap, long double *);
1317 			break;
1318 		default:
1319 			retval = -1;
1320 			goto bailout;
1321 			break; /* NOTREACHED */
1322 		}
1323 
1324 		if (devstat_arg_list[metric].argtype == DEVSTAT_ARG_SKIP)
1325 			continue;
1326 
1327 		switch (metric) {
1328 		case DSM_TOTAL_BYTES:
1329 			*destu64 = totalbytes;
1330 			break;
1331 		case DSM_TOTAL_BYTES_READ:
1332 			*destu64 = totalbytesread;
1333 			break;
1334 		case DSM_TOTAL_BYTES_WRITE:
1335 			*destu64 = totalbyteswrite;
1336 			break;
1337 		case DSM_TOTAL_BYTES_FREE:
1338 			*destu64 = totalbytesfree;
1339 			break;
1340 		case DSM_TOTAL_TRANSFERS:
1341 			*destu64 = totaltransfers;
1342 			break;
1343 		case DSM_TOTAL_TRANSFERS_READ:
1344 			*destu64 = totaltransfersread;
1345 			break;
1346 		case DSM_TOTAL_TRANSFERS_WRITE:
1347 			*destu64 = totaltransferswrite;
1348 			break;
1349 		case DSM_TOTAL_TRANSFERS_FREE:
1350 			*destu64 = totaltransfersfree;
1351 			break;
1352 		case DSM_TOTAL_TRANSFERS_OTHER:
1353 			*destu64 = totaltransfersother;
1354 			break;
1355 		case DSM_TOTAL_BLOCKS:
1356 			*destu64 = totalblocks;
1357 			break;
1358 		case DSM_TOTAL_BLOCKS_READ:
1359 			*destu64 = totalblocksread;
1360 			break;
1361 		case DSM_TOTAL_BLOCKS_WRITE:
1362 			*destu64 = totalblockswrite;
1363 			break;
1364 		case DSM_TOTAL_BLOCKS_FREE:
1365 			*destu64 = totalblocksfree;
1366 			break;
1367 		case DSM_KB_PER_TRANSFER:
1368 			*destld = totalbytes;
1369 			*destld /= 1024;
1370 			if (totaltransfers > 0)
1371 				*destld /= totaltransfers;
1372 			else
1373 				*destld = 0.0;
1374 			break;
1375 		case DSM_KB_PER_TRANSFER_READ:
1376 			*destld = totalbytesread;
1377 			*destld /= 1024;
1378 			if (totaltransfersread > 0)
1379 				*destld /= totaltransfersread;
1380 			else
1381 				*destld = 0.0;
1382 			break;
1383 		case DSM_KB_PER_TRANSFER_WRITE:
1384 			*destld = totalbyteswrite;
1385 			*destld /= 1024;
1386 			if (totaltransferswrite > 0)
1387 				*destld /= totaltransferswrite;
1388 			else
1389 				*destld = 0.0;
1390 			break;
1391 		case DSM_KB_PER_TRANSFER_FREE:
1392 			*destld = totalbytesfree;
1393 			*destld /= 1024;
1394 			if (totaltransfersfree > 0)
1395 				*destld /= totaltransfersfree;
1396 			else
1397 				*destld = 0.0;
1398 			break;
1399 		case DSM_TRANSFERS_PER_SECOND:
1400 			if (etime > 0.0) {
1401 				*destld = totaltransfers;
1402 				*destld /= etime;
1403 			} else
1404 				*destld = 0.0;
1405 			break;
1406 		case DSM_TRANSFERS_PER_SECOND_READ:
1407 			if (etime > 0.0) {
1408 				*destld = totaltransfersread;
1409 				*destld /= etime;
1410 			} else
1411 				*destld = 0.0;
1412 			break;
1413 		case DSM_TRANSFERS_PER_SECOND_WRITE:
1414 			if (etime > 0.0) {
1415 				*destld = totaltransferswrite;
1416 				*destld /= etime;
1417 			} else
1418 				*destld = 0.0;
1419 			break;
1420 		case DSM_TRANSFERS_PER_SECOND_FREE:
1421 			if (etime > 0.0) {
1422 				*destld = totaltransfersfree;
1423 				*destld /= etime;
1424 			} else
1425 				*destld = 0.0;
1426 			break;
1427 		case DSM_TRANSFERS_PER_SECOND_OTHER:
1428 			if (etime > 0.0) {
1429 				*destld = totaltransfersother;
1430 				*destld /= etime;
1431 			} else
1432 				*destld = 0.0;
1433 			break;
1434 		case DSM_MB_PER_SECOND:
1435 			*destld = totalbytes;
1436 			*destld /= 1024 * 1024;
1437 			if (etime > 0.0)
1438 				*destld /= etime;
1439 			else
1440 				*destld = 0.0;
1441 			break;
1442 		case DSM_MB_PER_SECOND_READ:
1443 			*destld = totalbytesread;
1444 			*destld /= 1024 * 1024;
1445 			if (etime > 0.0)
1446 				*destld /= etime;
1447 			else
1448 				*destld = 0.0;
1449 			break;
1450 		case DSM_MB_PER_SECOND_WRITE:
1451 			*destld = totalbyteswrite;
1452 			*destld /= 1024 * 1024;
1453 			if (etime > 0.0)
1454 				*destld /= etime;
1455 			else
1456 				*destld = 0.0;
1457 			break;
1458 		case DSM_MB_PER_SECOND_FREE:
1459 			*destld = totalbytesfree;
1460 			*destld /= 1024 * 1024;
1461 			if (etime > 0.0)
1462 				*destld /= etime;
1463 			else
1464 				*destld = 0.0;
1465 			break;
1466 		case DSM_BLOCKS_PER_SECOND:
1467 			*destld = totalblocks;
1468 			if (etime > 0.0)
1469 				*destld /= etime;
1470 			else
1471 				*destld = 0.0;
1472 			break;
1473 		case DSM_BLOCKS_PER_SECOND_READ:
1474 			*destld = totalblocksread;
1475 			if (etime > 0.0)
1476 				*destld /= etime;
1477 			else
1478 				*destld = 0.0;
1479 			break;
1480 		case DSM_BLOCKS_PER_SECOND_WRITE:
1481 			*destld = totalblockswrite;
1482 			if (etime > 0.0)
1483 				*destld /= etime;
1484 			else
1485 				*destld = 0.0;
1486 			break;
1487 		case DSM_BLOCKS_PER_SECOND_FREE:
1488 			*destld = totalblocksfree;
1489 			if (etime > 0.0)
1490 				*destld /= etime;
1491 			else
1492 				*destld = 0.0;
1493 			break;
1494 		/*
1495 		 * Some devstat callers update the duration and some don't.
1496 		 * So this will only be accurate if they provide the
1497 		 * duration.
1498 		 */
1499 		case DSM_MS_PER_TRANSACTION:
1500 			if (totaltransfers > 0) {
1501 				*destld = totalduration;
1502 				*destld /= totaltransfers;
1503 				*destld *= 1000;
1504 			} else
1505 				*destld = 0.0;
1506 			break;
1507 		case DSM_MS_PER_TRANSACTION_READ:
1508 			if (totaltransfersread > 0) {
1509 				*destld = totaldurationread;
1510 				*destld /= totaltransfersread;
1511 				*destld *= 1000;
1512 			} else
1513 				*destld = 0.0;
1514 			break;
1515 		case DSM_MS_PER_TRANSACTION_WRITE:
1516 			if (totaltransferswrite > 0) {
1517 				*destld = totaldurationwrite;
1518 				*destld /= totaltransferswrite;
1519 				*destld *= 1000;
1520 			} else
1521 				*destld = 0.0;
1522 			break;
1523 		case DSM_MS_PER_TRANSACTION_FREE:
1524 			if (totaltransfersfree > 0) {
1525 				*destld = totaldurationfree;
1526 				*destld /= totaltransfersfree;
1527 				*destld *= 1000;
1528 			} else
1529 				*destld = 0.0;
1530 			break;
1531 		case DSM_MS_PER_TRANSACTION_OTHER:
1532 			if (totaltransfersother > 0) {
1533 				*destld = totaldurationother;
1534 				*destld /= totaltransfersother;
1535 				*destld *= 1000;
1536 			} else
1537 				*destld = 0.0;
1538 			break;
1539 		case DSM_BUSY_PCT:
1540 			*destld = DELTA_T(busy_time);
1541 			if (*destld < 0)
1542 				*destld = 0;
1543 			*destld /= etime;
1544 			*destld *= 100;
1545 			if (*destld < 0)
1546 				*destld = 0;
1547 			break;
1548 		case DSM_QUEUE_LENGTH:
1549 			*destu64 = current->start_count - current->end_count;
1550 			break;
1551 		case DSM_TOTAL_DURATION:
1552 			*destld = totalduration;
1553 			break;
1554 		case DSM_TOTAL_DURATION_READ:
1555 			*destld = totaldurationread;
1556 			break;
1557 		case DSM_TOTAL_DURATION_WRITE:
1558 			*destld = totaldurationwrite;
1559 			break;
1560 		case DSM_TOTAL_DURATION_FREE:
1561 			*destld = totaldurationfree;
1562 			break;
1563 		case DSM_TOTAL_DURATION_OTHER:
1564 			*destld = totaldurationother;
1565 			break;
1566 		case DSM_TOTAL_BUSY_TIME:
1567 			*destld = DELTA_T(busy_time);
1568 			break;
1569 /*
1570  * XXX: comment out the default block to see if any case's are missing.
1571  */
1572 #if 1
1573 		default:
1574 			/*
1575 			 * This shouldn't happen, since we should have
1576 			 * caught any out of range metrics at the top of
1577 			 * the loop.
1578 			 */
1579 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1580 				 "%s: unknown metric %d", __func__, metric);
1581 			retval = -1;
1582 			goto bailout;
1583 			break; /* NOTREACHED */
1584 #endif
1585 		}
1586 	}
1587 
1588 bailout:
1589 
1590 	va_end(ap);
1591 	return(retval);
1592 }
1593 
1594 static int
1595 readkmem(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes)
1596 {
1597 
1598 	if (kvm_read(kd, addr, buf, nbytes) == -1) {
1599 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1600 			 "%s: error reading value (kvm_read): %s", __func__,
1601 			 kvm_geterr(kd));
1602 		return(-1);
1603 	}
1604 	return(0);
1605 }
1606 
1607 static int
1608 readkmem_nl(kvm_t *kd, const char *name, void *buf, size_t nbytes)
1609 {
1610 	struct nlist nl[2];
1611 
1612 	nl[0].n_name = (char *)name;
1613 	nl[1].n_name = NULL;
1614 
1615 	if (kvm_nlist(kd, nl) == -1) {
1616 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1617 			 "%s: error getting name list (kvm_nlist): %s",
1618 			 __func__, kvm_geterr(kd));
1619 		return(-1);
1620 	}
1621 	return(readkmem(kd, nl[0].n_value, buf, nbytes));
1622 }
1623 
1624 /*
1625  * This duplicates the functionality of the kernel sysctl handler for poking
1626  * through crash dumps.
1627  */
1628 static char *
1629 get_devstat_kvm(kvm_t *kd)
1630 {
1631 	int i, wp;
1632 	long gen;
1633 	struct devstat *nds;
1634 	struct devstat ds;
1635 	struct devstatlist dhead;
1636 	int num_devs;
1637 	char *rv = NULL;
1638 
1639 	if ((num_devs = devstat_getnumdevs(kd)) <= 0)
1640 		return(NULL);
1641 	if (KREADNL(kd, X_DEVICE_STATQ, dhead) == -1)
1642 		return(NULL);
1643 
1644 	nds = STAILQ_FIRST(&dhead);
1645 
1646 	if ((rv = malloc(sizeof(gen))) == NULL) {
1647 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1648 			 "%s: out of memory (initial malloc failed)",
1649 			 __func__);
1650 		return(NULL);
1651 	}
1652 	gen = devstat_getgeneration(kd);
1653 	memcpy(rv, &gen, sizeof(gen));
1654 	wp = sizeof(gen);
1655 	/*
1656 	 * Now push out all the devices.
1657 	 */
1658 	for (i = 0; (nds != NULL) && (i < num_devs);
1659 	     nds = STAILQ_NEXT(nds, dev_links), i++) {
1660 		if (readkmem(kd, (long)nds, &ds, sizeof(ds)) == -1) {
1661 			free(rv);
1662 			return(NULL);
1663 		}
1664 		nds = &ds;
1665 		rv = (char *)reallocf(rv, sizeof(gen) +
1666 				      sizeof(ds) * (i + 1));
1667 		if (rv == NULL) {
1668 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1669 				 "%s: out of memory (malloc failed)",
1670 				 __func__);
1671 			return(NULL);
1672 		}
1673 		memcpy(rv + wp, &ds, sizeof(ds));
1674 		wp += sizeof(ds);
1675 	}
1676 	return(rv);
1677 }
1678