15e53a4f9SPedro F. Giffuni /*- 25e53a4f9SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 35e53a4f9SPedro F. Giffuni * 428fb27baSJustin T. Gibbs * Copyright (c) 1997, 1998 Kenneth D. Merry. 528fb27baSJustin T. Gibbs * All rights reserved. 628fb27baSJustin T. Gibbs * 728fb27baSJustin T. Gibbs * Redistribution and use in source and binary forms, with or without 828fb27baSJustin T. Gibbs * modification, are permitted provided that the following conditions 928fb27baSJustin T. Gibbs * are met: 1028fb27baSJustin T. Gibbs * 1. Redistributions of source code must retain the above copyright 1128fb27baSJustin T. Gibbs * notice, this list of conditions and the following disclaimer. 1228fb27baSJustin T. Gibbs * 2. Redistributions in binary form must reproduce the above copyright 1328fb27baSJustin T. Gibbs * notice, this list of conditions and the following disclaimer in the 1428fb27baSJustin T. Gibbs * documentation and/or other materials provided with the distribution. 1528fb27baSJustin T. Gibbs * 3. The name of the author may not be used to endorse or promote products 1628fb27baSJustin T. Gibbs * derived from this software without specific prior written permission. 1728fb27baSJustin T. Gibbs * 1828fb27baSJustin T. Gibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1928fb27baSJustin T. Gibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2028fb27baSJustin T. Gibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2128fb27baSJustin T. Gibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2228fb27baSJustin T. Gibbs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2328fb27baSJustin T. Gibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2428fb27baSJustin T. Gibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2528fb27baSJustin T. Gibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2628fb27baSJustin T. Gibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2728fb27baSJustin T. Gibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2828fb27baSJustin T. Gibbs * SUCH DAMAGE. 2928fb27baSJustin T. Gibbs */ 3028fb27baSJustin T. Gibbs 31e67f5b9fSMatthew Dillon #include <sys/cdefs.h> 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}, 78*d282baddSAlexander Motin {"nvme", DEVSTAT_TYPE_IF_NVME, DEVSTAT_MATCH_IF}, 7928fb27baSJustin T. Gibbs {"worm", DEVSTAT_TYPE_WORM, DEVSTAT_MATCH_TYPE}, 8028fb27baSJustin T. Gibbs {"sa", DEVSTAT_TYPE_SEQUENTIAL,DEVSTAT_MATCH_TYPE}, 8128fb27baSJustin T. Gibbs {"pass", DEVSTAT_TYPE_PASS, DEVSTAT_MATCH_PASS}, 8228fb27baSJustin T. Gibbs {"optical", DEVSTAT_TYPE_OPTICAL, DEVSTAT_MATCH_TYPE}, 8328fb27baSJustin T. Gibbs {"array", DEVSTAT_TYPE_STORARRAY, DEVSTAT_MATCH_TYPE}, 8428fb27baSJustin T. Gibbs {"changer", DEVSTAT_TYPE_CHANGER, DEVSTAT_MATCH_TYPE}, 8528fb27baSJustin T. Gibbs {"scanner", DEVSTAT_TYPE_SCANNER, DEVSTAT_MATCH_TYPE}, 8628fb27baSJustin T. Gibbs {"printer", DEVSTAT_TYPE_PRINTER, DEVSTAT_MATCH_TYPE}, 8728fb27baSJustin T. Gibbs {"floppy", DEVSTAT_TYPE_FLOPPY, DEVSTAT_MATCH_TYPE}, 8828fb27baSJustin T. Gibbs {"proc", DEVSTAT_TYPE_PROCESSOR, DEVSTAT_MATCH_TYPE}, 8928fb27baSJustin T. Gibbs {"comm", DEVSTAT_TYPE_COMM, DEVSTAT_MATCH_TYPE}, 9028fb27baSJustin T. Gibbs {"enclosure", DEVSTAT_TYPE_ENCLOSURE, DEVSTAT_MATCH_TYPE}, 9128fb27baSJustin T. Gibbs {NULL, 0, 0} 9228fb27baSJustin T. Gibbs }; 9328fb27baSJustin T. Gibbs 94c4a5ef6eSThomas Moestl struct devstat_args { 95c4a5ef6eSThomas Moestl devstat_metric metric; 96c4a5ef6eSThomas Moestl devstat_arg_type argtype; 97c4a5ef6eSThomas Moestl } devstat_arg_list[] = { 98c4a5ef6eSThomas Moestl { DSM_NONE, DEVSTAT_ARG_NOTYPE }, 99c4a5ef6eSThomas Moestl { DSM_TOTAL_BYTES, DEVSTAT_ARG_UINT64 }, 100c4a5ef6eSThomas Moestl { DSM_TOTAL_BYTES_READ, DEVSTAT_ARG_UINT64 }, 101c4a5ef6eSThomas Moestl { DSM_TOTAL_BYTES_WRITE, DEVSTAT_ARG_UINT64 }, 102c4a5ef6eSThomas Moestl { DSM_TOTAL_TRANSFERS, DEVSTAT_ARG_UINT64 }, 103c4a5ef6eSThomas Moestl { DSM_TOTAL_TRANSFERS_READ, DEVSTAT_ARG_UINT64 }, 104c4a5ef6eSThomas Moestl { DSM_TOTAL_TRANSFERS_WRITE, DEVSTAT_ARG_UINT64 }, 105773865deSPoul-Henning Kamp { DSM_TOTAL_TRANSFERS_OTHER, DEVSTAT_ARG_UINT64 }, 106c4a5ef6eSThomas Moestl { DSM_TOTAL_BLOCKS, DEVSTAT_ARG_UINT64 }, 107c4a5ef6eSThomas Moestl { DSM_TOTAL_BLOCKS_READ, DEVSTAT_ARG_UINT64 }, 108c4a5ef6eSThomas Moestl { DSM_TOTAL_BLOCKS_WRITE, DEVSTAT_ARG_UINT64 }, 109c4a5ef6eSThomas Moestl { DSM_KB_PER_TRANSFER, DEVSTAT_ARG_LD }, 110c4a5ef6eSThomas Moestl { DSM_KB_PER_TRANSFER_READ, DEVSTAT_ARG_LD }, 111c4a5ef6eSThomas Moestl { DSM_KB_PER_TRANSFER_WRITE, DEVSTAT_ARG_LD }, 112c4a5ef6eSThomas Moestl { DSM_TRANSFERS_PER_SECOND, DEVSTAT_ARG_LD }, 113c4a5ef6eSThomas Moestl { DSM_TRANSFERS_PER_SECOND_READ, DEVSTAT_ARG_LD }, 114c4a5ef6eSThomas Moestl { DSM_TRANSFERS_PER_SECOND_WRITE, DEVSTAT_ARG_LD }, 115c4a5ef6eSThomas Moestl { DSM_TRANSFERS_PER_SECOND_OTHER, DEVSTAT_ARG_LD }, 116c4a5ef6eSThomas Moestl { DSM_MB_PER_SECOND, DEVSTAT_ARG_LD }, 117c4a5ef6eSThomas Moestl { DSM_MB_PER_SECOND_READ, DEVSTAT_ARG_LD }, 118c4a5ef6eSThomas Moestl { DSM_MB_PER_SECOND_WRITE, DEVSTAT_ARG_LD }, 119c4a5ef6eSThomas Moestl { DSM_BLOCKS_PER_SECOND, DEVSTAT_ARG_LD }, 120c4a5ef6eSThomas Moestl { DSM_BLOCKS_PER_SECOND_READ, DEVSTAT_ARG_LD }, 121c4a5ef6eSThomas Moestl { DSM_BLOCKS_PER_SECOND_WRITE, DEVSTAT_ARG_LD }, 122c4a5ef6eSThomas Moestl { DSM_MS_PER_TRANSACTION, DEVSTAT_ARG_LD }, 123c4a5ef6eSThomas Moestl { DSM_MS_PER_TRANSACTION_READ, DEVSTAT_ARG_LD }, 124884539f7SKenneth D. Merry { DSM_MS_PER_TRANSACTION_WRITE, DEVSTAT_ARG_LD }, 125ec0fa09cSPoul-Henning Kamp { DSM_SKIP, DEVSTAT_ARG_SKIP }, 126773865deSPoul-Henning Kamp { DSM_TOTAL_BYTES_FREE, DEVSTAT_ARG_UINT64 }, 127773865deSPoul-Henning Kamp { DSM_TOTAL_TRANSFERS_FREE, DEVSTAT_ARG_UINT64 }, 128773865deSPoul-Henning Kamp { DSM_TOTAL_BLOCKS_FREE, DEVSTAT_ARG_UINT64 }, 129773865deSPoul-Henning Kamp { DSM_KB_PER_TRANSFER_FREE, DEVSTAT_ARG_LD }, 130773865deSPoul-Henning Kamp { DSM_MB_PER_SECOND_FREE, DEVSTAT_ARG_LD }, 131773865deSPoul-Henning Kamp { DSM_TRANSFERS_PER_SECOND_FREE, DEVSTAT_ARG_LD }, 132773865deSPoul-Henning Kamp { DSM_BLOCKS_PER_SECOND_FREE, DEVSTAT_ARG_LD }, 13336eab1f5SPoul-Henning Kamp { DSM_MS_PER_TRANSACTION_OTHER, DEVSTAT_ARG_LD }, 134773865deSPoul-Henning Kamp { DSM_MS_PER_TRANSACTION_FREE, DEVSTAT_ARG_LD }, 13536eab1f5SPoul-Henning Kamp { DSM_BUSY_PCT, DEVSTAT_ARG_LD }, 13636eab1f5SPoul-Henning Kamp { DSM_QUEUE_LENGTH, DEVSTAT_ARG_UINT64 }, 137fdd6757eSMikolaj Golub { DSM_TOTAL_DURATION, DEVSTAT_ARG_LD }, 138fdd6757eSMikolaj Golub { DSM_TOTAL_DURATION_READ, DEVSTAT_ARG_LD }, 139fdd6757eSMikolaj Golub { DSM_TOTAL_DURATION_WRITE, DEVSTAT_ARG_LD }, 140fdd6757eSMikolaj Golub { DSM_TOTAL_DURATION_FREE, DEVSTAT_ARG_LD }, 141fdd6757eSMikolaj Golub { DSM_TOTAL_DURATION_OTHER, DEVSTAT_ARG_LD }, 142fdd6757eSMikolaj Golub { DSM_TOTAL_BUSY_TIME, DEVSTAT_ARG_LD }, 143c4a5ef6eSThomas Moestl }; 144c4a5ef6eSThomas Moestl 145c3508206SKenneth D. Merry static const char *namelist[] = { 146c4a5ef6eSThomas Moestl #define X_NUMDEVS 0 147c4a5ef6eSThomas Moestl "_devstat_num_devs", 148c4a5ef6eSThomas Moestl #define X_GENERATION 1 149c4a5ef6eSThomas Moestl "_devstat_generation", 150c4a5ef6eSThomas Moestl #define X_VERSION 2 151c4a5ef6eSThomas Moestl "_devstat_version", 152c4a5ef6eSThomas Moestl #define X_DEVICE_STATQ 3 153c4a5ef6eSThomas Moestl "_device_statq", 154ef6b3fcfSSergey Kandaurov #define X_TIME_UPTIME 4 155ef6b3fcfSSergey Kandaurov "_time_uptime", 156ef6b3fcfSSergey Kandaurov #define X_END 5 157c4a5ef6eSThomas Moestl }; 158c4a5ef6eSThomas Moestl 15928fb27baSJustin T. Gibbs /* 16028fb27baSJustin T. Gibbs * Local function declarations. 16128fb27baSJustin T. Gibbs */ 16228fb27baSJustin T. Gibbs static int compare_select(const void *arg1, const void *arg2); 163c4a5ef6eSThomas Moestl static int readkmem(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes); 164c3508206SKenneth D. Merry static int readkmem_nl(kvm_t *kd, const char *name, void *buf, size_t nbytes); 165c4a5ef6eSThomas Moestl static char *get_devstat_kvm(kvm_t *kd); 166c4a5ef6eSThomas Moestl 167c4a5ef6eSThomas Moestl #define KREADNL(kd, var, val) \ 168c4a5ef6eSThomas Moestl readkmem_nl(kd, namelist[var], &val, sizeof(val)) 16928fb27baSJustin T. Gibbs 17028fb27baSJustin T. Gibbs int 171c4a5ef6eSThomas Moestl devstat_getnumdevs(kvm_t *kd) 17228fb27baSJustin T. Gibbs { 17328fb27baSJustin T. Gibbs size_t numdevsize; 17428fb27baSJustin T. Gibbs int numdevs; 17528fb27baSJustin T. Gibbs 17628fb27baSJustin T. Gibbs numdevsize = sizeof(int); 17728fb27baSJustin T. Gibbs 17828fb27baSJustin T. Gibbs /* 17928fb27baSJustin T. Gibbs * Find out how many devices we have in the system. 18028fb27baSJustin T. Gibbs */ 181c4a5ef6eSThomas Moestl if (kd == NULL) { 18228fb27baSJustin T. Gibbs if (sysctlbyname("kern.devstat.numdevs", &numdevs, 18328fb27baSJustin T. Gibbs &numdevsize, NULL, 0) == -1) { 184c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 185c4a5ef6eSThomas Moestl "%s: error getting number of devices\n" 1867d72cda3SMaxime Henrion "%s: %s", __func__, __func__, 187c4a5ef6eSThomas Moestl strerror(errno)); 18828fb27baSJustin T. Gibbs return(-1); 18928fb27baSJustin T. Gibbs } else 19028fb27baSJustin T. Gibbs return(numdevs); 191c4a5ef6eSThomas Moestl } else { 192c3508206SKenneth D. Merry 193c4a5ef6eSThomas Moestl if (KREADNL(kd, X_NUMDEVS, numdevs) == -1) 194c4a5ef6eSThomas Moestl return(-1); 195c4a5ef6eSThomas Moestl else 196c4a5ef6eSThomas Moestl return(numdevs); 197c4a5ef6eSThomas Moestl } 19828fb27baSJustin T. Gibbs } 19928fb27baSJustin T. Gibbs 20028fb27baSJustin T. Gibbs /* 20128fb27baSJustin T. Gibbs * This is an easy way to get the generation number, but the generation is 20228fb27baSJustin T. Gibbs * supplied in a more atmoic manner by the kern.devstat.all sysctl. 20328fb27baSJustin T. Gibbs * Because this generation sysctl is separate from the statistics sysctl, 20428fb27baSJustin T. Gibbs * the device list and the generation could change between the time that 2056a4d9095SSergey Kandaurov * this function is called and the device list is retrieved. 20628fb27baSJustin T. Gibbs */ 207bcc6a3daSKenneth D. Merry long 208c4a5ef6eSThomas Moestl devstat_getgeneration(kvm_t *kd) 20928fb27baSJustin T. Gibbs { 21028fb27baSJustin T. Gibbs size_t gensize; 211bcc6a3daSKenneth D. Merry long generation; 21228fb27baSJustin T. Gibbs 213bcc6a3daSKenneth D. Merry gensize = sizeof(long); 21428fb27baSJustin T. Gibbs 21528fb27baSJustin T. Gibbs /* 21628fb27baSJustin T. Gibbs * Get the current generation number. 21728fb27baSJustin T. Gibbs */ 218c4a5ef6eSThomas Moestl if (kd == NULL) { 21928fb27baSJustin T. Gibbs if (sysctlbyname("kern.devstat.generation", &generation, 22028fb27baSJustin T. Gibbs &gensize, NULL, 0) == -1) { 221c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 222c4a5ef6eSThomas Moestl "%s: error getting devstat generation\n%s: %s", 2237d72cda3SMaxime Henrion __func__, __func__, strerror(errno)); 22428fb27baSJustin T. Gibbs return(-1); 22528fb27baSJustin T. Gibbs } else 22628fb27baSJustin T. Gibbs return(generation); 227c4a5ef6eSThomas Moestl } else { 228c4a5ef6eSThomas Moestl if (KREADNL(kd, X_GENERATION, generation) == -1) 229c4a5ef6eSThomas Moestl return(-1); 230c4a5ef6eSThomas Moestl else 231c4a5ef6eSThomas Moestl return(generation); 232c4a5ef6eSThomas Moestl } 23328fb27baSJustin T. Gibbs } 23428fb27baSJustin T. Gibbs 23528fb27baSJustin T. Gibbs /* 23628fb27baSJustin T. Gibbs * Get the current devstat version. The return value of this function 23728fb27baSJustin T. Gibbs * should be compared with DEVSTAT_VERSION, which is defined in 23828fb27baSJustin T. Gibbs * sys/devicestat.h. This will enable userland programs to determine 23928fb27baSJustin T. Gibbs * whether they are out of sync with the kernel. 24028fb27baSJustin T. Gibbs */ 24128fb27baSJustin T. Gibbs int 242c4a5ef6eSThomas Moestl devstat_getversion(kvm_t *kd) 24328fb27baSJustin T. Gibbs { 24428fb27baSJustin T. Gibbs size_t versize; 24528fb27baSJustin T. Gibbs int version; 24628fb27baSJustin T. Gibbs 24728fb27baSJustin T. Gibbs versize = sizeof(int); 24828fb27baSJustin T. Gibbs 24928fb27baSJustin T. Gibbs /* 25028fb27baSJustin T. Gibbs * Get the current devstat version. 25128fb27baSJustin T. Gibbs */ 252c4a5ef6eSThomas Moestl if (kd == NULL) { 25328fb27baSJustin T. Gibbs if (sysctlbyname("kern.devstat.version", &version, &versize, 25428fb27baSJustin T. Gibbs NULL, 0) == -1) { 255c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 256c4a5ef6eSThomas Moestl "%s: error getting devstat version\n%s: %s", 2577d72cda3SMaxime Henrion __func__, __func__, strerror(errno)); 25828fb27baSJustin T. Gibbs return(-1); 25928fb27baSJustin T. Gibbs } else 26028fb27baSJustin T. Gibbs return(version); 261c4a5ef6eSThomas Moestl } else { 262c4a5ef6eSThomas Moestl if (KREADNL(kd, X_VERSION, version) == -1) 263c4a5ef6eSThomas Moestl return(-1); 264c4a5ef6eSThomas Moestl else 265c4a5ef6eSThomas Moestl return(version); 266c4a5ef6eSThomas Moestl } 26728fb27baSJustin T. Gibbs } 26828fb27baSJustin T. Gibbs 26928fb27baSJustin T. Gibbs /* 27028fb27baSJustin T. Gibbs * Check the devstat version we know about against the devstat version the 27128fb27baSJustin T. Gibbs * kernel knows about. If they don't match, print an error into the 27228fb27baSJustin T. Gibbs * devstat error buffer, and return -1. If they match, return 0. 27328fb27baSJustin T. Gibbs */ 27428fb27baSJustin T. Gibbs int 275c4a5ef6eSThomas Moestl devstat_checkversion(kvm_t *kd) 27628fb27baSJustin T. Gibbs { 2776d3e1426SBrian Somers int buflen, res, retval = 0, version; 27828fb27baSJustin T. Gibbs 279c4a5ef6eSThomas Moestl version = devstat_getversion(kd); 280eded794aSKenneth D. Merry 281eded794aSKenneth D. Merry if (version != DEVSTAT_VERSION) { 282eded794aSKenneth D. Merry /* 2836d3e1426SBrian Somers * If getversion() returns an error (i.e. -1), then it 284eded794aSKenneth D. Merry * has printed an error message in the buffer. Therefore, 285eded794aSKenneth D. Merry * we need to add a \n to the end of that message before we 286eded794aSKenneth D. Merry * print our own message in the buffer. 287eded794aSKenneth D. Merry */ 2886d3e1426SBrian Somers if (version == -1) 289eded794aSKenneth D. Merry buflen = strlen(devstat_errbuf); 2906d3e1426SBrian Somers else 2916d3e1426SBrian Somers buflen = 0; 292eded794aSKenneth D. Merry 2936d3e1426SBrian Somers res = snprintf(devstat_errbuf + buflen, 2946d3e1426SBrian Somers DEVSTAT_ERRBUF_SIZE - buflen, 2956d3e1426SBrian Somers "%s%s: userland devstat version %d is not " 2964fb9d384SKenneth D. Merry "the same as the kernel\n%s: devstat " 2974fb9d384SKenneth D. Merry "version %d\n", version == -1 ? "\n" : "", 2987d72cda3SMaxime Henrion __func__, DEVSTAT_VERSION, __func__, version); 29928fb27baSJustin T. Gibbs 3006d3e1426SBrian Somers if (res < 0) 3016d3e1426SBrian Somers devstat_errbuf[buflen] = '\0'; 302eded794aSKenneth D. Merry 3036d3e1426SBrian Somers buflen = strlen(devstat_errbuf); 304eded794aSKenneth D. Merry if (version < DEVSTAT_VERSION) 3056d3e1426SBrian Somers res = snprintf(devstat_errbuf + buflen, 3066d3e1426SBrian Somers DEVSTAT_ERRBUF_SIZE - buflen, 3074fb9d384SKenneth D. Merry "%s: libdevstat newer than kernel\n", 3087d72cda3SMaxime Henrion __func__); 30928fb27baSJustin T. Gibbs else 3106d3e1426SBrian Somers res = snprintf(devstat_errbuf + buflen, 3116d3e1426SBrian Somers DEVSTAT_ERRBUF_SIZE - buflen, 3124fb9d384SKenneth D. Merry "%s: kernel newer than libdevstat\n", 3137d72cda3SMaxime Henrion __func__); 31428fb27baSJustin T. Gibbs 3156d3e1426SBrian Somers if (res < 0) 3166d3e1426SBrian Somers devstat_errbuf[buflen] = '\0'; 31728fb27baSJustin T. Gibbs 31828fb27baSJustin T. Gibbs retval = -1; 31928fb27baSJustin T. Gibbs } 32028fb27baSJustin T. Gibbs 32128fb27baSJustin T. Gibbs return(retval); 32228fb27baSJustin T. Gibbs } 32328fb27baSJustin T. Gibbs 32428fb27baSJustin T. Gibbs /* 32528fb27baSJustin T. Gibbs * Get the current list of devices and statistics, and the current 32628fb27baSJustin T. Gibbs * generation number. 32728fb27baSJustin T. Gibbs * 32828fb27baSJustin T. Gibbs * Return values: 32928fb27baSJustin T. Gibbs * -1 -- error 33028fb27baSJustin T. Gibbs * 0 -- device list is unchanged 33128fb27baSJustin T. Gibbs * 1 -- device list has changed 33228fb27baSJustin T. Gibbs */ 33328fb27baSJustin T. Gibbs int 334c4a5ef6eSThomas Moestl devstat_getdevs(kvm_t *kd, struct statinfo *stats) 33528fb27baSJustin T. Gibbs { 33628fb27baSJustin T. Gibbs int error; 33728fb27baSJustin T. Gibbs size_t dssize; 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 oldgeneration = dinfo->generation; 35228fb27baSJustin T. Gibbs 353ef6b3fcfSSergey Kandaurov if (kd == NULL) { 3547194d335SPoul-Henning Kamp clock_gettime(CLOCK_MONOTONIC, &ts); 3557194d335SPoul-Henning Kamp stats->snap_time = ts.tv_sec + ts.tv_nsec * 1e-9; 356c4a5ef6eSThomas Moestl 357c4a5ef6eSThomas Moestl /* If this is our first time through, mem_ptr will be null. */ 35828fb27baSJustin T. Gibbs if (dinfo->mem_ptr == NULL) { 35928fb27baSJustin T. Gibbs /* 36028fb27baSJustin T. Gibbs * Get the number of devices. If it's negative, it's an 36128fb27baSJustin T. Gibbs * error. Don't bother setting the error string, since 36228fb27baSJustin T. Gibbs * getnumdevs() has already done that for us. 36328fb27baSJustin T. Gibbs */ 36416830b0cSPoul-Henning Kamp if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0) 36528fb27baSJustin T. Gibbs return(-1); 36628fb27baSJustin T. Gibbs 36728fb27baSJustin T. Gibbs /* 368c4a5ef6eSThomas Moestl * The kern.devstat.all sysctl returns the current 369c4a5ef6eSThomas Moestl * generation number, as well as all the devices. 370c4a5ef6eSThomas Moestl * So we need four bytes more. 37128fb27baSJustin T. Gibbs */ 372c4a5ef6eSThomas Moestl dssize = (dinfo->numdevs * sizeof(struct devstat)) + 373c4a5ef6eSThomas Moestl sizeof(long); 37428fb27baSJustin T. Gibbs dinfo->mem_ptr = (u_int8_t *)malloc(dssize); 375a18e4616SGuy Helmer if (dinfo->mem_ptr == NULL) { 376a18e4616SGuy Helmer snprintf(devstat_errbuf, sizeof(devstat_errbuf), 377a18e4616SGuy Helmer "%s: Cannot allocate memory for mem_ptr element", 378a18e4616SGuy Helmer __func__); 379a18e4616SGuy Helmer return(-1); 380a18e4616SGuy Helmer } 38128fb27baSJustin T. Gibbs } else 382c4a5ef6eSThomas Moestl dssize = (dinfo->numdevs * sizeof(struct devstat)) + 383c4a5ef6eSThomas Moestl sizeof(long); 38428fb27baSJustin T. Gibbs 38528fb27baSJustin T. Gibbs /* 38628fb27baSJustin T. Gibbs * Request all of the devices. We only really allow for one 387c4a5ef6eSThomas Moestl * ENOMEM failure. It would, of course, be possible to just go 388c4a5ef6eSThomas Moestl * in a loop and keep reallocing the device structure until we 389c4a5ef6eSThomas Moestl * don't get ENOMEM back. I'm not sure it's worth it, though. 390c4a5ef6eSThomas Moestl * If devices are being added to the system that quickly, maybe 391c4a5ef6eSThomas Moestl * the user can just wait until all devices are added. 39228fb27baSJustin T. Gibbs */ 39336eab1f5SPoul-Henning Kamp for (;;) { 39436eab1f5SPoul-Henning Kamp error = sysctlbyname("kern.devstat.all", 39536eab1f5SPoul-Henning Kamp dinfo->mem_ptr, 39636eab1f5SPoul-Henning Kamp &dssize, NULL, 0); 39736eab1f5SPoul-Henning Kamp if (error != -1 || errno != EBUSY) 39836eab1f5SPoul-Henning Kamp break; 39936eab1f5SPoul-Henning Kamp } 40036eab1f5SPoul-Henning Kamp if (error == -1) { 40128fb27baSJustin T. Gibbs /* 40228fb27baSJustin T. Gibbs * If we get ENOMEM back, that means that there are 40328fb27baSJustin T. Gibbs * more devices now, so we need to allocate more 40428fb27baSJustin T. Gibbs * space for the device array. 40528fb27baSJustin T. Gibbs */ 40628fb27baSJustin T. Gibbs if (errno == ENOMEM) { 40728fb27baSJustin T. Gibbs /* 408c4a5ef6eSThomas Moestl * No need to set the error string here, 4097194d335SPoul-Henning Kamp * devstat_getnumdevs() will do that if it fails. 41028fb27baSJustin T. Gibbs */ 41116830b0cSPoul-Henning Kamp if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0) 41228fb27baSJustin T. Gibbs return(-1); 41328fb27baSJustin T. Gibbs 414c4a5ef6eSThomas Moestl dssize = (dinfo->numdevs * 415c4a5ef6eSThomas Moestl sizeof(struct devstat)) + sizeof(long); 416c4a5ef6eSThomas Moestl dinfo->mem_ptr = (u_int8_t *) 417c4a5ef6eSThomas Moestl realloc(dinfo->mem_ptr, dssize); 41828fb27baSJustin T. Gibbs if ((error = sysctlbyname("kern.devstat.all", 41928fb27baSJustin T. Gibbs dinfo->mem_ptr, &dssize, NULL, 0)) == -1) { 420c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, 421c4a5ef6eSThomas Moestl sizeof(devstat_errbuf), 422c4a5ef6eSThomas Moestl "%s: error getting device " 4237d72cda3SMaxime Henrion "stats\n%s: %s", __func__, 4247d72cda3SMaxime Henrion __func__, strerror(errno)); 42528fb27baSJustin T. Gibbs return(-1); 42628fb27baSJustin T. Gibbs } 42728fb27baSJustin T. Gibbs } else { 428c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 42928fb27baSJustin T. Gibbs "%s: error getting device stats\n" 4307d72cda3SMaxime Henrion "%s: %s", __func__, __func__, 43128fb27baSJustin T. Gibbs strerror(errno)); 43228fb27baSJustin T. Gibbs return(-1); 43328fb27baSJustin T. Gibbs } 43428fb27baSJustin T. Gibbs } 43528fb27baSJustin T. Gibbs 436c4a5ef6eSThomas Moestl } else { 437ef6b3fcfSSergey Kandaurov if (KREADNL(kd, X_TIME_UPTIME, ts.tv_sec) == -1) 438ef6b3fcfSSergey Kandaurov return(-1); 439ef6b3fcfSSergey Kandaurov else 440ef6b3fcfSSergey Kandaurov stats->snap_time = ts.tv_sec; 441ef6b3fcfSSergey Kandaurov 442c4a5ef6eSThomas Moestl /* 443c4a5ef6eSThomas Moestl * This is of course non-atomic, but since we are working 444c4a5ef6eSThomas Moestl * on a core dump, the generation is unlikely to change 445c4a5ef6eSThomas Moestl */ 44616830b0cSPoul-Henning Kamp if ((dinfo->numdevs = devstat_getnumdevs(kd)) == -1) 447c4a5ef6eSThomas Moestl return(-1); 448140490e2SMaxime Henrion if ((dinfo->mem_ptr = (u_int8_t *)get_devstat_kvm(kd)) == NULL) 449c4a5ef6eSThomas Moestl return(-1); 450c4a5ef6eSThomas Moestl } 45128fb27baSJustin T. Gibbs /* 45228fb27baSJustin T. Gibbs * The sysctl spits out the generation as the first four bytes, 45328fb27baSJustin T. Gibbs * then all of the device statistics structures. 45428fb27baSJustin T. Gibbs */ 455bcc6a3daSKenneth D. Merry dinfo->generation = *(long *)dinfo->mem_ptr; 45628fb27baSJustin T. Gibbs 45728fb27baSJustin T. Gibbs /* 45828fb27baSJustin T. Gibbs * If the generation has changed, and if the current number of 45928fb27baSJustin T. Gibbs * devices is not the same as the number of devices recorded in the 46028fb27baSJustin T. Gibbs * devinfo structure, it is likely that the device list has shrunk. 46128fb27baSJustin T. Gibbs * The reason that it is likely that the device list has shrunk in 46228fb27baSJustin T. Gibbs * this case is that if the device list has grown, the sysctl above 46328fb27baSJustin T. Gibbs * will return an ENOMEM error, and we will reset the number of 46428fb27baSJustin T. Gibbs * devices and reallocate the device array. If the second sysctl 46528fb27baSJustin T. Gibbs * fails, we will return an error and therefore never get to this 46628fb27baSJustin T. Gibbs * point. If the device list has shrunk, the sysctl will not 46728fb27baSJustin T. Gibbs * return an error since we have more space allocated than is 46828fb27baSJustin T. Gibbs * necessary. So, in the shrinkage case, we catch it here and 46928fb27baSJustin T. Gibbs * reallocate the array so that we don't use any more space than is 47028fb27baSJustin T. Gibbs * necessary. 47128fb27baSJustin T. Gibbs */ 47228fb27baSJustin T. Gibbs if (oldgeneration != dinfo->generation) { 47316830b0cSPoul-Henning Kamp if (devstat_getnumdevs(kd) != dinfo->numdevs) { 47416830b0cSPoul-Henning Kamp if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0) 47528fb27baSJustin T. Gibbs return(-1); 47628fb27baSJustin T. Gibbs dssize = (dinfo->numdevs * sizeof(struct devstat)) + 477bcc6a3daSKenneth D. Merry sizeof(long); 47828fb27baSJustin T. Gibbs dinfo->mem_ptr = (u_int8_t *)realloc(dinfo->mem_ptr, 47928fb27baSJustin T. Gibbs dssize); 48028fb27baSJustin T. Gibbs } 48128fb27baSJustin T. Gibbs retval = 1; 48228fb27baSJustin T. Gibbs } 48328fb27baSJustin T. Gibbs 484bcc6a3daSKenneth D. Merry dinfo->devices = (struct devstat *)(dinfo->mem_ptr + sizeof(long)); 48528fb27baSJustin T. Gibbs 48628fb27baSJustin T. Gibbs return(retval); 48728fb27baSJustin T. Gibbs } 48828fb27baSJustin T. Gibbs 48928fb27baSJustin T. Gibbs /* 49028fb27baSJustin T. Gibbs * selectdevs(): 49128fb27baSJustin T. Gibbs * 49228fb27baSJustin T. Gibbs * Devices are selected/deselected based upon the following criteria: 49328fb27baSJustin T. Gibbs * - devices specified by the user on the command line 49428fb27baSJustin T. Gibbs * - devices matching any device type expressions given on the command line 49528fb27baSJustin T. Gibbs * - devices with the highest I/O, if 'top' mode is enabled 49628fb27baSJustin T. Gibbs * - the first n unselected devices in the device list, if maxshowdevs 49728fb27baSJustin T. Gibbs * devices haven't already been selected and if the user has not 49828fb27baSJustin T. Gibbs * specified any devices on the command line and if we're in "add" mode. 49928fb27baSJustin T. Gibbs * 50028fb27baSJustin T. Gibbs * Input parameters: 50128fb27baSJustin T. Gibbs * - device selection list (dev_select) 50228fb27baSJustin T. Gibbs * - current number of devices selected (num_selected) 50328fb27baSJustin T. Gibbs * - total number of devices in the selection list (num_selections) 50428fb27baSJustin T. Gibbs * - devstat generation as of the last time selectdevs() was called 50528fb27baSJustin T. Gibbs * (select_generation) 50628fb27baSJustin T. Gibbs * - current devstat generation (current_generation) 50728fb27baSJustin T. Gibbs * - current list of devices and statistics (devices) 50828fb27baSJustin T. Gibbs * - number of devices in the current device list (numdevs) 50928fb27baSJustin T. Gibbs * - compiled version of the command line device type arguments (matches) 51028fb27baSJustin T. Gibbs * - This is optional. If the number of devices is 0, this will be ignored. 51128fb27baSJustin T. Gibbs * - The matching code pays attention to the current selection mode. So 51228fb27baSJustin T. Gibbs * if you pass in a matching expression, it will be evaluated based 51328fb27baSJustin T. Gibbs * upon the selection mode that is passed in. See below for details. 51428fb27baSJustin T. Gibbs * - number of device type matching expressions (num_matches) 51528fb27baSJustin T. Gibbs * - Set to 0 to disable the matching code. 51628fb27baSJustin T. Gibbs * - list of devices specified on the command line by the user (dev_selections) 51728fb27baSJustin T. Gibbs * - number of devices selected on the command line by the user 51828fb27baSJustin T. Gibbs * (num_dev_selections) 51928fb27baSJustin T. Gibbs * - Our selection mode. There are four different selection modes: 52028fb27baSJustin T. Gibbs * - add mode. (DS_SELECT_ADD) Any devices matching devices explicitly 52128fb27baSJustin T. Gibbs * selected by the user or devices matching a pattern given by the 52228fb27baSJustin T. Gibbs * user will be selected in addition to devices that are already 52328fb27baSJustin T. Gibbs * selected. Additional devices will be selected, up to maxshowdevs 52428fb27baSJustin T. Gibbs * number of devices. 52528fb27baSJustin T. Gibbs * - only mode. (DS_SELECT_ONLY) Only devices matching devices 52628fb27baSJustin T. Gibbs * explicitly given by the user or devices matching a pattern 52728fb27baSJustin T. Gibbs * given by the user will be selected. No other devices will be 52828fb27baSJustin T. Gibbs * selected. 52928fb27baSJustin T. Gibbs * - addonly mode. (DS_SELECT_ADDONLY) This is similar to add and 53028fb27baSJustin T. Gibbs * only. Basically, this will not de-select any devices that are 53128fb27baSJustin T. Gibbs * current selected, as only mode would, but it will also not 53228fb27baSJustin T. Gibbs * gratuitously select up to maxshowdevs devices as add mode would. 53328fb27baSJustin T. Gibbs * - remove mode. (DS_SELECT_REMOVE) Any devices matching devices 53428fb27baSJustin T. Gibbs * explicitly selected by the user or devices matching a pattern 53528fb27baSJustin T. Gibbs * given by the user will be de-selected. 53628fb27baSJustin T. Gibbs * - maximum number of devices we can select (maxshowdevs) 53728fb27baSJustin T. Gibbs * - flag indicating whether or not we're in 'top' mode (perf_select) 53828fb27baSJustin T. Gibbs * 53928fb27baSJustin T. Gibbs * Output data: 54028fb27baSJustin T. Gibbs * - the device selection list may be modified and passed back out 54128fb27baSJustin T. Gibbs * - the number of devices selected and the total number of items in the 54228fb27baSJustin T. Gibbs * device selection list may be changed 54328fb27baSJustin T. Gibbs * - the selection generation may be changed to match the current generation 54428fb27baSJustin T. Gibbs * 54528fb27baSJustin T. Gibbs * Return values: 54628fb27baSJustin T. Gibbs * -1 -- error 54728fb27baSJustin T. Gibbs * 0 -- selected devices are unchanged 54828fb27baSJustin T. Gibbs * 1 -- selected devices changed 54928fb27baSJustin T. Gibbs */ 55028fb27baSJustin T. Gibbs int 551c4a5ef6eSThomas Moestl devstat_selectdevs(struct device_selection **dev_select, int *num_selected, 552bcc6a3daSKenneth D. Merry int *num_selections, long *select_generation, 553c4a5ef6eSThomas Moestl long current_generation, struct devstat *devices, 554c4a5ef6eSThomas Moestl int numdevs, struct devstat_match *matches, int num_matches, 55528fb27baSJustin T. Gibbs char **dev_selections, int num_dev_selections, 556c4a5ef6eSThomas Moestl devstat_select_mode select_mode, int maxshowdevs, 557c4a5ef6eSThomas Moestl int perf_select) 55828fb27baSJustin T. Gibbs { 559be04b6d1SDavid E. O'Brien int i, j, k; 56028fb27baSJustin T. Gibbs int init_selections = 0, init_selected_var = 0; 56128fb27baSJustin T. Gibbs struct device_selection *old_dev_select = NULL; 56228fb27baSJustin T. Gibbs int old_num_selections = 0, old_num_selected; 56328fb27baSJustin T. Gibbs int selection_number = 0; 56428fb27baSJustin T. Gibbs int changed = 0, found = 0; 56528fb27baSJustin T. Gibbs 566d2d0b144SPoul-Henning Kamp if ((dev_select == NULL) || (devices == NULL) || (numdevs < 0)) 56728fb27baSJustin T. Gibbs return(-1); 56828fb27baSJustin T. Gibbs 56928fb27baSJustin T. Gibbs /* 57028fb27baSJustin T. Gibbs * We always want to make sure that we have as many dev_select 57128fb27baSJustin T. Gibbs * entries as there are devices. 57228fb27baSJustin T. Gibbs */ 57328fb27baSJustin T. Gibbs /* 57428fb27baSJustin T. Gibbs * In this case, we haven't selected devices before. 57528fb27baSJustin T. Gibbs */ 57628fb27baSJustin T. Gibbs if (*dev_select == NULL) { 57728fb27baSJustin T. Gibbs *dev_select = (struct device_selection *)malloc(numdevs * 57828fb27baSJustin T. Gibbs sizeof(struct device_selection)); 57928fb27baSJustin T. Gibbs *select_generation = current_generation; 58028fb27baSJustin T. Gibbs init_selections = 1; 58128fb27baSJustin T. Gibbs changed = 1; 58228fb27baSJustin T. Gibbs /* 58328fb27baSJustin T. Gibbs * In this case, we have selected devices before, but the device 58428fb27baSJustin T. Gibbs * list has changed since we last selected devices, so we need to 58528fb27baSJustin T. Gibbs * either enlarge or reduce the size of the device selection list. 58684a03ac6SAndriy Gapon * But delay the resizing until after copying the data to old_dev_select 58784a03ac6SAndriy Gapon * as to not lose any data in the case of reducing the size. 58828fb27baSJustin T. Gibbs */ 58928fb27baSJustin T. Gibbs } else if (*num_selections != numdevs) { 59028fb27baSJustin T. Gibbs *select_generation = current_generation; 59128fb27baSJustin T. Gibbs init_selections = 1; 59228fb27baSJustin T. Gibbs /* 59328fb27baSJustin T. Gibbs * In this case, we've selected devices before, and the selection 59428fb27baSJustin T. Gibbs * list is the same size as it was the last time, but the device 59528fb27baSJustin T. Gibbs * list has changed. 59628fb27baSJustin T. Gibbs */ 59728fb27baSJustin T. Gibbs } else if (*select_generation < current_generation) { 59828fb27baSJustin T. Gibbs *select_generation = current_generation; 59928fb27baSJustin T. Gibbs init_selections = 1; 60028fb27baSJustin T. Gibbs } 60128fb27baSJustin T. Gibbs 602a18e4616SGuy Helmer if (*dev_select == NULL) { 603a18e4616SGuy Helmer snprintf(devstat_errbuf, sizeof(devstat_errbuf), 604a18e4616SGuy Helmer "%s: Cannot (re)allocate memory for dev_select argument", 605a18e4616SGuy Helmer __func__); 606a18e4616SGuy Helmer return(-1); 607a18e4616SGuy Helmer } 608a18e4616SGuy Helmer 60928fb27baSJustin T. Gibbs /* 61028fb27baSJustin T. Gibbs * If we're in "only" mode, we want to clear out the selected 61128fb27baSJustin T. Gibbs * variable since we're going to select exactly what the user wants 61228fb27baSJustin T. Gibbs * this time through. 61328fb27baSJustin T. Gibbs */ 61428fb27baSJustin T. Gibbs if (select_mode == DS_SELECT_ONLY) 61528fb27baSJustin T. Gibbs init_selected_var = 1; 61628fb27baSJustin T. Gibbs 61728fb27baSJustin T. Gibbs /* 61828fb27baSJustin T. Gibbs * In all cases, we want to back up the number of selected devices. 61928fb27baSJustin T. Gibbs * It is a quick and accurate way to determine whether the selected 62028fb27baSJustin T. Gibbs * devices have changed. 62128fb27baSJustin T. Gibbs */ 62228fb27baSJustin T. Gibbs old_num_selected = *num_selected; 62328fb27baSJustin T. Gibbs 62428fb27baSJustin T. Gibbs /* 62528fb27baSJustin T. Gibbs * We want to make a backup of the current selection list if 62628fb27baSJustin T. Gibbs * the list of devices has changed, or if we're in performance 62728fb27baSJustin T. Gibbs * selection mode. In both cases, we don't want to make a backup 62828fb27baSJustin T. Gibbs * if we already know for sure that the list will be different. 62928fb27baSJustin T. Gibbs * This is certainly the case if this is our first time through the 63028fb27baSJustin T. Gibbs * selection code. 63128fb27baSJustin T. Gibbs */ 63228fb27baSJustin T. Gibbs if (((init_selected_var != 0) || (init_selections != 0) 63328fb27baSJustin T. Gibbs || (perf_select != 0)) && (changed == 0)){ 63428fb27baSJustin T. Gibbs old_dev_select = (struct device_selection *)malloc( 63528fb27baSJustin T. Gibbs *num_selections * sizeof(struct device_selection)); 636a18e4616SGuy Helmer if (old_dev_select == NULL) { 637a18e4616SGuy Helmer snprintf(devstat_errbuf, sizeof(devstat_errbuf), 638a18e4616SGuy Helmer "%s: Cannot allocate memory for selection list backup", 639a18e4616SGuy Helmer __func__); 640a18e4616SGuy Helmer return(-1); 641a18e4616SGuy Helmer } 64228fb27baSJustin T. Gibbs old_num_selections = *num_selections; 64328fb27baSJustin T. Gibbs bcopy(*dev_select, old_dev_select, 64428fb27baSJustin T. Gibbs sizeof(struct device_selection) * *num_selections); 64528fb27baSJustin T. Gibbs } 64628fb27baSJustin T. Gibbs 64784a03ac6SAndriy Gapon if (!changed && *num_selections != numdevs) { 64884a03ac6SAndriy Gapon *dev_select = (struct device_selection *)reallocf(*dev_select, 64984a03ac6SAndriy Gapon numdevs * sizeof(struct device_selection)); 65084a03ac6SAndriy Gapon } 65184a03ac6SAndriy Gapon 65228fb27baSJustin T. Gibbs if (init_selections != 0) { 65328fb27baSJustin T. Gibbs bzero(*dev_select, sizeof(struct device_selection) * numdevs); 65428fb27baSJustin T. Gibbs 65528fb27baSJustin T. Gibbs for (i = 0; i < numdevs; i++) { 65628fb27baSJustin T. Gibbs (*dev_select)[i].device_number = 65728fb27baSJustin T. Gibbs devices[i].device_number; 65828fb27baSJustin T. Gibbs strncpy((*dev_select)[i].device_name, 65928fb27baSJustin T. Gibbs devices[i].device_name, 66028fb27baSJustin T. Gibbs DEVSTAT_NAME_LEN); 66117ee2b20SKenneth D. Merry (*dev_select)[i].device_name[DEVSTAT_NAME_LEN - 1]='\0'; 66228fb27baSJustin T. Gibbs (*dev_select)[i].unit_number = devices[i].unit_number; 66328fb27baSJustin T. Gibbs (*dev_select)[i].position = i; 66428fb27baSJustin T. Gibbs } 66528fb27baSJustin T. Gibbs *num_selections = numdevs; 66628fb27baSJustin T. Gibbs } else if (init_selected_var != 0) { 66728fb27baSJustin T. Gibbs for (i = 0; i < numdevs; i++) 66828fb27baSJustin T. Gibbs (*dev_select)[i].selected = 0; 66928fb27baSJustin T. Gibbs } 67028fb27baSJustin T. Gibbs 67128fb27baSJustin T. Gibbs /* we haven't gotten around to selecting anything yet.. */ 67228fb27baSJustin T. Gibbs if ((select_mode == DS_SELECT_ONLY) || (init_selections != 0) 67328fb27baSJustin T. Gibbs || (init_selected_var != 0)) 67428fb27baSJustin T. Gibbs *num_selected = 0; 67528fb27baSJustin T. Gibbs 67628fb27baSJustin T. Gibbs /* 67728fb27baSJustin T. Gibbs * Look through any devices the user specified on the command line 67828fb27baSJustin T. Gibbs * and see if they match known devices. If so, select them. 67928fb27baSJustin T. Gibbs */ 68028fb27baSJustin T. Gibbs for (i = 0; (i < *num_selections) && (num_dev_selections > 0); i++) { 68128fb27baSJustin T. Gibbs char tmpstr[80]; 68228fb27baSJustin T. Gibbs 68317ee2b20SKenneth D. Merry snprintf(tmpstr, sizeof(tmpstr), "%s%d", 68417ee2b20SKenneth D. Merry (*dev_select)[i].device_name, 68528fb27baSJustin T. Gibbs (*dev_select)[i].unit_number); 68628fb27baSJustin T. Gibbs for (j = 0; j < num_dev_selections; j++) { 68728fb27baSJustin T. Gibbs if (strcmp(tmpstr, dev_selections[j]) == 0) { 68828fb27baSJustin T. Gibbs /* 68928fb27baSJustin T. Gibbs * Here we do different things based on the 69028fb27baSJustin T. Gibbs * mode we're in. If we're in add or 69128fb27baSJustin T. Gibbs * addonly mode, we only select this device 69228fb27baSJustin T. Gibbs * if it hasn't already been selected. 69328fb27baSJustin T. Gibbs * Otherwise, we would be unnecessarily 69428fb27baSJustin T. Gibbs * changing the selection order and 69528fb27baSJustin T. Gibbs * incrementing the selection count. If 69628fb27baSJustin T. Gibbs * we're in only mode, we unconditionally 69728fb27baSJustin T. Gibbs * select this device, since in only mode 69828fb27baSJustin T. Gibbs * any previous selections are erased and 69928fb27baSJustin T. Gibbs * manually specified devices are the first 70028fb27baSJustin T. Gibbs * ones to be selected. If we're in remove 70128fb27baSJustin T. Gibbs * mode, we de-select the specified device and 70228fb27baSJustin T. Gibbs * decrement the selection count. 70328fb27baSJustin T. Gibbs */ 70428fb27baSJustin T. Gibbs switch(select_mode) { 70528fb27baSJustin T. Gibbs case DS_SELECT_ADD: 70628fb27baSJustin T. Gibbs case DS_SELECT_ADDONLY: 70728fb27baSJustin T. Gibbs if ((*dev_select)[i].selected) 70828fb27baSJustin T. Gibbs break; 70928fb27baSJustin T. Gibbs /* FALLTHROUGH */ 71028fb27baSJustin T. Gibbs case DS_SELECT_ONLY: 71128fb27baSJustin T. Gibbs (*dev_select)[i].selected = 71228fb27baSJustin T. Gibbs ++selection_number; 71328fb27baSJustin T. Gibbs (*num_selected)++; 71428fb27baSJustin T. Gibbs break; 71528fb27baSJustin T. Gibbs case DS_SELECT_REMOVE: 71628fb27baSJustin T. Gibbs (*dev_select)[i].selected = 0; 71728fb27baSJustin T. Gibbs (*num_selected)--; 71828fb27baSJustin T. Gibbs /* 71928fb27baSJustin T. Gibbs * This isn't passed back out, we 72028fb27baSJustin T. Gibbs * just use it to keep track of 72128fb27baSJustin T. Gibbs * how many devices we've removed. 72228fb27baSJustin T. Gibbs */ 72328fb27baSJustin T. Gibbs num_dev_selections--; 72428fb27baSJustin T. Gibbs break; 72528fb27baSJustin T. Gibbs } 72628fb27baSJustin T. Gibbs break; 72728fb27baSJustin T. Gibbs } 72828fb27baSJustin T. Gibbs } 72928fb27baSJustin T. Gibbs } 73028fb27baSJustin T. Gibbs 73128fb27baSJustin T. Gibbs /* 73228fb27baSJustin T. Gibbs * Go through the user's device type expressions and select devices 73328fb27baSJustin T. Gibbs * accordingly. We only do this if the number of devices already 73428fb27baSJustin T. Gibbs * selected is less than the maximum number we can show. 73528fb27baSJustin T. Gibbs */ 73628fb27baSJustin T. Gibbs for (i = 0; (i < num_matches) && (*num_selected < maxshowdevs); i++) { 73728fb27baSJustin T. Gibbs /* We should probably indicate some error here */ 73828fb27baSJustin T. Gibbs if ((matches[i].match_fields == DEVSTAT_MATCH_NONE) 73928fb27baSJustin T. Gibbs || (matches[i].num_match_categories <= 0)) 74028fb27baSJustin T. Gibbs continue; 74128fb27baSJustin T. Gibbs 74228fb27baSJustin T. Gibbs for (j = 0; j < numdevs; j++) { 74328fb27baSJustin T. Gibbs int num_match_categories; 74428fb27baSJustin T. Gibbs 74528fb27baSJustin T. Gibbs num_match_categories = matches[i].num_match_categories; 74628fb27baSJustin T. Gibbs 74728fb27baSJustin T. Gibbs /* 74828fb27baSJustin T. Gibbs * Determine whether or not the current device 74928fb27baSJustin T. Gibbs * matches the given matching expression. This if 75028fb27baSJustin T. Gibbs * statement consists of three components: 75128fb27baSJustin T. Gibbs * - the device type check 75228fb27baSJustin T. Gibbs * - the device interface check 75328fb27baSJustin T. Gibbs * - the passthrough check 75428fb27baSJustin T. Gibbs * If a the matching test is successful, it 75528fb27baSJustin T. Gibbs * decrements the number of matching categories, 75628fb27baSJustin T. Gibbs * and if we've reached the last element that 75728fb27baSJustin T. Gibbs * needed to be matched, the if statement succeeds. 75828fb27baSJustin T. Gibbs * 75928fb27baSJustin T. Gibbs */ 76028fb27baSJustin T. Gibbs if ((((matches[i].match_fields & DEVSTAT_MATCH_TYPE)!=0) 76128fb27baSJustin T. Gibbs && ((devices[j].device_type & DEVSTAT_TYPE_MASK) == 76228fb27baSJustin T. Gibbs (matches[i].device_type & DEVSTAT_TYPE_MASK)) 76328fb27baSJustin T. Gibbs &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0) 76428fb27baSJustin T. Gibbs || (((matches[i].match_fields & 76528fb27baSJustin T. Gibbs DEVSTAT_MATCH_PASS) == 0) 76628fb27baSJustin T. Gibbs && ((devices[j].device_type & 76728fb27baSJustin T. Gibbs DEVSTAT_TYPE_PASS) == 0))) 76828fb27baSJustin T. Gibbs && (--num_match_categories == 0)) 76928fb27baSJustin T. Gibbs || (((matches[i].match_fields & DEVSTAT_MATCH_IF) != 0) 77028fb27baSJustin T. Gibbs && ((devices[j].device_type & DEVSTAT_TYPE_IF_MASK) == 77128fb27baSJustin T. Gibbs (matches[i].device_type & DEVSTAT_TYPE_IF_MASK)) 77228fb27baSJustin T. Gibbs &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0) 77328fb27baSJustin T. Gibbs || (((matches[i].match_fields & 77428fb27baSJustin T. Gibbs DEVSTAT_MATCH_PASS) == 0) 77528fb27baSJustin T. Gibbs && ((devices[j].device_type & 77628fb27baSJustin T. Gibbs DEVSTAT_TYPE_PASS) == 0))) 77728fb27baSJustin T. Gibbs && (--num_match_categories == 0)) 77828fb27baSJustin T. Gibbs || (((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0) 77928fb27baSJustin T. Gibbs && ((devices[j].device_type & DEVSTAT_TYPE_PASS) != 0) 78028fb27baSJustin T. Gibbs && (--num_match_categories == 0))) { 78128fb27baSJustin T. Gibbs 78228fb27baSJustin T. Gibbs /* 78328fb27baSJustin T. Gibbs * This is probably a non-optimal solution 78428fb27baSJustin T. Gibbs * to the problem that the devices in the 78528fb27baSJustin T. Gibbs * device list will not be in the same 78628fb27baSJustin T. Gibbs * order as the devices in the selection 78728fb27baSJustin T. Gibbs * array. 78828fb27baSJustin T. Gibbs */ 78928fb27baSJustin T. Gibbs for (k = 0; k < numdevs; k++) { 79028fb27baSJustin T. Gibbs if ((*dev_select)[k].position == j) { 79128fb27baSJustin T. Gibbs found = 1; 79228fb27baSJustin T. Gibbs break; 79328fb27baSJustin T. Gibbs } 79428fb27baSJustin T. Gibbs } 79528fb27baSJustin T. Gibbs 79628fb27baSJustin T. Gibbs /* 79728fb27baSJustin T. Gibbs * There shouldn't be a case where a device 79828fb27baSJustin T. Gibbs * in the device list is not in the 79928fb27baSJustin T. Gibbs * selection list...but it could happen. 80028fb27baSJustin T. Gibbs */ 80128fb27baSJustin T. Gibbs if (found != 1) { 80228fb27baSJustin T. Gibbs fprintf(stderr, "selectdevs: couldn't" 80328fb27baSJustin T. Gibbs " find %s%d in selection " 80428fb27baSJustin T. Gibbs "list\n", 80528fb27baSJustin T. Gibbs devices[j].device_name, 80628fb27baSJustin T. Gibbs devices[j].unit_number); 80728fb27baSJustin T. Gibbs break; 80828fb27baSJustin T. Gibbs } 80928fb27baSJustin T. Gibbs 81028fb27baSJustin T. Gibbs /* 81128fb27baSJustin T. Gibbs * We do different things based upon the 81228fb27baSJustin T. Gibbs * mode we're in. If we're in add or only 81328fb27baSJustin T. Gibbs * mode, we go ahead and select this device 81428fb27baSJustin T. Gibbs * if it hasn't already been selected. If 81528fb27baSJustin T. Gibbs * it has already been selected, we leave 81628fb27baSJustin T. Gibbs * it alone so we don't mess up the 81728fb27baSJustin T. Gibbs * selection ordering. Manually specified 81828fb27baSJustin T. Gibbs * devices have already been selected, and 81928fb27baSJustin T. Gibbs * they have higher priority than pattern 82028fb27baSJustin T. Gibbs * matched devices. If we're in remove 82128fb27baSJustin T. Gibbs * mode, we de-select the given device and 82228fb27baSJustin T. Gibbs * decrement the selected count. 82328fb27baSJustin T. Gibbs */ 82428fb27baSJustin T. Gibbs switch(select_mode) { 82528fb27baSJustin T. Gibbs case DS_SELECT_ADD: 82628fb27baSJustin T. Gibbs case DS_SELECT_ADDONLY: 82728fb27baSJustin T. Gibbs case DS_SELECT_ONLY: 82828fb27baSJustin T. Gibbs if ((*dev_select)[k].selected != 0) 82928fb27baSJustin T. Gibbs break; 83028fb27baSJustin T. Gibbs (*dev_select)[k].selected = 83128fb27baSJustin T. Gibbs ++selection_number; 83228fb27baSJustin T. Gibbs (*num_selected)++; 83328fb27baSJustin T. Gibbs break; 83428fb27baSJustin T. Gibbs case DS_SELECT_REMOVE: 83528fb27baSJustin T. Gibbs (*dev_select)[k].selected = 0; 83628fb27baSJustin T. Gibbs (*num_selected)--; 83728fb27baSJustin T. Gibbs break; 83828fb27baSJustin T. Gibbs } 83928fb27baSJustin T. Gibbs } 84028fb27baSJustin T. Gibbs } 84128fb27baSJustin T. Gibbs } 84228fb27baSJustin T. Gibbs 84328fb27baSJustin T. Gibbs /* 84428fb27baSJustin T. Gibbs * Here we implement "top" mode. Devices are sorted in the 84528fb27baSJustin T. Gibbs * selection array based on two criteria: whether or not they are 84628fb27baSJustin T. Gibbs * selected (not selection number, just the fact that they are 84728fb27baSJustin T. Gibbs * selected!) and the number of bytes in the "bytes" field of the 84828fb27baSJustin T. Gibbs * selection structure. The bytes field generally must be kept up 84928fb27baSJustin T. Gibbs * by the user. In the future, it may be maintained by library 85028fb27baSJustin T. Gibbs * functions, but for now the user has to do the work. 85128fb27baSJustin T. Gibbs * 85228fb27baSJustin T. Gibbs * At first glance, it may seem wrong that we don't go through and 85328fb27baSJustin T. Gibbs * select every device in the case where the user hasn't specified 85428fb27baSJustin T. Gibbs * any devices or patterns. In fact, though, it won't make any 85528fb27baSJustin T. Gibbs * difference in the device sorting. In that particular case (i.e. 85628fb27baSJustin T. Gibbs * when we're in "add" or "only" mode, and the user hasn't 85728fb27baSJustin T. Gibbs * specified anything) the first time through no devices will be 85828fb27baSJustin T. Gibbs * selected, so the only criterion used to sort them will be their 85928fb27baSJustin T. Gibbs * performance. The second time through, and every time thereafter, 86028fb27baSJustin T. Gibbs * all devices will be selected, so again selection won't matter. 86128fb27baSJustin T. Gibbs */ 86228fb27baSJustin T. Gibbs if (perf_select != 0) { 86328fb27baSJustin T. Gibbs 86428fb27baSJustin T. Gibbs /* Sort the device array by throughput */ 86528fb27baSJustin T. Gibbs qsort(*dev_select, *num_selections, 86628fb27baSJustin T. Gibbs sizeof(struct device_selection), 86728fb27baSJustin T. Gibbs compare_select); 86828fb27baSJustin T. Gibbs 86928fb27baSJustin T. Gibbs if (*num_selected == 0) { 87028fb27baSJustin T. Gibbs /* 87128fb27baSJustin T. Gibbs * Here we select every device in the array, if it 87228fb27baSJustin T. Gibbs * isn't already selected. Because the 'selected' 87328fb27baSJustin T. Gibbs * variable in the selection array entries contains 87428fb27baSJustin T. Gibbs * the selection order, the devstats routine can show 87528fb27baSJustin T. Gibbs * the devices that were selected first. 87628fb27baSJustin T. Gibbs */ 87728fb27baSJustin T. Gibbs for (i = 0; i < *num_selections; i++) { 87828fb27baSJustin T. Gibbs if ((*dev_select)[i].selected == 0) { 87928fb27baSJustin T. Gibbs (*dev_select)[i].selected = 88028fb27baSJustin T. Gibbs ++selection_number; 88128fb27baSJustin T. Gibbs (*num_selected)++; 88228fb27baSJustin T. Gibbs } 88328fb27baSJustin T. Gibbs } 88428fb27baSJustin T. Gibbs } else { 88528fb27baSJustin T. Gibbs selection_number = 0; 88628fb27baSJustin T. Gibbs for (i = 0; i < *num_selections; i++) { 88728fb27baSJustin T. Gibbs if ((*dev_select)[i].selected != 0) { 88828fb27baSJustin T. Gibbs (*dev_select)[i].selected = 88928fb27baSJustin T. Gibbs ++selection_number; 89028fb27baSJustin T. Gibbs } 89128fb27baSJustin T. Gibbs } 89228fb27baSJustin T. Gibbs } 89328fb27baSJustin T. Gibbs } 89428fb27baSJustin T. Gibbs 89528fb27baSJustin T. Gibbs /* 89628fb27baSJustin T. Gibbs * If we're in the "add" selection mode and if we haven't already 89728fb27baSJustin T. Gibbs * selected maxshowdevs number of devices, go through the array and 89828fb27baSJustin T. Gibbs * select any unselected devices. If we're in "only" mode, we 89928fb27baSJustin T. Gibbs * obviously don't want to select anything other than what the user 90028fb27baSJustin T. Gibbs * specifies. If we're in "remove" mode, it probably isn't a good 90128fb27baSJustin T. Gibbs * idea to go through and select any more devices, since we might 90228fb27baSJustin T. Gibbs * end up selecting something that the user wants removed. Through 90328fb27baSJustin T. Gibbs * more complicated logic, we could actually figure this out, but 90428fb27baSJustin T. Gibbs * that would probably require combining this loop with the various 90528fb27baSJustin T. Gibbs * selections loops above. 90628fb27baSJustin T. Gibbs */ 90728fb27baSJustin T. Gibbs if ((select_mode == DS_SELECT_ADD) && (*num_selected < maxshowdevs)) { 90828fb27baSJustin T. Gibbs for (i = 0; i < *num_selections; i++) 90928fb27baSJustin T. Gibbs if ((*dev_select)[i].selected == 0) { 91028fb27baSJustin T. Gibbs (*dev_select)[i].selected = ++selection_number; 91128fb27baSJustin T. Gibbs (*num_selected)++; 91228fb27baSJustin T. Gibbs } 91328fb27baSJustin T. Gibbs } 91428fb27baSJustin T. Gibbs 91528fb27baSJustin T. Gibbs /* 91628fb27baSJustin T. Gibbs * Look at the number of devices that have been selected. If it 91728fb27baSJustin T. Gibbs * has changed, set the changed variable. Otherwise, if we've 91828fb27baSJustin T. Gibbs * made a backup of the selection list, compare it to the current 91928fb27baSJustin T. Gibbs * selection list to see if the selected devices have changed. 92028fb27baSJustin T. Gibbs */ 92128fb27baSJustin T. Gibbs if ((changed == 0) && (old_num_selected != *num_selected)) 92228fb27baSJustin T. Gibbs changed = 1; 92328fb27baSJustin T. Gibbs else if ((changed == 0) && (old_dev_select != NULL)) { 92428fb27baSJustin T. Gibbs /* 92528fb27baSJustin T. Gibbs * Now we go through the selection list and we look at 92628fb27baSJustin T. Gibbs * it three different ways. 92728fb27baSJustin T. Gibbs */ 92828fb27baSJustin T. Gibbs for (i = 0; (i < *num_selections) && (changed == 0) && 92928fb27baSJustin T. Gibbs (i < old_num_selections); i++) { 93028fb27baSJustin T. Gibbs /* 93128fb27baSJustin T. Gibbs * If the device at index i in both the new and old 93228fb27baSJustin T. Gibbs * selection arrays has the same device number and 93328fb27baSJustin T. Gibbs * selection status, it hasn't changed. We 93428fb27baSJustin T. Gibbs * continue on to the next index. 93528fb27baSJustin T. Gibbs */ 93628fb27baSJustin T. Gibbs if (((*dev_select)[i].device_number == 93728fb27baSJustin T. Gibbs old_dev_select[i].device_number) 93828fb27baSJustin T. Gibbs && ((*dev_select)[i].selected == 93928fb27baSJustin T. Gibbs old_dev_select[i].selected)) 94028fb27baSJustin T. Gibbs continue; 94128fb27baSJustin T. Gibbs 94228fb27baSJustin T. Gibbs /* 94328fb27baSJustin T. Gibbs * Now, if we're still going through the if 94428fb27baSJustin T. Gibbs * statement, the above test wasn't true. So we 94528fb27baSJustin T. Gibbs * check here to see if the device at index i in 94628fb27baSJustin T. Gibbs * the current array is the same as the device at 94728fb27baSJustin T. Gibbs * index i in the old array. If it is, that means 94828fb27baSJustin T. Gibbs * that its selection number has changed. Set 94928fb27baSJustin T. Gibbs * changed to 1 and exit the loop. 95028fb27baSJustin T. Gibbs */ 95128fb27baSJustin T. Gibbs else if ((*dev_select)[i].device_number == 95228fb27baSJustin T. Gibbs old_dev_select[i].device_number) { 95328fb27baSJustin T. Gibbs changed = 1; 95428fb27baSJustin T. Gibbs break; 95528fb27baSJustin T. Gibbs } 95628fb27baSJustin T. Gibbs /* 95728fb27baSJustin T. Gibbs * If we get here, then the device at index i in 95828fb27baSJustin T. Gibbs * the current array isn't the same device as the 95928fb27baSJustin T. Gibbs * device at index i in the old array. 96028fb27baSJustin T. Gibbs */ 96128fb27baSJustin T. Gibbs else { 962c3508206SKenneth D. Merry found = 0; 96328fb27baSJustin T. Gibbs 96428fb27baSJustin T. Gibbs /* 96528fb27baSJustin T. Gibbs * Search through the old selection array 96628fb27baSJustin T. Gibbs * looking for a device with the same 96728fb27baSJustin T. Gibbs * device number as the device at index i 96828fb27baSJustin T. Gibbs * in the current array. If the selection 96928fb27baSJustin T. Gibbs * status is the same, then we mark it as 97028fb27baSJustin T. Gibbs * found. If the selection status isn't 97128fb27baSJustin T. Gibbs * the same, we break out of the loop. 97228fb27baSJustin T. Gibbs * Since found isn't set, changed will be 97328fb27baSJustin T. Gibbs * set to 1 below. 97428fb27baSJustin T. Gibbs */ 97528fb27baSJustin T. Gibbs for (j = 0; j < old_num_selections; j++) { 97628fb27baSJustin T. Gibbs if (((*dev_select)[i].device_number == 97728fb27baSJustin T. Gibbs old_dev_select[j].device_number) 97828fb27baSJustin T. Gibbs && ((*dev_select)[i].selected == 97928fb27baSJustin T. Gibbs old_dev_select[j].selected)){ 98028fb27baSJustin T. Gibbs found = 1; 98128fb27baSJustin T. Gibbs break; 98228fb27baSJustin T. Gibbs } 98328fb27baSJustin T. Gibbs else if ((*dev_select)[i].device_number 98428fb27baSJustin T. Gibbs == old_dev_select[j].device_number) 98528fb27baSJustin T. Gibbs break; 98628fb27baSJustin T. Gibbs } 98728fb27baSJustin T. Gibbs if (found == 0) 98828fb27baSJustin T. Gibbs changed = 1; 98928fb27baSJustin T. Gibbs } 99028fb27baSJustin T. Gibbs } 99128fb27baSJustin T. Gibbs } 99228fb27baSJustin T. Gibbs if (old_dev_select != NULL) 99328fb27baSJustin T. Gibbs free(old_dev_select); 99428fb27baSJustin T. Gibbs 99528fb27baSJustin T. Gibbs return(changed); 99628fb27baSJustin T. Gibbs } 99728fb27baSJustin T. Gibbs 99828fb27baSJustin T. Gibbs /* 99928fb27baSJustin T. Gibbs * Comparison routine for qsort() above. Note that the comparison here is 100028fb27baSJustin T. Gibbs * backwards -- generally, it should return a value to indicate whether 100128fb27baSJustin T. Gibbs * arg1 is <, =, or > arg2. Instead, it returns the opposite. The reason 100228fb27baSJustin T. Gibbs * it returns the opposite is so that the selection array will be sorted in 100328fb27baSJustin T. Gibbs * order of decreasing performance. We sort on two parameters. The first 100428fb27baSJustin T. Gibbs * sort key is whether or not one or the other of the devices in question 100528fb27baSJustin T. Gibbs * has been selected. If one of them has, and the other one has not, the 100628fb27baSJustin T. Gibbs * selected device is automatically more important than the unselected 100728fb27baSJustin T. Gibbs * device. If neither device is selected, we judge the devices based upon 100828fb27baSJustin T. Gibbs * performance. 100928fb27baSJustin T. Gibbs */ 101028fb27baSJustin T. Gibbs static int 101128fb27baSJustin T. Gibbs compare_select(const void *arg1, const void *arg2) 101228fb27baSJustin T. Gibbs { 1013c3508206SKenneth D. Merry if ((((const struct device_selection *)arg1)->selected) 1014c3508206SKenneth D. Merry && (((const struct device_selection *)arg2)->selected == 0)) 101528fb27baSJustin T. Gibbs return(-1); 1016c3508206SKenneth D. Merry else if ((((const struct device_selection *)arg1)->selected == 0) 1017c3508206SKenneth D. Merry && (((const struct device_selection *)arg2)->selected)) 101828fb27baSJustin T. Gibbs return(1); 1019c3508206SKenneth D. Merry else if (((const struct device_selection *)arg2)->bytes < 1020c3508206SKenneth D. Merry ((const struct device_selection *)arg1)->bytes) 102128fb27baSJustin T. Gibbs return(-1); 1022c3508206SKenneth D. Merry else if (((const struct device_selection *)arg2)->bytes > 1023c3508206SKenneth D. Merry ((const struct device_selection *)arg1)->bytes) 102428fb27baSJustin T. Gibbs return(1); 102528fb27baSJustin T. Gibbs else 102628fb27baSJustin T. Gibbs return(0); 102728fb27baSJustin T. Gibbs } 102828fb27baSJustin T. Gibbs 102928fb27baSJustin T. Gibbs /* 103028fb27baSJustin T. Gibbs * Take a string with the general format "arg1,arg2,arg3", and build a 103128fb27baSJustin T. Gibbs * device matching expression from it. 103228fb27baSJustin T. Gibbs */ 103328fb27baSJustin T. Gibbs int 1034c4a5ef6eSThomas Moestl devstat_buildmatch(char *match_str, struct devstat_match **matches, 1035c4a5ef6eSThomas Moestl int *num_matches) 103628fb27baSJustin T. Gibbs { 103728fb27baSJustin T. Gibbs char *tstr[5]; 103828fb27baSJustin T. Gibbs char **tempstr; 103928fb27baSJustin T. Gibbs int num_args; 1040be04b6d1SDavid E. O'Brien int i, j; 104128fb27baSJustin T. Gibbs 104228fb27baSJustin T. Gibbs /* We can't do much without a string to parse */ 104328fb27baSJustin T. Gibbs if (match_str == NULL) { 1044c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 10457d72cda3SMaxime Henrion "%s: no match expression", __func__); 104628fb27baSJustin T. Gibbs return(-1); 104728fb27baSJustin T. Gibbs } 104828fb27baSJustin T. Gibbs 104928fb27baSJustin T. Gibbs /* 105028fb27baSJustin T. Gibbs * Break the (comma delimited) input string out into separate strings. 105128fb27baSJustin T. Gibbs */ 105228fb27baSJustin T. Gibbs for (tempstr = tstr, num_args = 0; 10531046090fSSergey Kandaurov (*tempstr = strsep(&match_str, ",")) != NULL && (num_args < 5);) 10541046090fSSergey Kandaurov if (**tempstr != '\0') { 10551046090fSSergey Kandaurov num_args++; 105628fb27baSJustin T. Gibbs if (++tempstr >= &tstr[5]) 105728fb27baSJustin T. Gibbs break; 10581046090fSSergey Kandaurov } 105928fb27baSJustin T. Gibbs 106028fb27baSJustin T. Gibbs /* The user gave us too many type arguments */ 106128fb27baSJustin T. Gibbs if (num_args > 3) { 1062c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 10637d72cda3SMaxime Henrion "%s: too many type arguments", __func__); 106428fb27baSJustin T. Gibbs return(-1); 106528fb27baSJustin T. Gibbs } 106628fb27baSJustin T. Gibbs 106728fb27baSJustin T. Gibbs if (*num_matches == 0) 1068a18e4616SGuy Helmer *matches = NULL; 1069a18e4616SGuy Helmer 1070a18e4616SGuy Helmer *matches = (struct devstat_match *)reallocf(*matches, 107128fb27baSJustin T. Gibbs sizeof(struct devstat_match) * (*num_matches + 1)); 107228fb27baSJustin T. Gibbs 1073a18e4616SGuy Helmer if (*matches == NULL) { 1074a18e4616SGuy Helmer snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1075a18e4616SGuy Helmer "%s: Cannot allocate memory for matches list", __func__); 1076a18e4616SGuy Helmer return(-1); 1077a18e4616SGuy Helmer } 1078a18e4616SGuy Helmer 107928fb27baSJustin T. Gibbs /* Make sure the current entry is clear */ 108028fb27baSJustin T. Gibbs bzero(&matches[0][*num_matches], sizeof(struct devstat_match)); 108128fb27baSJustin T. Gibbs 108228fb27baSJustin T. Gibbs /* 108328fb27baSJustin T. Gibbs * Step through the arguments the user gave us and build a device 108428fb27baSJustin T. Gibbs * matching expression from them. 108528fb27baSJustin T. Gibbs */ 108628fb27baSJustin T. Gibbs for (i = 0; i < num_args; i++) { 108728fb27baSJustin T. Gibbs char *tempstr2, *tempstr3; 108828fb27baSJustin T. Gibbs 108928fb27baSJustin T. Gibbs /* 109028fb27baSJustin T. Gibbs * Get rid of leading white space. 109128fb27baSJustin T. Gibbs */ 109228fb27baSJustin T. Gibbs tempstr2 = tstr[i]; 109328fb27baSJustin T. Gibbs while (isspace(*tempstr2) && (*tempstr2 != '\0')) 109428fb27baSJustin T. Gibbs tempstr2++; 109528fb27baSJustin T. Gibbs 109628fb27baSJustin T. Gibbs /* 109728fb27baSJustin T. Gibbs * Get rid of trailing white space. 109828fb27baSJustin T. Gibbs */ 109928fb27baSJustin T. Gibbs tempstr3 = &tempstr2[strlen(tempstr2) - 1]; 110028fb27baSJustin T. Gibbs 110128fb27baSJustin T. Gibbs while ((*tempstr3 != '\0') && (tempstr3 > tempstr2) 110228fb27baSJustin T. Gibbs && (isspace(*tempstr3))) { 110328fb27baSJustin T. Gibbs *tempstr3 = '\0'; 110428fb27baSJustin T. Gibbs tempstr3--; 110528fb27baSJustin T. Gibbs } 110628fb27baSJustin T. Gibbs 110728fb27baSJustin T. Gibbs /* 110828fb27baSJustin T. Gibbs * Go through the match table comparing the user's 110928fb27baSJustin T. Gibbs * arguments to known device types, interfaces, etc. 111028fb27baSJustin T. Gibbs */ 111128fb27baSJustin T. Gibbs for (j = 0; match_table[j].match_str != NULL; j++) { 111228fb27baSJustin T. Gibbs /* 111328fb27baSJustin T. Gibbs * We do case-insensitive matching, in case someone 111428fb27baSJustin T. Gibbs * wants to enter "SCSI" instead of "scsi" or 111528fb27baSJustin T. Gibbs * something like that. Only compare as many 111628fb27baSJustin T. Gibbs * characters as are in the string in the match 111728fb27baSJustin T. Gibbs * table. This should help if someone tries to use 111828fb27baSJustin T. Gibbs * a super-long match expression. 111928fb27baSJustin T. Gibbs */ 112028fb27baSJustin T. Gibbs if (strncasecmp(tempstr2, match_table[j].match_str, 112128fb27baSJustin T. Gibbs strlen(match_table[j].match_str)) == 0) { 112228fb27baSJustin T. Gibbs /* 112328fb27baSJustin T. Gibbs * Make sure the user hasn't specified two 112428fb27baSJustin T. Gibbs * items of the same type, like "da" and 112528fb27baSJustin T. Gibbs * "cd". One device cannot be both. 112628fb27baSJustin T. Gibbs */ 112728fb27baSJustin T. Gibbs if (((*matches)[*num_matches].match_fields & 112828fb27baSJustin T. Gibbs match_table[j].match_field) != 0) { 1129c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, 1130c4a5ef6eSThomas Moestl sizeof(devstat_errbuf), 113128fb27baSJustin T. Gibbs "%s: cannot have more than " 113228fb27baSJustin T. Gibbs "one match item in a single " 11337d72cda3SMaxime Henrion "category", __func__); 113428fb27baSJustin T. Gibbs return(-1); 113528fb27baSJustin T. Gibbs } 113628fb27baSJustin T. Gibbs /* 113728fb27baSJustin T. Gibbs * If we've gotten this far, we have a 113828fb27baSJustin T. Gibbs * winner. Set the appropriate fields in 113928fb27baSJustin T. Gibbs * the match entry. 114028fb27baSJustin T. Gibbs */ 114128fb27baSJustin T. Gibbs (*matches)[*num_matches].match_fields |= 114228fb27baSJustin T. Gibbs match_table[j].match_field; 114328fb27baSJustin T. Gibbs (*matches)[*num_matches].device_type |= 114428fb27baSJustin T. Gibbs match_table[j].type; 114528fb27baSJustin T. Gibbs (*matches)[*num_matches].num_match_categories++; 114628fb27baSJustin T. Gibbs break; 114728fb27baSJustin T. Gibbs } 114828fb27baSJustin T. Gibbs } 114928fb27baSJustin T. Gibbs /* 115028fb27baSJustin T. Gibbs * We should have found a match in the above for loop. If 115128fb27baSJustin T. Gibbs * not, that means the user entered an invalid device type 115228fb27baSJustin T. Gibbs * or interface. 115328fb27baSJustin T. Gibbs */ 115428fb27baSJustin T. Gibbs if ((*matches)[*num_matches].num_match_categories != (i + 1)) { 115517ee2b20SKenneth D. Merry snprintf(devstat_errbuf, sizeof(devstat_errbuf), 11567d72cda3SMaxime Henrion "%s: unknown match item \"%s\"", __func__, 115728fb27baSJustin T. Gibbs tstr[i]); 115828fb27baSJustin T. Gibbs return(-1); 115928fb27baSJustin T. Gibbs } 116028fb27baSJustin T. Gibbs } 116128fb27baSJustin T. Gibbs 116228fb27baSJustin T. Gibbs (*num_matches)++; 116328fb27baSJustin T. Gibbs 116428fb27baSJustin T. Gibbs return(0); 116528fb27baSJustin T. Gibbs } 116628fb27baSJustin T. Gibbs 116728fb27baSJustin T. Gibbs /* 116828fb27baSJustin T. Gibbs * Compute a number of device statistics. Only one field is mandatory, and 116928fb27baSJustin T. Gibbs * that is "current". Everything else is optional. The caller passes in 117028fb27baSJustin T. Gibbs * pointers to variables to hold the various statistics he desires. If he 117128fb27baSJustin T. Gibbs * doesn't want a particular staistic, he should pass in a NULL pointer. 117228fb27baSJustin T. Gibbs * Return values: 117328fb27baSJustin T. Gibbs * 0 -- success 117428fb27baSJustin T. Gibbs * -1 -- failure 117528fb27baSJustin T. Gibbs */ 117628fb27baSJustin T. Gibbs int 117728fb27baSJustin T. Gibbs compute_stats(struct devstat *current, struct devstat *previous, 117828fb27baSJustin T. Gibbs long double etime, u_int64_t *total_bytes, 117928fb27baSJustin T. Gibbs u_int64_t *total_transfers, u_int64_t *total_blocks, 118028fb27baSJustin T. Gibbs long double *kb_per_transfer, long double *transfers_per_second, 118128fb27baSJustin T. Gibbs long double *mb_per_second, long double *blocks_per_second, 118228fb27baSJustin T. Gibbs long double *ms_per_transaction) 118328fb27baSJustin T. Gibbs { 1184884539f7SKenneth D. Merry return(devstat_compute_statistics(current, previous, etime, 1185884539f7SKenneth D. Merry total_bytes ? DSM_TOTAL_BYTES : DSM_SKIP, 1186884539f7SKenneth D. Merry total_bytes, 1187884539f7SKenneth D. Merry total_transfers ? DSM_TOTAL_TRANSFERS : DSM_SKIP, 1188884539f7SKenneth D. Merry total_transfers, 1189884539f7SKenneth D. Merry total_blocks ? DSM_TOTAL_BLOCKS : DSM_SKIP, 1190884539f7SKenneth D. Merry total_blocks, 1191884539f7SKenneth D. Merry kb_per_transfer ? DSM_KB_PER_TRANSFER : DSM_SKIP, 1192884539f7SKenneth D. Merry kb_per_transfer, 1193884539f7SKenneth D. Merry transfers_per_second ? DSM_TRANSFERS_PER_SECOND : DSM_SKIP, 1194884539f7SKenneth D. Merry transfers_per_second, 1195884539f7SKenneth D. Merry mb_per_second ? DSM_MB_PER_SECOND : DSM_SKIP, 1196884539f7SKenneth D. Merry mb_per_second, 1197884539f7SKenneth D. Merry blocks_per_second ? DSM_BLOCKS_PER_SECOND : DSM_SKIP, 1198884539f7SKenneth D. Merry blocks_per_second, 1199884539f7SKenneth D. Merry ms_per_transaction ? DSM_MS_PER_TRANSACTION : DSM_SKIP, 1200884539f7SKenneth D. Merry ms_per_transaction, 1201884539f7SKenneth D. Merry DSM_NONE)); 120228fb27baSJustin T. Gibbs } 120328fb27baSJustin T. Gibbs 12047194d335SPoul-Henning Kamp 12057194d335SPoul-Henning Kamp /* This is 1/2^64 */ 12067194d335SPoul-Henning Kamp #define BINTIME_SCALE 5.42101086242752217003726400434970855712890625e-20 12077194d335SPoul-Henning Kamp 120828fb27baSJustin T. Gibbs long double 12097194d335SPoul-Henning Kamp devstat_compute_etime(struct bintime *cur_time, struct bintime *prev_time) 121028fb27baSJustin T. Gibbs { 121128fb27baSJustin T. Gibbs long double etime; 121228fb27baSJustin T. Gibbs 12137194d335SPoul-Henning Kamp etime = cur_time->sec; 12147194d335SPoul-Henning Kamp etime += cur_time->frac * BINTIME_SCALE; 12157194d335SPoul-Henning Kamp if (prev_time != NULL) { 12167194d335SPoul-Henning Kamp etime -= prev_time->sec; 12177194d335SPoul-Henning Kamp etime -= prev_time->frac * BINTIME_SCALE; 12187194d335SPoul-Henning Kamp } 121928fb27baSJustin T. Gibbs return(etime); 122028fb27baSJustin T. Gibbs } 1221c4a5ef6eSThomas Moestl 122236eab1f5SPoul-Henning Kamp #define DELTA(field, index) \ 122336eab1f5SPoul-Henning Kamp (current->field[(index)] - (previous ? previous->field[(index)] : 0)) 122436eab1f5SPoul-Henning Kamp 122536eab1f5SPoul-Henning Kamp #define DELTA_T(field) \ 122636eab1f5SPoul-Henning Kamp devstat_compute_etime(¤t->field, \ 122736eab1f5SPoul-Henning Kamp (previous ? &previous->field : NULL)) 122836eab1f5SPoul-Henning Kamp 1229c4a5ef6eSThomas Moestl int 1230c4a5ef6eSThomas Moestl devstat_compute_statistics(struct devstat *current, struct devstat *previous, 1231c4a5ef6eSThomas Moestl long double etime, ...) 1232c4a5ef6eSThomas Moestl { 123336eab1f5SPoul-Henning Kamp u_int64_t totalbytes, totalbytesread, totalbyteswrite, totalbytesfree; 1234c4a5ef6eSThomas Moestl u_int64_t totaltransfers, totaltransfersread, totaltransferswrite; 1235c4a5ef6eSThomas Moestl u_int64_t totaltransfersother, totalblocks, totalblocksread; 123636eab1f5SPoul-Henning Kamp u_int64_t totalblockswrite, totaltransfersfree, totalblocksfree; 1237fdd6757eSMikolaj Golub long double totalduration, totaldurationread, totaldurationwrite; 1238fdd6757eSMikolaj Golub long double totaldurationfree, totaldurationother; 1239c4a5ef6eSThomas Moestl va_list ap; 1240c4a5ef6eSThomas Moestl devstat_metric metric; 1241c4a5ef6eSThomas Moestl u_int64_t *destu64; 1242c4a5ef6eSThomas Moestl long double *destld; 1243fdd6757eSMikolaj Golub int retval; 1244c4a5ef6eSThomas Moestl 1245c4a5ef6eSThomas Moestl retval = 0; 1246c4a5ef6eSThomas Moestl 1247c4a5ef6eSThomas Moestl /* 1248c4a5ef6eSThomas Moestl * current is the only mandatory field. 1249c4a5ef6eSThomas Moestl */ 1250c4a5ef6eSThomas Moestl if (current == NULL) { 1251c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 12527d72cda3SMaxime Henrion "%s: current stats structure was NULL", __func__); 1253c4a5ef6eSThomas Moestl return(-1); 1254c4a5ef6eSThomas Moestl } 1255c4a5ef6eSThomas Moestl 125636eab1f5SPoul-Henning Kamp totalbytesread = DELTA(bytes, DEVSTAT_READ); 125736eab1f5SPoul-Henning Kamp totalbyteswrite = DELTA(bytes, DEVSTAT_WRITE); 125836eab1f5SPoul-Henning Kamp totalbytesfree = DELTA(bytes, DEVSTAT_FREE); 125936eab1f5SPoul-Henning Kamp totalbytes = totalbytesread + totalbyteswrite + totalbytesfree; 1260c4a5ef6eSThomas Moestl 126136eab1f5SPoul-Henning Kamp totaltransfersread = DELTA(operations, DEVSTAT_READ); 126236eab1f5SPoul-Henning Kamp totaltransferswrite = DELTA(operations, DEVSTAT_WRITE); 126336eab1f5SPoul-Henning Kamp totaltransfersother = DELTA(operations, DEVSTAT_NO_DATA); 126436eab1f5SPoul-Henning Kamp totaltransfersfree = DELTA(operations, DEVSTAT_FREE); 1265c4a5ef6eSThomas Moestl totaltransfers = totaltransfersread + totaltransferswrite + 126636eab1f5SPoul-Henning Kamp totaltransfersother + totaltransfersfree; 1267c4a5ef6eSThomas Moestl 1268c4a5ef6eSThomas Moestl totalblocks = totalbytes; 1269c4a5ef6eSThomas Moestl totalblocksread = totalbytesread; 1270c4a5ef6eSThomas Moestl totalblockswrite = totalbyteswrite; 127136eab1f5SPoul-Henning Kamp totalblocksfree = totalbytesfree; 1272c4a5ef6eSThomas Moestl 1273c4a5ef6eSThomas Moestl if (current->block_size > 0) { 1274c4a5ef6eSThomas Moestl totalblocks /= current->block_size; 1275c4a5ef6eSThomas Moestl totalblocksread /= current->block_size; 1276c4a5ef6eSThomas Moestl totalblockswrite /= current->block_size; 127736eab1f5SPoul-Henning Kamp totalblocksfree /= current->block_size; 1278c4a5ef6eSThomas Moestl } else { 1279c4a5ef6eSThomas Moestl totalblocks /= 512; 1280c4a5ef6eSThomas Moestl totalblocksread /= 512; 1281c4a5ef6eSThomas Moestl totalblockswrite /= 512; 128236eab1f5SPoul-Henning Kamp totalblocksfree /= 512; 1283c4a5ef6eSThomas Moestl } 1284c4a5ef6eSThomas Moestl 1285fdd6757eSMikolaj Golub totaldurationread = DELTA_T(duration[DEVSTAT_READ]); 1286fdd6757eSMikolaj Golub totaldurationwrite = DELTA_T(duration[DEVSTAT_WRITE]); 1287fdd6757eSMikolaj Golub totaldurationfree = DELTA_T(duration[DEVSTAT_FREE]); 1288fdd6757eSMikolaj Golub totaldurationother = DELTA_T(duration[DEVSTAT_NO_DATA]); 1289fdd6757eSMikolaj Golub totalduration = totaldurationread + totaldurationwrite + 1290fdd6757eSMikolaj Golub totaldurationfree + totaldurationother; 1291fdd6757eSMikolaj Golub 1292c4a5ef6eSThomas Moestl va_start(ap, etime); 1293c4a5ef6eSThomas Moestl 1294c4a5ef6eSThomas Moestl while ((metric = (devstat_metric)va_arg(ap, devstat_metric)) != 0) { 1295c4a5ef6eSThomas Moestl 1296c4a5ef6eSThomas Moestl if (metric == DSM_NONE) 1297c4a5ef6eSThomas Moestl break; 1298c4a5ef6eSThomas Moestl 1299c4a5ef6eSThomas Moestl if (metric >= DSM_MAX) { 1300c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 13017d72cda3SMaxime Henrion "%s: metric %d is out of range", __func__, 1302c4a5ef6eSThomas Moestl metric); 1303c4a5ef6eSThomas Moestl retval = -1; 1304c4a5ef6eSThomas Moestl goto bailout; 1305c4a5ef6eSThomas Moestl } 1306c4a5ef6eSThomas Moestl 1307c4a5ef6eSThomas Moestl switch (devstat_arg_list[metric].argtype) { 1308c4a5ef6eSThomas Moestl case DEVSTAT_ARG_UINT64: 1309c4a5ef6eSThomas Moestl destu64 = (u_int64_t *)va_arg(ap, u_int64_t *); 1310c4a5ef6eSThomas Moestl break; 1311c4a5ef6eSThomas Moestl case DEVSTAT_ARG_LD: 1312c4a5ef6eSThomas Moestl destld = (long double *)va_arg(ap, long double *); 1313884539f7SKenneth D. Merry break; 1314884539f7SKenneth D. Merry case DEVSTAT_ARG_SKIP: 1315884539f7SKenneth D. Merry destld = (long double *)va_arg(ap, long double *); 1316c4a5ef6eSThomas Moestl break; 1317c4a5ef6eSThomas Moestl default: 1318c4a5ef6eSThomas Moestl retval = -1; 1319c4a5ef6eSThomas Moestl goto bailout; 1320c4a5ef6eSThomas Moestl break; /* NOTREACHED */ 1321c4a5ef6eSThomas Moestl } 1322c4a5ef6eSThomas Moestl 1323884539f7SKenneth D. Merry if (devstat_arg_list[metric].argtype == DEVSTAT_ARG_SKIP) 1324884539f7SKenneth D. Merry continue; 1325884539f7SKenneth D. Merry 1326c4a5ef6eSThomas Moestl switch (metric) { 1327c4a5ef6eSThomas Moestl case DSM_TOTAL_BYTES: 1328c4a5ef6eSThomas Moestl *destu64 = totalbytes; 1329c4a5ef6eSThomas Moestl break; 1330c4a5ef6eSThomas Moestl case DSM_TOTAL_BYTES_READ: 1331c4a5ef6eSThomas Moestl *destu64 = totalbytesread; 1332c4a5ef6eSThomas Moestl break; 1333c4a5ef6eSThomas Moestl case DSM_TOTAL_BYTES_WRITE: 1334c4a5ef6eSThomas Moestl *destu64 = totalbyteswrite; 1335c4a5ef6eSThomas Moestl break; 133636eab1f5SPoul-Henning Kamp case DSM_TOTAL_BYTES_FREE: 133736eab1f5SPoul-Henning Kamp *destu64 = totalbytesfree; 133836eab1f5SPoul-Henning Kamp break; 1339c4a5ef6eSThomas Moestl case DSM_TOTAL_TRANSFERS: 1340c4a5ef6eSThomas Moestl *destu64 = totaltransfers; 1341c4a5ef6eSThomas Moestl break; 1342c4a5ef6eSThomas Moestl case DSM_TOTAL_TRANSFERS_READ: 1343c4a5ef6eSThomas Moestl *destu64 = totaltransfersread; 1344c4a5ef6eSThomas Moestl break; 1345c4a5ef6eSThomas Moestl case DSM_TOTAL_TRANSFERS_WRITE: 1346c4a5ef6eSThomas Moestl *destu64 = totaltransferswrite; 1347c4a5ef6eSThomas Moestl break; 134836eab1f5SPoul-Henning Kamp case DSM_TOTAL_TRANSFERS_FREE: 134936eab1f5SPoul-Henning Kamp *destu64 = totaltransfersfree; 135036eab1f5SPoul-Henning Kamp break; 1351c4a5ef6eSThomas Moestl case DSM_TOTAL_TRANSFERS_OTHER: 1352c4a5ef6eSThomas Moestl *destu64 = totaltransfersother; 1353c4a5ef6eSThomas Moestl break; 1354c4a5ef6eSThomas Moestl case DSM_TOTAL_BLOCKS: 1355c4a5ef6eSThomas Moestl *destu64 = totalblocks; 1356c4a5ef6eSThomas Moestl break; 1357c4a5ef6eSThomas Moestl case DSM_TOTAL_BLOCKS_READ: 1358c4a5ef6eSThomas Moestl *destu64 = totalblocksread; 1359c4a5ef6eSThomas Moestl break; 1360c4a5ef6eSThomas Moestl case DSM_TOTAL_BLOCKS_WRITE: 1361c4a5ef6eSThomas Moestl *destu64 = totalblockswrite; 1362c4a5ef6eSThomas Moestl break; 136336eab1f5SPoul-Henning Kamp case DSM_TOTAL_BLOCKS_FREE: 136436eab1f5SPoul-Henning Kamp *destu64 = totalblocksfree; 136536eab1f5SPoul-Henning Kamp break; 1366c4a5ef6eSThomas Moestl case DSM_KB_PER_TRANSFER: 1367c4a5ef6eSThomas Moestl *destld = totalbytes; 1368c4a5ef6eSThomas Moestl *destld /= 1024; 1369c4a5ef6eSThomas Moestl if (totaltransfers > 0) 1370c4a5ef6eSThomas Moestl *destld /= totaltransfers; 1371c4a5ef6eSThomas Moestl else 1372c4a5ef6eSThomas Moestl *destld = 0.0; 1373c4a5ef6eSThomas Moestl break; 1374c4a5ef6eSThomas Moestl case DSM_KB_PER_TRANSFER_READ: 1375c4a5ef6eSThomas Moestl *destld = totalbytesread; 1376c4a5ef6eSThomas Moestl *destld /= 1024; 1377c4a5ef6eSThomas Moestl if (totaltransfersread > 0) 1378c4a5ef6eSThomas Moestl *destld /= totaltransfersread; 1379c4a5ef6eSThomas Moestl else 1380c4a5ef6eSThomas Moestl *destld = 0.0; 1381c4a5ef6eSThomas Moestl break; 1382c4a5ef6eSThomas Moestl case DSM_KB_PER_TRANSFER_WRITE: 1383c4a5ef6eSThomas Moestl *destld = totalbyteswrite; 1384c4a5ef6eSThomas Moestl *destld /= 1024; 1385c4a5ef6eSThomas Moestl if (totaltransferswrite > 0) 1386c4a5ef6eSThomas Moestl *destld /= totaltransferswrite; 1387c4a5ef6eSThomas Moestl else 1388c4a5ef6eSThomas Moestl *destld = 0.0; 1389c4a5ef6eSThomas Moestl break; 139036eab1f5SPoul-Henning Kamp case DSM_KB_PER_TRANSFER_FREE: 139136eab1f5SPoul-Henning Kamp *destld = totalbytesfree; 139236eab1f5SPoul-Henning Kamp *destld /= 1024; 139336eab1f5SPoul-Henning Kamp if (totaltransfersfree > 0) 139436eab1f5SPoul-Henning Kamp *destld /= totaltransfersfree; 139536eab1f5SPoul-Henning Kamp else 139636eab1f5SPoul-Henning Kamp *destld = 0.0; 139736eab1f5SPoul-Henning Kamp break; 1398c4a5ef6eSThomas Moestl case DSM_TRANSFERS_PER_SECOND: 1399c4a5ef6eSThomas Moestl if (etime > 0.0) { 1400c4a5ef6eSThomas Moestl *destld = totaltransfers; 1401c4a5ef6eSThomas Moestl *destld /= etime; 1402c4a5ef6eSThomas Moestl } else 1403c4a5ef6eSThomas Moestl *destld = 0.0; 1404c4a5ef6eSThomas Moestl break; 1405c4a5ef6eSThomas Moestl case DSM_TRANSFERS_PER_SECOND_READ: 1406c4a5ef6eSThomas Moestl if (etime > 0.0) { 1407c4a5ef6eSThomas Moestl *destld = totaltransfersread; 1408c4a5ef6eSThomas Moestl *destld /= etime; 1409c4a5ef6eSThomas Moestl } else 1410c4a5ef6eSThomas Moestl *destld = 0.0; 1411c4a5ef6eSThomas Moestl break; 1412c4a5ef6eSThomas Moestl case DSM_TRANSFERS_PER_SECOND_WRITE: 1413c4a5ef6eSThomas Moestl if (etime > 0.0) { 1414c4a5ef6eSThomas Moestl *destld = totaltransferswrite; 1415c4a5ef6eSThomas Moestl *destld /= etime; 1416c4a5ef6eSThomas Moestl } else 1417c4a5ef6eSThomas Moestl *destld = 0.0; 1418c4a5ef6eSThomas Moestl break; 141936eab1f5SPoul-Henning Kamp case DSM_TRANSFERS_PER_SECOND_FREE: 142036eab1f5SPoul-Henning Kamp if (etime > 0.0) { 142136eab1f5SPoul-Henning Kamp *destld = totaltransfersfree; 142236eab1f5SPoul-Henning Kamp *destld /= etime; 142336eab1f5SPoul-Henning Kamp } else 142436eab1f5SPoul-Henning Kamp *destld = 0.0; 142536eab1f5SPoul-Henning Kamp break; 1426c4a5ef6eSThomas Moestl case DSM_TRANSFERS_PER_SECOND_OTHER: 1427c4a5ef6eSThomas Moestl if (etime > 0.0) { 1428c4a5ef6eSThomas Moestl *destld = totaltransfersother; 1429c4a5ef6eSThomas Moestl *destld /= etime; 1430c4a5ef6eSThomas Moestl } else 1431c4a5ef6eSThomas Moestl *destld = 0.0; 1432c4a5ef6eSThomas Moestl break; 1433c4a5ef6eSThomas Moestl case DSM_MB_PER_SECOND: 1434c4a5ef6eSThomas Moestl *destld = totalbytes; 1435c4a5ef6eSThomas Moestl *destld /= 1024 * 1024; 1436c4a5ef6eSThomas Moestl if (etime > 0.0) 1437c4a5ef6eSThomas Moestl *destld /= etime; 1438c4a5ef6eSThomas Moestl else 1439c4a5ef6eSThomas Moestl *destld = 0.0; 1440c4a5ef6eSThomas Moestl break; 1441c4a5ef6eSThomas Moestl case DSM_MB_PER_SECOND_READ: 1442c4a5ef6eSThomas Moestl *destld = totalbytesread; 1443c4a5ef6eSThomas Moestl *destld /= 1024 * 1024; 1444c4a5ef6eSThomas Moestl if (etime > 0.0) 1445c4a5ef6eSThomas Moestl *destld /= etime; 1446c4a5ef6eSThomas Moestl else 1447c4a5ef6eSThomas Moestl *destld = 0.0; 1448c4a5ef6eSThomas Moestl break; 1449c4a5ef6eSThomas Moestl case DSM_MB_PER_SECOND_WRITE: 1450c4a5ef6eSThomas Moestl *destld = totalbyteswrite; 1451c4a5ef6eSThomas Moestl *destld /= 1024 * 1024; 1452c4a5ef6eSThomas Moestl if (etime > 0.0) 1453c4a5ef6eSThomas Moestl *destld /= etime; 1454c4a5ef6eSThomas Moestl else 1455c4a5ef6eSThomas Moestl *destld = 0.0; 1456c4a5ef6eSThomas Moestl break; 145736eab1f5SPoul-Henning Kamp case DSM_MB_PER_SECOND_FREE: 145836eab1f5SPoul-Henning Kamp *destld = totalbytesfree; 145936eab1f5SPoul-Henning Kamp *destld /= 1024 * 1024; 146036eab1f5SPoul-Henning Kamp if (etime > 0.0) 146136eab1f5SPoul-Henning Kamp *destld /= etime; 146236eab1f5SPoul-Henning Kamp else 146336eab1f5SPoul-Henning Kamp *destld = 0.0; 146436eab1f5SPoul-Henning Kamp break; 1465c4a5ef6eSThomas Moestl case DSM_BLOCKS_PER_SECOND: 1466c4a5ef6eSThomas Moestl *destld = totalblocks; 1467c4a5ef6eSThomas Moestl if (etime > 0.0) 1468c4a5ef6eSThomas Moestl *destld /= etime; 1469c4a5ef6eSThomas Moestl else 1470c4a5ef6eSThomas Moestl *destld = 0.0; 1471c4a5ef6eSThomas Moestl break; 1472c4a5ef6eSThomas Moestl case DSM_BLOCKS_PER_SECOND_READ: 1473c4a5ef6eSThomas Moestl *destld = totalblocksread; 1474c4a5ef6eSThomas Moestl if (etime > 0.0) 1475c4a5ef6eSThomas Moestl *destld /= etime; 1476c4a5ef6eSThomas Moestl else 1477c4a5ef6eSThomas Moestl *destld = 0.0; 1478c4a5ef6eSThomas Moestl break; 1479c4a5ef6eSThomas Moestl case DSM_BLOCKS_PER_SECOND_WRITE: 1480c4a5ef6eSThomas Moestl *destld = totalblockswrite; 1481c4a5ef6eSThomas Moestl if (etime > 0.0) 1482c4a5ef6eSThomas Moestl *destld /= etime; 1483c4a5ef6eSThomas Moestl else 1484c4a5ef6eSThomas Moestl *destld = 0.0; 1485c4a5ef6eSThomas Moestl break; 148636eab1f5SPoul-Henning Kamp case DSM_BLOCKS_PER_SECOND_FREE: 148736eab1f5SPoul-Henning Kamp *destld = totalblocksfree; 148836eab1f5SPoul-Henning Kamp if (etime > 0.0) 148936eab1f5SPoul-Henning Kamp *destld /= etime; 149036eab1f5SPoul-Henning Kamp else 149136eab1f5SPoul-Henning Kamp *destld = 0.0; 149236eab1f5SPoul-Henning Kamp break; 1493c4a5ef6eSThomas Moestl /* 1494999efd90SKenneth D. Merry * Some devstat callers update the duration and some don't. 1495999efd90SKenneth D. Merry * So this will only be accurate if they provide the 1496999efd90SKenneth D. Merry * duration. 1497c4a5ef6eSThomas Moestl */ 1498c4a5ef6eSThomas Moestl case DSM_MS_PER_TRANSACTION: 1499c4a5ef6eSThomas Moestl if (totaltransfers > 0) { 1500fdd6757eSMikolaj Golub *destld = totalduration; 1501c4a5ef6eSThomas Moestl *destld /= totaltransfers; 1502c4a5ef6eSThomas Moestl *destld *= 1000; 1503c4a5ef6eSThomas Moestl } else 1504c4a5ef6eSThomas Moestl *destld = 0.0; 1505c4a5ef6eSThomas Moestl break; 1506c4a5ef6eSThomas Moestl case DSM_MS_PER_TRANSACTION_READ: 1507c4a5ef6eSThomas Moestl if (totaltransfersread > 0) { 1508fdd6757eSMikolaj Golub *destld = totaldurationread; 1509c4a5ef6eSThomas Moestl *destld /= totaltransfersread; 1510c4a5ef6eSThomas Moestl *destld *= 1000; 1511c4a5ef6eSThomas Moestl } else 1512c4a5ef6eSThomas Moestl *destld = 0.0; 1513c4a5ef6eSThomas Moestl break; 1514c4a5ef6eSThomas Moestl case DSM_MS_PER_TRANSACTION_WRITE: 1515c4a5ef6eSThomas Moestl if (totaltransferswrite > 0) { 1516fdd6757eSMikolaj Golub *destld = totaldurationwrite; 1517c4a5ef6eSThomas Moestl *destld /= totaltransferswrite; 1518c4a5ef6eSThomas Moestl *destld *= 1000; 1519c4a5ef6eSThomas Moestl } else 1520c4a5ef6eSThomas Moestl *destld = 0.0; 1521c4a5ef6eSThomas Moestl break; 152236eab1f5SPoul-Henning Kamp case DSM_MS_PER_TRANSACTION_FREE: 152336eab1f5SPoul-Henning Kamp if (totaltransfersfree > 0) { 1524fdd6757eSMikolaj Golub *destld = totaldurationfree; 152536eab1f5SPoul-Henning Kamp *destld /= totaltransfersfree; 152636eab1f5SPoul-Henning Kamp *destld *= 1000; 152736eab1f5SPoul-Henning Kamp } else 152836eab1f5SPoul-Henning Kamp *destld = 0.0; 152936eab1f5SPoul-Henning Kamp break; 153036eab1f5SPoul-Henning Kamp case DSM_MS_PER_TRANSACTION_OTHER: 153136eab1f5SPoul-Henning Kamp if (totaltransfersother > 0) { 1532fdd6757eSMikolaj Golub *destld = totaldurationother; 153336eab1f5SPoul-Henning Kamp *destld /= totaltransfersother; 153436eab1f5SPoul-Henning Kamp *destld *= 1000; 153536eab1f5SPoul-Henning Kamp } else 153636eab1f5SPoul-Henning Kamp *destld = 0.0; 153736eab1f5SPoul-Henning Kamp break; 153836eab1f5SPoul-Henning Kamp case DSM_BUSY_PCT: 153936eab1f5SPoul-Henning Kamp *destld = DELTA_T(busy_time); 154036eab1f5SPoul-Henning Kamp if (*destld < 0) 154136eab1f5SPoul-Henning Kamp *destld = 0; 154236eab1f5SPoul-Henning Kamp *destld /= etime; 154336eab1f5SPoul-Henning Kamp *destld *= 100; 15441455c4e2SPoul-Henning Kamp if (*destld < 0) 15451455c4e2SPoul-Henning Kamp *destld = 0; 154636eab1f5SPoul-Henning Kamp break; 154736eab1f5SPoul-Henning Kamp case DSM_QUEUE_LENGTH: 154836eab1f5SPoul-Henning Kamp *destu64 = current->start_count - current->end_count; 154936eab1f5SPoul-Henning Kamp break; 1550fdd6757eSMikolaj Golub case DSM_TOTAL_DURATION: 1551fdd6757eSMikolaj Golub *destld = totalduration; 1552fdd6757eSMikolaj Golub break; 1553fdd6757eSMikolaj Golub case DSM_TOTAL_DURATION_READ: 1554fdd6757eSMikolaj Golub *destld = totaldurationread; 1555fdd6757eSMikolaj Golub break; 1556fdd6757eSMikolaj Golub case DSM_TOTAL_DURATION_WRITE: 1557fdd6757eSMikolaj Golub *destld = totaldurationwrite; 1558fdd6757eSMikolaj Golub break; 1559fdd6757eSMikolaj Golub case DSM_TOTAL_DURATION_FREE: 1560fdd6757eSMikolaj Golub *destld = totaldurationfree; 1561fdd6757eSMikolaj Golub break; 1562fdd6757eSMikolaj Golub case DSM_TOTAL_DURATION_OTHER: 1563fdd6757eSMikolaj Golub *destld = totaldurationother; 1564fdd6757eSMikolaj Golub break; 1565fdd6757eSMikolaj Golub case DSM_TOTAL_BUSY_TIME: 1566fdd6757eSMikolaj Golub *destld = DELTA_T(busy_time); 1567fdd6757eSMikolaj Golub break; 156836eab1f5SPoul-Henning Kamp /* 156936eab1f5SPoul-Henning Kamp * XXX: comment out the default block to see if any case's are missing. 157036eab1f5SPoul-Henning Kamp */ 157136eab1f5SPoul-Henning Kamp #if 1 1572c4a5ef6eSThomas Moestl default: 1573c4a5ef6eSThomas Moestl /* 1574c4a5ef6eSThomas Moestl * This shouldn't happen, since we should have 1575c4a5ef6eSThomas Moestl * caught any out of range metrics at the top of 1576c4a5ef6eSThomas Moestl * the loop. 1577c4a5ef6eSThomas Moestl */ 1578c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 15797d72cda3SMaxime Henrion "%s: unknown metric %d", __func__, metric); 1580c4a5ef6eSThomas Moestl retval = -1; 1581c4a5ef6eSThomas Moestl goto bailout; 1582c4a5ef6eSThomas Moestl break; /* NOTREACHED */ 158336eab1f5SPoul-Henning Kamp #endif 1584c4a5ef6eSThomas Moestl } 1585c4a5ef6eSThomas Moestl } 1586c4a5ef6eSThomas Moestl 1587c4a5ef6eSThomas Moestl bailout: 1588c4a5ef6eSThomas Moestl 1589c4a5ef6eSThomas Moestl va_end(ap); 1590c4a5ef6eSThomas Moestl return(retval); 1591c4a5ef6eSThomas Moestl } 1592c4a5ef6eSThomas Moestl 1593c4a5ef6eSThomas Moestl static int 1594c4a5ef6eSThomas Moestl readkmem(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes) 1595c4a5ef6eSThomas Moestl { 1596c4a5ef6eSThomas Moestl 1597c4a5ef6eSThomas Moestl if (kvm_read(kd, addr, buf, nbytes) == -1) { 1598c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 15997d72cda3SMaxime Henrion "%s: error reading value (kvm_read): %s", __func__, 1600c4a5ef6eSThomas Moestl kvm_geterr(kd)); 1601c4a5ef6eSThomas Moestl return(-1); 1602c4a5ef6eSThomas Moestl } 1603c4a5ef6eSThomas Moestl return(0); 1604c4a5ef6eSThomas Moestl } 1605c4a5ef6eSThomas Moestl 1606c4a5ef6eSThomas Moestl static int 1607c3508206SKenneth D. Merry readkmem_nl(kvm_t *kd, const char *name, void *buf, size_t nbytes) 1608c4a5ef6eSThomas Moestl { 1609c3508206SKenneth D. Merry struct nlist nl[2]; 1610c3508206SKenneth D. Merry 1611f664b76fSAlexander Kabaev nl[0].n_name = (char *)name; 1612c3508206SKenneth D. Merry nl[1].n_name = NULL; 1613c4a5ef6eSThomas Moestl 1614c4a5ef6eSThomas Moestl if (kvm_nlist(kd, nl) == -1) { 1615c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1616c4a5ef6eSThomas Moestl "%s: error getting name list (kvm_nlist): %s", 16177d72cda3SMaxime Henrion __func__, kvm_geterr(kd)); 1618c4a5ef6eSThomas Moestl return(-1); 1619c4a5ef6eSThomas Moestl } 1620c4a5ef6eSThomas Moestl return(readkmem(kd, nl[0].n_value, buf, nbytes)); 1621c4a5ef6eSThomas Moestl } 1622c4a5ef6eSThomas Moestl 1623c4a5ef6eSThomas Moestl /* 1624c4a5ef6eSThomas Moestl * This duplicates the functionality of the kernel sysctl handler for poking 1625c4a5ef6eSThomas Moestl * through crash dumps. 1626c4a5ef6eSThomas Moestl */ 1627c4a5ef6eSThomas Moestl static char * 1628c4a5ef6eSThomas Moestl get_devstat_kvm(kvm_t *kd) 1629c4a5ef6eSThomas Moestl { 16309dbcd4b0SStefan Farfeleder int i, wp; 1631c4a5ef6eSThomas Moestl long gen; 1632c4a5ef6eSThomas Moestl struct devstat *nds; 1633c4a5ef6eSThomas Moestl struct devstat ds; 1634c4a5ef6eSThomas Moestl struct devstatlist dhead; 1635c4a5ef6eSThomas Moestl int num_devs; 1636c4a5ef6eSThomas Moestl char *rv = NULL; 1637c4a5ef6eSThomas Moestl 16387194d335SPoul-Henning Kamp if ((num_devs = devstat_getnumdevs(kd)) <= 0) 1639c4a5ef6eSThomas Moestl return(NULL); 1640c4a5ef6eSThomas Moestl if (KREADNL(kd, X_DEVICE_STATQ, dhead) == -1) 1641c4a5ef6eSThomas Moestl return(NULL); 1642c4a5ef6eSThomas Moestl 1643c4a5ef6eSThomas Moestl nds = STAILQ_FIRST(&dhead); 1644c4a5ef6eSThomas Moestl 1645c4a5ef6eSThomas Moestl if ((rv = malloc(sizeof(gen))) == NULL) { 1646c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1647c4a5ef6eSThomas Moestl "%s: out of memory (initial malloc failed)", 16487d72cda3SMaxime Henrion __func__); 1649c4a5ef6eSThomas Moestl return(NULL); 1650c4a5ef6eSThomas Moestl } 16517194d335SPoul-Henning Kamp gen = devstat_getgeneration(kd); 1652c4a5ef6eSThomas Moestl memcpy(rv, &gen, sizeof(gen)); 1653c4a5ef6eSThomas Moestl wp = sizeof(gen); 1654c4a5ef6eSThomas Moestl /* 1655c4a5ef6eSThomas Moestl * Now push out all the devices. 1656c4a5ef6eSThomas Moestl */ 1657c4a5ef6eSThomas Moestl for (i = 0; (nds != NULL) && (i < num_devs); 1658c4a5ef6eSThomas Moestl nds = STAILQ_NEXT(nds, dev_links), i++) { 1659c4a5ef6eSThomas Moestl if (readkmem(kd, (long)nds, &ds, sizeof(ds)) == -1) { 1660c4a5ef6eSThomas Moestl free(rv); 1661c4a5ef6eSThomas Moestl return(NULL); 1662c4a5ef6eSThomas Moestl } 1663c4a5ef6eSThomas Moestl nds = &ds; 1664c4a5ef6eSThomas Moestl rv = (char *)reallocf(rv, sizeof(gen) + 1665c4a5ef6eSThomas Moestl sizeof(ds) * (i + 1)); 1666c4a5ef6eSThomas Moestl if (rv == NULL) { 1667c4a5ef6eSThomas Moestl snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1668c4a5ef6eSThomas Moestl "%s: out of memory (malloc failed)", 16697d72cda3SMaxime Henrion __func__); 1670c4a5ef6eSThomas Moestl return(NULL); 1671c4a5ef6eSThomas Moestl } 1672c4a5ef6eSThomas Moestl memcpy(rv + wp, &ds, sizeof(ds)); 1673c4a5ef6eSThomas Moestl wp += sizeof(ds); 1674c4a5ef6eSThomas Moestl } 1675c4a5ef6eSThomas Moestl return(rv); 1676c4a5ef6eSThomas Moestl } 1677