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