xref: /freebsd/lib/libdevstat/devstat.c (revision 36eab1f55ace4b4529f568b4d9f0406ac6cc16a6)
128fb27baSJustin T. Gibbs /*
228fb27baSJustin T. Gibbs  * Copyright (c) 1997, 1998 Kenneth D. Merry.
328fb27baSJustin T. Gibbs  * All rights reserved.
428fb27baSJustin T. Gibbs  *
528fb27baSJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
628fb27baSJustin T. Gibbs  * modification, are permitted provided that the following conditions
728fb27baSJustin T. Gibbs  * are met:
828fb27baSJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
928fb27baSJustin T. Gibbs  *    notice, this list of conditions and the following disclaimer.
1028fb27baSJustin T. Gibbs  * 2. Redistributions in binary form must reproduce the above copyright
1128fb27baSJustin T. Gibbs  *    notice, this list of conditions and the following disclaimer in the
1228fb27baSJustin T. Gibbs  *    documentation and/or other materials provided with the distribution.
1328fb27baSJustin T. Gibbs  * 3. The name of the author may not be used to endorse or promote products
1428fb27baSJustin T. Gibbs  *    derived from this software without specific prior written permission.
1528fb27baSJustin T. Gibbs  *
1628fb27baSJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1728fb27baSJustin T. Gibbs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1828fb27baSJustin T. Gibbs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1928fb27baSJustin T. Gibbs  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2028fb27baSJustin T. Gibbs  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2128fb27baSJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2228fb27baSJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2328fb27baSJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2428fb27baSJustin T. Gibbs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2528fb27baSJustin T. Gibbs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2628fb27baSJustin T. Gibbs  * SUCH DAMAGE.
2728fb27baSJustin T. Gibbs  */
2828fb27baSJustin T. Gibbs 
29e67f5b9fSMatthew Dillon #include <sys/cdefs.h>
30e67f5b9fSMatthew Dillon __FBSDID("$FreeBSD$");
31e67f5b9fSMatthew Dillon 
3228fb27baSJustin T. Gibbs #include <sys/types.h>
3328fb27baSJustin T. Gibbs #include <sys/sysctl.h>
3428fb27baSJustin T. Gibbs #include <sys/errno.h>
3512590690SPoul-Henning Kamp #include <sys/resource.h>
36c4a5ef6eSThomas Moestl #include <sys/queue.h>
3728fb27baSJustin T. Gibbs 
38eded794aSKenneth D. Merry #include <ctype.h>
3928fb27baSJustin T. Gibbs #include <err.h>
40c4a5ef6eSThomas Moestl #include <fcntl.h>
41c4a5ef6eSThomas Moestl #include <limits.h>
4228fb27baSJustin T. Gibbs #include <stdio.h>
4328fb27baSJustin T. Gibbs #include <stdlib.h>
4428fb27baSJustin T. Gibbs #include <string.h>
45c4a5ef6eSThomas Moestl #include <stdarg.h>
46c4a5ef6eSThomas Moestl #include <kvm.h>
4728fb27baSJustin T. Gibbs 
4828fb27baSJustin T. Gibbs #include "devstat.h"
4928fb27baSJustin T. Gibbs 
50c4a5ef6eSThomas Moestl typedef enum {
51c4a5ef6eSThomas Moestl 	DEVSTAT_ARG_NOTYPE,
52c4a5ef6eSThomas Moestl 	DEVSTAT_ARG_UINT64,
53884539f7SKenneth D. Merry 	DEVSTAT_ARG_LD,
54884539f7SKenneth D. Merry 	DEVSTAT_ARG_SKIP
55c4a5ef6eSThomas Moestl } devstat_arg_type;
56c4a5ef6eSThomas Moestl 
5728fb27baSJustin T. Gibbs char devstat_errbuf[DEVSTAT_ERRBUF_SIZE];
5828fb27baSJustin T. Gibbs 
5928fb27baSJustin T. Gibbs /*
6028fb27baSJustin T. Gibbs  * Table to match descriptive strings with device types.  These are in
6128fb27baSJustin T. Gibbs  * order from most common to least common to speed search time.
6228fb27baSJustin T. Gibbs  */
6328fb27baSJustin T. Gibbs struct devstat_match_table match_table[] = {
6428fb27baSJustin T. Gibbs 	{"da",		DEVSTAT_TYPE_DIRECT,	DEVSTAT_MATCH_TYPE},
6528fb27baSJustin T. Gibbs 	{"cd",		DEVSTAT_TYPE_CDROM,	DEVSTAT_MATCH_TYPE},
6628fb27baSJustin T. Gibbs 	{"scsi",	DEVSTAT_TYPE_IF_SCSI,	DEVSTAT_MATCH_IF},
6728fb27baSJustin T. Gibbs 	{"ide",		DEVSTAT_TYPE_IF_IDE,	DEVSTAT_MATCH_IF},
6828fb27baSJustin T. Gibbs 	{"other",	DEVSTAT_TYPE_IF_OTHER,	DEVSTAT_MATCH_IF},
6928fb27baSJustin T. Gibbs 	{"worm",	DEVSTAT_TYPE_WORM,	DEVSTAT_MATCH_TYPE},
7028fb27baSJustin T. Gibbs 	{"sa",		DEVSTAT_TYPE_SEQUENTIAL,DEVSTAT_MATCH_TYPE},
7128fb27baSJustin T. Gibbs 	{"pass",	DEVSTAT_TYPE_PASS,	DEVSTAT_MATCH_PASS},
7228fb27baSJustin T. Gibbs 	{"optical",	DEVSTAT_TYPE_OPTICAL,	DEVSTAT_MATCH_TYPE},
7328fb27baSJustin T. Gibbs 	{"array",	DEVSTAT_TYPE_STORARRAY,	DEVSTAT_MATCH_TYPE},
7428fb27baSJustin T. Gibbs 	{"changer",	DEVSTAT_TYPE_CHANGER,	DEVSTAT_MATCH_TYPE},
7528fb27baSJustin T. Gibbs 	{"scanner",	DEVSTAT_TYPE_SCANNER,	DEVSTAT_MATCH_TYPE},
7628fb27baSJustin T. Gibbs 	{"printer",	DEVSTAT_TYPE_PRINTER,	DEVSTAT_MATCH_TYPE},
7728fb27baSJustin T. Gibbs 	{"floppy",	DEVSTAT_TYPE_FLOPPY,	DEVSTAT_MATCH_TYPE},
7828fb27baSJustin T. Gibbs 	{"proc",	DEVSTAT_TYPE_PROCESSOR,	DEVSTAT_MATCH_TYPE},
7928fb27baSJustin T. Gibbs 	{"comm",	DEVSTAT_TYPE_COMM,	DEVSTAT_MATCH_TYPE},
8028fb27baSJustin T. Gibbs 	{"enclosure",	DEVSTAT_TYPE_ENCLOSURE,	DEVSTAT_MATCH_TYPE},
8128fb27baSJustin T. Gibbs 	{NULL,		0,			0}
8228fb27baSJustin T. Gibbs };
8328fb27baSJustin T. Gibbs 
84c4a5ef6eSThomas Moestl struct devstat_args {
85c4a5ef6eSThomas Moestl 	devstat_metric 		metric;
86c4a5ef6eSThomas Moestl 	devstat_arg_type	argtype;
87c4a5ef6eSThomas Moestl } devstat_arg_list[] = {
88c4a5ef6eSThomas Moestl 	{ DSM_NONE, DEVSTAT_ARG_NOTYPE },
89c4a5ef6eSThomas Moestl 	{ DSM_TOTAL_BYTES, DEVSTAT_ARG_UINT64 },
90c4a5ef6eSThomas Moestl 	{ DSM_TOTAL_BYTES_READ, DEVSTAT_ARG_UINT64 },
91c4a5ef6eSThomas Moestl 	{ DSM_TOTAL_BYTES_WRITE, DEVSTAT_ARG_UINT64 },
9236eab1f5SPoul-Henning Kamp 	{ DSM_TOTAL_BYTES_FREE, DEVSTAT_ARG_UINT64 },
93c4a5ef6eSThomas Moestl 	{ DSM_TOTAL_TRANSFERS, DEVSTAT_ARG_UINT64 },
9436eab1f5SPoul-Henning Kamp 	{ DSM_TOTAL_TRANSFERS_OTHER, DEVSTAT_ARG_UINT64 },
95c4a5ef6eSThomas Moestl 	{ DSM_TOTAL_TRANSFERS_READ, DEVSTAT_ARG_UINT64 },
96c4a5ef6eSThomas Moestl 	{ DSM_TOTAL_TRANSFERS_WRITE, DEVSTAT_ARG_UINT64 },
9736eab1f5SPoul-Henning Kamp 	{ DSM_TOTAL_TRANSFERS_FREE, DEVSTAT_ARG_UINT64 },
98c4a5ef6eSThomas Moestl 	{ DSM_TOTAL_BLOCKS, DEVSTAT_ARG_UINT64 },
99c4a5ef6eSThomas Moestl 	{ DSM_TOTAL_BLOCKS_READ, DEVSTAT_ARG_UINT64 },
100c4a5ef6eSThomas Moestl 	{ DSM_TOTAL_BLOCKS_WRITE, DEVSTAT_ARG_UINT64 },
10136eab1f5SPoul-Henning Kamp 	{ DSM_TOTAL_BLOCKS_FREE, DEVSTAT_ARG_UINT64 },
102c4a5ef6eSThomas Moestl 	{ DSM_KB_PER_TRANSFER, DEVSTAT_ARG_LD },
103c4a5ef6eSThomas Moestl 	{ DSM_KB_PER_TRANSFER_READ, DEVSTAT_ARG_LD },
104c4a5ef6eSThomas Moestl 	{ DSM_KB_PER_TRANSFER_WRITE, DEVSTAT_ARG_LD },
10536eab1f5SPoul-Henning Kamp 	{ DSM_KB_PER_TRANSFER_FREE, DEVSTAT_ARG_LD },
106c4a5ef6eSThomas Moestl 	{ DSM_TRANSFERS_PER_SECOND, DEVSTAT_ARG_LD },
107c4a5ef6eSThomas Moestl 	{ DSM_TRANSFERS_PER_SECOND_READ, DEVSTAT_ARG_LD },
108c4a5ef6eSThomas Moestl 	{ DSM_TRANSFERS_PER_SECOND_WRITE, DEVSTAT_ARG_LD },
10936eab1f5SPoul-Henning Kamp 	{ DSM_TRANSFERS_PER_SECOND_FREE, DEVSTAT_ARG_LD },
110c4a5ef6eSThomas Moestl 	{ DSM_TRANSFERS_PER_SECOND_OTHER, DEVSTAT_ARG_LD },
111c4a5ef6eSThomas Moestl 	{ DSM_MB_PER_SECOND, DEVSTAT_ARG_LD },
112c4a5ef6eSThomas Moestl 	{ DSM_MB_PER_SECOND_READ, DEVSTAT_ARG_LD },
113c4a5ef6eSThomas Moestl 	{ DSM_MB_PER_SECOND_WRITE, DEVSTAT_ARG_LD },
11436eab1f5SPoul-Henning Kamp 	{ DSM_MB_PER_SECOND_FREE, DEVSTAT_ARG_LD },
115c4a5ef6eSThomas Moestl 	{ DSM_BLOCKS_PER_SECOND, DEVSTAT_ARG_LD },
116c4a5ef6eSThomas Moestl 	{ DSM_BLOCKS_PER_SECOND_READ, DEVSTAT_ARG_LD },
117c4a5ef6eSThomas Moestl 	{ DSM_BLOCKS_PER_SECOND_WRITE, DEVSTAT_ARG_LD },
11836eab1f5SPoul-Henning Kamp 	{ DSM_BLOCKS_PER_SECOND_FREE, DEVSTAT_ARG_LD },
119c4a5ef6eSThomas Moestl 	{ DSM_MS_PER_TRANSACTION, DEVSTAT_ARG_LD },
120c4a5ef6eSThomas Moestl 	{ DSM_MS_PER_TRANSACTION_READ, DEVSTAT_ARG_LD },
121884539f7SKenneth D. Merry 	{ DSM_MS_PER_TRANSACTION_WRITE, DEVSTAT_ARG_LD },
12236eab1f5SPoul-Henning Kamp 	{ DSM_MS_PER_TRANSACTION_FREE, DEVSTAT_ARG_LD },
12336eab1f5SPoul-Henning Kamp 	{ DSM_MS_PER_TRANSACTION_OTHER, DEVSTAT_ARG_LD },
12436eab1f5SPoul-Henning Kamp 	{ DSM_BUSY_PCT, DEVSTAT_ARG_LD },
12536eab1f5SPoul-Henning Kamp 	{ DSM_QUEUE_LENGTH, DEVSTAT_ARG_UINT64 },
126884539f7SKenneth D. Merry 	{ DSM_SKIP, DEVSTAT_ARG_SKIP }
127c4a5ef6eSThomas Moestl };
128c4a5ef6eSThomas Moestl 
129c3508206SKenneth D. Merry static const char *namelist[] = {
130c4a5ef6eSThomas Moestl #define X_NUMDEVS	0
131c4a5ef6eSThomas Moestl 	"_devstat_num_devs",
132c4a5ef6eSThomas Moestl #define X_GENERATION	1
133c4a5ef6eSThomas Moestl 	"_devstat_generation",
134c4a5ef6eSThomas Moestl #define X_VERSION	2
135c4a5ef6eSThomas Moestl 	"_devstat_version",
136c4a5ef6eSThomas Moestl #define X_DEVICE_STATQ	3
137c4a5ef6eSThomas Moestl 	"_device_statq",
138c4a5ef6eSThomas Moestl #define X_END		4
139c4a5ef6eSThomas Moestl };
140c4a5ef6eSThomas Moestl 
14128fb27baSJustin T. Gibbs /*
14228fb27baSJustin T. Gibbs  * Local function declarations.
14328fb27baSJustin T. Gibbs  */
14428fb27baSJustin T. Gibbs static int compare_select(const void *arg1, const void *arg2);
145c4a5ef6eSThomas Moestl static int readkmem(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes);
146c3508206SKenneth D. Merry static int readkmem_nl(kvm_t *kd, const char *name, void *buf, size_t nbytes);
147c4a5ef6eSThomas Moestl static char *get_devstat_kvm(kvm_t *kd);
148c4a5ef6eSThomas Moestl 
149c4a5ef6eSThomas Moestl #define KREADNL(kd, var, val) \
150c4a5ef6eSThomas Moestl 	readkmem_nl(kd, namelist[var], &val, sizeof(val))
15128fb27baSJustin T. Gibbs 
15228fb27baSJustin T. Gibbs int
153c4a5ef6eSThomas Moestl devstat_getnumdevs(kvm_t *kd)
15428fb27baSJustin T. Gibbs {
15528fb27baSJustin T. Gibbs 	size_t numdevsize;
15628fb27baSJustin T. Gibbs 	int numdevs;
157c3508206SKenneth D. Merry 	const char *func_name = "devstat_getnumdevs";
15828fb27baSJustin T. Gibbs 
15928fb27baSJustin T. Gibbs 	numdevsize = sizeof(int);
16028fb27baSJustin T. Gibbs 
16128fb27baSJustin T. Gibbs 	/*
16228fb27baSJustin T. Gibbs 	 * Find out how many devices we have in the system.
16328fb27baSJustin T. Gibbs 	 */
164c4a5ef6eSThomas Moestl 	if (kd == NULL) {
16528fb27baSJustin T. Gibbs 		if (sysctlbyname("kern.devstat.numdevs", &numdevs,
16628fb27baSJustin T. Gibbs 				 &numdevsize, NULL, 0) == -1) {
167c4a5ef6eSThomas Moestl 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
168c4a5ef6eSThomas Moestl 				 "%s: error getting number of devices\n"
169c4a5ef6eSThomas Moestl 				 "%s: %s", func_name, func_name,
170c4a5ef6eSThomas Moestl 				 strerror(errno));
17128fb27baSJustin T. Gibbs 			return(-1);
17228fb27baSJustin T. Gibbs 		} else
17328fb27baSJustin T. Gibbs 			return(numdevs);
174c4a5ef6eSThomas Moestl 	} else {
175c3508206SKenneth D. Merry 
176c4a5ef6eSThomas Moestl 		if (KREADNL(kd, X_NUMDEVS, numdevs) == -1)
177c4a5ef6eSThomas Moestl 			return(-1);
178c4a5ef6eSThomas Moestl 		else
179c4a5ef6eSThomas Moestl 			return(numdevs);
180c4a5ef6eSThomas Moestl 	}
18128fb27baSJustin T. Gibbs }
18228fb27baSJustin T. Gibbs 
18328fb27baSJustin T. Gibbs /*
18428fb27baSJustin T. Gibbs  * This is an easy way to get the generation number, but the generation is
18528fb27baSJustin T. Gibbs  * supplied in a more atmoic manner by the kern.devstat.all sysctl.
18628fb27baSJustin T. Gibbs  * Because this generation sysctl is separate from the statistics sysctl,
18728fb27baSJustin T. Gibbs  * the device list and the generation could change between the time that
18828fb27baSJustin T. Gibbs  * this function is called and the device list is retreived.
18928fb27baSJustin T. Gibbs  */
190bcc6a3daSKenneth D. Merry long
191c4a5ef6eSThomas Moestl devstat_getgeneration(kvm_t *kd)
19228fb27baSJustin T. Gibbs {
19328fb27baSJustin T. Gibbs 	size_t gensize;
194bcc6a3daSKenneth D. Merry 	long generation;
195c3508206SKenneth D. Merry 	const char *func_name = "devstat_getgeneration";
19628fb27baSJustin T. Gibbs 
197bcc6a3daSKenneth D. Merry 	gensize = sizeof(long);
19828fb27baSJustin T. Gibbs 
19928fb27baSJustin T. Gibbs 	/*
20028fb27baSJustin T. Gibbs 	 * Get the current generation number.
20128fb27baSJustin T. Gibbs 	 */
202c4a5ef6eSThomas Moestl 	if (kd == NULL) {
20328fb27baSJustin T. Gibbs 		if (sysctlbyname("kern.devstat.generation", &generation,
20428fb27baSJustin T. Gibbs 				 &gensize, NULL, 0) == -1) {
205c4a5ef6eSThomas Moestl 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
206c4a5ef6eSThomas Moestl 				 "%s: error getting devstat generation\n%s: %s",
207c4a5ef6eSThomas Moestl 				 func_name, func_name, strerror(errno));
20828fb27baSJustin T. Gibbs 			return(-1);
20928fb27baSJustin T. Gibbs 		} else
21028fb27baSJustin T. Gibbs 			return(generation);
211c4a5ef6eSThomas Moestl 	} else {
212c4a5ef6eSThomas Moestl 		if (KREADNL(kd, X_GENERATION, generation) == -1)
213c4a5ef6eSThomas Moestl 			return(-1);
214c4a5ef6eSThomas Moestl 		else
215c4a5ef6eSThomas Moestl 			return(generation);
216c4a5ef6eSThomas Moestl 	}
21728fb27baSJustin T. Gibbs }
21828fb27baSJustin T. Gibbs 
21928fb27baSJustin T. Gibbs /*
22028fb27baSJustin T. Gibbs  * Get the current devstat version.  The return value of this function
22128fb27baSJustin T. Gibbs  * should be compared with DEVSTAT_VERSION, which is defined in
22228fb27baSJustin T. Gibbs  * sys/devicestat.h.  This will enable userland programs to determine
22328fb27baSJustin T. Gibbs  * whether they are out of sync with the kernel.
22428fb27baSJustin T. Gibbs  */
22528fb27baSJustin T. Gibbs int
226c4a5ef6eSThomas Moestl devstat_getversion(kvm_t *kd)
22728fb27baSJustin T. Gibbs {
22828fb27baSJustin T. Gibbs 	size_t versize;
22928fb27baSJustin T. Gibbs 	int version;
230c3508206SKenneth D. Merry 	const char *func_name = "devstat_getversion";
23128fb27baSJustin T. Gibbs 
23228fb27baSJustin T. Gibbs 	versize = sizeof(int);
23328fb27baSJustin T. Gibbs 
23428fb27baSJustin T. Gibbs 	/*
23528fb27baSJustin T. Gibbs 	 * Get the current devstat version.
23628fb27baSJustin T. Gibbs 	 */
237c4a5ef6eSThomas Moestl 	if (kd == NULL) {
23828fb27baSJustin T. Gibbs 		if (sysctlbyname("kern.devstat.version", &version, &versize,
23928fb27baSJustin T. Gibbs 				 NULL, 0) == -1) {
240c4a5ef6eSThomas Moestl 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
241c4a5ef6eSThomas Moestl 				 "%s: error getting devstat version\n%s: %s",
242c4a5ef6eSThomas Moestl 				 func_name, func_name, strerror(errno));
24328fb27baSJustin T. Gibbs 			return(-1);
24428fb27baSJustin T. Gibbs 		} else
24528fb27baSJustin T. Gibbs 			return(version);
246c4a5ef6eSThomas Moestl 	} else {
247c4a5ef6eSThomas Moestl 		if (KREADNL(kd, X_VERSION, version) == -1)
248c4a5ef6eSThomas Moestl 			return(-1);
249c4a5ef6eSThomas Moestl 		else
250c4a5ef6eSThomas Moestl 			return(version);
251c4a5ef6eSThomas Moestl 	}
25228fb27baSJustin T. Gibbs }
25328fb27baSJustin T. Gibbs 
25428fb27baSJustin T. Gibbs /*
25528fb27baSJustin T. Gibbs  * Check the devstat version we know about against the devstat version the
25628fb27baSJustin T. Gibbs  * kernel knows about.  If they don't match, print an error into the
25728fb27baSJustin T. Gibbs  * devstat error buffer, and return -1.  If they match, return 0.
25828fb27baSJustin T. Gibbs  */
25928fb27baSJustin T. Gibbs int
260c4a5ef6eSThomas Moestl devstat_checkversion(kvm_t *kd)
26128fb27baSJustin T. Gibbs {
262c3508206SKenneth D. Merry 	const char *func_name = "devstat_checkversion";
2636d3e1426SBrian Somers 	int buflen, res, retval = 0, version;
26428fb27baSJustin T. Gibbs 
265c4a5ef6eSThomas Moestl 	version = devstat_getversion(kd);
266eded794aSKenneth D. Merry 
267eded794aSKenneth D. Merry 	if (version != DEVSTAT_VERSION) {
268eded794aSKenneth D. Merry 		/*
2696d3e1426SBrian Somers 		 * If getversion() returns an error (i.e. -1), then it
270eded794aSKenneth D. Merry 		 * has printed an error message in the buffer.  Therefore,
271eded794aSKenneth D. Merry 		 * we need to add a \n to the end of that message before we
272eded794aSKenneth D. Merry 		 * print our own message in the buffer.
273eded794aSKenneth D. Merry 		 */
2746d3e1426SBrian Somers 		if (version == -1)
275eded794aSKenneth D. Merry 			buflen = strlen(devstat_errbuf);
2766d3e1426SBrian Somers 		else
2776d3e1426SBrian Somers 			buflen = 0;
278eded794aSKenneth D. Merry 
2796d3e1426SBrian Somers 		res = snprintf(devstat_errbuf + buflen,
2806d3e1426SBrian Somers 			       DEVSTAT_ERRBUF_SIZE - buflen,
2816d3e1426SBrian Somers 			       "%s%s: userland devstat version %d is not "
2824fb9d384SKenneth D. Merry 			       "the same as the kernel\n%s: devstat "
2834fb9d384SKenneth D. Merry 			       "version %d\n", version == -1 ? "\n" : "",
2844fb9d384SKenneth D. Merry 			       func_name, DEVSTAT_VERSION, func_name, version);
28528fb27baSJustin T. Gibbs 
2866d3e1426SBrian Somers 		if (res < 0)
2876d3e1426SBrian Somers 			devstat_errbuf[buflen] = '\0';
288eded794aSKenneth D. Merry 
2896d3e1426SBrian Somers 		buflen = strlen(devstat_errbuf);
290eded794aSKenneth D. Merry 		if (version < DEVSTAT_VERSION)
2916d3e1426SBrian Somers 			res = snprintf(devstat_errbuf + buflen,
2926d3e1426SBrian Somers 				       DEVSTAT_ERRBUF_SIZE - buflen,
2934fb9d384SKenneth D. Merry 				       "%s: libdevstat newer than kernel\n",
2944fb9d384SKenneth D. Merry 				       func_name);
29528fb27baSJustin T. Gibbs 		else
2966d3e1426SBrian Somers 			res = snprintf(devstat_errbuf + buflen,
2976d3e1426SBrian Somers 				       DEVSTAT_ERRBUF_SIZE - buflen,
2984fb9d384SKenneth D. Merry 				       "%s: kernel newer than libdevstat\n",
2994fb9d384SKenneth D. Merry 				       func_name);
30028fb27baSJustin T. Gibbs 
3016d3e1426SBrian Somers 		if (res < 0)
3026d3e1426SBrian Somers 			devstat_errbuf[buflen] = '\0';
30328fb27baSJustin T. Gibbs 
30428fb27baSJustin T. Gibbs 		retval = -1;
30528fb27baSJustin T. Gibbs 	}
30628fb27baSJustin T. Gibbs 
30728fb27baSJustin T. Gibbs 	return(retval);
30828fb27baSJustin T. Gibbs }
30928fb27baSJustin T. Gibbs 
31028fb27baSJustin T. Gibbs /*
31128fb27baSJustin T. Gibbs  * Get the current list of devices and statistics, and the current
31228fb27baSJustin T. Gibbs  * generation number.
31328fb27baSJustin T. Gibbs  *
31428fb27baSJustin T. Gibbs  * Return values:
31528fb27baSJustin T. Gibbs  * -1  -- error
31628fb27baSJustin T. Gibbs  *  0  -- device list is unchanged
31728fb27baSJustin T. Gibbs  *  1  -- device list has changed
31828fb27baSJustin T. Gibbs  */
31928fb27baSJustin T. Gibbs int
320c4a5ef6eSThomas Moestl devstat_getdevs(kvm_t *kd, struct statinfo *stats)
32128fb27baSJustin T. Gibbs {
32228fb27baSJustin T. Gibbs 	int error;
32328fb27baSJustin T. Gibbs 	size_t dssize;
324bcc6a3daSKenneth D. Merry 	int oldnumdevs;
325bcc6a3daSKenneth D. Merry 	long oldgeneration;
32628fb27baSJustin T. Gibbs 	int retval = 0;
32728fb27baSJustin T. Gibbs 	struct devinfo *dinfo;
328c3508206SKenneth D. Merry 	const char *func_name = "devstat_getdevs";
3297194d335SPoul-Henning Kamp 	struct timespec ts;
33028fb27baSJustin T. Gibbs 
33128fb27baSJustin T. Gibbs 	dinfo = stats->dinfo;
33228fb27baSJustin T. Gibbs 
33328fb27baSJustin T. Gibbs 	if (dinfo == NULL) {
334c4a5ef6eSThomas Moestl 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
335c4a5ef6eSThomas Moestl 			 "%s: stats->dinfo was NULL", func_name);
33628fb27baSJustin T. Gibbs 		return(-1);
33728fb27baSJustin T. Gibbs 	}
33828fb27baSJustin T. Gibbs 
33928fb27baSJustin T. Gibbs 	oldnumdevs = dinfo->numdevs;
34028fb27baSJustin T. Gibbs 	oldgeneration = dinfo->generation;
34128fb27baSJustin T. Gibbs 
3427194d335SPoul-Henning Kamp 	clock_gettime(CLOCK_MONOTONIC, &ts);
3437194d335SPoul-Henning Kamp 	stats->snap_time = ts.tv_sec + ts.tv_nsec * 1e-9;
344c4a5ef6eSThomas Moestl 
345c4a5ef6eSThomas Moestl 	if (kd == NULL) {
346c4a5ef6eSThomas Moestl 		/* If this is our first time through, mem_ptr will be null. */
34728fb27baSJustin T. Gibbs 		if (dinfo->mem_ptr == NULL) {
34828fb27baSJustin T. Gibbs 			/*
34928fb27baSJustin T. Gibbs 			 * Get the number of devices.  If it's negative, it's an
35028fb27baSJustin T. Gibbs 			 * error.  Don't bother setting the error string, since
35128fb27baSJustin T. Gibbs 			 * getnumdevs() has already done that for us.
35228fb27baSJustin T. Gibbs 			 */
35316830b0cSPoul-Henning Kamp 			if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0)
35428fb27baSJustin T. Gibbs 				return(-1);
35528fb27baSJustin T. Gibbs 
35628fb27baSJustin T. Gibbs 			/*
357c4a5ef6eSThomas Moestl 			 * The kern.devstat.all sysctl returns the current
358c4a5ef6eSThomas Moestl 			 * generation number, as well as all the devices.
359c4a5ef6eSThomas Moestl 			 * So we need four bytes more.
36028fb27baSJustin T. Gibbs 			 */
361c4a5ef6eSThomas Moestl 			dssize = (dinfo->numdevs * sizeof(struct devstat)) +
362c4a5ef6eSThomas Moestl 				 sizeof(long);
36328fb27baSJustin T. Gibbs 			dinfo->mem_ptr = (u_int8_t *)malloc(dssize);
36428fb27baSJustin T. Gibbs 		} else
365c4a5ef6eSThomas Moestl 			dssize = (dinfo->numdevs * sizeof(struct devstat)) +
366c4a5ef6eSThomas Moestl 				 sizeof(long);
36728fb27baSJustin T. Gibbs 
36828fb27baSJustin T. Gibbs 		/*
36928fb27baSJustin T. Gibbs 		 * Request all of the devices.  We only really allow for one
370c4a5ef6eSThomas Moestl 		 * ENOMEM failure.  It would, of course, be possible to just go
371c4a5ef6eSThomas Moestl 		 * in a loop and keep reallocing the device structure until we
372c4a5ef6eSThomas Moestl 		 * don't get ENOMEM back.  I'm not sure it's worth it, though.
373c4a5ef6eSThomas Moestl 		 * If devices are being added to the system that quickly, maybe
374c4a5ef6eSThomas Moestl 		 * the user can just wait until all devices are added.
37528fb27baSJustin T. Gibbs 		 */
37636eab1f5SPoul-Henning Kamp 		for (;;) {
37736eab1f5SPoul-Henning Kamp 			error = sysctlbyname("kern.devstat.all",
37836eab1f5SPoul-Henning Kamp 					     dinfo->mem_ptr,
37936eab1f5SPoul-Henning Kamp 					     &dssize, NULL, 0);
38036eab1f5SPoul-Henning Kamp 			if (error != -1 || errno != EBUSY)
38136eab1f5SPoul-Henning Kamp 				break;
38236eab1f5SPoul-Henning Kamp 		}
38336eab1f5SPoul-Henning Kamp 		if (error == -1) {
38428fb27baSJustin T. Gibbs 			/*
38528fb27baSJustin T. Gibbs 			 * If we get ENOMEM back, that means that there are
38628fb27baSJustin T. Gibbs 			 * more devices now, so we need to allocate more
38728fb27baSJustin T. Gibbs 			 * space for the device array.
38828fb27baSJustin T. Gibbs 			 */
38928fb27baSJustin T. Gibbs 			if (errno == ENOMEM) {
39028fb27baSJustin T. Gibbs 				/*
391c4a5ef6eSThomas Moestl 				 * No need to set the error string here,
3927194d335SPoul-Henning Kamp 				 * devstat_getnumdevs() will do that if it fails.
39328fb27baSJustin T. Gibbs 				 */
39416830b0cSPoul-Henning Kamp 				if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0)
39528fb27baSJustin T. Gibbs 					return(-1);
39628fb27baSJustin T. Gibbs 
397c4a5ef6eSThomas Moestl 				dssize = (dinfo->numdevs *
398c4a5ef6eSThomas Moestl 					sizeof(struct devstat)) + sizeof(long);
399c4a5ef6eSThomas Moestl 				dinfo->mem_ptr = (u_int8_t *)
400c4a5ef6eSThomas Moestl 					realloc(dinfo->mem_ptr, dssize);
40128fb27baSJustin T. Gibbs 				if ((error = sysctlbyname("kern.devstat.all",
40228fb27baSJustin T. Gibbs 				    dinfo->mem_ptr, &dssize, NULL, 0)) == -1) {
403c4a5ef6eSThomas Moestl 					snprintf(devstat_errbuf,
404c4a5ef6eSThomas Moestl 						 sizeof(devstat_errbuf),
405c4a5ef6eSThomas Moestl 					    	 "%s: error getting device "
406c4a5ef6eSThomas Moestl 					    	 "stats\n%s: %s", func_name,
407c4a5ef6eSThomas Moestl 					    	 func_name, strerror(errno));
40828fb27baSJustin T. Gibbs 					return(-1);
40928fb27baSJustin T. Gibbs 				}
41028fb27baSJustin T. Gibbs 			} else {
411c4a5ef6eSThomas Moestl 				snprintf(devstat_errbuf, sizeof(devstat_errbuf),
41228fb27baSJustin T. Gibbs 					 "%s: error getting device stats\n"
41328fb27baSJustin T. Gibbs 					 "%s: %s", func_name, func_name,
41428fb27baSJustin T. Gibbs 					 strerror(errno));
41528fb27baSJustin T. Gibbs 				return(-1);
41628fb27baSJustin T. Gibbs 			}
41728fb27baSJustin T. Gibbs 		}
41828fb27baSJustin T. Gibbs 
419c4a5ef6eSThomas Moestl 	} else {
420c4a5ef6eSThomas Moestl 		/*
421c4a5ef6eSThomas Moestl 		 * This is of course non-atomic, but since we are working
422c4a5ef6eSThomas Moestl 		 * on a core dump, the generation is unlikely to change
423c4a5ef6eSThomas Moestl 		 */
42416830b0cSPoul-Henning Kamp 		if ((dinfo->numdevs = devstat_getnumdevs(kd)) == -1)
425c4a5ef6eSThomas Moestl 			return(-1);
426c4a5ef6eSThomas Moestl 		if ((dinfo->mem_ptr = get_devstat_kvm(kd)) == NULL)
427c4a5ef6eSThomas Moestl 			return(-1);
428c4a5ef6eSThomas Moestl 	}
42928fb27baSJustin T. Gibbs 	/*
43028fb27baSJustin T. Gibbs 	 * The sysctl spits out the generation as the first four bytes,
43128fb27baSJustin T. Gibbs 	 * then all of the device statistics structures.
43228fb27baSJustin T. Gibbs 	 */
433bcc6a3daSKenneth D. Merry 	dinfo->generation = *(long *)dinfo->mem_ptr;
43428fb27baSJustin T. Gibbs 
43528fb27baSJustin T. Gibbs 	/*
43628fb27baSJustin T. Gibbs 	 * If the generation has changed, and if the current number of
43728fb27baSJustin T. Gibbs 	 * devices is not the same as the number of devices recorded in the
43828fb27baSJustin T. Gibbs 	 * devinfo structure, it is likely that the device list has shrunk.
43928fb27baSJustin T. Gibbs 	 * The reason that it is likely that the device list has shrunk in
44028fb27baSJustin T. Gibbs 	 * this case is that if the device list has grown, the sysctl above
44128fb27baSJustin T. Gibbs 	 * will return an ENOMEM error, and we will reset the number of
44228fb27baSJustin T. Gibbs 	 * devices and reallocate the device array.  If the second sysctl
44328fb27baSJustin T. Gibbs 	 * fails, we will return an error and therefore never get to this
44428fb27baSJustin T. Gibbs 	 * point.  If the device list has shrunk, the sysctl will not
44528fb27baSJustin T. Gibbs 	 * return an error since we have more space allocated than is
44628fb27baSJustin T. Gibbs 	 * necessary.  So, in the shrinkage case, we catch it here and
44728fb27baSJustin T. Gibbs 	 * reallocate the array so that we don't use any more space than is
44828fb27baSJustin T. Gibbs 	 * necessary.
44928fb27baSJustin T. Gibbs 	 */
45028fb27baSJustin T. Gibbs 	if (oldgeneration != dinfo->generation) {
45116830b0cSPoul-Henning Kamp 		if (devstat_getnumdevs(kd) != dinfo->numdevs) {
45216830b0cSPoul-Henning Kamp 			if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0)
45328fb27baSJustin T. Gibbs 				return(-1);
45428fb27baSJustin T. Gibbs 			dssize = (dinfo->numdevs * sizeof(struct devstat)) +
455bcc6a3daSKenneth D. Merry 				sizeof(long);
45628fb27baSJustin T. Gibbs 			dinfo->mem_ptr = (u_int8_t *)realloc(dinfo->mem_ptr,
45728fb27baSJustin T. Gibbs 							     dssize);
45828fb27baSJustin T. Gibbs 		}
45928fb27baSJustin T. Gibbs 		retval = 1;
46028fb27baSJustin T. Gibbs 	}
46128fb27baSJustin T. Gibbs 
462bcc6a3daSKenneth D. Merry 	dinfo->devices = (struct devstat *)(dinfo->mem_ptr + sizeof(long));
46328fb27baSJustin T. Gibbs 
46428fb27baSJustin T. Gibbs 	return(retval);
46528fb27baSJustin T. Gibbs }
46628fb27baSJustin T. Gibbs 
46728fb27baSJustin T. Gibbs /*
46828fb27baSJustin T. Gibbs  * selectdevs():
46928fb27baSJustin T. Gibbs  *
47028fb27baSJustin T. Gibbs  * Devices are selected/deselected based upon the following criteria:
47128fb27baSJustin T. Gibbs  * - devices specified by the user on the command line
47228fb27baSJustin T. Gibbs  * - devices matching any device type expressions given on the command line
47328fb27baSJustin T. Gibbs  * - devices with the highest I/O, if 'top' mode is enabled
47428fb27baSJustin T. Gibbs  * - the first n unselected devices in the device list, if maxshowdevs
47528fb27baSJustin T. Gibbs  *   devices haven't already been selected and if the user has not
47628fb27baSJustin T. Gibbs  *   specified any devices on the command line and if we're in "add" mode.
47728fb27baSJustin T. Gibbs  *
47828fb27baSJustin T. Gibbs  * Input parameters:
47928fb27baSJustin T. Gibbs  * - device selection list (dev_select)
48028fb27baSJustin T. Gibbs  * - current number of devices selected (num_selected)
48128fb27baSJustin T. Gibbs  * - total number of devices in the selection list (num_selections)
48228fb27baSJustin T. Gibbs  * - devstat generation as of the last time selectdevs() was called
48328fb27baSJustin T. Gibbs  *   (select_generation)
48428fb27baSJustin T. Gibbs  * - current devstat generation (current_generation)
48528fb27baSJustin T. Gibbs  * - current list of devices and statistics (devices)
48628fb27baSJustin T. Gibbs  * - number of devices in the current device list (numdevs)
48728fb27baSJustin T. Gibbs  * - compiled version of the command line device type arguments (matches)
48828fb27baSJustin T. Gibbs  *   - This is optional.  If the number of devices is 0, this will be ignored.
48928fb27baSJustin T. Gibbs  *   - The matching code pays attention to the current selection mode.  So
49028fb27baSJustin T. Gibbs  *     if you pass in a matching expression, it will be evaluated based
49128fb27baSJustin T. Gibbs  *     upon the selection mode that is passed in.  See below for details.
49228fb27baSJustin T. Gibbs  * - number of device type matching expressions (num_matches)
49328fb27baSJustin T. Gibbs  *   - Set to 0 to disable the matching code.
49428fb27baSJustin T. Gibbs  * - list of devices specified on the command line by the user (dev_selections)
49528fb27baSJustin T. Gibbs  * - number of devices selected on the command line by the user
49628fb27baSJustin T. Gibbs  *   (num_dev_selections)
49728fb27baSJustin T. Gibbs  * - Our selection mode.  There are four different selection modes:
49828fb27baSJustin T. Gibbs  *      - add mode.  (DS_SELECT_ADD) Any devices matching devices explicitly
49928fb27baSJustin T. Gibbs  *        selected by the user or devices matching a pattern given by the
50028fb27baSJustin T. Gibbs  *        user will be selected in addition to devices that are already
50128fb27baSJustin T. Gibbs  *        selected.  Additional devices will be selected, up to maxshowdevs
50228fb27baSJustin T. Gibbs  *        number of devices.
50328fb27baSJustin T. Gibbs  *      - only mode. (DS_SELECT_ONLY)  Only devices matching devices
50428fb27baSJustin T. Gibbs  *        explicitly given by the user or devices matching a pattern
50528fb27baSJustin T. Gibbs  *        given by the user will be selected.  No other devices will be
50628fb27baSJustin T. Gibbs  *        selected.
50728fb27baSJustin T. Gibbs  *      - addonly mode.  (DS_SELECT_ADDONLY)  This is similar to add and
50828fb27baSJustin T. Gibbs  *        only.  Basically, this will not de-select any devices that are
50928fb27baSJustin T. Gibbs  *        current selected, as only mode would, but it will also not
51028fb27baSJustin T. Gibbs  *        gratuitously select up to maxshowdevs devices as add mode would.
51128fb27baSJustin T. Gibbs  *      - remove mode.  (DS_SELECT_REMOVE)  Any devices matching devices
51228fb27baSJustin T. Gibbs  *        explicitly selected by the user or devices matching a pattern
51328fb27baSJustin T. Gibbs  *        given by the user will be de-selected.
51428fb27baSJustin T. Gibbs  * - maximum number of devices we can select (maxshowdevs)
51528fb27baSJustin T. Gibbs  * - flag indicating whether or not we're in 'top' mode (perf_select)
51628fb27baSJustin T. Gibbs  *
51728fb27baSJustin T. Gibbs  * Output data:
51828fb27baSJustin T. Gibbs  * - the device selection list may be modified and passed back out
51928fb27baSJustin T. Gibbs  * - the number of devices selected and the total number of items in the
52028fb27baSJustin T. Gibbs  *   device selection list may be changed
52128fb27baSJustin T. Gibbs  * - the selection generation may be changed to match the current generation
52228fb27baSJustin T. Gibbs  *
52328fb27baSJustin T. Gibbs  * Return values:
52428fb27baSJustin T. Gibbs  * -1  -- error
52528fb27baSJustin T. Gibbs  *  0  -- selected devices are unchanged
52628fb27baSJustin T. Gibbs  *  1  -- selected devices changed
52728fb27baSJustin T. Gibbs  */
52828fb27baSJustin T. Gibbs int
529c4a5ef6eSThomas Moestl devstat_selectdevs(struct device_selection **dev_select, int *num_selected,
530bcc6a3daSKenneth D. Merry 		   int *num_selections, long *select_generation,
531c4a5ef6eSThomas Moestl 		   long current_generation, struct devstat *devices,
532c4a5ef6eSThomas Moestl 		   int numdevs, struct devstat_match *matches, int num_matches,
53328fb27baSJustin T. Gibbs 		   char **dev_selections, int num_dev_selections,
534c4a5ef6eSThomas Moestl 		   devstat_select_mode select_mode, int maxshowdevs,
535c4a5ef6eSThomas Moestl 		   int perf_select)
53628fb27baSJustin T. Gibbs {
537be04b6d1SDavid E. O'Brien 	int i, j, k;
53828fb27baSJustin T. Gibbs 	int init_selections = 0, init_selected_var = 0;
53928fb27baSJustin T. Gibbs 	struct device_selection *old_dev_select = NULL;
54028fb27baSJustin T. Gibbs 	int old_num_selections = 0, old_num_selected;
54128fb27baSJustin T. Gibbs 	int selection_number = 0;
54228fb27baSJustin T. Gibbs 	int changed = 0, found = 0;
54328fb27baSJustin T. Gibbs 
54428fb27baSJustin T. Gibbs 	if ((dev_select == NULL) || (devices == NULL) || (numdevs <= 0))
54528fb27baSJustin T. Gibbs 		return(-1);
54628fb27baSJustin T. Gibbs 
54728fb27baSJustin T. Gibbs 	/*
54828fb27baSJustin T. Gibbs 	 * We always want to make sure that we have as many dev_select
54928fb27baSJustin T. Gibbs 	 * entries as there are devices.
55028fb27baSJustin T. Gibbs 	 */
55128fb27baSJustin T. Gibbs 	/*
55228fb27baSJustin T. Gibbs 	 * In this case, we haven't selected devices before.
55328fb27baSJustin T. Gibbs 	 */
55428fb27baSJustin T. Gibbs 	if (*dev_select == NULL) {
55528fb27baSJustin T. Gibbs 		*dev_select = (struct device_selection *)malloc(numdevs *
55628fb27baSJustin T. Gibbs 			sizeof(struct device_selection));
55728fb27baSJustin T. Gibbs 		*select_generation = current_generation;
55828fb27baSJustin T. Gibbs 		init_selections = 1;
55928fb27baSJustin T. Gibbs 		changed = 1;
56028fb27baSJustin T. Gibbs 	/*
56128fb27baSJustin T. Gibbs 	 * In this case, we have selected devices before, but the device
56228fb27baSJustin T. Gibbs 	 * list has changed since we last selected devices, so we need to
56328fb27baSJustin T. Gibbs 	 * either enlarge or reduce the size of the device selection list.
56428fb27baSJustin T. Gibbs 	 */
56528fb27baSJustin T. Gibbs 	} else if (*num_selections != numdevs) {
56628fb27baSJustin T. Gibbs 		*dev_select = (struct device_selection *)realloc(*dev_select,
56728fb27baSJustin T. Gibbs 			numdevs * sizeof(struct device_selection));
56828fb27baSJustin T. Gibbs 		*select_generation = current_generation;
56928fb27baSJustin T. Gibbs 		init_selections = 1;
57028fb27baSJustin T. Gibbs 	/*
57128fb27baSJustin T. Gibbs 	 * In this case, we've selected devices before, and the selection
57228fb27baSJustin T. Gibbs 	 * list is the same size as it was the last time, but the device
57328fb27baSJustin T. Gibbs 	 * list has changed.
57428fb27baSJustin T. Gibbs 	 */
57528fb27baSJustin T. Gibbs 	} else if (*select_generation < current_generation) {
57628fb27baSJustin T. Gibbs 		*select_generation = current_generation;
57728fb27baSJustin T. Gibbs 		init_selections = 1;
57828fb27baSJustin T. Gibbs 	}
57928fb27baSJustin T. Gibbs 
58028fb27baSJustin T. Gibbs 	/*
58128fb27baSJustin T. Gibbs 	 * If we're in "only" mode, we want to clear out the selected
58228fb27baSJustin T. Gibbs 	 * variable since we're going to select exactly what the user wants
58328fb27baSJustin T. Gibbs 	 * this time through.
58428fb27baSJustin T. Gibbs 	 */
58528fb27baSJustin T. Gibbs 	if (select_mode == DS_SELECT_ONLY)
58628fb27baSJustin T. Gibbs 		init_selected_var = 1;
58728fb27baSJustin T. Gibbs 
58828fb27baSJustin T. Gibbs 	/*
58928fb27baSJustin T. Gibbs 	 * In all cases, we want to back up the number of selected devices.
59028fb27baSJustin T. Gibbs 	 * It is a quick and accurate way to determine whether the selected
59128fb27baSJustin T. Gibbs 	 * devices have changed.
59228fb27baSJustin T. Gibbs 	 */
59328fb27baSJustin T. Gibbs 	old_num_selected = *num_selected;
59428fb27baSJustin T. Gibbs 
59528fb27baSJustin T. Gibbs 	/*
59628fb27baSJustin T. Gibbs 	 * We want to make a backup of the current selection list if
59728fb27baSJustin T. Gibbs 	 * the list of devices has changed, or if we're in performance
59828fb27baSJustin T. Gibbs 	 * selection mode.  In both cases, we don't want to make a backup
59928fb27baSJustin T. Gibbs 	 * if we already know for sure that the list will be different.
60028fb27baSJustin T. Gibbs 	 * This is certainly the case if this is our first time through the
60128fb27baSJustin T. Gibbs 	 * selection code.
60228fb27baSJustin T. Gibbs 	 */
60328fb27baSJustin T. Gibbs 	if (((init_selected_var != 0) || (init_selections != 0)
60428fb27baSJustin T. Gibbs 	 || (perf_select != 0)) && (changed == 0)){
60528fb27baSJustin T. Gibbs 		old_dev_select = (struct device_selection *)malloc(
60628fb27baSJustin T. Gibbs 		    *num_selections * sizeof(struct device_selection));
60728fb27baSJustin T. Gibbs 		old_num_selections = *num_selections;
60828fb27baSJustin T. Gibbs 		bcopy(*dev_select, old_dev_select,
60928fb27baSJustin T. Gibbs 		    sizeof(struct device_selection) * *num_selections);
61028fb27baSJustin T. Gibbs 	}
61128fb27baSJustin T. Gibbs 
61228fb27baSJustin T. Gibbs 	if (init_selections != 0) {
61328fb27baSJustin T. Gibbs 		bzero(*dev_select, sizeof(struct device_selection) * numdevs);
61428fb27baSJustin T. Gibbs 
61528fb27baSJustin T. Gibbs 		for (i = 0; i < numdevs; i++) {
61628fb27baSJustin T. Gibbs 			(*dev_select)[i].device_number =
61728fb27baSJustin T. Gibbs 				devices[i].device_number;
61828fb27baSJustin T. Gibbs 			strncpy((*dev_select)[i].device_name,
61928fb27baSJustin T. Gibbs 				devices[i].device_name,
62028fb27baSJustin T. Gibbs 				DEVSTAT_NAME_LEN);
62117ee2b20SKenneth D. Merry 			(*dev_select)[i].device_name[DEVSTAT_NAME_LEN - 1]='\0';
62228fb27baSJustin T. Gibbs 			(*dev_select)[i].unit_number = devices[i].unit_number;
62328fb27baSJustin T. Gibbs 			(*dev_select)[i].position = i;
62428fb27baSJustin T. Gibbs 		}
62528fb27baSJustin T. Gibbs 		*num_selections = numdevs;
62628fb27baSJustin T. Gibbs 	} else if (init_selected_var != 0) {
62728fb27baSJustin T. Gibbs 		for (i = 0; i < numdevs; i++)
62828fb27baSJustin T. Gibbs 			(*dev_select)[i].selected = 0;
62928fb27baSJustin T. Gibbs 	}
63028fb27baSJustin T. Gibbs 
63128fb27baSJustin T. Gibbs 	/* we haven't gotten around to selecting anything yet.. */
63228fb27baSJustin T. Gibbs 	if ((select_mode == DS_SELECT_ONLY) || (init_selections != 0)
63328fb27baSJustin T. Gibbs 	 || (init_selected_var != 0))
63428fb27baSJustin T. Gibbs 		*num_selected = 0;
63528fb27baSJustin T. Gibbs 
63628fb27baSJustin T. Gibbs 	/*
63728fb27baSJustin T. Gibbs 	 * Look through any devices the user specified on the command line
63828fb27baSJustin T. Gibbs 	 * and see if they match known devices.  If so, select them.
63928fb27baSJustin T. Gibbs 	 */
64028fb27baSJustin T. Gibbs 	for (i = 0; (i < *num_selections) && (num_dev_selections > 0); i++) {
64128fb27baSJustin T. Gibbs 		char tmpstr[80];
64228fb27baSJustin T. Gibbs 
64317ee2b20SKenneth D. Merry 		snprintf(tmpstr, sizeof(tmpstr), "%s%d",
64417ee2b20SKenneth D. Merry 			 (*dev_select)[i].device_name,
64528fb27baSJustin T. Gibbs 			 (*dev_select)[i].unit_number);
64628fb27baSJustin T. Gibbs 		for (j = 0; j < num_dev_selections; j++) {
64728fb27baSJustin T. Gibbs 			if (strcmp(tmpstr, dev_selections[j]) == 0) {
64828fb27baSJustin T. Gibbs 				/*
64928fb27baSJustin T. Gibbs 				 * Here we do different things based on the
65028fb27baSJustin T. Gibbs 				 * mode we're in.  If we're in add or
65128fb27baSJustin T. Gibbs 				 * addonly mode, we only select this device
65228fb27baSJustin T. Gibbs 				 * if it hasn't already been selected.
65328fb27baSJustin T. Gibbs 				 * Otherwise, we would be unnecessarily
65428fb27baSJustin T. Gibbs 				 * changing the selection order and
65528fb27baSJustin T. Gibbs 				 * incrementing the selection count.  If
65628fb27baSJustin T. Gibbs 				 * we're in only mode, we unconditionally
65728fb27baSJustin T. Gibbs 				 * select this device, since in only mode
65828fb27baSJustin T. Gibbs 				 * any previous selections are erased and
65928fb27baSJustin T. Gibbs 				 * manually specified devices are the first
66028fb27baSJustin T. Gibbs 				 * ones to be selected.  If we're in remove
66128fb27baSJustin T. Gibbs 				 * mode, we de-select the specified device and
66228fb27baSJustin T. Gibbs 				 * decrement the selection count.
66328fb27baSJustin T. Gibbs 				 */
66428fb27baSJustin T. Gibbs 				switch(select_mode) {
66528fb27baSJustin T. Gibbs 				case DS_SELECT_ADD:
66628fb27baSJustin T. Gibbs 				case DS_SELECT_ADDONLY:
66728fb27baSJustin T. Gibbs 					if ((*dev_select)[i].selected)
66828fb27baSJustin T. Gibbs 						break;
66928fb27baSJustin T. Gibbs 					/* FALLTHROUGH */
67028fb27baSJustin T. Gibbs 				case DS_SELECT_ONLY:
67128fb27baSJustin T. Gibbs 					(*dev_select)[i].selected =
67228fb27baSJustin T. Gibbs 						++selection_number;
67328fb27baSJustin T. Gibbs 					(*num_selected)++;
67428fb27baSJustin T. Gibbs 					break;
67528fb27baSJustin T. Gibbs 				case DS_SELECT_REMOVE:
67628fb27baSJustin T. Gibbs 					(*dev_select)[i].selected = 0;
67728fb27baSJustin T. Gibbs 					(*num_selected)--;
67828fb27baSJustin T. Gibbs 					/*
67928fb27baSJustin T. Gibbs 					 * This isn't passed back out, we
68028fb27baSJustin T. Gibbs 					 * just use it to keep track of
68128fb27baSJustin T. Gibbs 					 * how many devices we've removed.
68228fb27baSJustin T. Gibbs 					 */
68328fb27baSJustin T. Gibbs 					num_dev_selections--;
68428fb27baSJustin T. Gibbs 					break;
68528fb27baSJustin T. Gibbs 				}
68628fb27baSJustin T. Gibbs 				break;
68728fb27baSJustin T. Gibbs 			}
68828fb27baSJustin T. Gibbs 		}
68928fb27baSJustin T. Gibbs 	}
69028fb27baSJustin T. Gibbs 
69128fb27baSJustin T. Gibbs 	/*
69228fb27baSJustin T. Gibbs 	 * Go through the user's device type expressions and select devices
69328fb27baSJustin T. Gibbs 	 * accordingly.  We only do this if the number of devices already
69428fb27baSJustin T. Gibbs 	 * selected is less than the maximum number we can show.
69528fb27baSJustin T. Gibbs 	 */
69628fb27baSJustin T. Gibbs 	for (i = 0; (i < num_matches) && (*num_selected < maxshowdevs); i++) {
69728fb27baSJustin T. Gibbs 		/* We should probably indicate some error here */
69828fb27baSJustin T. Gibbs 		if ((matches[i].match_fields == DEVSTAT_MATCH_NONE)
69928fb27baSJustin T. Gibbs 		 || (matches[i].num_match_categories <= 0))
70028fb27baSJustin T. Gibbs 			continue;
70128fb27baSJustin T. Gibbs 
70228fb27baSJustin T. Gibbs 		for (j = 0; j < numdevs; j++) {
70328fb27baSJustin T. Gibbs 			int num_match_categories;
70428fb27baSJustin T. Gibbs 
70528fb27baSJustin T. Gibbs 			num_match_categories = matches[i].num_match_categories;
70628fb27baSJustin T. Gibbs 
70728fb27baSJustin T. Gibbs 			/*
70828fb27baSJustin T. Gibbs 			 * Determine whether or not the current device
70928fb27baSJustin T. Gibbs 			 * matches the given matching expression.  This if
71028fb27baSJustin T. Gibbs 			 * statement consists of three components:
71128fb27baSJustin T. Gibbs 			 *   - the device type check
71228fb27baSJustin T. Gibbs 			 *   - the device interface check
71328fb27baSJustin T. Gibbs 			 *   - the passthrough check
71428fb27baSJustin T. Gibbs 			 * If a the matching test is successful, it
71528fb27baSJustin T. Gibbs 			 * decrements the number of matching categories,
71628fb27baSJustin T. Gibbs 			 * and if we've reached the last element that
71728fb27baSJustin T. Gibbs 			 * needed to be matched, the if statement succeeds.
71828fb27baSJustin T. Gibbs 			 *
71928fb27baSJustin T. Gibbs 			 */
72028fb27baSJustin T. Gibbs 			if ((((matches[i].match_fields & DEVSTAT_MATCH_TYPE)!=0)
72128fb27baSJustin T. Gibbs 			  && ((devices[j].device_type & DEVSTAT_TYPE_MASK) ==
72228fb27baSJustin T. Gibbs 			        (matches[i].device_type & DEVSTAT_TYPE_MASK))
72328fb27baSJustin T. Gibbs 			  &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0)
72428fb27baSJustin T. Gibbs 			   || (((matches[i].match_fields &
72528fb27baSJustin T. Gibbs 				DEVSTAT_MATCH_PASS) == 0)
72628fb27baSJustin T. Gibbs 			    && ((devices[j].device_type &
72728fb27baSJustin T. Gibbs 			        DEVSTAT_TYPE_PASS) == 0)))
72828fb27baSJustin T. Gibbs 			  && (--num_match_categories == 0))
72928fb27baSJustin T. Gibbs 			 || (((matches[i].match_fields & DEVSTAT_MATCH_IF) != 0)
73028fb27baSJustin T. Gibbs 			  && ((devices[j].device_type & DEVSTAT_TYPE_IF_MASK) ==
73128fb27baSJustin T. Gibbs 			        (matches[i].device_type & DEVSTAT_TYPE_IF_MASK))
73228fb27baSJustin T. Gibbs 			  &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0)
73328fb27baSJustin T. Gibbs 			   || (((matches[i].match_fields &
73428fb27baSJustin T. Gibbs 				DEVSTAT_MATCH_PASS) == 0)
73528fb27baSJustin T. Gibbs 			    && ((devices[j].device_type &
73628fb27baSJustin T. Gibbs 				DEVSTAT_TYPE_PASS) == 0)))
73728fb27baSJustin T. Gibbs 			  && (--num_match_categories == 0))
73828fb27baSJustin T. Gibbs 			 || (((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0)
73928fb27baSJustin T. Gibbs 			  && ((devices[j].device_type & DEVSTAT_TYPE_PASS) != 0)
74028fb27baSJustin T. Gibbs 			  && (--num_match_categories == 0))) {
74128fb27baSJustin T. Gibbs 
74228fb27baSJustin T. Gibbs 				/*
74328fb27baSJustin T. Gibbs 				 * This is probably a non-optimal solution
74428fb27baSJustin T. Gibbs 				 * to the problem that the devices in the
74528fb27baSJustin T. Gibbs 				 * device list will not be in the same
74628fb27baSJustin T. Gibbs 				 * order as the devices in the selection
74728fb27baSJustin T. Gibbs 				 * array.
74828fb27baSJustin T. Gibbs 				 */
74928fb27baSJustin T. Gibbs 				for (k = 0; k < numdevs; k++) {
75028fb27baSJustin T. Gibbs 					if ((*dev_select)[k].position == j) {
75128fb27baSJustin T. Gibbs 						found = 1;
75228fb27baSJustin T. Gibbs 						break;
75328fb27baSJustin T. Gibbs 					}
75428fb27baSJustin T. Gibbs 				}
75528fb27baSJustin T. Gibbs 
75628fb27baSJustin T. Gibbs 				/*
75728fb27baSJustin T. Gibbs 				 * There shouldn't be a case where a device
75828fb27baSJustin T. Gibbs 				 * in the device list is not in the
75928fb27baSJustin T. Gibbs 				 * selection list...but it could happen.
76028fb27baSJustin T. Gibbs 				 */
76128fb27baSJustin T. Gibbs 				if (found != 1) {
76228fb27baSJustin T. Gibbs 					fprintf(stderr, "selectdevs: couldn't"
76328fb27baSJustin T. Gibbs 						" find %s%d in selection "
76428fb27baSJustin T. Gibbs 						"list\n",
76528fb27baSJustin T. Gibbs 						devices[j].device_name,
76628fb27baSJustin T. Gibbs 						devices[j].unit_number);
76728fb27baSJustin T. Gibbs 					break;
76828fb27baSJustin T. Gibbs 				}
76928fb27baSJustin T. Gibbs 
77028fb27baSJustin T. Gibbs 				/*
77128fb27baSJustin T. Gibbs 				 * We do different things based upon the
77228fb27baSJustin T. Gibbs 				 * mode we're in.  If we're in add or only
77328fb27baSJustin T. Gibbs 				 * mode, we go ahead and select this device
77428fb27baSJustin T. Gibbs 				 * if it hasn't already been selected.  If
77528fb27baSJustin T. Gibbs 				 * it has already been selected, we leave
77628fb27baSJustin T. Gibbs 				 * it alone so we don't mess up the
77728fb27baSJustin T. Gibbs 				 * selection ordering.  Manually specified
77828fb27baSJustin T. Gibbs 				 * devices have already been selected, and
77928fb27baSJustin T. Gibbs 				 * they have higher priority than pattern
78028fb27baSJustin T. Gibbs 				 * matched devices.  If we're in remove
78128fb27baSJustin T. Gibbs 				 * mode, we de-select the given device and
78228fb27baSJustin T. Gibbs 				 * decrement the selected count.
78328fb27baSJustin T. Gibbs 				 */
78428fb27baSJustin T. Gibbs 				switch(select_mode) {
78528fb27baSJustin T. Gibbs 				case DS_SELECT_ADD:
78628fb27baSJustin T. Gibbs 				case DS_SELECT_ADDONLY:
78728fb27baSJustin T. Gibbs 				case DS_SELECT_ONLY:
78828fb27baSJustin T. Gibbs 					if ((*dev_select)[k].selected != 0)
78928fb27baSJustin T. Gibbs 						break;
79028fb27baSJustin T. Gibbs 					(*dev_select)[k].selected =
79128fb27baSJustin T. Gibbs 						++selection_number;
79228fb27baSJustin T. Gibbs 					(*num_selected)++;
79328fb27baSJustin T. Gibbs 					break;
79428fb27baSJustin T. Gibbs 				case DS_SELECT_REMOVE:
79528fb27baSJustin T. Gibbs 					(*dev_select)[k].selected = 0;
79628fb27baSJustin T. Gibbs 					(*num_selected)--;
79728fb27baSJustin T. Gibbs 					break;
79828fb27baSJustin T. Gibbs 				}
79928fb27baSJustin T. Gibbs 			}
80028fb27baSJustin T. Gibbs 		}
80128fb27baSJustin T. Gibbs 	}
80228fb27baSJustin T. Gibbs 
80328fb27baSJustin T. Gibbs 	/*
80428fb27baSJustin T. Gibbs 	 * Here we implement "top" mode.  Devices are sorted in the
80528fb27baSJustin T. Gibbs 	 * selection array based on two criteria:  whether or not they are
80628fb27baSJustin T. Gibbs 	 * selected (not selection number, just the fact that they are
80728fb27baSJustin T. Gibbs 	 * selected!) and the number of bytes in the "bytes" field of the
80828fb27baSJustin T. Gibbs 	 * selection structure.  The bytes field generally must be kept up
80928fb27baSJustin T. Gibbs 	 * by the user.  In the future, it may be maintained by library
81028fb27baSJustin T. Gibbs 	 * functions, but for now the user has to do the work.
81128fb27baSJustin T. Gibbs 	 *
81228fb27baSJustin T. Gibbs 	 * At first glance, it may seem wrong that we don't go through and
81328fb27baSJustin T. Gibbs 	 * select every device in the case where the user hasn't specified
81428fb27baSJustin T. Gibbs 	 * any devices or patterns.  In fact, though, it won't make any
81528fb27baSJustin T. Gibbs 	 * difference in the device sorting.  In that particular case (i.e.
81628fb27baSJustin T. Gibbs 	 * when we're in "add" or "only" mode, and the user hasn't
81728fb27baSJustin T. Gibbs 	 * specified anything) the first time through no devices will be
81828fb27baSJustin T. Gibbs 	 * selected, so the only criterion used to sort them will be their
81928fb27baSJustin T. Gibbs 	 * performance.  The second time through, and every time thereafter,
82028fb27baSJustin T. Gibbs 	 * all devices will be selected, so again selection won't matter.
82128fb27baSJustin T. Gibbs 	 */
82228fb27baSJustin T. Gibbs 	if (perf_select != 0) {
82328fb27baSJustin T. Gibbs 
82428fb27baSJustin T. Gibbs 		/* Sort the device array by throughput  */
82528fb27baSJustin T. Gibbs 		qsort(*dev_select, *num_selections,
82628fb27baSJustin T. Gibbs 		      sizeof(struct device_selection),
82728fb27baSJustin T. Gibbs 		      compare_select);
82828fb27baSJustin T. Gibbs 
82928fb27baSJustin T. Gibbs 		if (*num_selected == 0) {
83028fb27baSJustin T. Gibbs 			/*
83128fb27baSJustin T. Gibbs 			 * Here we select every device in the array, if it
83228fb27baSJustin T. Gibbs 			 * isn't already selected.  Because the 'selected'
83328fb27baSJustin T. Gibbs 			 * variable in the selection array entries contains
83428fb27baSJustin T. Gibbs 			 * the selection order, the devstats routine can show
83528fb27baSJustin T. Gibbs 			 * the devices that were selected first.
83628fb27baSJustin T. Gibbs 			 */
83728fb27baSJustin T. Gibbs 			for (i = 0; i < *num_selections; i++) {
83828fb27baSJustin T. Gibbs 				if ((*dev_select)[i].selected == 0) {
83928fb27baSJustin T. Gibbs 					(*dev_select)[i].selected =
84028fb27baSJustin T. Gibbs 						++selection_number;
84128fb27baSJustin T. Gibbs 					(*num_selected)++;
84228fb27baSJustin T. Gibbs 				}
84328fb27baSJustin T. Gibbs 			}
84428fb27baSJustin T. Gibbs 		} else {
84528fb27baSJustin T. Gibbs 			selection_number = 0;
84628fb27baSJustin T. Gibbs 			for (i = 0; i < *num_selections; i++) {
84728fb27baSJustin T. Gibbs 				if ((*dev_select)[i].selected != 0) {
84828fb27baSJustin T. Gibbs 					(*dev_select)[i].selected =
84928fb27baSJustin T. Gibbs 						++selection_number;
85028fb27baSJustin T. Gibbs 				}
85128fb27baSJustin T. Gibbs 			}
85228fb27baSJustin T. Gibbs 		}
85328fb27baSJustin T. Gibbs 	}
85428fb27baSJustin T. Gibbs 
85528fb27baSJustin T. Gibbs 	/*
85628fb27baSJustin T. Gibbs 	 * If we're in the "add" selection mode and if we haven't already
85728fb27baSJustin T. Gibbs 	 * selected maxshowdevs number of devices, go through the array and
85828fb27baSJustin T. Gibbs 	 * select any unselected devices.  If we're in "only" mode, we
85928fb27baSJustin T. Gibbs 	 * obviously don't want to select anything other than what the user
86028fb27baSJustin T. Gibbs 	 * specifies.  If we're in "remove" mode, it probably isn't a good
86128fb27baSJustin T. Gibbs 	 * idea to go through and select any more devices, since we might
86228fb27baSJustin T. Gibbs 	 * end up selecting something that the user wants removed.  Through
86328fb27baSJustin T. Gibbs 	 * more complicated logic, we could actually figure this out, but
86428fb27baSJustin T. Gibbs 	 * that would probably require combining this loop with the various
86528fb27baSJustin T. Gibbs 	 * selections loops above.
86628fb27baSJustin T. Gibbs 	 */
86728fb27baSJustin T. Gibbs 	if ((select_mode == DS_SELECT_ADD) && (*num_selected < maxshowdevs)) {
86828fb27baSJustin T. Gibbs 		for (i = 0; i < *num_selections; i++)
86928fb27baSJustin T. Gibbs 			if ((*dev_select)[i].selected == 0) {
87028fb27baSJustin T. Gibbs 				(*dev_select)[i].selected = ++selection_number;
87128fb27baSJustin T. Gibbs 				(*num_selected)++;
87228fb27baSJustin T. Gibbs 			}
87328fb27baSJustin T. Gibbs 	}
87428fb27baSJustin T. Gibbs 
87528fb27baSJustin T. Gibbs 	/*
87628fb27baSJustin T. Gibbs 	 * Look at the number of devices that have been selected.  If it
87728fb27baSJustin T. Gibbs 	 * has changed, set the changed variable.  Otherwise, if we've
87828fb27baSJustin T. Gibbs 	 * made a backup of the selection list, compare it to the current
87928fb27baSJustin T. Gibbs 	 * selection list to see if the selected devices have changed.
88028fb27baSJustin T. Gibbs 	 */
88128fb27baSJustin T. Gibbs 	if ((changed == 0) && (old_num_selected != *num_selected))
88228fb27baSJustin T. Gibbs 		changed = 1;
88328fb27baSJustin T. Gibbs 	else if ((changed == 0) && (old_dev_select != NULL)) {
88428fb27baSJustin T. Gibbs 		/*
88528fb27baSJustin T. Gibbs 		 * Now we go through the selection list and we look at
88628fb27baSJustin T. Gibbs 		 * it three different ways.
88728fb27baSJustin T. Gibbs 		 */
88828fb27baSJustin T. Gibbs 		for (i = 0; (i < *num_selections) && (changed == 0) &&
88928fb27baSJustin T. Gibbs 		     (i < old_num_selections); i++) {
89028fb27baSJustin T. Gibbs 			/*
89128fb27baSJustin T. Gibbs 			 * If the device at index i in both the new and old
89228fb27baSJustin T. Gibbs 			 * selection arrays has the same device number and
89328fb27baSJustin T. Gibbs 			 * selection status, it hasn't changed.  We
89428fb27baSJustin T. Gibbs 			 * continue on to the next index.
89528fb27baSJustin T. Gibbs 			 */
89628fb27baSJustin T. Gibbs 			if (((*dev_select)[i].device_number ==
89728fb27baSJustin T. Gibbs 			     old_dev_select[i].device_number)
89828fb27baSJustin T. Gibbs 			 && ((*dev_select)[i].selected ==
89928fb27baSJustin T. Gibbs 			     old_dev_select[i].selected))
90028fb27baSJustin T. Gibbs 				continue;
90128fb27baSJustin T. Gibbs 
90228fb27baSJustin T. Gibbs 			/*
90328fb27baSJustin T. Gibbs 			 * Now, if we're still going through the if
90428fb27baSJustin T. Gibbs 			 * statement, the above test wasn't true.  So we
90528fb27baSJustin T. Gibbs 			 * check here to see if the device at index i in
90628fb27baSJustin T. Gibbs 			 * the current array is the same as the device at
90728fb27baSJustin T. Gibbs 			 * index i in the old array.  If it is, that means
90828fb27baSJustin T. Gibbs 			 * that its selection number has changed.  Set
90928fb27baSJustin T. Gibbs 			 * changed to 1 and exit the loop.
91028fb27baSJustin T. Gibbs 			 */
91128fb27baSJustin T. Gibbs 			else if ((*dev_select)[i].device_number ==
91228fb27baSJustin T. Gibbs 			          old_dev_select[i].device_number) {
91328fb27baSJustin T. Gibbs 				changed = 1;
91428fb27baSJustin T. Gibbs 				break;
91528fb27baSJustin T. Gibbs 			}
91628fb27baSJustin T. Gibbs 			/*
91728fb27baSJustin T. Gibbs 			 * If we get here, then the device at index i in
91828fb27baSJustin T. Gibbs 			 * the current array isn't the same device as the
91928fb27baSJustin T. Gibbs 			 * device at index i in the old array.
92028fb27baSJustin T. Gibbs 			 */
92128fb27baSJustin T. Gibbs 			else {
922c3508206SKenneth D. Merry 				found = 0;
92328fb27baSJustin T. Gibbs 
92428fb27baSJustin T. Gibbs 				/*
92528fb27baSJustin T. Gibbs 				 * Search through the old selection array
92628fb27baSJustin T. Gibbs 				 * looking for a device with the same
92728fb27baSJustin T. Gibbs 				 * device number as the device at index i
92828fb27baSJustin T. Gibbs 				 * in the current array.  If the selection
92928fb27baSJustin T. Gibbs 				 * status is the same, then we mark it as
93028fb27baSJustin T. Gibbs 				 * found.  If the selection status isn't
93128fb27baSJustin T. Gibbs 				 * the same, we break out of the loop.
93228fb27baSJustin T. Gibbs 				 * Since found isn't set, changed will be
93328fb27baSJustin T. Gibbs 				 * set to 1 below.
93428fb27baSJustin T. Gibbs 				 */
93528fb27baSJustin T. Gibbs 				for (j = 0; j < old_num_selections; j++) {
93628fb27baSJustin T. Gibbs 					if (((*dev_select)[i].device_number ==
93728fb27baSJustin T. Gibbs 					      old_dev_select[j].device_number)
93828fb27baSJustin T. Gibbs 					 && ((*dev_select)[i].selected ==
93928fb27baSJustin T. Gibbs 					      old_dev_select[j].selected)){
94028fb27baSJustin T. Gibbs 						found = 1;
94128fb27baSJustin T. Gibbs 						break;
94228fb27baSJustin T. Gibbs 					}
94328fb27baSJustin T. Gibbs 					else if ((*dev_select)[i].device_number
94428fb27baSJustin T. Gibbs 					    == old_dev_select[j].device_number)
94528fb27baSJustin T. Gibbs 						break;
94628fb27baSJustin T. Gibbs 				}
94728fb27baSJustin T. Gibbs 				if (found == 0)
94828fb27baSJustin T. Gibbs 					changed = 1;
94928fb27baSJustin T. Gibbs 			}
95028fb27baSJustin T. Gibbs 		}
95128fb27baSJustin T. Gibbs 	}
95228fb27baSJustin T. Gibbs 	if (old_dev_select != NULL)
95328fb27baSJustin T. Gibbs 		free(old_dev_select);
95428fb27baSJustin T. Gibbs 
95528fb27baSJustin T. Gibbs 	return(changed);
95628fb27baSJustin T. Gibbs }
95728fb27baSJustin T. Gibbs 
95828fb27baSJustin T. Gibbs /*
95928fb27baSJustin T. Gibbs  * Comparison routine for qsort() above.  Note that the comparison here is
96028fb27baSJustin T. Gibbs  * backwards -- generally, it should return a value to indicate whether
96128fb27baSJustin T. Gibbs  * arg1 is <, =, or > arg2.  Instead, it returns the opposite.  The reason
96228fb27baSJustin T. Gibbs  * it returns the opposite is so that the selection array will be sorted in
96328fb27baSJustin T. Gibbs  * order of decreasing performance.  We sort on two parameters.  The first
96428fb27baSJustin T. Gibbs  * sort key is whether or not one or the other of the devices in question
96528fb27baSJustin T. Gibbs  * has been selected.  If one of them has, and the other one has not, the
96628fb27baSJustin T. Gibbs  * selected device is automatically more important than the unselected
96728fb27baSJustin T. Gibbs  * device.  If neither device is selected, we judge the devices based upon
96828fb27baSJustin T. Gibbs  * performance.
96928fb27baSJustin T. Gibbs  */
97028fb27baSJustin T. Gibbs static int
97128fb27baSJustin T. Gibbs compare_select(const void *arg1, const void *arg2)
97228fb27baSJustin T. Gibbs {
973c3508206SKenneth D. Merry 	if ((((const struct device_selection *)arg1)->selected)
974c3508206SKenneth D. Merry 	 && (((const struct device_selection *)arg2)->selected == 0))
97528fb27baSJustin T. Gibbs 		return(-1);
976c3508206SKenneth D. Merry 	else if ((((const struct device_selection *)arg1)->selected == 0)
977c3508206SKenneth D. Merry 	      && (((const struct device_selection *)arg2)->selected))
97828fb27baSJustin T. Gibbs 		return(1);
979c3508206SKenneth D. Merry 	else if (((const struct device_selection *)arg2)->bytes <
980c3508206SKenneth D. Merry 	         ((const struct device_selection *)arg1)->bytes)
98128fb27baSJustin T. Gibbs 		return(-1);
982c3508206SKenneth D. Merry 	else if (((const struct device_selection *)arg2)->bytes >
983c3508206SKenneth D. Merry 		 ((const struct device_selection *)arg1)->bytes)
98428fb27baSJustin T. Gibbs 		return(1);
98528fb27baSJustin T. Gibbs 	else
98628fb27baSJustin T. Gibbs 		return(0);
98728fb27baSJustin T. Gibbs }
98828fb27baSJustin T. Gibbs 
98928fb27baSJustin T. Gibbs /*
99028fb27baSJustin T. Gibbs  * Take a string with the general format "arg1,arg2,arg3", and build a
99128fb27baSJustin T. Gibbs  * device matching expression from it.
99228fb27baSJustin T. Gibbs  */
99328fb27baSJustin T. Gibbs int
994c4a5ef6eSThomas Moestl devstat_buildmatch(char *match_str, struct devstat_match **matches,
995c4a5ef6eSThomas Moestl 		   int *num_matches)
99628fb27baSJustin T. Gibbs {
99728fb27baSJustin T. Gibbs 	char *tstr[5];
99828fb27baSJustin T. Gibbs 	char **tempstr;
99928fb27baSJustin T. Gibbs 	int num_args;
1000be04b6d1SDavid E. O'Brien 	int i, j;
1001c3508206SKenneth D. Merry 	const char *func_name = "devstat_buildmatch";
100228fb27baSJustin T. Gibbs 
100328fb27baSJustin T. Gibbs 	/* We can't do much without a string to parse */
100428fb27baSJustin T. Gibbs 	if (match_str == NULL) {
1005c4a5ef6eSThomas Moestl 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1006c4a5ef6eSThomas Moestl 			 "%s: no match expression", func_name);
100728fb27baSJustin T. Gibbs 		return(-1);
100828fb27baSJustin T. Gibbs 	}
100928fb27baSJustin T. Gibbs 
101028fb27baSJustin T. Gibbs 	/*
101128fb27baSJustin T. Gibbs 	 * Break the (comma delimited) input string out into separate strings.
101228fb27baSJustin T. Gibbs 	 */
101328fb27baSJustin T. Gibbs 	for (tempstr = tstr, num_args  = 0;
101428fb27baSJustin T. Gibbs 	     (*tempstr = strsep(&match_str, ",")) != NULL && (num_args < 5);
101528fb27baSJustin T. Gibbs 	     num_args++)
101628fb27baSJustin T. Gibbs 		if (**tempstr != '\0')
101728fb27baSJustin T. Gibbs 			if (++tempstr >= &tstr[5])
101828fb27baSJustin T. Gibbs 				break;
101928fb27baSJustin T. Gibbs 
102028fb27baSJustin T. Gibbs 	/* The user gave us too many type arguments */
102128fb27baSJustin T. Gibbs 	if (num_args > 3) {
1022c4a5ef6eSThomas Moestl 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1023c4a5ef6eSThomas Moestl 			 "%s: too many type arguments", func_name);
102428fb27baSJustin T. Gibbs 		return(-1);
102528fb27baSJustin T. Gibbs 	}
102628fb27baSJustin T. Gibbs 
102728fb27baSJustin T. Gibbs 	/*
102828fb27baSJustin T. Gibbs 	 * Since you can't realloc a pointer that hasn't been malloced
102928fb27baSJustin T. Gibbs 	 * first, we malloc first and then realloc.
103028fb27baSJustin T. Gibbs 	 */
103128fb27baSJustin T. Gibbs 	if (*num_matches == 0)
103228fb27baSJustin T. Gibbs 		*matches = (struct devstat_match *)malloc(
103328fb27baSJustin T. Gibbs 			   sizeof(struct devstat_match));
103428fb27baSJustin T. Gibbs 	else
103528fb27baSJustin T. Gibbs 		*matches = (struct devstat_match *)realloc(*matches,
103628fb27baSJustin T. Gibbs 			  sizeof(struct devstat_match) * (*num_matches + 1));
103728fb27baSJustin T. Gibbs 
103828fb27baSJustin T. Gibbs 	/* Make sure the current entry is clear */
103928fb27baSJustin T. Gibbs 	bzero(&matches[0][*num_matches], sizeof(struct devstat_match));
104028fb27baSJustin T. Gibbs 
104128fb27baSJustin T. Gibbs 	/*
104228fb27baSJustin T. Gibbs 	 * Step through the arguments the user gave us and build a device
104328fb27baSJustin T. Gibbs 	 * matching expression from them.
104428fb27baSJustin T. Gibbs 	 */
104528fb27baSJustin T. Gibbs 	for (i = 0; i < num_args; i++) {
104628fb27baSJustin T. Gibbs 		char *tempstr2, *tempstr3;
104728fb27baSJustin T. Gibbs 
104828fb27baSJustin T. Gibbs 		/*
104928fb27baSJustin T. Gibbs 		 * Get rid of leading white space.
105028fb27baSJustin T. Gibbs 		 */
105128fb27baSJustin T. Gibbs 		tempstr2 = tstr[i];
105228fb27baSJustin T. Gibbs 		while (isspace(*tempstr2) && (*tempstr2 != '\0'))
105328fb27baSJustin T. Gibbs 			tempstr2++;
105428fb27baSJustin T. Gibbs 
105528fb27baSJustin T. Gibbs 		/*
105628fb27baSJustin T. Gibbs 		 * Get rid of trailing white space.
105728fb27baSJustin T. Gibbs 		 */
105828fb27baSJustin T. Gibbs 		tempstr3 = &tempstr2[strlen(tempstr2) - 1];
105928fb27baSJustin T. Gibbs 
106028fb27baSJustin T. Gibbs 		while ((*tempstr3 != '\0') && (tempstr3 > tempstr2)
106128fb27baSJustin T. Gibbs 		    && (isspace(*tempstr3))) {
106228fb27baSJustin T. Gibbs 			*tempstr3 = '\0';
106328fb27baSJustin T. Gibbs 			tempstr3--;
106428fb27baSJustin T. Gibbs 		}
106528fb27baSJustin T. Gibbs 
106628fb27baSJustin T. Gibbs 		/*
106728fb27baSJustin T. Gibbs 		 * Go through the match table comparing the user's
106828fb27baSJustin T. Gibbs 		 * arguments to known device types, interfaces, etc.
106928fb27baSJustin T. Gibbs 		 */
107028fb27baSJustin T. Gibbs 		for (j = 0; match_table[j].match_str != NULL; j++) {
107128fb27baSJustin T. Gibbs 			/*
107228fb27baSJustin T. Gibbs 			 * We do case-insensitive matching, in case someone
107328fb27baSJustin T. Gibbs 			 * wants to enter "SCSI" instead of "scsi" or
107428fb27baSJustin T. Gibbs 			 * something like that.  Only compare as many
107528fb27baSJustin T. Gibbs 			 * characters as are in the string in the match
107628fb27baSJustin T. Gibbs 			 * table.  This should help if someone tries to use
107728fb27baSJustin T. Gibbs 			 * a super-long match expression.
107828fb27baSJustin T. Gibbs 			 */
107928fb27baSJustin T. Gibbs 			if (strncasecmp(tempstr2, match_table[j].match_str,
108028fb27baSJustin T. Gibbs 			    strlen(match_table[j].match_str)) == 0) {
108128fb27baSJustin T. Gibbs 				/*
108228fb27baSJustin T. Gibbs 				 * Make sure the user hasn't specified two
108328fb27baSJustin T. Gibbs 				 * items of the same type, like "da" and
108428fb27baSJustin T. Gibbs 				 * "cd".  One device cannot be both.
108528fb27baSJustin T. Gibbs 				 */
108628fb27baSJustin T. Gibbs 				if (((*matches)[*num_matches].match_fields &
108728fb27baSJustin T. Gibbs 				    match_table[j].match_field) != 0) {
1088c4a5ef6eSThomas Moestl 					snprintf(devstat_errbuf,
1089c4a5ef6eSThomas Moestl 						 sizeof(devstat_errbuf),
109028fb27baSJustin T. Gibbs 						 "%s: cannot have more than "
109128fb27baSJustin T. Gibbs 						 "one match item in a single "
109228fb27baSJustin T. Gibbs 						 "category", func_name);
109328fb27baSJustin T. Gibbs 					return(-1);
109428fb27baSJustin T. Gibbs 				}
109528fb27baSJustin T. Gibbs 				/*
109628fb27baSJustin T. Gibbs 				 * If we've gotten this far, we have a
109728fb27baSJustin T. Gibbs 				 * winner.  Set the appropriate fields in
109828fb27baSJustin T. Gibbs 				 * the match entry.
109928fb27baSJustin T. Gibbs 				 */
110028fb27baSJustin T. Gibbs 				(*matches)[*num_matches].match_fields |=
110128fb27baSJustin T. Gibbs 					match_table[j].match_field;
110228fb27baSJustin T. Gibbs 				(*matches)[*num_matches].device_type |=
110328fb27baSJustin T. Gibbs 					match_table[j].type;
110428fb27baSJustin T. Gibbs 				(*matches)[*num_matches].num_match_categories++;
110528fb27baSJustin T. Gibbs 				break;
110628fb27baSJustin T. Gibbs 			}
110728fb27baSJustin T. Gibbs 		}
110828fb27baSJustin T. Gibbs 		/*
110928fb27baSJustin T. Gibbs 		 * We should have found a match in the above for loop.  If
111028fb27baSJustin T. Gibbs 		 * not, that means the user entered an invalid device type
111128fb27baSJustin T. Gibbs 		 * or interface.
111228fb27baSJustin T. Gibbs 		 */
111328fb27baSJustin T. Gibbs 		if ((*matches)[*num_matches].num_match_categories != (i + 1)) {
111417ee2b20SKenneth D. Merry 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
111528fb27baSJustin T. Gibbs 				 "%s: unknown match item \"%s\"", func_name,
111628fb27baSJustin T. Gibbs 				 tstr[i]);
111728fb27baSJustin T. Gibbs 			return(-1);
111828fb27baSJustin T. Gibbs 		}
111928fb27baSJustin T. Gibbs 	}
112028fb27baSJustin T. Gibbs 
112128fb27baSJustin T. Gibbs 	(*num_matches)++;
112228fb27baSJustin T. Gibbs 
112328fb27baSJustin T. Gibbs 	return(0);
112428fb27baSJustin T. Gibbs }
112528fb27baSJustin T. Gibbs 
112628fb27baSJustin T. Gibbs /*
112728fb27baSJustin T. Gibbs  * Compute a number of device statistics.  Only one field is mandatory, and
112828fb27baSJustin T. Gibbs  * that is "current".  Everything else is optional.  The caller passes in
112928fb27baSJustin T. Gibbs  * pointers to variables to hold the various statistics he desires.  If he
113028fb27baSJustin T. Gibbs  * doesn't want a particular staistic, he should pass in a NULL pointer.
113128fb27baSJustin T. Gibbs  * Return values:
113228fb27baSJustin T. Gibbs  * 0   -- success
113328fb27baSJustin T. Gibbs  * -1  -- failure
113428fb27baSJustin T. Gibbs  */
113528fb27baSJustin T. Gibbs int
113628fb27baSJustin T. Gibbs compute_stats(struct devstat *current, struct devstat *previous,
113728fb27baSJustin T. Gibbs 	      long double etime, u_int64_t *total_bytes,
113828fb27baSJustin T. Gibbs 	      u_int64_t *total_transfers, u_int64_t *total_blocks,
113928fb27baSJustin T. Gibbs 	      long double *kb_per_transfer, long double *transfers_per_second,
114028fb27baSJustin T. Gibbs 	      long double *mb_per_second, long double *blocks_per_second,
114128fb27baSJustin T. Gibbs 	      long double *ms_per_transaction)
114228fb27baSJustin T. Gibbs {
1143884539f7SKenneth D. Merry 	return(devstat_compute_statistics(current, previous, etime,
1144884539f7SKenneth D. Merry 	       total_bytes ? DSM_TOTAL_BYTES : DSM_SKIP,
1145884539f7SKenneth D. Merry 	       total_bytes,
1146884539f7SKenneth D. Merry 	       total_transfers ? DSM_TOTAL_TRANSFERS : DSM_SKIP,
1147884539f7SKenneth D. Merry 	       total_transfers,
1148884539f7SKenneth D. Merry 	       total_blocks ? DSM_TOTAL_BLOCKS : DSM_SKIP,
1149884539f7SKenneth D. Merry 	       total_blocks,
1150884539f7SKenneth D. Merry 	       kb_per_transfer ? DSM_KB_PER_TRANSFER : DSM_SKIP,
1151884539f7SKenneth D. Merry 	       kb_per_transfer,
1152884539f7SKenneth D. Merry 	       transfers_per_second ? DSM_TRANSFERS_PER_SECOND : DSM_SKIP,
1153884539f7SKenneth D. Merry 	       transfers_per_second,
1154884539f7SKenneth D. Merry 	       mb_per_second ? DSM_MB_PER_SECOND : DSM_SKIP,
1155884539f7SKenneth D. Merry 	       mb_per_second,
1156884539f7SKenneth D. Merry 	       blocks_per_second ? DSM_BLOCKS_PER_SECOND : DSM_SKIP,
1157884539f7SKenneth D. Merry 	       blocks_per_second,
1158884539f7SKenneth D. Merry 	       ms_per_transaction ? DSM_MS_PER_TRANSACTION : DSM_SKIP,
1159884539f7SKenneth D. Merry 	       ms_per_transaction,
1160884539f7SKenneth D. Merry 	       DSM_NONE));
116128fb27baSJustin T. Gibbs }
116228fb27baSJustin T. Gibbs 
11637194d335SPoul-Henning Kamp 
11647194d335SPoul-Henning Kamp /* This is 1/2^64 */
11657194d335SPoul-Henning Kamp #define BINTIME_SCALE 5.42101086242752217003726400434970855712890625e-20
11667194d335SPoul-Henning Kamp 
116728fb27baSJustin T. Gibbs long double
11687194d335SPoul-Henning Kamp devstat_compute_etime(struct bintime *cur_time, struct bintime *prev_time)
116928fb27baSJustin T. Gibbs {
117028fb27baSJustin T. Gibbs 	long double etime;
117128fb27baSJustin T. Gibbs 
11727194d335SPoul-Henning Kamp 	etime = cur_time->sec;
11737194d335SPoul-Henning Kamp 	etime += cur_time->frac * BINTIME_SCALE;
11747194d335SPoul-Henning Kamp 	if (prev_time != NULL) {
11757194d335SPoul-Henning Kamp 		etime -= prev_time->sec;
11767194d335SPoul-Henning Kamp 		etime -= prev_time->frac * BINTIME_SCALE;
11777194d335SPoul-Henning Kamp 	}
117828fb27baSJustin T. Gibbs 	return(etime);
117928fb27baSJustin T. Gibbs }
1180c4a5ef6eSThomas Moestl 
118136eab1f5SPoul-Henning Kamp #define DELTA(field, index)				\
118236eab1f5SPoul-Henning Kamp 	(current->field[(index)] - (previous ? previous->field[(index)] : 0))
118336eab1f5SPoul-Henning Kamp 
118436eab1f5SPoul-Henning Kamp #define DELTA_T(field)					\
118536eab1f5SPoul-Henning Kamp 	devstat_compute_etime(&current->field,  	\
118636eab1f5SPoul-Henning Kamp 	(previous ? &previous->field : NULL))
118736eab1f5SPoul-Henning Kamp 
1188c4a5ef6eSThomas Moestl int
1189c4a5ef6eSThomas Moestl devstat_compute_statistics(struct devstat *current, struct devstat *previous,
1190c4a5ef6eSThomas Moestl 			   long double etime, ...)
1191c4a5ef6eSThomas Moestl {
1192c3508206SKenneth D. Merry 	const char *func_name = "devstat_compute_statistics";
119336eab1f5SPoul-Henning Kamp 	u_int64_t totalbytes, totalbytesread, totalbyteswrite, totalbytesfree;
1194c4a5ef6eSThomas Moestl 	u_int64_t totaltransfers, totaltransfersread, totaltransferswrite;
1195c4a5ef6eSThomas Moestl 	u_int64_t totaltransfersother, totalblocks, totalblocksread;
119636eab1f5SPoul-Henning Kamp 	u_int64_t totalblockswrite, totaltransfersfree, totalblocksfree;
1197c4a5ef6eSThomas Moestl 	va_list ap;
1198c4a5ef6eSThomas Moestl 	devstat_metric metric;
1199c4a5ef6eSThomas Moestl 	u_int64_t *destu64;
1200c4a5ef6eSThomas Moestl 	long double *destld;
120136eab1f5SPoul-Henning Kamp 	int retval, i;
1202c4a5ef6eSThomas Moestl 
1203c4a5ef6eSThomas Moestl 	retval = 0;
1204c4a5ef6eSThomas Moestl 
1205c4a5ef6eSThomas Moestl 	/*
1206c4a5ef6eSThomas Moestl 	 * current is the only mandatory field.
1207c4a5ef6eSThomas Moestl 	 */
1208c4a5ef6eSThomas Moestl 	if (current == NULL) {
1209c4a5ef6eSThomas Moestl 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1210c4a5ef6eSThomas Moestl 			 "%s: current stats structure was NULL", func_name);
1211c4a5ef6eSThomas Moestl 		return(-1);
1212c4a5ef6eSThomas Moestl 	}
1213c4a5ef6eSThomas Moestl 
121436eab1f5SPoul-Henning Kamp 	totalbytesread = DELTA(bytes, DEVSTAT_READ);
121536eab1f5SPoul-Henning Kamp 	totalbyteswrite = DELTA(bytes, DEVSTAT_WRITE);
121636eab1f5SPoul-Henning Kamp 	totalbytesfree = DELTA(bytes, DEVSTAT_FREE);
121736eab1f5SPoul-Henning Kamp 	totalbytes = totalbytesread + totalbyteswrite + totalbytesfree;
1218c4a5ef6eSThomas Moestl 
121936eab1f5SPoul-Henning Kamp 	totaltransfersread = DELTA(operations, DEVSTAT_READ);
122036eab1f5SPoul-Henning Kamp 	totaltransferswrite = DELTA(operations, DEVSTAT_WRITE);
122136eab1f5SPoul-Henning Kamp 	totaltransfersother = DELTA(operations, DEVSTAT_NO_DATA);
122236eab1f5SPoul-Henning Kamp 	totaltransfersfree = DELTA(operations, DEVSTAT_FREE);
1223c4a5ef6eSThomas Moestl 	totaltransfers = totaltransfersread + totaltransferswrite +
122436eab1f5SPoul-Henning Kamp 			 totaltransfersother + totaltransfersfree;
1225c4a5ef6eSThomas Moestl 
1226c4a5ef6eSThomas Moestl 	totalblocks = totalbytes;
1227c4a5ef6eSThomas Moestl 	totalblocksread = totalbytesread;
1228c4a5ef6eSThomas Moestl 	totalblockswrite = totalbyteswrite;
122936eab1f5SPoul-Henning Kamp 	totalblocksfree = totalbytesfree;
1230c4a5ef6eSThomas Moestl 
1231c4a5ef6eSThomas Moestl 	if (current->block_size > 0) {
1232c4a5ef6eSThomas Moestl 		totalblocks /= current->block_size;
1233c4a5ef6eSThomas Moestl 		totalblocksread /= current->block_size;
1234c4a5ef6eSThomas Moestl 		totalblockswrite /= current->block_size;
123536eab1f5SPoul-Henning Kamp 		totalblocksfree /= current->block_size;
1236c4a5ef6eSThomas Moestl 	} else {
1237c4a5ef6eSThomas Moestl 		totalblocks /= 512;
1238c4a5ef6eSThomas Moestl 		totalblocksread /= 512;
1239c4a5ef6eSThomas Moestl 		totalblockswrite /= 512;
124036eab1f5SPoul-Henning Kamp 		totalblocksfree /= 512;
1241c4a5ef6eSThomas Moestl 	}
1242c4a5ef6eSThomas Moestl 
1243c4a5ef6eSThomas Moestl 	va_start(ap, etime);
1244c4a5ef6eSThomas Moestl 
1245c4a5ef6eSThomas Moestl 	while ((metric = (devstat_metric)va_arg(ap, devstat_metric)) != 0) {
1246c4a5ef6eSThomas Moestl 
1247c4a5ef6eSThomas Moestl 		if (metric == DSM_NONE)
1248c4a5ef6eSThomas Moestl 			break;
1249c4a5ef6eSThomas Moestl 
1250c4a5ef6eSThomas Moestl 		if (metric >= DSM_MAX) {
1251c4a5ef6eSThomas Moestl 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1252c4a5ef6eSThomas Moestl 				 "%s: metric %d is out of range", func_name,
1253c4a5ef6eSThomas Moestl 				 metric);
1254c4a5ef6eSThomas Moestl 			retval = -1;
1255c4a5ef6eSThomas Moestl 			goto bailout;
1256c4a5ef6eSThomas Moestl 		}
1257c4a5ef6eSThomas Moestl 
1258c4a5ef6eSThomas Moestl 		switch (devstat_arg_list[metric].argtype) {
1259c4a5ef6eSThomas Moestl 		case DEVSTAT_ARG_UINT64:
1260c4a5ef6eSThomas Moestl 			destu64 = (u_int64_t *)va_arg(ap, u_int64_t *);
1261c4a5ef6eSThomas Moestl 			break;
1262c4a5ef6eSThomas Moestl 		case DEVSTAT_ARG_LD:
1263c4a5ef6eSThomas Moestl 			destld = (long double *)va_arg(ap, long double *);
1264884539f7SKenneth D. Merry 			break;
1265884539f7SKenneth D. Merry 		case DEVSTAT_ARG_SKIP:
1266884539f7SKenneth D. Merry 			destld = (long double *)va_arg(ap, long double *);
1267c4a5ef6eSThomas Moestl 			break;
1268c4a5ef6eSThomas Moestl 		default:
1269c4a5ef6eSThomas Moestl 			retval = -1;
1270c4a5ef6eSThomas Moestl 			goto bailout;
1271c4a5ef6eSThomas Moestl 			break; /* NOTREACHED */
1272c4a5ef6eSThomas Moestl 		}
1273c4a5ef6eSThomas Moestl 
1274884539f7SKenneth D. Merry 		if (devstat_arg_list[metric].argtype == DEVSTAT_ARG_SKIP)
1275884539f7SKenneth D. Merry 			continue;
1276884539f7SKenneth D. Merry 
1277c4a5ef6eSThomas Moestl 		switch (metric) {
1278c4a5ef6eSThomas Moestl 		case DSM_TOTAL_BYTES:
1279c4a5ef6eSThomas Moestl 			*destu64 = totalbytes;
1280c4a5ef6eSThomas Moestl 			break;
1281c4a5ef6eSThomas Moestl 		case DSM_TOTAL_BYTES_READ:
1282c4a5ef6eSThomas Moestl 			*destu64 = totalbytesread;
1283c4a5ef6eSThomas Moestl 			break;
1284c4a5ef6eSThomas Moestl 		case DSM_TOTAL_BYTES_WRITE:
1285c4a5ef6eSThomas Moestl 			*destu64 = totalbyteswrite;
1286c4a5ef6eSThomas Moestl 			break;
128736eab1f5SPoul-Henning Kamp 		case DSM_TOTAL_BYTES_FREE:
128836eab1f5SPoul-Henning Kamp 			*destu64 = totalbytesfree;
128936eab1f5SPoul-Henning Kamp 			break;
1290c4a5ef6eSThomas Moestl 		case DSM_TOTAL_TRANSFERS:
1291c4a5ef6eSThomas Moestl 			*destu64 = totaltransfers;
1292c4a5ef6eSThomas Moestl 			break;
1293c4a5ef6eSThomas Moestl 		case DSM_TOTAL_TRANSFERS_READ:
1294c4a5ef6eSThomas Moestl 			*destu64 = totaltransfersread;
1295c4a5ef6eSThomas Moestl 			break;
1296c4a5ef6eSThomas Moestl 		case DSM_TOTAL_TRANSFERS_WRITE:
1297c4a5ef6eSThomas Moestl 			*destu64 = totaltransferswrite;
1298c4a5ef6eSThomas Moestl 			break;
129936eab1f5SPoul-Henning Kamp 		case DSM_TOTAL_TRANSFERS_FREE:
130036eab1f5SPoul-Henning Kamp 			*destu64 = totaltransfersfree;
130136eab1f5SPoul-Henning Kamp 			break;
1302c4a5ef6eSThomas Moestl 		case DSM_TOTAL_TRANSFERS_OTHER:
1303c4a5ef6eSThomas Moestl 			*destu64 = totaltransfersother;
1304c4a5ef6eSThomas Moestl 			break;
1305c4a5ef6eSThomas Moestl 		case DSM_TOTAL_BLOCKS:
1306c4a5ef6eSThomas Moestl 			*destu64 = totalblocks;
1307c4a5ef6eSThomas Moestl 			break;
1308c4a5ef6eSThomas Moestl 		case DSM_TOTAL_BLOCKS_READ:
1309c4a5ef6eSThomas Moestl 			*destu64 = totalblocksread;
1310c4a5ef6eSThomas Moestl 			break;
1311c4a5ef6eSThomas Moestl 		case DSM_TOTAL_BLOCKS_WRITE:
1312c4a5ef6eSThomas Moestl 			*destu64 = totalblockswrite;
1313c4a5ef6eSThomas Moestl 			break;
131436eab1f5SPoul-Henning Kamp 		case DSM_TOTAL_BLOCKS_FREE:
131536eab1f5SPoul-Henning Kamp 			*destu64 = totalblocksfree;
131636eab1f5SPoul-Henning Kamp 			break;
1317c4a5ef6eSThomas Moestl 		case DSM_KB_PER_TRANSFER:
1318c4a5ef6eSThomas Moestl 			*destld = totalbytes;
1319c4a5ef6eSThomas Moestl 			*destld /= 1024;
1320c4a5ef6eSThomas Moestl 			if (totaltransfers > 0)
1321c4a5ef6eSThomas Moestl 				*destld /= totaltransfers;
1322c4a5ef6eSThomas Moestl 			else
1323c4a5ef6eSThomas Moestl 				*destld = 0.0;
1324c4a5ef6eSThomas Moestl 			break;
1325c4a5ef6eSThomas Moestl 		case DSM_KB_PER_TRANSFER_READ:
1326c4a5ef6eSThomas Moestl 			*destld = totalbytesread;
1327c4a5ef6eSThomas Moestl 			*destld /= 1024;
1328c4a5ef6eSThomas Moestl 			if (totaltransfersread > 0)
1329c4a5ef6eSThomas Moestl 				*destld /= totaltransfersread;
1330c4a5ef6eSThomas Moestl 			else
1331c4a5ef6eSThomas Moestl 				*destld = 0.0;
1332c4a5ef6eSThomas Moestl 			break;
1333c4a5ef6eSThomas Moestl 		case DSM_KB_PER_TRANSFER_WRITE:
1334c4a5ef6eSThomas Moestl 			*destld = totalbyteswrite;
1335c4a5ef6eSThomas Moestl 			*destld /= 1024;
1336c4a5ef6eSThomas Moestl 			if (totaltransferswrite > 0)
1337c4a5ef6eSThomas Moestl 				*destld /= totaltransferswrite;
1338c4a5ef6eSThomas Moestl 			else
1339c4a5ef6eSThomas Moestl 				*destld = 0.0;
1340c4a5ef6eSThomas Moestl 			break;
134136eab1f5SPoul-Henning Kamp 		case DSM_KB_PER_TRANSFER_FREE:
134236eab1f5SPoul-Henning Kamp 			*destld = totalbytesfree;
134336eab1f5SPoul-Henning Kamp 			*destld /= 1024;
134436eab1f5SPoul-Henning Kamp 			if (totaltransfersfree > 0)
134536eab1f5SPoul-Henning Kamp 				*destld /= totaltransfersfree;
134636eab1f5SPoul-Henning Kamp 			else
134736eab1f5SPoul-Henning Kamp 				*destld = 0.0;
134836eab1f5SPoul-Henning Kamp 			break;
1349c4a5ef6eSThomas Moestl 		case DSM_TRANSFERS_PER_SECOND:
1350c4a5ef6eSThomas Moestl 			if (etime > 0.0) {
1351c4a5ef6eSThomas Moestl 				*destld = totaltransfers;
1352c4a5ef6eSThomas Moestl 				*destld /= etime;
1353c4a5ef6eSThomas Moestl 			} else
1354c4a5ef6eSThomas Moestl 				*destld = 0.0;
1355c4a5ef6eSThomas Moestl 			break;
1356c4a5ef6eSThomas Moestl 		case DSM_TRANSFERS_PER_SECOND_READ:
1357c4a5ef6eSThomas Moestl 			if (etime > 0.0) {
1358c4a5ef6eSThomas Moestl 				*destld = totaltransfersread;
1359c4a5ef6eSThomas Moestl 				*destld /= etime;
1360c4a5ef6eSThomas Moestl 			} else
1361c4a5ef6eSThomas Moestl 				*destld = 0.0;
1362c4a5ef6eSThomas Moestl 			break;
1363c4a5ef6eSThomas Moestl 		case DSM_TRANSFERS_PER_SECOND_WRITE:
1364c4a5ef6eSThomas Moestl 			if (etime > 0.0) {
1365c4a5ef6eSThomas Moestl 				*destld = totaltransferswrite;
1366c4a5ef6eSThomas Moestl 				*destld /= etime;
1367c4a5ef6eSThomas Moestl 			} else
1368c4a5ef6eSThomas Moestl 				*destld = 0.0;
1369c4a5ef6eSThomas Moestl 			break;
137036eab1f5SPoul-Henning Kamp 		case DSM_TRANSFERS_PER_SECOND_FREE:
137136eab1f5SPoul-Henning Kamp 			if (etime > 0.0) {
137236eab1f5SPoul-Henning Kamp 				*destld = totaltransfersfree;
137336eab1f5SPoul-Henning Kamp 				*destld /= etime;
137436eab1f5SPoul-Henning Kamp 			} else
137536eab1f5SPoul-Henning Kamp 				*destld = 0.0;
137636eab1f5SPoul-Henning Kamp 			break;
1377c4a5ef6eSThomas Moestl 		case DSM_TRANSFERS_PER_SECOND_OTHER:
1378c4a5ef6eSThomas Moestl 			if (etime > 0.0) {
1379c4a5ef6eSThomas Moestl 				*destld = totaltransfersother;
1380c4a5ef6eSThomas Moestl 				*destld /= etime;
1381c4a5ef6eSThomas Moestl 			} else
1382c4a5ef6eSThomas Moestl 				*destld = 0.0;
1383c4a5ef6eSThomas Moestl 			break;
1384c4a5ef6eSThomas Moestl 		case DSM_MB_PER_SECOND:
1385c4a5ef6eSThomas Moestl 			*destld = totalbytes;
1386c4a5ef6eSThomas Moestl 			*destld /= 1024 * 1024;
1387c4a5ef6eSThomas Moestl 			if (etime > 0.0)
1388c4a5ef6eSThomas Moestl 				*destld /= etime;
1389c4a5ef6eSThomas Moestl 			else
1390c4a5ef6eSThomas Moestl 				*destld = 0.0;
1391c4a5ef6eSThomas Moestl 			break;
1392c4a5ef6eSThomas Moestl 		case DSM_MB_PER_SECOND_READ:
1393c4a5ef6eSThomas Moestl 			*destld = totalbytesread;
1394c4a5ef6eSThomas Moestl 			*destld /= 1024 * 1024;
1395c4a5ef6eSThomas Moestl 			if (etime > 0.0)
1396c4a5ef6eSThomas Moestl 				*destld /= etime;
1397c4a5ef6eSThomas Moestl 			else
1398c4a5ef6eSThomas Moestl 				*destld = 0.0;
1399c4a5ef6eSThomas Moestl 			break;
1400c4a5ef6eSThomas Moestl 		case DSM_MB_PER_SECOND_WRITE:
1401c4a5ef6eSThomas Moestl 			*destld = totalbyteswrite;
1402c4a5ef6eSThomas Moestl 			*destld /= 1024 * 1024;
1403c4a5ef6eSThomas Moestl 			if (etime > 0.0)
1404c4a5ef6eSThomas Moestl 				*destld /= etime;
1405c4a5ef6eSThomas Moestl 			else
1406c4a5ef6eSThomas Moestl 				*destld = 0.0;
1407c4a5ef6eSThomas Moestl 			break;
140836eab1f5SPoul-Henning Kamp 		case DSM_MB_PER_SECOND_FREE:
140936eab1f5SPoul-Henning Kamp 			*destld = totalbytesfree;
141036eab1f5SPoul-Henning Kamp 			*destld /= 1024 * 1024;
141136eab1f5SPoul-Henning Kamp 			if (etime > 0.0)
141236eab1f5SPoul-Henning Kamp 				*destld /= etime;
141336eab1f5SPoul-Henning Kamp 			else
141436eab1f5SPoul-Henning Kamp 				*destld = 0.0;
141536eab1f5SPoul-Henning Kamp 			break;
1416c4a5ef6eSThomas Moestl 		case DSM_BLOCKS_PER_SECOND:
1417c4a5ef6eSThomas Moestl 			*destld = totalblocks;
1418c4a5ef6eSThomas Moestl 			if (etime > 0.0)
1419c4a5ef6eSThomas Moestl 				*destld /= etime;
1420c4a5ef6eSThomas Moestl 			else
1421c4a5ef6eSThomas Moestl 				*destld = 0.0;
1422c4a5ef6eSThomas Moestl 			break;
1423c4a5ef6eSThomas Moestl 		case DSM_BLOCKS_PER_SECOND_READ:
1424c4a5ef6eSThomas Moestl 			*destld = totalblocksread;
1425c4a5ef6eSThomas Moestl 			if (etime > 0.0)
1426c4a5ef6eSThomas Moestl 				*destld /= etime;
1427c4a5ef6eSThomas Moestl 			else
1428c4a5ef6eSThomas Moestl 				*destld = 0.0;
1429c4a5ef6eSThomas Moestl 			break;
1430c4a5ef6eSThomas Moestl 		case DSM_BLOCKS_PER_SECOND_WRITE:
1431c4a5ef6eSThomas Moestl 			*destld = totalblockswrite;
1432c4a5ef6eSThomas Moestl 			if (etime > 0.0)
1433c4a5ef6eSThomas Moestl 				*destld /= etime;
1434c4a5ef6eSThomas Moestl 			else
1435c4a5ef6eSThomas Moestl 				*destld = 0.0;
1436c4a5ef6eSThomas Moestl 			break;
143736eab1f5SPoul-Henning Kamp 		case DSM_BLOCKS_PER_SECOND_FREE:
143836eab1f5SPoul-Henning Kamp 			*destld = totalblocksfree;
143936eab1f5SPoul-Henning Kamp 			if (etime > 0.0)
144036eab1f5SPoul-Henning Kamp 				*destld /= etime;
144136eab1f5SPoul-Henning Kamp 			else
144236eab1f5SPoul-Henning Kamp 				*destld = 0.0;
144336eab1f5SPoul-Henning Kamp 			break;
1444c4a5ef6eSThomas Moestl 		/*
1445c4a5ef6eSThomas Moestl 		 * This calculation is somewhat bogus.  It simply divides
1446c4a5ef6eSThomas Moestl 		 * the elapsed time by the total number of transactions
1447c4a5ef6eSThomas Moestl 		 * completed.  While that does give the caller a good
1448c4a5ef6eSThomas Moestl 		 * picture of the average rate of transaction completion,
1449c4a5ef6eSThomas Moestl 		 * it doesn't necessarily give the caller a good view of
1450c4a5ef6eSThomas Moestl 		 * how long transactions took to complete on average.
1451c4a5ef6eSThomas Moestl 		 * Those two numbers will be different for a device that
1452c4a5ef6eSThomas Moestl 		 * can handle more than one transaction at a time.  e.g.
1453c4a5ef6eSThomas Moestl 		 * SCSI disks doing tagged queueing.
1454c4a5ef6eSThomas Moestl 		 *
1455c4a5ef6eSThomas Moestl 		 * The only way to accurately determine the real average
1456c4a5ef6eSThomas Moestl 		 * time per transaction would be to compute and store the
1457c4a5ef6eSThomas Moestl 		 * time on a per-transaction basis.  That currently isn't
1458c4a5ef6eSThomas Moestl 		 * done in the kernel, and would only be desireable if it
1459c4a5ef6eSThomas Moestl 		 * could be implemented in a somewhat non-intrusive and high
1460c4a5ef6eSThomas Moestl 		 * performance way.
1461c4a5ef6eSThomas Moestl 		 */
1462c4a5ef6eSThomas Moestl 		case DSM_MS_PER_TRANSACTION:
1463c4a5ef6eSThomas Moestl 			if (totaltransfers > 0) {
146436eab1f5SPoul-Henning Kamp 				*destld = 0;
146536eab1f5SPoul-Henning Kamp 				for (i = 0; i < DEVSTAT_N_TRANS_FLAGS; i++)
146636eab1f5SPoul-Henning Kamp 					*destld += DELTA_T(duration[i]);
1467c4a5ef6eSThomas Moestl 				*destld /= totaltransfers;
1468c4a5ef6eSThomas Moestl 				*destld *= 1000;
1469c4a5ef6eSThomas Moestl 			} else
1470c4a5ef6eSThomas Moestl 				*destld = 0.0;
1471c4a5ef6eSThomas Moestl 			break;
1472c4a5ef6eSThomas Moestl 		/*
1473c4a5ef6eSThomas Moestl 		 * As above, these next two really only give the average
1474c4a5ef6eSThomas Moestl 		 * rate of completion for read and write transactions, not
1475c4a5ef6eSThomas Moestl 		 * the average time the transaction took to complete.
1476c4a5ef6eSThomas Moestl 		 */
1477c4a5ef6eSThomas Moestl 		case DSM_MS_PER_TRANSACTION_READ:
1478c4a5ef6eSThomas Moestl 			if (totaltransfersread > 0) {
147936eab1f5SPoul-Henning Kamp 				*destld = DELTA_T(duration[DEVSTAT_READ]);
1480c4a5ef6eSThomas Moestl 				*destld /= totaltransfersread;
1481c4a5ef6eSThomas Moestl 				*destld *= 1000;
1482c4a5ef6eSThomas Moestl 			} else
1483c4a5ef6eSThomas Moestl 				*destld = 0.0;
1484c4a5ef6eSThomas Moestl 			break;
1485c4a5ef6eSThomas Moestl 		case DSM_MS_PER_TRANSACTION_WRITE:
1486c4a5ef6eSThomas Moestl 			if (totaltransferswrite > 0) {
148736eab1f5SPoul-Henning Kamp 				*destld = DELTA_T(duration[DEVSTAT_WRITE]);
1488c4a5ef6eSThomas Moestl 				*destld /= totaltransferswrite;
1489c4a5ef6eSThomas Moestl 				*destld *= 1000;
1490c4a5ef6eSThomas Moestl 			} else
1491c4a5ef6eSThomas Moestl 				*destld = 0.0;
1492c4a5ef6eSThomas Moestl 			break;
149336eab1f5SPoul-Henning Kamp 		case DSM_MS_PER_TRANSACTION_FREE:
149436eab1f5SPoul-Henning Kamp 			if (totaltransfersfree > 0) {
149536eab1f5SPoul-Henning Kamp 				*destld = DELTA_T(duration[DEVSTAT_FREE]);
149636eab1f5SPoul-Henning Kamp 				*destld /= totaltransfersfree;
149736eab1f5SPoul-Henning Kamp 				*destld *= 1000;
149836eab1f5SPoul-Henning Kamp 			} else
149936eab1f5SPoul-Henning Kamp 				*destld = 0.0;
150036eab1f5SPoul-Henning Kamp 			break;
150136eab1f5SPoul-Henning Kamp 		case DSM_MS_PER_TRANSACTION_OTHER:
150236eab1f5SPoul-Henning Kamp 			if (totaltransfersother > 0) {
150336eab1f5SPoul-Henning Kamp 				*destld = DELTA_T(duration[DEVSTAT_NO_DATA]);
150436eab1f5SPoul-Henning Kamp 				*destld /= totaltransfersother;
150536eab1f5SPoul-Henning Kamp 				*destld *= 1000;
150636eab1f5SPoul-Henning Kamp 			} else
150736eab1f5SPoul-Henning Kamp 				*destld = 0.0;
150836eab1f5SPoul-Henning Kamp 			break;
150936eab1f5SPoul-Henning Kamp 		case DSM_BUSY_PCT:
151036eab1f5SPoul-Henning Kamp 			*destld = DELTA_T(busy_time);
151136eab1f5SPoul-Henning Kamp 			if (*destld < 0)
151236eab1f5SPoul-Henning Kamp 				*destld = 0;
151336eab1f5SPoul-Henning Kamp 			*destld /= etime;
151436eab1f5SPoul-Henning Kamp 			*destld *= 100;
151536eab1f5SPoul-Henning Kamp 			break;
151636eab1f5SPoul-Henning Kamp 		case DSM_QUEUE_LENGTH:
151736eab1f5SPoul-Henning Kamp 			*destu64 = current->start_count - current->end_count;
151836eab1f5SPoul-Henning Kamp 			break;
151936eab1f5SPoul-Henning Kamp /*
152036eab1f5SPoul-Henning Kamp  * XXX: comment out the default block to see if any case's are missing.
152136eab1f5SPoul-Henning Kamp  */
152236eab1f5SPoul-Henning Kamp #if 1
1523c4a5ef6eSThomas Moestl 		default:
1524c4a5ef6eSThomas Moestl 			/*
1525c4a5ef6eSThomas Moestl 			 * This shouldn't happen, since we should have
1526c4a5ef6eSThomas Moestl 			 * caught any out of range metrics at the top of
1527c4a5ef6eSThomas Moestl 			 * the loop.
1528c4a5ef6eSThomas Moestl 			 */
1529c4a5ef6eSThomas Moestl 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1530c4a5ef6eSThomas Moestl 				 "%s: unknown metric %d", func_name, metric);
1531c4a5ef6eSThomas Moestl 			retval = -1;
1532c4a5ef6eSThomas Moestl 			goto bailout;
1533c4a5ef6eSThomas Moestl 			break; /* NOTREACHED */
153436eab1f5SPoul-Henning Kamp #endif
1535c4a5ef6eSThomas Moestl 		}
1536c4a5ef6eSThomas Moestl 	}
1537c4a5ef6eSThomas Moestl 
1538c4a5ef6eSThomas Moestl bailout:
1539c4a5ef6eSThomas Moestl 
1540c4a5ef6eSThomas Moestl 	va_end(ap);
1541c4a5ef6eSThomas Moestl 	return(retval);
1542c4a5ef6eSThomas Moestl }
1543c4a5ef6eSThomas Moestl 
1544c4a5ef6eSThomas Moestl static int
1545c4a5ef6eSThomas Moestl readkmem(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes)
1546c4a5ef6eSThomas Moestl {
1547c3508206SKenneth D. Merry 	const char *func_name = "readkmem";
1548c4a5ef6eSThomas Moestl 
1549c4a5ef6eSThomas Moestl 	if (kvm_read(kd, addr, buf, nbytes) == -1) {
1550c4a5ef6eSThomas Moestl 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1551c4a5ef6eSThomas Moestl 			 "%s: error reading value (kvm_read): %s", func_name,
1552c4a5ef6eSThomas Moestl 			 kvm_geterr(kd));
1553c4a5ef6eSThomas Moestl 		return(-1);
1554c4a5ef6eSThomas Moestl 	}
1555c4a5ef6eSThomas Moestl 	return(0);
1556c4a5ef6eSThomas Moestl }
1557c4a5ef6eSThomas Moestl 
1558c4a5ef6eSThomas Moestl static int
1559c3508206SKenneth D. Merry readkmem_nl(kvm_t *kd, const char *name, void *buf, size_t nbytes)
1560c4a5ef6eSThomas Moestl {
1561c3508206SKenneth D. Merry 	const char *func_name = "readkmem_nl";
1562c3508206SKenneth D. Merry 	struct nlist nl[2];
1563c3508206SKenneth D. Merry 
1564c3508206SKenneth D. Merry 	(const char *)nl[0].n_name = name;
1565c3508206SKenneth D. Merry 	nl[1].n_name = NULL;
1566c4a5ef6eSThomas Moestl 
1567c4a5ef6eSThomas Moestl 	if (kvm_nlist(kd, nl) == -1) {
1568c4a5ef6eSThomas Moestl 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1569c4a5ef6eSThomas Moestl 			 "%s: error getting name list (kvm_nlist): %s",
1570c4a5ef6eSThomas Moestl 			 func_name, kvm_geterr(kd));
1571c4a5ef6eSThomas Moestl 		return(-1);
1572c4a5ef6eSThomas Moestl 	}
1573c4a5ef6eSThomas Moestl 	return(readkmem(kd, nl[0].n_value, buf, nbytes));
1574c4a5ef6eSThomas Moestl }
1575c4a5ef6eSThomas Moestl 
1576c4a5ef6eSThomas Moestl /*
1577c4a5ef6eSThomas Moestl  * This duplicates the functionality of the kernel sysctl handler for poking
1578c4a5ef6eSThomas Moestl  * through crash dumps.
1579c4a5ef6eSThomas Moestl  */
1580c4a5ef6eSThomas Moestl static char *
1581c4a5ef6eSThomas Moestl get_devstat_kvm(kvm_t *kd)
1582c4a5ef6eSThomas Moestl {
1583c4a5ef6eSThomas Moestl 	int error, i, wp;
1584c4a5ef6eSThomas Moestl 	long gen;
1585c4a5ef6eSThomas Moestl 	struct devstat *nds;
1586c4a5ef6eSThomas Moestl 	struct devstat ds;
1587c4a5ef6eSThomas Moestl 	struct devstatlist dhead;
1588c4a5ef6eSThomas Moestl 	int num_devs;
1589c4a5ef6eSThomas Moestl 	char *rv = NULL;
1590c3508206SKenneth D. Merry 	const char *func_name = "get_devstat_kvm";
1591c4a5ef6eSThomas Moestl 
15927194d335SPoul-Henning Kamp 	if ((num_devs = devstat_getnumdevs(kd)) <= 0)
1593c4a5ef6eSThomas Moestl 		return(NULL);
1594c4a5ef6eSThomas Moestl 	error = 0;
1595c4a5ef6eSThomas Moestl 	if (KREADNL(kd, X_DEVICE_STATQ, dhead) == -1)
1596c4a5ef6eSThomas Moestl 		return(NULL);
1597c4a5ef6eSThomas Moestl 
1598c4a5ef6eSThomas Moestl 	nds = STAILQ_FIRST(&dhead);
1599c4a5ef6eSThomas Moestl 
1600c4a5ef6eSThomas Moestl 	if ((rv = malloc(sizeof(gen))) == NULL) {
1601c4a5ef6eSThomas Moestl 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1602c4a5ef6eSThomas Moestl 			 "%s: out of memory (initial malloc failed)",
1603c4a5ef6eSThomas Moestl 			 func_name);
1604c4a5ef6eSThomas Moestl 		return(NULL);
1605c4a5ef6eSThomas Moestl 	}
16067194d335SPoul-Henning Kamp 	gen = devstat_getgeneration(kd);
1607c4a5ef6eSThomas Moestl 	memcpy(rv, &gen, sizeof(gen));
1608c4a5ef6eSThomas Moestl 	wp = sizeof(gen);
1609c4a5ef6eSThomas Moestl 	/*
1610c4a5ef6eSThomas Moestl 	 * Now push out all the devices.
1611c4a5ef6eSThomas Moestl 	 */
1612c4a5ef6eSThomas Moestl 	for (i = 0; (nds != NULL) && (i < num_devs);
1613c4a5ef6eSThomas Moestl 	     nds = STAILQ_NEXT(nds, dev_links), i++) {
1614c4a5ef6eSThomas Moestl 		if (readkmem(kd, (long)nds, &ds, sizeof(ds)) == -1) {
1615c4a5ef6eSThomas Moestl 			free(rv);
1616c4a5ef6eSThomas Moestl 			return(NULL);
1617c4a5ef6eSThomas Moestl 		}
1618c4a5ef6eSThomas Moestl 		nds = &ds;
1619c4a5ef6eSThomas Moestl 		rv = (char *)reallocf(rv, sizeof(gen) +
1620c4a5ef6eSThomas Moestl 				      sizeof(ds) * (i + 1));
1621c4a5ef6eSThomas Moestl 		if (rv == NULL) {
1622c4a5ef6eSThomas Moestl 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1623c4a5ef6eSThomas Moestl 				 "%s: out of memory (malloc failed)",
1624c4a5ef6eSThomas Moestl 				 func_name);
1625c4a5ef6eSThomas Moestl 			return(NULL);
1626c4a5ef6eSThomas Moestl 		}
1627c4a5ef6eSThomas Moestl 		memcpy(rv + wp, &ds, sizeof(ds));
1628c4a5ef6eSThomas Moestl 		wp += sizeof(ds);
1629c4a5ef6eSThomas Moestl 	}
1630c4a5ef6eSThomas Moestl 	return(rv);
1631c4a5ef6eSThomas Moestl }
1632