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> 471f85f715SBruce Evans #include <nlist.h> 4828fb27baSJustin T. Gibbs 4928fb27baSJustin T. Gibbs #include "devstat.h" 5028fb27baSJustin T. Gibbs 510b1b370cSPoul-Henning Kamp int 520b1b370cSPoul-Henning Kamp compute_stats(struct devstat *current, struct devstat *previous, 530b1b370cSPoul-Henning Kamp long double etime, u_int64_t *total_bytes, 540b1b370cSPoul-Henning Kamp u_int64_t *total_transfers, u_int64_t *total_blocks, 550b1b370cSPoul-Henning Kamp long double *kb_per_transfer, long double *transfers_per_second, 560b1b370cSPoul-Henning Kamp long double *mb_per_second, long double *blocks_per_second, 570b1b370cSPoul-Henning Kamp long double *ms_per_transaction); 580b1b370cSPoul-Henning Kamp 59c4a5ef6eSThomas Moestl typedef enum { 60c4a5ef6eSThomas Moestl DEVSTAT_ARG_NOTYPE, 61c4a5ef6eSThomas Moestl DEVSTAT_ARG_UINT64, 62884539f7SKenneth D. Merry DEVSTAT_ARG_LD, 63884539f7SKenneth D. Merry DEVSTAT_ARG_SKIP 64c4a5ef6eSThomas Moestl } devstat_arg_type; 65c4a5ef6eSThomas Moestl 6628fb27baSJustin T. Gibbs char devstat_errbuf[DEVSTAT_ERRBUF_SIZE]; 6728fb27baSJustin T. Gibbs 6828fb27baSJustin T. Gibbs /* 6928fb27baSJustin T. Gibbs * Table to match descriptive strings with device types. These are in 7028fb27baSJustin T. Gibbs * order from most common to least common to speed search time. 7128fb27baSJustin T. Gibbs */ 7228fb27baSJustin T. Gibbs struct devstat_match_table match_table[] = { 7328fb27baSJustin T. Gibbs {"da", DEVSTAT_TYPE_DIRECT, DEVSTAT_MATCH_TYPE}, 7428fb27baSJustin T. Gibbs {"cd", DEVSTAT_TYPE_CDROM, DEVSTAT_MATCH_TYPE}, 7528fb27baSJustin T. Gibbs {"scsi", DEVSTAT_TYPE_IF_SCSI, DEVSTAT_MATCH_IF}, 7628fb27baSJustin T. Gibbs {"ide", DEVSTAT_TYPE_IF_IDE, DEVSTAT_MATCH_IF}, 7728fb27baSJustin T. Gibbs {"other", DEVSTAT_TYPE_IF_OTHER, 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 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 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 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 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 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 int oldnumdevs; 338bcc6a3daSKenneth D. Merry long oldgeneration; 33928fb27baSJustin T. Gibbs int retval = 0; 34028fb27baSJustin T. Gibbs struct devinfo *dinfo; 3417194d335SPoul-Henning Kamp struct timespec ts; 34228fb27baSJustin T. Gibbs 34328fb27baSJustin T. Gibbs dinfo = stats->dinfo; 34428fb27baSJustin T. Gibbs 34528fb27baSJustin T. Gibbs if (dinfo == NULL) { 346c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 3477d72cda3SMaxime Henrion "%s: stats->dinfo was NULL", __func__); 34828fb27baSJustin T. Gibbs return(-1); 34928fb27baSJustin T. Gibbs } 35028fb27baSJustin T. Gibbs 35128fb27baSJustin T. Gibbs oldnumdevs = dinfo->numdevs; 35228fb27baSJustin T. Gibbs oldgeneration = dinfo->generation; 35328fb27baSJustin T. Gibbs 354ef6b3fcfSSergey Kandaurov if (kd == NULL) { 3557194d335SPoul-Henning Kamp clock_gettime(CLOCK_MONOTONIC, &ts); 3567194d335SPoul-Henning Kamp stats->snap_time = ts.tv_sec + ts.tv_nsec * 1e-9; 357c4a5ef6eSThomas Moestl 358c4a5ef6eSThomas Moestl /* If this is our first time through, mem_ptr will be null. */ 35928fb27baSJustin T. Gibbs if (dinfo->mem_ptr == NULL) { 36028fb27baSJustin T. Gibbs /* 36128fb27baSJustin T. Gibbs * Get the number of devices. If it's negative, it's an 36228fb27baSJustin T. Gibbs * error. Don't bother setting the error string, since 36328fb27baSJustin T. Gibbs * getnumdevs() has already done that for us. 36428fb27baSJustin T. Gibbs */ 36516830b0cSPoul-Henning Kamp if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0) 36628fb27baSJustin T. Gibbs return(-1); 36728fb27baSJustin T. Gibbs 36828fb27baSJustin T. Gibbs /* 369c4a5ef6eSThomas Moestl * The kern.devstat.all sysctl returns the current 370c4a5ef6eSThomas Moestl * generation number, as well as all the devices. 371c4a5ef6eSThomas Moestl * So we need four bytes more. 37228fb27baSJustin T. Gibbs */ 373c4a5ef6eSThomas Moestl dssize = (dinfo->numdevs * sizeof(struct devstat)) + 374c4a5ef6eSThomas Moestl sizeof(long); 37528fb27baSJustin T. Gibbs dinfo->mem_ptr = (u_int8_t *)malloc(dssize); 376a18e4616SGuy Helmer if (dinfo->mem_ptr == NULL) { 377a18e4616SGuy Helmer snprintf(devstat_errbuf, sizeof(devstat_errbuf), 378a18e4616SGuy Helmer "%s: Cannot allocate memory for mem_ptr element", 379a18e4616SGuy Helmer __func__); 380a18e4616SGuy Helmer return(-1); 381a18e4616SGuy Helmer } 38228fb27baSJustin T. Gibbs } else 383c4a5ef6eSThomas Moestl dssize = (dinfo->numdevs * sizeof(struct devstat)) + 384c4a5ef6eSThomas Moestl sizeof(long); 38528fb27baSJustin T. Gibbs 38628fb27baSJustin T. Gibbs /* 38728fb27baSJustin T. Gibbs * Request all of the devices. We only really allow for one 388c4a5ef6eSThomas Moestl * ENOMEM failure. It would, of course, be possible to just go 389c4a5ef6eSThomas Moestl * in a loop and keep reallocing the device structure until we 390c4a5ef6eSThomas Moestl * don't get ENOMEM back. I'm not sure it's worth it, though. 391c4a5ef6eSThomas Moestl * If devices are being added to the system that quickly, maybe 392c4a5ef6eSThomas Moestl * the user can just wait until all devices are added. 39328fb27baSJustin T. Gibbs */ 39436eab1f5SPoul-Henning Kamp for (;;) { 39536eab1f5SPoul-Henning Kamp error = sysctlbyname("kern.devstat.all", 39636eab1f5SPoul-Henning Kamp dinfo->mem_ptr, 39736eab1f5SPoul-Henning Kamp &dssize, NULL, 0); 39836eab1f5SPoul-Henning Kamp if (error != -1 || errno != EBUSY) 39936eab1f5SPoul-Henning Kamp break; 40036eab1f5SPoul-Henning Kamp } 40136eab1f5SPoul-Henning Kamp if (error == -1) { 40228fb27baSJustin T. Gibbs /* 40328fb27baSJustin T. Gibbs * If we get ENOMEM back, that means that there are 40428fb27baSJustin T. Gibbs * more devices now, so we need to allocate more 40528fb27baSJustin T. Gibbs * space for the device array. 40628fb27baSJustin T. Gibbs */ 40728fb27baSJustin T. Gibbs if (errno == ENOMEM) { 40828fb27baSJustin T. Gibbs /* 409c4a5ef6eSThomas Moestl * No need to set the error string here, 4107194d335SPoul-Henning Kamp * devstat_getnumdevs() will do that if it fails. 41128fb27baSJustin T. Gibbs */ 41216830b0cSPoul-Henning Kamp if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0) 41328fb27baSJustin T. Gibbs return(-1); 41428fb27baSJustin T. Gibbs 415c4a5ef6eSThomas Moestl dssize = (dinfo->numdevs * 416c4a5ef6eSThomas Moestl sizeof(struct devstat)) + sizeof(long); 417c4a5ef6eSThomas Moestl dinfo->mem_ptr = (u_int8_t *) 418c4a5ef6eSThomas Moestl realloc(dinfo->mem_ptr, dssize); 41928fb27baSJustin T. Gibbs if ((error = sysctlbyname("kern.devstat.all", 42028fb27baSJustin T. Gibbs dinfo->mem_ptr, &dssize, NULL, 0)) == -1) { 421c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, 422c4a5ef6eSThomas Moestl sizeof(devstat_errbuf), 423c4a5ef6eSThomas Moestl "%s: error getting device " 4247d72cda3SMaxime Henrion "stats\n%s: %s", __func__, 4257d72cda3SMaxime Henrion __func__, strerror(errno)); 42628fb27baSJustin T. Gibbs return(-1); 42728fb27baSJustin T. Gibbs } 42828fb27baSJustin T. Gibbs } else { 429c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 43028fb27baSJustin T. Gibbs "%s: error getting device stats\n" 4317d72cda3SMaxime Henrion "%s: %s", __func__, __func__, 43228fb27baSJustin T. Gibbs strerror(errno)); 43328fb27baSJustin T. Gibbs return(-1); 43428fb27baSJustin T. Gibbs } 43528fb27baSJustin T. Gibbs } 43628fb27baSJustin T. Gibbs 437c4a5ef6eSThomas Moestl } else { 438ef6b3fcfSSergey Kandaurov if (KREADNL(kd, X_TIME_UPTIME, ts.tv_sec) == -1) 439ef6b3fcfSSergey Kandaurov return(-1); 440ef6b3fcfSSergey Kandaurov else 441ef6b3fcfSSergey Kandaurov stats->snap_time = ts.tv_sec; 442ef6b3fcfSSergey Kandaurov 443c4a5ef6eSThomas Moestl /* 444c4a5ef6eSThomas Moestl * This is of course non-atomic, but since we are working 445c4a5ef6eSThomas Moestl * on a core dump, the generation is unlikely to change 446c4a5ef6eSThomas Moestl */ 44716830b0cSPoul-Henning Kamp if ((dinfo->numdevs = devstat_getnumdevs(kd)) == -1) 448c4a5ef6eSThomas Moestl return(-1); 449140490e2SMaxime Henrion if ((dinfo->mem_ptr = (u_int8_t *)get_devstat_kvm(kd)) == NULL) 450c4a5ef6eSThomas Moestl return(-1); 451c4a5ef6eSThomas Moestl } 45228fb27baSJustin T. Gibbs /* 45328fb27baSJustin T. Gibbs * The sysctl spits out the generation as the first four bytes, 45428fb27baSJustin T. Gibbs * then all of the device statistics structures. 45528fb27baSJustin T. Gibbs */ 456bcc6a3daSKenneth D. Merry dinfo->generation = *(long *)dinfo->mem_ptr; 45728fb27baSJustin T. Gibbs 45828fb27baSJustin T. Gibbs /* 45928fb27baSJustin T. Gibbs * If the generation has changed, and if the current number of 46028fb27baSJustin T. Gibbs * devices is not the same as the number of devices recorded in the 46128fb27baSJustin T. Gibbs * devinfo structure, it is likely that the device list has shrunk. 46228fb27baSJustin T. Gibbs * The reason that it is likely that the device list has shrunk in 46328fb27baSJustin T. Gibbs * this case is that if the device list has grown, the sysctl above 46428fb27baSJustin T. Gibbs * will return an ENOMEM error, and we will reset the number of 46528fb27baSJustin T. Gibbs * devices and reallocate the device array. If the second sysctl 46628fb27baSJustin T. Gibbs * fails, we will return an error and therefore never get to this 46728fb27baSJustin T. Gibbs * point. If the device list has shrunk, the sysctl will not 46828fb27baSJustin T. Gibbs * return an error since we have more space allocated than is 46928fb27baSJustin T. Gibbs * necessary. So, in the shrinkage case, we catch it here and 47028fb27baSJustin T. Gibbs * reallocate the array so that we don't use any more space than is 47128fb27baSJustin T. Gibbs * necessary. 47228fb27baSJustin T. Gibbs */ 47328fb27baSJustin T. Gibbs if (oldgeneration != dinfo->generation) { 47416830b0cSPoul-Henning Kamp if (devstat_getnumdevs(kd) != dinfo->numdevs) { 47516830b0cSPoul-Henning Kamp if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0) 47628fb27baSJustin T. Gibbs return(-1); 47728fb27baSJustin T. Gibbs dssize = (dinfo->numdevs * sizeof(struct devstat)) + 478bcc6a3daSKenneth D. Merry sizeof(long); 47928fb27baSJustin T. Gibbs dinfo->mem_ptr = (u_int8_t *)realloc(dinfo->mem_ptr, 48028fb27baSJustin T. Gibbs dssize); 48128fb27baSJustin T. Gibbs } 48228fb27baSJustin T. Gibbs retval = 1; 48328fb27baSJustin T. Gibbs } 48428fb27baSJustin T. Gibbs 485bcc6a3daSKenneth D. Merry dinfo->devices = (struct devstat *)(dinfo->mem_ptr + sizeof(long)); 48628fb27baSJustin T. Gibbs 48728fb27baSJustin T. Gibbs return(retval); 48828fb27baSJustin T. Gibbs } 48928fb27baSJustin T. Gibbs 49028fb27baSJustin T. Gibbs /* 49128fb27baSJustin T. Gibbs * selectdevs(): 49228fb27baSJustin T. Gibbs * 49328fb27baSJustin T. Gibbs * Devices are selected/deselected based upon the following criteria: 49428fb27baSJustin T. Gibbs * - devices specified by the user on the command line 49528fb27baSJustin T. Gibbs * - devices matching any device type expressions given on the command line 49628fb27baSJustin T. Gibbs * - devices with the highest I/O, if 'top' mode is enabled 49728fb27baSJustin T. Gibbs * - the first n unselected devices in the device list, if maxshowdevs 49828fb27baSJustin T. Gibbs * devices haven't already been selected and if the user has not 49928fb27baSJustin T. Gibbs * specified any devices on the command line and if we're in "add" mode. 50028fb27baSJustin T. Gibbs * 50128fb27baSJustin T. Gibbs * Input parameters: 50228fb27baSJustin T. Gibbs * - device selection list (dev_select) 50328fb27baSJustin T. Gibbs * - current number of devices selected (num_selected) 50428fb27baSJustin T. Gibbs * - total number of devices in the selection list (num_selections) 50528fb27baSJustin T. Gibbs * - devstat generation as of the last time selectdevs() was called 50628fb27baSJustin T. Gibbs * (select_generation) 50728fb27baSJustin T. Gibbs * - current devstat generation (current_generation) 50828fb27baSJustin T. Gibbs * - current list of devices and statistics (devices) 50928fb27baSJustin T. Gibbs * - number of devices in the current device list (numdevs) 51028fb27baSJustin T. Gibbs * - compiled version of the command line device type arguments (matches) 51128fb27baSJustin T. Gibbs * - This is optional. If the number of devices is 0, this will be ignored. 51228fb27baSJustin T. Gibbs * - The matching code pays attention to the current selection mode. So 51328fb27baSJustin T. Gibbs * if you pass in a matching expression, it will be evaluated based 51428fb27baSJustin T. Gibbs * upon the selection mode that is passed in. See below for details. 51528fb27baSJustin T. Gibbs * - number of device type matching expressions (num_matches) 51628fb27baSJustin T. Gibbs * - Set to 0 to disable the matching code. 51728fb27baSJustin T. Gibbs * - list of devices specified on the command line by the user (dev_selections) 51828fb27baSJustin T. Gibbs * - number of devices selected on the command line by the user 51928fb27baSJustin T. Gibbs * (num_dev_selections) 52028fb27baSJustin T. Gibbs * - Our selection mode. There are four different selection modes: 52128fb27baSJustin T. Gibbs * - add mode. (DS_SELECT_ADD) Any devices matching devices explicitly 52228fb27baSJustin T. Gibbs * selected by the user or devices matching a pattern given by the 52328fb27baSJustin T. Gibbs * user will be selected in addition to devices that are already 52428fb27baSJustin T. Gibbs * selected. Additional devices will be selected, up to maxshowdevs 52528fb27baSJustin T. Gibbs * number of devices. 52628fb27baSJustin T. Gibbs * - only mode. (DS_SELECT_ONLY) Only devices matching devices 52728fb27baSJustin T. Gibbs * explicitly given by the user or devices matching a pattern 52828fb27baSJustin T. Gibbs * given by the user will be selected. No other devices will be 52928fb27baSJustin T. Gibbs * selected. 53028fb27baSJustin T. Gibbs * - addonly mode. (DS_SELECT_ADDONLY) This is similar to add and 53128fb27baSJustin T. Gibbs * only. Basically, this will not de-select any devices that are 53228fb27baSJustin T. Gibbs * current selected, as only mode would, but it will also not 53328fb27baSJustin T. Gibbs * gratuitously select up to maxshowdevs devices as add mode would. 53428fb27baSJustin T. Gibbs * - remove mode. (DS_SELECT_REMOVE) Any devices matching devices 53528fb27baSJustin T. Gibbs * explicitly selected by the user or devices matching a pattern 53628fb27baSJustin T. Gibbs * given by the user will be de-selected. 53728fb27baSJustin T. Gibbs * - maximum number of devices we can select (maxshowdevs) 53828fb27baSJustin T. Gibbs * - flag indicating whether or not we're in 'top' mode (perf_select) 53928fb27baSJustin T. Gibbs * 54028fb27baSJustin T. Gibbs * Output data: 54128fb27baSJustin T. Gibbs * - the device selection list may be modified and passed back out 54228fb27baSJustin T. Gibbs * - the number of devices selected and the total number of items in the 54328fb27baSJustin T. Gibbs * device selection list may be changed 54428fb27baSJustin T. Gibbs * - the selection generation may be changed to match the current generation 54528fb27baSJustin T. Gibbs * 54628fb27baSJustin T. Gibbs * Return values: 54728fb27baSJustin T. Gibbs * -1 -- error 54828fb27baSJustin T. Gibbs * 0 -- selected devices are unchanged 54928fb27baSJustin T. Gibbs * 1 -- selected devices changed 55028fb27baSJustin T. Gibbs */ 55128fb27baSJustin T. Gibbs int 552c4a5ef6eSThomas Moestl devstat_selectdevs(struct device_selection **dev_select, int *num_selected, 553bcc6a3daSKenneth D. Merry int *num_selections, long *select_generation, 554c4a5ef6eSThomas Moestl long current_generation, struct devstat *devices, 555c4a5ef6eSThomas Moestl int numdevs, struct devstat_match *matches, int num_matches, 55628fb27baSJustin T. Gibbs char **dev_selections, int num_dev_selections, 557c4a5ef6eSThomas Moestl devstat_select_mode select_mode, int maxshowdevs, 558c4a5ef6eSThomas Moestl int perf_select) 55928fb27baSJustin T. Gibbs { 560be04b6d1SDavid E. O'Brien int i, j, k; 56128fb27baSJustin T. Gibbs int init_selections = 0, init_selected_var = 0; 56228fb27baSJustin T. Gibbs struct device_selection *old_dev_select = NULL; 56328fb27baSJustin T. Gibbs int old_num_selections = 0, old_num_selected; 56428fb27baSJustin T. Gibbs int selection_number = 0; 56528fb27baSJustin T. Gibbs int changed = 0, found = 0; 56628fb27baSJustin T. Gibbs 567d2d0b144SPoul-Henning Kamp if ((dev_select == NULL) || (devices == NULL) || (numdevs < 0)) 56828fb27baSJustin T. Gibbs return(-1); 56928fb27baSJustin T. Gibbs 57028fb27baSJustin T. Gibbs /* 57128fb27baSJustin T. Gibbs * We always want to make sure that we have as many dev_select 57228fb27baSJustin T. Gibbs * entries as there are devices. 57328fb27baSJustin T. Gibbs */ 57428fb27baSJustin T. Gibbs /* 57528fb27baSJustin T. Gibbs * In this case, we haven't selected devices before. 57628fb27baSJustin T. Gibbs */ 57728fb27baSJustin T. Gibbs if (*dev_select == NULL) { 57828fb27baSJustin T. Gibbs *dev_select = (struct device_selection *)malloc(numdevs * 57928fb27baSJustin T. Gibbs sizeof(struct device_selection)); 58028fb27baSJustin T. Gibbs *select_generation = current_generation; 58128fb27baSJustin T. Gibbs init_selections = 1; 58228fb27baSJustin T. Gibbs changed = 1; 58328fb27baSJustin T. Gibbs /* 58428fb27baSJustin T. Gibbs * In this case, we have selected devices before, but the device 58528fb27baSJustin T. Gibbs * list has changed since we last selected devices, so we need to 58628fb27baSJustin T. Gibbs * either enlarge or reduce the size of the device selection list. 58728fb27baSJustin T. Gibbs */ 58828fb27baSJustin T. Gibbs } else if (*num_selections != numdevs) { 589a18e4616SGuy Helmer *dev_select = (struct device_selection *)reallocf(*dev_select, 59028fb27baSJustin T. Gibbs numdevs * sizeof(struct device_selection)); 59128fb27baSJustin T. Gibbs *select_generation = current_generation; 59228fb27baSJustin T. Gibbs init_selections = 1; 59328fb27baSJustin T. Gibbs /* 59428fb27baSJustin T. Gibbs * In this case, we've selected devices before, and the selection 59528fb27baSJustin T. Gibbs * list is the same size as it was the last time, but the device 59628fb27baSJustin T. Gibbs * list has changed. 59728fb27baSJustin T. Gibbs */ 59828fb27baSJustin T. Gibbs } else if (*select_generation < current_generation) { 59928fb27baSJustin T. Gibbs *select_generation = current_generation; 60028fb27baSJustin T. Gibbs init_selections = 1; 60128fb27baSJustin T. Gibbs } 60228fb27baSJustin T. Gibbs 603a18e4616SGuy Helmer if (*dev_select == NULL) { 604a18e4616SGuy Helmer snprintf(devstat_errbuf, sizeof(devstat_errbuf), 605a18e4616SGuy Helmer "%s: Cannot (re)allocate memory for dev_select argument", 606a18e4616SGuy Helmer __func__); 607a18e4616SGuy Helmer return(-1); 608a18e4616SGuy Helmer } 609a18e4616SGuy Helmer 61028fb27baSJustin T. Gibbs /* 61128fb27baSJustin T. Gibbs * If we're in "only" mode, we want to clear out the selected 61228fb27baSJustin T. Gibbs * variable since we're going to select exactly what the user wants 61328fb27baSJustin T. Gibbs * this time through. 61428fb27baSJustin T. Gibbs */ 61528fb27baSJustin T. Gibbs if (select_mode == DS_SELECT_ONLY) 61628fb27baSJustin T. Gibbs init_selected_var = 1; 61728fb27baSJustin T. Gibbs 61828fb27baSJustin T. Gibbs /* 61928fb27baSJustin T. Gibbs * In all cases, we want to back up the number of selected devices. 62028fb27baSJustin T. Gibbs * It is a quick and accurate way to determine whether the selected 62128fb27baSJustin T. Gibbs * devices have changed. 62228fb27baSJustin T. Gibbs */ 62328fb27baSJustin T. Gibbs old_num_selected = *num_selected; 62428fb27baSJustin T. Gibbs 62528fb27baSJustin T. Gibbs /* 62628fb27baSJustin T. Gibbs * We want to make a backup of the current selection list if 62728fb27baSJustin T. Gibbs * the list of devices has changed, or if we're in performance 62828fb27baSJustin T. Gibbs * selection mode. In both cases, we don't want to make a backup 62928fb27baSJustin T. Gibbs * if we already know for sure that the list will be different. 63028fb27baSJustin T. Gibbs * This is certainly the case if this is our first time through the 63128fb27baSJustin T. Gibbs * selection code. 63228fb27baSJustin T. Gibbs */ 63328fb27baSJustin T. Gibbs if (((init_selected_var != 0) || (init_selections != 0) 63428fb27baSJustin T. Gibbs || (perf_select != 0)) && (changed == 0)){ 63528fb27baSJustin T. Gibbs old_dev_select = (struct device_selection *)malloc( 63628fb27baSJustin T. Gibbs *num_selections * sizeof(struct device_selection)); 637a18e4616SGuy Helmer if (old_dev_select == NULL) { 638a18e4616SGuy Helmer snprintf(devstat_errbuf, sizeof(devstat_errbuf), 639a18e4616SGuy Helmer "%s: Cannot allocate memory for selection list backup", 640a18e4616SGuy Helmer __func__); 641a18e4616SGuy Helmer return(-1); 642a18e4616SGuy Helmer } 64328fb27baSJustin T. Gibbs old_num_selections = *num_selections; 64428fb27baSJustin T. Gibbs bcopy(*dev_select, old_dev_select, 64528fb27baSJustin T. Gibbs sizeof(struct device_selection) * *num_selections); 64628fb27baSJustin T. Gibbs } 64728fb27baSJustin T. Gibbs 64828fb27baSJustin T. Gibbs if (init_selections != 0) { 64928fb27baSJustin T. Gibbs bzero(*dev_select, sizeof(struct device_selection) * numdevs); 65028fb27baSJustin T. Gibbs 65128fb27baSJustin T. Gibbs for (i = 0; i < numdevs; i++) { 65228fb27baSJustin T. Gibbs (*dev_select)[i].device_number = 65328fb27baSJustin T. Gibbs devices[i].device_number; 65428fb27baSJustin T. Gibbs strncpy((*dev_select)[i].device_name, 65528fb27baSJustin T. Gibbs devices[i].device_name, 65628fb27baSJustin T. Gibbs DEVSTAT_NAME_LEN); 65717ee2b20SKenneth D. Merry (*dev_select)[i].device_name[DEVSTAT_NAME_LEN - 1]='\0'; 65828fb27baSJustin T. Gibbs (*dev_select)[i].unit_number = devices[i].unit_number; 65928fb27baSJustin T. Gibbs (*dev_select)[i].position = i; 66028fb27baSJustin T. Gibbs } 66128fb27baSJustin T. Gibbs *num_selections = numdevs; 66228fb27baSJustin T. Gibbs } else if (init_selected_var != 0) { 66328fb27baSJustin T. Gibbs for (i = 0; i < numdevs; i++) 66428fb27baSJustin T. Gibbs (*dev_select)[i].selected = 0; 66528fb27baSJustin T. Gibbs } 66628fb27baSJustin T. Gibbs 66728fb27baSJustin T. Gibbs /* we haven't gotten around to selecting anything yet.. */ 66828fb27baSJustin T. Gibbs if ((select_mode == DS_SELECT_ONLY) || (init_selections != 0) 66928fb27baSJustin T. Gibbs || (init_selected_var != 0)) 67028fb27baSJustin T. Gibbs *num_selected = 0; 67128fb27baSJustin T. Gibbs 67228fb27baSJustin T. Gibbs /* 67328fb27baSJustin T. Gibbs * Look through any devices the user specified on the command line 67428fb27baSJustin T. Gibbs * and see if they match known devices. If so, select them. 67528fb27baSJustin T. Gibbs */ 67628fb27baSJustin T. Gibbs for (i = 0; (i < *num_selections) && (num_dev_selections > 0); i++) { 67728fb27baSJustin T. Gibbs char tmpstr[80]; 67828fb27baSJustin T. Gibbs 67917ee2b20SKenneth D. Merry snprintf(tmpstr, sizeof(tmpstr), "%s%d", 68017ee2b20SKenneth D. Merry (*dev_select)[i].device_name, 68128fb27baSJustin T. Gibbs (*dev_select)[i].unit_number); 68228fb27baSJustin T. Gibbs for (j = 0; j < num_dev_selections; j++) { 68328fb27baSJustin T. Gibbs if (strcmp(tmpstr, dev_selections[j]) == 0) { 68428fb27baSJustin T. Gibbs /* 68528fb27baSJustin T. Gibbs * Here we do different things based on the 68628fb27baSJustin T. Gibbs * mode we're in. If we're in add or 68728fb27baSJustin T. Gibbs * addonly mode, we only select this device 68828fb27baSJustin T. Gibbs * if it hasn't already been selected. 68928fb27baSJustin T. Gibbs * Otherwise, we would be unnecessarily 69028fb27baSJustin T. Gibbs * changing the selection order and 69128fb27baSJustin T. Gibbs * incrementing the selection count. If 69228fb27baSJustin T. Gibbs * we're in only mode, we unconditionally 69328fb27baSJustin T. Gibbs * select this device, since in only mode 69428fb27baSJustin T. Gibbs * any previous selections are erased and 69528fb27baSJustin T. Gibbs * manually specified devices are the first 69628fb27baSJustin T. Gibbs * ones to be selected. If we're in remove 69728fb27baSJustin T. Gibbs * mode, we de-select the specified device and 69828fb27baSJustin T. Gibbs * decrement the selection count. 69928fb27baSJustin T. Gibbs */ 70028fb27baSJustin T. Gibbs switch(select_mode) { 70128fb27baSJustin T. Gibbs case DS_SELECT_ADD: 70228fb27baSJustin T. Gibbs case DS_SELECT_ADDONLY: 70328fb27baSJustin T. Gibbs if ((*dev_select)[i].selected) 70428fb27baSJustin T. Gibbs break; 70528fb27baSJustin T. Gibbs /* FALLTHROUGH */ 70628fb27baSJustin T. Gibbs case DS_SELECT_ONLY: 70728fb27baSJustin T. Gibbs (*dev_select)[i].selected = 70828fb27baSJustin T. Gibbs ++selection_number; 70928fb27baSJustin T. Gibbs (*num_selected)++; 71028fb27baSJustin T. Gibbs break; 71128fb27baSJustin T. Gibbs case DS_SELECT_REMOVE: 71228fb27baSJustin T. Gibbs (*dev_select)[i].selected = 0; 71328fb27baSJustin T. Gibbs (*num_selected)--; 71428fb27baSJustin T. Gibbs /* 71528fb27baSJustin T. Gibbs * This isn't passed back out, we 71628fb27baSJustin T. Gibbs * just use it to keep track of 71728fb27baSJustin T. Gibbs * how many devices we've removed. 71828fb27baSJustin T. Gibbs */ 71928fb27baSJustin T. Gibbs num_dev_selections--; 72028fb27baSJustin T. Gibbs break; 72128fb27baSJustin T. Gibbs } 72228fb27baSJustin T. Gibbs break; 72328fb27baSJustin T. Gibbs } 72428fb27baSJustin T. Gibbs } 72528fb27baSJustin T. Gibbs } 72628fb27baSJustin T. Gibbs 72728fb27baSJustin T. Gibbs /* 72828fb27baSJustin T. Gibbs * Go through the user's device type expressions and select devices 72928fb27baSJustin T. Gibbs * accordingly. We only do this if the number of devices already 73028fb27baSJustin T. Gibbs * selected is less than the maximum number we can show. 73128fb27baSJustin T. Gibbs */ 73228fb27baSJustin T. Gibbs for (i = 0; (i < num_matches) && (*num_selected < maxshowdevs); i++) { 73328fb27baSJustin T. Gibbs /* We should probably indicate some error here */ 73428fb27baSJustin T. Gibbs if ((matches[i].match_fields == DEVSTAT_MATCH_NONE) 73528fb27baSJustin T. Gibbs || (matches[i].num_match_categories <= 0)) 73628fb27baSJustin T. Gibbs continue; 73728fb27baSJustin T. Gibbs 73828fb27baSJustin T. Gibbs for (j = 0; j < numdevs; j++) { 73928fb27baSJustin T. Gibbs int num_match_categories; 74028fb27baSJustin T. Gibbs 74128fb27baSJustin T. Gibbs num_match_categories = matches[i].num_match_categories; 74228fb27baSJustin T. Gibbs 74328fb27baSJustin T. Gibbs /* 74428fb27baSJustin T. Gibbs * Determine whether or not the current device 74528fb27baSJustin T. Gibbs * matches the given matching expression. This if 74628fb27baSJustin T. Gibbs * statement consists of three components: 74728fb27baSJustin T. Gibbs * - the device type check 74828fb27baSJustin T. Gibbs * - the device interface check 74928fb27baSJustin T. Gibbs * - the passthrough check 75028fb27baSJustin T. Gibbs * If a the matching test is successful, it 75128fb27baSJustin T. Gibbs * decrements the number of matching categories, 75228fb27baSJustin T. Gibbs * and if we've reached the last element that 75328fb27baSJustin T. Gibbs * needed to be matched, the if statement succeeds. 75428fb27baSJustin T. Gibbs * 75528fb27baSJustin T. Gibbs */ 75628fb27baSJustin T. Gibbs if ((((matches[i].match_fields & DEVSTAT_MATCH_TYPE)!=0) 75728fb27baSJustin T. Gibbs && ((devices[j].device_type & DEVSTAT_TYPE_MASK) == 75828fb27baSJustin T. Gibbs (matches[i].device_type & DEVSTAT_TYPE_MASK)) 75928fb27baSJustin T. Gibbs &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0) 76028fb27baSJustin T. Gibbs || (((matches[i].match_fields & 76128fb27baSJustin T. Gibbs DEVSTAT_MATCH_PASS) == 0) 76228fb27baSJustin T. Gibbs && ((devices[j].device_type & 76328fb27baSJustin T. Gibbs DEVSTAT_TYPE_PASS) == 0))) 76428fb27baSJustin T. Gibbs && (--num_match_categories == 0)) 76528fb27baSJustin T. Gibbs || (((matches[i].match_fields & DEVSTAT_MATCH_IF) != 0) 76628fb27baSJustin T. Gibbs && ((devices[j].device_type & DEVSTAT_TYPE_IF_MASK) == 76728fb27baSJustin T. Gibbs (matches[i].device_type & DEVSTAT_TYPE_IF_MASK)) 76828fb27baSJustin T. Gibbs &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0) 76928fb27baSJustin T. Gibbs || (((matches[i].match_fields & 77028fb27baSJustin T. Gibbs DEVSTAT_MATCH_PASS) == 0) 77128fb27baSJustin T. Gibbs && ((devices[j].device_type & 77228fb27baSJustin T. Gibbs DEVSTAT_TYPE_PASS) == 0))) 77328fb27baSJustin T. Gibbs && (--num_match_categories == 0)) 77428fb27baSJustin T. Gibbs || (((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0) 77528fb27baSJustin T. Gibbs && ((devices[j].device_type & DEVSTAT_TYPE_PASS) != 0) 77628fb27baSJustin T. Gibbs && (--num_match_categories == 0))) { 77728fb27baSJustin T. Gibbs 77828fb27baSJustin T. Gibbs /* 77928fb27baSJustin T. Gibbs * This is probably a non-optimal solution 78028fb27baSJustin T. Gibbs * to the problem that the devices in the 78128fb27baSJustin T. Gibbs * device list will not be in the same 78228fb27baSJustin T. Gibbs * order as the devices in the selection 78328fb27baSJustin T. Gibbs * array. 78428fb27baSJustin T. Gibbs */ 78528fb27baSJustin T. Gibbs for (k = 0; k < numdevs; k++) { 78628fb27baSJustin T. Gibbs if ((*dev_select)[k].position == j) { 78728fb27baSJustin T. Gibbs found = 1; 78828fb27baSJustin T. Gibbs break; 78928fb27baSJustin T. Gibbs } 79028fb27baSJustin T. Gibbs } 79128fb27baSJustin T. Gibbs 79228fb27baSJustin T. Gibbs /* 79328fb27baSJustin T. Gibbs * There shouldn't be a case where a device 79428fb27baSJustin T. Gibbs * in the device list is not in the 79528fb27baSJustin T. Gibbs * selection list...but it could happen. 79628fb27baSJustin T. Gibbs */ 79728fb27baSJustin T. Gibbs if (found != 1) { 79828fb27baSJustin T. Gibbs fprintf(stderr, "selectdevs: couldn't" 79928fb27baSJustin T. Gibbs " find %s%d in selection " 80028fb27baSJustin T. Gibbs "list\n", 80128fb27baSJustin T. Gibbs devices[j].device_name, 80228fb27baSJustin T. Gibbs devices[j].unit_number); 80328fb27baSJustin T. Gibbs break; 80428fb27baSJustin T. Gibbs } 80528fb27baSJustin T. Gibbs 80628fb27baSJustin T. Gibbs /* 80728fb27baSJustin T. Gibbs * We do different things based upon the 80828fb27baSJustin T. Gibbs * mode we're in. If we're in add or only 80928fb27baSJustin T. Gibbs * mode, we go ahead and select this device 81028fb27baSJustin T. Gibbs * if it hasn't already been selected. If 81128fb27baSJustin T. Gibbs * it has already been selected, we leave 81228fb27baSJustin T. Gibbs * it alone so we don't mess up the 81328fb27baSJustin T. Gibbs * selection ordering. Manually specified 81428fb27baSJustin T. Gibbs * devices have already been selected, and 81528fb27baSJustin T. Gibbs * they have higher priority than pattern 81628fb27baSJustin T. Gibbs * matched devices. If we're in remove 81728fb27baSJustin T. Gibbs * mode, we de-select the given device and 81828fb27baSJustin T. Gibbs * decrement the selected count. 81928fb27baSJustin T. Gibbs */ 82028fb27baSJustin T. Gibbs switch(select_mode) { 82128fb27baSJustin T. Gibbs case DS_SELECT_ADD: 82228fb27baSJustin T. Gibbs case DS_SELECT_ADDONLY: 82328fb27baSJustin T. Gibbs case DS_SELECT_ONLY: 82428fb27baSJustin T. Gibbs if ((*dev_select)[k].selected != 0) 82528fb27baSJustin T. Gibbs break; 82628fb27baSJustin T. Gibbs (*dev_select)[k].selected = 82728fb27baSJustin T. Gibbs ++selection_number; 82828fb27baSJustin T. Gibbs (*num_selected)++; 82928fb27baSJustin T. Gibbs break; 83028fb27baSJustin T. Gibbs case DS_SELECT_REMOVE: 83128fb27baSJustin T. Gibbs (*dev_select)[k].selected = 0; 83228fb27baSJustin T. Gibbs (*num_selected)--; 83328fb27baSJustin T. Gibbs break; 83428fb27baSJustin T. Gibbs } 83528fb27baSJustin T. Gibbs } 83628fb27baSJustin T. Gibbs } 83728fb27baSJustin T. Gibbs } 83828fb27baSJustin T. Gibbs 83928fb27baSJustin T. Gibbs /* 84028fb27baSJustin T. Gibbs * Here we implement "top" mode. Devices are sorted in the 84128fb27baSJustin T. Gibbs * selection array based on two criteria: whether or not they are 84228fb27baSJustin T. Gibbs * selected (not selection number, just the fact that they are 84328fb27baSJustin T. Gibbs * selected!) and the number of bytes in the "bytes" field of the 84428fb27baSJustin T. Gibbs * selection structure. The bytes field generally must be kept up 84528fb27baSJustin T. Gibbs * by the user. In the future, it may be maintained by library 84628fb27baSJustin T. Gibbs * functions, but for now the user has to do the work. 84728fb27baSJustin T. Gibbs * 84828fb27baSJustin T. Gibbs * At first glance, it may seem wrong that we don't go through and 84928fb27baSJustin T. Gibbs * select every device in the case where the user hasn't specified 85028fb27baSJustin T. Gibbs * any devices or patterns. In fact, though, it won't make any 85128fb27baSJustin T. Gibbs * difference in the device sorting. In that particular case (i.e. 85228fb27baSJustin T. Gibbs * when we're in "add" or "only" mode, and the user hasn't 85328fb27baSJustin T. Gibbs * specified anything) the first time through no devices will be 85428fb27baSJustin T. Gibbs * selected, so the only criterion used to sort them will be their 85528fb27baSJustin T. Gibbs * performance. The second time through, and every time thereafter, 85628fb27baSJustin T. Gibbs * all devices will be selected, so again selection won't matter. 85728fb27baSJustin T. Gibbs */ 85828fb27baSJustin T. Gibbs if (perf_select != 0) { 85928fb27baSJustin T. Gibbs 86028fb27baSJustin T. Gibbs /* Sort the device array by throughput */ 86128fb27baSJustin T. Gibbs qsort(*dev_select, *num_selections, 86228fb27baSJustin T. Gibbs sizeof(struct device_selection), 86328fb27baSJustin T. Gibbs compare_select); 86428fb27baSJustin T. Gibbs 86528fb27baSJustin T. Gibbs if (*num_selected == 0) { 86628fb27baSJustin T. Gibbs /* 86728fb27baSJustin T. Gibbs * Here we select every device in the array, if it 86828fb27baSJustin T. Gibbs * isn't already selected. Because the 'selected' 86928fb27baSJustin T. Gibbs * variable in the selection array entries contains 87028fb27baSJustin T. Gibbs * the selection order, the devstats routine can show 87128fb27baSJustin T. Gibbs * the devices that were selected first. 87228fb27baSJustin T. Gibbs */ 87328fb27baSJustin T. Gibbs for (i = 0; i < *num_selections; i++) { 87428fb27baSJustin T. Gibbs if ((*dev_select)[i].selected == 0) { 87528fb27baSJustin T. Gibbs (*dev_select)[i].selected = 87628fb27baSJustin T. Gibbs ++selection_number; 87728fb27baSJustin T. Gibbs (*num_selected)++; 87828fb27baSJustin T. Gibbs } 87928fb27baSJustin T. Gibbs } 88028fb27baSJustin T. Gibbs } else { 88128fb27baSJustin T. Gibbs selection_number = 0; 88228fb27baSJustin T. Gibbs for (i = 0; i < *num_selections; i++) { 88328fb27baSJustin T. Gibbs if ((*dev_select)[i].selected != 0) { 88428fb27baSJustin T. Gibbs (*dev_select)[i].selected = 88528fb27baSJustin T. Gibbs ++selection_number; 88628fb27baSJustin T. Gibbs } 88728fb27baSJustin T. Gibbs } 88828fb27baSJustin T. Gibbs } 88928fb27baSJustin T. Gibbs } 89028fb27baSJustin T. Gibbs 89128fb27baSJustin T. Gibbs /* 89228fb27baSJustin T. Gibbs * If we're in the "add" selection mode and if we haven't already 89328fb27baSJustin T. Gibbs * selected maxshowdevs number of devices, go through the array and 89428fb27baSJustin T. Gibbs * select any unselected devices. If we're in "only" mode, we 89528fb27baSJustin T. Gibbs * obviously don't want to select anything other than what the user 89628fb27baSJustin T. Gibbs * specifies. If we're in "remove" mode, it probably isn't a good 89728fb27baSJustin T. Gibbs * idea to go through and select any more devices, since we might 89828fb27baSJustin T. Gibbs * end up selecting something that the user wants removed. Through 89928fb27baSJustin T. Gibbs * more complicated logic, we could actually figure this out, but 90028fb27baSJustin T. Gibbs * that would probably require combining this loop with the various 90128fb27baSJustin T. Gibbs * selections loops above. 90228fb27baSJustin T. Gibbs */ 90328fb27baSJustin T. Gibbs if ((select_mode == DS_SELECT_ADD) && (*num_selected < maxshowdevs)) { 90428fb27baSJustin T. Gibbs for (i = 0; i < *num_selections; i++) 90528fb27baSJustin T. Gibbs if ((*dev_select)[i].selected == 0) { 90628fb27baSJustin T. Gibbs (*dev_select)[i].selected = ++selection_number; 90728fb27baSJustin T. Gibbs (*num_selected)++; 90828fb27baSJustin T. Gibbs } 90928fb27baSJustin T. Gibbs } 91028fb27baSJustin T. Gibbs 91128fb27baSJustin T. Gibbs /* 91228fb27baSJustin T. Gibbs * Look at the number of devices that have been selected. If it 91328fb27baSJustin T. Gibbs * has changed, set the changed variable. Otherwise, if we've 91428fb27baSJustin T. Gibbs * made a backup of the selection list, compare it to the current 91528fb27baSJustin T. Gibbs * selection list to see if the selected devices have changed. 91628fb27baSJustin T. Gibbs */ 91728fb27baSJustin T. Gibbs if ((changed == 0) && (old_num_selected != *num_selected)) 91828fb27baSJustin T. Gibbs changed = 1; 91928fb27baSJustin T. Gibbs else if ((changed == 0) && (old_dev_select != NULL)) { 92028fb27baSJustin T. Gibbs /* 92128fb27baSJustin T. Gibbs * Now we go through the selection list and we look at 92228fb27baSJustin T. Gibbs * it three different ways. 92328fb27baSJustin T. Gibbs */ 92428fb27baSJustin T. Gibbs for (i = 0; (i < *num_selections) && (changed == 0) && 92528fb27baSJustin T. Gibbs (i < old_num_selections); i++) { 92628fb27baSJustin T. Gibbs /* 92728fb27baSJustin T. Gibbs * If the device at index i in both the new and old 92828fb27baSJustin T. Gibbs * selection arrays has the same device number and 92928fb27baSJustin T. Gibbs * selection status, it hasn't changed. We 93028fb27baSJustin T. Gibbs * continue on to the next index. 93128fb27baSJustin T. Gibbs */ 93228fb27baSJustin T. Gibbs if (((*dev_select)[i].device_number == 93328fb27baSJustin T. Gibbs old_dev_select[i].device_number) 93428fb27baSJustin T. Gibbs && ((*dev_select)[i].selected == 93528fb27baSJustin T. Gibbs old_dev_select[i].selected)) 93628fb27baSJustin T. Gibbs continue; 93728fb27baSJustin T. Gibbs 93828fb27baSJustin T. Gibbs /* 93928fb27baSJustin T. Gibbs * Now, if we're still going through the if 94028fb27baSJustin T. Gibbs * statement, the above test wasn't true. So we 94128fb27baSJustin T. Gibbs * check here to see if the device at index i in 94228fb27baSJustin T. Gibbs * the current array is the same as the device at 94328fb27baSJustin T. Gibbs * index i in the old array. If it is, that means 94428fb27baSJustin T. Gibbs * that its selection number has changed. Set 94528fb27baSJustin T. Gibbs * changed to 1 and exit the loop. 94628fb27baSJustin T. Gibbs */ 94728fb27baSJustin T. Gibbs else if ((*dev_select)[i].device_number == 94828fb27baSJustin T. Gibbs old_dev_select[i].device_number) { 94928fb27baSJustin T. Gibbs changed = 1; 95028fb27baSJustin T. Gibbs break; 95128fb27baSJustin T. Gibbs } 95228fb27baSJustin T. Gibbs /* 95328fb27baSJustin T. Gibbs * If we get here, then the device at index i in 95428fb27baSJustin T. Gibbs * the current array isn't the same device as the 95528fb27baSJustin T. Gibbs * device at index i in the old array. 95628fb27baSJustin T. Gibbs */ 95728fb27baSJustin T. Gibbs else { 958c3508206SKenneth D. Merry found = 0; 95928fb27baSJustin T. Gibbs 96028fb27baSJustin T. Gibbs /* 96128fb27baSJustin T. Gibbs * Search through the old selection array 96228fb27baSJustin T. Gibbs * looking for a device with the same 96328fb27baSJustin T. Gibbs * device number as the device at index i 96428fb27baSJustin T. Gibbs * in the current array. If the selection 96528fb27baSJustin T. Gibbs * status is the same, then we mark it as 96628fb27baSJustin T. Gibbs * found. If the selection status isn't 96728fb27baSJustin T. Gibbs * the same, we break out of the loop. 96828fb27baSJustin T. Gibbs * Since found isn't set, changed will be 96928fb27baSJustin T. Gibbs * set to 1 below. 97028fb27baSJustin T. Gibbs */ 97128fb27baSJustin T. Gibbs for (j = 0; j < old_num_selections; j++) { 97228fb27baSJustin T. Gibbs if (((*dev_select)[i].device_number == 97328fb27baSJustin T. Gibbs old_dev_select[j].device_number) 97428fb27baSJustin T. Gibbs && ((*dev_select)[i].selected == 97528fb27baSJustin T. Gibbs old_dev_select[j].selected)){ 97628fb27baSJustin T. Gibbs found = 1; 97728fb27baSJustin T. Gibbs break; 97828fb27baSJustin T. Gibbs } 97928fb27baSJustin T. Gibbs else if ((*dev_select)[i].device_number 98028fb27baSJustin T. Gibbs == old_dev_select[j].device_number) 98128fb27baSJustin T. Gibbs break; 98228fb27baSJustin T. Gibbs } 98328fb27baSJustin T. Gibbs if (found == 0) 98428fb27baSJustin T. Gibbs changed = 1; 98528fb27baSJustin T. Gibbs } 98628fb27baSJustin T. Gibbs } 98728fb27baSJustin T. Gibbs } 98828fb27baSJustin T. Gibbs if (old_dev_select != NULL) 98928fb27baSJustin T. Gibbs free(old_dev_select); 99028fb27baSJustin T. Gibbs 99128fb27baSJustin T. Gibbs return(changed); 99228fb27baSJustin T. Gibbs } 99328fb27baSJustin T. Gibbs 99428fb27baSJustin T. Gibbs /* 99528fb27baSJustin T. Gibbs * Comparison routine for qsort() above. Note that the comparison here is 99628fb27baSJustin T. Gibbs * backwards -- generally, it should return a value to indicate whether 99728fb27baSJustin T. Gibbs * arg1 is <, =, or > arg2. Instead, it returns the opposite. The reason 99828fb27baSJustin T. Gibbs * it returns the opposite is so that the selection array will be sorted in 99928fb27baSJustin T. Gibbs * order of decreasing performance. We sort on two parameters. The first 100028fb27baSJustin T. Gibbs * sort key is whether or not one or the other of the devices in question 100128fb27baSJustin T. Gibbs * has been selected. If one of them has, and the other one has not, the 100228fb27baSJustin T. Gibbs * selected device is automatically more important than the unselected 100328fb27baSJustin T. Gibbs * device. If neither device is selected, we judge the devices based upon 100428fb27baSJustin T. Gibbs * performance. 100528fb27baSJustin T. Gibbs */ 100628fb27baSJustin T. Gibbs static int 100728fb27baSJustin T. Gibbs compare_select(const void *arg1, const void *arg2) 100828fb27baSJustin T. Gibbs { 1009c3508206SKenneth D. Merry if ((((const struct device_selection *)arg1)->selected) 1010c3508206SKenneth D. Merry && (((const struct device_selection *)arg2)->selected == 0)) 101128fb27baSJustin T. Gibbs return(-1); 1012c3508206SKenneth D. Merry else if ((((const struct device_selection *)arg1)->selected == 0) 1013c3508206SKenneth D. Merry && (((const struct device_selection *)arg2)->selected)) 101428fb27baSJustin T. Gibbs return(1); 1015c3508206SKenneth D. Merry else if (((const struct device_selection *)arg2)->bytes < 1016c3508206SKenneth D. Merry ((const struct device_selection *)arg1)->bytes) 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); 102128fb27baSJustin T. Gibbs else 102228fb27baSJustin T. Gibbs return(0); 102328fb27baSJustin T. Gibbs } 102428fb27baSJustin T. Gibbs 102528fb27baSJustin T. Gibbs /* 102628fb27baSJustin T. Gibbs * Take a string with the general format "arg1,arg2,arg3", and build a 102728fb27baSJustin T. Gibbs * device matching expression from it. 102828fb27baSJustin T. Gibbs */ 102928fb27baSJustin T. Gibbs int 1030c4a5ef6eSThomas Moestl devstat_buildmatch(char *match_str, struct devstat_match **matches, 1031c4a5ef6eSThomas Moestl int *num_matches) 103228fb27baSJustin T. Gibbs { 103328fb27baSJustin T. Gibbs char *tstr[5]; 103428fb27baSJustin T. Gibbs char **tempstr; 103528fb27baSJustin T. Gibbs int num_args; 1036be04b6d1SDavid E. O'Brien int i, j; 103728fb27baSJustin T. Gibbs 103828fb27baSJustin T. Gibbs /* We can't do much without a string to parse */ 103928fb27baSJustin T. Gibbs if (match_str == NULL) { 1040c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 10417d72cda3SMaxime Henrion "%s: no match expression", __func__); 104228fb27baSJustin T. Gibbs return(-1); 104328fb27baSJustin T. Gibbs } 104428fb27baSJustin T. Gibbs 104528fb27baSJustin T. Gibbs /* 104628fb27baSJustin T. Gibbs * Break the (comma delimited) input string out into separate strings. 104728fb27baSJustin T. Gibbs */ 104828fb27baSJustin T. Gibbs for (tempstr = tstr, num_args = 0; 10491046090fSSergey Kandaurov (*tempstr = strsep(&match_str, ",")) != NULL && (num_args < 5);) 10501046090fSSergey Kandaurov if (**tempstr != '\0') { 10511046090fSSergey Kandaurov num_args++; 105228fb27baSJustin T. Gibbs if (++tempstr >= &tstr[5]) 105328fb27baSJustin T. Gibbs break; 10541046090fSSergey Kandaurov } 105528fb27baSJustin T. Gibbs 105628fb27baSJustin T. Gibbs /* The user gave us too many type arguments */ 105728fb27baSJustin T. Gibbs if (num_args > 3) { 1058c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 10597d72cda3SMaxime Henrion "%s: too many type arguments", __func__); 106028fb27baSJustin T. Gibbs return(-1); 106128fb27baSJustin T. Gibbs } 106228fb27baSJustin T. Gibbs 106328fb27baSJustin T. Gibbs if (*num_matches == 0) 1064a18e4616SGuy Helmer *matches = NULL; 1065a18e4616SGuy Helmer 1066a18e4616SGuy Helmer *matches = (struct devstat_match *)reallocf(*matches, 106728fb27baSJustin T. Gibbs sizeof(struct devstat_match) * (*num_matches + 1)); 106828fb27baSJustin T. Gibbs 1069a18e4616SGuy Helmer if (*matches == NULL) { 1070a18e4616SGuy Helmer snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1071a18e4616SGuy Helmer "%s: Cannot allocate memory for matches list", __func__); 1072a18e4616SGuy Helmer return(-1); 1073a18e4616SGuy Helmer } 1074a18e4616SGuy Helmer 107528fb27baSJustin T. Gibbs /* Make sure the current entry is clear */ 107628fb27baSJustin T. Gibbs bzero(&matches[0][*num_matches], sizeof(struct devstat_match)); 107728fb27baSJustin T. Gibbs 107828fb27baSJustin T. Gibbs /* 107928fb27baSJustin T. Gibbs * Step through the arguments the user gave us and build a device 108028fb27baSJustin T. Gibbs * matching expression from them. 108128fb27baSJustin T. Gibbs */ 108228fb27baSJustin T. Gibbs for (i = 0; i < num_args; i++) { 108328fb27baSJustin T. Gibbs char *tempstr2, *tempstr3; 108428fb27baSJustin T. Gibbs 108528fb27baSJustin T. Gibbs /* 108628fb27baSJustin T. Gibbs * Get rid of leading white space. 108728fb27baSJustin T. Gibbs */ 108828fb27baSJustin T. Gibbs tempstr2 = tstr[i]; 108928fb27baSJustin T. Gibbs while (isspace(*tempstr2) && (*tempstr2 != '\0')) 109028fb27baSJustin T. Gibbs tempstr2++; 109128fb27baSJustin T. Gibbs 109228fb27baSJustin T. Gibbs /* 109328fb27baSJustin T. Gibbs * Get rid of trailing white space. 109428fb27baSJustin T. Gibbs */ 109528fb27baSJustin T. Gibbs tempstr3 = &tempstr2[strlen(tempstr2) - 1]; 109628fb27baSJustin T. Gibbs 109728fb27baSJustin T. Gibbs while ((*tempstr3 != '\0') && (tempstr3 > tempstr2) 109828fb27baSJustin T. Gibbs && (isspace(*tempstr3))) { 109928fb27baSJustin T. Gibbs *tempstr3 = '\0'; 110028fb27baSJustin T. Gibbs tempstr3--; 110128fb27baSJustin T. Gibbs } 110228fb27baSJustin T. Gibbs 110328fb27baSJustin T. Gibbs /* 110428fb27baSJustin T. Gibbs * Go through the match table comparing the user's 110528fb27baSJustin T. Gibbs * arguments to known device types, interfaces, etc. 110628fb27baSJustin T. Gibbs */ 110728fb27baSJustin T. Gibbs for (j = 0; match_table[j].match_str != NULL; j++) { 110828fb27baSJustin T. Gibbs /* 110928fb27baSJustin T. Gibbs * We do case-insensitive matching, in case someone 111028fb27baSJustin T. Gibbs * wants to enter "SCSI" instead of "scsi" or 111128fb27baSJustin T. Gibbs * something like that. Only compare as many 111228fb27baSJustin T. Gibbs * characters as are in the string in the match 111328fb27baSJustin T. Gibbs * table. This should help if someone tries to use 111428fb27baSJustin T. Gibbs * a super-long match expression. 111528fb27baSJustin T. Gibbs */ 111628fb27baSJustin T. Gibbs if (strncasecmp(tempstr2, match_table[j].match_str, 111728fb27baSJustin T. Gibbs strlen(match_table[j].match_str)) == 0) { 111828fb27baSJustin T. Gibbs /* 111928fb27baSJustin T. Gibbs * Make sure the user hasn't specified two 112028fb27baSJustin T. Gibbs * items of the same type, like "da" and 112128fb27baSJustin T. Gibbs * "cd". One device cannot be both. 112228fb27baSJustin T. Gibbs */ 112328fb27baSJustin T. Gibbs if (((*matches)[*num_matches].match_fields & 112428fb27baSJustin T. Gibbs match_table[j].match_field) != 0) { 1125c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, 1126c4a5ef6eSThomas Moestl sizeof(devstat_errbuf), 112728fb27baSJustin T. Gibbs "%s: cannot have more than " 112828fb27baSJustin T. Gibbs "one match item in a single " 11297d72cda3SMaxime Henrion "category", __func__); 113028fb27baSJustin T. Gibbs return(-1); 113128fb27baSJustin T. Gibbs } 113228fb27baSJustin T. Gibbs /* 113328fb27baSJustin T. Gibbs * If we've gotten this far, we have a 113428fb27baSJustin T. Gibbs * winner. Set the appropriate fields in 113528fb27baSJustin T. Gibbs * the match entry. 113628fb27baSJustin T. Gibbs */ 113728fb27baSJustin T. Gibbs (*matches)[*num_matches].match_fields |= 113828fb27baSJustin T. Gibbs match_table[j].match_field; 113928fb27baSJustin T. Gibbs (*matches)[*num_matches].device_type |= 114028fb27baSJustin T. Gibbs match_table[j].type; 114128fb27baSJustin T. Gibbs (*matches)[*num_matches].num_match_categories++; 114228fb27baSJustin T. Gibbs break; 114328fb27baSJustin T. Gibbs } 114428fb27baSJustin T. Gibbs } 114528fb27baSJustin T. Gibbs /* 114628fb27baSJustin T. Gibbs * We should have found a match in the above for loop. If 114728fb27baSJustin T. Gibbs * not, that means the user entered an invalid device type 114828fb27baSJustin T. Gibbs * or interface. 114928fb27baSJustin T. Gibbs */ 115028fb27baSJustin T. Gibbs if ((*matches)[*num_matches].num_match_categories != (i + 1)) { 115117ee2b20SKenneth D. Merry snprintf(devstat_errbuf, sizeof(devstat_errbuf), 11527d72cda3SMaxime Henrion "%s: unknown match item \"%s\"", __func__, 115328fb27baSJustin T. Gibbs tstr[i]); 115428fb27baSJustin T. Gibbs return(-1); 115528fb27baSJustin T. Gibbs } 115628fb27baSJustin T. Gibbs } 115728fb27baSJustin T. Gibbs 115828fb27baSJustin T. Gibbs (*num_matches)++; 115928fb27baSJustin T. Gibbs 116028fb27baSJustin T. Gibbs return(0); 116128fb27baSJustin T. Gibbs } 116228fb27baSJustin T. Gibbs 116328fb27baSJustin T. Gibbs /* 116428fb27baSJustin T. Gibbs * Compute a number of device statistics. Only one field is mandatory, and 116528fb27baSJustin T. Gibbs * that is "current". Everything else is optional. The caller passes in 116628fb27baSJustin T. Gibbs * pointers to variables to hold the various statistics he desires. If he 116728fb27baSJustin T. Gibbs * doesn't want a particular staistic, he should pass in a NULL pointer. 116828fb27baSJustin T. Gibbs * Return values: 116928fb27baSJustin T. Gibbs * 0 -- success 117028fb27baSJustin T. Gibbs * -1 -- failure 117128fb27baSJustin T. Gibbs */ 117228fb27baSJustin T. Gibbs int 117328fb27baSJustin T. Gibbs compute_stats(struct devstat *current, struct devstat *previous, 117428fb27baSJustin T. Gibbs long double etime, u_int64_t *total_bytes, 117528fb27baSJustin T. Gibbs u_int64_t *total_transfers, u_int64_t *total_blocks, 117628fb27baSJustin T. Gibbs long double *kb_per_transfer, long double *transfers_per_second, 117728fb27baSJustin T. Gibbs long double *mb_per_second, long double *blocks_per_second, 117828fb27baSJustin T. Gibbs long double *ms_per_transaction) 117928fb27baSJustin T. Gibbs { 1180884539f7SKenneth D. Merry return(devstat_compute_statistics(current, previous, etime, 1181884539f7SKenneth D. Merry total_bytes ? DSM_TOTAL_BYTES : DSM_SKIP, 1182884539f7SKenneth D. Merry total_bytes, 1183884539f7SKenneth D. Merry total_transfers ? DSM_TOTAL_TRANSFERS : DSM_SKIP, 1184884539f7SKenneth D. Merry total_transfers, 1185884539f7SKenneth D. Merry total_blocks ? DSM_TOTAL_BLOCKS : DSM_SKIP, 1186884539f7SKenneth D. Merry total_blocks, 1187884539f7SKenneth D. Merry kb_per_transfer ? DSM_KB_PER_TRANSFER : DSM_SKIP, 1188884539f7SKenneth D. Merry kb_per_transfer, 1189884539f7SKenneth D. Merry transfers_per_second ? DSM_TRANSFERS_PER_SECOND : DSM_SKIP, 1190884539f7SKenneth D. Merry transfers_per_second, 1191884539f7SKenneth D. Merry mb_per_second ? DSM_MB_PER_SECOND : DSM_SKIP, 1192884539f7SKenneth D. Merry mb_per_second, 1193884539f7SKenneth D. Merry blocks_per_second ? DSM_BLOCKS_PER_SECOND : DSM_SKIP, 1194884539f7SKenneth D. Merry blocks_per_second, 1195884539f7SKenneth D. Merry ms_per_transaction ? DSM_MS_PER_TRANSACTION : DSM_SKIP, 1196884539f7SKenneth D. Merry ms_per_transaction, 1197884539f7SKenneth D. Merry DSM_NONE)); 119828fb27baSJustin T. Gibbs } 119928fb27baSJustin T. Gibbs 12007194d335SPoul-Henning Kamp 12017194d335SPoul-Henning Kamp /* This is 1/2^64 */ 12027194d335SPoul-Henning Kamp #define BINTIME_SCALE 5.42101086242752217003726400434970855712890625e-20 12037194d335SPoul-Henning Kamp 120428fb27baSJustin T. Gibbs long double 12057194d335SPoul-Henning Kamp devstat_compute_etime(struct bintime *cur_time, struct bintime *prev_time) 120628fb27baSJustin T. Gibbs { 120728fb27baSJustin T. Gibbs long double etime; 120828fb27baSJustin T. Gibbs 12097194d335SPoul-Henning Kamp etime = cur_time->sec; 12107194d335SPoul-Henning Kamp etime += cur_time->frac * BINTIME_SCALE; 12117194d335SPoul-Henning Kamp if (prev_time != NULL) { 12127194d335SPoul-Henning Kamp etime -= prev_time->sec; 12137194d335SPoul-Henning Kamp etime -= prev_time->frac * BINTIME_SCALE; 12147194d335SPoul-Henning Kamp } 121528fb27baSJustin T. Gibbs return(etime); 121628fb27baSJustin T. Gibbs } 1217c4a5ef6eSThomas Moestl 121836eab1f5SPoul-Henning Kamp #define DELTA(field, index) \ 121936eab1f5SPoul-Henning Kamp (current->field[(index)] - (previous ? previous->field[(index)] : 0)) 122036eab1f5SPoul-Henning Kamp 122136eab1f5SPoul-Henning Kamp #define DELTA_T(field) \ 122236eab1f5SPoul-Henning Kamp devstat_compute_etime(¤t->field, \ 122336eab1f5SPoul-Henning Kamp (previous ? &previous->field : NULL)) 122436eab1f5SPoul-Henning Kamp 1225c4a5ef6eSThomas Moestl int 1226c4a5ef6eSThomas Moestl devstat_compute_statistics(struct devstat *current, struct devstat *previous, 1227c4a5ef6eSThomas Moestl long double etime, ...) 1228c4a5ef6eSThomas Moestl { 122936eab1f5SPoul-Henning Kamp u_int64_t totalbytes, totalbytesread, totalbyteswrite, totalbytesfree; 1230c4a5ef6eSThomas Moestl u_int64_t totaltransfers, totaltransfersread, totaltransferswrite; 1231c4a5ef6eSThomas Moestl u_int64_t totaltransfersother, totalblocks, totalblocksread; 123236eab1f5SPoul-Henning Kamp u_int64_t totalblockswrite, totaltransfersfree, totalblocksfree; 1233fdd6757eSMikolaj Golub long double totalduration, totaldurationread, totaldurationwrite; 1234fdd6757eSMikolaj Golub long double totaldurationfree, totaldurationother; 1235c4a5ef6eSThomas Moestl va_list ap; 1236c4a5ef6eSThomas Moestl devstat_metric metric; 1237c4a5ef6eSThomas Moestl u_int64_t *destu64; 1238c4a5ef6eSThomas Moestl long double *destld; 1239fdd6757eSMikolaj Golub int retval; 1240c4a5ef6eSThomas Moestl 1241c4a5ef6eSThomas Moestl retval = 0; 1242c4a5ef6eSThomas Moestl 1243c4a5ef6eSThomas Moestl /* 1244c4a5ef6eSThomas Moestl * current is the only mandatory field. 1245c4a5ef6eSThomas Moestl */ 1246c4a5ef6eSThomas Moestl if (current == NULL) { 1247c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 12487d72cda3SMaxime Henrion "%s: current stats structure was NULL", __func__); 1249c4a5ef6eSThomas Moestl return(-1); 1250c4a5ef6eSThomas Moestl } 1251c4a5ef6eSThomas Moestl 125236eab1f5SPoul-Henning Kamp totalbytesread = DELTA(bytes, DEVSTAT_READ); 125336eab1f5SPoul-Henning Kamp totalbyteswrite = DELTA(bytes, DEVSTAT_WRITE); 125436eab1f5SPoul-Henning Kamp totalbytesfree = DELTA(bytes, DEVSTAT_FREE); 125536eab1f5SPoul-Henning Kamp totalbytes = totalbytesread + totalbyteswrite + totalbytesfree; 1256c4a5ef6eSThomas Moestl 125736eab1f5SPoul-Henning Kamp totaltransfersread = DELTA(operations, DEVSTAT_READ); 125836eab1f5SPoul-Henning Kamp totaltransferswrite = DELTA(operations, DEVSTAT_WRITE); 125936eab1f5SPoul-Henning Kamp totaltransfersother = DELTA(operations, DEVSTAT_NO_DATA); 126036eab1f5SPoul-Henning Kamp totaltransfersfree = DELTA(operations, DEVSTAT_FREE); 1261c4a5ef6eSThomas Moestl totaltransfers = totaltransfersread + totaltransferswrite + 126236eab1f5SPoul-Henning Kamp totaltransfersother + totaltransfersfree; 1263c4a5ef6eSThomas Moestl 1264c4a5ef6eSThomas Moestl totalblocks = totalbytes; 1265c4a5ef6eSThomas Moestl totalblocksread = totalbytesread; 1266c4a5ef6eSThomas Moestl totalblockswrite = totalbyteswrite; 126736eab1f5SPoul-Henning Kamp totalblocksfree = totalbytesfree; 1268c4a5ef6eSThomas Moestl 1269c4a5ef6eSThomas Moestl if (current->block_size > 0) { 1270c4a5ef6eSThomas Moestl totalblocks /= current->block_size; 1271c4a5ef6eSThomas Moestl totalblocksread /= current->block_size; 1272c4a5ef6eSThomas Moestl totalblockswrite /= current->block_size; 127336eab1f5SPoul-Henning Kamp totalblocksfree /= current->block_size; 1274c4a5ef6eSThomas Moestl } else { 1275c4a5ef6eSThomas Moestl totalblocks /= 512; 1276c4a5ef6eSThomas Moestl totalblocksread /= 512; 1277c4a5ef6eSThomas Moestl totalblockswrite /= 512; 127836eab1f5SPoul-Henning Kamp totalblocksfree /= 512; 1279c4a5ef6eSThomas Moestl } 1280c4a5ef6eSThomas Moestl 1281fdd6757eSMikolaj Golub totaldurationread = DELTA_T(duration[DEVSTAT_READ]); 1282fdd6757eSMikolaj Golub totaldurationwrite = DELTA_T(duration[DEVSTAT_WRITE]); 1283fdd6757eSMikolaj Golub totaldurationfree = DELTA_T(duration[DEVSTAT_FREE]); 1284fdd6757eSMikolaj Golub totaldurationother = DELTA_T(duration[DEVSTAT_NO_DATA]); 1285fdd6757eSMikolaj Golub totalduration = totaldurationread + totaldurationwrite + 1286fdd6757eSMikolaj Golub totaldurationfree + totaldurationother; 1287fdd6757eSMikolaj Golub 1288c4a5ef6eSThomas Moestl va_start(ap, etime); 1289c4a5ef6eSThomas Moestl 1290c4a5ef6eSThomas Moestl while ((metric = (devstat_metric)va_arg(ap, devstat_metric)) != 0) { 1291c4a5ef6eSThomas Moestl 1292c4a5ef6eSThomas Moestl if (metric == DSM_NONE) 1293c4a5ef6eSThomas Moestl break; 1294c4a5ef6eSThomas Moestl 1295c4a5ef6eSThomas Moestl if (metric >= DSM_MAX) { 1296c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 12977d72cda3SMaxime Henrion "%s: metric %d is out of range", __func__, 1298c4a5ef6eSThomas Moestl metric); 1299c4a5ef6eSThomas Moestl retval = -1; 1300c4a5ef6eSThomas Moestl goto bailout; 1301c4a5ef6eSThomas Moestl } 1302c4a5ef6eSThomas Moestl 1303c4a5ef6eSThomas Moestl switch (devstat_arg_list[metric].argtype) { 1304c4a5ef6eSThomas Moestl case DEVSTAT_ARG_UINT64: 1305c4a5ef6eSThomas Moestl destu64 = (u_int64_t *)va_arg(ap, u_int64_t *); 1306c4a5ef6eSThomas Moestl break; 1307c4a5ef6eSThomas Moestl case DEVSTAT_ARG_LD: 1308c4a5ef6eSThomas Moestl destld = (long double *)va_arg(ap, long double *); 1309884539f7SKenneth D. Merry break; 1310884539f7SKenneth D. Merry case DEVSTAT_ARG_SKIP: 1311884539f7SKenneth D. Merry destld = (long double *)va_arg(ap, long double *); 1312c4a5ef6eSThomas Moestl break; 1313c4a5ef6eSThomas Moestl default: 1314c4a5ef6eSThomas Moestl retval = -1; 1315c4a5ef6eSThomas Moestl goto bailout; 1316c4a5ef6eSThomas Moestl break; /* NOTREACHED */ 1317c4a5ef6eSThomas Moestl } 1318c4a5ef6eSThomas Moestl 1319884539f7SKenneth D. Merry if (devstat_arg_list[metric].argtype == DEVSTAT_ARG_SKIP) 1320884539f7SKenneth D. Merry continue; 1321884539f7SKenneth D. Merry 1322c4a5ef6eSThomas Moestl switch (metric) { 1323c4a5ef6eSThomas Moestl case DSM_TOTAL_BYTES: 1324c4a5ef6eSThomas Moestl *destu64 = totalbytes; 1325c4a5ef6eSThomas Moestl break; 1326c4a5ef6eSThomas Moestl case DSM_TOTAL_BYTES_READ: 1327c4a5ef6eSThomas Moestl *destu64 = totalbytesread; 1328c4a5ef6eSThomas Moestl break; 1329c4a5ef6eSThomas Moestl case DSM_TOTAL_BYTES_WRITE: 1330c4a5ef6eSThomas Moestl *destu64 = totalbyteswrite; 1331c4a5ef6eSThomas Moestl break; 133236eab1f5SPoul-Henning Kamp case DSM_TOTAL_BYTES_FREE: 133336eab1f5SPoul-Henning Kamp *destu64 = totalbytesfree; 133436eab1f5SPoul-Henning Kamp break; 1335c4a5ef6eSThomas Moestl case DSM_TOTAL_TRANSFERS: 1336c4a5ef6eSThomas Moestl *destu64 = totaltransfers; 1337c4a5ef6eSThomas Moestl break; 1338c4a5ef6eSThomas Moestl case DSM_TOTAL_TRANSFERS_READ: 1339c4a5ef6eSThomas Moestl *destu64 = totaltransfersread; 1340c4a5ef6eSThomas Moestl break; 1341c4a5ef6eSThomas Moestl case DSM_TOTAL_TRANSFERS_WRITE: 1342c4a5ef6eSThomas Moestl *destu64 = totaltransferswrite; 1343c4a5ef6eSThomas Moestl break; 134436eab1f5SPoul-Henning Kamp case DSM_TOTAL_TRANSFERS_FREE: 134536eab1f5SPoul-Henning Kamp *destu64 = totaltransfersfree; 134636eab1f5SPoul-Henning Kamp break; 1347c4a5ef6eSThomas Moestl case DSM_TOTAL_TRANSFERS_OTHER: 1348c4a5ef6eSThomas Moestl *destu64 = totaltransfersother; 1349c4a5ef6eSThomas Moestl break; 1350c4a5ef6eSThomas Moestl case DSM_TOTAL_BLOCKS: 1351c4a5ef6eSThomas Moestl *destu64 = totalblocks; 1352c4a5ef6eSThomas Moestl break; 1353c4a5ef6eSThomas Moestl case DSM_TOTAL_BLOCKS_READ: 1354c4a5ef6eSThomas Moestl *destu64 = totalblocksread; 1355c4a5ef6eSThomas Moestl break; 1356c4a5ef6eSThomas Moestl case DSM_TOTAL_BLOCKS_WRITE: 1357c4a5ef6eSThomas Moestl *destu64 = totalblockswrite; 1358c4a5ef6eSThomas Moestl break; 135936eab1f5SPoul-Henning Kamp case DSM_TOTAL_BLOCKS_FREE: 136036eab1f5SPoul-Henning Kamp *destu64 = totalblocksfree; 136136eab1f5SPoul-Henning Kamp break; 1362c4a5ef6eSThomas Moestl case DSM_KB_PER_TRANSFER: 1363c4a5ef6eSThomas Moestl *destld = totalbytes; 1364c4a5ef6eSThomas Moestl *destld /= 1024; 1365c4a5ef6eSThomas Moestl if (totaltransfers > 0) 1366c4a5ef6eSThomas Moestl *destld /= totaltransfers; 1367c4a5ef6eSThomas Moestl else 1368c4a5ef6eSThomas Moestl *destld = 0.0; 1369c4a5ef6eSThomas Moestl break; 1370c4a5ef6eSThomas Moestl case DSM_KB_PER_TRANSFER_READ: 1371c4a5ef6eSThomas Moestl *destld = totalbytesread; 1372c4a5ef6eSThomas Moestl *destld /= 1024; 1373c4a5ef6eSThomas Moestl if (totaltransfersread > 0) 1374c4a5ef6eSThomas Moestl *destld /= totaltransfersread; 1375c4a5ef6eSThomas Moestl else 1376c4a5ef6eSThomas Moestl *destld = 0.0; 1377c4a5ef6eSThomas Moestl break; 1378c4a5ef6eSThomas Moestl case DSM_KB_PER_TRANSFER_WRITE: 1379c4a5ef6eSThomas Moestl *destld = totalbyteswrite; 1380c4a5ef6eSThomas Moestl *destld /= 1024; 1381c4a5ef6eSThomas Moestl if (totaltransferswrite > 0) 1382c4a5ef6eSThomas Moestl *destld /= totaltransferswrite; 1383c4a5ef6eSThomas Moestl else 1384c4a5ef6eSThomas Moestl *destld = 0.0; 1385c4a5ef6eSThomas Moestl break; 138636eab1f5SPoul-Henning Kamp case DSM_KB_PER_TRANSFER_FREE: 138736eab1f5SPoul-Henning Kamp *destld = totalbytesfree; 138836eab1f5SPoul-Henning Kamp *destld /= 1024; 138936eab1f5SPoul-Henning Kamp if (totaltransfersfree > 0) 139036eab1f5SPoul-Henning Kamp *destld /= totaltransfersfree; 139136eab1f5SPoul-Henning Kamp else 139236eab1f5SPoul-Henning Kamp *destld = 0.0; 139336eab1f5SPoul-Henning Kamp break; 1394c4a5ef6eSThomas Moestl case DSM_TRANSFERS_PER_SECOND: 1395c4a5ef6eSThomas Moestl if (etime > 0.0) { 1396c4a5ef6eSThomas Moestl *destld = totaltransfers; 1397c4a5ef6eSThomas Moestl *destld /= etime; 1398c4a5ef6eSThomas Moestl } else 1399c4a5ef6eSThomas Moestl *destld = 0.0; 1400c4a5ef6eSThomas Moestl break; 1401c4a5ef6eSThomas Moestl case DSM_TRANSFERS_PER_SECOND_READ: 1402c4a5ef6eSThomas Moestl if (etime > 0.0) { 1403c4a5ef6eSThomas Moestl *destld = totaltransfersread; 1404c4a5ef6eSThomas Moestl *destld /= etime; 1405c4a5ef6eSThomas Moestl } else 1406c4a5ef6eSThomas Moestl *destld = 0.0; 1407c4a5ef6eSThomas Moestl break; 1408c4a5ef6eSThomas Moestl case DSM_TRANSFERS_PER_SECOND_WRITE: 1409c4a5ef6eSThomas Moestl if (etime > 0.0) { 1410c4a5ef6eSThomas Moestl *destld = totaltransferswrite; 1411c4a5ef6eSThomas Moestl *destld /= etime; 1412c4a5ef6eSThomas Moestl } else 1413c4a5ef6eSThomas Moestl *destld = 0.0; 1414c4a5ef6eSThomas Moestl break; 141536eab1f5SPoul-Henning Kamp case DSM_TRANSFERS_PER_SECOND_FREE: 141636eab1f5SPoul-Henning Kamp if (etime > 0.0) { 141736eab1f5SPoul-Henning Kamp *destld = totaltransfersfree; 141836eab1f5SPoul-Henning Kamp *destld /= etime; 141936eab1f5SPoul-Henning Kamp } else 142036eab1f5SPoul-Henning Kamp *destld = 0.0; 142136eab1f5SPoul-Henning Kamp break; 1422c4a5ef6eSThomas Moestl case DSM_TRANSFERS_PER_SECOND_OTHER: 1423c4a5ef6eSThomas Moestl if (etime > 0.0) { 1424c4a5ef6eSThomas Moestl *destld = totaltransfersother; 1425c4a5ef6eSThomas Moestl *destld /= etime; 1426c4a5ef6eSThomas Moestl } else 1427c4a5ef6eSThomas Moestl *destld = 0.0; 1428c4a5ef6eSThomas Moestl break; 1429c4a5ef6eSThomas Moestl case DSM_MB_PER_SECOND: 1430c4a5ef6eSThomas Moestl *destld = totalbytes; 1431c4a5ef6eSThomas Moestl *destld /= 1024 * 1024; 1432c4a5ef6eSThomas Moestl if (etime > 0.0) 1433c4a5ef6eSThomas Moestl *destld /= etime; 1434c4a5ef6eSThomas Moestl else 1435c4a5ef6eSThomas Moestl *destld = 0.0; 1436c4a5ef6eSThomas Moestl break; 1437c4a5ef6eSThomas Moestl case DSM_MB_PER_SECOND_READ: 1438c4a5ef6eSThomas Moestl *destld = totalbytesread; 1439c4a5ef6eSThomas Moestl *destld /= 1024 * 1024; 1440c4a5ef6eSThomas Moestl if (etime > 0.0) 1441c4a5ef6eSThomas Moestl *destld /= etime; 1442c4a5ef6eSThomas Moestl else 1443c4a5ef6eSThomas Moestl *destld = 0.0; 1444c4a5ef6eSThomas Moestl break; 1445c4a5ef6eSThomas Moestl case DSM_MB_PER_SECOND_WRITE: 1446c4a5ef6eSThomas Moestl *destld = totalbyteswrite; 1447c4a5ef6eSThomas Moestl *destld /= 1024 * 1024; 1448c4a5ef6eSThomas Moestl if (etime > 0.0) 1449c4a5ef6eSThomas Moestl *destld /= etime; 1450c4a5ef6eSThomas Moestl else 1451c4a5ef6eSThomas Moestl *destld = 0.0; 1452c4a5ef6eSThomas Moestl break; 145336eab1f5SPoul-Henning Kamp case DSM_MB_PER_SECOND_FREE: 145436eab1f5SPoul-Henning Kamp *destld = totalbytesfree; 145536eab1f5SPoul-Henning Kamp *destld /= 1024 * 1024; 145636eab1f5SPoul-Henning Kamp if (etime > 0.0) 145736eab1f5SPoul-Henning Kamp *destld /= etime; 145836eab1f5SPoul-Henning Kamp else 145936eab1f5SPoul-Henning Kamp *destld = 0.0; 146036eab1f5SPoul-Henning Kamp break; 1461c4a5ef6eSThomas Moestl case DSM_BLOCKS_PER_SECOND: 1462c4a5ef6eSThomas Moestl *destld = totalblocks; 1463c4a5ef6eSThomas Moestl if (etime > 0.0) 1464c4a5ef6eSThomas Moestl *destld /= etime; 1465c4a5ef6eSThomas Moestl else 1466c4a5ef6eSThomas Moestl *destld = 0.0; 1467c4a5ef6eSThomas Moestl break; 1468c4a5ef6eSThomas Moestl case DSM_BLOCKS_PER_SECOND_READ: 1469c4a5ef6eSThomas Moestl *destld = totalblocksread; 1470c4a5ef6eSThomas Moestl if (etime > 0.0) 1471c4a5ef6eSThomas Moestl *destld /= etime; 1472c4a5ef6eSThomas Moestl else 1473c4a5ef6eSThomas Moestl *destld = 0.0; 1474c4a5ef6eSThomas Moestl break; 1475c4a5ef6eSThomas Moestl case DSM_BLOCKS_PER_SECOND_WRITE: 1476c4a5ef6eSThomas Moestl *destld = totalblockswrite; 1477c4a5ef6eSThomas Moestl if (etime > 0.0) 1478c4a5ef6eSThomas Moestl *destld /= etime; 1479c4a5ef6eSThomas Moestl else 1480c4a5ef6eSThomas Moestl *destld = 0.0; 1481c4a5ef6eSThomas Moestl break; 148236eab1f5SPoul-Henning Kamp case DSM_BLOCKS_PER_SECOND_FREE: 148336eab1f5SPoul-Henning Kamp *destld = totalblocksfree; 148436eab1f5SPoul-Henning Kamp if (etime > 0.0) 148536eab1f5SPoul-Henning Kamp *destld /= etime; 148636eab1f5SPoul-Henning Kamp else 148736eab1f5SPoul-Henning Kamp *destld = 0.0; 148836eab1f5SPoul-Henning Kamp break; 1489c4a5ef6eSThomas Moestl /* 1490*999efd90SKenneth D. Merry * Some devstat callers update the duration and some don't. 1491*999efd90SKenneth D. Merry * So this will only be accurate if they provide the 1492*999efd90SKenneth D. Merry * duration. 1493c4a5ef6eSThomas Moestl */ 1494c4a5ef6eSThomas Moestl case DSM_MS_PER_TRANSACTION: 1495c4a5ef6eSThomas Moestl if (totaltransfers > 0) { 1496fdd6757eSMikolaj Golub *destld = totalduration; 1497c4a5ef6eSThomas Moestl *destld /= totaltransfers; 1498c4a5ef6eSThomas Moestl *destld *= 1000; 1499c4a5ef6eSThomas Moestl } else 1500c4a5ef6eSThomas Moestl *destld = 0.0; 1501c4a5ef6eSThomas Moestl break; 1502c4a5ef6eSThomas Moestl case DSM_MS_PER_TRANSACTION_READ: 1503c4a5ef6eSThomas Moestl if (totaltransfersread > 0) { 1504fdd6757eSMikolaj Golub *destld = totaldurationread; 1505c4a5ef6eSThomas Moestl *destld /= totaltransfersread; 1506c4a5ef6eSThomas Moestl *destld *= 1000; 1507c4a5ef6eSThomas Moestl } else 1508c4a5ef6eSThomas Moestl *destld = 0.0; 1509c4a5ef6eSThomas Moestl break; 1510c4a5ef6eSThomas Moestl case DSM_MS_PER_TRANSACTION_WRITE: 1511c4a5ef6eSThomas Moestl if (totaltransferswrite > 0) { 1512fdd6757eSMikolaj Golub *destld = totaldurationwrite; 1513c4a5ef6eSThomas Moestl *destld /= totaltransferswrite; 1514c4a5ef6eSThomas Moestl *destld *= 1000; 1515c4a5ef6eSThomas Moestl } else 1516c4a5ef6eSThomas Moestl *destld = 0.0; 1517c4a5ef6eSThomas Moestl break; 151836eab1f5SPoul-Henning Kamp case DSM_MS_PER_TRANSACTION_FREE: 151936eab1f5SPoul-Henning Kamp if (totaltransfersfree > 0) { 1520fdd6757eSMikolaj Golub *destld = totaldurationfree; 152136eab1f5SPoul-Henning Kamp *destld /= totaltransfersfree; 152236eab1f5SPoul-Henning Kamp *destld *= 1000; 152336eab1f5SPoul-Henning Kamp } else 152436eab1f5SPoul-Henning Kamp *destld = 0.0; 152536eab1f5SPoul-Henning Kamp break; 152636eab1f5SPoul-Henning Kamp case DSM_MS_PER_TRANSACTION_OTHER: 152736eab1f5SPoul-Henning Kamp if (totaltransfersother > 0) { 1528fdd6757eSMikolaj Golub *destld = totaldurationother; 152936eab1f5SPoul-Henning Kamp *destld /= totaltransfersother; 153036eab1f5SPoul-Henning Kamp *destld *= 1000; 153136eab1f5SPoul-Henning Kamp } else 153236eab1f5SPoul-Henning Kamp *destld = 0.0; 153336eab1f5SPoul-Henning Kamp break; 153436eab1f5SPoul-Henning Kamp case DSM_BUSY_PCT: 153536eab1f5SPoul-Henning Kamp *destld = DELTA_T(busy_time); 153636eab1f5SPoul-Henning Kamp if (*destld < 0) 153736eab1f5SPoul-Henning Kamp *destld = 0; 153836eab1f5SPoul-Henning Kamp *destld /= etime; 153936eab1f5SPoul-Henning Kamp *destld *= 100; 15401455c4e2SPoul-Henning Kamp if (*destld < 0) 15411455c4e2SPoul-Henning Kamp *destld = 0; 154236eab1f5SPoul-Henning Kamp break; 154336eab1f5SPoul-Henning Kamp case DSM_QUEUE_LENGTH: 154436eab1f5SPoul-Henning Kamp *destu64 = current->start_count - current->end_count; 154536eab1f5SPoul-Henning Kamp break; 1546fdd6757eSMikolaj Golub case DSM_TOTAL_DURATION: 1547fdd6757eSMikolaj Golub *destld = totalduration; 1548fdd6757eSMikolaj Golub break; 1549fdd6757eSMikolaj Golub case DSM_TOTAL_DURATION_READ: 1550fdd6757eSMikolaj Golub *destld = totaldurationread; 1551fdd6757eSMikolaj Golub break; 1552fdd6757eSMikolaj Golub case DSM_TOTAL_DURATION_WRITE: 1553fdd6757eSMikolaj Golub *destld = totaldurationwrite; 1554fdd6757eSMikolaj Golub break; 1555fdd6757eSMikolaj Golub case DSM_TOTAL_DURATION_FREE: 1556fdd6757eSMikolaj Golub *destld = totaldurationfree; 1557fdd6757eSMikolaj Golub break; 1558fdd6757eSMikolaj Golub case DSM_TOTAL_DURATION_OTHER: 1559fdd6757eSMikolaj Golub *destld = totaldurationother; 1560fdd6757eSMikolaj Golub break; 1561fdd6757eSMikolaj Golub case DSM_TOTAL_BUSY_TIME: 1562fdd6757eSMikolaj Golub *destld = DELTA_T(busy_time); 1563fdd6757eSMikolaj Golub break; 156436eab1f5SPoul-Henning Kamp /* 156536eab1f5SPoul-Henning Kamp * XXX: comment out the default block to see if any case's are missing. 156636eab1f5SPoul-Henning Kamp */ 156736eab1f5SPoul-Henning Kamp #if 1 1568c4a5ef6eSThomas Moestl default: 1569c4a5ef6eSThomas Moestl /* 1570c4a5ef6eSThomas Moestl * This shouldn't happen, since we should have 1571c4a5ef6eSThomas Moestl * caught any out of range metrics at the top of 1572c4a5ef6eSThomas Moestl * the loop. 1573c4a5ef6eSThomas Moestl */ 1574c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 15757d72cda3SMaxime Henrion "%s: unknown metric %d", __func__, metric); 1576c4a5ef6eSThomas Moestl retval = -1; 1577c4a5ef6eSThomas Moestl goto bailout; 1578c4a5ef6eSThomas Moestl break; /* NOTREACHED */ 157936eab1f5SPoul-Henning Kamp #endif 1580c4a5ef6eSThomas Moestl } 1581c4a5ef6eSThomas Moestl } 1582c4a5ef6eSThomas Moestl 1583c4a5ef6eSThomas Moestl bailout: 1584c4a5ef6eSThomas Moestl 1585c4a5ef6eSThomas Moestl va_end(ap); 1586c4a5ef6eSThomas Moestl return(retval); 1587c4a5ef6eSThomas Moestl } 1588c4a5ef6eSThomas Moestl 1589c4a5ef6eSThomas Moestl static int 1590c4a5ef6eSThomas Moestl readkmem(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes) 1591c4a5ef6eSThomas Moestl { 1592c4a5ef6eSThomas Moestl 1593c4a5ef6eSThomas Moestl if (kvm_read(kd, addr, buf, nbytes) == -1) { 1594c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 15957d72cda3SMaxime Henrion "%s: error reading value (kvm_read): %s", __func__, 1596c4a5ef6eSThomas Moestl kvm_geterr(kd)); 1597c4a5ef6eSThomas Moestl return(-1); 1598c4a5ef6eSThomas Moestl } 1599c4a5ef6eSThomas Moestl return(0); 1600c4a5ef6eSThomas Moestl } 1601c4a5ef6eSThomas Moestl 1602c4a5ef6eSThomas Moestl static int 1603c3508206SKenneth D. Merry readkmem_nl(kvm_t *kd, const char *name, void *buf, size_t nbytes) 1604c4a5ef6eSThomas Moestl { 1605c3508206SKenneth D. Merry struct nlist nl[2]; 1606c3508206SKenneth D. Merry 1607f664b76fSAlexander Kabaev nl[0].n_name = (char *)name; 1608c3508206SKenneth D. Merry nl[1].n_name = NULL; 1609c4a5ef6eSThomas Moestl 1610c4a5ef6eSThomas Moestl if (kvm_nlist(kd, nl) == -1) { 1611c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1612c4a5ef6eSThomas Moestl "%s: error getting name list (kvm_nlist): %s", 16137d72cda3SMaxime Henrion __func__, kvm_geterr(kd)); 1614c4a5ef6eSThomas Moestl return(-1); 1615c4a5ef6eSThomas Moestl } 1616c4a5ef6eSThomas Moestl return(readkmem(kd, nl[0].n_value, buf, nbytes)); 1617c4a5ef6eSThomas Moestl } 1618c4a5ef6eSThomas Moestl 1619c4a5ef6eSThomas Moestl /* 1620c4a5ef6eSThomas Moestl * This duplicates the functionality of the kernel sysctl handler for poking 1621c4a5ef6eSThomas Moestl * through crash dumps. 1622c4a5ef6eSThomas Moestl */ 1623c4a5ef6eSThomas Moestl static char * 1624c4a5ef6eSThomas Moestl get_devstat_kvm(kvm_t *kd) 1625c4a5ef6eSThomas Moestl { 16269dbcd4b0SStefan Farfeleder int i, wp; 1627c4a5ef6eSThomas Moestl long gen; 1628c4a5ef6eSThomas Moestl struct devstat *nds; 1629c4a5ef6eSThomas Moestl struct devstat ds; 1630c4a5ef6eSThomas Moestl struct devstatlist dhead; 1631c4a5ef6eSThomas Moestl int num_devs; 1632c4a5ef6eSThomas Moestl char *rv = NULL; 1633c4a5ef6eSThomas Moestl 16347194d335SPoul-Henning Kamp if ((num_devs = devstat_getnumdevs(kd)) <= 0) 1635c4a5ef6eSThomas Moestl return(NULL); 1636c4a5ef6eSThomas Moestl if (KREADNL(kd, X_DEVICE_STATQ, dhead) == -1) 1637c4a5ef6eSThomas Moestl return(NULL); 1638c4a5ef6eSThomas Moestl 1639c4a5ef6eSThomas Moestl nds = STAILQ_FIRST(&dhead); 1640c4a5ef6eSThomas Moestl 1641c4a5ef6eSThomas Moestl if ((rv = malloc(sizeof(gen))) == NULL) { 1642c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1643c4a5ef6eSThomas Moestl "%s: out of memory (initial malloc failed)", 16447d72cda3SMaxime Henrion __func__); 1645c4a5ef6eSThomas Moestl return(NULL); 1646c4a5ef6eSThomas Moestl } 16477194d335SPoul-Henning Kamp gen = devstat_getgeneration(kd); 1648c4a5ef6eSThomas Moestl memcpy(rv, &gen, sizeof(gen)); 1649c4a5ef6eSThomas Moestl wp = sizeof(gen); 1650c4a5ef6eSThomas Moestl /* 1651c4a5ef6eSThomas Moestl * Now push out all the devices. 1652c4a5ef6eSThomas Moestl */ 1653c4a5ef6eSThomas Moestl for (i = 0; (nds != NULL) && (i < num_devs); 1654c4a5ef6eSThomas Moestl nds = STAILQ_NEXT(nds, dev_links), i++) { 1655c4a5ef6eSThomas Moestl if (readkmem(kd, (long)nds, &ds, sizeof(ds)) == -1) { 1656c4a5ef6eSThomas Moestl free(rv); 1657c4a5ef6eSThomas Moestl return(NULL); 1658c4a5ef6eSThomas Moestl } 1659c4a5ef6eSThomas Moestl nds = &ds; 1660c4a5ef6eSThomas Moestl rv = (char *)reallocf(rv, sizeof(gen) + 1661c4a5ef6eSThomas Moestl sizeof(ds) * (i + 1)); 1662c4a5ef6eSThomas Moestl if (rv == NULL) { 1663c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1664c4a5ef6eSThomas Moestl "%s: out of memory (malloc failed)", 16657d72cda3SMaxime Henrion __func__); 1666c4a5ef6eSThomas Moestl return(NULL); 1667c4a5ef6eSThomas Moestl } 1668c4a5ef6eSThomas Moestl memcpy(rv + wp, &ds, sizeof(ds)); 1669c4a5ef6eSThomas Moestl wp += sizeof(ds); 1670c4a5ef6eSThomas Moestl } 1671c4a5ef6eSThomas Moestl return(rv); 1672c4a5ef6eSThomas Moestl } 1673