18d2fbde5SJustin T. Gibbs /* 25dc445bfSKenneth D. Merry * Copyright (c) 1997, 1998, 2000, 2001 Kenneth D. Merry 38d2fbde5SJustin T. Gibbs * All rights reserved. 48d2fbde5SJustin T. Gibbs * 58d2fbde5SJustin T. Gibbs * Redistribution and use in source and binary forms, with or without 68d2fbde5SJustin T. Gibbs * modification, are permitted provided that the following conditions 78d2fbde5SJustin T. Gibbs * are met: 88d2fbde5SJustin T. Gibbs * 1. Redistributions of source code must retain the above copyright 98d2fbde5SJustin T. Gibbs * notice, this list of conditions and the following disclaimer. 108d2fbde5SJustin T. Gibbs * 2. Redistributions in binary form must reproduce the above copyright 118d2fbde5SJustin T. Gibbs * notice, this list of conditions and the following disclaimer in the 128d2fbde5SJustin T. Gibbs * documentation and/or other materials provided with the distribution. 138d2fbde5SJustin T. Gibbs * 3. The name of the author may not be used to endorse or promote products 148d2fbde5SJustin T. Gibbs * derived from this software without specific prior written permission. 158d2fbde5SJustin T. Gibbs * 168d2fbde5SJustin T. Gibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 178d2fbde5SJustin T. Gibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 188d2fbde5SJustin T. Gibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 198d2fbde5SJustin T. Gibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 208d2fbde5SJustin T. Gibbs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 218d2fbde5SJustin T. Gibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 228d2fbde5SJustin T. Gibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 238d2fbde5SJustin T. Gibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 248d2fbde5SJustin T. Gibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 258d2fbde5SJustin T. Gibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 268d2fbde5SJustin T. Gibbs * SUCH DAMAGE. 278743d3dcSPhilippe Charnier * 288743d3dcSPhilippe Charnier * $FreeBSD$ 298d2fbde5SJustin T. Gibbs */ 308d2fbde5SJustin T. Gibbs /* 318d2fbde5SJustin T. Gibbs * Parts of this program are derived from the original FreeBSD iostat 328d2fbde5SJustin T. Gibbs * program: 338d2fbde5SJustin T. Gibbs */ 34dea673e9SRodney W. Grimes /*- 35dea673e9SRodney W. Grimes * Copyright (c) 1986, 1991, 1993 36dea673e9SRodney W. Grimes * The Regents of the University of California. All rights reserved. 37dea673e9SRodney W. Grimes * 38dea673e9SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 39dea673e9SRodney W. Grimes * modification, are permitted provided that the following conditions 40dea673e9SRodney W. Grimes * are met: 41dea673e9SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 42dea673e9SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 43dea673e9SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 44dea673e9SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 45dea673e9SRodney W. Grimes * documentation and/or other materials provided with the distribution. 46dea673e9SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 47dea673e9SRodney W. Grimes * must display the following acknowledgement: 48dea673e9SRodney W. Grimes * This product includes software developed by the University of 49dea673e9SRodney W. Grimes * California, Berkeley and its contributors. 50dea673e9SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 51dea673e9SRodney W. Grimes * may be used to endorse or promote products derived from this software 52dea673e9SRodney W. Grimes * without specific prior written permission. 53dea673e9SRodney W. Grimes * 54dea673e9SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55dea673e9SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56dea673e9SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57dea673e9SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58dea673e9SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59dea673e9SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60dea673e9SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61dea673e9SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62dea673e9SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63dea673e9SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64dea673e9SRodney W. Grimes * SUCH DAMAGE. 65dea673e9SRodney W. Grimes */ 668d2fbde5SJustin T. Gibbs /* 678d2fbde5SJustin T. Gibbs * Ideas for the new iostat statistics output modes taken from the NetBSD 688d2fbde5SJustin T. Gibbs * version of iostat: 698d2fbde5SJustin T. Gibbs */ 708d2fbde5SJustin T. Gibbs /* 718d2fbde5SJustin T. Gibbs * Copyright (c) 1996 John M. Vinopal 728d2fbde5SJustin T. Gibbs * All rights reserved. 738d2fbde5SJustin T. Gibbs * 748d2fbde5SJustin T. Gibbs * Redistribution and use in source and binary forms, with or without 758d2fbde5SJustin T. Gibbs * modification, are permitted provided that the following conditions 768d2fbde5SJustin T. Gibbs * are met: 778d2fbde5SJustin T. Gibbs * 1. Redistributions of source code must retain the above copyright 788d2fbde5SJustin T. Gibbs * notice, this list of conditions and the following disclaimer. 798d2fbde5SJustin T. Gibbs * 2. Redistributions in binary form must reproduce the above copyright 808d2fbde5SJustin T. Gibbs * notice, this list of conditions and the following disclaimer in the 818d2fbde5SJustin T. Gibbs * documentation and/or other materials provided with the distribution. 828d2fbde5SJustin T. Gibbs * 3. All advertising materials mentioning features or use of this software 838d2fbde5SJustin T. Gibbs * must display the following acknowledgement: 848d2fbde5SJustin T. Gibbs * This product includes software developed for the NetBSD Project 858d2fbde5SJustin T. Gibbs * by John M. Vinopal. 868d2fbde5SJustin T. Gibbs * 4. The name of the author may not be used to endorse or promote products 878d2fbde5SJustin T. Gibbs * derived from this software without specific prior written permission. 888d2fbde5SJustin T. Gibbs * 898d2fbde5SJustin T. Gibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 908d2fbde5SJustin T. Gibbs * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 918d2fbde5SJustin T. Gibbs * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 928d2fbde5SJustin T. Gibbs * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 938d2fbde5SJustin T. Gibbs * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 948d2fbde5SJustin T. Gibbs * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 958d2fbde5SJustin T. Gibbs * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 968d2fbde5SJustin T. Gibbs * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 978d2fbde5SJustin T. Gibbs * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 988d2fbde5SJustin T. Gibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 998d2fbde5SJustin T. Gibbs * SUCH DAMAGE. 1008d2fbde5SJustin T. Gibbs */ 101dea673e9SRodney W. Grimes 102dea673e9SRodney W. Grimes 103dea673e9SRodney W. Grimes #include <sys/param.h> 1048d2fbde5SJustin T. Gibbs #include <sys/errno.h> 105dea673e9SRodney W. Grimes #include <sys/dkstat.h> 1065fc5ae5cSThomas Moestl #include <sys/sysctl.h> 107dea673e9SRodney W. Grimes 108dea673e9SRodney W. Grimes #include <err.h> 109dea673e9SRodney W. Grimes #include <ctype.h> 110dea673e9SRodney W. Grimes #include <fcntl.h> 111dea673e9SRodney W. Grimes #include <kvm.h> 112e65cded7SKris Kennaway #include <signal.h> 113dea673e9SRodney W. Grimes #include <stdio.h> 114dea673e9SRodney W. Grimes #include <stdlib.h> 115dea673e9SRodney W. Grimes #include <string.h> 116dea673e9SRodney W. Grimes #include <unistd.h> 1178d2fbde5SJustin T. Gibbs #include <limits.h> 1188d2fbde5SJustin T. Gibbs #include <devstat.h> 1195dc445bfSKenneth D. Merry #include <math.h> 120dea673e9SRodney W. Grimes 121dea673e9SRodney W. Grimes struct nlist namelist[] = { 1228d2fbde5SJustin T. Gibbs #define X_TK_NIN 0 123dea673e9SRodney W. Grimes { "_tk_nin" }, 1248d2fbde5SJustin T. Gibbs #define X_TK_NOUT 1 125dea673e9SRodney W. Grimes { "_tk_nout" }, 1268d2fbde5SJustin T. Gibbs #define X_CP_TIME 2 127dea673e9SRodney W. Grimes { "_cp_time" }, 1285dc445bfSKenneth D. Merry #define X_BOOTTIME 3 1295dc445bfSKenneth D. Merry { "_boottime" }, 1305dc445bfSKenneth D. Merry #define X_END 3 131dea673e9SRodney W. Grimes { NULL }, 132dea673e9SRodney W. Grimes }; 133dea673e9SRodney W. Grimes 1348d2fbde5SJustin T. Gibbs struct statinfo cur, last; 1358d2fbde5SJustin T. Gibbs int num_devices; 1368d2fbde5SJustin T. Gibbs struct device_selection *dev_select; 1378d2fbde5SJustin T. Gibbs int maxshowdevs; 1382fa4aaa0SMatthew Dillon int dflag = 0, Iflag = 0, Cflag = 0, Tflag = 0, oflag = 0, Kflag = 0; 139e65cded7SKris Kennaway volatile sig_atomic_t phdr_flag = 0; 140dea673e9SRodney W. Grimes 1418d2fbde5SJustin T. Gibbs /* local function declarations */ 1428d2fbde5SJustin T. Gibbs static void usage(void); 1438d2fbde5SJustin T. Gibbs static void phdr(int signo); 144e65cded7SKris Kennaway static void do_phdr(); 1455dc445bfSKenneth D. Merry static void devstats(int perf_select, long double etime, int havelast); 1468d2fbde5SJustin T. Gibbs static void cpustats(void); 1475dc445bfSKenneth D. Merry static int readvar(kvm_t *kd, const char *name, int nlid, void *ptr, 1485dc445bfSKenneth D. Merry size_t len); 149dea673e9SRodney W. Grimes 1508d2fbde5SJustin T. Gibbs static void 1518d2fbde5SJustin T. Gibbs usage(void) 1528d2fbde5SJustin T. Gibbs { 1538d2fbde5SJustin T. Gibbs /* 1548d2fbde5SJustin T. Gibbs * We also support the following 'traditional' syntax: 1558d2fbde5SJustin T. Gibbs * iostat [drives] [wait [count]] 1568d2fbde5SJustin T. Gibbs * This isn't mentioned in the man page, or the usage statement, 1578d2fbde5SJustin T. Gibbs * but it is supported. 1588d2fbde5SJustin T. Gibbs */ 1598743d3dcSPhilippe Charnier fprintf(stderr, "usage: iostat [-CdhIKoT?] [-c count] [-M core]" 1608743d3dcSPhilippe Charnier " [-n devs] [-N system]\n" 1618743d3dcSPhilippe Charnier "\t [-t type,if,pass] [-w wait] [drives]\n"); 1628d2fbde5SJustin T. Gibbs } 163dea673e9SRodney W. Grimes 164dea673e9SRodney W. Grimes int 1658d2fbde5SJustin T. Gibbs main(int argc, char **argv) 166dea673e9SRodney W. Grimes { 1678d2fbde5SJustin T. Gibbs int c; 168dea673e9SRodney W. Grimes register int i; 1698d2fbde5SJustin T. Gibbs int tflag = 0, hflag = 0, cflag = 0, wflag = 0, nflag = 0; 1708d2fbde5SJustin T. Gibbs int count = 0, waittime = 0; 1718d2fbde5SJustin T. Gibbs char *memf = NULL, *nlistf = NULL; 1728d2fbde5SJustin T. Gibbs struct devstat_match *matches; 1738d2fbde5SJustin T. Gibbs int num_matches = 0; 174dea673e9SRodney W. Grimes char errbuf[_POSIX2_LINE_MAX]; 1755fc5ae5cSThomas Moestl kvm_t *kd = NULL; 1768d2fbde5SJustin T. Gibbs int headercount; 177bcc6a3daSKenneth D. Merry long generation; 1788d2fbde5SJustin T. Gibbs int num_devices_specified; 179bcc6a3daSKenneth D. Merry int num_selected, num_selections; 180bcc6a3daSKenneth D. Merry long select_generation; 1818d2fbde5SJustin T. Gibbs char **specified_devices; 1828d2fbde5SJustin T. Gibbs devstat_select_mode select_mode; 1835dc445bfSKenneth D. Merry int havelast = 0; 184dea673e9SRodney W. Grimes 1858d2fbde5SJustin T. Gibbs matches = NULL; 1868d2fbde5SJustin T. Gibbs maxshowdevs = 3; 1878d2fbde5SJustin T. Gibbs 1888743d3dcSPhilippe Charnier while ((c = getopt(argc, argv, "c:CdhIKM:n:N:ot:Tw:?")) != -1) { 1898d2fbde5SJustin T. Gibbs switch(c) { 190dea673e9SRodney W. Grimes case 'c': 1918d2fbde5SJustin T. Gibbs cflag++; 1928d2fbde5SJustin T. Gibbs count = atoi(optarg); 1938d2fbde5SJustin T. Gibbs if (count < 1) 1948d2fbde5SJustin T. Gibbs errx(1, "count %d is < 1", count); 1958d2fbde5SJustin T. Gibbs break; 1968d2fbde5SJustin T. Gibbs case 'C': 1978d2fbde5SJustin T. Gibbs Cflag++; 1988d2fbde5SJustin T. Gibbs break; 1998d2fbde5SJustin T. Gibbs case 'd': 2008d2fbde5SJustin T. Gibbs dflag++; 2018d2fbde5SJustin T. Gibbs break; 2028d2fbde5SJustin T. Gibbs case 'h': 2038d2fbde5SJustin T. Gibbs hflag++; 2048d2fbde5SJustin T. Gibbs break; 2058d2fbde5SJustin T. Gibbs case 'I': 2068d2fbde5SJustin T. Gibbs Iflag++; 207dea673e9SRodney W. Grimes break; 208e4d7fe02SKenneth D. Merry case 'K': 209e4d7fe02SKenneth D. Merry Kflag++; 210e4d7fe02SKenneth D. Merry break; 211dea673e9SRodney W. Grimes case 'M': 212dea673e9SRodney W. Grimes memf = optarg; 213dea673e9SRodney W. Grimes break; 2148d2fbde5SJustin T. Gibbs case 'n': 2158d2fbde5SJustin T. Gibbs nflag++; 2168d2fbde5SJustin T. Gibbs maxshowdevs = atoi(optarg); 2178d2fbde5SJustin T. Gibbs if (maxshowdevs < 0) 2185d4a8fa9SPhilippe Charnier errx(1, "number of devices %d is < 0", 2198d2fbde5SJustin T. Gibbs maxshowdevs); 2208d2fbde5SJustin T. Gibbs break; 221dea673e9SRodney W. Grimes case 'N': 222dea673e9SRodney W. Grimes nlistf = optarg; 223dea673e9SRodney W. Grimes break; 2248d2fbde5SJustin T. Gibbs case 'o': 2258d2fbde5SJustin T. Gibbs oflag++; 226dea673e9SRodney W. Grimes break; 2278d2fbde5SJustin T. Gibbs case 't': 2288d2fbde5SJustin T. Gibbs tflag++; 2295fc5ae5cSThomas Moestl if (devstat_buildmatch(optarg, &matches, 2308d2fbde5SJustin T. Gibbs &num_matches) != 0) 2318d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 2328d2fbde5SJustin T. Gibbs break; 2338d2fbde5SJustin T. Gibbs case 'T': 2348d2fbde5SJustin T. Gibbs Tflag++; 2358d2fbde5SJustin T. Gibbs break; 2368d2fbde5SJustin T. Gibbs case 'w': 2378d2fbde5SJustin T. Gibbs wflag++; 2388d2fbde5SJustin T. Gibbs waittime = atoi(optarg); 2398d2fbde5SJustin T. Gibbs if (waittime < 1) 2408d2fbde5SJustin T. Gibbs errx(1, "wait time is < 1"); 2418d2fbde5SJustin T. Gibbs break; 242dea673e9SRodney W. Grimes default: 243dea673e9SRodney W. Grimes usage(); 2448743d3dcSPhilippe Charnier exit(1); 2458d2fbde5SJustin T. Gibbs break; 246dea673e9SRodney W. Grimes } 2478d2fbde5SJustin T. Gibbs } 2488d2fbde5SJustin T. Gibbs 249dea673e9SRodney W. Grimes argc -= optind; 250dea673e9SRodney W. Grimes argv += optind; 251dea673e9SRodney W. Grimes 2525dc445bfSKenneth D. Merry if (nlistf != NULL || memf != NULL) { 2535fc5ae5cSThomas Moestl kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 2545fc5ae5cSThomas Moestl 2555dc445bfSKenneth D. Merry if (kd == NULL) 2565fc5ae5cSThomas Moestl errx(1, "kvm_openfiles: %s", errbuf); 2575fc5ae5cSThomas Moestl 2585fc5ae5cSThomas Moestl if (kvm_nlist(kd, namelist) == -1) 2595fc5ae5cSThomas Moestl errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 2605fc5ae5cSThomas Moestl } 261dea673e9SRodney W. Grimes 2628d2fbde5SJustin T. Gibbs /* 2638d2fbde5SJustin T. Gibbs * Make sure that the userland devstat version matches the kernel 2648d2fbde5SJustin T. Gibbs * devstat version. If not, exit and print a message informing 2658d2fbde5SJustin T. Gibbs * the user of his mistake. 2668d2fbde5SJustin T. Gibbs */ 2675fc5ae5cSThomas Moestl if (devstat_checkversion(kd) < 0) 2688d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 2698d2fbde5SJustin T. Gibbs 2708d2fbde5SJustin T. Gibbs /* 2715dc445bfSKenneth D. Merry * Make sure Tflag and/or Cflag are set if dflag == 0. If dflag is 2725dc445bfSKenneth D. Merry * greater than 0, they may be 0 or non-zero. 2735dc445bfSKenneth D. Merry */ 2745dc445bfSKenneth D. Merry if (dflag == 0) { 2755dc445bfSKenneth D. Merry Cflag = 1; 2765dc445bfSKenneth D. Merry Tflag = 1; 2775dc445bfSKenneth D. Merry } 2785dc445bfSKenneth D. Merry 2795dc445bfSKenneth D. Merry /* 2808d2fbde5SJustin T. Gibbs * Figure out how many devices we should display. 2818d2fbde5SJustin T. Gibbs */ 2828d2fbde5SJustin T. Gibbs if (nflag == 0) { 2838d2fbde5SJustin T. Gibbs if (oflag > 0) { 2848d2fbde5SJustin T. Gibbs if ((dflag > 0) && (Cflag == 0) && (Tflag == 0)) 2858d2fbde5SJustin T. Gibbs maxshowdevs = 5; 2868d2fbde5SJustin T. Gibbs else if ((dflag > 0) && (Tflag > 0) && (Cflag == 0)) 2878d2fbde5SJustin T. Gibbs maxshowdevs = 5; 2888d2fbde5SJustin T. Gibbs else 2898d2fbde5SJustin T. Gibbs maxshowdevs = 4; 2908d2fbde5SJustin T. Gibbs } else { 2918d2fbde5SJustin T. Gibbs if ((dflag > 0) && (Cflag == 0)) 2928d2fbde5SJustin T. Gibbs maxshowdevs = 4; 2938d2fbde5SJustin T. Gibbs else 2948d2fbde5SJustin T. Gibbs maxshowdevs = 3; 2958d2fbde5SJustin T. Gibbs } 2968d2fbde5SJustin T. Gibbs } 2978d2fbde5SJustin T. Gibbs 2988d2fbde5SJustin T. Gibbs /* find out how many devices we have */ 2995fc5ae5cSThomas Moestl if ((num_devices = devstat_getnumdevs(kd)) < 0) 3008d2fbde5SJustin T. Gibbs err(1, "can't get number of devices"); 3018d2fbde5SJustin T. Gibbs 3025dc445bfSKenneth D. Merry cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 3035dc445bfSKenneth D. Merry if (cur.dinfo == NULL) 3045dc445bfSKenneth D. Merry err(1, "malloc failed"); 3055dc445bfSKenneth D. Merry 3065dc445bfSKenneth D. Merry last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 3075dc445bfSKenneth D. Merry if (last.dinfo == NULL) 3085dc445bfSKenneth D. Merry err(1, "malloc failed"); 3095dc445bfSKenneth D. Merry 3108d2fbde5SJustin T. Gibbs bzero(cur.dinfo, sizeof(struct devinfo)); 3118d2fbde5SJustin T. Gibbs bzero(last.dinfo, sizeof(struct devinfo)); 3128d2fbde5SJustin T. Gibbs 3138d2fbde5SJustin T. Gibbs /* 3148d2fbde5SJustin T. Gibbs * Grab all the devices. We don't look to see if the list has 3158d2fbde5SJustin T. Gibbs * changed here, since it almost certainly has. We only look for 3168d2fbde5SJustin T. Gibbs * errors. 3178d2fbde5SJustin T. Gibbs */ 3185fc5ae5cSThomas Moestl if (devstat_getdevs(kd, &cur) == -1) 3198d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 3208d2fbde5SJustin T. Gibbs 3218d2fbde5SJustin T. Gibbs num_devices = cur.dinfo->numdevs; 3228d2fbde5SJustin T. Gibbs generation = cur.dinfo->generation; 3238d2fbde5SJustin T. Gibbs 3248d2fbde5SJustin T. Gibbs /* 3258d2fbde5SJustin T. Gibbs * If the user specified any devices on the command line, see if 3268d2fbde5SJustin T. Gibbs * they are in the list of devices we have now. 3278d2fbde5SJustin T. Gibbs */ 3285dc445bfSKenneth D. Merry specified_devices = (char **)malloc(sizeof(char *)); 3295dc445bfSKenneth D. Merry if (specified_devices == NULL) 3305dc445bfSKenneth D. Merry err(1, "malloc failed"); 3315dc445bfSKenneth D. Merry 3328d2fbde5SJustin T. Gibbs for (num_devices_specified = 0; *argv; ++argv) { 3338d2fbde5SJustin T. Gibbs if (isdigit(**argv)) 3348d2fbde5SJustin T. Gibbs break; 3358d2fbde5SJustin T. Gibbs num_devices_specified++; 3368d2fbde5SJustin T. Gibbs specified_devices = (char **)realloc(specified_devices, 3378d2fbde5SJustin T. Gibbs sizeof(char *) * 3388d2fbde5SJustin T. Gibbs num_devices_specified); 3395dc445bfSKenneth D. Merry if (specified_devices == NULL) 3405dc445bfSKenneth D. Merry err(1, "realloc failed"); 3415dc445bfSKenneth D. Merry 3428d2fbde5SJustin T. Gibbs specified_devices[num_devices_specified - 1] = *argv; 3438d2fbde5SJustin T. Gibbs 3448d2fbde5SJustin T. Gibbs } 3452fa4aaa0SMatthew Dillon if (nflag == 0 && maxshowdevs < num_devices_specified) 3462fa4aaa0SMatthew Dillon maxshowdevs = num_devices_specified; 3478d2fbde5SJustin T. Gibbs 3488d2fbde5SJustin T. Gibbs dev_select = NULL; 3498d2fbde5SJustin T. Gibbs 3508d2fbde5SJustin T. Gibbs if ((num_devices_specified == 0) && (num_matches == 0)) 3518d2fbde5SJustin T. Gibbs select_mode = DS_SELECT_ADD; 3528d2fbde5SJustin T. Gibbs else 3538d2fbde5SJustin T. Gibbs select_mode = DS_SELECT_ONLY; 3548d2fbde5SJustin T. Gibbs 3558d2fbde5SJustin T. Gibbs /* 3568d2fbde5SJustin T. Gibbs * At this point, selectdevs will almost surely indicate that the 3578d2fbde5SJustin T. Gibbs * device list has changed, so we don't look for return values of 0 3588d2fbde5SJustin T. Gibbs * or 1. If we get back -1, though, there is an error. 3598d2fbde5SJustin T. Gibbs */ 3605fc5ae5cSThomas Moestl if (devstat_selectdevs(&dev_select, &num_selected, 3615fc5ae5cSThomas Moestl &num_selections, &select_generation, generation, 3625fc5ae5cSThomas Moestl cur.dinfo->devices, num_devices, matches, 3635fc5ae5cSThomas Moestl num_matches, specified_devices, 3645fc5ae5cSThomas Moestl num_devices_specified, select_mode, maxshowdevs, 3655fc5ae5cSThomas Moestl hflag) == -1) 3668d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 3678d2fbde5SJustin T. Gibbs 3688d2fbde5SJustin T. Gibbs /* 3698d2fbde5SJustin T. Gibbs * Look for the traditional wait time and count arguments. 3708d2fbde5SJustin T. Gibbs */ 3718d2fbde5SJustin T. Gibbs if (*argv) { 3728d2fbde5SJustin T. Gibbs waittime = atoi(*argv); 3738d2fbde5SJustin T. Gibbs 3748d2fbde5SJustin T. Gibbs /* Let the user know he goofed, but keep going anyway */ 3758d2fbde5SJustin T. Gibbs if (wflag != 0) 3768d2fbde5SJustin T. Gibbs warnx("discarding previous wait interval, using" 3778d2fbde5SJustin T. Gibbs " %d instead", waittime); 3788d2fbde5SJustin T. Gibbs wflag++; 3798d2fbde5SJustin T. Gibbs 3808d2fbde5SJustin T. Gibbs if (*++argv) { 3818d2fbde5SJustin T. Gibbs count = atoi(*argv); 3828d2fbde5SJustin T. Gibbs if (cflag != 0) 3838d2fbde5SJustin T. Gibbs warnx("discarding previous count, using %d" 3848d2fbde5SJustin T. Gibbs " instead", count); 3858d2fbde5SJustin T. Gibbs cflag++; 3868d2fbde5SJustin T. Gibbs } else 3878d2fbde5SJustin T. Gibbs count = -1; 3888d2fbde5SJustin T. Gibbs } 3898d2fbde5SJustin T. Gibbs 3908d2fbde5SJustin T. Gibbs /* 3918d2fbde5SJustin T. Gibbs * If the user specified a count, but not an interval, we default 3928d2fbde5SJustin T. Gibbs * to an interval of 1 second. 3938d2fbde5SJustin T. Gibbs */ 3948d2fbde5SJustin T. Gibbs if ((wflag == 0) && (cflag > 0)) 3958d2fbde5SJustin T. Gibbs waittime = 1; 3968d2fbde5SJustin T. Gibbs 3978d2fbde5SJustin T. Gibbs /* 3988d2fbde5SJustin T. Gibbs * If the user specified a wait time, but not a count, we want to 3998d2fbde5SJustin T. Gibbs * go on ad infinitum. This can be redundant if the user uses the 4008d2fbde5SJustin T. Gibbs * traditional method of specifying the wait, since in that case we 4018d2fbde5SJustin T. Gibbs * already set count = -1 above. Oh well. 4028d2fbde5SJustin T. Gibbs */ 4038d2fbde5SJustin T. Gibbs if ((wflag > 0) && (cflag == 0)) 4048d2fbde5SJustin T. Gibbs count = -1; 4058d2fbde5SJustin T. Gibbs 4065dc445bfSKenneth D. Merry bzero(&cur.cp_time, sizeof(cur.cp_time)); 4075dc445bfSKenneth D. Merry cur.tk_nout = 0; 4085dc445bfSKenneth D. Merry cur.tk_nin = 0; 4095dc445bfSKenneth D. Merry 4105dc445bfSKenneth D. Merry /* 4115dc445bfSKenneth D. Merry * Set the busy time to the system boot time, so the stats are 4125dc445bfSKenneth D. Merry * calculated since system boot. 4135dc445bfSKenneth D. Merry */ 4145dc445bfSKenneth D. Merry if (readvar(kd, "kern.boottime", X_BOOTTIME, &cur.busy_time, 4155dc445bfSKenneth D. Merry sizeof(cur.busy_time)) != 0) 4165dc445bfSKenneth D. Merry exit(1); 4175dc445bfSKenneth D. Merry 418dea673e9SRodney W. Grimes /* 4198d2fbde5SJustin T. Gibbs * If the user stops the program (control-Z) and then resumes it, 4208d2fbde5SJustin T. Gibbs * print out the header again. 421dea673e9SRodney W. Grimes */ 422dea673e9SRodney W. Grimes (void)signal(SIGCONT, phdr); 423dea673e9SRodney W. Grimes 4248d2fbde5SJustin T. Gibbs for (headercount = 1;;) { 4258d2fbde5SJustin T. Gibbs struct devinfo *tmp_dinfo; 4268d2fbde5SJustin T. Gibbs long tmp; 4275dc445bfSKenneth D. Merry long double etime; 4285dc445bfSKenneth D. Merry 4295dc445bfSKenneth D. Merry if (Tflag > 0) { 4305dc445bfSKenneth D. Merry if ((readvar(kd, "kern.tty_nin", X_TK_NIN, &cur.tk_nin, 4315dc445bfSKenneth D. Merry sizeof(cur.tk_nin)) != 0) 4325dc445bfSKenneth D. Merry || (readvar(kd, "kern.tty_nout", X_TK_NOUT, 4335dc445bfSKenneth D. Merry &cur.tk_nout, sizeof(cur.tk_nout))!= 0)) { 4345dc445bfSKenneth D. Merry Tflag = 0; 4355dc445bfSKenneth D. Merry warnx("disabling TTY statistics"); 4365dc445bfSKenneth D. Merry } 4375dc445bfSKenneth D. Merry } 4385dc445bfSKenneth D. Merry 439e65cded7SKris Kennaway if (phdr_flag) { 440e65cded7SKris Kennaway phdr_flag = 0; 441e65cded7SKris Kennaway do_phdr(); 442e65cded7SKris Kennaway } 443e65cded7SKris Kennaway 4445dc445bfSKenneth D. Merry if (Cflag > 0) { 4455dc445bfSKenneth D. Merry if (readvar(kd, "kern.cp_time", X_CP_TIME, 4465dc445bfSKenneth D. Merry &cur.cp_time, sizeof(cur.cp_time)) != 0) { 4475dc445bfSKenneth D. Merry Cflag = 0; 4485dc445bfSKenneth D. Merry warnx("disabling CPU time statistics"); 4495dc445bfSKenneth D. Merry } 4505dc445bfSKenneth D. Merry } 4518d2fbde5SJustin T. Gibbs 4528d2fbde5SJustin T. Gibbs if (!--headercount) { 453e65cded7SKris Kennaway do_phdr(); 4548d2fbde5SJustin T. Gibbs headercount = 20; 455dea673e9SRodney W. Grimes } 4568d2fbde5SJustin T. Gibbs 4578d2fbde5SJustin T. Gibbs tmp_dinfo = last.dinfo; 4588d2fbde5SJustin T. Gibbs last.dinfo = cur.dinfo; 4598d2fbde5SJustin T. Gibbs cur.dinfo = tmp_dinfo; 4608d2fbde5SJustin T. Gibbs 4618d2fbde5SJustin T. Gibbs last.busy_time = cur.busy_time; 4628d2fbde5SJustin T. Gibbs 4638d2fbde5SJustin T. Gibbs /* 4648d2fbde5SJustin T. Gibbs * Here what we want to do is refresh our device stats. 4655fc5ae5cSThomas Moestl * devstat_getdevs() returns 1 when the device list has changed. 4668d2fbde5SJustin T. Gibbs * If the device list has changed, we want to go through 4678d2fbde5SJustin T. Gibbs * the selection process again, in case a device that we 4688d2fbde5SJustin T. Gibbs * were previously displaying has gone away. 4698d2fbde5SJustin T. Gibbs */ 4705fc5ae5cSThomas Moestl switch (devstat_getdevs(kd, &cur)) { 4718d2fbde5SJustin T. Gibbs case -1: 4728d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 4738d2fbde5SJustin T. Gibbs break; 4748d2fbde5SJustin T. Gibbs case 1: { 4758d2fbde5SJustin T. Gibbs int retval; 4768d2fbde5SJustin T. Gibbs 4778d2fbde5SJustin T. Gibbs num_devices = cur.dinfo->numdevs; 4788d2fbde5SJustin T. Gibbs generation = cur.dinfo->generation; 4795fc5ae5cSThomas Moestl retval = devstat_selectdevs(&dev_select, &num_selected, 4805fc5ae5cSThomas Moestl &num_selections, 4815fc5ae5cSThomas Moestl &select_generation, 4825fc5ae5cSThomas Moestl generation, 4835fc5ae5cSThomas Moestl cur.dinfo->devices, 4845fc5ae5cSThomas Moestl num_devices, matches, 4855fc5ae5cSThomas Moestl num_matches, 4868d2fbde5SJustin T. Gibbs specified_devices, 4878d2fbde5SJustin T. Gibbs num_devices_specified, 4885fc5ae5cSThomas Moestl select_mode, maxshowdevs, 4895fc5ae5cSThomas Moestl hflag); 4908d2fbde5SJustin T. Gibbs switch(retval) { 4918d2fbde5SJustin T. Gibbs case -1: 4928d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 4938d2fbde5SJustin T. Gibbs break; 4948d2fbde5SJustin T. Gibbs case 1: 495e65cded7SKris Kennaway do_phdr(); 4968d2fbde5SJustin T. Gibbs headercount = 20; 4978d2fbde5SJustin T. Gibbs break; 4988d2fbde5SJustin T. Gibbs default: 4998d2fbde5SJustin T. Gibbs break; 500dea673e9SRodney W. Grimes } 5018d2fbde5SJustin T. Gibbs break; 5028d2fbde5SJustin T. Gibbs } 5038d2fbde5SJustin T. Gibbs default: 5048d2fbde5SJustin T. Gibbs break; 5058d2fbde5SJustin T. Gibbs } 5068d2fbde5SJustin T. Gibbs 5078d2fbde5SJustin T. Gibbs /* 5088d2fbde5SJustin T. Gibbs * We only want to re-select devices if we're in 'top' 5098d2fbde5SJustin T. Gibbs * mode. This is the only mode where the devices selected 5108d2fbde5SJustin T. Gibbs * could actually change. 5118d2fbde5SJustin T. Gibbs */ 5128d2fbde5SJustin T. Gibbs if (hflag > 0) { 5138d2fbde5SJustin T. Gibbs int retval; 5145fc5ae5cSThomas Moestl retval = devstat_selectdevs(&dev_select, &num_selected, 5155fc5ae5cSThomas Moestl &num_selections, 5165fc5ae5cSThomas Moestl &select_generation, 5175fc5ae5cSThomas Moestl generation, 5185fc5ae5cSThomas Moestl cur.dinfo->devices, 5195fc5ae5cSThomas Moestl num_devices, matches, 5205fc5ae5cSThomas Moestl num_matches, 5218d2fbde5SJustin T. Gibbs specified_devices, 5228d2fbde5SJustin T. Gibbs num_devices_specified, 5235fc5ae5cSThomas Moestl select_mode, maxshowdevs, 5245fc5ae5cSThomas Moestl hflag); 5258d2fbde5SJustin T. Gibbs switch(retval) { 5268d2fbde5SJustin T. Gibbs case -1: 5278d2fbde5SJustin T. Gibbs errx(1,"%s", devstat_errbuf); 5288d2fbde5SJustin T. Gibbs break; 5298d2fbde5SJustin T. Gibbs case 1: 530e65cded7SKris Kennaway do_phdr(); 5318d2fbde5SJustin T. Gibbs headercount = 20; 5328d2fbde5SJustin T. Gibbs break; 5338d2fbde5SJustin T. Gibbs default: 5348d2fbde5SJustin T. Gibbs break; 5358d2fbde5SJustin T. Gibbs } 5368d2fbde5SJustin T. Gibbs } 5378d2fbde5SJustin T. Gibbs 5385dc445bfSKenneth D. Merry if (Tflag > 0) { 539dea673e9SRodney W. Grimes tmp = cur.tk_nin; 540dea673e9SRodney W. Grimes cur.tk_nin -= last.tk_nin; 541dea673e9SRodney W. Grimes last.tk_nin = tmp; 542dea673e9SRodney W. Grimes tmp = cur.tk_nout; 543dea673e9SRodney W. Grimes cur.tk_nout -= last.tk_nout; 544dea673e9SRodney W. Grimes last.tk_nout = tmp; 545dea673e9SRodney W. Grimes } 5465dc445bfSKenneth D. Merry 5475dc445bfSKenneth D. Merry etime = devstat_compute_etime(cur.busy_time, last.busy_time); 5485dc445bfSKenneth D. Merry 549dea673e9SRodney W. Grimes if (etime == 0.0) 550dea673e9SRodney W. Grimes etime = 1.0; 5515dc445bfSKenneth D. Merry 5525dc445bfSKenneth D. Merry for (i = 0; i < CPUSTATES; i++) { 5535dc445bfSKenneth D. Merry tmp = cur.cp_time[i]; 5545dc445bfSKenneth D. Merry cur.cp_time[i] -= last.cp_time[i]; 5555dc445bfSKenneth D. Merry last.cp_time[i] = tmp; 5565dc445bfSKenneth D. Merry } 5575dc445bfSKenneth D. Merry 5585dc445bfSKenneth D. Merry if (Tflag > 0) 5595dc445bfSKenneth D. Merry printf("%4.0Lf%5.0Lf", cur.tk_nin / etime, 5608d2fbde5SJustin T. Gibbs cur.tk_nout/etime); 5615dc445bfSKenneth D. Merry 5625fc5ae5cSThomas Moestl devstats(hflag, etime, havelast); 5635dc445bfSKenneth D. Merry 5645dc445bfSKenneth D. Merry if (Cflag > 0) 565dea673e9SRodney W. Grimes cpustats(); 5665dc445bfSKenneth D. Merry 5678d2fbde5SJustin T. Gibbs printf("\n"); 5688d2fbde5SJustin T. Gibbs fflush(stdout); 569dea673e9SRodney W. Grimes 5708d2fbde5SJustin T. Gibbs if (count >= 0 && --count <= 0) 571dea673e9SRodney W. Grimes break; 5728d2fbde5SJustin T. Gibbs 5738d2fbde5SJustin T. Gibbs sleep(waittime); 5745fc5ae5cSThomas Moestl havelast = 1; 575dea673e9SRodney W. Grimes } 5768d2fbde5SJustin T. Gibbs 577dea673e9SRodney W. Grimes exit(0); 578dea673e9SRodney W. Grimes } 579dea673e9SRodney W. Grimes 5808d2fbde5SJustin T. Gibbs static void 5818d2fbde5SJustin T. Gibbs phdr(int signo) 582dea673e9SRodney W. Grimes { 583e65cded7SKris Kennaway 584e65cded7SKris Kennaway phdr_flag = 1; 585e65cded7SKris Kennaway } 586e65cded7SKris Kennaway 587e65cded7SKris Kennaway static void 588e65cded7SKris Kennaway do_phdr() 589e65cded7SKris Kennaway { 590dea673e9SRodney W. Grimes register int i; 5918d2fbde5SJustin T. Gibbs int printed; 592dea673e9SRodney W. Grimes 5935dc445bfSKenneth D. Merry if (Tflag > 0) 594dea673e9SRodney W. Grimes (void)printf(" tty"); 5958d2fbde5SJustin T. Gibbs for (i = 0, printed=0;(i < num_devices) && (printed < maxshowdevs);i++){ 5968d2fbde5SJustin T. Gibbs int di; 5978d2fbde5SJustin T. Gibbs if ((dev_select[i].selected != 0) 5988d2fbde5SJustin T. Gibbs && (dev_select[i].selected <= maxshowdevs)) { 5998d2fbde5SJustin T. Gibbs di = dev_select[i].position; 6008d2fbde5SJustin T. Gibbs if (oflag > 0) 6018d2fbde5SJustin T. Gibbs (void)printf("%12.6s%d ", 6028d2fbde5SJustin T. Gibbs cur.dinfo->devices[di].device_name, 6038d2fbde5SJustin T. Gibbs cur.dinfo->devices[di].unit_number); 6048d2fbde5SJustin T. Gibbs else 6058d2fbde5SJustin T. Gibbs printf("%15.6s%d ", 6068d2fbde5SJustin T. Gibbs cur.dinfo->devices[di].device_name, 6078d2fbde5SJustin T. Gibbs cur.dinfo->devices[di].unit_number); 6088d2fbde5SJustin T. Gibbs printed++; 6098d2fbde5SJustin T. Gibbs } 6108d2fbde5SJustin T. Gibbs } 6115dc445bfSKenneth D. Merry if (Cflag > 0) 6128d2fbde5SJustin T. Gibbs (void)printf(" cpu\n"); 6138d2fbde5SJustin T. Gibbs else 6148d2fbde5SJustin T. Gibbs (void)printf("\n"); 6158d2fbde5SJustin T. Gibbs 6165dc445bfSKenneth D. Merry if (Tflag > 0) 6178d2fbde5SJustin T. Gibbs (void)printf(" tin tout"); 6188d2fbde5SJustin T. Gibbs 6198d2fbde5SJustin T. Gibbs for (i=0, printed = 0;(i < num_devices) && (printed < maxshowdevs);i++){ 6208d2fbde5SJustin T. Gibbs if ((dev_select[i].selected != 0) 6218d2fbde5SJustin T. Gibbs && (dev_select[i].selected <= maxshowdevs)) { 6222fa4aaa0SMatthew Dillon if (oflag > 0) { 6238d2fbde5SJustin T. Gibbs if (Iflag == 0) 624dea673e9SRodney W. Grimes (void)printf(" sps tps msps "); 6258d2fbde5SJustin T. Gibbs else 6268d2fbde5SJustin T. Gibbs (void)printf(" blk xfr msps "); 6272fa4aaa0SMatthew Dillon } else { 6288d2fbde5SJustin T. Gibbs if (Iflag == 0) 6298d2fbde5SJustin T. Gibbs printf(" KB/t tps MB/s "); 6308d2fbde5SJustin T. Gibbs else 6318d2fbde5SJustin T. Gibbs printf(" KB/t xfrs MB "); 6322fa4aaa0SMatthew Dillon } 6338d2fbde5SJustin T. Gibbs printed++; 6348d2fbde5SJustin T. Gibbs } 6358d2fbde5SJustin T. Gibbs } 6365dc445bfSKenneth D. Merry if (Cflag > 0) 637936d754aSAndrey A. Chernov (void)printf(" us ni sy in id\n"); 6388d2fbde5SJustin T. Gibbs else 6398d2fbde5SJustin T. Gibbs printf("\n"); 640dea673e9SRodney W. Grimes } 641dea673e9SRodney W. Grimes 6428d2fbde5SJustin T. Gibbs static void 6435dc445bfSKenneth D. Merry devstats(int perf_select, long double etime, int havelast) 644dea673e9SRodney W. Grimes { 645dea673e9SRodney W. Grimes register int dn; 6468d2fbde5SJustin T. Gibbs long double transfers_per_second; 6478d2fbde5SJustin T. Gibbs long double kb_per_transfer, mb_per_second; 6488d2fbde5SJustin T. Gibbs u_int64_t total_bytes, total_transfers, total_blocks; 6498d2fbde5SJustin T. Gibbs long double total_mb; 6508d2fbde5SJustin T. Gibbs long double blocks_per_second, ms_per_transaction; 651dea673e9SRodney W. Grimes 6528d2fbde5SJustin T. Gibbs for (dn = 0; dn < num_devices; dn++) { 6538d2fbde5SJustin T. Gibbs int di; 6548d2fbde5SJustin T. Gibbs 6558d2fbde5SJustin T. Gibbs if (((perf_select == 0) && (dev_select[dn].selected == 0)) 6568d2fbde5SJustin T. Gibbs || (dev_select[dn].selected > maxshowdevs)) 657dea673e9SRodney W. Grimes continue; 658dea673e9SRodney W. Grimes 6598d2fbde5SJustin T. Gibbs di = dev_select[dn].position; 660dea673e9SRodney W. Grimes 6615fc5ae5cSThomas Moestl if (devstat_compute_statistics(&cur.dinfo->devices[di], 6625dc445bfSKenneth D. Merry havelast ? &last.dinfo->devices[di] : NULL, etime, 6635fc5ae5cSThomas Moestl DSM_TOTAL_BYTES, &total_bytes, 6645fc5ae5cSThomas Moestl DSM_TOTAL_TRANSFERS, &total_transfers, 6655fc5ae5cSThomas Moestl DSM_TOTAL_BLOCKS, &total_blocks, 6665fc5ae5cSThomas Moestl DSM_KB_PER_TRANSFER, &kb_per_transfer, 6675fc5ae5cSThomas Moestl DSM_TRANSFERS_PER_SECOND, &transfers_per_second, 6685fc5ae5cSThomas Moestl DSM_MB_PER_SECOND, &mb_per_second, 6695fc5ae5cSThomas Moestl DSM_BLOCKS_PER_SECOND, &blocks_per_second, 6705fc5ae5cSThomas Moestl DSM_MS_PER_TRANSACTION, &ms_per_transaction, 6715fc5ae5cSThomas Moestl DSM_NONE) != 0) 6728d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 6738d2fbde5SJustin T. Gibbs 6748d2fbde5SJustin T. Gibbs if (perf_select != 0) { 6758d2fbde5SJustin T. Gibbs dev_select[dn].bytes = total_bytes; 6768d2fbde5SJustin T. Gibbs if ((dev_select[dn].selected == 0) 6778d2fbde5SJustin T. Gibbs || (dev_select[dn].selected > maxshowdevs)) 6788d2fbde5SJustin T. Gibbs continue; 6798d2fbde5SJustin T. Gibbs } 6808d2fbde5SJustin T. Gibbs 6812fa4aaa0SMatthew Dillon if (Kflag) { 6822fa4aaa0SMatthew Dillon int block_size = cur.dinfo->devices[di].block_size; 683e4d7fe02SKenneth D. Merry total_blocks = total_blocks * (block_size ? 684e4d7fe02SKenneth D. Merry block_size : 512) / 1024; 6852fa4aaa0SMatthew Dillon } 6862fa4aaa0SMatthew Dillon 6878d2fbde5SJustin T. Gibbs if (oflag > 0) { 6882fa4aaa0SMatthew Dillon int msdig = (ms_per_transaction < 100.0) ? 1 : 0; 6898d2fbde5SJustin T. Gibbs 6908d2fbde5SJustin T. Gibbs if (Iflag == 0) 6912fa4aaa0SMatthew Dillon printf("%4.0Lf%4.0Lf%5.*Lf ", 6928d2fbde5SJustin T. Gibbs blocks_per_second, 6938d2fbde5SJustin T. Gibbs transfers_per_second, 6942fa4aaa0SMatthew Dillon msdig, 6958d2fbde5SJustin T. Gibbs ms_per_transaction); 696dea673e9SRodney W. Grimes else 6972fa4aaa0SMatthew Dillon printf("%4.1qu%4.1qu%5.*Lf ", 6988d2fbde5SJustin T. Gibbs total_blocks, 6998d2fbde5SJustin T. Gibbs total_transfers, 7002fa4aaa0SMatthew Dillon msdig, 7018d2fbde5SJustin T. Gibbs ms_per_transaction); 7028d2fbde5SJustin T. Gibbs } else { 7038d2fbde5SJustin T. Gibbs if (Iflag == 0) 7048d2fbde5SJustin T. Gibbs printf(" %5.2Lf %3.0Lf %5.2Lf ", 7058d2fbde5SJustin T. Gibbs kb_per_transfer, 7068d2fbde5SJustin T. Gibbs transfers_per_second, 7078d2fbde5SJustin T. Gibbs mb_per_second); 7088d2fbde5SJustin T. Gibbs else { 7098d2fbde5SJustin T. Gibbs total_mb = total_bytes; 7108d2fbde5SJustin T. Gibbs total_mb /= 1024 * 1024; 7118d2fbde5SJustin T. Gibbs 7128d2fbde5SJustin T. Gibbs printf(" %5.2Lf %3.1qu %5.2Lf ", 7138d2fbde5SJustin T. Gibbs kb_per_transfer, 7148d2fbde5SJustin T. Gibbs total_transfers, 7158d2fbde5SJustin T. Gibbs total_mb); 7168d2fbde5SJustin T. Gibbs } 7178d2fbde5SJustin T. Gibbs } 718dea673e9SRodney W. Grimes } 719dea673e9SRodney W. Grimes } 720dea673e9SRodney W. Grimes 7218d2fbde5SJustin T. Gibbs static void 7228d2fbde5SJustin T. Gibbs cpustats(void) 723dea673e9SRodney W. Grimes { 724dea673e9SRodney W. Grimes register int state; 725dea673e9SRodney W. Grimes double time; 726dea673e9SRodney W. Grimes 7278d2fbde5SJustin T. Gibbs time = 0.0; 7288d2fbde5SJustin T. Gibbs 729dea673e9SRodney W. Grimes for (state = 0; state < CPUSTATES; ++state) 730dea673e9SRodney W. Grimes time += cur.cp_time[state]; 731dea673e9SRodney W. Grimes for (state = 0; state < CPUSTATES; ++state) 7328d2fbde5SJustin T. Gibbs printf("%3.0f", 7335dc445bfSKenneth D. Merry rint(100. * cur.cp_time[state] / (time ? time : 1))); 734dea673e9SRodney W. Grimes } 7355fc5ae5cSThomas Moestl 7365dc445bfSKenneth D. Merry static int 7375dc445bfSKenneth D. Merry readvar(kvm_t *kd, const char *name, int nlid, void *ptr, size_t len) 7385fc5ae5cSThomas Moestl { 7395dc445bfSKenneth D. Merry if (kd != NULL) { 7405dc445bfSKenneth D. Merry ssize_t nbytes; 7415dc445bfSKenneth D. Merry 7425dc445bfSKenneth D. Merry nbytes = kvm_read(kd, nlid, ptr, len); 7435dc445bfSKenneth D. Merry 7445dc445bfSKenneth D. Merry if (nbytes == 0) { 7455dc445bfSKenneth D. Merry warnx("kvm_read(%s): %s", name, kvm_geterr(kd)); 7465dc445bfSKenneth D. Merry return (1); 7475dc445bfSKenneth D. Merry } 7485dc445bfSKenneth D. Merry if (nbytes != len) { 7495dc445bfSKenneth D. Merry warnx("kvm_read(%s): expected %lu bytes, got %ld bytes", 7505dc445bfSKenneth D. Merry name, (unsigned long)len, (long)nbytes); 7515dc445bfSKenneth D. Merry return (1); 7525dc445bfSKenneth D. Merry } 7535dc445bfSKenneth D. Merry } else { 7545fc5ae5cSThomas Moestl size_t nlen = len; 7555fc5ae5cSThomas Moestl 7565dc445bfSKenneth D. Merry if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) { 7575dc445bfSKenneth D. Merry warn("sysctl(%s...) failed", name); 7585dc445bfSKenneth D. Merry return (1); 7595dc445bfSKenneth D. Merry } 7605dc445bfSKenneth D. Merry if (nlen != len) { 7615dc445bfSKenneth D. Merry warnx("sysctl(%s...): expected %lu, got %lu", name, 7625fc5ae5cSThomas Moestl (unsigned long)len, (unsigned long)nlen); 7635dc445bfSKenneth D. Merry return (1); 7645dc445bfSKenneth D. Merry } 7655dc445bfSKenneth D. Merry } 7665dc445bfSKenneth D. Merry return (0); 7675fc5ae5cSThomas Moestl } 768