xref: /freebsd/lib/libdevstat/devstat.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
15e53a4f9SPedro F. Giffuni /*-
25e53a4f9SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
35e53a4f9SPedro F. Giffuni  *
428fb27baSJustin T. Gibbs  * Copyright (c) 1997, 1998 Kenneth D. Merry.
528fb27baSJustin T. Gibbs  * All rights reserved.
628fb27baSJustin T. Gibbs  *
728fb27baSJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
828fb27baSJustin T. Gibbs  * modification, are permitted provided that the following conditions
928fb27baSJustin T. Gibbs  * are met:
1028fb27baSJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
1128fb27baSJustin T. Gibbs  *    notice, this list of conditions and the following disclaimer.
1228fb27baSJustin T. Gibbs  * 2. Redistributions in binary form must reproduce the above copyright
1328fb27baSJustin T. Gibbs  *    notice, this list of conditions and the following disclaimer in the
1428fb27baSJustin T. Gibbs  *    documentation and/or other materials provided with the distribution.
1528fb27baSJustin T. Gibbs  * 3. The name of the author may not be used to endorse or promote products
1628fb27baSJustin T. Gibbs  *    derived from this software without specific prior written permission.
1728fb27baSJustin T. Gibbs  *
1828fb27baSJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1928fb27baSJustin T. Gibbs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2028fb27baSJustin T. Gibbs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2128fb27baSJustin T. Gibbs  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2228fb27baSJustin T. Gibbs  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2328fb27baSJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2428fb27baSJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2528fb27baSJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2628fb27baSJustin T. Gibbs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2728fb27baSJustin T. Gibbs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2828fb27baSJustin T. Gibbs  * SUCH DAMAGE.
2928fb27baSJustin T. Gibbs  */
3028fb27baSJustin T. Gibbs 
3128fb27baSJustin T. Gibbs #include <sys/types.h>
3228fb27baSJustin T. Gibbs #include <sys/sysctl.h>
3328fb27baSJustin T. Gibbs #include <sys/errno.h>
3412590690SPoul-Henning Kamp #include <sys/resource.h>
35c4a5ef6eSThomas Moestl #include <sys/queue.h>
3628fb27baSJustin T. Gibbs 
37eded794aSKenneth D. Merry #include <ctype.h>
3828fb27baSJustin T. Gibbs #include <err.h>
39c4a5ef6eSThomas Moestl #include <fcntl.h>
40c4a5ef6eSThomas Moestl #include <limits.h>
4128fb27baSJustin T. Gibbs #include <stdio.h>
4228fb27baSJustin T. Gibbs #include <stdlib.h>
4328fb27baSJustin T. Gibbs #include <string.h>
44c4a5ef6eSThomas Moestl #include <stdarg.h>
45c4a5ef6eSThomas Moestl #include <kvm.h>
461f85f715SBruce Evans #include <nlist.h>
4728fb27baSJustin T. Gibbs 
4828fb27baSJustin T. Gibbs #include "devstat.h"
4928fb27baSJustin T. Gibbs 
500b1b370cSPoul-Henning Kamp int
510b1b370cSPoul-Henning Kamp compute_stats(struct devstat *current, struct devstat *previous,
520b1b370cSPoul-Henning Kamp 	      long double etime, u_int64_t *total_bytes,
530b1b370cSPoul-Henning Kamp 	      u_int64_t *total_transfers, u_int64_t *total_blocks,
540b1b370cSPoul-Henning Kamp 	      long double *kb_per_transfer, long double *transfers_per_second,
550b1b370cSPoul-Henning Kamp 	      long double *mb_per_second, long double *blocks_per_second,
560b1b370cSPoul-Henning Kamp 	      long double *ms_per_transaction);
570b1b370cSPoul-Henning Kamp 
58c4a5ef6eSThomas Moestl typedef enum {
59c4a5ef6eSThomas Moestl 	DEVSTAT_ARG_NOTYPE,
60c4a5ef6eSThomas Moestl 	DEVSTAT_ARG_UINT64,
61884539f7SKenneth D. Merry 	DEVSTAT_ARG_LD,
62884539f7SKenneth D. Merry 	DEVSTAT_ARG_SKIP
63c4a5ef6eSThomas Moestl } devstat_arg_type;
64c4a5ef6eSThomas Moestl 
6528fb27baSJustin T. Gibbs char devstat_errbuf[DEVSTAT_ERRBUF_SIZE];
6628fb27baSJustin T. Gibbs 
6728fb27baSJustin T. Gibbs /*
6828fb27baSJustin T. Gibbs  * Table to match descriptive strings with device types.  These are in
6928fb27baSJustin T. Gibbs  * order from most common to least common to speed search time.
7028fb27baSJustin T. Gibbs  */
7128fb27baSJustin T. Gibbs struct devstat_match_table match_table[] = {
7228fb27baSJustin T. Gibbs 	{"da",		DEVSTAT_TYPE_DIRECT,	DEVSTAT_MATCH_TYPE},
7328fb27baSJustin T. Gibbs 	{"cd",		DEVSTAT_TYPE_CDROM,	DEVSTAT_MATCH_TYPE},
7428fb27baSJustin T. Gibbs 	{"scsi",	DEVSTAT_TYPE_IF_SCSI,	DEVSTAT_MATCH_IF},
7528fb27baSJustin T. Gibbs 	{"ide",		DEVSTAT_TYPE_IF_IDE,	DEVSTAT_MATCH_IF},
7628fb27baSJustin T. Gibbs 	{"other",	DEVSTAT_TYPE_IF_OTHER,	DEVSTAT_MATCH_IF},
77*d282baddSAlexander Motin 	{"nvme",	DEVSTAT_TYPE_IF_NVME,	DEVSTAT_MATCH_IF},
7828fb27baSJustin T. Gibbs 	{"worm",	DEVSTAT_TYPE_WORM,	DEVSTAT_MATCH_TYPE},
7928fb27baSJustin T. Gibbs 	{"sa",		DEVSTAT_TYPE_SEQUENTIAL,DEVSTAT_MATCH_TYPE},
8028fb27baSJustin T. Gibbs 	{"pass",	DEVSTAT_TYPE_PASS,	DEVSTAT_MATCH_PASS},
8128fb27baSJustin T. Gibbs 	{"optical",	DEVSTAT_TYPE_OPTICAL,	DEVSTAT_MATCH_TYPE},
8228fb27baSJustin T. Gibbs 	{"array",	DEVSTAT_TYPE_STORARRAY,	DEVSTAT_MATCH_TYPE},
8328fb27baSJustin T. Gibbs 	{"changer",	DEVSTAT_TYPE_CHANGER,	DEVSTAT_MATCH_TYPE},
8428fb27baSJustin T. Gibbs 	{"scanner",	DEVSTAT_TYPE_SCANNER,	DEVSTAT_MATCH_TYPE},
8528fb27baSJustin T. Gibbs 	{"printer",	DEVSTAT_TYPE_PRINTER,	DEVSTAT_MATCH_TYPE},
8628fb27baSJustin T. Gibbs 	{"floppy",	DEVSTAT_TYPE_FLOPPY,	DEVSTAT_MATCH_TYPE},
8728fb27baSJustin T. Gibbs 	{"proc",	DEVSTAT_TYPE_PROCESSOR,	DEVSTAT_MATCH_TYPE},
8828fb27baSJustin T. Gibbs 	{"comm",	DEVSTAT_TYPE_COMM,	DEVSTAT_MATCH_TYPE},
8928fb27baSJustin T. Gibbs 	{"enclosure",	DEVSTAT_TYPE_ENCLOSURE,	DEVSTAT_MATCH_TYPE},
9028fb27baSJustin T. Gibbs 	{NULL,		0,			0}
9128fb27baSJustin T. Gibbs };
9228fb27baSJustin T. Gibbs 
93c4a5ef6eSThomas Moestl struct devstat_args {
94c4a5ef6eSThomas Moestl 	devstat_metric 		metric;
95c4a5ef6eSThomas Moestl 	devstat_arg_type	argtype;
96c4a5ef6eSThomas Moestl } devstat_arg_list[] = {
97c4a5ef6eSThomas Moestl 	{ DSM_NONE, DEVSTAT_ARG_NOTYPE },
98c4a5ef6eSThomas Moestl 	{ DSM_TOTAL_BYTES, DEVSTAT_ARG_UINT64 },
99c4a5ef6eSThomas Moestl 	{ DSM_TOTAL_BYTES_READ, DEVSTAT_ARG_UINT64 },
100c4a5ef6eSThomas Moestl 	{ DSM_TOTAL_BYTES_WRITE, DEVSTAT_ARG_UINT64 },
101c4a5ef6eSThomas Moestl 	{ DSM_TOTAL_TRANSFERS, DEVSTAT_ARG_UINT64 },
102c4a5ef6eSThomas Moestl 	{ DSM_TOTAL_TRANSFERS_READ, DEVSTAT_ARG_UINT64 },
103c4a5ef6eSThomas Moestl 	{ DSM_TOTAL_TRANSFERS_WRITE, DEVSTAT_ARG_UINT64 },
104773865deSPoul-Henning Kamp 	{ DSM_TOTAL_TRANSFERS_OTHER, DEVSTAT_ARG_UINT64 },
105c4a5ef6eSThomas Moestl 	{ DSM_TOTAL_BLOCKS, DEVSTAT_ARG_UINT64 },
106c4a5ef6eSThomas Moestl 	{ DSM_TOTAL_BLOCKS_READ, DEVSTAT_ARG_UINT64 },
107c4a5ef6eSThomas Moestl 	{ DSM_TOTAL_BLOCKS_WRITE, DEVSTAT_ARG_UINT64 },
108c4a5ef6eSThomas Moestl 	{ DSM_KB_PER_TRANSFER, DEVSTAT_ARG_LD },
109c4a5ef6eSThomas Moestl 	{ DSM_KB_PER_TRANSFER_READ, DEVSTAT_ARG_LD },
110c4a5ef6eSThomas Moestl 	{ DSM_KB_PER_TRANSFER_WRITE, DEVSTAT_ARG_LD },
111c4a5ef6eSThomas Moestl 	{ DSM_TRANSFERS_PER_SECOND, DEVSTAT_ARG_LD },
112c4a5ef6eSThomas Moestl 	{ DSM_TRANSFERS_PER_SECOND_READ, DEVSTAT_ARG_LD },
113c4a5ef6eSThomas Moestl 	{ DSM_TRANSFERS_PER_SECOND_WRITE, DEVSTAT_ARG_LD },
114c4a5ef6eSThomas Moestl 	{ DSM_TRANSFERS_PER_SECOND_OTHER, DEVSTAT_ARG_LD },
115c4a5ef6eSThomas Moestl 	{ DSM_MB_PER_SECOND, DEVSTAT_ARG_LD },
116c4a5ef6eSThomas Moestl 	{ DSM_MB_PER_SECOND_READ, DEVSTAT_ARG_LD },
117c4a5ef6eSThomas Moestl 	{ DSM_MB_PER_SECOND_WRITE, DEVSTAT_ARG_LD },
118c4a5ef6eSThomas Moestl 	{ DSM_BLOCKS_PER_SECOND, DEVSTAT_ARG_LD },
119c4a5ef6eSThomas Moestl 	{ DSM_BLOCKS_PER_SECOND_READ, DEVSTAT_ARG_LD },
120c4a5ef6eSThomas Moestl 	{ DSM_BLOCKS_PER_SECOND_WRITE, DEVSTAT_ARG_LD },
121c4a5ef6eSThomas Moestl 	{ DSM_MS_PER_TRANSACTION, DEVSTAT_ARG_LD },
122c4a5ef6eSThomas Moestl 	{ DSM_MS_PER_TRANSACTION_READ, DEVSTAT_ARG_LD },
123884539f7SKenneth D. Merry 	{ DSM_MS_PER_TRANSACTION_WRITE, DEVSTAT_ARG_LD },
124ec0fa09cSPoul-Henning Kamp 	{ DSM_SKIP, DEVSTAT_ARG_SKIP },
125773865deSPoul-Henning Kamp 	{ DSM_TOTAL_BYTES_FREE, DEVSTAT_ARG_UINT64 },
126773865deSPoul-Henning Kamp 	{ DSM_TOTAL_TRANSFERS_FREE, DEVSTAT_ARG_UINT64 },
127773865deSPoul-Henning Kamp 	{ DSM_TOTAL_BLOCKS_FREE, DEVSTAT_ARG_UINT64 },
128773865deSPoul-Henning Kamp 	{ DSM_KB_PER_TRANSFER_FREE, DEVSTAT_ARG_LD },
129773865deSPoul-Henning Kamp 	{ DSM_MB_PER_SECOND_FREE, DEVSTAT_ARG_LD },
130773865deSPoul-Henning Kamp 	{ DSM_TRANSFERS_PER_SECOND_FREE, DEVSTAT_ARG_LD },
131773865deSPoul-Henning Kamp 	{ DSM_BLOCKS_PER_SECOND_FREE, DEVSTAT_ARG_LD },
13236eab1f5SPoul-Henning Kamp 	{ DSM_MS_PER_TRANSACTION_OTHER, DEVSTAT_ARG_LD },
133773865deSPoul-Henning Kamp 	{ DSM_MS_PER_TRANSACTION_FREE, DEVSTAT_ARG_LD },
13436eab1f5SPoul-Henning Kamp 	{ DSM_BUSY_PCT, DEVSTAT_ARG_LD },
13536eab1f5SPoul-Henning Kamp 	{ DSM_QUEUE_LENGTH, DEVSTAT_ARG_UINT64 },
136fdd6757eSMikolaj Golub 	{ DSM_TOTAL_DURATION, DEVSTAT_ARG_LD },
137fdd6757eSMikolaj Golub 	{ DSM_TOTAL_DURATION_READ, DEVSTAT_ARG_LD },
138fdd6757eSMikolaj Golub 	{ DSM_TOTAL_DURATION_WRITE, DEVSTAT_ARG_LD },
139fdd6757eSMikolaj Golub 	{ DSM_TOTAL_DURATION_FREE, DEVSTAT_ARG_LD },
140fdd6757eSMikolaj Golub 	{ DSM_TOTAL_DURATION_OTHER, DEVSTAT_ARG_LD },
141fdd6757eSMikolaj Golub 	{ DSM_TOTAL_BUSY_TIME, DEVSTAT_ARG_LD },
142c4a5ef6eSThomas Moestl };
143c4a5ef6eSThomas Moestl 
144c3508206SKenneth D. Merry static const char *namelist[] = {
145c4a5ef6eSThomas Moestl #define X_NUMDEVS	0
146c4a5ef6eSThomas Moestl 	"_devstat_num_devs",
147c4a5ef6eSThomas Moestl #define X_GENERATION	1
148c4a5ef6eSThomas Moestl 	"_devstat_generation",
149c4a5ef6eSThomas Moestl #define X_VERSION	2
150c4a5ef6eSThomas Moestl 	"_devstat_version",
151c4a5ef6eSThomas Moestl #define X_DEVICE_STATQ	3
152c4a5ef6eSThomas Moestl 	"_device_statq",
153ef6b3fcfSSergey Kandaurov #define X_TIME_UPTIME	4
154ef6b3fcfSSergey Kandaurov 	"_time_uptime",
155ef6b3fcfSSergey Kandaurov #define X_END		5
156c4a5ef6eSThomas Moestl };
157c4a5ef6eSThomas Moestl 
15828fb27baSJustin T. Gibbs /*
15928fb27baSJustin T. Gibbs  * Local function declarations.
16028fb27baSJustin T. Gibbs  */
16128fb27baSJustin T. Gibbs static int compare_select(const void *arg1, const void *arg2);
162c4a5ef6eSThomas Moestl static int readkmem(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes);
163c3508206SKenneth D. Merry static int readkmem_nl(kvm_t *kd, const char *name, void *buf, size_t nbytes);
164c4a5ef6eSThomas Moestl static char *get_devstat_kvm(kvm_t *kd);
165c4a5ef6eSThomas Moestl 
166c4a5ef6eSThomas Moestl #define KREADNL(kd, var, val) \
167c4a5ef6eSThomas Moestl 	readkmem_nl(kd, namelist[var], &val, sizeof(val))
16828fb27baSJustin T. Gibbs 
16928fb27baSJustin T. Gibbs int
devstat_getnumdevs(kvm_t * kd)170c4a5ef6eSThomas Moestl devstat_getnumdevs(kvm_t *kd)
17128fb27baSJustin T. Gibbs {
17228fb27baSJustin T. Gibbs 	size_t numdevsize;
17328fb27baSJustin T. Gibbs 	int numdevs;
17428fb27baSJustin T. Gibbs 
17528fb27baSJustin T. Gibbs 	numdevsize = sizeof(int);
17628fb27baSJustin T. Gibbs 
17728fb27baSJustin T. Gibbs 	/*
17828fb27baSJustin T. Gibbs 	 * Find out how many devices we have in the system.
17928fb27baSJustin T. Gibbs 	 */
180c4a5ef6eSThomas Moestl 	if (kd == NULL) {
18128fb27baSJustin T. Gibbs 		if (sysctlbyname("kern.devstat.numdevs", &numdevs,
18228fb27baSJustin T. Gibbs 				 &numdevsize, NULL, 0) == -1) {
183c4a5ef6eSThomas Moestl 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
184c4a5ef6eSThomas Moestl 				 "%s: error getting number of devices\n"
1857d72cda3SMaxime Henrion 				 "%s: %s", __func__, __func__,
186c4a5ef6eSThomas Moestl 				 strerror(errno));
18728fb27baSJustin T. Gibbs 			return(-1);
18828fb27baSJustin T. Gibbs 		} else
18928fb27baSJustin T. Gibbs 			return(numdevs);
190c4a5ef6eSThomas Moestl 	} else {
191c3508206SKenneth D. Merry 
192c4a5ef6eSThomas Moestl 		if (KREADNL(kd, X_NUMDEVS, numdevs) == -1)
193c4a5ef6eSThomas Moestl 			return(-1);
194c4a5ef6eSThomas Moestl 		else
195c4a5ef6eSThomas Moestl 			return(numdevs);
196c4a5ef6eSThomas Moestl 	}
19728fb27baSJustin T. Gibbs }
19828fb27baSJustin T. Gibbs 
19928fb27baSJustin T. Gibbs /*
20028fb27baSJustin T. Gibbs  * This is an easy way to get the generation number, but the generation is
20128fb27baSJustin T. Gibbs  * supplied in a more atmoic manner by the kern.devstat.all sysctl.
20228fb27baSJustin T. Gibbs  * Because this generation sysctl is separate from the statistics sysctl,
20328fb27baSJustin T. Gibbs  * the device list and the generation could change between the time that
2046a4d9095SSergey Kandaurov  * this function is called and the device list is retrieved.
20528fb27baSJustin T. Gibbs  */
206bcc6a3daSKenneth D. Merry long
devstat_getgeneration(kvm_t * kd)207c4a5ef6eSThomas Moestl devstat_getgeneration(kvm_t *kd)
20828fb27baSJustin T. Gibbs {
20928fb27baSJustin T. Gibbs 	size_t gensize;
210bcc6a3daSKenneth D. Merry 	long generation;
21128fb27baSJustin T. Gibbs 
212bcc6a3daSKenneth D. Merry 	gensize = sizeof(long);
21328fb27baSJustin T. Gibbs 
21428fb27baSJustin T. Gibbs 	/*
21528fb27baSJustin T. Gibbs 	 * Get the current generation number.
21628fb27baSJustin T. Gibbs 	 */
217c4a5ef6eSThomas Moestl 	if (kd == NULL) {
21828fb27baSJustin T. Gibbs 		if (sysctlbyname("kern.devstat.generation", &generation,
21928fb27baSJustin T. Gibbs 				 &gensize, NULL, 0) == -1) {
220c4a5ef6eSThomas Moestl 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
221c4a5ef6eSThomas Moestl 				 "%s: error getting devstat generation\n%s: %s",
2227d72cda3SMaxime Henrion 				 __func__, __func__, strerror(errno));
22328fb27baSJustin T. Gibbs 			return(-1);
22428fb27baSJustin T. Gibbs 		} else
22528fb27baSJustin T. Gibbs 			return(generation);
226c4a5ef6eSThomas Moestl 	} else {
227c4a5ef6eSThomas Moestl 		if (KREADNL(kd, X_GENERATION, generation) == -1)
228c4a5ef6eSThomas Moestl 			return(-1);
229c4a5ef6eSThomas Moestl 		else
230c4a5ef6eSThomas Moestl 			return(generation);
231c4a5ef6eSThomas Moestl 	}
23228fb27baSJustin T. Gibbs }
23328fb27baSJustin T. Gibbs 
23428fb27baSJustin T. Gibbs /*
23528fb27baSJustin T. Gibbs  * Get the current devstat version.  The return value of this function
23628fb27baSJustin T. Gibbs  * should be compared with DEVSTAT_VERSION, which is defined in
23728fb27baSJustin T. Gibbs  * sys/devicestat.h.  This will enable userland programs to determine
23828fb27baSJustin T. Gibbs  * whether they are out of sync with the kernel.
23928fb27baSJustin T. Gibbs  */
24028fb27baSJustin T. Gibbs int
devstat_getversion(kvm_t * kd)241c4a5ef6eSThomas Moestl devstat_getversion(kvm_t *kd)
24228fb27baSJustin T. Gibbs {
24328fb27baSJustin T. Gibbs 	size_t versize;
24428fb27baSJustin T. Gibbs 	int version;
24528fb27baSJustin T. Gibbs 
24628fb27baSJustin T. Gibbs 	versize = sizeof(int);
24728fb27baSJustin T. Gibbs 
24828fb27baSJustin T. Gibbs 	/*
24928fb27baSJustin T. Gibbs 	 * Get the current devstat version.
25028fb27baSJustin T. Gibbs 	 */
251c4a5ef6eSThomas Moestl 	if (kd == NULL) {
25228fb27baSJustin T. Gibbs 		if (sysctlbyname("kern.devstat.version", &version, &versize,
25328fb27baSJustin T. Gibbs 				 NULL, 0) == -1) {
254c4a5ef6eSThomas Moestl 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
255c4a5ef6eSThomas Moestl 				 "%s: error getting devstat version\n%s: %s",
2567d72cda3SMaxime Henrion 				 __func__, __func__, strerror(errno));
25728fb27baSJustin T. Gibbs 			return(-1);
25828fb27baSJustin T. Gibbs 		} else
25928fb27baSJustin T. Gibbs 			return(version);
260c4a5ef6eSThomas Moestl 	} else {
261c4a5ef6eSThomas Moestl 		if (KREADNL(kd, X_VERSION, version) == -1)
262c4a5ef6eSThomas Moestl 			return(-1);
263c4a5ef6eSThomas Moestl 		else
264c4a5ef6eSThomas Moestl 			return(version);
265c4a5ef6eSThomas Moestl 	}
26628fb27baSJustin T. Gibbs }
26728fb27baSJustin T. Gibbs 
26828fb27baSJustin T. Gibbs /*
26928fb27baSJustin T. Gibbs  * Check the devstat version we know about against the devstat version the
27028fb27baSJustin T. Gibbs  * kernel knows about.  If they don't match, print an error into the
27128fb27baSJustin T. Gibbs  * devstat error buffer, and return -1.  If they match, return 0.
27228fb27baSJustin T. Gibbs  */
27328fb27baSJustin T. Gibbs int
devstat_checkversion(kvm_t * kd)274c4a5ef6eSThomas Moestl devstat_checkversion(kvm_t *kd)
27528fb27baSJustin T. Gibbs {
2766d3e1426SBrian Somers 	int buflen, res, retval = 0, version;
27728fb27baSJustin T. Gibbs 
278c4a5ef6eSThomas Moestl 	version = devstat_getversion(kd);
279eded794aSKenneth D. Merry 
280eded794aSKenneth D. Merry 	if (version != DEVSTAT_VERSION) {
281eded794aSKenneth D. Merry 		/*
2826d3e1426SBrian Somers 		 * If getversion() returns an error (i.e. -1), then it
283eded794aSKenneth D. Merry 		 * has printed an error message in the buffer.  Therefore,
284eded794aSKenneth D. Merry 		 * we need to add a \n to the end of that message before we
285eded794aSKenneth D. Merry 		 * print our own message in the buffer.
286eded794aSKenneth D. Merry 		 */
2876d3e1426SBrian Somers 		if (version == -1)
288eded794aSKenneth D. Merry 			buflen = strlen(devstat_errbuf);
2896d3e1426SBrian Somers 		else
2906d3e1426SBrian Somers 			buflen = 0;
291eded794aSKenneth D. Merry 
2926d3e1426SBrian Somers 		res = snprintf(devstat_errbuf + buflen,
2936d3e1426SBrian Somers 			       DEVSTAT_ERRBUF_SIZE - buflen,
2946d3e1426SBrian Somers 			       "%s%s: userland devstat version %d is not "
2954fb9d384SKenneth D. Merry 			       "the same as the kernel\n%s: devstat "
2964fb9d384SKenneth D. Merry 			       "version %d\n", version == -1 ? "\n" : "",
2977d72cda3SMaxime Henrion 			       __func__, DEVSTAT_VERSION, __func__, version);
29828fb27baSJustin T. Gibbs 
2996d3e1426SBrian Somers 		if (res < 0)
3006d3e1426SBrian Somers 			devstat_errbuf[buflen] = '\0';
301eded794aSKenneth D. Merry 
3026d3e1426SBrian Somers 		buflen = strlen(devstat_errbuf);
303eded794aSKenneth D. Merry 		if (version < DEVSTAT_VERSION)
3046d3e1426SBrian Somers 			res = snprintf(devstat_errbuf + buflen,
3056d3e1426SBrian Somers 				       DEVSTAT_ERRBUF_SIZE - buflen,
3064fb9d384SKenneth D. Merry 				       "%s: libdevstat newer than kernel\n",
3077d72cda3SMaxime Henrion 				       __func__);
30828fb27baSJustin T. Gibbs 		else
3096d3e1426SBrian Somers 			res = snprintf(devstat_errbuf + buflen,
3106d3e1426SBrian Somers 				       DEVSTAT_ERRBUF_SIZE - buflen,
3114fb9d384SKenneth D. Merry 				       "%s: kernel newer than libdevstat\n",
3127d72cda3SMaxime Henrion 				       __func__);
31328fb27baSJustin T. Gibbs 
3146d3e1426SBrian Somers 		if (res < 0)
3156d3e1426SBrian Somers 			devstat_errbuf[buflen] = '\0';
31628fb27baSJustin T. Gibbs 
31728fb27baSJustin T. Gibbs 		retval = -1;
31828fb27baSJustin T. Gibbs 	}
31928fb27baSJustin T. Gibbs 
32028fb27baSJustin T. Gibbs 	return(retval);
32128fb27baSJustin T. Gibbs }
32228fb27baSJustin T. Gibbs 
32328fb27baSJustin T. Gibbs /*
32428fb27baSJustin T. Gibbs  * Get the current list of devices and statistics, and the current
32528fb27baSJustin T. Gibbs  * generation number.
32628fb27baSJustin T. Gibbs  *
32728fb27baSJustin T. Gibbs  * Return values:
32828fb27baSJustin T. Gibbs  * -1  -- error
32928fb27baSJustin T. Gibbs  *  0  -- device list is unchanged
33028fb27baSJustin T. Gibbs  *  1  -- device list has changed
33128fb27baSJustin T. Gibbs  */
33228fb27baSJustin T. Gibbs int
devstat_getdevs(kvm_t * kd,struct statinfo * stats)333c4a5ef6eSThomas Moestl devstat_getdevs(kvm_t *kd, struct statinfo *stats)
33428fb27baSJustin T. Gibbs {
33528fb27baSJustin T. Gibbs 	int error;
33628fb27baSJustin T. Gibbs 	size_t dssize;
337bcc6a3daSKenneth D. Merry 	long oldgeneration;
33828fb27baSJustin T. Gibbs 	int retval = 0;
33928fb27baSJustin T. Gibbs 	struct devinfo *dinfo;
3407194d335SPoul-Henning Kamp 	struct timespec ts;
34128fb27baSJustin T. Gibbs 
34228fb27baSJustin T. Gibbs 	dinfo = stats->dinfo;
34328fb27baSJustin T. Gibbs 
34428fb27baSJustin T. Gibbs 	if (dinfo == NULL) {
345c4a5ef6eSThomas Moestl 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
3467d72cda3SMaxime Henrion 			 "%s: stats->dinfo was NULL", __func__);
34728fb27baSJustin T. Gibbs 		return(-1);
34828fb27baSJustin T. Gibbs 	}
34928fb27baSJustin T. Gibbs 
35028fb27baSJustin T. Gibbs 	oldgeneration = dinfo->generation;
35128fb27baSJustin T. Gibbs 
352ef6b3fcfSSergey Kandaurov 	if (kd == NULL) {
3537194d335SPoul-Henning Kamp 		clock_gettime(CLOCK_MONOTONIC, &ts);
3547194d335SPoul-Henning Kamp 		stats->snap_time = ts.tv_sec + ts.tv_nsec * 1e-9;
355c4a5ef6eSThomas Moestl 
356c4a5ef6eSThomas Moestl 		/* If this is our first time through, mem_ptr will be null. */
35728fb27baSJustin T. Gibbs 		if (dinfo->mem_ptr == NULL) {
35828fb27baSJustin T. Gibbs 			/*
35928fb27baSJustin T. Gibbs 			 * Get the number of devices.  If it's negative, it's an
36028fb27baSJustin T. Gibbs 			 * error.  Don't bother setting the error string, since
36128fb27baSJustin T. Gibbs 			 * getnumdevs() has already done that for us.
36228fb27baSJustin T. Gibbs 			 */
36316830b0cSPoul-Henning Kamp 			if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0)
36428fb27baSJustin T. Gibbs 				return(-1);
36528fb27baSJustin T. Gibbs 
36628fb27baSJustin T. Gibbs 			/*
367c4a5ef6eSThomas Moestl 			 * The kern.devstat.all sysctl returns the current
368c4a5ef6eSThomas Moestl 			 * generation number, as well as all the devices.
369c4a5ef6eSThomas Moestl 			 * So we need four bytes more.
37028fb27baSJustin T. Gibbs 			 */
371c4a5ef6eSThomas Moestl 			dssize = (dinfo->numdevs * sizeof(struct devstat)) +
372c4a5ef6eSThomas Moestl 				 sizeof(long);
37328fb27baSJustin T. Gibbs 			dinfo->mem_ptr = (u_int8_t *)malloc(dssize);
374a18e4616SGuy Helmer 			if (dinfo->mem_ptr == NULL) {
375a18e4616SGuy Helmer 				snprintf(devstat_errbuf, sizeof(devstat_errbuf),
376a18e4616SGuy Helmer 					 "%s: Cannot allocate memory for mem_ptr element",
377a18e4616SGuy Helmer 					 __func__);
378a18e4616SGuy Helmer 				return(-1);
379a18e4616SGuy Helmer 			}
38028fb27baSJustin T. Gibbs 		} else
381c4a5ef6eSThomas Moestl 			dssize = (dinfo->numdevs * sizeof(struct devstat)) +
382c4a5ef6eSThomas Moestl 				 sizeof(long);
38328fb27baSJustin T. Gibbs 
38428fb27baSJustin T. Gibbs 		/*
38528fb27baSJustin T. Gibbs 		 * Request all of the devices.  We only really allow for one
386c4a5ef6eSThomas Moestl 		 * ENOMEM failure.  It would, of course, be possible to just go
387c4a5ef6eSThomas Moestl 		 * in a loop and keep reallocing the device structure until we
388c4a5ef6eSThomas Moestl 		 * don't get ENOMEM back.  I'm not sure it's worth it, though.
389c4a5ef6eSThomas Moestl 		 * If devices are being added to the system that quickly, maybe
390c4a5ef6eSThomas Moestl 		 * the user can just wait until all devices are added.
39128fb27baSJustin T. Gibbs 		 */
39236eab1f5SPoul-Henning Kamp 		for (;;) {
39336eab1f5SPoul-Henning Kamp 			error = sysctlbyname("kern.devstat.all",
39436eab1f5SPoul-Henning Kamp 					     dinfo->mem_ptr,
39536eab1f5SPoul-Henning Kamp 					     &dssize, NULL, 0);
39636eab1f5SPoul-Henning Kamp 			if (error != -1 || errno != EBUSY)
39736eab1f5SPoul-Henning Kamp 				break;
39836eab1f5SPoul-Henning Kamp 		}
39936eab1f5SPoul-Henning Kamp 		if (error == -1) {
40028fb27baSJustin T. Gibbs 			/*
40128fb27baSJustin T. Gibbs 			 * If we get ENOMEM back, that means that there are
40228fb27baSJustin T. Gibbs 			 * more devices now, so we need to allocate more
40328fb27baSJustin T. Gibbs 			 * space for the device array.
40428fb27baSJustin T. Gibbs 			 */
40528fb27baSJustin T. Gibbs 			if (errno == ENOMEM) {
40628fb27baSJustin T. Gibbs 				/*
407c4a5ef6eSThomas Moestl 				 * No need to set the error string here,
4087194d335SPoul-Henning Kamp 				 * devstat_getnumdevs() will do that if it fails.
40928fb27baSJustin T. Gibbs 				 */
41016830b0cSPoul-Henning Kamp 				if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0)
41128fb27baSJustin T. Gibbs 					return(-1);
41228fb27baSJustin T. Gibbs 
413c4a5ef6eSThomas Moestl 				dssize = (dinfo->numdevs *
414c4a5ef6eSThomas Moestl 					sizeof(struct devstat)) + sizeof(long);
415c4a5ef6eSThomas Moestl 				dinfo->mem_ptr = (u_int8_t *)
416c4a5ef6eSThomas Moestl 					realloc(dinfo->mem_ptr, dssize);
41728fb27baSJustin T. Gibbs 				if ((error = sysctlbyname("kern.devstat.all",
41828fb27baSJustin T. Gibbs 				    dinfo->mem_ptr, &dssize, NULL, 0)) == -1) {
419c4a5ef6eSThomas Moestl 					snprintf(devstat_errbuf,
420c4a5ef6eSThomas Moestl 						 sizeof(devstat_errbuf),
421c4a5ef6eSThomas Moestl 					    	 "%s: error getting device "
4227d72cda3SMaxime Henrion 					    	 "stats\n%s: %s", __func__,
4237d72cda3SMaxime Henrion 					    	 __func__, strerror(errno));
42428fb27baSJustin T. Gibbs 					return(-1);
42528fb27baSJustin T. Gibbs 				}
42628fb27baSJustin T. Gibbs 			} else {
427c4a5ef6eSThomas Moestl 				snprintf(devstat_errbuf, sizeof(devstat_errbuf),
42828fb27baSJustin T. Gibbs 					 "%s: error getting device stats\n"
4297d72cda3SMaxime Henrion 					 "%s: %s", __func__, __func__,
43028fb27baSJustin T. Gibbs 					 strerror(errno));
43128fb27baSJustin T. Gibbs 				return(-1);
43228fb27baSJustin T. Gibbs 			}
43328fb27baSJustin T. Gibbs 		}
43428fb27baSJustin T. Gibbs 
435c4a5ef6eSThomas Moestl 	} else {
436ef6b3fcfSSergey Kandaurov 		if (KREADNL(kd, X_TIME_UPTIME, ts.tv_sec) == -1)
437ef6b3fcfSSergey Kandaurov 			return(-1);
438ef6b3fcfSSergey Kandaurov 		else
439ef6b3fcfSSergey Kandaurov 			stats->snap_time = ts.tv_sec;
440ef6b3fcfSSergey Kandaurov 
441c4a5ef6eSThomas Moestl 		/*
442c4a5ef6eSThomas Moestl 		 * This is of course non-atomic, but since we are working
443c4a5ef6eSThomas Moestl 		 * on a core dump, the generation is unlikely to change
444c4a5ef6eSThomas Moestl 		 */
44516830b0cSPoul-Henning Kamp 		if ((dinfo->numdevs = devstat_getnumdevs(kd)) == -1)
446c4a5ef6eSThomas Moestl 			return(-1);
447140490e2SMaxime Henrion 		if ((dinfo->mem_ptr = (u_int8_t *)get_devstat_kvm(kd)) == NULL)
448c4a5ef6eSThomas Moestl 			return(-1);
449c4a5ef6eSThomas Moestl 	}
45028fb27baSJustin T. Gibbs 	/*
45128fb27baSJustin T. Gibbs 	 * The sysctl spits out the generation as the first four bytes,
45228fb27baSJustin T. Gibbs 	 * then all of the device statistics structures.
45328fb27baSJustin T. Gibbs 	 */
454bcc6a3daSKenneth D. Merry 	dinfo->generation = *(long *)dinfo->mem_ptr;
45528fb27baSJustin T. Gibbs 
45628fb27baSJustin T. Gibbs 	/*
45728fb27baSJustin T. Gibbs 	 * If the generation has changed, and if the current number of
45828fb27baSJustin T. Gibbs 	 * devices is not the same as the number of devices recorded in the
45928fb27baSJustin T. Gibbs 	 * devinfo structure, it is likely that the device list has shrunk.
46028fb27baSJustin T. Gibbs 	 * The reason that it is likely that the device list has shrunk in
46128fb27baSJustin T. Gibbs 	 * this case is that if the device list has grown, the sysctl above
46228fb27baSJustin T. Gibbs 	 * will return an ENOMEM error, and we will reset the number of
46328fb27baSJustin T. Gibbs 	 * devices and reallocate the device array.  If the second sysctl
46428fb27baSJustin T. Gibbs 	 * fails, we will return an error and therefore never get to this
46528fb27baSJustin T. Gibbs 	 * point.  If the device list has shrunk, the sysctl will not
46628fb27baSJustin T. Gibbs 	 * return an error since we have more space allocated than is
46728fb27baSJustin T. Gibbs 	 * necessary.  So, in the shrinkage case, we catch it here and
46828fb27baSJustin T. Gibbs 	 * reallocate the array so that we don't use any more space than is
46928fb27baSJustin T. Gibbs 	 * necessary.
47028fb27baSJustin T. Gibbs 	 */
47128fb27baSJustin T. Gibbs 	if (oldgeneration != dinfo->generation) {
47216830b0cSPoul-Henning Kamp 		if (devstat_getnumdevs(kd) != dinfo->numdevs) {
47316830b0cSPoul-Henning Kamp 			if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0)
47428fb27baSJustin T. Gibbs 				return(-1);
47528fb27baSJustin T. Gibbs 			dssize = (dinfo->numdevs * sizeof(struct devstat)) +
476bcc6a3daSKenneth D. Merry 				sizeof(long);
47728fb27baSJustin T. Gibbs 			dinfo->mem_ptr = (u_int8_t *)realloc(dinfo->mem_ptr,
47828fb27baSJustin T. Gibbs 							     dssize);
47928fb27baSJustin T. Gibbs 		}
48028fb27baSJustin T. Gibbs 		retval = 1;
48128fb27baSJustin T. Gibbs 	}
48228fb27baSJustin T. Gibbs 
483bcc6a3daSKenneth D. Merry 	dinfo->devices = (struct devstat *)(dinfo->mem_ptr + sizeof(long));
48428fb27baSJustin T. Gibbs 
48528fb27baSJustin T. Gibbs 	return(retval);
48628fb27baSJustin T. Gibbs }
48728fb27baSJustin T. Gibbs 
48828fb27baSJustin T. Gibbs /*
48928fb27baSJustin T. Gibbs  * selectdevs():
49028fb27baSJustin T. Gibbs  *
49128fb27baSJustin T. Gibbs  * Devices are selected/deselected based upon the following criteria:
49228fb27baSJustin T. Gibbs  * - devices specified by the user on the command line
49328fb27baSJustin T. Gibbs  * - devices matching any device type expressions given on the command line
49428fb27baSJustin T. Gibbs  * - devices with the highest I/O, if 'top' mode is enabled
49528fb27baSJustin T. Gibbs  * - the first n unselected devices in the device list, if maxshowdevs
49628fb27baSJustin T. Gibbs  *   devices haven't already been selected and if the user has not
49728fb27baSJustin T. Gibbs  *   specified any devices on the command line and if we're in "add" mode.
49828fb27baSJustin T. Gibbs  *
49928fb27baSJustin T. Gibbs  * Input parameters:
50028fb27baSJustin T. Gibbs  * - device selection list (dev_select)
50128fb27baSJustin T. Gibbs  * - current number of devices selected (num_selected)
50228fb27baSJustin T. Gibbs  * - total number of devices in the selection list (num_selections)
50328fb27baSJustin T. Gibbs  * - devstat generation as of the last time selectdevs() was called
50428fb27baSJustin T. Gibbs  *   (select_generation)
50528fb27baSJustin T. Gibbs  * - current devstat generation (current_generation)
50628fb27baSJustin T. Gibbs  * - current list of devices and statistics (devices)
50728fb27baSJustin T. Gibbs  * - number of devices in the current device list (numdevs)
50828fb27baSJustin T. Gibbs  * - compiled version of the command line device type arguments (matches)
50928fb27baSJustin T. Gibbs  *   - This is optional.  If the number of devices is 0, this will be ignored.
51028fb27baSJustin T. Gibbs  *   - The matching code pays attention to the current selection mode.  So
51128fb27baSJustin T. Gibbs  *     if you pass in a matching expression, it will be evaluated based
51228fb27baSJustin T. Gibbs  *     upon the selection mode that is passed in.  See below for details.
51328fb27baSJustin T. Gibbs  * - number of device type matching expressions (num_matches)
51428fb27baSJustin T. Gibbs  *   - Set to 0 to disable the matching code.
51528fb27baSJustin T. Gibbs  * - list of devices specified on the command line by the user (dev_selections)
51628fb27baSJustin T. Gibbs  * - number of devices selected on the command line by the user
51728fb27baSJustin T. Gibbs  *   (num_dev_selections)
51828fb27baSJustin T. Gibbs  * - Our selection mode.  There are four different selection modes:
51928fb27baSJustin T. Gibbs  *      - add mode.  (DS_SELECT_ADD) Any devices matching devices explicitly
52028fb27baSJustin T. Gibbs  *        selected by the user or devices matching a pattern given by the
52128fb27baSJustin T. Gibbs  *        user will be selected in addition to devices that are already
52228fb27baSJustin T. Gibbs  *        selected.  Additional devices will be selected, up to maxshowdevs
52328fb27baSJustin T. Gibbs  *        number of devices.
52428fb27baSJustin T. Gibbs  *      - only mode. (DS_SELECT_ONLY)  Only devices matching devices
52528fb27baSJustin T. Gibbs  *        explicitly given by the user or devices matching a pattern
52628fb27baSJustin T. Gibbs  *        given by the user will be selected.  No other devices will be
52728fb27baSJustin T. Gibbs  *        selected.
52828fb27baSJustin T. Gibbs  *      - addonly mode.  (DS_SELECT_ADDONLY)  This is similar to add and
52928fb27baSJustin T. Gibbs  *        only.  Basically, this will not de-select any devices that are
53028fb27baSJustin T. Gibbs  *        current selected, as only mode would, but it will also not
53128fb27baSJustin T. Gibbs  *        gratuitously select up to maxshowdevs devices as add mode would.
53228fb27baSJustin T. Gibbs  *      - remove mode.  (DS_SELECT_REMOVE)  Any devices matching devices
53328fb27baSJustin T. Gibbs  *        explicitly selected by the user or devices matching a pattern
53428fb27baSJustin T. Gibbs  *        given by the user will be de-selected.
53528fb27baSJustin T. Gibbs  * - maximum number of devices we can select (maxshowdevs)
53628fb27baSJustin T. Gibbs  * - flag indicating whether or not we're in 'top' mode (perf_select)
53728fb27baSJustin T. Gibbs  *
53828fb27baSJustin T. Gibbs  * Output data:
53928fb27baSJustin T. Gibbs  * - the device selection list may be modified and passed back out
54028fb27baSJustin T. Gibbs  * - the number of devices selected and the total number of items in the
54128fb27baSJustin T. Gibbs  *   device selection list may be changed
54228fb27baSJustin T. Gibbs  * - the selection generation may be changed to match the current generation
54328fb27baSJustin T. Gibbs  *
54428fb27baSJustin T. Gibbs  * Return values:
54528fb27baSJustin T. Gibbs  * -1  -- error
54628fb27baSJustin T. Gibbs  *  0  -- selected devices are unchanged
54728fb27baSJustin T. Gibbs  *  1  -- selected devices changed
54828fb27baSJustin T. Gibbs  */
54928fb27baSJustin T. Gibbs int
devstat_selectdevs(struct device_selection ** dev_select,int * num_selected,int * num_selections,long * select_generation,long current_generation,struct devstat * devices,int numdevs,struct devstat_match * matches,int num_matches,char ** dev_selections,int num_dev_selections,devstat_select_mode select_mode,int maxshowdevs,int perf_select)550c4a5ef6eSThomas Moestl devstat_selectdevs(struct device_selection **dev_select, int *num_selected,
551bcc6a3daSKenneth D. Merry 		   int *num_selections, long *select_generation,
552c4a5ef6eSThomas Moestl 		   long current_generation, struct devstat *devices,
553c4a5ef6eSThomas Moestl 		   int numdevs, struct devstat_match *matches, int num_matches,
55428fb27baSJustin T. Gibbs 		   char **dev_selections, int num_dev_selections,
555c4a5ef6eSThomas Moestl 		   devstat_select_mode select_mode, int maxshowdevs,
556c4a5ef6eSThomas Moestl 		   int perf_select)
55728fb27baSJustin T. Gibbs {
558be04b6d1SDavid E. O'Brien 	int i, j, k;
55928fb27baSJustin T. Gibbs 	int init_selections = 0, init_selected_var = 0;
56028fb27baSJustin T. Gibbs 	struct device_selection *old_dev_select = NULL;
56128fb27baSJustin T. Gibbs 	int old_num_selections = 0, old_num_selected;
56228fb27baSJustin T. Gibbs 	int selection_number = 0;
56328fb27baSJustin T. Gibbs 	int changed = 0, found = 0;
56428fb27baSJustin T. Gibbs 
565d2d0b144SPoul-Henning Kamp 	if ((dev_select == NULL) || (devices == NULL) || (numdevs < 0))
56628fb27baSJustin T. Gibbs 		return(-1);
56728fb27baSJustin T. Gibbs 
56828fb27baSJustin T. Gibbs 	/*
56928fb27baSJustin T. Gibbs 	 * We always want to make sure that we have as many dev_select
57028fb27baSJustin T. Gibbs 	 * entries as there are devices.
57128fb27baSJustin T. Gibbs 	 */
57228fb27baSJustin T. Gibbs 	/*
57328fb27baSJustin T. Gibbs 	 * In this case, we haven't selected devices before.
57428fb27baSJustin T. Gibbs 	 */
57528fb27baSJustin T. Gibbs 	if (*dev_select == NULL) {
57628fb27baSJustin T. Gibbs 		*dev_select = (struct device_selection *)malloc(numdevs *
57728fb27baSJustin T. Gibbs 			sizeof(struct device_selection));
57828fb27baSJustin T. Gibbs 		*select_generation = current_generation;
57928fb27baSJustin T. Gibbs 		init_selections = 1;
58028fb27baSJustin T. Gibbs 		changed = 1;
58128fb27baSJustin T. Gibbs 	/*
58228fb27baSJustin T. Gibbs 	 * In this case, we have selected devices before, but the device
58328fb27baSJustin T. Gibbs 	 * list has changed since we last selected devices, so we need to
58428fb27baSJustin T. Gibbs 	 * either enlarge or reduce the size of the device selection list.
58584a03ac6SAndriy Gapon 	 * But delay the resizing until after copying the data to old_dev_select
58684a03ac6SAndriy Gapon 	 * as to not lose any data in the case of reducing the size.
58728fb27baSJustin T. Gibbs 	 */
58828fb27baSJustin T. Gibbs 	} else if (*num_selections != numdevs) {
58928fb27baSJustin T. Gibbs 		*select_generation = current_generation;
59028fb27baSJustin T. Gibbs 		init_selections = 1;
59128fb27baSJustin T. Gibbs 	/*
59228fb27baSJustin T. Gibbs 	 * In this case, we've selected devices before, and the selection
59328fb27baSJustin T. Gibbs 	 * list is the same size as it was the last time, but the device
59428fb27baSJustin T. Gibbs 	 * list has changed.
59528fb27baSJustin T. Gibbs 	 */
59628fb27baSJustin T. Gibbs 	} else if (*select_generation < current_generation) {
59728fb27baSJustin T. Gibbs 		*select_generation = current_generation;
59828fb27baSJustin T. Gibbs 		init_selections = 1;
59928fb27baSJustin T. Gibbs 	}
60028fb27baSJustin T. Gibbs 
601a18e4616SGuy Helmer 	if (*dev_select == NULL) {
602a18e4616SGuy Helmer 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
603a18e4616SGuy Helmer 			 "%s: Cannot (re)allocate memory for dev_select argument",
604a18e4616SGuy Helmer 			 __func__);
605a18e4616SGuy Helmer 		return(-1);
606a18e4616SGuy Helmer 	}
607a18e4616SGuy Helmer 
60828fb27baSJustin T. Gibbs 	/*
60928fb27baSJustin T. Gibbs 	 * If we're in "only" mode, we want to clear out the selected
61028fb27baSJustin T. Gibbs 	 * variable since we're going to select exactly what the user wants
61128fb27baSJustin T. Gibbs 	 * this time through.
61228fb27baSJustin T. Gibbs 	 */
61328fb27baSJustin T. Gibbs 	if (select_mode == DS_SELECT_ONLY)
61428fb27baSJustin T. Gibbs 		init_selected_var = 1;
61528fb27baSJustin T. Gibbs 
61628fb27baSJustin T. Gibbs 	/*
61728fb27baSJustin T. Gibbs 	 * In all cases, we want to back up the number of selected devices.
61828fb27baSJustin T. Gibbs 	 * It is a quick and accurate way to determine whether the selected
61928fb27baSJustin T. Gibbs 	 * devices have changed.
62028fb27baSJustin T. Gibbs 	 */
62128fb27baSJustin T. Gibbs 	old_num_selected = *num_selected;
62228fb27baSJustin T. Gibbs 
62328fb27baSJustin T. Gibbs 	/*
62428fb27baSJustin T. Gibbs 	 * We want to make a backup of the current selection list if
62528fb27baSJustin T. Gibbs 	 * the list of devices has changed, or if we're in performance
62628fb27baSJustin T. Gibbs 	 * selection mode.  In both cases, we don't want to make a backup
62728fb27baSJustin T. Gibbs 	 * if we already know for sure that the list will be different.
62828fb27baSJustin T. Gibbs 	 * This is certainly the case if this is our first time through the
62928fb27baSJustin T. Gibbs 	 * selection code.
63028fb27baSJustin T. Gibbs 	 */
63128fb27baSJustin T. Gibbs 	if (((init_selected_var != 0) || (init_selections != 0)
63228fb27baSJustin T. Gibbs 	 || (perf_select != 0)) && (changed == 0)){
63328fb27baSJustin T. Gibbs 		old_dev_select = (struct device_selection *)malloc(
63428fb27baSJustin T. Gibbs 		    *num_selections * sizeof(struct device_selection));
635a18e4616SGuy Helmer 		if (old_dev_select == NULL) {
636a18e4616SGuy Helmer 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
637a18e4616SGuy Helmer 				 "%s: Cannot allocate memory for selection list backup",
638a18e4616SGuy Helmer 				 __func__);
639a18e4616SGuy Helmer 			return(-1);
640a18e4616SGuy Helmer 		}
64128fb27baSJustin T. Gibbs 		old_num_selections = *num_selections;
64228fb27baSJustin T. Gibbs 		bcopy(*dev_select, old_dev_select,
64328fb27baSJustin T. Gibbs 		    sizeof(struct device_selection) * *num_selections);
64428fb27baSJustin T. Gibbs 	}
64528fb27baSJustin T. Gibbs 
64684a03ac6SAndriy Gapon 	if (!changed && *num_selections != numdevs) {
64784a03ac6SAndriy Gapon 		*dev_select = (struct device_selection *)reallocf(*dev_select,
64884a03ac6SAndriy Gapon 			numdevs * sizeof(struct device_selection));
64984a03ac6SAndriy Gapon 	}
65084a03ac6SAndriy Gapon 
65128fb27baSJustin T. Gibbs 	if (init_selections != 0) {
65228fb27baSJustin T. Gibbs 		bzero(*dev_select, sizeof(struct device_selection) * numdevs);
65328fb27baSJustin T. Gibbs 
65428fb27baSJustin T. Gibbs 		for (i = 0; i < numdevs; i++) {
65528fb27baSJustin T. Gibbs 			(*dev_select)[i].device_number =
65628fb27baSJustin T. Gibbs 				devices[i].device_number;
65728fb27baSJustin T. Gibbs 			strncpy((*dev_select)[i].device_name,
65828fb27baSJustin T. Gibbs 				devices[i].device_name,
65928fb27baSJustin T. Gibbs 				DEVSTAT_NAME_LEN);
66017ee2b20SKenneth D. Merry 			(*dev_select)[i].device_name[DEVSTAT_NAME_LEN - 1]='\0';
66128fb27baSJustin T. Gibbs 			(*dev_select)[i].unit_number = devices[i].unit_number;
66228fb27baSJustin T. Gibbs 			(*dev_select)[i].position = i;
66328fb27baSJustin T. Gibbs 		}
66428fb27baSJustin T. Gibbs 		*num_selections = numdevs;
66528fb27baSJustin T. Gibbs 	} else if (init_selected_var != 0) {
66628fb27baSJustin T. Gibbs 		for (i = 0; i < numdevs; i++)
66728fb27baSJustin T. Gibbs 			(*dev_select)[i].selected = 0;
66828fb27baSJustin T. Gibbs 	}
66928fb27baSJustin T. Gibbs 
67028fb27baSJustin T. Gibbs 	/* we haven't gotten around to selecting anything yet.. */
67128fb27baSJustin T. Gibbs 	if ((select_mode == DS_SELECT_ONLY) || (init_selections != 0)
67228fb27baSJustin T. Gibbs 	 || (init_selected_var != 0))
67328fb27baSJustin T. Gibbs 		*num_selected = 0;
67428fb27baSJustin T. Gibbs 
67528fb27baSJustin T. Gibbs 	/*
67628fb27baSJustin T. Gibbs 	 * Look through any devices the user specified on the command line
67728fb27baSJustin T. Gibbs 	 * and see if they match known devices.  If so, select them.
67828fb27baSJustin T. Gibbs 	 */
67928fb27baSJustin T. Gibbs 	for (i = 0; (i < *num_selections) && (num_dev_selections > 0); i++) {
68028fb27baSJustin T. Gibbs 		char tmpstr[80];
68128fb27baSJustin T. Gibbs 
68217ee2b20SKenneth D. Merry 		snprintf(tmpstr, sizeof(tmpstr), "%s%d",
68317ee2b20SKenneth D. Merry 			 (*dev_select)[i].device_name,
68428fb27baSJustin T. Gibbs 			 (*dev_select)[i].unit_number);
68528fb27baSJustin T. Gibbs 		for (j = 0; j < num_dev_selections; j++) {
68628fb27baSJustin T. Gibbs 			if (strcmp(tmpstr, dev_selections[j]) == 0) {
68728fb27baSJustin T. Gibbs 				/*
68828fb27baSJustin T. Gibbs 				 * Here we do different things based on the
68928fb27baSJustin T. Gibbs 				 * mode we're in.  If we're in add or
69028fb27baSJustin T. Gibbs 				 * addonly mode, we only select this device
69128fb27baSJustin T. Gibbs 				 * if it hasn't already been selected.
69228fb27baSJustin T. Gibbs 				 * Otherwise, we would be unnecessarily
69328fb27baSJustin T. Gibbs 				 * changing the selection order and
69428fb27baSJustin T. Gibbs 				 * incrementing the selection count.  If
69528fb27baSJustin T. Gibbs 				 * we're in only mode, we unconditionally
69628fb27baSJustin T. Gibbs 				 * select this device, since in only mode
69728fb27baSJustin T. Gibbs 				 * any previous selections are erased and
69828fb27baSJustin T. Gibbs 				 * manually specified devices are the first
69928fb27baSJustin T. Gibbs 				 * ones to be selected.  If we're in remove
70028fb27baSJustin T. Gibbs 				 * mode, we de-select the specified device and
70128fb27baSJustin T. Gibbs 				 * decrement the selection count.
70228fb27baSJustin T. Gibbs 				 */
70328fb27baSJustin T. Gibbs 				switch(select_mode) {
70428fb27baSJustin T. Gibbs 				case DS_SELECT_ADD:
70528fb27baSJustin T. Gibbs 				case DS_SELECT_ADDONLY:
70628fb27baSJustin T. Gibbs 					if ((*dev_select)[i].selected)
70728fb27baSJustin T. Gibbs 						break;
70828fb27baSJustin T. Gibbs 					/* FALLTHROUGH */
70928fb27baSJustin T. Gibbs 				case DS_SELECT_ONLY:
71028fb27baSJustin T. Gibbs 					(*dev_select)[i].selected =
71128fb27baSJustin T. Gibbs 						++selection_number;
71228fb27baSJustin T. Gibbs 					(*num_selected)++;
71328fb27baSJustin T. Gibbs 					break;
71428fb27baSJustin T. Gibbs 				case DS_SELECT_REMOVE:
71528fb27baSJustin T. Gibbs 					(*dev_select)[i].selected = 0;
71628fb27baSJustin T. Gibbs 					(*num_selected)--;
71728fb27baSJustin T. Gibbs 					/*
71828fb27baSJustin T. Gibbs 					 * This isn't passed back out, we
71928fb27baSJustin T. Gibbs 					 * just use it to keep track of
72028fb27baSJustin T. Gibbs 					 * how many devices we've removed.
72128fb27baSJustin T. Gibbs 					 */
72228fb27baSJustin T. Gibbs 					num_dev_selections--;
72328fb27baSJustin T. Gibbs 					break;
72428fb27baSJustin T. Gibbs 				}
72528fb27baSJustin T. Gibbs 				break;
72628fb27baSJustin T. Gibbs 			}
72728fb27baSJustin T. Gibbs 		}
72828fb27baSJustin T. Gibbs 	}
72928fb27baSJustin T. Gibbs 
73028fb27baSJustin T. Gibbs 	/*
73128fb27baSJustin T. Gibbs 	 * Go through the user's device type expressions and select devices
73228fb27baSJustin T. Gibbs 	 * accordingly.  We only do this if the number of devices already
73328fb27baSJustin T. Gibbs 	 * selected is less than the maximum number we can show.
73428fb27baSJustin T. Gibbs 	 */
73528fb27baSJustin T. Gibbs 	for (i = 0; (i < num_matches) && (*num_selected < maxshowdevs); i++) {
73628fb27baSJustin T. Gibbs 		/* We should probably indicate some error here */
73728fb27baSJustin T. Gibbs 		if ((matches[i].match_fields == DEVSTAT_MATCH_NONE)
73828fb27baSJustin T. Gibbs 		 || (matches[i].num_match_categories <= 0))
73928fb27baSJustin T. Gibbs 			continue;
74028fb27baSJustin T. Gibbs 
74128fb27baSJustin T. Gibbs 		for (j = 0; j < numdevs; j++) {
74228fb27baSJustin T. Gibbs 			int num_match_categories;
74328fb27baSJustin T. Gibbs 
74428fb27baSJustin T. Gibbs 			num_match_categories = matches[i].num_match_categories;
74528fb27baSJustin T. Gibbs 
74628fb27baSJustin T. Gibbs 			/*
74728fb27baSJustin T. Gibbs 			 * Determine whether or not the current device
74828fb27baSJustin T. Gibbs 			 * matches the given matching expression.  This if
74928fb27baSJustin T. Gibbs 			 * statement consists of three components:
75028fb27baSJustin T. Gibbs 			 *   - the device type check
75128fb27baSJustin T. Gibbs 			 *   - the device interface check
75228fb27baSJustin T. Gibbs 			 *   - the passthrough check
75328fb27baSJustin T. Gibbs 			 * If a the matching test is successful, it
75428fb27baSJustin T. Gibbs 			 * decrements the number of matching categories,
75528fb27baSJustin T. Gibbs 			 * and if we've reached the last element that
75628fb27baSJustin T. Gibbs 			 * needed to be matched, the if statement succeeds.
75728fb27baSJustin T. Gibbs 			 *
75828fb27baSJustin T. Gibbs 			 */
75928fb27baSJustin T. Gibbs 			if ((((matches[i].match_fields & DEVSTAT_MATCH_TYPE)!=0)
76028fb27baSJustin T. Gibbs 			  && ((devices[j].device_type & DEVSTAT_TYPE_MASK) ==
76128fb27baSJustin T. Gibbs 			        (matches[i].device_type & DEVSTAT_TYPE_MASK))
76228fb27baSJustin T. Gibbs 			  &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0)
76328fb27baSJustin T. Gibbs 			   || (((matches[i].match_fields &
76428fb27baSJustin T. Gibbs 				DEVSTAT_MATCH_PASS) == 0)
76528fb27baSJustin T. Gibbs 			    && ((devices[j].device_type &
76628fb27baSJustin T. Gibbs 			        DEVSTAT_TYPE_PASS) == 0)))
76728fb27baSJustin T. Gibbs 			  && (--num_match_categories == 0))
76828fb27baSJustin T. Gibbs 			 || (((matches[i].match_fields & DEVSTAT_MATCH_IF) != 0)
76928fb27baSJustin T. Gibbs 			  && ((devices[j].device_type & DEVSTAT_TYPE_IF_MASK) ==
77028fb27baSJustin T. Gibbs 			        (matches[i].device_type & DEVSTAT_TYPE_IF_MASK))
77128fb27baSJustin T. Gibbs 			  &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0)
77228fb27baSJustin T. Gibbs 			   || (((matches[i].match_fields &
77328fb27baSJustin T. Gibbs 				DEVSTAT_MATCH_PASS) == 0)
77428fb27baSJustin T. Gibbs 			    && ((devices[j].device_type &
77528fb27baSJustin T. Gibbs 				DEVSTAT_TYPE_PASS) == 0)))
77628fb27baSJustin T. Gibbs 			  && (--num_match_categories == 0))
77728fb27baSJustin T. Gibbs 			 || (((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0)
77828fb27baSJustin T. Gibbs 			  && ((devices[j].device_type & DEVSTAT_TYPE_PASS) != 0)
77928fb27baSJustin T. Gibbs 			  && (--num_match_categories == 0))) {
78028fb27baSJustin T. Gibbs 
78128fb27baSJustin T. Gibbs 				/*
78228fb27baSJustin T. Gibbs 				 * This is probably a non-optimal solution
78328fb27baSJustin T. Gibbs 				 * to the problem that the devices in the
78428fb27baSJustin T. Gibbs 				 * device list will not be in the same
78528fb27baSJustin T. Gibbs 				 * order as the devices in the selection
78628fb27baSJustin T. Gibbs 				 * array.
78728fb27baSJustin T. Gibbs 				 */
78828fb27baSJustin T. Gibbs 				for (k = 0; k < numdevs; k++) {
78928fb27baSJustin T. Gibbs 					if ((*dev_select)[k].position == j) {
79028fb27baSJustin T. Gibbs 						found = 1;
79128fb27baSJustin T. Gibbs 						break;
79228fb27baSJustin T. Gibbs 					}
79328fb27baSJustin T. Gibbs 				}
79428fb27baSJustin T. Gibbs 
79528fb27baSJustin T. Gibbs 				/*
79628fb27baSJustin T. Gibbs 				 * There shouldn't be a case where a device
79728fb27baSJustin T. Gibbs 				 * in the device list is not in the
79828fb27baSJustin T. Gibbs 				 * selection list...but it could happen.
79928fb27baSJustin T. Gibbs 				 */
80028fb27baSJustin T. Gibbs 				if (found != 1) {
80128fb27baSJustin T. Gibbs 					fprintf(stderr, "selectdevs: couldn't"
80228fb27baSJustin T. Gibbs 						" find %s%d in selection "
80328fb27baSJustin T. Gibbs 						"list\n",
80428fb27baSJustin T. Gibbs 						devices[j].device_name,
80528fb27baSJustin T. Gibbs 						devices[j].unit_number);
80628fb27baSJustin T. Gibbs 					break;
80728fb27baSJustin T. Gibbs 				}
80828fb27baSJustin T. Gibbs 
80928fb27baSJustin T. Gibbs 				/*
81028fb27baSJustin T. Gibbs 				 * We do different things based upon the
81128fb27baSJustin T. Gibbs 				 * mode we're in.  If we're in add or only
81228fb27baSJustin T. Gibbs 				 * mode, we go ahead and select this device
81328fb27baSJustin T. Gibbs 				 * if it hasn't already been selected.  If
81428fb27baSJustin T. Gibbs 				 * it has already been selected, we leave
81528fb27baSJustin T. Gibbs 				 * it alone so we don't mess up the
81628fb27baSJustin T. Gibbs 				 * selection ordering.  Manually specified
81728fb27baSJustin T. Gibbs 				 * devices have already been selected, and
81828fb27baSJustin T. Gibbs 				 * they have higher priority than pattern
81928fb27baSJustin T. Gibbs 				 * matched devices.  If we're in remove
82028fb27baSJustin T. Gibbs 				 * mode, we de-select the given device and
82128fb27baSJustin T. Gibbs 				 * decrement the selected count.
82228fb27baSJustin T. Gibbs 				 */
82328fb27baSJustin T. Gibbs 				switch(select_mode) {
82428fb27baSJustin T. Gibbs 				case DS_SELECT_ADD:
82528fb27baSJustin T. Gibbs 				case DS_SELECT_ADDONLY:
82628fb27baSJustin T. Gibbs 				case DS_SELECT_ONLY:
82728fb27baSJustin T. Gibbs 					if ((*dev_select)[k].selected != 0)
82828fb27baSJustin T. Gibbs 						break;
82928fb27baSJustin T. Gibbs 					(*dev_select)[k].selected =
83028fb27baSJustin T. Gibbs 						++selection_number;
83128fb27baSJustin T. Gibbs 					(*num_selected)++;
83228fb27baSJustin T. Gibbs 					break;
83328fb27baSJustin T. Gibbs 				case DS_SELECT_REMOVE:
83428fb27baSJustin T. Gibbs 					(*dev_select)[k].selected = 0;
83528fb27baSJustin T. Gibbs 					(*num_selected)--;
83628fb27baSJustin T. Gibbs 					break;
83728fb27baSJustin T. Gibbs 				}
83828fb27baSJustin T. Gibbs 			}
83928fb27baSJustin T. Gibbs 		}
84028fb27baSJustin T. Gibbs 	}
84128fb27baSJustin T. Gibbs 
84228fb27baSJustin T. Gibbs 	/*
84328fb27baSJustin T. Gibbs 	 * Here we implement "top" mode.  Devices are sorted in the
84428fb27baSJustin T. Gibbs 	 * selection array based on two criteria:  whether or not they are
84528fb27baSJustin T. Gibbs 	 * selected (not selection number, just the fact that they are
84628fb27baSJustin T. Gibbs 	 * selected!) and the number of bytes in the "bytes" field of the
84728fb27baSJustin T. Gibbs 	 * selection structure.  The bytes field generally must be kept up
84828fb27baSJustin T. Gibbs 	 * by the user.  In the future, it may be maintained by library
84928fb27baSJustin T. Gibbs 	 * functions, but for now the user has to do the work.
85028fb27baSJustin T. Gibbs 	 *
85128fb27baSJustin T. Gibbs 	 * At first glance, it may seem wrong that we don't go through and
85228fb27baSJustin T. Gibbs 	 * select every device in the case where the user hasn't specified
85328fb27baSJustin T. Gibbs 	 * any devices or patterns.  In fact, though, it won't make any
85428fb27baSJustin T. Gibbs 	 * difference in the device sorting.  In that particular case (i.e.
85528fb27baSJustin T. Gibbs 	 * when we're in "add" or "only" mode, and the user hasn't
85628fb27baSJustin T. Gibbs 	 * specified anything) the first time through no devices will be
85728fb27baSJustin T. Gibbs 	 * selected, so the only criterion used to sort them will be their
85828fb27baSJustin T. Gibbs 	 * performance.  The second time through, and every time thereafter,
85928fb27baSJustin T. Gibbs 	 * all devices will be selected, so again selection won't matter.
86028fb27baSJustin T. Gibbs 	 */
86128fb27baSJustin T. Gibbs 	if (perf_select != 0) {
86228fb27baSJustin T. Gibbs 
86328fb27baSJustin T. Gibbs 		/* Sort the device array by throughput  */
86428fb27baSJustin T. Gibbs 		qsort(*dev_select, *num_selections,
86528fb27baSJustin T. Gibbs 		      sizeof(struct device_selection),
86628fb27baSJustin T. Gibbs 		      compare_select);
86728fb27baSJustin T. Gibbs 
86828fb27baSJustin T. Gibbs 		if (*num_selected == 0) {
86928fb27baSJustin T. Gibbs 			/*
87028fb27baSJustin T. Gibbs 			 * Here we select every device in the array, if it
87128fb27baSJustin T. Gibbs 			 * isn't already selected.  Because the 'selected'
87228fb27baSJustin T. Gibbs 			 * variable in the selection array entries contains
87328fb27baSJustin T. Gibbs 			 * the selection order, the devstats routine can show
87428fb27baSJustin T. Gibbs 			 * the devices that were selected first.
87528fb27baSJustin T. Gibbs 			 */
87628fb27baSJustin T. Gibbs 			for (i = 0; i < *num_selections; i++) {
87728fb27baSJustin T. Gibbs 				if ((*dev_select)[i].selected == 0) {
87828fb27baSJustin T. Gibbs 					(*dev_select)[i].selected =
87928fb27baSJustin T. Gibbs 						++selection_number;
88028fb27baSJustin T. Gibbs 					(*num_selected)++;
88128fb27baSJustin T. Gibbs 				}
88228fb27baSJustin T. Gibbs 			}
88328fb27baSJustin T. Gibbs 		} else {
88428fb27baSJustin T. Gibbs 			selection_number = 0;
88528fb27baSJustin T. Gibbs 			for (i = 0; i < *num_selections; i++) {
88628fb27baSJustin T. Gibbs 				if ((*dev_select)[i].selected != 0) {
88728fb27baSJustin T. Gibbs 					(*dev_select)[i].selected =
88828fb27baSJustin T. Gibbs 						++selection_number;
88928fb27baSJustin T. Gibbs 				}
89028fb27baSJustin T. Gibbs 			}
89128fb27baSJustin T. Gibbs 		}
89228fb27baSJustin T. Gibbs 	}
89328fb27baSJustin T. Gibbs 
89428fb27baSJustin T. Gibbs 	/*
89528fb27baSJustin T. Gibbs 	 * If we're in the "add" selection mode and if we haven't already
89628fb27baSJustin T. Gibbs 	 * selected maxshowdevs number of devices, go through the array and
89728fb27baSJustin T. Gibbs 	 * select any unselected devices.  If we're in "only" mode, we
89828fb27baSJustin T. Gibbs 	 * obviously don't want to select anything other than what the user
89928fb27baSJustin T. Gibbs 	 * specifies.  If we're in "remove" mode, it probably isn't a good
90028fb27baSJustin T. Gibbs 	 * idea to go through and select any more devices, since we might
90128fb27baSJustin T. Gibbs 	 * end up selecting something that the user wants removed.  Through
90228fb27baSJustin T. Gibbs 	 * more complicated logic, we could actually figure this out, but
90328fb27baSJustin T. Gibbs 	 * that would probably require combining this loop with the various
90428fb27baSJustin T. Gibbs 	 * selections loops above.
90528fb27baSJustin T. Gibbs 	 */
90628fb27baSJustin T. Gibbs 	if ((select_mode == DS_SELECT_ADD) && (*num_selected < maxshowdevs)) {
90728fb27baSJustin T. Gibbs 		for (i = 0; i < *num_selections; i++)
90828fb27baSJustin T. Gibbs 			if ((*dev_select)[i].selected == 0) {
90928fb27baSJustin T. Gibbs 				(*dev_select)[i].selected = ++selection_number;
91028fb27baSJustin T. Gibbs 				(*num_selected)++;
91128fb27baSJustin T. Gibbs 			}
91228fb27baSJustin T. Gibbs 	}
91328fb27baSJustin T. Gibbs 
91428fb27baSJustin T. Gibbs 	/*
91528fb27baSJustin T. Gibbs 	 * Look at the number of devices that have been selected.  If it
91628fb27baSJustin T. Gibbs 	 * has changed, set the changed variable.  Otherwise, if we've
91728fb27baSJustin T. Gibbs 	 * made a backup of the selection list, compare it to the current
91828fb27baSJustin T. Gibbs 	 * selection list to see if the selected devices have changed.
91928fb27baSJustin T. Gibbs 	 */
92028fb27baSJustin T. Gibbs 	if ((changed == 0) && (old_num_selected != *num_selected))
92128fb27baSJustin T. Gibbs 		changed = 1;
92228fb27baSJustin T. Gibbs 	else if ((changed == 0) && (old_dev_select != NULL)) {
92328fb27baSJustin T. Gibbs 		/*
92428fb27baSJustin T. Gibbs 		 * Now we go through the selection list and we look at
92528fb27baSJustin T. Gibbs 		 * it three different ways.
92628fb27baSJustin T. Gibbs 		 */
92728fb27baSJustin T. Gibbs 		for (i = 0; (i < *num_selections) && (changed == 0) &&
92828fb27baSJustin T. Gibbs 		     (i < old_num_selections); i++) {
92928fb27baSJustin T. Gibbs 			/*
93028fb27baSJustin T. Gibbs 			 * If the device at index i in both the new and old
93128fb27baSJustin T. Gibbs 			 * selection arrays has the same device number and
93228fb27baSJustin T. Gibbs 			 * selection status, it hasn't changed.  We
93328fb27baSJustin T. Gibbs 			 * continue on to the next index.
93428fb27baSJustin T. Gibbs 			 */
93528fb27baSJustin T. Gibbs 			if (((*dev_select)[i].device_number ==
93628fb27baSJustin T. Gibbs 			     old_dev_select[i].device_number)
93728fb27baSJustin T. Gibbs 			 && ((*dev_select)[i].selected ==
93828fb27baSJustin T. Gibbs 			     old_dev_select[i].selected))
93928fb27baSJustin T. Gibbs 				continue;
94028fb27baSJustin T. Gibbs 
94128fb27baSJustin T. Gibbs 			/*
94228fb27baSJustin T. Gibbs 			 * Now, if we're still going through the if
94328fb27baSJustin T. Gibbs 			 * statement, the above test wasn't true.  So we
94428fb27baSJustin T. Gibbs 			 * check here to see if the device at index i in
94528fb27baSJustin T. Gibbs 			 * the current array is the same as the device at
94628fb27baSJustin T. Gibbs 			 * index i in the old array.  If it is, that means
94728fb27baSJustin T. Gibbs 			 * that its selection number has changed.  Set
94828fb27baSJustin T. Gibbs 			 * changed to 1 and exit the loop.
94928fb27baSJustin T. Gibbs 			 */
95028fb27baSJustin T. Gibbs 			else if ((*dev_select)[i].device_number ==
95128fb27baSJustin T. Gibbs 			          old_dev_select[i].device_number) {
95228fb27baSJustin T. Gibbs 				changed = 1;
95328fb27baSJustin T. Gibbs 				break;
95428fb27baSJustin T. Gibbs 			}
95528fb27baSJustin T. Gibbs 			/*
95628fb27baSJustin T. Gibbs 			 * If we get here, then the device at index i in
95728fb27baSJustin T. Gibbs 			 * the current array isn't the same device as the
95828fb27baSJustin T. Gibbs 			 * device at index i in the old array.
95928fb27baSJustin T. Gibbs 			 */
96028fb27baSJustin T. Gibbs 			else {
961c3508206SKenneth D. Merry 				found = 0;
96228fb27baSJustin T. Gibbs 
96328fb27baSJustin T. Gibbs 				/*
96428fb27baSJustin T. Gibbs 				 * Search through the old selection array
96528fb27baSJustin T. Gibbs 				 * looking for a device with the same
96628fb27baSJustin T. Gibbs 				 * device number as the device at index i
96728fb27baSJustin T. Gibbs 				 * in the current array.  If the selection
96828fb27baSJustin T. Gibbs 				 * status is the same, then we mark it as
96928fb27baSJustin T. Gibbs 				 * found.  If the selection status isn't
97028fb27baSJustin T. Gibbs 				 * the same, we break out of the loop.
97128fb27baSJustin T. Gibbs 				 * Since found isn't set, changed will be
97228fb27baSJustin T. Gibbs 				 * set to 1 below.
97328fb27baSJustin T. Gibbs 				 */
97428fb27baSJustin T. Gibbs 				for (j = 0; j < old_num_selections; j++) {
97528fb27baSJustin T. Gibbs 					if (((*dev_select)[i].device_number ==
97628fb27baSJustin T. Gibbs 					      old_dev_select[j].device_number)
97728fb27baSJustin T. Gibbs 					 && ((*dev_select)[i].selected ==
97828fb27baSJustin T. Gibbs 					      old_dev_select[j].selected)){
97928fb27baSJustin T. Gibbs 						found = 1;
98028fb27baSJustin T. Gibbs 						break;
98128fb27baSJustin T. Gibbs 					}
98228fb27baSJustin T. Gibbs 					else if ((*dev_select)[i].device_number
98328fb27baSJustin T. Gibbs 					    == old_dev_select[j].device_number)
98428fb27baSJustin T. Gibbs 						break;
98528fb27baSJustin T. Gibbs 				}
98628fb27baSJustin T. Gibbs 				if (found == 0)
98728fb27baSJustin T. Gibbs 					changed = 1;
98828fb27baSJustin T. Gibbs 			}
98928fb27baSJustin T. Gibbs 		}
99028fb27baSJustin T. Gibbs 	}
99128fb27baSJustin T. Gibbs 	if (old_dev_select != NULL)
99228fb27baSJustin T. Gibbs 		free(old_dev_select);
99328fb27baSJustin T. Gibbs 
99428fb27baSJustin T. Gibbs 	return(changed);
99528fb27baSJustin T. Gibbs }
99628fb27baSJustin T. Gibbs 
99728fb27baSJustin T. Gibbs /*
99828fb27baSJustin T. Gibbs  * Comparison routine for qsort() above.  Note that the comparison here is
99928fb27baSJustin T. Gibbs  * backwards -- generally, it should return a value to indicate whether
100028fb27baSJustin T. Gibbs  * arg1 is <, =, or > arg2.  Instead, it returns the opposite.  The reason
100128fb27baSJustin T. Gibbs  * it returns the opposite is so that the selection array will be sorted in
100228fb27baSJustin T. Gibbs  * order of decreasing performance.  We sort on two parameters.  The first
100328fb27baSJustin T. Gibbs  * sort key is whether or not one or the other of the devices in question
100428fb27baSJustin T. Gibbs  * has been selected.  If one of them has, and the other one has not, the
100528fb27baSJustin T. Gibbs  * selected device is automatically more important than the unselected
100628fb27baSJustin T. Gibbs  * device.  If neither device is selected, we judge the devices based upon
100728fb27baSJustin T. Gibbs  * performance.
100828fb27baSJustin T. Gibbs  */
100928fb27baSJustin T. Gibbs static int
compare_select(const void * arg1,const void * arg2)101028fb27baSJustin T. Gibbs compare_select(const void *arg1, const void *arg2)
101128fb27baSJustin T. Gibbs {
1012c3508206SKenneth D. Merry 	if ((((const struct device_selection *)arg1)->selected)
1013c3508206SKenneth D. Merry 	 && (((const struct device_selection *)arg2)->selected == 0))
101428fb27baSJustin T. Gibbs 		return(-1);
1015c3508206SKenneth D. Merry 	else if ((((const struct device_selection *)arg1)->selected == 0)
1016c3508206SKenneth D. Merry 	      && (((const struct device_selection *)arg2)->selected))
101728fb27baSJustin T. Gibbs 		return(1);
1018c3508206SKenneth D. Merry 	else if (((const struct device_selection *)arg2)->bytes <
1019c3508206SKenneth D. Merry 	         ((const struct device_selection *)arg1)->bytes)
102028fb27baSJustin T. Gibbs 		return(-1);
1021c3508206SKenneth D. Merry 	else if (((const struct device_selection *)arg2)->bytes >
1022c3508206SKenneth D. Merry 		 ((const struct device_selection *)arg1)->bytes)
102328fb27baSJustin T. Gibbs 		return(1);
102428fb27baSJustin T. Gibbs 	else
102528fb27baSJustin T. Gibbs 		return(0);
102628fb27baSJustin T. Gibbs }
102728fb27baSJustin T. Gibbs 
102828fb27baSJustin T. Gibbs /*
102928fb27baSJustin T. Gibbs  * Take a string with the general format "arg1,arg2,arg3", and build a
103028fb27baSJustin T. Gibbs  * device matching expression from it.
103128fb27baSJustin T. Gibbs  */
103228fb27baSJustin T. Gibbs int
devstat_buildmatch(char * match_str,struct devstat_match ** matches,int * num_matches)1033c4a5ef6eSThomas Moestl devstat_buildmatch(char *match_str, struct devstat_match **matches,
1034c4a5ef6eSThomas Moestl 		   int *num_matches)
103528fb27baSJustin T. Gibbs {
103628fb27baSJustin T. Gibbs 	char *tstr[5];
103728fb27baSJustin T. Gibbs 	char **tempstr;
103828fb27baSJustin T. Gibbs 	int num_args;
1039be04b6d1SDavid E. O'Brien 	int i, j;
104028fb27baSJustin T. Gibbs 
104128fb27baSJustin T. Gibbs 	/* We can't do much without a string to parse */
104228fb27baSJustin T. Gibbs 	if (match_str == NULL) {
1043c4a5ef6eSThomas Moestl 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
10447d72cda3SMaxime Henrion 			 "%s: no match expression", __func__);
104528fb27baSJustin T. Gibbs 		return(-1);
104628fb27baSJustin T. Gibbs 	}
104728fb27baSJustin T. Gibbs 
104828fb27baSJustin T. Gibbs 	/*
104928fb27baSJustin T. Gibbs 	 * Break the (comma delimited) input string out into separate strings.
105028fb27baSJustin T. Gibbs 	 */
105128fb27baSJustin T. Gibbs 	for (tempstr = tstr, num_args  = 0;
10521046090fSSergey Kandaurov 	     (*tempstr = strsep(&match_str, ",")) != NULL && (num_args < 5);)
10531046090fSSergey Kandaurov 		if (**tempstr != '\0') {
10541046090fSSergey Kandaurov 			num_args++;
105528fb27baSJustin T. Gibbs 			if (++tempstr >= &tstr[5])
105628fb27baSJustin T. Gibbs 				break;
10571046090fSSergey Kandaurov 		}
105828fb27baSJustin T. Gibbs 
105928fb27baSJustin T. Gibbs 	/* The user gave us too many type arguments */
106028fb27baSJustin T. Gibbs 	if (num_args > 3) {
1061c4a5ef6eSThomas Moestl 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
10627d72cda3SMaxime Henrion 			 "%s: too many type arguments", __func__);
106328fb27baSJustin T. Gibbs 		return(-1);
106428fb27baSJustin T. Gibbs 	}
106528fb27baSJustin T. Gibbs 
106628fb27baSJustin T. Gibbs 	if (*num_matches == 0)
1067a18e4616SGuy Helmer 		*matches = NULL;
1068a18e4616SGuy Helmer 
1069a18e4616SGuy Helmer 	*matches = (struct devstat_match *)reallocf(*matches,
107028fb27baSJustin T. Gibbs 		  sizeof(struct devstat_match) * (*num_matches + 1));
107128fb27baSJustin T. Gibbs 
1072a18e4616SGuy Helmer 	if (*matches == NULL) {
1073a18e4616SGuy Helmer 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1074a18e4616SGuy Helmer 			 "%s: Cannot allocate memory for matches list", __func__);
1075a18e4616SGuy Helmer 		return(-1);
1076a18e4616SGuy Helmer 	}
1077a18e4616SGuy Helmer 
107828fb27baSJustin T. Gibbs 	/* Make sure the current entry is clear */
107928fb27baSJustin T. Gibbs 	bzero(&matches[0][*num_matches], sizeof(struct devstat_match));
108028fb27baSJustin T. Gibbs 
108128fb27baSJustin T. Gibbs 	/*
108228fb27baSJustin T. Gibbs 	 * Step through the arguments the user gave us and build a device
108328fb27baSJustin T. Gibbs 	 * matching expression from them.
108428fb27baSJustin T. Gibbs 	 */
108528fb27baSJustin T. Gibbs 	for (i = 0; i < num_args; i++) {
108628fb27baSJustin T. Gibbs 		char *tempstr2, *tempstr3;
108728fb27baSJustin T. Gibbs 
108828fb27baSJustin T. Gibbs 		/*
108928fb27baSJustin T. Gibbs 		 * Get rid of leading white space.
109028fb27baSJustin T. Gibbs 		 */
109128fb27baSJustin T. Gibbs 		tempstr2 = tstr[i];
109228fb27baSJustin T. Gibbs 		while (isspace(*tempstr2) && (*tempstr2 != '\0'))
109328fb27baSJustin T. Gibbs 			tempstr2++;
109428fb27baSJustin T. Gibbs 
109528fb27baSJustin T. Gibbs 		/*
109628fb27baSJustin T. Gibbs 		 * Get rid of trailing white space.
109728fb27baSJustin T. Gibbs 		 */
109828fb27baSJustin T. Gibbs 		tempstr3 = &tempstr2[strlen(tempstr2) - 1];
109928fb27baSJustin T. Gibbs 
110028fb27baSJustin T. Gibbs 		while ((*tempstr3 != '\0') && (tempstr3 > tempstr2)
110128fb27baSJustin T. Gibbs 		    && (isspace(*tempstr3))) {
110228fb27baSJustin T. Gibbs 			*tempstr3 = '\0';
110328fb27baSJustin T. Gibbs 			tempstr3--;
110428fb27baSJustin T. Gibbs 		}
110528fb27baSJustin T. Gibbs 
110628fb27baSJustin T. Gibbs 		/*
110728fb27baSJustin T. Gibbs 		 * Go through the match table comparing the user's
110828fb27baSJustin T. Gibbs 		 * arguments to known device types, interfaces, etc.
110928fb27baSJustin T. Gibbs 		 */
111028fb27baSJustin T. Gibbs 		for (j = 0; match_table[j].match_str != NULL; j++) {
111128fb27baSJustin T. Gibbs 			/*
111228fb27baSJustin T. Gibbs 			 * We do case-insensitive matching, in case someone
111328fb27baSJustin T. Gibbs 			 * wants to enter "SCSI" instead of "scsi" or
111428fb27baSJustin T. Gibbs 			 * something like that.  Only compare as many
111528fb27baSJustin T. Gibbs 			 * characters as are in the string in the match
111628fb27baSJustin T. Gibbs 			 * table.  This should help if someone tries to use
111728fb27baSJustin T. Gibbs 			 * a super-long match expression.
111828fb27baSJustin T. Gibbs 			 */
111928fb27baSJustin T. Gibbs 			if (strncasecmp(tempstr2, match_table[j].match_str,
112028fb27baSJustin T. Gibbs 			    strlen(match_table[j].match_str)) == 0) {
112128fb27baSJustin T. Gibbs 				/*
112228fb27baSJustin T. Gibbs 				 * Make sure the user hasn't specified two
112328fb27baSJustin T. Gibbs 				 * items of the same type, like "da" and
112428fb27baSJustin T. Gibbs 				 * "cd".  One device cannot be both.
112528fb27baSJustin T. Gibbs 				 */
112628fb27baSJustin T. Gibbs 				if (((*matches)[*num_matches].match_fields &
112728fb27baSJustin T. Gibbs 				    match_table[j].match_field) != 0) {
1128c4a5ef6eSThomas Moestl 					snprintf(devstat_errbuf,
1129c4a5ef6eSThomas Moestl 						 sizeof(devstat_errbuf),
113028fb27baSJustin T. Gibbs 						 "%s: cannot have more than "
113128fb27baSJustin T. Gibbs 						 "one match item in a single "
11327d72cda3SMaxime Henrion 						 "category", __func__);
113328fb27baSJustin T. Gibbs 					return(-1);
113428fb27baSJustin T. Gibbs 				}
113528fb27baSJustin T. Gibbs 				/*
113628fb27baSJustin T. Gibbs 				 * If we've gotten this far, we have a
113728fb27baSJustin T. Gibbs 				 * winner.  Set the appropriate fields in
113828fb27baSJustin T. Gibbs 				 * the match entry.
113928fb27baSJustin T. Gibbs 				 */
114028fb27baSJustin T. Gibbs 				(*matches)[*num_matches].match_fields |=
114128fb27baSJustin T. Gibbs 					match_table[j].match_field;
114228fb27baSJustin T. Gibbs 				(*matches)[*num_matches].device_type |=
114328fb27baSJustin T. Gibbs 					match_table[j].type;
114428fb27baSJustin T. Gibbs 				(*matches)[*num_matches].num_match_categories++;
114528fb27baSJustin T. Gibbs 				break;
114628fb27baSJustin T. Gibbs 			}
114728fb27baSJustin T. Gibbs 		}
114828fb27baSJustin T. Gibbs 		/*
114928fb27baSJustin T. Gibbs 		 * We should have found a match in the above for loop.  If
115028fb27baSJustin T. Gibbs 		 * not, that means the user entered an invalid device type
115128fb27baSJustin T. Gibbs 		 * or interface.
115228fb27baSJustin T. Gibbs 		 */
115328fb27baSJustin T. Gibbs 		if ((*matches)[*num_matches].num_match_categories != (i + 1)) {
115417ee2b20SKenneth D. Merry 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
11557d72cda3SMaxime Henrion 				 "%s: unknown match item \"%s\"", __func__,
115628fb27baSJustin T. Gibbs 				 tstr[i]);
115728fb27baSJustin T. Gibbs 			return(-1);
115828fb27baSJustin T. Gibbs 		}
115928fb27baSJustin T. Gibbs 	}
116028fb27baSJustin T. Gibbs 
116128fb27baSJustin T. Gibbs 	(*num_matches)++;
116228fb27baSJustin T. Gibbs 
116328fb27baSJustin T. Gibbs 	return(0);
116428fb27baSJustin T. Gibbs }
116528fb27baSJustin T. Gibbs 
116628fb27baSJustin T. Gibbs /*
116728fb27baSJustin T. Gibbs  * Compute a number of device statistics.  Only one field is mandatory, and
116828fb27baSJustin T. Gibbs  * that is "current".  Everything else is optional.  The caller passes in
116928fb27baSJustin T. Gibbs  * pointers to variables to hold the various statistics he desires.  If he
117028fb27baSJustin T. Gibbs  * doesn't want a particular staistic, he should pass in a NULL pointer.
117128fb27baSJustin T. Gibbs  * Return values:
117228fb27baSJustin T. Gibbs  * 0   -- success
117328fb27baSJustin T. Gibbs  * -1  -- failure
117428fb27baSJustin T. Gibbs  */
117528fb27baSJustin T. Gibbs int
compute_stats(struct devstat * current,struct devstat * previous,long double etime,u_int64_t * total_bytes,u_int64_t * total_transfers,u_int64_t * total_blocks,long double * kb_per_transfer,long double * transfers_per_second,long double * mb_per_second,long double * blocks_per_second,long double * ms_per_transaction)117628fb27baSJustin T. Gibbs compute_stats(struct devstat *current, struct devstat *previous,
117728fb27baSJustin T. Gibbs 	      long double etime, u_int64_t *total_bytes,
117828fb27baSJustin T. Gibbs 	      u_int64_t *total_transfers, u_int64_t *total_blocks,
117928fb27baSJustin T. Gibbs 	      long double *kb_per_transfer, long double *transfers_per_second,
118028fb27baSJustin T. Gibbs 	      long double *mb_per_second, long double *blocks_per_second,
118128fb27baSJustin T. Gibbs 	      long double *ms_per_transaction)
118228fb27baSJustin T. Gibbs {
1183884539f7SKenneth D. Merry 	return(devstat_compute_statistics(current, previous, etime,
1184884539f7SKenneth D. Merry 	       total_bytes ? DSM_TOTAL_BYTES : DSM_SKIP,
1185884539f7SKenneth D. Merry 	       total_bytes,
1186884539f7SKenneth D. Merry 	       total_transfers ? DSM_TOTAL_TRANSFERS : DSM_SKIP,
1187884539f7SKenneth D. Merry 	       total_transfers,
1188884539f7SKenneth D. Merry 	       total_blocks ? DSM_TOTAL_BLOCKS : DSM_SKIP,
1189884539f7SKenneth D. Merry 	       total_blocks,
1190884539f7SKenneth D. Merry 	       kb_per_transfer ? DSM_KB_PER_TRANSFER : DSM_SKIP,
1191884539f7SKenneth D. Merry 	       kb_per_transfer,
1192884539f7SKenneth D. Merry 	       transfers_per_second ? DSM_TRANSFERS_PER_SECOND : DSM_SKIP,
1193884539f7SKenneth D. Merry 	       transfers_per_second,
1194884539f7SKenneth D. Merry 	       mb_per_second ? DSM_MB_PER_SECOND : DSM_SKIP,
1195884539f7SKenneth D. Merry 	       mb_per_second,
1196884539f7SKenneth D. Merry 	       blocks_per_second ? DSM_BLOCKS_PER_SECOND : DSM_SKIP,
1197884539f7SKenneth D. Merry 	       blocks_per_second,
1198884539f7SKenneth D. Merry 	       ms_per_transaction ? DSM_MS_PER_TRANSACTION : DSM_SKIP,
1199884539f7SKenneth D. Merry 	       ms_per_transaction,
1200884539f7SKenneth D. Merry 	       DSM_NONE));
120128fb27baSJustin T. Gibbs }
120228fb27baSJustin T. Gibbs 
12037194d335SPoul-Henning Kamp 
12047194d335SPoul-Henning Kamp /* This is 1/2^64 */
12057194d335SPoul-Henning Kamp #define BINTIME_SCALE 5.42101086242752217003726400434970855712890625e-20
12067194d335SPoul-Henning Kamp 
120728fb27baSJustin T. Gibbs long double
devstat_compute_etime(struct bintime * cur_time,struct bintime * prev_time)12087194d335SPoul-Henning Kamp devstat_compute_etime(struct bintime *cur_time, struct bintime *prev_time)
120928fb27baSJustin T. Gibbs {
121028fb27baSJustin T. Gibbs 	long double etime;
121128fb27baSJustin T. Gibbs 
12127194d335SPoul-Henning Kamp 	etime = cur_time->sec;
12137194d335SPoul-Henning Kamp 	etime += cur_time->frac * BINTIME_SCALE;
12147194d335SPoul-Henning Kamp 	if (prev_time != NULL) {
12157194d335SPoul-Henning Kamp 		etime -= prev_time->sec;
12167194d335SPoul-Henning Kamp 		etime -= prev_time->frac * BINTIME_SCALE;
12177194d335SPoul-Henning Kamp 	}
121828fb27baSJustin T. Gibbs 	return(etime);
121928fb27baSJustin T. Gibbs }
1220c4a5ef6eSThomas Moestl 
122136eab1f5SPoul-Henning Kamp #define DELTA(field, index)				\
122236eab1f5SPoul-Henning Kamp 	(current->field[(index)] - (previous ? previous->field[(index)] : 0))
122336eab1f5SPoul-Henning Kamp 
122436eab1f5SPoul-Henning Kamp #define DELTA_T(field)					\
122536eab1f5SPoul-Henning Kamp 	devstat_compute_etime(&current->field,  	\
122636eab1f5SPoul-Henning Kamp 	(previous ? &previous->field : NULL))
122736eab1f5SPoul-Henning Kamp 
1228c4a5ef6eSThomas Moestl int
devstat_compute_statistics(struct devstat * current,struct devstat * previous,long double etime,...)1229c4a5ef6eSThomas Moestl devstat_compute_statistics(struct devstat *current, struct devstat *previous,
1230c4a5ef6eSThomas Moestl 			   long double etime, ...)
1231c4a5ef6eSThomas Moestl {
123236eab1f5SPoul-Henning Kamp 	u_int64_t totalbytes, totalbytesread, totalbyteswrite, totalbytesfree;
1233c4a5ef6eSThomas Moestl 	u_int64_t totaltransfers, totaltransfersread, totaltransferswrite;
1234c4a5ef6eSThomas Moestl 	u_int64_t totaltransfersother, totalblocks, totalblocksread;
123536eab1f5SPoul-Henning Kamp 	u_int64_t totalblockswrite, totaltransfersfree, totalblocksfree;
1236fdd6757eSMikolaj Golub 	long double totalduration, totaldurationread, totaldurationwrite;
1237fdd6757eSMikolaj Golub 	long double totaldurationfree, totaldurationother;
1238c4a5ef6eSThomas Moestl 	va_list ap;
1239c4a5ef6eSThomas Moestl 	devstat_metric metric;
1240c4a5ef6eSThomas Moestl 	u_int64_t *destu64;
1241c4a5ef6eSThomas Moestl 	long double *destld;
1242fdd6757eSMikolaj Golub 	int retval;
1243c4a5ef6eSThomas Moestl 
1244c4a5ef6eSThomas Moestl 	retval = 0;
1245c4a5ef6eSThomas Moestl 
1246c4a5ef6eSThomas Moestl 	/*
1247c4a5ef6eSThomas Moestl 	 * current is the only mandatory field.
1248c4a5ef6eSThomas Moestl 	 */
1249c4a5ef6eSThomas Moestl 	if (current == NULL) {
1250c4a5ef6eSThomas Moestl 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
12517d72cda3SMaxime Henrion 			 "%s: current stats structure was NULL", __func__);
1252c4a5ef6eSThomas Moestl 		return(-1);
1253c4a5ef6eSThomas Moestl 	}
1254c4a5ef6eSThomas Moestl 
125536eab1f5SPoul-Henning Kamp 	totalbytesread = DELTA(bytes, DEVSTAT_READ);
125636eab1f5SPoul-Henning Kamp 	totalbyteswrite = DELTA(bytes, DEVSTAT_WRITE);
125736eab1f5SPoul-Henning Kamp 	totalbytesfree = DELTA(bytes, DEVSTAT_FREE);
125836eab1f5SPoul-Henning Kamp 	totalbytes = totalbytesread + totalbyteswrite + totalbytesfree;
1259c4a5ef6eSThomas Moestl 
126036eab1f5SPoul-Henning Kamp 	totaltransfersread = DELTA(operations, DEVSTAT_READ);
126136eab1f5SPoul-Henning Kamp 	totaltransferswrite = DELTA(operations, DEVSTAT_WRITE);
126236eab1f5SPoul-Henning Kamp 	totaltransfersother = DELTA(operations, DEVSTAT_NO_DATA);
126336eab1f5SPoul-Henning Kamp 	totaltransfersfree = DELTA(operations, DEVSTAT_FREE);
1264c4a5ef6eSThomas Moestl 	totaltransfers = totaltransfersread + totaltransferswrite +
126536eab1f5SPoul-Henning Kamp 			 totaltransfersother + totaltransfersfree;
1266c4a5ef6eSThomas Moestl 
1267c4a5ef6eSThomas Moestl 	totalblocks = totalbytes;
1268c4a5ef6eSThomas Moestl 	totalblocksread = totalbytesread;
1269c4a5ef6eSThomas Moestl 	totalblockswrite = totalbyteswrite;
127036eab1f5SPoul-Henning Kamp 	totalblocksfree = totalbytesfree;
1271c4a5ef6eSThomas Moestl 
1272c4a5ef6eSThomas Moestl 	if (current->block_size > 0) {
1273c4a5ef6eSThomas Moestl 		totalblocks /= current->block_size;
1274c4a5ef6eSThomas Moestl 		totalblocksread /= current->block_size;
1275c4a5ef6eSThomas Moestl 		totalblockswrite /= current->block_size;
127636eab1f5SPoul-Henning Kamp 		totalblocksfree /= current->block_size;
1277c4a5ef6eSThomas Moestl 	} else {
1278c4a5ef6eSThomas Moestl 		totalblocks /= 512;
1279c4a5ef6eSThomas Moestl 		totalblocksread /= 512;
1280c4a5ef6eSThomas Moestl 		totalblockswrite /= 512;
128136eab1f5SPoul-Henning Kamp 		totalblocksfree /= 512;
1282c4a5ef6eSThomas Moestl 	}
1283c4a5ef6eSThomas Moestl 
1284fdd6757eSMikolaj Golub 	totaldurationread = DELTA_T(duration[DEVSTAT_READ]);
1285fdd6757eSMikolaj Golub 	totaldurationwrite = DELTA_T(duration[DEVSTAT_WRITE]);
1286fdd6757eSMikolaj Golub 	totaldurationfree = DELTA_T(duration[DEVSTAT_FREE]);
1287fdd6757eSMikolaj Golub 	totaldurationother = DELTA_T(duration[DEVSTAT_NO_DATA]);
1288fdd6757eSMikolaj Golub 	totalduration = totaldurationread + totaldurationwrite +
1289fdd6757eSMikolaj Golub 	    totaldurationfree + totaldurationother;
1290fdd6757eSMikolaj Golub 
1291c4a5ef6eSThomas Moestl 	va_start(ap, etime);
1292c4a5ef6eSThomas Moestl 
1293c4a5ef6eSThomas Moestl 	while ((metric = (devstat_metric)va_arg(ap, devstat_metric)) != 0) {
1294c4a5ef6eSThomas Moestl 
1295c4a5ef6eSThomas Moestl 		if (metric == DSM_NONE)
1296c4a5ef6eSThomas Moestl 			break;
1297c4a5ef6eSThomas Moestl 
1298c4a5ef6eSThomas Moestl 		if (metric >= DSM_MAX) {
1299c4a5ef6eSThomas Moestl 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
13007d72cda3SMaxime Henrion 				 "%s: metric %d is out of range", __func__,
1301c4a5ef6eSThomas Moestl 				 metric);
1302c4a5ef6eSThomas Moestl 			retval = -1;
1303c4a5ef6eSThomas Moestl 			goto bailout;
1304c4a5ef6eSThomas Moestl 		}
1305c4a5ef6eSThomas Moestl 
1306c4a5ef6eSThomas Moestl 		switch (devstat_arg_list[metric].argtype) {
1307c4a5ef6eSThomas Moestl 		case DEVSTAT_ARG_UINT64:
1308c4a5ef6eSThomas Moestl 			destu64 = (u_int64_t *)va_arg(ap, u_int64_t *);
1309c4a5ef6eSThomas Moestl 			break;
1310c4a5ef6eSThomas Moestl 		case DEVSTAT_ARG_LD:
1311c4a5ef6eSThomas Moestl 			destld = (long double *)va_arg(ap, long double *);
1312884539f7SKenneth D. Merry 			break;
1313884539f7SKenneth D. Merry 		case DEVSTAT_ARG_SKIP:
1314884539f7SKenneth D. Merry 			destld = (long double *)va_arg(ap, long double *);
1315c4a5ef6eSThomas Moestl 			break;
1316c4a5ef6eSThomas Moestl 		default:
1317c4a5ef6eSThomas Moestl 			retval = -1;
1318c4a5ef6eSThomas Moestl 			goto bailout;
1319c4a5ef6eSThomas Moestl 			break; /* NOTREACHED */
1320c4a5ef6eSThomas Moestl 		}
1321c4a5ef6eSThomas Moestl 
1322884539f7SKenneth D. Merry 		if (devstat_arg_list[metric].argtype == DEVSTAT_ARG_SKIP)
1323884539f7SKenneth D. Merry 			continue;
1324884539f7SKenneth D. Merry 
1325c4a5ef6eSThomas Moestl 		switch (metric) {
1326c4a5ef6eSThomas Moestl 		case DSM_TOTAL_BYTES:
1327c4a5ef6eSThomas Moestl 			*destu64 = totalbytes;
1328c4a5ef6eSThomas Moestl 			break;
1329c4a5ef6eSThomas Moestl 		case DSM_TOTAL_BYTES_READ:
1330c4a5ef6eSThomas Moestl 			*destu64 = totalbytesread;
1331c4a5ef6eSThomas Moestl 			break;
1332c4a5ef6eSThomas Moestl 		case DSM_TOTAL_BYTES_WRITE:
1333c4a5ef6eSThomas Moestl 			*destu64 = totalbyteswrite;
1334c4a5ef6eSThomas Moestl 			break;
133536eab1f5SPoul-Henning Kamp 		case DSM_TOTAL_BYTES_FREE:
133636eab1f5SPoul-Henning Kamp 			*destu64 = totalbytesfree;
133736eab1f5SPoul-Henning Kamp 			break;
1338c4a5ef6eSThomas Moestl 		case DSM_TOTAL_TRANSFERS:
1339c4a5ef6eSThomas Moestl 			*destu64 = totaltransfers;
1340c4a5ef6eSThomas Moestl 			break;
1341c4a5ef6eSThomas Moestl 		case DSM_TOTAL_TRANSFERS_READ:
1342c4a5ef6eSThomas Moestl 			*destu64 = totaltransfersread;
1343c4a5ef6eSThomas Moestl 			break;
1344c4a5ef6eSThomas Moestl 		case DSM_TOTAL_TRANSFERS_WRITE:
1345c4a5ef6eSThomas Moestl 			*destu64 = totaltransferswrite;
1346c4a5ef6eSThomas Moestl 			break;
134736eab1f5SPoul-Henning Kamp 		case DSM_TOTAL_TRANSFERS_FREE:
134836eab1f5SPoul-Henning Kamp 			*destu64 = totaltransfersfree;
134936eab1f5SPoul-Henning Kamp 			break;
1350c4a5ef6eSThomas Moestl 		case DSM_TOTAL_TRANSFERS_OTHER:
1351c4a5ef6eSThomas Moestl 			*destu64 = totaltransfersother;
1352c4a5ef6eSThomas Moestl 			break;
1353c4a5ef6eSThomas Moestl 		case DSM_TOTAL_BLOCKS:
1354c4a5ef6eSThomas Moestl 			*destu64 = totalblocks;
1355c4a5ef6eSThomas Moestl 			break;
1356c4a5ef6eSThomas Moestl 		case DSM_TOTAL_BLOCKS_READ:
1357c4a5ef6eSThomas Moestl 			*destu64 = totalblocksread;
1358c4a5ef6eSThomas Moestl 			break;
1359c4a5ef6eSThomas Moestl 		case DSM_TOTAL_BLOCKS_WRITE:
1360c4a5ef6eSThomas Moestl 			*destu64 = totalblockswrite;
1361c4a5ef6eSThomas Moestl 			break;
136236eab1f5SPoul-Henning Kamp 		case DSM_TOTAL_BLOCKS_FREE:
136336eab1f5SPoul-Henning Kamp 			*destu64 = totalblocksfree;
136436eab1f5SPoul-Henning Kamp 			break;
1365c4a5ef6eSThomas Moestl 		case DSM_KB_PER_TRANSFER:
1366c4a5ef6eSThomas Moestl 			*destld = totalbytes;
1367c4a5ef6eSThomas Moestl 			*destld /= 1024;
1368c4a5ef6eSThomas Moestl 			if (totaltransfers > 0)
1369c4a5ef6eSThomas Moestl 				*destld /= totaltransfers;
1370c4a5ef6eSThomas Moestl 			else
1371c4a5ef6eSThomas Moestl 				*destld = 0.0;
1372c4a5ef6eSThomas Moestl 			break;
1373c4a5ef6eSThomas Moestl 		case DSM_KB_PER_TRANSFER_READ:
1374c4a5ef6eSThomas Moestl 			*destld = totalbytesread;
1375c4a5ef6eSThomas Moestl 			*destld /= 1024;
1376c4a5ef6eSThomas Moestl 			if (totaltransfersread > 0)
1377c4a5ef6eSThomas Moestl 				*destld /= totaltransfersread;
1378c4a5ef6eSThomas Moestl 			else
1379c4a5ef6eSThomas Moestl 				*destld = 0.0;
1380c4a5ef6eSThomas Moestl 			break;
1381c4a5ef6eSThomas Moestl 		case DSM_KB_PER_TRANSFER_WRITE:
1382c4a5ef6eSThomas Moestl 			*destld = totalbyteswrite;
1383c4a5ef6eSThomas Moestl 			*destld /= 1024;
1384c4a5ef6eSThomas Moestl 			if (totaltransferswrite > 0)
1385c4a5ef6eSThomas Moestl 				*destld /= totaltransferswrite;
1386c4a5ef6eSThomas Moestl 			else
1387c4a5ef6eSThomas Moestl 				*destld = 0.0;
1388c4a5ef6eSThomas Moestl 			break;
138936eab1f5SPoul-Henning Kamp 		case DSM_KB_PER_TRANSFER_FREE:
139036eab1f5SPoul-Henning Kamp 			*destld = totalbytesfree;
139136eab1f5SPoul-Henning Kamp 			*destld /= 1024;
139236eab1f5SPoul-Henning Kamp 			if (totaltransfersfree > 0)
139336eab1f5SPoul-Henning Kamp 				*destld /= totaltransfersfree;
139436eab1f5SPoul-Henning Kamp 			else
139536eab1f5SPoul-Henning Kamp 				*destld = 0.0;
139636eab1f5SPoul-Henning Kamp 			break;
1397c4a5ef6eSThomas Moestl 		case DSM_TRANSFERS_PER_SECOND:
1398c4a5ef6eSThomas Moestl 			if (etime > 0.0) {
1399c4a5ef6eSThomas Moestl 				*destld = totaltransfers;
1400c4a5ef6eSThomas Moestl 				*destld /= etime;
1401c4a5ef6eSThomas Moestl 			} else
1402c4a5ef6eSThomas Moestl 				*destld = 0.0;
1403c4a5ef6eSThomas Moestl 			break;
1404c4a5ef6eSThomas Moestl 		case DSM_TRANSFERS_PER_SECOND_READ:
1405c4a5ef6eSThomas Moestl 			if (etime > 0.0) {
1406c4a5ef6eSThomas Moestl 				*destld = totaltransfersread;
1407c4a5ef6eSThomas Moestl 				*destld /= etime;
1408c4a5ef6eSThomas Moestl 			} else
1409c4a5ef6eSThomas Moestl 				*destld = 0.0;
1410c4a5ef6eSThomas Moestl 			break;
1411c4a5ef6eSThomas Moestl 		case DSM_TRANSFERS_PER_SECOND_WRITE:
1412c4a5ef6eSThomas Moestl 			if (etime > 0.0) {
1413c4a5ef6eSThomas Moestl 				*destld = totaltransferswrite;
1414c4a5ef6eSThomas Moestl 				*destld /= etime;
1415c4a5ef6eSThomas Moestl 			} else
1416c4a5ef6eSThomas Moestl 				*destld = 0.0;
1417c4a5ef6eSThomas Moestl 			break;
141836eab1f5SPoul-Henning Kamp 		case DSM_TRANSFERS_PER_SECOND_FREE:
141936eab1f5SPoul-Henning Kamp 			if (etime > 0.0) {
142036eab1f5SPoul-Henning Kamp 				*destld = totaltransfersfree;
142136eab1f5SPoul-Henning Kamp 				*destld /= etime;
142236eab1f5SPoul-Henning Kamp 			} else
142336eab1f5SPoul-Henning Kamp 				*destld = 0.0;
142436eab1f5SPoul-Henning Kamp 			break;
1425c4a5ef6eSThomas Moestl 		case DSM_TRANSFERS_PER_SECOND_OTHER:
1426c4a5ef6eSThomas Moestl 			if (etime > 0.0) {
1427c4a5ef6eSThomas Moestl 				*destld = totaltransfersother;
1428c4a5ef6eSThomas Moestl 				*destld /= etime;
1429c4a5ef6eSThomas Moestl 			} else
1430c4a5ef6eSThomas Moestl 				*destld = 0.0;
1431c4a5ef6eSThomas Moestl 			break;
1432c4a5ef6eSThomas Moestl 		case DSM_MB_PER_SECOND:
1433c4a5ef6eSThomas Moestl 			*destld = totalbytes;
1434c4a5ef6eSThomas Moestl 			*destld /= 1024 * 1024;
1435c4a5ef6eSThomas Moestl 			if (etime > 0.0)
1436c4a5ef6eSThomas Moestl 				*destld /= etime;
1437c4a5ef6eSThomas Moestl 			else
1438c4a5ef6eSThomas Moestl 				*destld = 0.0;
1439c4a5ef6eSThomas Moestl 			break;
1440c4a5ef6eSThomas Moestl 		case DSM_MB_PER_SECOND_READ:
1441c4a5ef6eSThomas Moestl 			*destld = totalbytesread;
1442c4a5ef6eSThomas Moestl 			*destld /= 1024 * 1024;
1443c4a5ef6eSThomas Moestl 			if (etime > 0.0)
1444c4a5ef6eSThomas Moestl 				*destld /= etime;
1445c4a5ef6eSThomas Moestl 			else
1446c4a5ef6eSThomas Moestl 				*destld = 0.0;
1447c4a5ef6eSThomas Moestl 			break;
1448c4a5ef6eSThomas Moestl 		case DSM_MB_PER_SECOND_WRITE:
1449c4a5ef6eSThomas Moestl 			*destld = totalbyteswrite;
1450c4a5ef6eSThomas Moestl 			*destld /= 1024 * 1024;
1451c4a5ef6eSThomas Moestl 			if (etime > 0.0)
1452c4a5ef6eSThomas Moestl 				*destld /= etime;
1453c4a5ef6eSThomas Moestl 			else
1454c4a5ef6eSThomas Moestl 				*destld = 0.0;
1455c4a5ef6eSThomas Moestl 			break;
145636eab1f5SPoul-Henning Kamp 		case DSM_MB_PER_SECOND_FREE:
145736eab1f5SPoul-Henning Kamp 			*destld = totalbytesfree;
145836eab1f5SPoul-Henning Kamp 			*destld /= 1024 * 1024;
145936eab1f5SPoul-Henning Kamp 			if (etime > 0.0)
146036eab1f5SPoul-Henning Kamp 				*destld /= etime;
146136eab1f5SPoul-Henning Kamp 			else
146236eab1f5SPoul-Henning Kamp 				*destld = 0.0;
146336eab1f5SPoul-Henning Kamp 			break;
1464c4a5ef6eSThomas Moestl 		case DSM_BLOCKS_PER_SECOND:
1465c4a5ef6eSThomas Moestl 			*destld = totalblocks;
1466c4a5ef6eSThomas Moestl 			if (etime > 0.0)
1467c4a5ef6eSThomas Moestl 				*destld /= etime;
1468c4a5ef6eSThomas Moestl 			else
1469c4a5ef6eSThomas Moestl 				*destld = 0.0;
1470c4a5ef6eSThomas Moestl 			break;
1471c4a5ef6eSThomas Moestl 		case DSM_BLOCKS_PER_SECOND_READ:
1472c4a5ef6eSThomas Moestl 			*destld = totalblocksread;
1473c4a5ef6eSThomas Moestl 			if (etime > 0.0)
1474c4a5ef6eSThomas Moestl 				*destld /= etime;
1475c4a5ef6eSThomas Moestl 			else
1476c4a5ef6eSThomas Moestl 				*destld = 0.0;
1477c4a5ef6eSThomas Moestl 			break;
1478c4a5ef6eSThomas Moestl 		case DSM_BLOCKS_PER_SECOND_WRITE:
1479c4a5ef6eSThomas Moestl 			*destld = totalblockswrite;
1480c4a5ef6eSThomas Moestl 			if (etime > 0.0)
1481c4a5ef6eSThomas Moestl 				*destld /= etime;
1482c4a5ef6eSThomas Moestl 			else
1483c4a5ef6eSThomas Moestl 				*destld = 0.0;
1484c4a5ef6eSThomas Moestl 			break;
148536eab1f5SPoul-Henning Kamp 		case DSM_BLOCKS_PER_SECOND_FREE:
148636eab1f5SPoul-Henning Kamp 			*destld = totalblocksfree;
148736eab1f5SPoul-Henning Kamp 			if (etime > 0.0)
148836eab1f5SPoul-Henning Kamp 				*destld /= etime;
148936eab1f5SPoul-Henning Kamp 			else
149036eab1f5SPoul-Henning Kamp 				*destld = 0.0;
149136eab1f5SPoul-Henning Kamp 			break;
1492c4a5ef6eSThomas Moestl 		/*
1493999efd90SKenneth D. Merry 		 * Some devstat callers update the duration and some don't.
1494999efd90SKenneth D. Merry 		 * So this will only be accurate if they provide the
1495999efd90SKenneth D. Merry 		 * duration.
1496c4a5ef6eSThomas Moestl 		 */
1497c4a5ef6eSThomas Moestl 		case DSM_MS_PER_TRANSACTION:
1498c4a5ef6eSThomas Moestl 			if (totaltransfers > 0) {
1499fdd6757eSMikolaj Golub 				*destld = totalduration;
1500c4a5ef6eSThomas Moestl 				*destld /= totaltransfers;
1501c4a5ef6eSThomas Moestl 				*destld *= 1000;
1502c4a5ef6eSThomas Moestl 			} else
1503c4a5ef6eSThomas Moestl 				*destld = 0.0;
1504c4a5ef6eSThomas Moestl 			break;
1505c4a5ef6eSThomas Moestl 		case DSM_MS_PER_TRANSACTION_READ:
1506c4a5ef6eSThomas Moestl 			if (totaltransfersread > 0) {
1507fdd6757eSMikolaj Golub 				*destld = totaldurationread;
1508c4a5ef6eSThomas Moestl 				*destld /= totaltransfersread;
1509c4a5ef6eSThomas Moestl 				*destld *= 1000;
1510c4a5ef6eSThomas Moestl 			} else
1511c4a5ef6eSThomas Moestl 				*destld = 0.0;
1512c4a5ef6eSThomas Moestl 			break;
1513c4a5ef6eSThomas Moestl 		case DSM_MS_PER_TRANSACTION_WRITE:
1514c4a5ef6eSThomas Moestl 			if (totaltransferswrite > 0) {
1515fdd6757eSMikolaj Golub 				*destld = totaldurationwrite;
1516c4a5ef6eSThomas Moestl 				*destld /= totaltransferswrite;
1517c4a5ef6eSThomas Moestl 				*destld *= 1000;
1518c4a5ef6eSThomas Moestl 			} else
1519c4a5ef6eSThomas Moestl 				*destld = 0.0;
1520c4a5ef6eSThomas Moestl 			break;
152136eab1f5SPoul-Henning Kamp 		case DSM_MS_PER_TRANSACTION_FREE:
152236eab1f5SPoul-Henning Kamp 			if (totaltransfersfree > 0) {
1523fdd6757eSMikolaj Golub 				*destld = totaldurationfree;
152436eab1f5SPoul-Henning Kamp 				*destld /= totaltransfersfree;
152536eab1f5SPoul-Henning Kamp 				*destld *= 1000;
152636eab1f5SPoul-Henning Kamp 			} else
152736eab1f5SPoul-Henning Kamp 				*destld = 0.0;
152836eab1f5SPoul-Henning Kamp 			break;
152936eab1f5SPoul-Henning Kamp 		case DSM_MS_PER_TRANSACTION_OTHER:
153036eab1f5SPoul-Henning Kamp 			if (totaltransfersother > 0) {
1531fdd6757eSMikolaj Golub 				*destld = totaldurationother;
153236eab1f5SPoul-Henning Kamp 				*destld /= totaltransfersother;
153336eab1f5SPoul-Henning Kamp 				*destld *= 1000;
153436eab1f5SPoul-Henning Kamp 			} else
153536eab1f5SPoul-Henning Kamp 				*destld = 0.0;
153636eab1f5SPoul-Henning Kamp 			break;
153736eab1f5SPoul-Henning Kamp 		case DSM_BUSY_PCT:
153836eab1f5SPoul-Henning Kamp 			*destld = DELTA_T(busy_time);
153936eab1f5SPoul-Henning Kamp 			if (*destld < 0)
154036eab1f5SPoul-Henning Kamp 				*destld = 0;
154136eab1f5SPoul-Henning Kamp 			*destld /= etime;
154236eab1f5SPoul-Henning Kamp 			*destld *= 100;
15431455c4e2SPoul-Henning Kamp 			if (*destld < 0)
15441455c4e2SPoul-Henning Kamp 				*destld = 0;
154536eab1f5SPoul-Henning Kamp 			break;
154636eab1f5SPoul-Henning Kamp 		case DSM_QUEUE_LENGTH:
154736eab1f5SPoul-Henning Kamp 			*destu64 = current->start_count - current->end_count;
154836eab1f5SPoul-Henning Kamp 			break;
1549fdd6757eSMikolaj Golub 		case DSM_TOTAL_DURATION:
1550fdd6757eSMikolaj Golub 			*destld = totalduration;
1551fdd6757eSMikolaj Golub 			break;
1552fdd6757eSMikolaj Golub 		case DSM_TOTAL_DURATION_READ:
1553fdd6757eSMikolaj Golub 			*destld = totaldurationread;
1554fdd6757eSMikolaj Golub 			break;
1555fdd6757eSMikolaj Golub 		case DSM_TOTAL_DURATION_WRITE:
1556fdd6757eSMikolaj Golub 			*destld = totaldurationwrite;
1557fdd6757eSMikolaj Golub 			break;
1558fdd6757eSMikolaj Golub 		case DSM_TOTAL_DURATION_FREE:
1559fdd6757eSMikolaj Golub 			*destld = totaldurationfree;
1560fdd6757eSMikolaj Golub 			break;
1561fdd6757eSMikolaj Golub 		case DSM_TOTAL_DURATION_OTHER:
1562fdd6757eSMikolaj Golub 			*destld = totaldurationother;
1563fdd6757eSMikolaj Golub 			break;
1564fdd6757eSMikolaj Golub 		case DSM_TOTAL_BUSY_TIME:
1565fdd6757eSMikolaj Golub 			*destld = DELTA_T(busy_time);
1566fdd6757eSMikolaj Golub 			break;
156736eab1f5SPoul-Henning Kamp /*
156836eab1f5SPoul-Henning Kamp  * XXX: comment out the default block to see if any case's are missing.
156936eab1f5SPoul-Henning Kamp  */
157036eab1f5SPoul-Henning Kamp #if 1
1571c4a5ef6eSThomas Moestl 		default:
1572c4a5ef6eSThomas Moestl 			/*
1573c4a5ef6eSThomas Moestl 			 * This shouldn't happen, since we should have
1574c4a5ef6eSThomas Moestl 			 * caught any out of range metrics at the top of
1575c4a5ef6eSThomas Moestl 			 * the loop.
1576c4a5ef6eSThomas Moestl 			 */
1577c4a5ef6eSThomas Moestl 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
15787d72cda3SMaxime Henrion 				 "%s: unknown metric %d", __func__, metric);
1579c4a5ef6eSThomas Moestl 			retval = -1;
1580c4a5ef6eSThomas Moestl 			goto bailout;
1581c4a5ef6eSThomas Moestl 			break; /* NOTREACHED */
158236eab1f5SPoul-Henning Kamp #endif
1583c4a5ef6eSThomas Moestl 		}
1584c4a5ef6eSThomas Moestl 	}
1585c4a5ef6eSThomas Moestl 
1586c4a5ef6eSThomas Moestl bailout:
1587c4a5ef6eSThomas Moestl 
1588c4a5ef6eSThomas Moestl 	va_end(ap);
1589c4a5ef6eSThomas Moestl 	return(retval);
1590c4a5ef6eSThomas Moestl }
1591c4a5ef6eSThomas Moestl 
1592c4a5ef6eSThomas Moestl static int
readkmem(kvm_t * kd,unsigned long addr,void * buf,size_t nbytes)1593c4a5ef6eSThomas Moestl readkmem(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes)
1594c4a5ef6eSThomas Moestl {
1595c4a5ef6eSThomas Moestl 
1596c4a5ef6eSThomas Moestl 	if (kvm_read(kd, addr, buf, nbytes) == -1) {
1597c4a5ef6eSThomas Moestl 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
15987d72cda3SMaxime Henrion 			 "%s: error reading value (kvm_read): %s", __func__,
1599c4a5ef6eSThomas Moestl 			 kvm_geterr(kd));
1600c4a5ef6eSThomas Moestl 		return(-1);
1601c4a5ef6eSThomas Moestl 	}
1602c4a5ef6eSThomas Moestl 	return(0);
1603c4a5ef6eSThomas Moestl }
1604c4a5ef6eSThomas Moestl 
1605c4a5ef6eSThomas Moestl static int
readkmem_nl(kvm_t * kd,const char * name,void * buf,size_t nbytes)1606c3508206SKenneth D. Merry readkmem_nl(kvm_t *kd, const char *name, void *buf, size_t nbytes)
1607c4a5ef6eSThomas Moestl {
1608c3508206SKenneth D. Merry 	struct nlist nl[2];
1609c3508206SKenneth D. Merry 
1610f664b76fSAlexander Kabaev 	nl[0].n_name = (char *)name;
1611c3508206SKenneth D. Merry 	nl[1].n_name = NULL;
1612c4a5ef6eSThomas Moestl 
1613c4a5ef6eSThomas Moestl 	if (kvm_nlist(kd, nl) == -1) {
1614c4a5ef6eSThomas Moestl 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1615c4a5ef6eSThomas Moestl 			 "%s: error getting name list (kvm_nlist): %s",
16167d72cda3SMaxime Henrion 			 __func__, kvm_geterr(kd));
1617c4a5ef6eSThomas Moestl 		return(-1);
1618c4a5ef6eSThomas Moestl 	}
1619c4a5ef6eSThomas Moestl 	return(readkmem(kd, nl[0].n_value, buf, nbytes));
1620c4a5ef6eSThomas Moestl }
1621c4a5ef6eSThomas Moestl 
1622c4a5ef6eSThomas Moestl /*
1623c4a5ef6eSThomas Moestl  * This duplicates the functionality of the kernel sysctl handler for poking
1624c4a5ef6eSThomas Moestl  * through crash dumps.
1625c4a5ef6eSThomas Moestl  */
1626c4a5ef6eSThomas Moestl static char *
get_devstat_kvm(kvm_t * kd)1627c4a5ef6eSThomas Moestl get_devstat_kvm(kvm_t *kd)
1628c4a5ef6eSThomas Moestl {
16299dbcd4b0SStefan Farfeleder 	int i, wp;
1630c4a5ef6eSThomas Moestl 	long gen;
1631c4a5ef6eSThomas Moestl 	struct devstat *nds;
1632c4a5ef6eSThomas Moestl 	struct devstat ds;
1633c4a5ef6eSThomas Moestl 	struct devstatlist dhead;
1634c4a5ef6eSThomas Moestl 	int num_devs;
1635c4a5ef6eSThomas Moestl 	char *rv = NULL;
1636c4a5ef6eSThomas Moestl 
16377194d335SPoul-Henning Kamp 	if ((num_devs = devstat_getnumdevs(kd)) <= 0)
1638c4a5ef6eSThomas Moestl 		return(NULL);
1639c4a5ef6eSThomas Moestl 	if (KREADNL(kd, X_DEVICE_STATQ, dhead) == -1)
1640c4a5ef6eSThomas Moestl 		return(NULL);
1641c4a5ef6eSThomas Moestl 
1642c4a5ef6eSThomas Moestl 	nds = STAILQ_FIRST(&dhead);
1643c4a5ef6eSThomas Moestl 
1644c4a5ef6eSThomas Moestl 	if ((rv = malloc(sizeof(gen))) == NULL) {
1645c4a5ef6eSThomas Moestl 		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1646c4a5ef6eSThomas Moestl 			 "%s: out of memory (initial malloc failed)",
16477d72cda3SMaxime Henrion 			 __func__);
1648c4a5ef6eSThomas Moestl 		return(NULL);
1649c4a5ef6eSThomas Moestl 	}
16507194d335SPoul-Henning Kamp 	gen = devstat_getgeneration(kd);
1651c4a5ef6eSThomas Moestl 	memcpy(rv, &gen, sizeof(gen));
1652c4a5ef6eSThomas Moestl 	wp = sizeof(gen);
1653c4a5ef6eSThomas Moestl 	/*
1654c4a5ef6eSThomas Moestl 	 * Now push out all the devices.
1655c4a5ef6eSThomas Moestl 	 */
1656c4a5ef6eSThomas Moestl 	for (i = 0; (nds != NULL) && (i < num_devs);
1657c4a5ef6eSThomas Moestl 	     nds = STAILQ_NEXT(nds, dev_links), i++) {
1658c4a5ef6eSThomas Moestl 		if (readkmem(kd, (long)nds, &ds, sizeof(ds)) == -1) {
1659c4a5ef6eSThomas Moestl 			free(rv);
1660c4a5ef6eSThomas Moestl 			return(NULL);
1661c4a5ef6eSThomas Moestl 		}
1662c4a5ef6eSThomas Moestl 		nds = &ds;
1663c4a5ef6eSThomas Moestl 		rv = (char *)reallocf(rv, sizeof(gen) +
1664c4a5ef6eSThomas Moestl 				      sizeof(ds) * (i + 1));
1665c4a5ef6eSThomas Moestl 		if (rv == NULL) {
1666c4a5ef6eSThomas Moestl 			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
1667c4a5ef6eSThomas Moestl 				 "%s: out of memory (malloc failed)",
16687d72cda3SMaxime Henrion 				 __func__);
1669c4a5ef6eSThomas Moestl 			return(NULL);
1670c4a5ef6eSThomas Moestl 		}
1671c4a5ef6eSThomas Moestl 		memcpy(rv + wp, &ds, sizeof(ds));
1672c4a5ef6eSThomas Moestl 		wp += sizeof(ds);
1673c4a5ef6eSThomas Moestl 	}
1674c4a5ef6eSThomas Moestl 	return(rv);
1675c4a5ef6eSThomas Moestl }
1676