139211Sgibbs/* 239211Sgibbs * Copyright (c) 1997, 1998 Kenneth D. Merry. 339211Sgibbs * All rights reserved. 439211Sgibbs * 539211Sgibbs * Redistribution and use in source and binary forms, with or without 639211Sgibbs * modification, are permitted provided that the following conditions 739211Sgibbs * are met: 839211Sgibbs * 1. Redistributions of source code must retain the above copyright 939211Sgibbs * notice, this list of conditions and the following disclaimer. 1039211Sgibbs * 2. Redistributions in binary form must reproduce the above copyright 1139211Sgibbs * notice, this list of conditions and the following disclaimer in the 1239211Sgibbs * documentation and/or other materials provided with the distribution. 1339211Sgibbs * 3. The name of the author may not be used to endorse or promote products 1439211Sgibbs * derived from this software without specific prior written permission. 1539211Sgibbs * 1639211Sgibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1739211Sgibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1839211Sgibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1939211Sgibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2039211Sgibbs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2139211Sgibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2239211Sgibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2339211Sgibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2439211Sgibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2539211Sgibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2639211Sgibbs * SUCH DAMAGE. 2739211Sgibbs */ 2839211Sgibbs 2983551Sdillon#include <sys/cdefs.h> 3083551Sdillon__FBSDID("$FreeBSD$"); 3183551Sdillon 3239211Sgibbs#include <sys/types.h> 3339211Sgibbs#include <sys/sysctl.h> 3439211Sgibbs#include <sys/errno.h> 35110998Sphk#include <sys/resource.h> 3681133Stmm#include <sys/queue.h> 3739211Sgibbs 3839451Sken#include <ctype.h> 3939211Sgibbs#include <err.h> 4081133Stmm#include <fcntl.h> 4181133Stmm#include <limits.h> 4239211Sgibbs#include <stdio.h> 4339211Sgibbs#include <stdlib.h> 4439211Sgibbs#include <string.h> 4581133Stmm#include <stdarg.h> 4681133Stmm#include <kvm.h> 47121064Sbde#include <nlist.h> 4839211Sgibbs 4939211Sgibbs#include "devstat.h" 5039211Sgibbs 51113710Sphkint 52113710Sphkcompute_stats(struct devstat *current, struct devstat *previous, 53113710Sphk long double etime, u_int64_t *total_bytes, 54113710Sphk u_int64_t *total_transfers, u_int64_t *total_blocks, 55113710Sphk long double *kb_per_transfer, long double *transfers_per_second, 56113710Sphk long double *mb_per_second, long double *blocks_per_second, 57113710Sphk long double *ms_per_transaction); 58113710Sphk 5981133Stmmtypedef enum { 6081133Stmm DEVSTAT_ARG_NOTYPE, 6181133Stmm DEVSTAT_ARG_UINT64, 6281883Sken DEVSTAT_ARG_LD, 6381883Sken DEVSTAT_ARG_SKIP 6481133Stmm} devstat_arg_type; 6581133Stmm 6639211Sgibbschar devstat_errbuf[DEVSTAT_ERRBUF_SIZE]; 6739211Sgibbs 6839211Sgibbs/* 6939211Sgibbs * Table to match descriptive strings with device types. These are in 7039211Sgibbs * order from most common to least common to speed search time. 7139211Sgibbs */ 7239211Sgibbsstruct devstat_match_table match_table[] = { 7339211Sgibbs {"da", DEVSTAT_TYPE_DIRECT, DEVSTAT_MATCH_TYPE}, 7439211Sgibbs {"cd", DEVSTAT_TYPE_CDROM, DEVSTAT_MATCH_TYPE}, 7539211Sgibbs {"scsi", DEVSTAT_TYPE_IF_SCSI, DEVSTAT_MATCH_IF}, 7639211Sgibbs {"ide", DEVSTAT_TYPE_IF_IDE, DEVSTAT_MATCH_IF}, 7739211Sgibbs {"other", DEVSTAT_TYPE_IF_OTHER, DEVSTAT_MATCH_IF}, 7839211Sgibbs {"worm", DEVSTAT_TYPE_WORM, DEVSTAT_MATCH_TYPE}, 7939211Sgibbs {"sa", DEVSTAT_TYPE_SEQUENTIAL,DEVSTAT_MATCH_TYPE}, 8039211Sgibbs {"pass", DEVSTAT_TYPE_PASS, DEVSTAT_MATCH_PASS}, 8139211Sgibbs {"optical", DEVSTAT_TYPE_OPTICAL, DEVSTAT_MATCH_TYPE}, 8239211Sgibbs {"array", DEVSTAT_TYPE_STORARRAY, DEVSTAT_MATCH_TYPE}, 8339211Sgibbs {"changer", DEVSTAT_TYPE_CHANGER, DEVSTAT_MATCH_TYPE}, 8439211Sgibbs {"scanner", DEVSTAT_TYPE_SCANNER, DEVSTAT_MATCH_TYPE}, 8539211Sgibbs {"printer", DEVSTAT_TYPE_PRINTER, DEVSTAT_MATCH_TYPE}, 8639211Sgibbs {"floppy", DEVSTAT_TYPE_FLOPPY, DEVSTAT_MATCH_TYPE}, 8739211Sgibbs {"proc", DEVSTAT_TYPE_PROCESSOR, DEVSTAT_MATCH_TYPE}, 8839211Sgibbs {"comm", DEVSTAT_TYPE_COMM, DEVSTAT_MATCH_TYPE}, 8939211Sgibbs {"enclosure", DEVSTAT_TYPE_ENCLOSURE, DEVSTAT_MATCH_TYPE}, 9039211Sgibbs {NULL, 0, 0} 9139211Sgibbs}; 9239211Sgibbs 9381133Stmmstruct devstat_args { 9481133Stmm devstat_metric metric; 9581133Stmm devstat_arg_type argtype; 9681133Stmm} devstat_arg_list[] = { 9781133Stmm { DSM_NONE, DEVSTAT_ARG_NOTYPE }, 9881133Stmm { DSM_TOTAL_BYTES, DEVSTAT_ARG_UINT64 }, 9981133Stmm { DSM_TOTAL_BYTES_READ, DEVSTAT_ARG_UINT64 }, 10081133Stmm { DSM_TOTAL_BYTES_WRITE, DEVSTAT_ARG_UINT64 }, 10181133Stmm { DSM_TOTAL_TRANSFERS, DEVSTAT_ARG_UINT64 }, 10281133Stmm { DSM_TOTAL_TRANSFERS_READ, DEVSTAT_ARG_UINT64 }, 10381133Stmm { DSM_TOTAL_TRANSFERS_WRITE, DEVSTAT_ARG_UINT64 }, 104112405Sphk { DSM_TOTAL_TRANSFERS_OTHER, DEVSTAT_ARG_UINT64 }, 10581133Stmm { DSM_TOTAL_BLOCKS, DEVSTAT_ARG_UINT64 }, 10681133Stmm { DSM_TOTAL_BLOCKS_READ, DEVSTAT_ARG_UINT64 }, 10781133Stmm { DSM_TOTAL_BLOCKS_WRITE, DEVSTAT_ARG_UINT64 }, 10881133Stmm { DSM_KB_PER_TRANSFER, DEVSTAT_ARG_LD }, 10981133Stmm { DSM_KB_PER_TRANSFER_READ, DEVSTAT_ARG_LD }, 11081133Stmm { DSM_KB_PER_TRANSFER_WRITE, DEVSTAT_ARG_LD }, 11181133Stmm { DSM_TRANSFERS_PER_SECOND, DEVSTAT_ARG_LD }, 11281133Stmm { DSM_TRANSFERS_PER_SECOND_READ, DEVSTAT_ARG_LD }, 11381133Stmm { DSM_TRANSFERS_PER_SECOND_WRITE, DEVSTAT_ARG_LD }, 11481133Stmm { DSM_TRANSFERS_PER_SECOND_OTHER, DEVSTAT_ARG_LD }, 11581133Stmm { DSM_MB_PER_SECOND, DEVSTAT_ARG_LD }, 11681133Stmm { DSM_MB_PER_SECOND_READ, DEVSTAT_ARG_LD }, 11781133Stmm { DSM_MB_PER_SECOND_WRITE, DEVSTAT_ARG_LD }, 11881133Stmm { DSM_BLOCKS_PER_SECOND, DEVSTAT_ARG_LD }, 11981133Stmm { DSM_BLOCKS_PER_SECOND_READ, DEVSTAT_ARG_LD }, 12081133Stmm { DSM_BLOCKS_PER_SECOND_WRITE, DEVSTAT_ARG_LD }, 12181133Stmm { DSM_MS_PER_TRANSACTION, DEVSTAT_ARG_LD }, 12281133Stmm { DSM_MS_PER_TRANSACTION_READ, DEVSTAT_ARG_LD }, 12381883Sken { DSM_MS_PER_TRANSACTION_WRITE, DEVSTAT_ARG_LD }, 124112406Sphk { DSM_SKIP, DEVSTAT_ARG_SKIP }, 125112405Sphk { DSM_TOTAL_BYTES_FREE, DEVSTAT_ARG_UINT64 }, 126112405Sphk { DSM_TOTAL_TRANSFERS_FREE, DEVSTAT_ARG_UINT64 }, 127112405Sphk { DSM_TOTAL_BLOCKS_FREE, DEVSTAT_ARG_UINT64 }, 128112405Sphk { DSM_KB_PER_TRANSFER_FREE, DEVSTAT_ARG_LD }, 129112405Sphk { DSM_MB_PER_SECOND_FREE, DEVSTAT_ARG_LD }, 130112405Sphk { DSM_TRANSFERS_PER_SECOND_FREE, DEVSTAT_ARG_LD }, 131112405Sphk { DSM_BLOCKS_PER_SECOND_FREE, DEVSTAT_ARG_LD }, 132112405Sphk { DSM_MS_PER_TRANSACTION_OTHER, DEVSTAT_ARG_LD }, 133112373Sphk { DSM_MS_PER_TRANSACTION_FREE, DEVSTAT_ARG_LD }, 134112373Sphk { DSM_BUSY_PCT, DEVSTAT_ARG_LD }, 135112373Sphk { DSM_QUEUE_LENGTH, DEVSTAT_ARG_UINT64 }, 136244270Strociny { DSM_TOTAL_DURATION, DEVSTAT_ARG_LD }, 137244270Strociny { DSM_TOTAL_DURATION_READ, DEVSTAT_ARG_LD }, 138244270Strociny { DSM_TOTAL_DURATION_WRITE, DEVSTAT_ARG_LD }, 139244270Strociny { DSM_TOTAL_DURATION_FREE, DEVSTAT_ARG_LD }, 140244270Strociny { DSM_TOTAL_DURATION_OTHER, DEVSTAT_ARG_LD }, 141244270Strociny { DSM_TOTAL_BUSY_TIME, DEVSTAT_ARG_LD }, 14281133Stmm}; 14381133Stmm 14483868Skenstatic const char *namelist[] = { 14581133Stmm#define X_NUMDEVS 0 14681133Stmm "_devstat_num_devs", 14781133Stmm#define X_GENERATION 1 14881133Stmm "_devstat_generation", 14981133Stmm#define X_VERSION 2 15081133Stmm "_devstat_version", 15181133Stmm#define X_DEVICE_STATQ 3 15281133Stmm "_device_statq", 15381133Stmm#define X_END 4 15481133Stmm}; 15581133Stmm 15639211Sgibbs/* 15739211Sgibbs * Local function declarations. 15839211Sgibbs */ 15939211Sgibbsstatic int compare_select(const void *arg1, const void *arg2); 16081133Stmmstatic int readkmem(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes); 16183868Skenstatic int readkmem_nl(kvm_t *kd, const char *name, void *buf, size_t nbytes); 16281133Stmmstatic char *get_devstat_kvm(kvm_t *kd); 16339211Sgibbs 16481133Stmm#define KREADNL(kd, var, val) \ 16581133Stmm readkmem_nl(kd, namelist[var], &val, sizeof(val)) 16681133Stmm 16739211Sgibbsint 16881133Stmmdevstat_getnumdevs(kvm_t *kd) 16939211Sgibbs{ 17039211Sgibbs size_t numdevsize; 17139211Sgibbs int numdevs; 17239211Sgibbs 17339211Sgibbs numdevsize = sizeof(int); 17439211Sgibbs 17539211Sgibbs /* 17639211Sgibbs * Find out how many devices we have in the system. 17739211Sgibbs */ 17881133Stmm if (kd == NULL) { 17981133Stmm if (sysctlbyname("kern.devstat.numdevs", &numdevs, 18081133Stmm &numdevsize, NULL, 0) == -1) { 18181133Stmm snprintf(devstat_errbuf, sizeof(devstat_errbuf), 18282028Sken "%s: error getting number of devices\n" 183145553Smux "%s: %s", __func__, __func__, 18482028Sken strerror(errno)); 18581133Stmm return(-1); 18681133Stmm } else 18781133Stmm return(numdevs); 18881133Stmm } else { 18983868Sken 19081133Stmm if (KREADNL(kd, X_NUMDEVS, numdevs) == -1) 19181133Stmm return(-1); 19281133Stmm else 19381133Stmm return(numdevs); 19481133Stmm } 19539211Sgibbs} 19639211Sgibbs 19739211Sgibbs/* 19839211Sgibbs * This is an easy way to get the generation number, but the generation is 19939211Sgibbs * supplied in a more atmoic manner by the kern.devstat.all sysctl. 20039211Sgibbs * Because this generation sysctl is separate from the statistics sysctl, 20139211Sgibbs * the device list and the generation could change between the time that 20239211Sgibbs * this function is called and the device list is retreived. 20339211Sgibbs */ 20439498Skenlong 20581133Stmmdevstat_getgeneration(kvm_t *kd) 20639211Sgibbs{ 20739211Sgibbs size_t gensize; 20839498Sken long generation; 20939211Sgibbs 21039498Sken gensize = sizeof(long); 21139211Sgibbs 21239211Sgibbs /* 21339211Sgibbs * Get the current generation number. 21439211Sgibbs */ 21581133Stmm if (kd == NULL) { 21681133Stmm if (sysctlbyname("kern.devstat.generation", &generation, 21781133Stmm &gensize, NULL, 0) == -1) { 21881133Stmm snprintf(devstat_errbuf, sizeof(devstat_errbuf), 21982028Sken "%s: error getting devstat generation\n%s: %s", 220145553Smux __func__, __func__, strerror(errno)); 22181133Stmm return(-1); 22281133Stmm } else 22381133Stmm return(generation); 22481133Stmm } else { 22581133Stmm if (KREADNL(kd, X_GENERATION, generation) == -1) 22681133Stmm return(-1); 22781133Stmm else 22881133Stmm return(generation); 22981133Stmm } 23039211Sgibbs} 23139211Sgibbs 23239211Sgibbs/* 23339211Sgibbs * Get the current devstat version. The return value of this function 23439211Sgibbs * should be compared with DEVSTAT_VERSION, which is defined in 23539211Sgibbs * sys/devicestat.h. This will enable userland programs to determine 23639211Sgibbs * whether they are out of sync with the kernel. 23739211Sgibbs */ 23839211Sgibbsint 23981133Stmmdevstat_getversion(kvm_t *kd) 24039211Sgibbs{ 24139211Sgibbs size_t versize; 24239211Sgibbs int version; 24339211Sgibbs 24439211Sgibbs versize = sizeof(int); 24539211Sgibbs 24639211Sgibbs /* 24739211Sgibbs * Get the current devstat version. 24839211Sgibbs */ 24981133Stmm if (kd == NULL) { 25081133Stmm if (sysctlbyname("kern.devstat.version", &version, &versize, 25181133Stmm NULL, 0) == -1) { 25281133Stmm snprintf(devstat_errbuf, sizeof(devstat_errbuf), 25382028Sken "%s: error getting devstat version\n%s: %s", 254145553Smux __func__, __func__, strerror(errno)); 25581133Stmm return(-1); 25681133Stmm } else 25781133Stmm return(version); 25881133Stmm } else { 25981133Stmm if (KREADNL(kd, X_VERSION, version) == -1) 26081133Stmm return(-1); 26181133Stmm else 26281133Stmm return(version); 26381133Stmm } 26439211Sgibbs} 26539211Sgibbs 26639211Sgibbs/* 26739211Sgibbs * Check the devstat version we know about against the devstat version the 26839211Sgibbs * kernel knows about. If they don't match, print an error into the 26939211Sgibbs * devstat error buffer, and return -1. If they match, return 0. 27039211Sgibbs */ 27139211Sgibbsint 27281133Stmmdevstat_checkversion(kvm_t *kd) 27339211Sgibbs{ 27481984Sbrian int buflen, res, retval = 0, version; 27539211Sgibbs 27681133Stmm version = devstat_getversion(kd); 27739451Sken 27839451Sken if (version != DEVSTAT_VERSION) { 27939451Sken /* 28081984Sbrian * If getversion() returns an error (i.e. -1), then it 28139451Sken * has printed an error message in the buffer. Therefore, 28239451Sken * we need to add a \n to the end of that message before we 28339451Sken * print our own message in the buffer. 28439451Sken */ 28581984Sbrian if (version == -1) 28639451Sken buflen = strlen(devstat_errbuf); 28781984Sbrian else 28881984Sbrian buflen = 0; 28939451Sken 29081984Sbrian res = snprintf(devstat_errbuf + buflen, 29182028Sken DEVSTAT_ERRBUF_SIZE - buflen, 29282028Sken "%s%s: userland devstat version %d is not " 29382028Sken "the same as the kernel\n%s: devstat " 29482028Sken "version %d\n", version == -1 ? "\n" : "", 295145553Smux __func__, DEVSTAT_VERSION, __func__, version); 29639211Sgibbs 29781984Sbrian if (res < 0) 29881984Sbrian devstat_errbuf[buflen] = '\0'; 29939451Sken 30081984Sbrian buflen = strlen(devstat_errbuf); 30182028Sken if (version < DEVSTAT_VERSION) 30281984Sbrian res = snprintf(devstat_errbuf + buflen, 30382028Sken DEVSTAT_ERRBUF_SIZE - buflen, 30482028Sken "%s: libdevstat newer than kernel\n", 305145553Smux __func__); 30682028Sken else 30781984Sbrian res = snprintf(devstat_errbuf + buflen, 30882028Sken DEVSTAT_ERRBUF_SIZE - buflen, 30982028Sken "%s: kernel newer than libdevstat\n", 310145553Smux __func__); 31139211Sgibbs 31281984Sbrian if (res < 0) 31381984Sbrian devstat_errbuf[buflen] = '\0'; 31439211Sgibbs 31539211Sgibbs retval = -1; 31639211Sgibbs } 31739211Sgibbs 31839211Sgibbs return(retval); 31939211Sgibbs} 32039211Sgibbs 32139211Sgibbs/* 32239211Sgibbs * Get the current list of devices and statistics, and the current 32339211Sgibbs * generation number. 32439211Sgibbs * 32539211Sgibbs * Return values: 32639211Sgibbs * -1 -- error 32739211Sgibbs * 0 -- device list is unchanged 32839211Sgibbs * 1 -- device list has changed 32939211Sgibbs */ 33039211Sgibbsint 33181133Stmmdevstat_getdevs(kvm_t *kd, struct statinfo *stats) 33239211Sgibbs{ 33339211Sgibbs int error; 33439211Sgibbs size_t dssize; 33539498Sken int oldnumdevs; 33639498Sken long oldgeneration; 33739211Sgibbs int retval = 0; 33839211Sgibbs struct devinfo *dinfo; 339112288Sphk struct timespec ts; 34039211Sgibbs 34139211Sgibbs dinfo = stats->dinfo; 34239211Sgibbs 34339211Sgibbs if (dinfo == NULL) { 34481133Stmm snprintf(devstat_errbuf, sizeof(devstat_errbuf), 345145553Smux "%s: stats->dinfo was NULL", __func__); 34639211Sgibbs return(-1); 34739211Sgibbs } 34839211Sgibbs 34939211Sgibbs oldnumdevs = dinfo->numdevs; 35039211Sgibbs oldgeneration = dinfo->generation; 35139211Sgibbs 352112288Sphk clock_gettime(CLOCK_MONOTONIC, &ts); 353112288Sphk stats->snap_time = ts.tv_sec + ts.tv_nsec * 1e-9; 35439211Sgibbs 35581133Stmm if (kd == NULL) { 35681133Stmm /* If this is our first time through, mem_ptr will be null. */ 35781133Stmm if (dinfo->mem_ptr == NULL) { 35839211Sgibbs /* 35981133Stmm * Get the number of devices. If it's negative, it's an 36081133Stmm * error. Don't bother setting the error string, since 36181133Stmm * getnumdevs() has already done that for us. 36239211Sgibbs */ 363112293Sphk if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0) 36439211Sgibbs return(-1); 36581133Stmm 36681133Stmm /* 36781133Stmm * The kern.devstat.all sysctl returns the current 36881133Stmm * generation number, as well as all the devices. 36981133Stmm * So we need four bytes more. 37081133Stmm */ 37181133Stmm dssize = (dinfo->numdevs * sizeof(struct devstat)) + 37281133Stmm sizeof(long); 37381133Stmm dinfo->mem_ptr = (u_int8_t *)malloc(dssize); 374229735Sghelmer if (dinfo->mem_ptr == NULL) { 375229735Sghelmer snprintf(devstat_errbuf, sizeof(devstat_errbuf), 376229735Sghelmer "%s: Cannot allocate memory for mem_ptr element", 377229735Sghelmer __func__); 378229735Sghelmer return(-1); 379229735Sghelmer } 38081133Stmm } else 38181133Stmm dssize = (dinfo->numdevs * sizeof(struct devstat)) + 38281133Stmm sizeof(long); 38339211Sgibbs 38481133Stmm /* 38581133Stmm * Request all of the devices. We only really allow for one 38681133Stmm * ENOMEM failure. It would, of course, be possible to just go 38781133Stmm * in a loop and keep reallocing the device structure until we 38881133Stmm * don't get ENOMEM back. I'm not sure it's worth it, though. 38981133Stmm * If devices are being added to the system that quickly, maybe 39081133Stmm * the user can just wait until all devices are added. 39181133Stmm */ 392112373Sphk for (;;) { 393112373Sphk error = sysctlbyname("kern.devstat.all", 394112373Sphk dinfo->mem_ptr, 395112373Sphk &dssize, NULL, 0); 396112373Sphk if (error != -1 || errno != EBUSY) 397112373Sphk break; 398112373Sphk } 399112373Sphk if (error == -1) { 40081133Stmm /* 40181133Stmm * If we get ENOMEM back, that means that there are 40281133Stmm * more devices now, so we need to allocate more 40381133Stmm * space for the device array. 40481133Stmm */ 40581133Stmm if (errno == ENOMEM) { 40681133Stmm /* 40781133Stmm * No need to set the error string here, 408112288Sphk * devstat_getnumdevs() will do that if it fails. 40981133Stmm */ 410112293Sphk if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0) 41181133Stmm return(-1); 41281133Stmm 41381133Stmm dssize = (dinfo->numdevs * 41481133Stmm sizeof(struct devstat)) + sizeof(long); 41581133Stmm dinfo->mem_ptr = (u_int8_t *) 41681133Stmm realloc(dinfo->mem_ptr, dssize); 41781133Stmm if ((error = sysctlbyname("kern.devstat.all", 41881133Stmm dinfo->mem_ptr, &dssize, NULL, 0)) == -1) { 41981133Stmm snprintf(devstat_errbuf, 42082028Sken sizeof(devstat_errbuf), 42182028Sken "%s: error getting device " 422145553Smux "stats\n%s: %s", __func__, 423145553Smux __func__, strerror(errno)); 42481133Stmm return(-1); 42581133Stmm } 42681133Stmm } else { 42781133Stmm snprintf(devstat_errbuf, sizeof(devstat_errbuf), 42882028Sken "%s: error getting device stats\n" 429145553Smux "%s: %s", __func__, __func__, 43082028Sken strerror(errno)); 43139211Sgibbs return(-1); 43239211Sgibbs } 43381133Stmm } 43481133Stmm 43581133Stmm } else { 43681133Stmm /* 43781133Stmm * This is of course non-atomic, but since we are working 43881133Stmm * on a core dump, the generation is unlikely to change 43981133Stmm */ 440112293Sphk if ((dinfo->numdevs = devstat_getnumdevs(kd)) == -1) 44139211Sgibbs return(-1); 442145549Smux if ((dinfo->mem_ptr = (u_int8_t *)get_devstat_kvm(kd)) == NULL) 44381133Stmm return(-1); 44481133Stmm } 44539211Sgibbs /* 44639211Sgibbs * The sysctl spits out the generation as the first four bytes, 44739211Sgibbs * then all of the device statistics structures. 44839211Sgibbs */ 44939498Sken dinfo->generation = *(long *)dinfo->mem_ptr; 45039211Sgibbs 45139211Sgibbs /* 45239211Sgibbs * If the generation has changed, and if the current number of 45339211Sgibbs * devices is not the same as the number of devices recorded in the 45439211Sgibbs * devinfo structure, it is likely that the device list has shrunk. 45539211Sgibbs * The reason that it is likely that the device list has shrunk in 45639211Sgibbs * this case is that if the device list has grown, the sysctl above 45739211Sgibbs * will return an ENOMEM error, and we will reset the number of 45839211Sgibbs * devices and reallocate the device array. If the second sysctl 45939211Sgibbs * fails, we will return an error and therefore never get to this 46039211Sgibbs * point. If the device list has shrunk, the sysctl will not 46139211Sgibbs * return an error since we have more space allocated than is 46239211Sgibbs * necessary. So, in the shrinkage case, we catch it here and 46339211Sgibbs * reallocate the array so that we don't use any more space than is 46439211Sgibbs * necessary. 46539211Sgibbs */ 46639211Sgibbs if (oldgeneration != dinfo->generation) { 467112293Sphk if (devstat_getnumdevs(kd) != dinfo->numdevs) { 468112293Sphk if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0) 46939211Sgibbs return(-1); 47039211Sgibbs dssize = (dinfo->numdevs * sizeof(struct devstat)) + 47139498Sken sizeof(long); 47239211Sgibbs dinfo->mem_ptr = (u_int8_t *)realloc(dinfo->mem_ptr, 47339211Sgibbs dssize); 47439211Sgibbs } 47539211Sgibbs retval = 1; 47639211Sgibbs } 47739211Sgibbs 47839498Sken dinfo->devices = (struct devstat *)(dinfo->mem_ptr + sizeof(long)); 47939211Sgibbs 48039211Sgibbs return(retval); 48139211Sgibbs} 48239211Sgibbs 48339211Sgibbs/* 48439211Sgibbs * selectdevs(): 48539211Sgibbs * 48639211Sgibbs * Devices are selected/deselected based upon the following criteria: 48739211Sgibbs * - devices specified by the user on the command line 48839211Sgibbs * - devices matching any device type expressions given on the command line 48939211Sgibbs * - devices with the highest I/O, if 'top' mode is enabled 49039211Sgibbs * - the first n unselected devices in the device list, if maxshowdevs 49139211Sgibbs * devices haven't already been selected and if the user has not 49239211Sgibbs * specified any devices on the command line and if we're in "add" mode. 49339211Sgibbs * 49439211Sgibbs * Input parameters: 49539211Sgibbs * - device selection list (dev_select) 49639211Sgibbs * - current number of devices selected (num_selected) 49739211Sgibbs * - total number of devices in the selection list (num_selections) 49839211Sgibbs * - devstat generation as of the last time selectdevs() was called 49939211Sgibbs * (select_generation) 50039211Sgibbs * - current devstat generation (current_generation) 50139211Sgibbs * - current list of devices and statistics (devices) 50239211Sgibbs * - number of devices in the current device list (numdevs) 50339211Sgibbs * - compiled version of the command line device type arguments (matches) 50439211Sgibbs * - This is optional. If the number of devices is 0, this will be ignored. 50539211Sgibbs * - The matching code pays attention to the current selection mode. So 50639211Sgibbs * if you pass in a matching expression, it will be evaluated based 50739211Sgibbs * upon the selection mode that is passed in. See below for details. 50839211Sgibbs * - number of device type matching expressions (num_matches) 50939211Sgibbs * - Set to 0 to disable the matching code. 51039211Sgibbs * - list of devices specified on the command line by the user (dev_selections) 51139211Sgibbs * - number of devices selected on the command line by the user 51239211Sgibbs * (num_dev_selections) 51339211Sgibbs * - Our selection mode. There are four different selection modes: 51439211Sgibbs * - add mode. (DS_SELECT_ADD) Any devices matching devices explicitly 51539211Sgibbs * selected by the user or devices matching a pattern given by the 51639211Sgibbs * user will be selected in addition to devices that are already 51739211Sgibbs * selected. Additional devices will be selected, up to maxshowdevs 51839211Sgibbs * number of devices. 51939211Sgibbs * - only mode. (DS_SELECT_ONLY) Only devices matching devices 52039211Sgibbs * explicitly given by the user or devices matching a pattern 52139211Sgibbs * given by the user will be selected. No other devices will be 52239211Sgibbs * selected. 52339211Sgibbs * - addonly mode. (DS_SELECT_ADDONLY) This is similar to add and 52439211Sgibbs * only. Basically, this will not de-select any devices that are 52539211Sgibbs * current selected, as only mode would, but it will also not 52639211Sgibbs * gratuitously select up to maxshowdevs devices as add mode would. 52739211Sgibbs * - remove mode. (DS_SELECT_REMOVE) Any devices matching devices 52839211Sgibbs * explicitly selected by the user or devices matching a pattern 52939211Sgibbs * given by the user will be de-selected. 53039211Sgibbs * - maximum number of devices we can select (maxshowdevs) 53139211Sgibbs * - flag indicating whether or not we're in 'top' mode (perf_select) 53239211Sgibbs * 53339211Sgibbs * Output data: 53439211Sgibbs * - the device selection list may be modified and passed back out 53539211Sgibbs * - the number of devices selected and the total number of items in the 53639211Sgibbs * device selection list may be changed 53739211Sgibbs * - the selection generation may be changed to match the current generation 53839211Sgibbs * 53939211Sgibbs * Return values: 54039211Sgibbs * -1 -- error 54139211Sgibbs * 0 -- selected devices are unchanged 54239211Sgibbs * 1 -- selected devices changed 54339211Sgibbs */ 54439211Sgibbsint 54581133Stmmdevstat_selectdevs(struct device_selection **dev_select, int *num_selected, 54681133Stmm int *num_selections, long *select_generation, 54781133Stmm long current_generation, struct devstat *devices, 54881133Stmm int numdevs, struct devstat_match *matches, int num_matches, 54981133Stmm char **dev_selections, int num_dev_selections, 55081133Stmm devstat_select_mode select_mode, int maxshowdevs, 55181133Stmm int perf_select) 55239211Sgibbs{ 55392913Sobrien int i, j, k; 55439211Sgibbs int init_selections = 0, init_selected_var = 0; 55539211Sgibbs struct device_selection *old_dev_select = NULL; 55639211Sgibbs int old_num_selections = 0, old_num_selected; 55739211Sgibbs int selection_number = 0; 55839211Sgibbs int changed = 0, found = 0; 55939211Sgibbs 560119029Sphk if ((dev_select == NULL) || (devices == NULL) || (numdevs < 0)) 56139211Sgibbs return(-1); 56239211Sgibbs 56339211Sgibbs /* 56439211Sgibbs * We always want to make sure that we have as many dev_select 56539211Sgibbs * entries as there are devices. 56639211Sgibbs */ 56739211Sgibbs /* 56839211Sgibbs * In this case, we haven't selected devices before. 56939211Sgibbs */ 57039211Sgibbs if (*dev_select == NULL) { 57139211Sgibbs *dev_select = (struct device_selection *)malloc(numdevs * 57239211Sgibbs sizeof(struct device_selection)); 57339211Sgibbs *select_generation = current_generation; 57439211Sgibbs init_selections = 1; 57539211Sgibbs changed = 1; 57639211Sgibbs /* 57739211Sgibbs * In this case, we have selected devices before, but the device 57839211Sgibbs * list has changed since we last selected devices, so we need to 57939211Sgibbs * either enlarge or reduce the size of the device selection list. 58039211Sgibbs */ 58139211Sgibbs } else if (*num_selections != numdevs) { 582229735Sghelmer *dev_select = (struct device_selection *)reallocf(*dev_select, 58339211Sgibbs numdevs * sizeof(struct device_selection)); 58439211Sgibbs *select_generation = current_generation; 58539211Sgibbs init_selections = 1; 58639211Sgibbs /* 58739211Sgibbs * In this case, we've selected devices before, and the selection 58839211Sgibbs * list is the same size as it was the last time, but the device 58939211Sgibbs * list has changed. 59039211Sgibbs */ 59139211Sgibbs } else if (*select_generation < current_generation) { 59239211Sgibbs *select_generation = current_generation; 59339211Sgibbs init_selections = 1; 59439211Sgibbs } 59539211Sgibbs 596229735Sghelmer if (*dev_select == NULL) { 597229735Sghelmer snprintf(devstat_errbuf, sizeof(devstat_errbuf), 598229735Sghelmer "%s: Cannot (re)allocate memory for dev_select argument", 599229735Sghelmer __func__); 600229735Sghelmer return(-1); 601229735Sghelmer } 602229735Sghelmer 60339211Sgibbs /* 60439211Sgibbs * If we're in "only" mode, we want to clear out the selected 60539211Sgibbs * variable since we're going to select exactly what the user wants 60639211Sgibbs * this time through. 60739211Sgibbs */ 60839211Sgibbs if (select_mode == DS_SELECT_ONLY) 60939211Sgibbs init_selected_var = 1; 61039211Sgibbs 61139211Sgibbs /* 61239211Sgibbs * In all cases, we want to back up the number of selected devices. 61339211Sgibbs * It is a quick and accurate way to determine whether the selected 61439211Sgibbs * devices have changed. 61539211Sgibbs */ 61639211Sgibbs old_num_selected = *num_selected; 61739211Sgibbs 61839211Sgibbs /* 61939211Sgibbs * We want to make a backup of the current selection list if 62039211Sgibbs * the list of devices has changed, or if we're in performance 62139211Sgibbs * selection mode. In both cases, we don't want to make a backup 62239211Sgibbs * if we already know for sure that the list will be different. 62339211Sgibbs * This is certainly the case if this is our first time through the 62439211Sgibbs * selection code. 62539211Sgibbs */ 62639211Sgibbs if (((init_selected_var != 0) || (init_selections != 0) 62739211Sgibbs || (perf_select != 0)) && (changed == 0)){ 62839211Sgibbs old_dev_select = (struct device_selection *)malloc( 62939211Sgibbs *num_selections * sizeof(struct device_selection)); 630229735Sghelmer if (old_dev_select == NULL) { 631229735Sghelmer snprintf(devstat_errbuf, sizeof(devstat_errbuf), 632229735Sghelmer "%s: Cannot allocate memory for selection list backup", 633229735Sghelmer __func__); 634229735Sghelmer return(-1); 635229735Sghelmer } 63639211Sgibbs old_num_selections = *num_selections; 63739211Sgibbs bcopy(*dev_select, old_dev_select, 63839211Sgibbs sizeof(struct device_selection) * *num_selections); 63939211Sgibbs } 64039211Sgibbs 64139211Sgibbs if (init_selections != 0) { 64239211Sgibbs bzero(*dev_select, sizeof(struct device_selection) * numdevs); 64339211Sgibbs 64439211Sgibbs for (i = 0; i < numdevs; i++) { 64539211Sgibbs (*dev_select)[i].device_number = 64639211Sgibbs devices[i].device_number; 64739211Sgibbs strncpy((*dev_select)[i].device_name, 64839211Sgibbs devices[i].device_name, 64939211Sgibbs DEVSTAT_NAME_LEN); 65040364Sken (*dev_select)[i].device_name[DEVSTAT_NAME_LEN - 1]='\0'; 65139211Sgibbs (*dev_select)[i].unit_number = devices[i].unit_number; 65239211Sgibbs (*dev_select)[i].position = i; 65339211Sgibbs } 65439211Sgibbs *num_selections = numdevs; 65539211Sgibbs } else if (init_selected_var != 0) { 65639211Sgibbs for (i = 0; i < numdevs; i++) 65739211Sgibbs (*dev_select)[i].selected = 0; 65839211Sgibbs } 65939211Sgibbs 66039211Sgibbs /* we haven't gotten around to selecting anything yet.. */ 66139211Sgibbs if ((select_mode == DS_SELECT_ONLY) || (init_selections != 0) 66239211Sgibbs || (init_selected_var != 0)) 66339211Sgibbs *num_selected = 0; 66439211Sgibbs 66539211Sgibbs /* 66639211Sgibbs * Look through any devices the user specified on the command line 66739211Sgibbs * and see if they match known devices. If so, select them. 66839211Sgibbs */ 66939211Sgibbs for (i = 0; (i < *num_selections) && (num_dev_selections > 0); i++) { 67039211Sgibbs char tmpstr[80]; 67139211Sgibbs 67240364Sken snprintf(tmpstr, sizeof(tmpstr), "%s%d", 67382028Sken (*dev_select)[i].device_name, 67482028Sken (*dev_select)[i].unit_number); 67539211Sgibbs for (j = 0; j < num_dev_selections; j++) { 67639211Sgibbs if (strcmp(tmpstr, dev_selections[j]) == 0) { 67739211Sgibbs /* 67839211Sgibbs * Here we do different things based on the 67939211Sgibbs * mode we're in. If we're in add or 68039211Sgibbs * addonly mode, we only select this device 68139211Sgibbs * if it hasn't already been selected. 68239211Sgibbs * Otherwise, we would be unnecessarily 68339211Sgibbs * changing the selection order and 68439211Sgibbs * incrementing the selection count. If 68539211Sgibbs * we're in only mode, we unconditionally 68639211Sgibbs * select this device, since in only mode 68739211Sgibbs * any previous selections are erased and 68839211Sgibbs * manually specified devices are the first 68939211Sgibbs * ones to be selected. If we're in remove 69039211Sgibbs * mode, we de-select the specified device and 69139211Sgibbs * decrement the selection count. 69239211Sgibbs */ 69339211Sgibbs switch(select_mode) { 69439211Sgibbs case DS_SELECT_ADD: 69539211Sgibbs case DS_SELECT_ADDONLY: 69639211Sgibbs if ((*dev_select)[i].selected) 69739211Sgibbs break; 69839211Sgibbs /* FALLTHROUGH */ 69939211Sgibbs case DS_SELECT_ONLY: 70039211Sgibbs (*dev_select)[i].selected = 70139211Sgibbs ++selection_number; 70239211Sgibbs (*num_selected)++; 70339211Sgibbs break; 70439211Sgibbs case DS_SELECT_REMOVE: 70539211Sgibbs (*dev_select)[i].selected = 0; 70639211Sgibbs (*num_selected)--; 70739211Sgibbs /* 70839211Sgibbs * This isn't passed back out, we 70939211Sgibbs * just use it to keep track of 71039211Sgibbs * how many devices we've removed. 71139211Sgibbs */ 71239211Sgibbs num_dev_selections--; 71339211Sgibbs break; 71439211Sgibbs } 71539211Sgibbs break; 71639211Sgibbs } 71739211Sgibbs } 71839211Sgibbs } 71939211Sgibbs 72039211Sgibbs /* 72139211Sgibbs * Go through the user's device type expressions and select devices 72239211Sgibbs * accordingly. We only do this if the number of devices already 72339211Sgibbs * selected is less than the maximum number we can show. 72439211Sgibbs */ 72539211Sgibbs for (i = 0; (i < num_matches) && (*num_selected < maxshowdevs); i++) { 72639211Sgibbs /* We should probably indicate some error here */ 72739211Sgibbs if ((matches[i].match_fields == DEVSTAT_MATCH_NONE) 72839211Sgibbs || (matches[i].num_match_categories <= 0)) 72939211Sgibbs continue; 73039211Sgibbs 73139211Sgibbs for (j = 0; j < numdevs; j++) { 73239211Sgibbs int num_match_categories; 73339211Sgibbs 73439211Sgibbs num_match_categories = matches[i].num_match_categories; 73539211Sgibbs 73639211Sgibbs /* 73739211Sgibbs * Determine whether or not the current device 73839211Sgibbs * matches the given matching expression. This if 73939211Sgibbs * statement consists of three components: 74039211Sgibbs * - the device type check 74139211Sgibbs * - the device interface check 74239211Sgibbs * - the passthrough check 74339211Sgibbs * If a the matching test is successful, it 74439211Sgibbs * decrements the number of matching categories, 74539211Sgibbs * and if we've reached the last element that 74639211Sgibbs * needed to be matched, the if statement succeeds. 74739211Sgibbs * 74839211Sgibbs */ 74939211Sgibbs if ((((matches[i].match_fields & DEVSTAT_MATCH_TYPE)!=0) 75039211Sgibbs && ((devices[j].device_type & DEVSTAT_TYPE_MASK) == 75139211Sgibbs (matches[i].device_type & DEVSTAT_TYPE_MASK)) 75239211Sgibbs &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0) 75339211Sgibbs || (((matches[i].match_fields & 75439211Sgibbs DEVSTAT_MATCH_PASS) == 0) 75539211Sgibbs && ((devices[j].device_type & 75639211Sgibbs DEVSTAT_TYPE_PASS) == 0))) 75739211Sgibbs && (--num_match_categories == 0)) 75839211Sgibbs || (((matches[i].match_fields & DEVSTAT_MATCH_IF) != 0) 75939211Sgibbs && ((devices[j].device_type & DEVSTAT_TYPE_IF_MASK) == 76039211Sgibbs (matches[i].device_type & DEVSTAT_TYPE_IF_MASK)) 76139211Sgibbs &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0) 76239211Sgibbs || (((matches[i].match_fields & 76339211Sgibbs DEVSTAT_MATCH_PASS) == 0) 76439211Sgibbs && ((devices[j].device_type & 76539211Sgibbs DEVSTAT_TYPE_PASS) == 0))) 76639211Sgibbs && (--num_match_categories == 0)) 76739211Sgibbs || (((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0) 76839211Sgibbs && ((devices[j].device_type & DEVSTAT_TYPE_PASS) != 0) 76939211Sgibbs && (--num_match_categories == 0))) { 77039211Sgibbs 77139211Sgibbs /* 77239211Sgibbs * This is probably a non-optimal solution 77339211Sgibbs * to the problem that the devices in the 77439211Sgibbs * device list will not be in the same 77539211Sgibbs * order as the devices in the selection 77639211Sgibbs * array. 77739211Sgibbs */ 77839211Sgibbs for (k = 0; k < numdevs; k++) { 77939211Sgibbs if ((*dev_select)[k].position == j) { 78039211Sgibbs found = 1; 78139211Sgibbs break; 78239211Sgibbs } 78339211Sgibbs } 78439211Sgibbs 78539211Sgibbs /* 78639211Sgibbs * There shouldn't be a case where a device 78739211Sgibbs * in the device list is not in the 78839211Sgibbs * selection list...but it could happen. 78939211Sgibbs */ 79039211Sgibbs if (found != 1) { 79139211Sgibbs fprintf(stderr, "selectdevs: couldn't" 79239211Sgibbs " find %s%d in selection " 79339211Sgibbs "list\n", 79439211Sgibbs devices[j].device_name, 79539211Sgibbs devices[j].unit_number); 79639211Sgibbs break; 79739211Sgibbs } 79839211Sgibbs 79939211Sgibbs /* 80039211Sgibbs * We do different things based upon the 80139211Sgibbs * mode we're in. If we're in add or only 80239211Sgibbs * mode, we go ahead and select this device 80339211Sgibbs * if it hasn't already been selected. If 80439211Sgibbs * it has already been selected, we leave 80539211Sgibbs * it alone so we don't mess up the 80639211Sgibbs * selection ordering. Manually specified 80739211Sgibbs * devices have already been selected, and 80839211Sgibbs * they have higher priority than pattern 80939211Sgibbs * matched devices. If we're in remove 81039211Sgibbs * mode, we de-select the given device and 81139211Sgibbs * decrement the selected count. 81239211Sgibbs */ 81339211Sgibbs switch(select_mode) { 81439211Sgibbs case DS_SELECT_ADD: 81539211Sgibbs case DS_SELECT_ADDONLY: 81639211Sgibbs case DS_SELECT_ONLY: 81739211Sgibbs if ((*dev_select)[k].selected != 0) 81839211Sgibbs break; 81939211Sgibbs (*dev_select)[k].selected = 82039211Sgibbs ++selection_number; 82139211Sgibbs (*num_selected)++; 82239211Sgibbs break; 82339211Sgibbs case DS_SELECT_REMOVE: 82439211Sgibbs (*dev_select)[k].selected = 0; 82539211Sgibbs (*num_selected)--; 82639211Sgibbs break; 82739211Sgibbs } 82839211Sgibbs } 82939211Sgibbs } 83039211Sgibbs } 83139211Sgibbs 83239211Sgibbs /* 83339211Sgibbs * Here we implement "top" mode. Devices are sorted in the 83439211Sgibbs * selection array based on two criteria: whether or not they are 83539211Sgibbs * selected (not selection number, just the fact that they are 83639211Sgibbs * selected!) and the number of bytes in the "bytes" field of the 83739211Sgibbs * selection structure. The bytes field generally must be kept up 83839211Sgibbs * by the user. In the future, it may be maintained by library 83939211Sgibbs * functions, but for now the user has to do the work. 84039211Sgibbs * 84139211Sgibbs * At first glance, it may seem wrong that we don't go through and 84239211Sgibbs * select every device in the case where the user hasn't specified 84339211Sgibbs * any devices or patterns. In fact, though, it won't make any 84439211Sgibbs * difference in the device sorting. In that particular case (i.e. 84539211Sgibbs * when we're in "add" or "only" mode, and the user hasn't 84639211Sgibbs * specified anything) the first time through no devices will be 84739211Sgibbs * selected, so the only criterion used to sort them will be their 84839211Sgibbs * performance. The second time through, and every time thereafter, 84939211Sgibbs * all devices will be selected, so again selection won't matter. 85039211Sgibbs */ 85139211Sgibbs if (perf_select != 0) { 85239211Sgibbs 85339211Sgibbs /* Sort the device array by throughput */ 85439211Sgibbs qsort(*dev_select, *num_selections, 85539211Sgibbs sizeof(struct device_selection), 85639211Sgibbs compare_select); 85739211Sgibbs 85839211Sgibbs if (*num_selected == 0) { 85939211Sgibbs /* 86039211Sgibbs * Here we select every device in the array, if it 86139211Sgibbs * isn't already selected. Because the 'selected' 86239211Sgibbs * variable in the selection array entries contains 86339211Sgibbs * the selection order, the devstats routine can show 86439211Sgibbs * the devices that were selected first. 86539211Sgibbs */ 86639211Sgibbs for (i = 0; i < *num_selections; i++) { 86739211Sgibbs if ((*dev_select)[i].selected == 0) { 86839211Sgibbs (*dev_select)[i].selected = 86939211Sgibbs ++selection_number; 87039211Sgibbs (*num_selected)++; 87139211Sgibbs } 87239211Sgibbs } 87339211Sgibbs } else { 87439211Sgibbs selection_number = 0; 87539211Sgibbs for (i = 0; i < *num_selections; i++) { 87639211Sgibbs if ((*dev_select)[i].selected != 0) { 87739211Sgibbs (*dev_select)[i].selected = 87839211Sgibbs ++selection_number; 87939211Sgibbs } 88039211Sgibbs } 88139211Sgibbs } 88239211Sgibbs } 88339211Sgibbs 88439211Sgibbs /* 88539211Sgibbs * If we're in the "add" selection mode and if we haven't already 88639211Sgibbs * selected maxshowdevs number of devices, go through the array and 88739211Sgibbs * select any unselected devices. If we're in "only" mode, we 88839211Sgibbs * obviously don't want to select anything other than what the user 88939211Sgibbs * specifies. If we're in "remove" mode, it probably isn't a good 89039211Sgibbs * idea to go through and select any more devices, since we might 89139211Sgibbs * end up selecting something that the user wants removed. Through 89239211Sgibbs * more complicated logic, we could actually figure this out, but 89339211Sgibbs * that would probably require combining this loop with the various 89439211Sgibbs * selections loops above. 89539211Sgibbs */ 89639211Sgibbs if ((select_mode == DS_SELECT_ADD) && (*num_selected < maxshowdevs)) { 89739211Sgibbs for (i = 0; i < *num_selections; i++) 89839211Sgibbs if ((*dev_select)[i].selected == 0) { 89939211Sgibbs (*dev_select)[i].selected = ++selection_number; 90039211Sgibbs (*num_selected)++; 90139211Sgibbs } 90239211Sgibbs } 90339211Sgibbs 90439211Sgibbs /* 90539211Sgibbs * Look at the number of devices that have been selected. If it 90639211Sgibbs * has changed, set the changed variable. Otherwise, if we've 90739211Sgibbs * made a backup of the selection list, compare it to the current 90839211Sgibbs * selection list to see if the selected devices have changed. 90939211Sgibbs */ 91039211Sgibbs if ((changed == 0) && (old_num_selected != *num_selected)) 91139211Sgibbs changed = 1; 91239211Sgibbs else if ((changed == 0) && (old_dev_select != NULL)) { 91339211Sgibbs /* 91439211Sgibbs * Now we go through the selection list and we look at 91539211Sgibbs * it three different ways. 91639211Sgibbs */ 91739211Sgibbs for (i = 0; (i < *num_selections) && (changed == 0) && 91839211Sgibbs (i < old_num_selections); i++) { 91939211Sgibbs /* 92039211Sgibbs * If the device at index i in both the new and old 92139211Sgibbs * selection arrays has the same device number and 92239211Sgibbs * selection status, it hasn't changed. We 92339211Sgibbs * continue on to the next index. 92439211Sgibbs */ 92539211Sgibbs if (((*dev_select)[i].device_number == 92639211Sgibbs old_dev_select[i].device_number) 92739211Sgibbs && ((*dev_select)[i].selected == 92839211Sgibbs old_dev_select[i].selected)) 92939211Sgibbs continue; 93039211Sgibbs 93139211Sgibbs /* 93239211Sgibbs * Now, if we're still going through the if 93339211Sgibbs * statement, the above test wasn't true. So we 93439211Sgibbs * check here to see if the device at index i in 93539211Sgibbs * the current array is the same as the device at 93639211Sgibbs * index i in the old array. If it is, that means 93739211Sgibbs * that its selection number has changed. Set 93839211Sgibbs * changed to 1 and exit the loop. 93939211Sgibbs */ 94039211Sgibbs else if ((*dev_select)[i].device_number == 94139211Sgibbs old_dev_select[i].device_number) { 94239211Sgibbs changed = 1; 94339211Sgibbs break; 94439211Sgibbs } 94539211Sgibbs /* 94639211Sgibbs * If we get here, then the device at index i in 94739211Sgibbs * the current array isn't the same device as the 94839211Sgibbs * device at index i in the old array. 94939211Sgibbs */ 95039211Sgibbs else { 95183868Sken found = 0; 95239211Sgibbs 95339211Sgibbs /* 95439211Sgibbs * Search through the old selection array 95539211Sgibbs * looking for a device with the same 95639211Sgibbs * device number as the device at index i 95739211Sgibbs * in the current array. If the selection 95839211Sgibbs * status is the same, then we mark it as 95939211Sgibbs * found. If the selection status isn't 96039211Sgibbs * the same, we break out of the loop. 96139211Sgibbs * Since found isn't set, changed will be 96239211Sgibbs * set to 1 below. 96339211Sgibbs */ 96439211Sgibbs for (j = 0; j < old_num_selections; j++) { 96539211Sgibbs if (((*dev_select)[i].device_number == 96639211Sgibbs old_dev_select[j].device_number) 96739211Sgibbs && ((*dev_select)[i].selected == 96839211Sgibbs old_dev_select[j].selected)){ 96939211Sgibbs found = 1; 97039211Sgibbs break; 97139211Sgibbs } 97239211Sgibbs else if ((*dev_select)[i].device_number 97339211Sgibbs == old_dev_select[j].device_number) 97439211Sgibbs break; 97539211Sgibbs } 97639211Sgibbs if (found == 0) 97739211Sgibbs changed = 1; 97839211Sgibbs } 97939211Sgibbs } 98039211Sgibbs } 98139211Sgibbs if (old_dev_select != NULL) 98239211Sgibbs free(old_dev_select); 98339211Sgibbs 98439211Sgibbs return(changed); 98539211Sgibbs} 98639211Sgibbs 98739211Sgibbs/* 98839211Sgibbs * Comparison routine for qsort() above. Note that the comparison here is 98939211Sgibbs * backwards -- generally, it should return a value to indicate whether 99039211Sgibbs * arg1 is <, =, or > arg2. Instead, it returns the opposite. The reason 99139211Sgibbs * it returns the opposite is so that the selection array will be sorted in 99239211Sgibbs * order of decreasing performance. We sort on two parameters. The first 99339211Sgibbs * sort key is whether or not one or the other of the devices in question 99439211Sgibbs * has been selected. If one of them has, and the other one has not, the 99539211Sgibbs * selected device is automatically more important than the unselected 99639211Sgibbs * device. If neither device is selected, we judge the devices based upon 99739211Sgibbs * performance. 99839211Sgibbs */ 99939211Sgibbsstatic int 100039211Sgibbscompare_select(const void *arg1, const void *arg2) 100139211Sgibbs{ 100283868Sken if ((((const struct device_selection *)arg1)->selected) 100383868Sken && (((const struct device_selection *)arg2)->selected == 0)) 100439211Sgibbs return(-1); 100583868Sken else if ((((const struct device_selection *)arg1)->selected == 0) 100683868Sken && (((const struct device_selection *)arg2)->selected)) 100739211Sgibbs return(1); 100883868Sken else if (((const struct device_selection *)arg2)->bytes < 100983868Sken ((const struct device_selection *)arg1)->bytes) 101039211Sgibbs return(-1); 101183868Sken else if (((const struct device_selection *)arg2)->bytes > 101283868Sken ((const struct device_selection *)arg1)->bytes) 101339211Sgibbs return(1); 101439211Sgibbs else 101539211Sgibbs return(0); 101639211Sgibbs} 101739211Sgibbs 101839211Sgibbs/* 101939211Sgibbs * Take a string with the general format "arg1,arg2,arg3", and build a 102039211Sgibbs * device matching expression from it. 102139211Sgibbs */ 102239211Sgibbsint 102381133Stmmdevstat_buildmatch(char *match_str, struct devstat_match **matches, 102481133Stmm int *num_matches) 102539211Sgibbs{ 102639211Sgibbs char *tstr[5]; 102739211Sgibbs char **tempstr; 102839211Sgibbs int num_args; 102992913Sobrien int i, j; 103039211Sgibbs 103139211Sgibbs /* We can't do much without a string to parse */ 103239211Sgibbs if (match_str == NULL) { 103381133Stmm snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1034145553Smux "%s: no match expression", __func__); 103539211Sgibbs return(-1); 103639211Sgibbs } 103739211Sgibbs 103839211Sgibbs /* 103939211Sgibbs * Break the (comma delimited) input string out into separate strings. 104039211Sgibbs */ 104139211Sgibbs for (tempstr = tstr, num_args = 0; 1042220319Spluknet (*tempstr = strsep(&match_str, ",")) != NULL && (num_args < 5);) 1043220319Spluknet if (**tempstr != '\0') { 1044220319Spluknet num_args++; 104539211Sgibbs if (++tempstr >= &tstr[5]) 104639211Sgibbs break; 1047220319Spluknet } 104839211Sgibbs 104939211Sgibbs /* The user gave us too many type arguments */ 105039211Sgibbs if (num_args > 3) { 105181133Stmm snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1052145553Smux "%s: too many type arguments", __func__); 105339211Sgibbs return(-1); 105439211Sgibbs } 105539211Sgibbs 105639211Sgibbs if (*num_matches == 0) 1057229735Sghelmer *matches = NULL; 1058229735Sghelmer 1059229735Sghelmer *matches = (struct devstat_match *)reallocf(*matches, 1060229735Sghelmer sizeof(struct devstat_match) * (*num_matches + 1)); 1061229735Sghelmer 1062229735Sghelmer if (*matches == NULL) { 1063229735Sghelmer snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1064229735Sghelmer "%s: Cannot allocate memory for matches list", __func__); 1065229735Sghelmer return(-1); 1066229735Sghelmer } 106739211Sgibbs 106839211Sgibbs /* Make sure the current entry is clear */ 106939211Sgibbs bzero(&matches[0][*num_matches], sizeof(struct devstat_match)); 107039211Sgibbs 107139211Sgibbs /* 107239211Sgibbs * Step through the arguments the user gave us and build a device 107339211Sgibbs * matching expression from them. 107439211Sgibbs */ 107539211Sgibbs for (i = 0; i < num_args; i++) { 107639211Sgibbs char *tempstr2, *tempstr3; 107739211Sgibbs 107839211Sgibbs /* 107939211Sgibbs * Get rid of leading white space. 108039211Sgibbs */ 108139211Sgibbs tempstr2 = tstr[i]; 108239211Sgibbs while (isspace(*tempstr2) && (*tempstr2 != '\0')) 108339211Sgibbs tempstr2++; 108439211Sgibbs 108539211Sgibbs /* 108639211Sgibbs * Get rid of trailing white space. 108739211Sgibbs */ 108839211Sgibbs tempstr3 = &tempstr2[strlen(tempstr2) - 1]; 108939211Sgibbs 109039211Sgibbs while ((*tempstr3 != '\0') && (tempstr3 > tempstr2) 109139211Sgibbs && (isspace(*tempstr3))) { 109239211Sgibbs *tempstr3 = '\0'; 109339211Sgibbs tempstr3--; 109439211Sgibbs } 109539211Sgibbs 109639211Sgibbs /* 109739211Sgibbs * Go through the match table comparing the user's 109839211Sgibbs * arguments to known device types, interfaces, etc. 109939211Sgibbs */ 110039211Sgibbs for (j = 0; match_table[j].match_str != NULL; j++) { 110139211Sgibbs /* 110239211Sgibbs * We do case-insensitive matching, in case someone 110339211Sgibbs * wants to enter "SCSI" instead of "scsi" or 110439211Sgibbs * something like that. Only compare as many 110539211Sgibbs * characters as are in the string in the match 110639211Sgibbs * table. This should help if someone tries to use 110739211Sgibbs * a super-long match expression. 110839211Sgibbs */ 110939211Sgibbs if (strncasecmp(tempstr2, match_table[j].match_str, 111039211Sgibbs strlen(match_table[j].match_str)) == 0) { 111139211Sgibbs /* 111239211Sgibbs * Make sure the user hasn't specified two 111339211Sgibbs * items of the same type, like "da" and 111439211Sgibbs * "cd". One device cannot be both. 111539211Sgibbs */ 111639211Sgibbs if (((*matches)[*num_matches].match_fields & 111739211Sgibbs match_table[j].match_field) != 0) { 111881133Stmm snprintf(devstat_errbuf, 111982028Sken sizeof(devstat_errbuf), 112082028Sken "%s: cannot have more than " 112182028Sken "one match item in a single " 1122145553Smux "category", __func__); 112339211Sgibbs return(-1); 112439211Sgibbs } 112539211Sgibbs /* 112639211Sgibbs * If we've gotten this far, we have a 112739211Sgibbs * winner. Set the appropriate fields in 112839211Sgibbs * the match entry. 112939211Sgibbs */ 113039211Sgibbs (*matches)[*num_matches].match_fields |= 113139211Sgibbs match_table[j].match_field; 113239211Sgibbs (*matches)[*num_matches].device_type |= 113339211Sgibbs match_table[j].type; 113439211Sgibbs (*matches)[*num_matches].num_match_categories++; 113539211Sgibbs break; 113639211Sgibbs } 113739211Sgibbs } 113839211Sgibbs /* 113939211Sgibbs * We should have found a match in the above for loop. If 114039211Sgibbs * not, that means the user entered an invalid device type 114139211Sgibbs * or interface. 114239211Sgibbs */ 114339211Sgibbs if ((*matches)[*num_matches].num_match_categories != (i + 1)) { 114440364Sken snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1145145553Smux "%s: unknown match item \"%s\"", __func__, 114682028Sken tstr[i]); 114739211Sgibbs return(-1); 114839211Sgibbs } 114939211Sgibbs } 115039211Sgibbs 115139211Sgibbs (*num_matches)++; 115239211Sgibbs 115339211Sgibbs return(0); 115439211Sgibbs} 115539211Sgibbs 115639211Sgibbs/* 115739211Sgibbs * Compute a number of device statistics. Only one field is mandatory, and 115839211Sgibbs * that is "current". Everything else is optional. The caller passes in 115939211Sgibbs * pointers to variables to hold the various statistics he desires. If he 116039211Sgibbs * doesn't want a particular staistic, he should pass in a NULL pointer. 116139211Sgibbs * Return values: 116239211Sgibbs * 0 -- success 116339211Sgibbs * -1 -- failure 116439211Sgibbs */ 116539211Sgibbsint 116639211Sgibbscompute_stats(struct devstat *current, struct devstat *previous, 116739211Sgibbs long double etime, u_int64_t *total_bytes, 116839211Sgibbs u_int64_t *total_transfers, u_int64_t *total_blocks, 116939211Sgibbs long double *kb_per_transfer, long double *transfers_per_second, 117039211Sgibbs long double *mb_per_second, long double *blocks_per_second, 117139211Sgibbs long double *ms_per_transaction) 117239211Sgibbs{ 117381883Sken return(devstat_compute_statistics(current, previous, etime, 117481883Sken total_bytes ? DSM_TOTAL_BYTES : DSM_SKIP, 117581883Sken total_bytes, 117681883Sken total_transfers ? DSM_TOTAL_TRANSFERS : DSM_SKIP, 117781883Sken total_transfers, 117881883Sken total_blocks ? DSM_TOTAL_BLOCKS : DSM_SKIP, 117981883Sken total_blocks, 118081883Sken kb_per_transfer ? DSM_KB_PER_TRANSFER : DSM_SKIP, 118181883Sken kb_per_transfer, 118281883Sken transfers_per_second ? DSM_TRANSFERS_PER_SECOND : DSM_SKIP, 118381883Sken transfers_per_second, 118481883Sken mb_per_second ? DSM_MB_PER_SECOND : DSM_SKIP, 118581883Sken mb_per_second, 118681883Sken blocks_per_second ? DSM_BLOCKS_PER_SECOND : DSM_SKIP, 118781883Sken blocks_per_second, 118881883Sken ms_per_transaction ? DSM_MS_PER_TRANSACTION : DSM_SKIP, 118981883Sken ms_per_transaction, 119081883Sken DSM_NONE)); 119139211Sgibbs} 119239211Sgibbs 1193112288Sphk 1194112288Sphk/* This is 1/2^64 */ 1195112288Sphk#define BINTIME_SCALE 5.42101086242752217003726400434970855712890625e-20 1196112288Sphk 119739211Sgibbslong double 1198112288Sphkdevstat_compute_etime(struct bintime *cur_time, struct bintime *prev_time) 119939211Sgibbs{ 120039211Sgibbs long double etime; 120139211Sgibbs 1202112288Sphk etime = cur_time->sec; 1203112288Sphk etime += cur_time->frac * BINTIME_SCALE; 1204112288Sphk if (prev_time != NULL) { 1205112288Sphk etime -= prev_time->sec; 1206112288Sphk etime -= prev_time->frac * BINTIME_SCALE; 1207112288Sphk } 120839211Sgibbs return(etime); 120939211Sgibbs} 121081133Stmm 1211112373Sphk#define DELTA(field, index) \ 1212112373Sphk (current->field[(index)] - (previous ? previous->field[(index)] : 0)) 1213112373Sphk 1214112373Sphk#define DELTA_T(field) \ 1215112373Sphk devstat_compute_etime(¤t->field, \ 1216112373Sphk (previous ? &previous->field : NULL)) 1217112373Sphk 121881133Stmmint 121981133Stmmdevstat_compute_statistics(struct devstat *current, struct devstat *previous, 122081133Stmm long double etime, ...) 122181133Stmm{ 1222112373Sphk u_int64_t totalbytes, totalbytesread, totalbyteswrite, totalbytesfree; 122381133Stmm u_int64_t totaltransfers, totaltransfersread, totaltransferswrite; 122481133Stmm u_int64_t totaltransfersother, totalblocks, totalblocksread; 1225112373Sphk u_int64_t totalblockswrite, totaltransfersfree, totalblocksfree; 1226244270Strociny long double totalduration, totaldurationread, totaldurationwrite; 1227244270Strociny long double totaldurationfree, totaldurationother; 122881133Stmm va_list ap; 122981133Stmm devstat_metric metric; 123081133Stmm u_int64_t *destu64; 123181133Stmm long double *destld; 1232244270Strociny int retval; 123381133Stmm 123481133Stmm retval = 0; 123581133Stmm 123681133Stmm /* 123781133Stmm * current is the only mandatory field. 123881133Stmm */ 123981133Stmm if (current == NULL) { 124081133Stmm snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1241145553Smux "%s: current stats structure was NULL", __func__); 124281133Stmm return(-1); 124381133Stmm } 124481133Stmm 1245112373Sphk totalbytesread = DELTA(bytes, DEVSTAT_READ); 1246112373Sphk totalbyteswrite = DELTA(bytes, DEVSTAT_WRITE); 1247112373Sphk totalbytesfree = DELTA(bytes, DEVSTAT_FREE); 1248112373Sphk totalbytes = totalbytesread + totalbyteswrite + totalbytesfree; 124981133Stmm 1250112373Sphk totaltransfersread = DELTA(operations, DEVSTAT_READ); 1251112373Sphk totaltransferswrite = DELTA(operations, DEVSTAT_WRITE); 1252112373Sphk totaltransfersother = DELTA(operations, DEVSTAT_NO_DATA); 1253112373Sphk totaltransfersfree = DELTA(operations, DEVSTAT_FREE); 125481133Stmm totaltransfers = totaltransfersread + totaltransferswrite + 1255112373Sphk totaltransfersother + totaltransfersfree; 125681133Stmm 125781133Stmm totalblocks = totalbytes; 125881133Stmm totalblocksread = totalbytesread; 125981133Stmm totalblockswrite = totalbyteswrite; 1260112373Sphk totalblocksfree = totalbytesfree; 126181133Stmm 126281133Stmm if (current->block_size > 0) { 126381133Stmm totalblocks /= current->block_size; 126481133Stmm totalblocksread /= current->block_size; 126581133Stmm totalblockswrite /= current->block_size; 1266112373Sphk totalblocksfree /= current->block_size; 126781133Stmm } else { 126881133Stmm totalblocks /= 512; 126981133Stmm totalblocksread /= 512; 127081133Stmm totalblockswrite /= 512; 1271112373Sphk totalblocksfree /= 512; 127281133Stmm } 127381133Stmm 1274244270Strociny totaldurationread = DELTA_T(duration[DEVSTAT_READ]); 1275244270Strociny totaldurationwrite = DELTA_T(duration[DEVSTAT_WRITE]); 1276244270Strociny totaldurationfree = DELTA_T(duration[DEVSTAT_FREE]); 1277244270Strociny totaldurationother = DELTA_T(duration[DEVSTAT_NO_DATA]); 1278244270Strociny totalduration = totaldurationread + totaldurationwrite + 1279244270Strociny totaldurationfree + totaldurationother; 1280244270Strociny 128181133Stmm va_start(ap, etime); 128281133Stmm 128381133Stmm while ((metric = (devstat_metric)va_arg(ap, devstat_metric)) != 0) { 128481133Stmm 128581133Stmm if (metric == DSM_NONE) 128681133Stmm break; 128781133Stmm 128881133Stmm if (metric >= DSM_MAX) { 128981133Stmm snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1290145553Smux "%s: metric %d is out of range", __func__, 129181133Stmm metric); 129281133Stmm retval = -1; 129381133Stmm goto bailout; 129481133Stmm } 129581133Stmm 129681133Stmm switch (devstat_arg_list[metric].argtype) { 129781133Stmm case DEVSTAT_ARG_UINT64: 129881133Stmm destu64 = (u_int64_t *)va_arg(ap, u_int64_t *); 129981133Stmm break; 130081133Stmm case DEVSTAT_ARG_LD: 130181133Stmm destld = (long double *)va_arg(ap, long double *); 130281133Stmm break; 130381883Sken case DEVSTAT_ARG_SKIP: 130481883Sken destld = (long double *)va_arg(ap, long double *); 130581883Sken break; 130681133Stmm default: 130781133Stmm retval = -1; 130881133Stmm goto bailout; 130981133Stmm break; /* NOTREACHED */ 131081133Stmm } 131181133Stmm 131281883Sken if (devstat_arg_list[metric].argtype == DEVSTAT_ARG_SKIP) 131381883Sken continue; 131481883Sken 131581133Stmm switch (metric) { 131681133Stmm case DSM_TOTAL_BYTES: 131781133Stmm *destu64 = totalbytes; 131881133Stmm break; 131981133Stmm case DSM_TOTAL_BYTES_READ: 132081133Stmm *destu64 = totalbytesread; 132181133Stmm break; 132281133Stmm case DSM_TOTAL_BYTES_WRITE: 132381133Stmm *destu64 = totalbyteswrite; 132481133Stmm break; 1325112373Sphk case DSM_TOTAL_BYTES_FREE: 1326112373Sphk *destu64 = totalbytesfree; 1327112373Sphk break; 132881133Stmm case DSM_TOTAL_TRANSFERS: 132981133Stmm *destu64 = totaltransfers; 133081133Stmm break; 133181133Stmm case DSM_TOTAL_TRANSFERS_READ: 133281133Stmm *destu64 = totaltransfersread; 133381133Stmm break; 133481133Stmm case DSM_TOTAL_TRANSFERS_WRITE: 133581133Stmm *destu64 = totaltransferswrite; 133681133Stmm break; 1337112373Sphk case DSM_TOTAL_TRANSFERS_FREE: 1338112373Sphk *destu64 = totaltransfersfree; 1339112373Sphk break; 134081133Stmm case DSM_TOTAL_TRANSFERS_OTHER: 134181133Stmm *destu64 = totaltransfersother; 134281133Stmm break; 134381133Stmm case DSM_TOTAL_BLOCKS: 134481133Stmm *destu64 = totalblocks; 134581133Stmm break; 134681133Stmm case DSM_TOTAL_BLOCKS_READ: 134781133Stmm *destu64 = totalblocksread; 134881133Stmm break; 134981133Stmm case DSM_TOTAL_BLOCKS_WRITE: 135081133Stmm *destu64 = totalblockswrite; 135181133Stmm break; 1352112373Sphk case DSM_TOTAL_BLOCKS_FREE: 1353112373Sphk *destu64 = totalblocksfree; 1354112373Sphk break; 135581133Stmm case DSM_KB_PER_TRANSFER: 135681133Stmm *destld = totalbytes; 135781133Stmm *destld /= 1024; 135881133Stmm if (totaltransfers > 0) 135981133Stmm *destld /= totaltransfers; 136081133Stmm else 136181133Stmm *destld = 0.0; 136281133Stmm break; 136381133Stmm case DSM_KB_PER_TRANSFER_READ: 136481133Stmm *destld = totalbytesread; 136581133Stmm *destld /= 1024; 136681133Stmm if (totaltransfersread > 0) 136781133Stmm *destld /= totaltransfersread; 136881133Stmm else 136981133Stmm *destld = 0.0; 137081133Stmm break; 137181133Stmm case DSM_KB_PER_TRANSFER_WRITE: 137281133Stmm *destld = totalbyteswrite; 137381133Stmm *destld /= 1024; 137481133Stmm if (totaltransferswrite > 0) 137581133Stmm *destld /= totaltransferswrite; 137681133Stmm else 137781133Stmm *destld = 0.0; 137881133Stmm break; 1379112373Sphk case DSM_KB_PER_TRANSFER_FREE: 1380112373Sphk *destld = totalbytesfree; 1381112373Sphk *destld /= 1024; 1382112373Sphk if (totaltransfersfree > 0) 1383112373Sphk *destld /= totaltransfersfree; 1384112373Sphk else 1385112373Sphk *destld = 0.0; 1386112373Sphk break; 138781133Stmm case DSM_TRANSFERS_PER_SECOND: 138881133Stmm if (etime > 0.0) { 138981133Stmm *destld = totaltransfers; 139081133Stmm *destld /= etime; 139181133Stmm } else 139281133Stmm *destld = 0.0; 139381133Stmm break; 139481133Stmm case DSM_TRANSFERS_PER_SECOND_READ: 139581133Stmm if (etime > 0.0) { 139681133Stmm *destld = totaltransfersread; 139781133Stmm *destld /= etime; 139881133Stmm } else 139981133Stmm *destld = 0.0; 140081133Stmm break; 140181133Stmm case DSM_TRANSFERS_PER_SECOND_WRITE: 140281133Stmm if (etime > 0.0) { 140381133Stmm *destld = totaltransferswrite; 140481133Stmm *destld /= etime; 140581133Stmm } else 140681133Stmm *destld = 0.0; 140781133Stmm break; 1408112373Sphk case DSM_TRANSFERS_PER_SECOND_FREE: 1409112373Sphk if (etime > 0.0) { 1410112373Sphk *destld = totaltransfersfree; 1411112373Sphk *destld /= etime; 1412112373Sphk } else 1413112373Sphk *destld = 0.0; 1414112373Sphk break; 141581133Stmm case DSM_TRANSFERS_PER_SECOND_OTHER: 141681133Stmm if (etime > 0.0) { 141781133Stmm *destld = totaltransfersother; 141881133Stmm *destld /= etime; 141981133Stmm } else 142081133Stmm *destld = 0.0; 142181133Stmm break; 142281133Stmm case DSM_MB_PER_SECOND: 142381133Stmm *destld = totalbytes; 142481133Stmm *destld /= 1024 * 1024; 142581133Stmm if (etime > 0.0) 142681133Stmm *destld /= etime; 142781133Stmm else 142881133Stmm *destld = 0.0; 142981133Stmm break; 143081133Stmm case DSM_MB_PER_SECOND_READ: 143181133Stmm *destld = totalbytesread; 143281133Stmm *destld /= 1024 * 1024; 143381133Stmm if (etime > 0.0) 143481133Stmm *destld /= etime; 143581133Stmm else 143681133Stmm *destld = 0.0; 143781133Stmm break; 143881133Stmm case DSM_MB_PER_SECOND_WRITE: 143981133Stmm *destld = totalbyteswrite; 144081133Stmm *destld /= 1024 * 1024; 144181133Stmm if (etime > 0.0) 144281133Stmm *destld /= etime; 144381133Stmm else 144481133Stmm *destld = 0.0; 144581133Stmm break; 1446112373Sphk case DSM_MB_PER_SECOND_FREE: 1447112373Sphk *destld = totalbytesfree; 1448112373Sphk *destld /= 1024 * 1024; 1449112373Sphk if (etime > 0.0) 1450112373Sphk *destld /= etime; 1451112373Sphk else 1452112373Sphk *destld = 0.0; 1453112373Sphk break; 145481133Stmm case DSM_BLOCKS_PER_SECOND: 145581133Stmm *destld = totalblocks; 145681133Stmm if (etime > 0.0) 145781133Stmm *destld /= etime; 145881133Stmm else 145981133Stmm *destld = 0.0; 146081133Stmm break; 146181133Stmm case DSM_BLOCKS_PER_SECOND_READ: 146281133Stmm *destld = totalblocksread; 146381133Stmm if (etime > 0.0) 146481133Stmm *destld /= etime; 146581133Stmm else 146681133Stmm *destld = 0.0; 146781133Stmm break; 146881133Stmm case DSM_BLOCKS_PER_SECOND_WRITE: 146981133Stmm *destld = totalblockswrite; 147081133Stmm if (etime > 0.0) 147181133Stmm *destld /= etime; 147281133Stmm else 147381133Stmm *destld = 0.0; 147481133Stmm break; 1475112373Sphk case DSM_BLOCKS_PER_SECOND_FREE: 1476112373Sphk *destld = totalblocksfree; 1477112373Sphk if (etime > 0.0) 1478112373Sphk *destld /= etime; 1479112373Sphk else 1480112373Sphk *destld = 0.0; 1481112373Sphk break; 148281133Stmm /* 148381133Stmm * This calculation is somewhat bogus. It simply divides 148481133Stmm * the elapsed time by the total number of transactions 148581133Stmm * completed. While that does give the caller a good 148681133Stmm * picture of the average rate of transaction completion, 148781133Stmm * it doesn't necessarily give the caller a good view of 148881133Stmm * how long transactions took to complete on average. 148981133Stmm * Those two numbers will be different for a device that 149081133Stmm * can handle more than one transaction at a time. e.g. 149181133Stmm * SCSI disks doing tagged queueing. 149281133Stmm * 149381133Stmm * The only way to accurately determine the real average 149481133Stmm * time per transaction would be to compute and store the 149581133Stmm * time on a per-transaction basis. That currently isn't 149681133Stmm * done in the kernel, and would only be desireable if it 149781133Stmm * could be implemented in a somewhat non-intrusive and high 149881133Stmm * performance way. 149981133Stmm */ 150081133Stmm case DSM_MS_PER_TRANSACTION: 150181133Stmm if (totaltransfers > 0) { 1502244270Strociny *destld = totalduration; 150381133Stmm *destld /= totaltransfers; 150481133Stmm *destld *= 1000; 150581133Stmm } else 150681133Stmm *destld = 0.0; 150781133Stmm break; 150881133Stmm /* 150981133Stmm * As above, these next two really only give the average 151081133Stmm * rate of completion for read and write transactions, not 151181133Stmm * the average time the transaction took to complete. 151281133Stmm */ 151381133Stmm case DSM_MS_PER_TRANSACTION_READ: 151481133Stmm if (totaltransfersread > 0) { 1515244270Strociny *destld = totaldurationread; 151681133Stmm *destld /= totaltransfersread; 151781133Stmm *destld *= 1000; 151881133Stmm } else 151981133Stmm *destld = 0.0; 152081133Stmm break; 152181133Stmm case DSM_MS_PER_TRANSACTION_WRITE: 152281133Stmm if (totaltransferswrite > 0) { 1523244270Strociny *destld = totaldurationwrite; 152481133Stmm *destld /= totaltransferswrite; 152581133Stmm *destld *= 1000; 152681133Stmm } else 152781133Stmm *destld = 0.0; 152881133Stmm break; 1529112373Sphk case DSM_MS_PER_TRANSACTION_FREE: 1530112373Sphk if (totaltransfersfree > 0) { 1531244270Strociny *destld = totaldurationfree; 1532112373Sphk *destld /= totaltransfersfree; 1533112373Sphk *destld *= 1000; 1534112373Sphk } else 1535112373Sphk *destld = 0.0; 1536112373Sphk break; 1537112373Sphk case DSM_MS_PER_TRANSACTION_OTHER: 1538112373Sphk if (totaltransfersother > 0) { 1539244270Strociny *destld = totaldurationother; 1540112373Sphk *destld /= totaltransfersother; 1541112373Sphk *destld *= 1000; 1542112373Sphk } else 1543112373Sphk *destld = 0.0; 1544112373Sphk break; 1545112373Sphk case DSM_BUSY_PCT: 1546112373Sphk *destld = DELTA_T(busy_time); 1547112373Sphk if (*destld < 0) 1548112373Sphk *destld = 0; 1549112373Sphk *destld /= etime; 1550112373Sphk *destld *= 100; 1551113281Sphk if (*destld < 0) 1552113281Sphk *destld = 0; 1553112373Sphk break; 1554112373Sphk case DSM_QUEUE_LENGTH: 1555112373Sphk *destu64 = current->start_count - current->end_count; 1556112373Sphk break; 1557244270Strociny case DSM_TOTAL_DURATION: 1558244270Strociny *destld = totalduration; 1559244270Strociny break; 1560244270Strociny case DSM_TOTAL_DURATION_READ: 1561244270Strociny *destld = totaldurationread; 1562244270Strociny break; 1563244270Strociny case DSM_TOTAL_DURATION_WRITE: 1564244270Strociny *destld = totaldurationwrite; 1565244270Strociny break; 1566244270Strociny case DSM_TOTAL_DURATION_FREE: 1567244270Strociny *destld = totaldurationfree; 1568244270Strociny break; 1569244270Strociny case DSM_TOTAL_DURATION_OTHER: 1570244270Strociny *destld = totaldurationother; 1571244270Strociny break; 1572244270Strociny case DSM_TOTAL_BUSY_TIME: 1573244270Strociny *destld = DELTA_T(busy_time); 1574244270Strociny break; 1575112373Sphk/* 1576112373Sphk * XXX: comment out the default block to see if any case's are missing. 1577112373Sphk */ 1578112373Sphk#if 1 157981133Stmm default: 158081133Stmm /* 158181133Stmm * This shouldn't happen, since we should have 158281133Stmm * caught any out of range metrics at the top of 158381133Stmm * the loop. 158481133Stmm */ 158581133Stmm snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1586145553Smux "%s: unknown metric %d", __func__, metric); 158781133Stmm retval = -1; 158881133Stmm goto bailout; 158981133Stmm break; /* NOTREACHED */ 1590112373Sphk#endif 159181133Stmm } 159281133Stmm } 159381133Stmm 159481133Stmmbailout: 159581133Stmm 159681133Stmm va_end(ap); 159781133Stmm return(retval); 159881133Stmm} 159981133Stmm 160081133Stmmstatic int 160181133Stmmreadkmem(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes) 160281133Stmm{ 160381133Stmm 160481133Stmm if (kvm_read(kd, addr, buf, nbytes) == -1) { 160581133Stmm snprintf(devstat_errbuf, sizeof(devstat_errbuf), 1606145553Smux "%s: error reading value (kvm_read): %s", __func__, 160781133Stmm kvm_geterr(kd)); 160881133Stmm return(-1); 160981133Stmm } 161081133Stmm return(0); 161181133Stmm} 161281133Stmm 161381133Stmmstatic int 161483868Skenreadkmem_nl(kvm_t *kd, const char *name, void *buf, size_t nbytes) 161581133Stmm{ 161683868Sken struct nlist nl[2]; 161781133Stmm 1618131075Skan nl[0].n_name = (char *)name; 161983868Sken nl[1].n_name = NULL; 162083868Sken 162181133Stmm if (kvm_nlist(kd, nl) == -1) { 162281133Stmm snprintf(devstat_errbuf, sizeof(devstat_errbuf), 162381133Stmm "%s: error getting name list (kvm_nlist): %s", 1624145553Smux __func__, kvm_geterr(kd)); 162581133Stmm return(-1); 162681133Stmm } 162781133Stmm return(readkmem(kd, nl[0].n_value, buf, nbytes)); 162881133Stmm} 162981133Stmm 163081133Stmm/* 163181133Stmm * This duplicates the functionality of the kernel sysctl handler for poking 163281133Stmm * through crash dumps. 163381133Stmm */ 163481133Stmmstatic char * 163581133Stmmget_devstat_kvm(kvm_t *kd) 163681133Stmm{ 1637150948Sstefanf int i, wp; 163881133Stmm long gen; 163981133Stmm struct devstat *nds; 164081133Stmm struct devstat ds; 164181133Stmm struct devstatlist dhead; 164281133Stmm int num_devs; 164381133Stmm char *rv = NULL; 164481133Stmm 1645112288Sphk if ((num_devs = devstat_getnumdevs(kd)) <= 0) 164681133Stmm return(NULL); 164781133Stmm if (KREADNL(kd, X_DEVICE_STATQ, dhead) == -1) 164881133Stmm return(NULL); 164981133Stmm 165081133Stmm nds = STAILQ_FIRST(&dhead); 165181133Stmm 165281133Stmm if ((rv = malloc(sizeof(gen))) == NULL) { 165381133Stmm snprintf(devstat_errbuf, sizeof(devstat_errbuf), 165481133Stmm "%s: out of memory (initial malloc failed)", 1655145553Smux __func__); 165681133Stmm return(NULL); 165781133Stmm } 1658112288Sphk gen = devstat_getgeneration(kd); 165981133Stmm memcpy(rv, &gen, sizeof(gen)); 166081133Stmm wp = sizeof(gen); 166181133Stmm /* 166281133Stmm * Now push out all the devices. 166381133Stmm */ 166481133Stmm for (i = 0; (nds != NULL) && (i < num_devs); 166581133Stmm nds = STAILQ_NEXT(nds, dev_links), i++) { 166681133Stmm if (readkmem(kd, (long)nds, &ds, sizeof(ds)) == -1) { 166781133Stmm free(rv); 166881133Stmm return(NULL); 166981133Stmm } 167081133Stmm nds = &ds; 167181133Stmm rv = (char *)reallocf(rv, sizeof(gen) + 167281133Stmm sizeof(ds) * (i + 1)); 167381133Stmm if (rv == NULL) { 167481133Stmm snprintf(devstat_errbuf, sizeof(devstat_errbuf), 167581133Stmm "%s: out of memory (malloc failed)", 1676145553Smux __func__); 167781133Stmm return(NULL); 167881133Stmm } 167981133Stmm memcpy(rv + wp, &ds, sizeof(ds)); 168081133Stmm wp += sizeof(ds); 168181133Stmm } 168281133Stmm return(rv); 168381133Stmm} 1684