128fb27baSJustin T. Gibbs /* 228fb27baSJustin T. Gibbs * Copyright (c) 1997, 1998 Kenneth D. Merry. 328fb27baSJustin T. Gibbs * All rights reserved. 428fb27baSJustin T. Gibbs * 528fb27baSJustin T. Gibbs * Redistribution and use in source and binary forms, with or without 628fb27baSJustin T. Gibbs * modification, are permitted provided that the following conditions 728fb27baSJustin T. Gibbs * are met: 828fb27baSJustin T. Gibbs * 1. Redistributions of source code must retain the above copyright 928fb27baSJustin T. Gibbs * notice, this list of conditions and the following disclaimer. 1028fb27baSJustin T. Gibbs * 2. Redistributions in binary form must reproduce the above copyright 1128fb27baSJustin T. Gibbs * notice, this list of conditions and the following disclaimer in the 1228fb27baSJustin T. Gibbs * documentation and/or other materials provided with the distribution. 1328fb27baSJustin T. Gibbs * 3. The name of the author may not be used to endorse or promote products 1428fb27baSJustin T. Gibbs * derived from this software without specific prior written permission. 1528fb27baSJustin T. Gibbs * 1628fb27baSJustin T. Gibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1728fb27baSJustin T. Gibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1828fb27baSJustin T. Gibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1928fb27baSJustin T. Gibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2028fb27baSJustin T. Gibbs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2128fb27baSJustin T. Gibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2228fb27baSJustin T. Gibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2328fb27baSJustin T. Gibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2428fb27baSJustin T. Gibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2528fb27baSJustin T. Gibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2628fb27baSJustin T. Gibbs * SUCH DAMAGE. 2728fb27baSJustin T. Gibbs */ 2828fb27baSJustin T. Gibbs 29e67f5b9fSMatthew Dillon #include <sys/cdefs.h> 30e67f5b9fSMatthew Dillon __FBSDID("$FreeBSD$"); 31e67f5b9fSMatthew Dillon 3228fb27baSJustin T. Gibbs #include <sys/types.h> 3328fb27baSJustin T. Gibbs #include <sys/sysctl.h> 3428fb27baSJustin T. Gibbs #include <sys/errno.h> 3512590690SPoul-Henning Kamp #include <sys/resource.h> 36c4a5ef6eSThomas Moestl #include <sys/queue.h> 3728fb27baSJustin T. Gibbs 38eded794aSKenneth D. Merry #include <ctype.h> 3928fb27baSJustin T. Gibbs #include <err.h> 40c4a5ef6eSThomas Moestl #include <fcntl.h> 41c4a5ef6eSThomas Moestl #include <limits.h> 4228fb27baSJustin T. Gibbs #include <stdio.h> 4328fb27baSJustin T. Gibbs #include <stdlib.h> 4428fb27baSJustin T. Gibbs #include <string.h> 45c4a5ef6eSThomas Moestl #include <stdarg.h> 46c4a5ef6eSThomas Moestl #include <kvm.h> 4728fb27baSJustin T. Gibbs 4828fb27baSJustin T. Gibbs #include "devstat.h" 4928fb27baSJustin T. Gibbs 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}, 7728fb27baSJustin T. Gibbs {"worm", DEVSTAT_TYPE_WORM, DEVSTAT_MATCH_TYPE}, 7828fb27baSJustin T. Gibbs {"sa", DEVSTAT_TYPE_SEQUENTIAL,DEVSTAT_MATCH_TYPE}, 7928fb27baSJustin T. Gibbs {"pass", DEVSTAT_TYPE_PASS, DEVSTAT_MATCH_PASS}, 8028fb27baSJustin T. Gibbs {"optical", DEVSTAT_TYPE_OPTICAL, DEVSTAT_MATCH_TYPE}, 8128fb27baSJustin T. Gibbs {"array", DEVSTAT_TYPE_STORARRAY, DEVSTAT_MATCH_TYPE}, 8228fb27baSJustin T. Gibbs {"changer", DEVSTAT_TYPE_CHANGER, DEVSTAT_MATCH_TYPE}, 8328fb27baSJustin T. Gibbs {"scanner", DEVSTAT_TYPE_SCANNER, DEVSTAT_MATCH_TYPE}, 8428fb27baSJustin T. Gibbs {"printer", DEVSTAT_TYPE_PRINTER, DEVSTAT_MATCH_TYPE}, 8528fb27baSJustin T. Gibbs {"floppy", DEVSTAT_TYPE_FLOPPY, DEVSTAT_MATCH_TYPE}, 8628fb27baSJustin T. Gibbs {"proc", DEVSTAT_TYPE_PROCESSOR, DEVSTAT_MATCH_TYPE}, 8728fb27baSJustin T. Gibbs {"comm", DEVSTAT_TYPE_COMM, DEVSTAT_MATCH_TYPE}, 8828fb27baSJustin T. Gibbs {"enclosure", DEVSTAT_TYPE_ENCLOSURE, DEVSTAT_MATCH_TYPE}, 8928fb27baSJustin T. Gibbs {NULL, 0, 0} 9028fb27baSJustin T. Gibbs }; 9128fb27baSJustin T. Gibbs 92c4a5ef6eSThomas Moestl struct devstat_args { 93c4a5ef6eSThomas Moestl devstat_metric metric; 94c4a5ef6eSThomas Moestl devstat_arg_type argtype; 95c4a5ef6eSThomas Moestl } devstat_arg_list[] = { 96c4a5ef6eSThomas Moestl { DSM_NONE, DEVSTAT_ARG_NOTYPE }, 97c4a5ef6eSThomas Moestl { DSM_TOTAL_BYTES, DEVSTAT_ARG_UINT64 }, 98c4a5ef6eSThomas Moestl { DSM_TOTAL_BYTES_READ, DEVSTAT_ARG_UINT64 }, 99c4a5ef6eSThomas Moestl { DSM_TOTAL_BYTES_WRITE, DEVSTAT_ARG_UINT64 }, 100c4a5ef6eSThomas Moestl { DSM_TOTAL_TRANSFERS, DEVSTAT_ARG_UINT64 }, 101c4a5ef6eSThomas Moestl { DSM_TOTAL_TRANSFERS_READ, DEVSTAT_ARG_UINT64 }, 102c4a5ef6eSThomas Moestl { DSM_TOTAL_TRANSFERS_WRITE, DEVSTAT_ARG_UINT64 }, 103773865deSPoul-Henning Kamp { DSM_TOTAL_TRANSFERS_OTHER, DEVSTAT_ARG_UINT64 }, 104c4a5ef6eSThomas Moestl { DSM_TOTAL_BLOCKS, DEVSTAT_ARG_UINT64 }, 105c4a5ef6eSThomas Moestl { DSM_TOTAL_BLOCKS_READ, DEVSTAT_ARG_UINT64 }, 106c4a5ef6eSThomas Moestl { DSM_TOTAL_BLOCKS_WRITE, DEVSTAT_ARG_UINT64 }, 107c4a5ef6eSThomas Moestl { DSM_KB_PER_TRANSFER, DEVSTAT_ARG_LD }, 108c4a5ef6eSThomas Moestl { DSM_KB_PER_TRANSFER_READ, DEVSTAT_ARG_LD }, 109c4a5ef6eSThomas Moestl { DSM_KB_PER_TRANSFER_WRITE, DEVSTAT_ARG_LD }, 110c4a5ef6eSThomas Moestl { DSM_TRANSFERS_PER_SECOND, DEVSTAT_ARG_LD }, 111c4a5ef6eSThomas Moestl { DSM_TRANSFERS_PER_SECOND_READ, DEVSTAT_ARG_LD }, 112c4a5ef6eSThomas Moestl { DSM_TRANSFERS_PER_SECOND_WRITE, DEVSTAT_ARG_LD }, 113c4a5ef6eSThomas Moestl { DSM_TRANSFERS_PER_SECOND_OTHER, DEVSTAT_ARG_LD }, 114c4a5ef6eSThomas Moestl { DSM_MB_PER_SECOND, DEVSTAT_ARG_LD }, 115c4a5ef6eSThomas Moestl { DSM_MB_PER_SECOND_READ, DEVSTAT_ARG_LD }, 116c4a5ef6eSThomas Moestl { DSM_MB_PER_SECOND_WRITE, DEVSTAT_ARG_LD }, 117c4a5ef6eSThomas Moestl { DSM_BLOCKS_PER_SECOND, DEVSTAT_ARG_LD }, 118c4a5ef6eSThomas Moestl { DSM_BLOCKS_PER_SECOND_READ, DEVSTAT_ARG_LD }, 119c4a5ef6eSThomas Moestl { DSM_BLOCKS_PER_SECOND_WRITE, DEVSTAT_ARG_LD }, 120c4a5ef6eSThomas Moestl { DSM_MS_PER_TRANSACTION, DEVSTAT_ARG_LD }, 121c4a5ef6eSThomas Moestl { DSM_MS_PER_TRANSACTION_READ, DEVSTAT_ARG_LD }, 122884539f7SKenneth D. Merry { DSM_MS_PER_TRANSACTION_WRITE, DEVSTAT_ARG_LD }, 123ec0fa09cSPoul-Henning Kamp { DSM_SKIP, DEVSTAT_ARG_SKIP }, 124773865deSPoul-Henning Kamp { DSM_TOTAL_BYTES_FREE, DEVSTAT_ARG_UINT64 }, 125773865deSPoul-Henning Kamp { DSM_TOTAL_TRANSFERS_FREE, DEVSTAT_ARG_UINT64 }, 126773865deSPoul-Henning Kamp { DSM_TOTAL_BLOCKS_FREE, DEVSTAT_ARG_UINT64 }, 127773865deSPoul-Henning Kamp { DSM_KB_PER_TRANSFER_FREE, DEVSTAT_ARG_LD }, 128773865deSPoul-Henning Kamp { DSM_MB_PER_SECOND_FREE, DEVSTAT_ARG_LD }, 129773865deSPoul-Henning Kamp { DSM_TRANSFERS_PER_SECOND_FREE, DEVSTAT_ARG_LD }, 130773865deSPoul-Henning Kamp { DSM_BLOCKS_PER_SECOND_FREE, DEVSTAT_ARG_LD }, 13136eab1f5SPoul-Henning Kamp { DSM_MS_PER_TRANSACTION_OTHER, DEVSTAT_ARG_LD }, 132773865deSPoul-Henning Kamp { DSM_MS_PER_TRANSACTION_FREE, DEVSTAT_ARG_LD }, 13336eab1f5SPoul-Henning Kamp { DSM_BUSY_PCT, DEVSTAT_ARG_LD }, 13436eab1f5SPoul-Henning Kamp { DSM_QUEUE_LENGTH, DEVSTAT_ARG_UINT64 }, 135c4a5ef6eSThomas Moestl }; 136c4a5ef6eSThomas Moestl 137c3508206SKenneth D. Merry static const char *namelist[] = { 138c4a5ef6eSThomas Moestl #define X_NUMDEVS 0 139c4a5ef6eSThomas Moestl "_devstat_num_devs", 140c4a5ef6eSThomas Moestl #define X_GENERATION 1 141c4a5ef6eSThomas Moestl "_devstat_generation", 142c4a5ef6eSThomas Moestl #define X_VERSION 2 143c4a5ef6eSThomas Moestl "_devstat_version", 144c4a5ef6eSThomas Moestl #define X_DEVICE_STATQ 3 145c4a5ef6eSThomas Moestl "_device_statq", 146c4a5ef6eSThomas Moestl #define X_END 4 147c4a5ef6eSThomas Moestl }; 148c4a5ef6eSThomas Moestl 14928fb27baSJustin T. Gibbs /* 15028fb27baSJustin T. Gibbs * Local function declarations. 15128fb27baSJustin T. Gibbs */ 15228fb27baSJustin T. Gibbs static int compare_select(const void *arg1, const void *arg2); 153c4a5ef6eSThomas Moestl static int readkmem(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes); 154c3508206SKenneth D. Merry static int readkmem_nl(kvm_t *kd, const char *name, void *buf, size_t nbytes); 155c4a5ef6eSThomas Moestl static char *get_devstat_kvm(kvm_t *kd); 156c4a5ef6eSThomas Moestl 157c4a5ef6eSThomas Moestl #define KREADNL(kd, var, val) \ 158c4a5ef6eSThomas Moestl readkmem_nl(kd, namelist[var], &val, sizeof(val)) 15928fb27baSJustin T. Gibbs 16028fb27baSJustin T. Gibbs int 161c4a5ef6eSThomas Moestl devstat_getnumdevs(kvm_t *kd) 16228fb27baSJustin T. Gibbs { 16328fb27baSJustin T. Gibbs size_t numdevsize; 16428fb27baSJustin T. Gibbs int numdevs; 165c3508206SKenneth D. Merry const char *func_name = "devstat_getnumdevs"; 16628fb27baSJustin T. Gibbs 16728fb27baSJustin T. Gibbs numdevsize = sizeof(int); 16828fb27baSJustin T. Gibbs 16928fb27baSJustin T. Gibbs /* 17028fb27baSJustin T. Gibbs * Find out how many devices we have in the system. 17128fb27baSJustin T. Gibbs */ 172c4a5ef6eSThomas Moestl if (kd == NULL) { 17328fb27baSJustin T. Gibbs if (sysctlbyname("kern.devstat.numdevs", &numdevs, 17428fb27baSJustin T. Gibbs &numdevsize, NULL, 0) == -1) { 175c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 176c4a5ef6eSThomas Moestl "%s: error getting number of devices\n" 177c4a5ef6eSThomas Moestl "%s: %s", func_name, func_name, 178c4a5ef6eSThomas Moestl strerror(errno)); 17928fb27baSJustin T. Gibbs return(-1); 18028fb27baSJustin T. Gibbs } else 18128fb27baSJustin T. Gibbs return(numdevs); 182c4a5ef6eSThomas Moestl } else { 183c3508206SKenneth D. Merry 184c4a5ef6eSThomas Moestl if (KREADNL(kd, X_NUMDEVS, numdevs) == -1) 185c4a5ef6eSThomas Moestl return(-1); 186c4a5ef6eSThomas Moestl else 187c4a5ef6eSThomas Moestl return(numdevs); 188c4a5ef6eSThomas Moestl } 18928fb27baSJustin T. Gibbs } 19028fb27baSJustin T. Gibbs 19128fb27baSJustin T. Gibbs /* 19228fb27baSJustin T. Gibbs * This is an easy way to get the generation number, but the generation is 19328fb27baSJustin T. Gibbs * supplied in a more atmoic manner by the kern.devstat.all sysctl. 19428fb27baSJustin T. Gibbs * Because this generation sysctl is separate from the statistics sysctl, 19528fb27baSJustin T. Gibbs * the device list and the generation could change between the time that 19628fb27baSJustin T. Gibbs * this function is called and the device list is retreived. 19728fb27baSJustin T. Gibbs */ 198bcc6a3daSKenneth D. Merry long 199c4a5ef6eSThomas Moestl devstat_getgeneration(kvm_t *kd) 20028fb27baSJustin T. Gibbs { 20128fb27baSJustin T. Gibbs size_t gensize; 202bcc6a3daSKenneth D. Merry long generation; 203c3508206SKenneth D. Merry const char *func_name = "devstat_getgeneration"; 20428fb27baSJustin T. Gibbs 205bcc6a3daSKenneth D. Merry gensize = sizeof(long); 20628fb27baSJustin T. Gibbs 20728fb27baSJustin T. Gibbs /* 20828fb27baSJustin T. Gibbs * Get the current generation number. 20928fb27baSJustin T. Gibbs */ 210c4a5ef6eSThomas Moestl if (kd == NULL) { 21128fb27baSJustin T. Gibbs if (sysctlbyname("kern.devstat.generation", &generation, 21228fb27baSJustin T. Gibbs &gensize, NULL, 0) == -1) { 213c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 214c4a5ef6eSThomas Moestl "%s: error getting devstat generation\n%s: %s", 215c4a5ef6eSThomas Moestl func_name, func_name, strerror(errno)); 21628fb27baSJustin T. Gibbs return(-1); 21728fb27baSJustin T. Gibbs } else 21828fb27baSJustin T. Gibbs return(generation); 219c4a5ef6eSThomas Moestl } else { 220c4a5ef6eSThomas Moestl if (KREADNL(kd, X_GENERATION, generation) == -1) 221c4a5ef6eSThomas Moestl return(-1); 222c4a5ef6eSThomas Moestl else 223c4a5ef6eSThomas Moestl return(generation); 224c4a5ef6eSThomas Moestl } 22528fb27baSJustin T. Gibbs } 22628fb27baSJustin T. Gibbs 22728fb27baSJustin T. Gibbs /* 22828fb27baSJustin T. Gibbs * Get the current devstat version. The return value of this function 22928fb27baSJustin T. Gibbs * should be compared with DEVSTAT_VERSION, which is defined in 23028fb27baSJustin T. Gibbs * sys/devicestat.h. This will enable userland programs to determine 23128fb27baSJustin T. Gibbs * whether they are out of sync with the kernel. 23228fb27baSJustin T. Gibbs */ 23328fb27baSJustin T. Gibbs int 234c4a5ef6eSThomas Moestl devstat_getversion(kvm_t *kd) 23528fb27baSJustin T. Gibbs { 23628fb27baSJustin T. Gibbs size_t versize; 23728fb27baSJustin T. Gibbs int version; 238c3508206SKenneth D. Merry const char *func_name = "devstat_getversion"; 23928fb27baSJustin T. Gibbs 24028fb27baSJustin T. Gibbs versize = sizeof(int); 24128fb27baSJustin T. Gibbs 24228fb27baSJustin T. Gibbs /* 24328fb27baSJustin T. Gibbs * Get the current devstat version. 24428fb27baSJustin T. Gibbs */ 245c4a5ef6eSThomas Moestl if (kd == NULL) { 24628fb27baSJustin T. Gibbs if (sysctlbyname("kern.devstat.version", &version, &versize, 24728fb27baSJustin T. Gibbs NULL, 0) == -1) { 248c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 249c4a5ef6eSThomas Moestl "%s: error getting devstat version\n%s: %s", 250c4a5ef6eSThomas Moestl func_name, func_name, strerror(errno)); 25128fb27baSJustin T. Gibbs return(-1); 25228fb27baSJustin T. Gibbs } else 25328fb27baSJustin T. Gibbs return(version); 254c4a5ef6eSThomas Moestl } else { 255c4a5ef6eSThomas Moestl if (KREADNL(kd, X_VERSION, version) == -1) 256c4a5ef6eSThomas Moestl return(-1); 257c4a5ef6eSThomas Moestl else 258c4a5ef6eSThomas Moestl return(version); 259c4a5ef6eSThomas Moestl } 26028fb27baSJustin T. Gibbs } 26128fb27baSJustin T. Gibbs 26228fb27baSJustin T. Gibbs /* 26328fb27baSJustin T. Gibbs * Check the devstat version we know about against the devstat version the 26428fb27baSJustin T. Gibbs * kernel knows about. If they don't match, print an error into the 26528fb27baSJustin T. Gibbs * devstat error buffer, and return -1. If they match, return 0. 26628fb27baSJustin T. Gibbs */ 26728fb27baSJustin T. Gibbs int 268c4a5ef6eSThomas Moestl devstat_checkversion(kvm_t *kd) 26928fb27baSJustin T. Gibbs { 270c3508206SKenneth D. Merry const char *func_name = "devstat_checkversion"; 2716d3e1426SBrian Somers int buflen, res, retval = 0, version; 27228fb27baSJustin T. Gibbs 273c4a5ef6eSThomas Moestl version = devstat_getversion(kd); 274eded794aSKenneth D. Merry 275eded794aSKenneth D. Merry if (version != DEVSTAT_VERSION) { 276eded794aSKenneth D. Merry /* 2776d3e1426SBrian Somers * If getversion() returns an error (i.e. -1), then it 278eded794aSKenneth D. Merry * has printed an error message in the buffer. Therefore, 279eded794aSKenneth D. Merry * we need to add a \n to the end of that message before we 280eded794aSKenneth D. Merry * print our own message in the buffer. 281eded794aSKenneth D. Merry */ 2826d3e1426SBrian Somers if (version == -1) 283eded794aSKenneth D. Merry buflen = strlen(devstat_errbuf); 2846d3e1426SBrian Somers else 2856d3e1426SBrian Somers buflen = 0; 286eded794aSKenneth D. Merry 2876d3e1426SBrian Somers res = snprintf(devstat_errbuf + buflen, 2886d3e1426SBrian Somers DEVSTAT_ERRBUF_SIZE - buflen, 2896d3e1426SBrian Somers "%s%s: userland devstat version %d is not " 2904fb9d384SKenneth D. Merry "the same as the kernel\n%s: devstat " 2914fb9d384SKenneth D. Merry "version %d\n", version == -1 ? "\n" : "", 2924fb9d384SKenneth D. Merry func_name, DEVSTAT_VERSION, func_name, version); 29328fb27baSJustin T. Gibbs 2946d3e1426SBrian Somers if (res < 0) 2956d3e1426SBrian Somers devstat_errbuf[buflen] = '\0'; 296eded794aSKenneth D. Merry 2976d3e1426SBrian Somers buflen = strlen(devstat_errbuf); 298eded794aSKenneth D. Merry if (version < DEVSTAT_VERSION) 2996d3e1426SBrian Somers res = snprintf(devstat_errbuf + buflen, 3006d3e1426SBrian Somers DEVSTAT_ERRBUF_SIZE - buflen, 3014fb9d384SKenneth D. Merry "%s: libdevstat newer than kernel\n", 3024fb9d384SKenneth D. Merry func_name); 30328fb27baSJustin T. Gibbs else 3046d3e1426SBrian Somers res = snprintf(devstat_errbuf + buflen, 3056d3e1426SBrian Somers DEVSTAT_ERRBUF_SIZE - buflen, 3064fb9d384SKenneth D. Merry "%s: kernel newer than libdevstat\n", 3074fb9d384SKenneth D. Merry func_name); 30828fb27baSJustin T. Gibbs 3096d3e1426SBrian Somers if (res < 0) 3106d3e1426SBrian Somers devstat_errbuf[buflen] = '\0'; 31128fb27baSJustin T. Gibbs 31228fb27baSJustin T. Gibbs retval = -1; 31328fb27baSJustin T. Gibbs } 31428fb27baSJustin T. Gibbs 31528fb27baSJustin T. Gibbs return(retval); 31628fb27baSJustin T. Gibbs } 31728fb27baSJustin T. Gibbs 31828fb27baSJustin T. Gibbs /* 31928fb27baSJustin T. Gibbs * Get the current list of devices and statistics, and the current 32028fb27baSJustin T. Gibbs * generation number. 32128fb27baSJustin T. Gibbs * 32228fb27baSJustin T. Gibbs * Return values: 32328fb27baSJustin T. Gibbs * -1 -- error 32428fb27baSJustin T. Gibbs * 0 -- device list is unchanged 32528fb27baSJustin T. Gibbs * 1 -- device list has changed 32628fb27baSJustin T. Gibbs */ 32728fb27baSJustin T. Gibbs int 328c4a5ef6eSThomas Moestl devstat_getdevs(kvm_t *kd, struct statinfo *stats) 32928fb27baSJustin T. Gibbs { 33028fb27baSJustin T. Gibbs int error; 33128fb27baSJustin T. Gibbs size_t dssize; 332bcc6a3daSKenneth D. Merry int oldnumdevs; 333bcc6a3daSKenneth D. Merry long oldgeneration; 33428fb27baSJustin T. Gibbs int retval = 0; 33528fb27baSJustin T. Gibbs struct devinfo *dinfo; 336c3508206SKenneth D. Merry const char *func_name = "devstat_getdevs"; 3377194d335SPoul-Henning Kamp struct timespec ts; 33828fb27baSJustin T. Gibbs 33928fb27baSJustin T. Gibbs dinfo = stats->dinfo; 34028fb27baSJustin T. Gibbs 34128fb27baSJustin T. Gibbs if (dinfo == NULL) { 342c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 343c4a5ef6eSThomas Moestl "%s: stats->dinfo was NULL", func_name); 34428fb27baSJustin T. Gibbs return(-1); 34528fb27baSJustin T. Gibbs } 34628fb27baSJustin T. Gibbs 34728fb27baSJustin T. Gibbs oldnumdevs = dinfo->numdevs; 34828fb27baSJustin T. Gibbs oldgeneration = dinfo->generation; 34928fb27baSJustin T. Gibbs 3507194d335SPoul-Henning Kamp clock_gettime(CLOCK_MONOTONIC, &ts); 3517194d335SPoul-Henning Kamp stats->snap_time = ts.tv_sec + ts.tv_nsec * 1e-9; 352c4a5ef6eSThomas Moestl 353c4a5ef6eSThomas Moestl if (kd == NULL) { 354c4a5ef6eSThomas Moestl /* If this is our first time through, mem_ptr will be null. */ 35528fb27baSJustin T. Gibbs if (dinfo->mem_ptr == NULL) { 35628fb27baSJustin T. Gibbs /* 35728fb27baSJustin T. Gibbs * Get the number of devices. If it's negative, it's an 35828fb27baSJustin T. Gibbs * error. Don't bother setting the error string, since 35928fb27baSJustin T. Gibbs * getnumdevs() has already done that for us. 36028fb27baSJustin T. Gibbs */ 36116830b0cSPoul-Henning Kamp if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0) 36228fb27baSJustin T. Gibbs return(-1); 36328fb27baSJustin T. Gibbs 36428fb27baSJustin T. Gibbs /* 365c4a5ef6eSThomas Moestl * The kern.devstat.all sysctl returns the current 366c4a5ef6eSThomas Moestl * generation number, as well as all the devices. 367c4a5ef6eSThomas Moestl * So we need four bytes more. 36828fb27baSJustin T. Gibbs */ 369c4a5ef6eSThomas Moestl dssize = (dinfo->numdevs * sizeof(struct devstat)) + 370c4a5ef6eSThomas Moestl sizeof(long); 37128fb27baSJustin T. Gibbs dinfo->mem_ptr = (u_int8_t *)malloc(dssize); 37228fb27baSJustin T. Gibbs } else 373c4a5ef6eSThomas Moestl dssize = (dinfo->numdevs * sizeof(struct devstat)) + 374c4a5ef6eSThomas Moestl sizeof(long); 37528fb27baSJustin T. Gibbs 37628fb27baSJustin T. Gibbs /* 37728fb27baSJustin T. Gibbs * Request all of the devices. We only really allow for one 378c4a5ef6eSThomas Moestl * ENOMEM failure. It would, of course, be possible to just go 379c4a5ef6eSThomas Moestl * in a loop and keep reallocing the device structure until we 380c4a5ef6eSThomas Moestl * don't get ENOMEM back. I'm not sure it's worth it, though. 381c4a5ef6eSThomas Moestl * If devices are being added to the system that quickly, maybe 382c4a5ef6eSThomas Moestl * the user can just wait until all devices are added. 38328fb27baSJustin T. Gibbs */ 38436eab1f5SPoul-Henning Kamp for (;;) { 38536eab1f5SPoul-Henning Kamp error = sysctlbyname("kern.devstat.all", 38636eab1f5SPoul-Henning Kamp dinfo->mem_ptr, 38736eab1f5SPoul-Henning Kamp &dssize, NULL, 0); 38836eab1f5SPoul-Henning Kamp if (error != -1 || errno != EBUSY) 38936eab1f5SPoul-Henning Kamp break; 39036eab1f5SPoul-Henning Kamp } 39136eab1f5SPoul-Henning Kamp if (error == -1) { 39228fb27baSJustin T. Gibbs /* 39328fb27baSJustin T. Gibbs * If we get ENOMEM back, that means that there are 39428fb27baSJustin T. Gibbs * more devices now, so we need to allocate more 39528fb27baSJustin T. Gibbs * space for the device array. 39628fb27baSJustin T. Gibbs */ 39728fb27baSJustin T. Gibbs if (errno == ENOMEM) { 39828fb27baSJustin T. Gibbs /* 399c4a5ef6eSThomas Moestl * No need to set the error string here, 4007194d335SPoul-Henning Kamp * devstat_getnumdevs() will do that if it fails. 40128fb27baSJustin T. Gibbs */ 40216830b0cSPoul-Henning Kamp if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0) 40328fb27baSJustin T. Gibbs return(-1); 40428fb27baSJustin T. Gibbs 405c4a5ef6eSThomas Moestl dssize = (dinfo->numdevs * 406c4a5ef6eSThomas Moestl sizeof(struct devstat)) + sizeof(long); 407c4a5ef6eSThomas Moestl dinfo->mem_ptr = (u_int8_t *) 408c4a5ef6eSThomas Moestl realloc(dinfo->mem_ptr, dssize); 40928fb27baSJustin T. Gibbs if ((error = sysctlbyname("kern.devstat.all", 41028fb27baSJustin T. Gibbs dinfo->mem_ptr, &dssize, NULL, 0)) == -1) { 411c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, 412c4a5ef6eSThomas Moestl sizeof(devstat_errbuf), 413c4a5ef6eSThomas Moestl "%s: error getting device " 414c4a5ef6eSThomas Moestl "stats\n%s: %s", func_name, 415c4a5ef6eSThomas Moestl func_name, strerror(errno)); 41628fb27baSJustin T. Gibbs return(-1); 41728fb27baSJustin T. Gibbs } 41828fb27baSJustin T. Gibbs } else { 419c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 42028fb27baSJustin T. Gibbs "%s: error getting device stats\n" 42128fb27baSJustin T. Gibbs "%s: %s", func_name, func_name, 42228fb27baSJustin T. Gibbs strerror(errno)); 42328fb27baSJustin T. Gibbs return(-1); 42428fb27baSJustin T. Gibbs } 42528fb27baSJustin T. Gibbs } 42628fb27baSJustin T. Gibbs 427c4a5ef6eSThomas Moestl } else { 428c4a5ef6eSThomas Moestl /* 429c4a5ef6eSThomas Moestl * This is of course non-atomic, but since we are working 430c4a5ef6eSThomas Moestl * on a core dump, the generation is unlikely to change 431c4a5ef6eSThomas Moestl */ 43216830b0cSPoul-Henning Kamp if ((dinfo->numdevs = devstat_getnumdevs(kd)) == -1) 433c4a5ef6eSThomas Moestl return(-1); 434c4a5ef6eSThomas Moestl if ((dinfo->mem_ptr = get_devstat_kvm(kd)) == NULL) 435c4a5ef6eSThomas Moestl return(-1); 436c4a5ef6eSThomas Moestl } 43728fb27baSJustin T. Gibbs /* 43828fb27baSJustin T. Gibbs * The sysctl spits out the generation as the first four bytes, 43928fb27baSJustin T. Gibbs * then all of the device statistics structures. 44028fb27baSJustin T. Gibbs */ 441bcc6a3daSKenneth D. Merry dinfo->generation = *(long *)dinfo->mem_ptr; 44228fb27baSJustin T. Gibbs 44328fb27baSJustin T. Gibbs /* 44428fb27baSJustin T. Gibbs * If the generation has changed, and if the current number of 44528fb27baSJustin T. Gibbs * devices is not the same as the number of devices recorded in the 44628fb27baSJustin T. Gibbs * devinfo structure, it is likely that the device list has shrunk. 44728fb27baSJustin T. Gibbs * The reason that it is likely that the device list has shrunk in 44828fb27baSJustin T. Gibbs * this case is that if the device list has grown, the sysctl above 44928fb27baSJustin T. Gibbs * will return an ENOMEM error, and we will reset the number of 45028fb27baSJustin T. Gibbs * devices and reallocate the device array. If the second sysctl 45128fb27baSJustin T. Gibbs * fails, we will return an error and therefore never get to this 45228fb27baSJustin T. Gibbs * point. If the device list has shrunk, the sysctl will not 45328fb27baSJustin T. Gibbs * return an error since we have more space allocated than is 45428fb27baSJustin T. Gibbs * necessary. So, in the shrinkage case, we catch it here and 45528fb27baSJustin T. Gibbs * reallocate the array so that we don't use any more space than is 45628fb27baSJustin T. Gibbs * necessary. 45728fb27baSJustin T. Gibbs */ 45828fb27baSJustin T. Gibbs if (oldgeneration != dinfo->generation) { 45916830b0cSPoul-Henning Kamp if (devstat_getnumdevs(kd) != dinfo->numdevs) { 46016830b0cSPoul-Henning Kamp if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0) 46128fb27baSJustin T. Gibbs return(-1); 46228fb27baSJustin T. Gibbs dssize = (dinfo->numdevs * sizeof(struct devstat)) + 463bcc6a3daSKenneth D. Merry sizeof(long); 46428fb27baSJustin T. Gibbs dinfo->mem_ptr = (u_int8_t *)realloc(dinfo->mem_ptr, 46528fb27baSJustin T. Gibbs dssize); 46628fb27baSJustin T. Gibbs } 46728fb27baSJustin T. Gibbs retval = 1; 46828fb27baSJustin T. Gibbs } 46928fb27baSJustin T. Gibbs 470bcc6a3daSKenneth D. Merry dinfo->devices = (struct devstat *)(dinfo->mem_ptr + sizeof(long)); 47128fb27baSJustin T. Gibbs 47228fb27baSJustin T. Gibbs return(retval); 47328fb27baSJustin T. Gibbs } 47428fb27baSJustin T. Gibbs 47528fb27baSJustin T. Gibbs /* 47628fb27baSJustin T. Gibbs * selectdevs(): 47728fb27baSJustin T. Gibbs * 47828fb27baSJustin T. Gibbs * Devices are selected/deselected based upon the following criteria: 47928fb27baSJustin T. Gibbs * - devices specified by the user on the command line 48028fb27baSJustin T. Gibbs * - devices matching any device type expressions given on the command line 48128fb27baSJustin T. Gibbs * - devices with the highest I/O, if 'top' mode is enabled 48228fb27baSJustin T. Gibbs * - the first n unselected devices in the device list, if maxshowdevs 48328fb27baSJustin T. Gibbs * devices haven't already been selected and if the user has not 48428fb27baSJustin T. Gibbs * specified any devices on the command line and if we're in "add" mode. 48528fb27baSJustin T. Gibbs * 48628fb27baSJustin T. Gibbs * Input parameters: 48728fb27baSJustin T. Gibbs * - device selection list (dev_select) 48828fb27baSJustin T. Gibbs * - current number of devices selected (num_selected) 48928fb27baSJustin T. Gibbs * - total number of devices in the selection list (num_selections) 49028fb27baSJustin T. Gibbs * - devstat generation as of the last time selectdevs() was called 49128fb27baSJustin T. Gibbs * (select_generation) 49228fb27baSJustin T. Gibbs * - current devstat generation (current_generation) 49328fb27baSJustin T. Gibbs * - current list of devices and statistics (devices) 49428fb27baSJustin T. Gibbs * - number of devices in the current device list (numdevs) 49528fb27baSJustin T. Gibbs * - compiled version of the command line device type arguments (matches) 49628fb27baSJustin T. Gibbs * - This is optional. If the number of devices is 0, this will be ignored. 49728fb27baSJustin T. Gibbs * - The matching code pays attention to the current selection mode. So 49828fb27baSJustin T. Gibbs * if you pass in a matching expression, it will be evaluated based 49928fb27baSJustin T. Gibbs * upon the selection mode that is passed in. See below for details. 50028fb27baSJustin T. Gibbs * - number of device type matching expressions (num_matches) 50128fb27baSJustin T. Gibbs * - Set to 0 to disable the matching code. 50228fb27baSJustin T. Gibbs * - list of devices specified on the command line by the user (dev_selections) 50328fb27baSJustin T. Gibbs * - number of devices selected on the command line by the user 50428fb27baSJustin T. Gibbs * (num_dev_selections) 50528fb27baSJustin T. Gibbs * - Our selection mode. There are four different selection modes: 50628fb27baSJustin T. Gibbs * - add mode. (DS_SELECT_ADD) Any devices matching devices explicitly 50728fb27baSJustin T. Gibbs * selected by the user or devices matching a pattern given by the 50828fb27baSJustin T. Gibbs * user will be selected in addition to devices that are already 50928fb27baSJustin T. Gibbs * selected. Additional devices will be selected, up to maxshowdevs 51028fb27baSJustin T. Gibbs * number of devices. 51128fb27baSJustin T. Gibbs * - only mode. (DS_SELECT_ONLY) Only devices matching devices 51228fb27baSJustin T. Gibbs * explicitly given by the user or devices matching a pattern 51328fb27baSJustin T. Gibbs * given by the user will be selected. No other devices will be 51428fb27baSJustin T. Gibbs * selected. 51528fb27baSJustin T. Gibbs * - addonly mode. (DS_SELECT_ADDONLY) This is similar to add and 51628fb27baSJustin T. Gibbs * only. Basically, this will not de-select any devices that are 51728fb27baSJustin T. Gibbs * current selected, as only mode would, but it will also not 51828fb27baSJustin T. Gibbs * gratuitously select up to maxshowdevs devices as add mode would. 51928fb27baSJustin T. Gibbs * - remove mode. (DS_SELECT_REMOVE) Any devices matching devices 52028fb27baSJustin T. Gibbs * explicitly selected by the user or devices matching a pattern 52128fb27baSJustin T. Gibbs * given by the user will be de-selected. 52228fb27baSJustin T. Gibbs * - maximum number of devices we can select (maxshowdevs) 52328fb27baSJustin T. Gibbs * - flag indicating whether or not we're in 'top' mode (perf_select) 52428fb27baSJustin T. Gibbs * 52528fb27baSJustin T. Gibbs * Output data: 52628fb27baSJustin T. Gibbs * - the device selection list may be modified and passed back out 52728fb27baSJustin T. Gibbs * - the number of devices selected and the total number of items in the 52828fb27baSJustin T. Gibbs * device selection list may be changed 52928fb27baSJustin T. Gibbs * - the selection generation may be changed to match the current generation 53028fb27baSJustin T. Gibbs * 53128fb27baSJustin T. Gibbs * Return values: 53228fb27baSJustin T. Gibbs * -1 -- error 53328fb27baSJustin T. Gibbs * 0 -- selected devices are unchanged 53428fb27baSJustin T. Gibbs * 1 -- selected devices changed 53528fb27baSJustin T. Gibbs */ 53628fb27baSJustin T. Gibbs int 537c4a5ef6eSThomas Moestl devstat_selectdevs(struct device_selection **dev_select, int *num_selected, 538bcc6a3daSKenneth D. Merry int *num_selections, long *select_generation, 539c4a5ef6eSThomas Moestl long current_generation, struct devstat *devices, 540c4a5ef6eSThomas Moestl int numdevs, struct devstat_match *matches, int num_matches, 54128fb27baSJustin T. Gibbs char **dev_selections, int num_dev_selections, 542c4a5ef6eSThomas Moestl devstat_select_mode select_mode, int maxshowdevs, 543c4a5ef6eSThomas Moestl int perf_select) 54428fb27baSJustin T. Gibbs { 545be04b6d1SDavid E. O'Brien int i, j, k; 54628fb27baSJustin T. Gibbs int init_selections = 0, init_selected_var = 0; 54728fb27baSJustin T. Gibbs struct device_selection *old_dev_select = NULL; 54828fb27baSJustin T. Gibbs int old_num_selections = 0, old_num_selected; 54928fb27baSJustin T. Gibbs int selection_number = 0; 55028fb27baSJustin T. Gibbs int changed = 0, found = 0; 55128fb27baSJustin T. Gibbs 55228fb27baSJustin T. Gibbs if ((dev_select == NULL) || (devices == NULL) || (numdevs <= 0)) 55328fb27baSJustin T. Gibbs return(-1); 55428fb27baSJustin T. Gibbs 55528fb27baSJustin T. Gibbs /* 55628fb27baSJustin T. Gibbs * We always want to make sure that we have as many dev_select 55728fb27baSJustin T. Gibbs * entries as there are devices. 55828fb27baSJustin T. Gibbs */ 55928fb27baSJustin T. Gibbs /* 56028fb27baSJustin T. Gibbs * In this case, we haven't selected devices before. 56128fb27baSJustin T. Gibbs */ 56228fb27baSJustin T. Gibbs if (*dev_select == NULL) { 56328fb27baSJustin T. Gibbs *dev_select = (struct device_selection *)malloc(numdevs * 56428fb27baSJustin T. Gibbs sizeof(struct device_selection)); 56528fb27baSJustin T. Gibbs *select_generation = current_generation; 56628fb27baSJustin T. Gibbs init_selections = 1; 56728fb27baSJustin T. Gibbs changed = 1; 56828fb27baSJustin T. Gibbs /* 56928fb27baSJustin T. Gibbs * In this case, we have selected devices before, but the device 57028fb27baSJustin T. Gibbs * list has changed since we last selected devices, so we need to 57128fb27baSJustin T. Gibbs * either enlarge or reduce the size of the device selection list. 57228fb27baSJustin T. Gibbs */ 57328fb27baSJustin T. Gibbs } else if (*num_selections != numdevs) { 57428fb27baSJustin T. Gibbs *dev_select = (struct device_selection *)realloc(*dev_select, 57528fb27baSJustin T. Gibbs numdevs * sizeof(struct device_selection)); 57628fb27baSJustin T. Gibbs *select_generation = current_generation; 57728fb27baSJustin T. Gibbs init_selections = 1; 57828fb27baSJustin T. Gibbs /* 57928fb27baSJustin T. Gibbs * In this case, we've selected devices before, and the selection 58028fb27baSJustin T. Gibbs * list is the same size as it was the last time, but the device 58128fb27baSJustin T. Gibbs * list has changed. 58228fb27baSJustin T. Gibbs */ 58328fb27baSJustin T. Gibbs } else if (*select_generation < current_generation) { 58428fb27baSJustin T. Gibbs *select_generation = current_generation; 58528fb27baSJustin T. Gibbs init_selections = 1; 58628fb27baSJustin T. Gibbs } 58728fb27baSJustin T. Gibbs 58828fb27baSJustin T. Gibbs /* 58928fb27baSJustin T. Gibbs * If we're in "only" mode, we want to clear out the selected 59028fb27baSJustin T. Gibbs * variable since we're going to select exactly what the user wants 59128fb27baSJustin T. Gibbs * this time through. 59228fb27baSJustin T. Gibbs */ 59328fb27baSJustin T. Gibbs if (select_mode == DS_SELECT_ONLY) 59428fb27baSJustin T. Gibbs init_selected_var = 1; 59528fb27baSJustin T. Gibbs 59628fb27baSJustin T. Gibbs /* 59728fb27baSJustin T. Gibbs * In all cases, we want to back up the number of selected devices. 59828fb27baSJustin T. Gibbs * It is a quick and accurate way to determine whether the selected 59928fb27baSJustin T. Gibbs * devices have changed. 60028fb27baSJustin T. Gibbs */ 60128fb27baSJustin T. Gibbs old_num_selected = *num_selected; 60228fb27baSJustin T. Gibbs 60328fb27baSJustin T. Gibbs /* 60428fb27baSJustin T. Gibbs * We want to make a backup of the current selection list if 60528fb27baSJustin T. Gibbs * the list of devices has changed, or if we're in performance 60628fb27baSJustin T. Gibbs * selection mode. In both cases, we don't want to make a backup 60728fb27baSJustin T. Gibbs * if we already know for sure that the list will be different. 60828fb27baSJustin T. Gibbs * This is certainly the case if this is our first time through the 60928fb27baSJustin T. Gibbs * selection code. 61028fb27baSJustin T. Gibbs */ 61128fb27baSJustin T. Gibbs if (((init_selected_var != 0) || (init_selections != 0) 61228fb27baSJustin T. Gibbs || (perf_select != 0)) && (changed == 0)){ 61328fb27baSJustin T. Gibbs old_dev_select = (struct device_selection *)malloc( 61428fb27baSJustin T. Gibbs *num_selections * sizeof(struct device_selection)); 61528fb27baSJustin T. Gibbs old_num_selections = *num_selections; 61628fb27baSJustin T. Gibbs bcopy(*dev_select, old_dev_select, 61728fb27baSJustin T. Gibbs sizeof(struct device_selection) * *num_selections); 61828fb27baSJustin T. Gibbs } 61928fb27baSJustin T. Gibbs 62028fb27baSJustin T. Gibbs if (init_selections != 0) { 62128fb27baSJustin T. Gibbs bzero(*dev_select, sizeof(struct device_selection) * numdevs); 62228fb27baSJustin T. Gibbs 62328fb27baSJustin T. Gibbs for (i = 0; i < numdevs; i++) { 62428fb27baSJustin T. Gibbs (*dev_select)[i].device_number = 62528fb27baSJustin T. Gibbs devices[i].device_number; 62628fb27baSJustin T. Gibbs strncpy((*dev_select)[i].device_name, 62728fb27baSJustin T. Gibbs devices[i].device_name, 62828fb27baSJustin T. Gibbs DEVSTAT_NAME_LEN); 62917ee2b20SKenneth D. Merry (*dev_select)[i].device_name[DEVSTAT_NAME_LEN - 1]='\0'; 63028fb27baSJustin T. Gibbs (*dev_select)[i].unit_number = devices[i].unit_number; 63128fb27baSJustin T. Gibbs (*dev_select)[i].position = i; 63228fb27baSJustin T. Gibbs } 63328fb27baSJustin T. Gibbs *num_selections = numdevs; 63428fb27baSJustin T. Gibbs } else if (init_selected_var != 0) { 63528fb27baSJustin T. Gibbs for (i = 0; i < numdevs; i++) 63628fb27baSJustin T. Gibbs (*dev_select)[i].selected = 0; 63728fb27baSJustin T. Gibbs } 63828fb27baSJustin T. Gibbs 63928fb27baSJustin T. Gibbs /* we haven't gotten around to selecting anything yet.. */ 64028fb27baSJustin T. Gibbs if ((select_mode == DS_SELECT_ONLY) || (init_selections != 0) 64128fb27baSJustin T. Gibbs || (init_selected_var != 0)) 64228fb27baSJustin T. Gibbs *num_selected = 0; 64328fb27baSJustin T. Gibbs 64428fb27baSJustin T. Gibbs /* 64528fb27baSJustin T. Gibbs * Look through any devices the user specified on the command line 64628fb27baSJustin T. Gibbs * and see if they match known devices. If so, select them. 64728fb27baSJustin T. Gibbs */ 64828fb27baSJustin T. Gibbs for (i = 0; (i < *num_selections) && (num_dev_selections > 0); i++) { 64928fb27baSJustin T. Gibbs char tmpstr[80]; 65028fb27baSJustin T. Gibbs 65117ee2b20SKenneth D. Merry snprintf(tmpstr, sizeof(tmpstr), "%s%d", 65217ee2b20SKenneth D. Merry (*dev_select)[i].device_name, 65328fb27baSJustin T. Gibbs (*dev_select)[i].unit_number); 65428fb27baSJustin T. Gibbs for (j = 0; j < num_dev_selections; j++) { 65528fb27baSJustin T. Gibbs if (strcmp(tmpstr, dev_selections[j]) == 0) { 65628fb27baSJustin T. Gibbs /* 65728fb27baSJustin T. Gibbs * Here we do different things based on the 65828fb27baSJustin T. Gibbs * mode we're in. If we're in add or 65928fb27baSJustin T. Gibbs * addonly mode, we only select this device 66028fb27baSJustin T. Gibbs * if it hasn't already been selected. 66128fb27baSJustin T. Gibbs * Otherwise, we would be unnecessarily 66228fb27baSJustin T. Gibbs * changing the selection order and 66328fb27baSJustin T. Gibbs * incrementing the selection count. If 66428fb27baSJustin T. Gibbs * we're in only mode, we unconditionally 66528fb27baSJustin T. Gibbs * select this device, since in only mode 66628fb27baSJustin T. Gibbs * any previous selections are erased and 66728fb27baSJustin T. Gibbs * manually specified devices are the first 66828fb27baSJustin T. Gibbs * ones to be selected. If we're in remove 66928fb27baSJustin T. Gibbs * mode, we de-select the specified device and 67028fb27baSJustin T. Gibbs * decrement the selection count. 67128fb27baSJustin T. Gibbs */ 67228fb27baSJustin T. Gibbs switch(select_mode) { 67328fb27baSJustin T. Gibbs case DS_SELECT_ADD: 67428fb27baSJustin T. Gibbs case DS_SELECT_ADDONLY: 67528fb27baSJustin T. Gibbs if ((*dev_select)[i].selected) 67628fb27baSJustin T. Gibbs break; 67728fb27baSJustin T. Gibbs /* FALLTHROUGH */ 67828fb27baSJustin T. Gibbs case DS_SELECT_ONLY: 67928fb27baSJustin T. Gibbs (*dev_select)[i].selected = 68028fb27baSJustin T. Gibbs ++selection_number; 68128fb27baSJustin T. Gibbs (*num_selected)++; 68228fb27baSJustin T. Gibbs break; 68328fb27baSJustin T. Gibbs case DS_SELECT_REMOVE: 68428fb27baSJustin T. Gibbs (*dev_select)[i].selected = 0; 68528fb27baSJustin T. Gibbs (*num_selected)--; 68628fb27baSJustin T. Gibbs /* 68728fb27baSJustin T. Gibbs * This isn't passed back out, we 68828fb27baSJustin T. Gibbs * just use it to keep track of 68928fb27baSJustin T. Gibbs * how many devices we've removed. 69028fb27baSJustin T. Gibbs */ 69128fb27baSJustin T. Gibbs num_dev_selections--; 69228fb27baSJustin T. Gibbs break; 69328fb27baSJustin T. Gibbs } 69428fb27baSJustin T. Gibbs break; 69528fb27baSJustin T. Gibbs } 69628fb27baSJustin T. Gibbs } 69728fb27baSJustin T. Gibbs } 69828fb27baSJustin T. Gibbs 69928fb27baSJustin T. Gibbs /* 70028fb27baSJustin T. Gibbs * Go through the user's device type expressions and select devices 70128fb27baSJustin T. Gibbs * accordingly. We only do this if the number of devices already 70228fb27baSJustin T. Gibbs * selected is less than the maximum number we can show. 70328fb27baSJustin T. Gibbs */ 70428fb27baSJustin T. Gibbs for (i = 0; (i < num_matches) && (*num_selected < maxshowdevs); i++) { 70528fb27baSJustin T. Gibbs /* We should probably indicate some error here */ 70628fb27baSJustin T. Gibbs if ((matches[i].match_fields == DEVSTAT_MATCH_NONE) 70728fb27baSJustin T. Gibbs || (matches[i].num_match_categories <= 0)) 70828fb27baSJustin T. Gibbs continue; 70928fb27baSJustin T. Gibbs 71028fb27baSJustin T. Gibbs for (j = 0; j < numdevs; j++) { 71128fb27baSJustin T. Gibbs int num_match_categories; 71228fb27baSJustin T. Gibbs 71328fb27baSJustin T. Gibbs num_match_categories = matches[i].num_match_categories; 71428fb27baSJustin T. Gibbs 71528fb27baSJustin T. Gibbs /* 71628fb27baSJustin T. Gibbs * Determine whether or not the current device 71728fb27baSJustin T. Gibbs * matches the given matching expression. This if 71828fb27baSJustin T. Gibbs * statement consists of three components: 71928fb27baSJustin T. Gibbs * - the device type check 72028fb27baSJustin T. Gibbs * - the device interface check 72128fb27baSJustin T. Gibbs * - the passthrough check 72228fb27baSJustin T. Gibbs * If a the matching test is successful, it 72328fb27baSJustin T. Gibbs * decrements the number of matching categories, 72428fb27baSJustin T. Gibbs * and if we've reached the last element that 72528fb27baSJustin T. Gibbs * needed to be matched, the if statement succeeds. 72628fb27baSJustin T. Gibbs * 72728fb27baSJustin T. Gibbs */ 72828fb27baSJustin T. Gibbs if ((((matches[i].match_fields & DEVSTAT_MATCH_TYPE)!=0) 72928fb27baSJustin T. Gibbs && ((devices[j].device_type & DEVSTAT_TYPE_MASK) == 73028fb27baSJustin T. Gibbs (matches[i].device_type & DEVSTAT_TYPE_MASK)) 73128fb27baSJustin T. Gibbs &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0) 73228fb27baSJustin T. Gibbs || (((matches[i].match_fields & 73328fb27baSJustin T. Gibbs DEVSTAT_MATCH_PASS) == 0) 73428fb27baSJustin T. Gibbs && ((devices[j].device_type & 73528fb27baSJustin T. Gibbs DEVSTAT_TYPE_PASS) == 0))) 73628fb27baSJustin T. Gibbs && (--num_match_categories == 0)) 73728fb27baSJustin T. Gibbs || (((matches[i].match_fields & DEVSTAT_MATCH_IF) != 0) 73828fb27baSJustin T. Gibbs && ((devices[j].device_type & DEVSTAT_TYPE_IF_MASK) == 73928fb27baSJustin T. Gibbs (matches[i].device_type & DEVSTAT_TYPE_IF_MASK)) 74028fb27baSJustin T. Gibbs &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0) 74128fb27baSJustin T. Gibbs || (((matches[i].match_fields & 74228fb27baSJustin T. Gibbs DEVSTAT_MATCH_PASS) == 0) 74328fb27baSJustin T. Gibbs && ((devices[j].device_type & 74428fb27baSJustin T. Gibbs DEVSTAT_TYPE_PASS) == 0))) 74528fb27baSJustin T. Gibbs && (--num_match_categories == 0)) 74628fb27baSJustin T. Gibbs || (((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0) 74728fb27baSJustin T. Gibbs && ((devices[j].device_type & DEVSTAT_TYPE_PASS) != 0) 74828fb27baSJustin T. Gibbs && (--num_match_categories == 0))) { 74928fb27baSJustin T. Gibbs 75028fb27baSJustin T. Gibbs /* 75128fb27baSJustin T. Gibbs * This is probably a non-optimal solution 75228fb27baSJustin T. Gibbs * to the problem that the devices in the 75328fb27baSJustin T. Gibbs * device list will not be in the same 75428fb27baSJustin T. Gibbs * order as the devices in the selection 75528fb27baSJustin T. Gibbs * array. 75628fb27baSJustin T. Gibbs */ 75728fb27baSJustin T. Gibbs for (k = 0; k < numdevs; k++) { 75828fb27baSJustin T. Gibbs if ((*dev_select)[k].position == j) { 75928fb27baSJustin T. Gibbs found = 1; 76028fb27baSJustin T. Gibbs break; 76128fb27baSJustin T. Gibbs } 76228fb27baSJustin T. Gibbs } 76328fb27baSJustin T. Gibbs 76428fb27baSJustin T. Gibbs /* 76528fb27baSJustin T. Gibbs * There shouldn't be a case where a device 76628fb27baSJustin T. Gibbs * in the device list is not in the 76728fb27baSJustin T. Gibbs * selection list...but it could happen. 76828fb27baSJustin T. Gibbs */ 76928fb27baSJustin T. Gibbs if (found != 1) { 77028fb27baSJustin T. Gibbs fprintf(stderr, "selectdevs: couldn't" 77128fb27baSJustin T. Gibbs " find %s%d in selection " 77228fb27baSJustin T. Gibbs "list\n", 77328fb27baSJustin T. Gibbs devices[j].device_name, 77428fb27baSJustin T. Gibbs devices[j].unit_number); 77528fb27baSJustin T. Gibbs break; 77628fb27baSJustin T. Gibbs } 77728fb27baSJustin T. Gibbs 77828fb27baSJustin T. Gibbs /* 77928fb27baSJustin T. Gibbs * We do different things based upon the 78028fb27baSJustin T. Gibbs * mode we're in. If we're in add or only 78128fb27baSJustin T. Gibbs * mode, we go ahead and select this device 78228fb27baSJustin T. Gibbs * if it hasn't already been selected. If 78328fb27baSJustin T. Gibbs * it has already been selected, we leave 78428fb27baSJustin T. Gibbs * it alone so we don't mess up the 78528fb27baSJustin T. Gibbs * selection ordering. Manually specified 78628fb27baSJustin T. Gibbs * devices have already been selected, and 78728fb27baSJustin T. Gibbs * they have higher priority than pattern 78828fb27baSJustin T. Gibbs * matched devices. If we're in remove 78928fb27baSJustin T. Gibbs * mode, we de-select the given device and 79028fb27baSJustin T. Gibbs * decrement the selected count. 79128fb27baSJustin T. Gibbs */ 79228fb27baSJustin T. Gibbs switch(select_mode) { 79328fb27baSJustin T. Gibbs case DS_SELECT_ADD: 79428fb27baSJustin T. Gibbs case DS_SELECT_ADDONLY: 79528fb27baSJustin T. Gibbs case DS_SELECT_ONLY: 79628fb27baSJustin T. Gibbs if ((*dev_select)[k].selected != 0) 79728fb27baSJustin T. Gibbs break; 79828fb27baSJustin T. Gibbs (*dev_select)[k].selected = 79928fb27baSJustin T. Gibbs ++selection_number; 80028fb27baSJustin T. Gibbs (*num_selected)++; 80128fb27baSJustin T. Gibbs break; 80228fb27baSJustin T. Gibbs case DS_SELECT_REMOVE: 80328fb27baSJustin T. Gibbs (*dev_select)[k].selected = 0; 80428fb27baSJustin T. Gibbs (*num_selected)--; 80528fb27baSJustin T. Gibbs break; 80628fb27baSJustin T. Gibbs } 80728fb27baSJustin T. Gibbs } 80828fb27baSJustin T. Gibbs } 80928fb27baSJustin T. Gibbs } 81028fb27baSJustin T. Gibbs 81128fb27baSJustin T. Gibbs /* 81228fb27baSJustin T. Gibbs * Here we implement "top" mode. Devices are sorted in the 81328fb27baSJustin T. Gibbs * selection array based on two criteria: whether or not they are 81428fb27baSJustin T. Gibbs * selected (not selection number, just the fact that they are 81528fb27baSJustin T. Gibbs * selected!) and the number of bytes in the "bytes" field of the 81628fb27baSJustin T. Gibbs * selection structure. The bytes field generally must be kept up 81728fb27baSJustin T. Gibbs * by the user. In the future, it may be maintained by library 81828fb27baSJustin T. Gibbs * functions, but for now the user has to do the work. 81928fb27baSJustin T. Gibbs * 82028fb27baSJustin T. Gibbs * At first glance, it may seem wrong that we don't go through and 82128fb27baSJustin T. Gibbs * select every device in the case where the user hasn't specified 82228fb27baSJustin T. Gibbs * any devices or patterns. In fact, though, it won't make any 82328fb27baSJustin T. Gibbs * difference in the device sorting. In that particular case (i.e. 82428fb27baSJustin T. Gibbs * when we're in "add" or "only" mode, and the user hasn't 82528fb27baSJustin T. Gibbs * specified anything) the first time through no devices will be 82628fb27baSJustin T. Gibbs * selected, so the only criterion used to sort them will be their 82728fb27baSJustin T. Gibbs * performance. The second time through, and every time thereafter, 82828fb27baSJustin T. Gibbs * all devices will be selected, so again selection won't matter. 82928fb27baSJustin T. Gibbs */ 83028fb27baSJustin T. Gibbs if (perf_select != 0) { 83128fb27baSJustin T. Gibbs 83228fb27baSJustin T. Gibbs /* Sort the device array by throughput */ 83328fb27baSJustin T. Gibbs qsort(*dev_select, *num_selections, 83428fb27baSJustin T. Gibbs sizeof(struct device_selection), 83528fb27baSJustin T. Gibbs compare_select); 83628fb27baSJustin T. Gibbs 83728fb27baSJustin T. Gibbs if (*num_selected == 0) { 83828fb27baSJustin T. Gibbs /* 83928fb27baSJustin T. Gibbs * Here we select every device in the array, if it 84028fb27baSJustin T. Gibbs * isn't already selected. Because the 'selected' 84128fb27baSJustin T. Gibbs * variable in the selection array entries contains 84228fb27baSJustin T. Gibbs * the selection order, the devstats routine can show 84328fb27baSJustin T. Gibbs * the devices that were selected first. 84428fb27baSJustin T. Gibbs */ 84528fb27baSJustin T. Gibbs for (i = 0; i < *num_selections; i++) { 84628fb27baSJustin T. Gibbs if ((*dev_select)[i].selected == 0) { 84728fb27baSJustin T. Gibbs (*dev_select)[i].selected = 84828fb27baSJustin T. Gibbs ++selection_number; 84928fb27baSJustin T. Gibbs (*num_selected)++; 85028fb27baSJustin T. Gibbs } 85128fb27baSJustin T. Gibbs } 85228fb27baSJustin T. Gibbs } else { 85328fb27baSJustin T. Gibbs selection_number = 0; 85428fb27baSJustin T. Gibbs for (i = 0; i < *num_selections; i++) { 85528fb27baSJustin T. Gibbs if ((*dev_select)[i].selected != 0) { 85628fb27baSJustin T. Gibbs (*dev_select)[i].selected = 85728fb27baSJustin T. Gibbs ++selection_number; 85828fb27baSJustin T. Gibbs } 85928fb27baSJustin T. Gibbs } 86028fb27baSJustin T. Gibbs } 86128fb27baSJustin T. Gibbs } 86228fb27baSJustin T. Gibbs 86328fb27baSJustin T. Gibbs /* 86428fb27baSJustin T. Gibbs * If we're in the "add" selection mode and if we haven't already 86528fb27baSJustin T. Gibbs * selected maxshowdevs number of devices, go through the array and 86628fb27baSJustin T. Gibbs * select any unselected devices. If we're in "only" mode, we 86728fb27baSJustin T. Gibbs * obviously don't want to select anything other than what the user 86828fb27baSJustin T. Gibbs * specifies. If we're in "remove" mode, it probably isn't a good 86928fb27baSJustin T. Gibbs * idea to go through and select any more devices, since we might 87028fb27baSJustin T. Gibbs * end up selecting something that the user wants removed. Through 87128fb27baSJustin T. Gibbs * more complicated logic, we could actually figure this out, but 87228fb27baSJustin T. Gibbs * that would probably require combining this loop with the various 87328fb27baSJustin T. Gibbs * selections loops above. 87428fb27baSJustin T. Gibbs */ 87528fb27baSJustin T. Gibbs if ((select_mode == DS_SELECT_ADD) && (*num_selected < maxshowdevs)) { 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 = ++selection_number; 87928fb27baSJustin T. Gibbs (*num_selected)++; 88028fb27baSJustin T. Gibbs } 88128fb27baSJustin T. Gibbs } 88228fb27baSJustin T. Gibbs 88328fb27baSJustin T. Gibbs /* 88428fb27baSJustin T. Gibbs * Look at the number of devices that have been selected. If it 88528fb27baSJustin T. Gibbs * has changed, set the changed variable. Otherwise, if we've 88628fb27baSJustin T. Gibbs * made a backup of the selection list, compare it to the current 88728fb27baSJustin T. Gibbs * selection list to see if the selected devices have changed. 88828fb27baSJustin T. Gibbs */ 88928fb27baSJustin T. Gibbs if ((changed == 0) && (old_num_selected != *num_selected)) 89028fb27baSJustin T. Gibbs changed = 1; 89128fb27baSJustin T. Gibbs else if ((changed == 0) && (old_dev_select != NULL)) { 89228fb27baSJustin T. Gibbs /* 89328fb27baSJustin T. Gibbs * Now we go through the selection list and we look at 89428fb27baSJustin T. Gibbs * it three different ways. 89528fb27baSJustin T. Gibbs */ 89628fb27baSJustin T. Gibbs for (i = 0; (i < *num_selections) && (changed == 0) && 89728fb27baSJustin T. Gibbs (i < old_num_selections); i++) { 89828fb27baSJustin T. Gibbs /* 89928fb27baSJustin T. Gibbs * If the device at index i in both the new and old 90028fb27baSJustin T. Gibbs * selection arrays has the same device number and 90128fb27baSJustin T. Gibbs * selection status, it hasn't changed. We 90228fb27baSJustin T. Gibbs * continue on to the next index. 90328fb27baSJustin T. Gibbs */ 90428fb27baSJustin T. Gibbs if (((*dev_select)[i].device_number == 90528fb27baSJustin T. Gibbs old_dev_select[i].device_number) 90628fb27baSJustin T. Gibbs && ((*dev_select)[i].selected == 90728fb27baSJustin T. Gibbs old_dev_select[i].selected)) 90828fb27baSJustin T. Gibbs continue; 90928fb27baSJustin T. Gibbs 91028fb27baSJustin T. Gibbs /* 91128fb27baSJustin T. Gibbs * Now, if we're still going through the if 91228fb27baSJustin T. Gibbs * statement, the above test wasn't true. So we 91328fb27baSJustin T. Gibbs * check here to see if the device at index i in 91428fb27baSJustin T. Gibbs * the current array is the same as the device at 91528fb27baSJustin T. Gibbs * index i in the old array. If it is, that means 91628fb27baSJustin T. Gibbs * that its selection number has changed. Set 91728fb27baSJustin T. Gibbs * changed to 1 and exit the loop. 91828fb27baSJustin T. Gibbs */ 91928fb27baSJustin T. Gibbs else if ((*dev_select)[i].device_number == 92028fb27baSJustin T. Gibbs old_dev_select[i].device_number) { 92128fb27baSJustin T. Gibbs changed = 1; 92228fb27baSJustin T. Gibbs break; 92328fb27baSJustin T. Gibbs } 92428fb27baSJustin T. Gibbs /* 92528fb27baSJustin T. Gibbs * If we get here, then the device at index i in 92628fb27baSJustin T. Gibbs * the current array isn't the same device as the 92728fb27baSJustin T. Gibbs * device at index i in the old array. 92828fb27baSJustin T. Gibbs */ 92928fb27baSJustin T. Gibbs else { 930c3508206SKenneth D. Merry found = 0; 93128fb27baSJustin T. Gibbs 93228fb27baSJustin T. Gibbs /* 93328fb27baSJustin T. Gibbs * Search through the old selection array 93428fb27baSJustin T. Gibbs * looking for a device with the same 93528fb27baSJustin T. Gibbs * device number as the device at index i 93628fb27baSJustin T. Gibbs * in the current array. If the selection 93728fb27baSJustin T. Gibbs * status is the same, then we mark it as 93828fb27baSJustin T. Gibbs * found. If the selection status isn't 93928fb27baSJustin T. Gibbs * the same, we break out of the loop. 94028fb27baSJustin T. Gibbs * Since found isn't set, changed will be 94128fb27baSJustin T. Gibbs * set to 1 below. 94228fb27baSJustin T. Gibbs */ 94328fb27baSJustin T. Gibbs for (j = 0; j < old_num_selections; j++) { 94428fb27baSJustin T. Gibbs if (((*dev_select)[i].device_number == 94528fb27baSJustin T. Gibbs old_dev_select[j].device_number) 94628fb27baSJustin T. Gibbs && ((*dev_select)[i].selected == 94728fb27baSJustin T. Gibbs old_dev_select[j].selected)){ 94828fb27baSJustin T. Gibbs found = 1; 94928fb27baSJustin T. Gibbs break; 95028fb27baSJustin T. Gibbs } 95128fb27baSJustin T. Gibbs else if ((*dev_select)[i].device_number 95228fb27baSJustin T. Gibbs == old_dev_select[j].device_number) 95328fb27baSJustin T. Gibbs break; 95428fb27baSJustin T. Gibbs } 95528fb27baSJustin T. Gibbs if (found == 0) 95628fb27baSJustin T. Gibbs changed = 1; 95728fb27baSJustin T. Gibbs } 95828fb27baSJustin T. Gibbs } 95928fb27baSJustin T. Gibbs } 96028fb27baSJustin T. Gibbs if (old_dev_select != NULL) 96128fb27baSJustin T. Gibbs free(old_dev_select); 96228fb27baSJustin T. Gibbs 96328fb27baSJustin T. Gibbs return(changed); 96428fb27baSJustin T. Gibbs } 96528fb27baSJustin T. Gibbs 96628fb27baSJustin T. Gibbs /* 96728fb27baSJustin T. Gibbs * Comparison routine for qsort() above. Note that the comparison here is 96828fb27baSJustin T. Gibbs * backwards -- generally, it should return a value to indicate whether 96928fb27baSJustin T. Gibbs * arg1 is <, =, or > arg2. Instead, it returns the opposite. The reason 97028fb27baSJustin T. Gibbs * it returns the opposite is so that the selection array will be sorted in 97128fb27baSJustin T. Gibbs * order of decreasing performance. We sort on two parameters. The first 97228fb27baSJustin T. Gibbs * sort key is whether or not one or the other of the devices in question 97328fb27baSJustin T. Gibbs * has been selected. If one of them has, and the other one has not, the 97428fb27baSJustin T. Gibbs * selected device is automatically more important than the unselected 97528fb27baSJustin T. Gibbs * device. If neither device is selected, we judge the devices based upon 97628fb27baSJustin T. Gibbs * performance. 97728fb27baSJustin T. Gibbs */ 97828fb27baSJustin T. Gibbs static int 97928fb27baSJustin T. Gibbs compare_select(const void *arg1, const void *arg2) 98028fb27baSJustin T. Gibbs { 981c3508206SKenneth D. Merry if ((((const struct device_selection *)arg1)->selected) 982c3508206SKenneth D. Merry && (((const struct device_selection *)arg2)->selected == 0)) 98328fb27baSJustin T. Gibbs return(-1); 984c3508206SKenneth D. Merry else if ((((const struct device_selection *)arg1)->selected == 0) 985c3508206SKenneth D. Merry && (((const struct device_selection *)arg2)->selected)) 98628fb27baSJustin T. Gibbs return(1); 987c3508206SKenneth D. Merry else if (((const struct device_selection *)arg2)->bytes < 988c3508206SKenneth D. Merry ((const struct device_selection *)arg1)->bytes) 98928fb27baSJustin T. Gibbs return(-1); 990c3508206SKenneth D. Merry else if (((const struct device_selection *)arg2)->bytes > 991c3508206SKenneth D. Merry ((const struct device_selection *)arg1)->bytes) 99228fb27baSJustin T. Gibbs return(1); 99328fb27baSJustin T. Gibbs else 99428fb27baSJustin T. Gibbs return(0); 99528fb27baSJustin T. Gibbs } 99628fb27baSJustin T. Gibbs 99728fb27baSJustin T. Gibbs /* 99828fb27baSJustin T. Gibbs * Take a string with the general format "arg1,arg2,arg3", and build a 99928fb27baSJustin T. Gibbs * device matching expression from it. 100028fb27baSJustin T. Gibbs */ 100128fb27baSJustin T. Gibbs int 1002c4a5ef6eSThomas Moestl devstat_buildmatch(char *match_str, struct devstat_match **matches, 1003c4a5ef6eSThomas Moestl int *num_matches) 100428fb27baSJustin T. Gibbs { 100528fb27baSJustin T. Gibbs char *tstr[5]; 100628fb27baSJustin T. Gibbs char **tempstr; 100728fb27baSJustin T. Gibbs int num_args; 1008be04b6d1SDavid E. O'Brien int i, j; 1009c3508206SKenneth D. Merry const char *func_name = "devstat_buildmatch"; 101028fb27baSJustin T. Gibbs 101128fb27baSJustin T. Gibbs /* We can't do much without a string to parse */ 101228fb27baSJustin T. Gibbs if (match_str == NULL) { 1013c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1014c4a5ef6eSThomas Moestl "%s: no match expression", func_name); 101528fb27baSJustin T. Gibbs return(-1); 101628fb27baSJustin T. Gibbs } 101728fb27baSJustin T. Gibbs 101828fb27baSJustin T. Gibbs /* 101928fb27baSJustin T. Gibbs * Break the (comma delimited) input string out into separate strings. 102028fb27baSJustin T. Gibbs */ 102128fb27baSJustin T. Gibbs for (tempstr = tstr, num_args = 0; 102228fb27baSJustin T. Gibbs (*tempstr = strsep(&match_str, ",")) != NULL && (num_args < 5); 102328fb27baSJustin T. Gibbs num_args++) 102428fb27baSJustin T. Gibbs if (**tempstr != '\0') 102528fb27baSJustin T. Gibbs if (++tempstr >= &tstr[5]) 102628fb27baSJustin T. Gibbs break; 102728fb27baSJustin T. Gibbs 102828fb27baSJustin T. Gibbs /* The user gave us too many type arguments */ 102928fb27baSJustin T. Gibbs if (num_args > 3) { 1030c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1031c4a5ef6eSThomas Moestl "%s: too many type arguments", func_name); 103228fb27baSJustin T. Gibbs return(-1); 103328fb27baSJustin T. Gibbs } 103428fb27baSJustin T. Gibbs 103528fb27baSJustin T. Gibbs /* 103628fb27baSJustin T. Gibbs * Since you can't realloc a pointer that hasn't been malloced 103728fb27baSJustin T. Gibbs * first, we malloc first and then realloc. 103828fb27baSJustin T. Gibbs */ 103928fb27baSJustin T. Gibbs if (*num_matches == 0) 104028fb27baSJustin T. Gibbs *matches = (struct devstat_match *)malloc( 104128fb27baSJustin T. Gibbs sizeof(struct devstat_match)); 104228fb27baSJustin T. Gibbs else 104328fb27baSJustin T. Gibbs *matches = (struct devstat_match *)realloc(*matches, 104428fb27baSJustin T. Gibbs sizeof(struct devstat_match) * (*num_matches + 1)); 104528fb27baSJustin T. Gibbs 104628fb27baSJustin T. Gibbs /* Make sure the current entry is clear */ 104728fb27baSJustin T. Gibbs bzero(&matches[0][*num_matches], sizeof(struct devstat_match)); 104828fb27baSJustin T. Gibbs 104928fb27baSJustin T. Gibbs /* 105028fb27baSJustin T. Gibbs * Step through the arguments the user gave us and build a device 105128fb27baSJustin T. Gibbs * matching expression from them. 105228fb27baSJustin T. Gibbs */ 105328fb27baSJustin T. Gibbs for (i = 0; i < num_args; i++) { 105428fb27baSJustin T. Gibbs char *tempstr2, *tempstr3; 105528fb27baSJustin T. Gibbs 105628fb27baSJustin T. Gibbs /* 105728fb27baSJustin T. Gibbs * Get rid of leading white space. 105828fb27baSJustin T. Gibbs */ 105928fb27baSJustin T. Gibbs tempstr2 = tstr[i]; 106028fb27baSJustin T. Gibbs while (isspace(*tempstr2) && (*tempstr2 != '\0')) 106128fb27baSJustin T. Gibbs tempstr2++; 106228fb27baSJustin T. Gibbs 106328fb27baSJustin T. Gibbs /* 106428fb27baSJustin T. Gibbs * Get rid of trailing white space. 106528fb27baSJustin T. Gibbs */ 106628fb27baSJustin T. Gibbs tempstr3 = &tempstr2[strlen(tempstr2) - 1]; 106728fb27baSJustin T. Gibbs 106828fb27baSJustin T. Gibbs while ((*tempstr3 != '\0') && (tempstr3 > tempstr2) 106928fb27baSJustin T. Gibbs && (isspace(*tempstr3))) { 107028fb27baSJustin T. Gibbs *tempstr3 = '\0'; 107128fb27baSJustin T. Gibbs tempstr3--; 107228fb27baSJustin T. Gibbs } 107328fb27baSJustin T. Gibbs 107428fb27baSJustin T. Gibbs /* 107528fb27baSJustin T. Gibbs * Go through the match table comparing the user's 107628fb27baSJustin T. Gibbs * arguments to known device types, interfaces, etc. 107728fb27baSJustin T. Gibbs */ 107828fb27baSJustin T. Gibbs for (j = 0; match_table[j].match_str != NULL; j++) { 107928fb27baSJustin T. Gibbs /* 108028fb27baSJustin T. Gibbs * We do case-insensitive matching, in case someone 108128fb27baSJustin T. Gibbs * wants to enter "SCSI" instead of "scsi" or 108228fb27baSJustin T. Gibbs * something like that. Only compare as many 108328fb27baSJustin T. Gibbs * characters as are in the string in the match 108428fb27baSJustin T. Gibbs * table. This should help if someone tries to use 108528fb27baSJustin T. Gibbs * a super-long match expression. 108628fb27baSJustin T. Gibbs */ 108728fb27baSJustin T. Gibbs if (strncasecmp(tempstr2, match_table[j].match_str, 108828fb27baSJustin T. Gibbs strlen(match_table[j].match_str)) == 0) { 108928fb27baSJustin T. Gibbs /* 109028fb27baSJustin T. Gibbs * Make sure the user hasn't specified two 109128fb27baSJustin T. Gibbs * items of the same type, like "da" and 109228fb27baSJustin T. Gibbs * "cd". One device cannot be both. 109328fb27baSJustin T. Gibbs */ 109428fb27baSJustin T. Gibbs if (((*matches)[*num_matches].match_fields & 109528fb27baSJustin T. Gibbs match_table[j].match_field) != 0) { 1096c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, 1097c4a5ef6eSThomas Moestl sizeof(devstat_errbuf), 109828fb27baSJustin T. Gibbs "%s: cannot have more than " 109928fb27baSJustin T. Gibbs "one match item in a single " 110028fb27baSJustin T. Gibbs "category", func_name); 110128fb27baSJustin T. Gibbs return(-1); 110228fb27baSJustin T. Gibbs } 110328fb27baSJustin T. Gibbs /* 110428fb27baSJustin T. Gibbs * If we've gotten this far, we have a 110528fb27baSJustin T. Gibbs * winner. Set the appropriate fields in 110628fb27baSJustin T. Gibbs * the match entry. 110728fb27baSJustin T. Gibbs */ 110828fb27baSJustin T. Gibbs (*matches)[*num_matches].match_fields |= 110928fb27baSJustin T. Gibbs match_table[j].match_field; 111028fb27baSJustin T. Gibbs (*matches)[*num_matches].device_type |= 111128fb27baSJustin T. Gibbs match_table[j].type; 111228fb27baSJustin T. Gibbs (*matches)[*num_matches].num_match_categories++; 111328fb27baSJustin T. Gibbs break; 111428fb27baSJustin T. Gibbs } 111528fb27baSJustin T. Gibbs } 111628fb27baSJustin T. Gibbs /* 111728fb27baSJustin T. Gibbs * We should have found a match in the above for loop. If 111828fb27baSJustin T. Gibbs * not, that means the user entered an invalid device type 111928fb27baSJustin T. Gibbs * or interface. 112028fb27baSJustin T. Gibbs */ 112128fb27baSJustin T. Gibbs if ((*matches)[*num_matches].num_match_categories != (i + 1)) { 112217ee2b20SKenneth D. Merry snprintf(devstat_errbuf, sizeof(devstat_errbuf), 112328fb27baSJustin T. Gibbs "%s: unknown match item \"%s\"", func_name, 112428fb27baSJustin T. Gibbs tstr[i]); 112528fb27baSJustin T. Gibbs return(-1); 112628fb27baSJustin T. Gibbs } 112728fb27baSJustin T. Gibbs } 112828fb27baSJustin T. Gibbs 112928fb27baSJustin T. Gibbs (*num_matches)++; 113028fb27baSJustin T. Gibbs 113128fb27baSJustin T. Gibbs return(0); 113228fb27baSJustin T. Gibbs } 113328fb27baSJustin T. Gibbs 113428fb27baSJustin T. Gibbs /* 113528fb27baSJustin T. Gibbs * Compute a number of device statistics. Only one field is mandatory, and 113628fb27baSJustin T. Gibbs * that is "current". Everything else is optional. The caller passes in 113728fb27baSJustin T. Gibbs * pointers to variables to hold the various statistics he desires. If he 113828fb27baSJustin T. Gibbs * doesn't want a particular staistic, he should pass in a NULL pointer. 113928fb27baSJustin T. Gibbs * Return values: 114028fb27baSJustin T. Gibbs * 0 -- success 114128fb27baSJustin T. Gibbs * -1 -- failure 114228fb27baSJustin T. Gibbs */ 114328fb27baSJustin T. Gibbs int 114428fb27baSJustin T. Gibbs compute_stats(struct devstat *current, struct devstat *previous, 114528fb27baSJustin T. Gibbs long double etime, u_int64_t *total_bytes, 114628fb27baSJustin T. Gibbs u_int64_t *total_transfers, u_int64_t *total_blocks, 114728fb27baSJustin T. Gibbs long double *kb_per_transfer, long double *transfers_per_second, 114828fb27baSJustin T. Gibbs long double *mb_per_second, long double *blocks_per_second, 114928fb27baSJustin T. Gibbs long double *ms_per_transaction) 115028fb27baSJustin T. Gibbs { 1151884539f7SKenneth D. Merry return(devstat_compute_statistics(current, previous, etime, 1152884539f7SKenneth D. Merry total_bytes ? DSM_TOTAL_BYTES : DSM_SKIP, 1153884539f7SKenneth D. Merry total_bytes, 1154884539f7SKenneth D. Merry total_transfers ? DSM_TOTAL_TRANSFERS : DSM_SKIP, 1155884539f7SKenneth D. Merry total_transfers, 1156884539f7SKenneth D. Merry total_blocks ? DSM_TOTAL_BLOCKS : DSM_SKIP, 1157884539f7SKenneth D. Merry total_blocks, 1158884539f7SKenneth D. Merry kb_per_transfer ? DSM_KB_PER_TRANSFER : DSM_SKIP, 1159884539f7SKenneth D. Merry kb_per_transfer, 1160884539f7SKenneth D. Merry transfers_per_second ? DSM_TRANSFERS_PER_SECOND : DSM_SKIP, 1161884539f7SKenneth D. Merry transfers_per_second, 1162884539f7SKenneth D. Merry mb_per_second ? DSM_MB_PER_SECOND : DSM_SKIP, 1163884539f7SKenneth D. Merry mb_per_second, 1164884539f7SKenneth D. Merry blocks_per_second ? DSM_BLOCKS_PER_SECOND : DSM_SKIP, 1165884539f7SKenneth D. Merry blocks_per_second, 1166884539f7SKenneth D. Merry ms_per_transaction ? DSM_MS_PER_TRANSACTION : DSM_SKIP, 1167884539f7SKenneth D. Merry ms_per_transaction, 1168884539f7SKenneth D. Merry DSM_NONE)); 116928fb27baSJustin T. Gibbs } 117028fb27baSJustin T. Gibbs 11717194d335SPoul-Henning Kamp 11727194d335SPoul-Henning Kamp /* This is 1/2^64 */ 11737194d335SPoul-Henning Kamp #define BINTIME_SCALE 5.42101086242752217003726400434970855712890625e-20 11747194d335SPoul-Henning Kamp 117528fb27baSJustin T. Gibbs long double 11767194d335SPoul-Henning Kamp devstat_compute_etime(struct bintime *cur_time, struct bintime *prev_time) 117728fb27baSJustin T. Gibbs { 117828fb27baSJustin T. Gibbs long double etime; 117928fb27baSJustin T. Gibbs 11807194d335SPoul-Henning Kamp etime = cur_time->sec; 11817194d335SPoul-Henning Kamp etime += cur_time->frac * BINTIME_SCALE; 11827194d335SPoul-Henning Kamp if (prev_time != NULL) { 11837194d335SPoul-Henning Kamp etime -= prev_time->sec; 11847194d335SPoul-Henning Kamp etime -= prev_time->frac * BINTIME_SCALE; 11857194d335SPoul-Henning Kamp } 118628fb27baSJustin T. Gibbs return(etime); 118728fb27baSJustin T. Gibbs } 1188c4a5ef6eSThomas Moestl 118936eab1f5SPoul-Henning Kamp #define DELTA(field, index) \ 119036eab1f5SPoul-Henning Kamp (current->field[(index)] - (previous ? previous->field[(index)] : 0)) 119136eab1f5SPoul-Henning Kamp 119236eab1f5SPoul-Henning Kamp #define DELTA_T(field) \ 119336eab1f5SPoul-Henning Kamp devstat_compute_etime(¤t->field, \ 119436eab1f5SPoul-Henning Kamp (previous ? &previous->field : NULL)) 119536eab1f5SPoul-Henning Kamp 1196c4a5ef6eSThomas Moestl int 1197c4a5ef6eSThomas Moestl devstat_compute_statistics(struct devstat *current, struct devstat *previous, 1198c4a5ef6eSThomas Moestl long double etime, ...) 1199c4a5ef6eSThomas Moestl { 1200c3508206SKenneth D. Merry const char *func_name = "devstat_compute_statistics"; 120136eab1f5SPoul-Henning Kamp u_int64_t totalbytes, totalbytesread, totalbyteswrite, totalbytesfree; 1202c4a5ef6eSThomas Moestl u_int64_t totaltransfers, totaltransfersread, totaltransferswrite; 1203c4a5ef6eSThomas Moestl u_int64_t totaltransfersother, totalblocks, totalblocksread; 120436eab1f5SPoul-Henning Kamp u_int64_t totalblockswrite, totaltransfersfree, totalblocksfree; 1205c4a5ef6eSThomas Moestl va_list ap; 1206c4a5ef6eSThomas Moestl devstat_metric metric; 1207c4a5ef6eSThomas Moestl u_int64_t *destu64; 1208c4a5ef6eSThomas Moestl long double *destld; 120936eab1f5SPoul-Henning Kamp int retval, i; 1210c4a5ef6eSThomas Moestl 1211c4a5ef6eSThomas Moestl retval = 0; 1212c4a5ef6eSThomas Moestl 1213c4a5ef6eSThomas Moestl /* 1214c4a5ef6eSThomas Moestl * current is the only mandatory field. 1215c4a5ef6eSThomas Moestl */ 1216c4a5ef6eSThomas Moestl if (current == NULL) { 1217c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1218c4a5ef6eSThomas Moestl "%s: current stats structure was NULL", func_name); 1219c4a5ef6eSThomas Moestl return(-1); 1220c4a5ef6eSThomas Moestl } 1221c4a5ef6eSThomas Moestl 122236eab1f5SPoul-Henning Kamp totalbytesread = DELTA(bytes, DEVSTAT_READ); 122336eab1f5SPoul-Henning Kamp totalbyteswrite = DELTA(bytes, DEVSTAT_WRITE); 122436eab1f5SPoul-Henning Kamp totalbytesfree = DELTA(bytes, DEVSTAT_FREE); 122536eab1f5SPoul-Henning Kamp totalbytes = totalbytesread + totalbyteswrite + totalbytesfree; 1226c4a5ef6eSThomas Moestl 122736eab1f5SPoul-Henning Kamp totaltransfersread = DELTA(operations, DEVSTAT_READ); 122836eab1f5SPoul-Henning Kamp totaltransferswrite = DELTA(operations, DEVSTAT_WRITE); 122936eab1f5SPoul-Henning Kamp totaltransfersother = DELTA(operations, DEVSTAT_NO_DATA); 123036eab1f5SPoul-Henning Kamp totaltransfersfree = DELTA(operations, DEVSTAT_FREE); 1231c4a5ef6eSThomas Moestl totaltransfers = totaltransfersread + totaltransferswrite + 123236eab1f5SPoul-Henning Kamp totaltransfersother + totaltransfersfree; 1233c4a5ef6eSThomas Moestl 1234c4a5ef6eSThomas Moestl totalblocks = totalbytes; 1235c4a5ef6eSThomas Moestl totalblocksread = totalbytesread; 1236c4a5ef6eSThomas Moestl totalblockswrite = totalbyteswrite; 123736eab1f5SPoul-Henning Kamp totalblocksfree = totalbytesfree; 1238c4a5ef6eSThomas Moestl 1239c4a5ef6eSThomas Moestl if (current->block_size > 0) { 1240c4a5ef6eSThomas Moestl totalblocks /= current->block_size; 1241c4a5ef6eSThomas Moestl totalblocksread /= current->block_size; 1242c4a5ef6eSThomas Moestl totalblockswrite /= current->block_size; 124336eab1f5SPoul-Henning Kamp totalblocksfree /= current->block_size; 1244c4a5ef6eSThomas Moestl } else { 1245c4a5ef6eSThomas Moestl totalblocks /= 512; 1246c4a5ef6eSThomas Moestl totalblocksread /= 512; 1247c4a5ef6eSThomas Moestl totalblockswrite /= 512; 124836eab1f5SPoul-Henning Kamp totalblocksfree /= 512; 1249c4a5ef6eSThomas Moestl } 1250c4a5ef6eSThomas Moestl 1251c4a5ef6eSThomas Moestl va_start(ap, etime); 1252c4a5ef6eSThomas Moestl 1253c4a5ef6eSThomas Moestl while ((metric = (devstat_metric)va_arg(ap, devstat_metric)) != 0) { 1254c4a5ef6eSThomas Moestl 1255c4a5ef6eSThomas Moestl if (metric == DSM_NONE) 1256c4a5ef6eSThomas Moestl break; 1257c4a5ef6eSThomas Moestl 1258c4a5ef6eSThomas Moestl if (metric >= DSM_MAX) { 1259c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1260c4a5ef6eSThomas Moestl "%s: metric %d is out of range", func_name, 1261c4a5ef6eSThomas Moestl metric); 1262c4a5ef6eSThomas Moestl retval = -1; 1263c4a5ef6eSThomas Moestl goto bailout; 1264c4a5ef6eSThomas Moestl } 1265c4a5ef6eSThomas Moestl 1266c4a5ef6eSThomas Moestl switch (devstat_arg_list[metric].argtype) { 1267c4a5ef6eSThomas Moestl case DEVSTAT_ARG_UINT64: 1268c4a5ef6eSThomas Moestl destu64 = (u_int64_t *)va_arg(ap, u_int64_t *); 1269c4a5ef6eSThomas Moestl break; 1270c4a5ef6eSThomas Moestl case DEVSTAT_ARG_LD: 1271c4a5ef6eSThomas Moestl destld = (long double *)va_arg(ap, long double *); 1272884539f7SKenneth D. Merry break; 1273884539f7SKenneth D. Merry case DEVSTAT_ARG_SKIP: 1274884539f7SKenneth D. Merry destld = (long double *)va_arg(ap, long double *); 1275c4a5ef6eSThomas Moestl break; 1276c4a5ef6eSThomas Moestl default: 1277c4a5ef6eSThomas Moestl retval = -1; 1278c4a5ef6eSThomas Moestl goto bailout; 1279c4a5ef6eSThomas Moestl break; /* NOTREACHED */ 1280c4a5ef6eSThomas Moestl } 1281c4a5ef6eSThomas Moestl 1282884539f7SKenneth D. Merry if (devstat_arg_list[metric].argtype == DEVSTAT_ARG_SKIP) 1283884539f7SKenneth D. Merry continue; 1284884539f7SKenneth D. Merry 1285c4a5ef6eSThomas Moestl switch (metric) { 1286c4a5ef6eSThomas Moestl case DSM_TOTAL_BYTES: 1287c4a5ef6eSThomas Moestl *destu64 = totalbytes; 1288c4a5ef6eSThomas Moestl break; 1289c4a5ef6eSThomas Moestl case DSM_TOTAL_BYTES_READ: 1290c4a5ef6eSThomas Moestl *destu64 = totalbytesread; 1291c4a5ef6eSThomas Moestl break; 1292c4a5ef6eSThomas Moestl case DSM_TOTAL_BYTES_WRITE: 1293c4a5ef6eSThomas Moestl *destu64 = totalbyteswrite; 1294c4a5ef6eSThomas Moestl break; 129536eab1f5SPoul-Henning Kamp case DSM_TOTAL_BYTES_FREE: 129636eab1f5SPoul-Henning Kamp *destu64 = totalbytesfree; 129736eab1f5SPoul-Henning Kamp break; 1298c4a5ef6eSThomas Moestl case DSM_TOTAL_TRANSFERS: 1299c4a5ef6eSThomas Moestl *destu64 = totaltransfers; 1300c4a5ef6eSThomas Moestl break; 1301c4a5ef6eSThomas Moestl case DSM_TOTAL_TRANSFERS_READ: 1302c4a5ef6eSThomas Moestl *destu64 = totaltransfersread; 1303c4a5ef6eSThomas Moestl break; 1304c4a5ef6eSThomas Moestl case DSM_TOTAL_TRANSFERS_WRITE: 1305c4a5ef6eSThomas Moestl *destu64 = totaltransferswrite; 1306c4a5ef6eSThomas Moestl break; 130736eab1f5SPoul-Henning Kamp case DSM_TOTAL_TRANSFERS_FREE: 130836eab1f5SPoul-Henning Kamp *destu64 = totaltransfersfree; 130936eab1f5SPoul-Henning Kamp break; 1310c4a5ef6eSThomas Moestl case DSM_TOTAL_TRANSFERS_OTHER: 1311c4a5ef6eSThomas Moestl *destu64 = totaltransfersother; 1312c4a5ef6eSThomas Moestl break; 1313c4a5ef6eSThomas Moestl case DSM_TOTAL_BLOCKS: 1314c4a5ef6eSThomas Moestl *destu64 = totalblocks; 1315c4a5ef6eSThomas Moestl break; 1316c4a5ef6eSThomas Moestl case DSM_TOTAL_BLOCKS_READ: 1317c4a5ef6eSThomas Moestl *destu64 = totalblocksread; 1318c4a5ef6eSThomas Moestl break; 1319c4a5ef6eSThomas Moestl case DSM_TOTAL_BLOCKS_WRITE: 1320c4a5ef6eSThomas Moestl *destu64 = totalblockswrite; 1321c4a5ef6eSThomas Moestl break; 132236eab1f5SPoul-Henning Kamp case DSM_TOTAL_BLOCKS_FREE: 132336eab1f5SPoul-Henning Kamp *destu64 = totalblocksfree; 132436eab1f5SPoul-Henning Kamp break; 1325c4a5ef6eSThomas Moestl case DSM_KB_PER_TRANSFER: 1326c4a5ef6eSThomas Moestl *destld = totalbytes; 1327c4a5ef6eSThomas Moestl *destld /= 1024; 1328c4a5ef6eSThomas Moestl if (totaltransfers > 0) 1329c4a5ef6eSThomas Moestl *destld /= totaltransfers; 1330c4a5ef6eSThomas Moestl else 1331c4a5ef6eSThomas Moestl *destld = 0.0; 1332c4a5ef6eSThomas Moestl break; 1333c4a5ef6eSThomas Moestl case DSM_KB_PER_TRANSFER_READ: 1334c4a5ef6eSThomas Moestl *destld = totalbytesread; 1335c4a5ef6eSThomas Moestl *destld /= 1024; 1336c4a5ef6eSThomas Moestl if (totaltransfersread > 0) 1337c4a5ef6eSThomas Moestl *destld /= totaltransfersread; 1338c4a5ef6eSThomas Moestl else 1339c4a5ef6eSThomas Moestl *destld = 0.0; 1340c4a5ef6eSThomas Moestl break; 1341c4a5ef6eSThomas Moestl case DSM_KB_PER_TRANSFER_WRITE: 1342c4a5ef6eSThomas Moestl *destld = totalbyteswrite; 1343c4a5ef6eSThomas Moestl *destld /= 1024; 1344c4a5ef6eSThomas Moestl if (totaltransferswrite > 0) 1345c4a5ef6eSThomas Moestl *destld /= totaltransferswrite; 1346c4a5ef6eSThomas Moestl else 1347c4a5ef6eSThomas Moestl *destld = 0.0; 1348c4a5ef6eSThomas Moestl break; 134936eab1f5SPoul-Henning Kamp case DSM_KB_PER_TRANSFER_FREE: 135036eab1f5SPoul-Henning Kamp *destld = totalbytesfree; 135136eab1f5SPoul-Henning Kamp *destld /= 1024; 135236eab1f5SPoul-Henning Kamp if (totaltransfersfree > 0) 135336eab1f5SPoul-Henning Kamp *destld /= totaltransfersfree; 135436eab1f5SPoul-Henning Kamp else 135536eab1f5SPoul-Henning Kamp *destld = 0.0; 135636eab1f5SPoul-Henning Kamp break; 1357c4a5ef6eSThomas Moestl case DSM_TRANSFERS_PER_SECOND: 1358c4a5ef6eSThomas Moestl if (etime > 0.0) { 1359c4a5ef6eSThomas Moestl *destld = totaltransfers; 1360c4a5ef6eSThomas Moestl *destld /= etime; 1361c4a5ef6eSThomas Moestl } else 1362c4a5ef6eSThomas Moestl *destld = 0.0; 1363c4a5ef6eSThomas Moestl break; 1364c4a5ef6eSThomas Moestl case DSM_TRANSFERS_PER_SECOND_READ: 1365c4a5ef6eSThomas Moestl if (etime > 0.0) { 1366c4a5ef6eSThomas Moestl *destld = totaltransfersread; 1367c4a5ef6eSThomas Moestl *destld /= etime; 1368c4a5ef6eSThomas Moestl } else 1369c4a5ef6eSThomas Moestl *destld = 0.0; 1370c4a5ef6eSThomas Moestl break; 1371c4a5ef6eSThomas Moestl case DSM_TRANSFERS_PER_SECOND_WRITE: 1372c4a5ef6eSThomas Moestl if (etime > 0.0) { 1373c4a5ef6eSThomas Moestl *destld = totaltransferswrite; 1374c4a5ef6eSThomas Moestl *destld /= etime; 1375c4a5ef6eSThomas Moestl } else 1376c4a5ef6eSThomas Moestl *destld = 0.0; 1377c4a5ef6eSThomas Moestl break; 137836eab1f5SPoul-Henning Kamp case DSM_TRANSFERS_PER_SECOND_FREE: 137936eab1f5SPoul-Henning Kamp if (etime > 0.0) { 138036eab1f5SPoul-Henning Kamp *destld = totaltransfersfree; 138136eab1f5SPoul-Henning Kamp *destld /= etime; 138236eab1f5SPoul-Henning Kamp } else 138336eab1f5SPoul-Henning Kamp *destld = 0.0; 138436eab1f5SPoul-Henning Kamp break; 1385c4a5ef6eSThomas Moestl case DSM_TRANSFERS_PER_SECOND_OTHER: 1386c4a5ef6eSThomas Moestl if (etime > 0.0) { 1387c4a5ef6eSThomas Moestl *destld = totaltransfersother; 1388c4a5ef6eSThomas Moestl *destld /= etime; 1389c4a5ef6eSThomas Moestl } else 1390c4a5ef6eSThomas Moestl *destld = 0.0; 1391c4a5ef6eSThomas Moestl break; 1392c4a5ef6eSThomas Moestl case DSM_MB_PER_SECOND: 1393c4a5ef6eSThomas Moestl *destld = totalbytes; 1394c4a5ef6eSThomas Moestl *destld /= 1024 * 1024; 1395c4a5ef6eSThomas Moestl if (etime > 0.0) 1396c4a5ef6eSThomas Moestl *destld /= etime; 1397c4a5ef6eSThomas Moestl else 1398c4a5ef6eSThomas Moestl *destld = 0.0; 1399c4a5ef6eSThomas Moestl break; 1400c4a5ef6eSThomas Moestl case DSM_MB_PER_SECOND_READ: 1401c4a5ef6eSThomas Moestl *destld = totalbytesread; 1402c4a5ef6eSThomas Moestl *destld /= 1024 * 1024; 1403c4a5ef6eSThomas Moestl if (etime > 0.0) 1404c4a5ef6eSThomas Moestl *destld /= etime; 1405c4a5ef6eSThomas Moestl else 1406c4a5ef6eSThomas Moestl *destld = 0.0; 1407c4a5ef6eSThomas Moestl break; 1408c4a5ef6eSThomas Moestl case DSM_MB_PER_SECOND_WRITE: 1409c4a5ef6eSThomas Moestl *destld = totalbyteswrite; 1410c4a5ef6eSThomas Moestl *destld /= 1024 * 1024; 1411c4a5ef6eSThomas Moestl if (etime > 0.0) 1412c4a5ef6eSThomas Moestl *destld /= etime; 1413c4a5ef6eSThomas Moestl else 1414c4a5ef6eSThomas Moestl *destld = 0.0; 1415c4a5ef6eSThomas Moestl break; 141636eab1f5SPoul-Henning Kamp case DSM_MB_PER_SECOND_FREE: 141736eab1f5SPoul-Henning Kamp *destld = totalbytesfree; 141836eab1f5SPoul-Henning Kamp *destld /= 1024 * 1024; 141936eab1f5SPoul-Henning Kamp if (etime > 0.0) 142036eab1f5SPoul-Henning Kamp *destld /= etime; 142136eab1f5SPoul-Henning Kamp else 142236eab1f5SPoul-Henning Kamp *destld = 0.0; 142336eab1f5SPoul-Henning Kamp break; 1424c4a5ef6eSThomas Moestl case DSM_BLOCKS_PER_SECOND: 1425c4a5ef6eSThomas Moestl *destld = totalblocks; 1426c4a5ef6eSThomas Moestl if (etime > 0.0) 1427c4a5ef6eSThomas Moestl *destld /= etime; 1428c4a5ef6eSThomas Moestl else 1429c4a5ef6eSThomas Moestl *destld = 0.0; 1430c4a5ef6eSThomas Moestl break; 1431c4a5ef6eSThomas Moestl case DSM_BLOCKS_PER_SECOND_READ: 1432c4a5ef6eSThomas Moestl *destld = totalblocksread; 1433c4a5ef6eSThomas Moestl if (etime > 0.0) 1434c4a5ef6eSThomas Moestl *destld /= etime; 1435c4a5ef6eSThomas Moestl else 1436c4a5ef6eSThomas Moestl *destld = 0.0; 1437c4a5ef6eSThomas Moestl break; 1438c4a5ef6eSThomas Moestl case DSM_BLOCKS_PER_SECOND_WRITE: 1439c4a5ef6eSThomas Moestl *destld = totalblockswrite; 1440c4a5ef6eSThomas Moestl if (etime > 0.0) 1441c4a5ef6eSThomas Moestl *destld /= etime; 1442c4a5ef6eSThomas Moestl else 1443c4a5ef6eSThomas Moestl *destld = 0.0; 1444c4a5ef6eSThomas Moestl break; 144536eab1f5SPoul-Henning Kamp case DSM_BLOCKS_PER_SECOND_FREE: 144636eab1f5SPoul-Henning Kamp *destld = totalblocksfree; 144736eab1f5SPoul-Henning Kamp if (etime > 0.0) 144836eab1f5SPoul-Henning Kamp *destld /= etime; 144936eab1f5SPoul-Henning Kamp else 145036eab1f5SPoul-Henning Kamp *destld = 0.0; 145136eab1f5SPoul-Henning Kamp break; 1452c4a5ef6eSThomas Moestl /* 1453c4a5ef6eSThomas Moestl * This calculation is somewhat bogus. It simply divides 1454c4a5ef6eSThomas Moestl * the elapsed time by the total number of transactions 1455c4a5ef6eSThomas Moestl * completed. While that does give the caller a good 1456c4a5ef6eSThomas Moestl * picture of the average rate of transaction completion, 1457c4a5ef6eSThomas Moestl * it doesn't necessarily give the caller a good view of 1458c4a5ef6eSThomas Moestl * how long transactions took to complete on average. 1459c4a5ef6eSThomas Moestl * Those two numbers will be different for a device that 1460c4a5ef6eSThomas Moestl * can handle more than one transaction at a time. e.g. 1461c4a5ef6eSThomas Moestl * SCSI disks doing tagged queueing. 1462c4a5ef6eSThomas Moestl * 1463c4a5ef6eSThomas Moestl * The only way to accurately determine the real average 1464c4a5ef6eSThomas Moestl * time per transaction would be to compute and store the 1465c4a5ef6eSThomas Moestl * time on a per-transaction basis. That currently isn't 1466c4a5ef6eSThomas Moestl * done in the kernel, and would only be desireable if it 1467c4a5ef6eSThomas Moestl * could be implemented in a somewhat non-intrusive and high 1468c4a5ef6eSThomas Moestl * performance way. 1469c4a5ef6eSThomas Moestl */ 1470c4a5ef6eSThomas Moestl case DSM_MS_PER_TRANSACTION: 1471c4a5ef6eSThomas Moestl if (totaltransfers > 0) { 147236eab1f5SPoul-Henning Kamp *destld = 0; 147336eab1f5SPoul-Henning Kamp for (i = 0; i < DEVSTAT_N_TRANS_FLAGS; i++) 147436eab1f5SPoul-Henning Kamp *destld += DELTA_T(duration[i]); 1475c4a5ef6eSThomas Moestl *destld /= totaltransfers; 1476c4a5ef6eSThomas Moestl *destld *= 1000; 1477c4a5ef6eSThomas Moestl } else 1478c4a5ef6eSThomas Moestl *destld = 0.0; 1479c4a5ef6eSThomas Moestl break; 1480c4a5ef6eSThomas Moestl /* 1481c4a5ef6eSThomas Moestl * As above, these next two really only give the average 1482c4a5ef6eSThomas Moestl * rate of completion for read and write transactions, not 1483c4a5ef6eSThomas Moestl * the average time the transaction took to complete. 1484c4a5ef6eSThomas Moestl */ 1485c4a5ef6eSThomas Moestl case DSM_MS_PER_TRANSACTION_READ: 1486c4a5ef6eSThomas Moestl if (totaltransfersread > 0) { 148736eab1f5SPoul-Henning Kamp *destld = DELTA_T(duration[DEVSTAT_READ]); 1488c4a5ef6eSThomas Moestl *destld /= totaltransfersread; 1489c4a5ef6eSThomas Moestl *destld *= 1000; 1490c4a5ef6eSThomas Moestl } else 1491c4a5ef6eSThomas Moestl *destld = 0.0; 1492c4a5ef6eSThomas Moestl break; 1493c4a5ef6eSThomas Moestl case DSM_MS_PER_TRANSACTION_WRITE: 1494c4a5ef6eSThomas Moestl if (totaltransferswrite > 0) { 149536eab1f5SPoul-Henning Kamp *destld = DELTA_T(duration[DEVSTAT_WRITE]); 1496c4a5ef6eSThomas Moestl *destld /= totaltransferswrite; 1497c4a5ef6eSThomas Moestl *destld *= 1000; 1498c4a5ef6eSThomas Moestl } else 1499c4a5ef6eSThomas Moestl *destld = 0.0; 1500c4a5ef6eSThomas Moestl break; 150136eab1f5SPoul-Henning Kamp case DSM_MS_PER_TRANSACTION_FREE: 150236eab1f5SPoul-Henning Kamp if (totaltransfersfree > 0) { 150336eab1f5SPoul-Henning Kamp *destld = DELTA_T(duration[DEVSTAT_FREE]); 150436eab1f5SPoul-Henning Kamp *destld /= totaltransfersfree; 150536eab1f5SPoul-Henning Kamp *destld *= 1000; 150636eab1f5SPoul-Henning Kamp } else 150736eab1f5SPoul-Henning Kamp *destld = 0.0; 150836eab1f5SPoul-Henning Kamp break; 150936eab1f5SPoul-Henning Kamp case DSM_MS_PER_TRANSACTION_OTHER: 151036eab1f5SPoul-Henning Kamp if (totaltransfersother > 0) { 151136eab1f5SPoul-Henning Kamp *destld = DELTA_T(duration[DEVSTAT_NO_DATA]); 151236eab1f5SPoul-Henning Kamp *destld /= totaltransfersother; 151336eab1f5SPoul-Henning Kamp *destld *= 1000; 151436eab1f5SPoul-Henning Kamp } else 151536eab1f5SPoul-Henning Kamp *destld = 0.0; 151636eab1f5SPoul-Henning Kamp break; 151736eab1f5SPoul-Henning Kamp case DSM_BUSY_PCT: 151836eab1f5SPoul-Henning Kamp *destld = DELTA_T(busy_time); 151936eab1f5SPoul-Henning Kamp if (*destld < 0) 152036eab1f5SPoul-Henning Kamp *destld = 0; 152136eab1f5SPoul-Henning Kamp *destld /= etime; 152236eab1f5SPoul-Henning Kamp *destld *= 100; 15231455c4e2SPoul-Henning Kamp if (*destld < 0) 15241455c4e2SPoul-Henning Kamp *destld = 0; 152536eab1f5SPoul-Henning Kamp break; 152636eab1f5SPoul-Henning Kamp case DSM_QUEUE_LENGTH: 152736eab1f5SPoul-Henning Kamp *destu64 = current->start_count - current->end_count; 152836eab1f5SPoul-Henning Kamp break; 152936eab1f5SPoul-Henning Kamp /* 153036eab1f5SPoul-Henning Kamp * XXX: comment out the default block to see if any case's are missing. 153136eab1f5SPoul-Henning Kamp */ 153236eab1f5SPoul-Henning Kamp #if 1 1533c4a5ef6eSThomas Moestl default: 1534c4a5ef6eSThomas Moestl /* 1535c4a5ef6eSThomas Moestl * This shouldn't happen, since we should have 1536c4a5ef6eSThomas Moestl * caught any out of range metrics at the top of 1537c4a5ef6eSThomas Moestl * the loop. 1538c4a5ef6eSThomas Moestl */ 1539c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1540c4a5ef6eSThomas Moestl "%s: unknown metric %d", func_name, metric); 1541c4a5ef6eSThomas Moestl retval = -1; 1542c4a5ef6eSThomas Moestl goto bailout; 1543c4a5ef6eSThomas Moestl break; /* NOTREACHED */ 154436eab1f5SPoul-Henning Kamp #endif 1545c4a5ef6eSThomas Moestl } 1546c4a5ef6eSThomas Moestl } 1547c4a5ef6eSThomas Moestl 1548c4a5ef6eSThomas Moestl bailout: 1549c4a5ef6eSThomas Moestl 1550c4a5ef6eSThomas Moestl va_end(ap); 1551c4a5ef6eSThomas Moestl return(retval); 1552c4a5ef6eSThomas Moestl } 1553c4a5ef6eSThomas Moestl 1554c4a5ef6eSThomas Moestl static int 1555c4a5ef6eSThomas Moestl readkmem(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes) 1556c4a5ef6eSThomas Moestl { 1557c3508206SKenneth D. Merry const char *func_name = "readkmem"; 1558c4a5ef6eSThomas Moestl 1559c4a5ef6eSThomas Moestl if (kvm_read(kd, addr, buf, nbytes) == -1) { 1560c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1561c4a5ef6eSThomas Moestl "%s: error reading value (kvm_read): %s", func_name, 1562c4a5ef6eSThomas Moestl kvm_geterr(kd)); 1563c4a5ef6eSThomas Moestl return(-1); 1564c4a5ef6eSThomas Moestl } 1565c4a5ef6eSThomas Moestl return(0); 1566c4a5ef6eSThomas Moestl } 1567c4a5ef6eSThomas Moestl 1568c4a5ef6eSThomas Moestl static int 1569c3508206SKenneth D. Merry readkmem_nl(kvm_t *kd, const char *name, void *buf, size_t nbytes) 1570c4a5ef6eSThomas Moestl { 1571c3508206SKenneth D. Merry const char *func_name = "readkmem_nl"; 1572c3508206SKenneth D. Merry struct nlist nl[2]; 1573c3508206SKenneth D. Merry 1574c3508206SKenneth D. Merry (const char *)nl[0].n_name = name; 1575c3508206SKenneth D. Merry nl[1].n_name = NULL; 1576c4a5ef6eSThomas Moestl 1577c4a5ef6eSThomas Moestl if (kvm_nlist(kd, nl) == -1) { 1578c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1579c4a5ef6eSThomas Moestl "%s: error getting name list (kvm_nlist): %s", 1580c4a5ef6eSThomas Moestl func_name, kvm_geterr(kd)); 1581c4a5ef6eSThomas Moestl return(-1); 1582c4a5ef6eSThomas Moestl } 1583c4a5ef6eSThomas Moestl return(readkmem(kd, nl[0].n_value, buf, nbytes)); 1584c4a5ef6eSThomas Moestl } 1585c4a5ef6eSThomas Moestl 1586c4a5ef6eSThomas Moestl /* 1587c4a5ef6eSThomas Moestl * This duplicates the functionality of the kernel sysctl handler for poking 1588c4a5ef6eSThomas Moestl * through crash dumps. 1589c4a5ef6eSThomas Moestl */ 1590c4a5ef6eSThomas Moestl static char * 1591c4a5ef6eSThomas Moestl get_devstat_kvm(kvm_t *kd) 1592c4a5ef6eSThomas Moestl { 1593c4a5ef6eSThomas Moestl int error, i, wp; 1594c4a5ef6eSThomas Moestl long gen; 1595c4a5ef6eSThomas Moestl struct devstat *nds; 1596c4a5ef6eSThomas Moestl struct devstat ds; 1597c4a5ef6eSThomas Moestl struct devstatlist dhead; 1598c4a5ef6eSThomas Moestl int num_devs; 1599c4a5ef6eSThomas Moestl char *rv = NULL; 1600c3508206SKenneth D. Merry const char *func_name = "get_devstat_kvm"; 1601c4a5ef6eSThomas Moestl 16027194d335SPoul-Henning Kamp if ((num_devs = devstat_getnumdevs(kd)) <= 0) 1603c4a5ef6eSThomas Moestl return(NULL); 1604c4a5ef6eSThomas Moestl error = 0; 1605c4a5ef6eSThomas Moestl if (KREADNL(kd, X_DEVICE_STATQ, dhead) == -1) 1606c4a5ef6eSThomas Moestl return(NULL); 1607c4a5ef6eSThomas Moestl 1608c4a5ef6eSThomas Moestl nds = STAILQ_FIRST(&dhead); 1609c4a5ef6eSThomas Moestl 1610c4a5ef6eSThomas Moestl if ((rv = malloc(sizeof(gen))) == NULL) { 1611c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1612c4a5ef6eSThomas Moestl "%s: out of memory (initial malloc failed)", 1613c4a5ef6eSThomas Moestl func_name); 1614c4a5ef6eSThomas Moestl return(NULL); 1615c4a5ef6eSThomas Moestl } 16167194d335SPoul-Henning Kamp gen = devstat_getgeneration(kd); 1617c4a5ef6eSThomas Moestl memcpy(rv, &gen, sizeof(gen)); 1618c4a5ef6eSThomas Moestl wp = sizeof(gen); 1619c4a5ef6eSThomas Moestl /* 1620c4a5ef6eSThomas Moestl * Now push out all the devices. 1621c4a5ef6eSThomas Moestl */ 1622c4a5ef6eSThomas Moestl for (i = 0; (nds != NULL) && (i < num_devs); 1623c4a5ef6eSThomas Moestl nds = STAILQ_NEXT(nds, dev_links), i++) { 1624c4a5ef6eSThomas Moestl if (readkmem(kd, (long)nds, &ds, sizeof(ds)) == -1) { 1625c4a5ef6eSThomas Moestl free(rv); 1626c4a5ef6eSThomas Moestl return(NULL); 1627c4a5ef6eSThomas Moestl } 1628c4a5ef6eSThomas Moestl nds = &ds; 1629c4a5ef6eSThomas Moestl rv = (char *)reallocf(rv, sizeof(gen) + 1630c4a5ef6eSThomas Moestl sizeof(ds) * (i + 1)); 1631c4a5ef6eSThomas Moestl if (rv == NULL) { 1632c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1633c4a5ef6eSThomas Moestl "%s: out of memory (malloc failed)", 1634c4a5ef6eSThomas Moestl func_name); 1635c4a5ef6eSThomas Moestl return(NULL); 1636c4a5ef6eSThomas Moestl } 1637c4a5ef6eSThomas Moestl memcpy(rv + wp, &ds, sizeof(ds)); 1638c4a5ef6eSThomas Moestl wp += sizeof(ds); 1639c4a5ef6eSThomas Moestl } 1640c4a5ef6eSThomas Moestl return(rv); 1641c4a5ef6eSThomas Moestl } 1642