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> 105190ff54eSPoul-Henning Kamp #include <sys/resource.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> 1122757068eSBruce Evans #include <nlist.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; 138775b0074SGuido van Rooij volatile sig_atomic_t headercount; 1392fa4aaa0SMatthew Dillon int dflag = 0, Iflag = 0, Cflag = 0, Tflag = 0, oflag = 0, Kflag = 0; 1406c359123SMaxim Konovalov int xflag = 0, zflag = 0; 141dea673e9SRodney W. Grimes 1428d2fbde5SJustin T. Gibbs /* local function declarations */ 1438d2fbde5SJustin T. Gibbs static void usage(void); 144775b0074SGuido van Rooij static void needhdr(int signo); 145775b0074SGuido van Rooij static void phdr(void); 1465dc445bfSKenneth D. Merry static void devstats(int perf_select, long double etime, int havelast); 1478d2fbde5SJustin T. Gibbs static void cpustats(void); 1485dc445bfSKenneth D. Merry static int readvar(kvm_t *kd, const char *name, int nlid, void *ptr, 1495dc445bfSKenneth D. Merry size_t len); 150dea673e9SRodney W. Grimes 1518d2fbde5SJustin T. Gibbs static void 1528d2fbde5SJustin T. Gibbs usage(void) 1538d2fbde5SJustin T. Gibbs { 1548d2fbde5SJustin T. Gibbs /* 1558d2fbde5SJustin T. Gibbs * We also support the following 'traditional' syntax: 1568d2fbde5SJustin T. Gibbs * iostat [drives] [wait [count]] 1578d2fbde5SJustin T. Gibbs * This isn't mentioned in the man page, or the usage statement, 1588d2fbde5SJustin T. Gibbs * but it is supported. 1598d2fbde5SJustin T. Gibbs */ 1606c359123SMaxim Konovalov fprintf(stderr, "usage: iostat [-CdhIKoTxz?] [-c count] [-M core]" 1618743d3dcSPhilippe Charnier " [-n devs] [-N system]\n" 1628743d3dcSPhilippe Charnier "\t [-t type,if,pass] [-w wait] [drives]\n"); 1638d2fbde5SJustin T. Gibbs } 164dea673e9SRodney W. Grimes 165dea673e9SRodney W. Grimes int 1668d2fbde5SJustin T. Gibbs main(int argc, char **argv) 167dea673e9SRodney W. Grimes { 16847e065b4SMaxim Konovalov int c, 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; 176bcc6a3daSKenneth D. Merry long generation; 1778d2fbde5SJustin T. Gibbs int num_devices_specified; 178bcc6a3daSKenneth D. Merry int num_selected, num_selections; 179bcc6a3daSKenneth D. Merry long select_generation; 1808d2fbde5SJustin T. Gibbs char **specified_devices; 1818d2fbde5SJustin T. Gibbs devstat_select_mode select_mode; 1825dc445bfSKenneth D. Merry int havelast = 0; 183dea673e9SRodney W. Grimes 1848d2fbde5SJustin T. Gibbs matches = NULL; 1858d2fbde5SJustin T. Gibbs maxshowdevs = 3; 1868d2fbde5SJustin T. Gibbs 1876c359123SMaxim Konovalov while ((c = getopt(argc, argv, "c:CdhIKM:n:N:ot:Tw:xz?")) != -1) { 1888d2fbde5SJustin T. Gibbs switch(c) { 189dea673e9SRodney W. Grimes case 'c': 1908d2fbde5SJustin T. Gibbs cflag++; 1918d2fbde5SJustin T. Gibbs count = atoi(optarg); 1928d2fbde5SJustin T. Gibbs if (count < 1) 1938d2fbde5SJustin T. Gibbs errx(1, "count %d is < 1", count); 1948d2fbde5SJustin T. Gibbs break; 1958d2fbde5SJustin T. Gibbs case 'C': 1968d2fbde5SJustin T. Gibbs Cflag++; 1978d2fbde5SJustin T. Gibbs break; 1988d2fbde5SJustin T. Gibbs case 'd': 1998d2fbde5SJustin T. Gibbs dflag++; 2008d2fbde5SJustin T. Gibbs break; 2018d2fbde5SJustin T. Gibbs case 'h': 2028d2fbde5SJustin T. Gibbs hflag++; 2038d2fbde5SJustin T. Gibbs break; 2048d2fbde5SJustin T. Gibbs case 'I': 2058d2fbde5SJustin T. Gibbs Iflag++; 206dea673e9SRodney W. Grimes break; 207e4d7fe02SKenneth D. Merry case 'K': 208e4d7fe02SKenneth D. Merry Kflag++; 209e4d7fe02SKenneth D. Merry break; 210dea673e9SRodney W. Grimes case 'M': 211dea673e9SRodney W. Grimes memf = optarg; 212dea673e9SRodney W. Grimes break; 2138d2fbde5SJustin T. Gibbs case 'n': 2148d2fbde5SJustin T. Gibbs nflag++; 2158d2fbde5SJustin T. Gibbs maxshowdevs = atoi(optarg); 2168d2fbde5SJustin T. Gibbs if (maxshowdevs < 0) 2175d4a8fa9SPhilippe Charnier errx(1, "number of devices %d is < 0", 2188d2fbde5SJustin T. Gibbs maxshowdevs); 2198d2fbde5SJustin T. Gibbs break; 220dea673e9SRodney W. Grimes case 'N': 221dea673e9SRodney W. Grimes nlistf = optarg; 222dea673e9SRodney W. Grimes break; 2238d2fbde5SJustin T. Gibbs case 'o': 2248d2fbde5SJustin T. Gibbs oflag++; 225dea673e9SRodney W. Grimes break; 2268d2fbde5SJustin T. Gibbs case 't': 2278d2fbde5SJustin T. Gibbs tflag++; 2285fc5ae5cSThomas Moestl if (devstat_buildmatch(optarg, &matches, 2298d2fbde5SJustin T. Gibbs &num_matches) != 0) 2308d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 2318d2fbde5SJustin T. Gibbs break; 2328d2fbde5SJustin T. Gibbs case 'T': 2338d2fbde5SJustin T. Gibbs Tflag++; 2348d2fbde5SJustin T. Gibbs break; 2358d2fbde5SJustin T. Gibbs case 'w': 2368d2fbde5SJustin T. Gibbs wflag++; 2378d2fbde5SJustin T. Gibbs waittime = atoi(optarg); 2388d2fbde5SJustin T. Gibbs if (waittime < 1) 2398d2fbde5SJustin T. Gibbs errx(1, "wait time is < 1"); 2408d2fbde5SJustin T. Gibbs break; 2416c359123SMaxim Konovalov case 'x': 2426c359123SMaxim Konovalov xflag++; 2436c359123SMaxim Konovalov break; 2446c359123SMaxim Konovalov case 'z': 2456c359123SMaxim Konovalov zflag++; 2466c359123SMaxim Konovalov break; 247dea673e9SRodney W. Grimes default: 248dea673e9SRodney W. Grimes usage(); 2498743d3dcSPhilippe Charnier exit(1); 2508d2fbde5SJustin T. Gibbs break; 251dea673e9SRodney W. Grimes } 2528d2fbde5SJustin T. Gibbs } 2538d2fbde5SJustin T. Gibbs 254dea673e9SRodney W. Grimes argc -= optind; 255dea673e9SRodney W. Grimes argv += optind; 256dea673e9SRodney W. Grimes 2575dc445bfSKenneth D. Merry if (nlistf != NULL || memf != NULL) { 2585fc5ae5cSThomas Moestl kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 2595fc5ae5cSThomas Moestl 2605dc445bfSKenneth D. Merry if (kd == NULL) 2615fc5ae5cSThomas Moestl errx(1, "kvm_openfiles: %s", errbuf); 2625fc5ae5cSThomas Moestl 2635fc5ae5cSThomas Moestl if (kvm_nlist(kd, namelist) == -1) 2645fc5ae5cSThomas Moestl errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 2655fc5ae5cSThomas Moestl } 266dea673e9SRodney W. Grimes 2678d2fbde5SJustin T. Gibbs /* 2688d2fbde5SJustin T. Gibbs * Make sure that the userland devstat version matches the kernel 2698d2fbde5SJustin T. Gibbs * devstat version. If not, exit and print a message informing 2708d2fbde5SJustin T. Gibbs * the user of his mistake. 2718d2fbde5SJustin T. Gibbs */ 2725fc5ae5cSThomas Moestl if (devstat_checkversion(kd) < 0) 2738d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 2748d2fbde5SJustin T. Gibbs 2758d2fbde5SJustin T. Gibbs /* 2765dc445bfSKenneth D. Merry * Make sure Tflag and/or Cflag are set if dflag == 0. If dflag is 2775dc445bfSKenneth D. Merry * greater than 0, they may be 0 or non-zero. 2785dc445bfSKenneth D. Merry */ 2796c359123SMaxim Konovalov if (dflag == 0 && xflag == 0) { 2805dc445bfSKenneth D. Merry Cflag = 1; 2815dc445bfSKenneth D. Merry Tflag = 1; 2825dc445bfSKenneth D. Merry } 2835dc445bfSKenneth D. Merry 2845dc445bfSKenneth D. Merry /* 2858d2fbde5SJustin T. Gibbs * Figure out how many devices we should display. 2868d2fbde5SJustin T. Gibbs */ 2878d2fbde5SJustin T. Gibbs if (nflag == 0) { 2888d2fbde5SJustin T. Gibbs if (oflag > 0) { 2898d2fbde5SJustin T. Gibbs if ((dflag > 0) && (Cflag == 0) && (Tflag == 0)) 2908d2fbde5SJustin T. Gibbs maxshowdevs = 5; 2918d2fbde5SJustin T. Gibbs else if ((dflag > 0) && (Tflag > 0) && (Cflag == 0)) 2928d2fbde5SJustin T. Gibbs maxshowdevs = 5; 2938d2fbde5SJustin T. Gibbs else 2948d2fbde5SJustin T. Gibbs maxshowdevs = 4; 2958d2fbde5SJustin T. Gibbs } else { 2968d2fbde5SJustin T. Gibbs if ((dflag > 0) && (Cflag == 0)) 2978d2fbde5SJustin T. Gibbs maxshowdevs = 4; 2988d2fbde5SJustin T. Gibbs else 2998d2fbde5SJustin T. Gibbs maxshowdevs = 3; 3008d2fbde5SJustin T. Gibbs } 3018d2fbde5SJustin T. Gibbs } 3028d2fbde5SJustin T. Gibbs 3038d2fbde5SJustin T. Gibbs /* find out how many devices we have */ 3045fc5ae5cSThomas Moestl if ((num_devices = devstat_getnumdevs(kd)) < 0) 3058d2fbde5SJustin T. Gibbs err(1, "can't get number of devices"); 3068d2fbde5SJustin T. Gibbs 3075dc445bfSKenneth D. Merry cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 3085dc445bfSKenneth D. Merry if (cur.dinfo == NULL) 3095dc445bfSKenneth D. Merry err(1, "malloc failed"); 3105dc445bfSKenneth D. Merry 3115dc445bfSKenneth D. Merry last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 3125dc445bfSKenneth D. Merry if (last.dinfo == NULL) 3135dc445bfSKenneth D. Merry err(1, "malloc failed"); 3145dc445bfSKenneth D. Merry 3158d2fbde5SJustin T. Gibbs bzero(cur.dinfo, sizeof(struct devinfo)); 3168d2fbde5SJustin T. Gibbs bzero(last.dinfo, sizeof(struct devinfo)); 3178d2fbde5SJustin T. Gibbs 3188d2fbde5SJustin T. Gibbs /* 3198d2fbde5SJustin T. Gibbs * Grab all the devices. We don't look to see if the list has 3208d2fbde5SJustin T. Gibbs * changed here, since it almost certainly has. We only look for 3218d2fbde5SJustin T. Gibbs * errors. 3228d2fbde5SJustin T. Gibbs */ 3235fc5ae5cSThomas Moestl if (devstat_getdevs(kd, &cur) == -1) 3248d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 3258d2fbde5SJustin T. Gibbs 3268d2fbde5SJustin T. Gibbs num_devices = cur.dinfo->numdevs; 3278d2fbde5SJustin T. Gibbs generation = cur.dinfo->generation; 3288d2fbde5SJustin T. Gibbs 3298d2fbde5SJustin T. Gibbs /* 3308d2fbde5SJustin T. Gibbs * If the user specified any devices on the command line, see if 3318d2fbde5SJustin T. Gibbs * they are in the list of devices we have now. 3328d2fbde5SJustin T. Gibbs */ 3335dc445bfSKenneth D. Merry specified_devices = (char **)malloc(sizeof(char *)); 3345dc445bfSKenneth D. Merry if (specified_devices == NULL) 3355dc445bfSKenneth D. Merry err(1, "malloc failed"); 3365dc445bfSKenneth D. Merry 3378d2fbde5SJustin T. Gibbs for (num_devices_specified = 0; *argv; ++argv) { 3388d2fbde5SJustin T. Gibbs if (isdigit(**argv)) 3398d2fbde5SJustin T. Gibbs break; 3408d2fbde5SJustin T. Gibbs num_devices_specified++; 3418d2fbde5SJustin T. Gibbs specified_devices = (char **)realloc(specified_devices, 3428d2fbde5SJustin T. Gibbs sizeof(char *) * 3438d2fbde5SJustin T. Gibbs num_devices_specified); 3445dc445bfSKenneth D. Merry if (specified_devices == NULL) 3455dc445bfSKenneth D. Merry err(1, "realloc failed"); 3465dc445bfSKenneth D. Merry 3478d2fbde5SJustin T. Gibbs specified_devices[num_devices_specified - 1] = *argv; 3488d2fbde5SJustin T. Gibbs 3498d2fbde5SJustin T. Gibbs } 3502fa4aaa0SMatthew Dillon if (nflag == 0 && maxshowdevs < num_devices_specified) 3512fa4aaa0SMatthew Dillon maxshowdevs = num_devices_specified; 3528d2fbde5SJustin T. Gibbs 3538d2fbde5SJustin T. Gibbs dev_select = NULL; 3548d2fbde5SJustin T. Gibbs 3558d2fbde5SJustin T. Gibbs if ((num_devices_specified == 0) && (num_matches == 0)) 3568d2fbde5SJustin T. Gibbs select_mode = DS_SELECT_ADD; 3578d2fbde5SJustin T. Gibbs else 3588d2fbde5SJustin T. Gibbs select_mode = DS_SELECT_ONLY; 3598d2fbde5SJustin T. Gibbs 3608d2fbde5SJustin T. Gibbs /* 3618d2fbde5SJustin T. Gibbs * At this point, selectdevs will almost surely indicate that the 3628d2fbde5SJustin T. Gibbs * device list has changed, so we don't look for return values of 0 3638d2fbde5SJustin T. Gibbs * or 1. If we get back -1, though, there is an error. 3648d2fbde5SJustin T. Gibbs */ 3655fc5ae5cSThomas Moestl if (devstat_selectdevs(&dev_select, &num_selected, 3665fc5ae5cSThomas Moestl &num_selections, &select_generation, generation, 3675fc5ae5cSThomas Moestl cur.dinfo->devices, num_devices, matches, 3685fc5ae5cSThomas Moestl num_matches, specified_devices, 3695fc5ae5cSThomas Moestl num_devices_specified, select_mode, maxshowdevs, 3705fc5ae5cSThomas Moestl hflag) == -1) 3718d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 3728d2fbde5SJustin T. Gibbs 3738d2fbde5SJustin T. Gibbs /* 3748d2fbde5SJustin T. Gibbs * Look for the traditional wait time and count arguments. 3758d2fbde5SJustin T. Gibbs */ 3768d2fbde5SJustin T. Gibbs if (*argv) { 3778d2fbde5SJustin T. Gibbs waittime = atoi(*argv); 3788d2fbde5SJustin T. Gibbs 3798d2fbde5SJustin T. Gibbs /* Let the user know he goofed, but keep going anyway */ 3808d2fbde5SJustin T. Gibbs if (wflag != 0) 3818d2fbde5SJustin T. Gibbs warnx("discarding previous wait interval, using" 3828d2fbde5SJustin T. Gibbs " %d instead", waittime); 3838d2fbde5SJustin T. Gibbs wflag++; 3848d2fbde5SJustin T. Gibbs 3858d2fbde5SJustin T. Gibbs if (*++argv) { 3868d2fbde5SJustin T. Gibbs count = atoi(*argv); 3878d2fbde5SJustin T. Gibbs if (cflag != 0) 3888d2fbde5SJustin T. Gibbs warnx("discarding previous count, using %d" 3898d2fbde5SJustin T. Gibbs " instead", count); 3908d2fbde5SJustin T. Gibbs cflag++; 3918d2fbde5SJustin T. Gibbs } else 3928d2fbde5SJustin T. Gibbs count = -1; 3938d2fbde5SJustin T. Gibbs } 3948d2fbde5SJustin T. Gibbs 3958d2fbde5SJustin T. Gibbs /* 3968d2fbde5SJustin T. Gibbs * If the user specified a count, but not an interval, we default 3978d2fbde5SJustin T. Gibbs * to an interval of 1 second. 3988d2fbde5SJustin T. Gibbs */ 3998d2fbde5SJustin T. Gibbs if ((wflag == 0) && (cflag > 0)) 4008d2fbde5SJustin T. Gibbs waittime = 1; 4018d2fbde5SJustin T. Gibbs 4028d2fbde5SJustin T. Gibbs /* 4038d2fbde5SJustin T. Gibbs * If the user specified a wait time, but not a count, we want to 4048d2fbde5SJustin T. Gibbs * go on ad infinitum. This can be redundant if the user uses the 4058d2fbde5SJustin T. Gibbs * traditional method of specifying the wait, since in that case we 4068d2fbde5SJustin T. Gibbs * already set count = -1 above. Oh well. 4078d2fbde5SJustin T. Gibbs */ 4088d2fbde5SJustin T. Gibbs if ((wflag > 0) && (cflag == 0)) 4098d2fbde5SJustin T. Gibbs count = -1; 4108d2fbde5SJustin T. Gibbs 4115dc445bfSKenneth D. Merry bzero(&cur.cp_time, sizeof(cur.cp_time)); 4125dc445bfSKenneth D. Merry cur.tk_nout = 0; 4135dc445bfSKenneth D. Merry cur.tk_nin = 0; 4145dc445bfSKenneth D. Merry 4155dc445bfSKenneth D. Merry /* 4167194d335SPoul-Henning Kamp * Set the snap time to the system boot time (ie: zero), so the 4177194d335SPoul-Henning Kamp * stats are calculated since system boot. 4185dc445bfSKenneth D. Merry */ 4197194d335SPoul-Henning Kamp cur.snap_time = 0; 4205dc445bfSKenneth D. Merry 421dea673e9SRodney W. Grimes /* 4228d2fbde5SJustin T. Gibbs * If the user stops the program (control-Z) and then resumes it, 4238d2fbde5SJustin T. Gibbs * print out the header again. 424dea673e9SRodney W. Grimes */ 425775b0074SGuido van Rooij (void)signal(SIGCONT, needhdr); 426dea673e9SRodney W. Grimes 4278d2fbde5SJustin T. Gibbs for (headercount = 1;;) { 4288d2fbde5SJustin T. Gibbs struct devinfo *tmp_dinfo; 4298d2fbde5SJustin T. Gibbs long tmp; 4305dc445bfSKenneth D. Merry long double etime; 4315dc445bfSKenneth D. Merry 4325dc445bfSKenneth D. Merry if (Tflag > 0) { 4335dc445bfSKenneth D. Merry if ((readvar(kd, "kern.tty_nin", X_TK_NIN, &cur.tk_nin, 4345dc445bfSKenneth D. Merry sizeof(cur.tk_nin)) != 0) 4355dc445bfSKenneth D. Merry || (readvar(kd, "kern.tty_nout", X_TK_NOUT, 4365dc445bfSKenneth D. Merry &cur.tk_nout, sizeof(cur.tk_nout))!= 0)) { 4375dc445bfSKenneth D. Merry Tflag = 0; 4385dc445bfSKenneth D. Merry warnx("disabling TTY statistics"); 4395dc445bfSKenneth D. Merry } 4405dc445bfSKenneth D. Merry } 4415dc445bfSKenneth D. Merry 4425dc445bfSKenneth D. Merry if (Cflag > 0) { 4435dc445bfSKenneth D. Merry if (readvar(kd, "kern.cp_time", X_CP_TIME, 4445dc445bfSKenneth D. Merry &cur.cp_time, sizeof(cur.cp_time)) != 0) { 4455dc445bfSKenneth D. Merry Cflag = 0; 4465dc445bfSKenneth D. Merry warnx("disabling CPU time statistics"); 4475dc445bfSKenneth D. Merry } 4485dc445bfSKenneth D. Merry } 4498d2fbde5SJustin T. Gibbs 4508d2fbde5SJustin T. Gibbs if (!--headercount) { 451775b0074SGuido van Rooij phdr(); 4528d2fbde5SJustin T. Gibbs headercount = 20; 453dea673e9SRodney W. Grimes } 4548d2fbde5SJustin T. Gibbs 4558d2fbde5SJustin T. Gibbs tmp_dinfo = last.dinfo; 4568d2fbde5SJustin T. Gibbs last.dinfo = cur.dinfo; 4578d2fbde5SJustin T. Gibbs cur.dinfo = tmp_dinfo; 4588d2fbde5SJustin T. Gibbs 4597194d335SPoul-Henning Kamp last.snap_time = cur.snap_time; 4608d2fbde5SJustin T. Gibbs 4618d2fbde5SJustin T. Gibbs /* 4628d2fbde5SJustin T. Gibbs * Here what we want to do is refresh our device stats. 4635fc5ae5cSThomas Moestl * devstat_getdevs() returns 1 when the device list has changed. 4648d2fbde5SJustin T. Gibbs * If the device list has changed, we want to go through 4658d2fbde5SJustin T. Gibbs * the selection process again, in case a device that we 4668d2fbde5SJustin T. Gibbs * were previously displaying has gone away. 4678d2fbde5SJustin T. Gibbs */ 4685fc5ae5cSThomas Moestl switch (devstat_getdevs(kd, &cur)) { 4698d2fbde5SJustin T. Gibbs case -1: 4708d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 4718d2fbde5SJustin T. Gibbs break; 4728d2fbde5SJustin T. Gibbs case 1: { 4738d2fbde5SJustin T. Gibbs int retval; 4748d2fbde5SJustin T. Gibbs 4758d2fbde5SJustin T. Gibbs num_devices = cur.dinfo->numdevs; 4768d2fbde5SJustin T. Gibbs generation = cur.dinfo->generation; 4775fc5ae5cSThomas Moestl retval = devstat_selectdevs(&dev_select, &num_selected, 4785fc5ae5cSThomas Moestl &num_selections, 4795fc5ae5cSThomas Moestl &select_generation, 4805fc5ae5cSThomas Moestl generation, 4815fc5ae5cSThomas Moestl cur.dinfo->devices, 4825fc5ae5cSThomas Moestl num_devices, matches, 4835fc5ae5cSThomas Moestl num_matches, 4848d2fbde5SJustin T. Gibbs specified_devices, 4858d2fbde5SJustin T. Gibbs num_devices_specified, 4865fc5ae5cSThomas Moestl select_mode, maxshowdevs, 4875fc5ae5cSThomas Moestl hflag); 4888d2fbde5SJustin T. Gibbs switch(retval) { 4898d2fbde5SJustin T. Gibbs case -1: 4908d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 4918d2fbde5SJustin T. Gibbs break; 4928d2fbde5SJustin T. Gibbs case 1: 493775b0074SGuido van Rooij phdr(); 4948d2fbde5SJustin T. Gibbs headercount = 20; 4958d2fbde5SJustin T. Gibbs break; 4968d2fbde5SJustin T. Gibbs default: 4978d2fbde5SJustin T. Gibbs break; 498dea673e9SRodney W. Grimes } 4998d2fbde5SJustin T. Gibbs break; 5008d2fbde5SJustin T. Gibbs } 5018d2fbde5SJustin T. Gibbs default: 5028d2fbde5SJustin T. Gibbs break; 5038d2fbde5SJustin T. Gibbs } 5048d2fbde5SJustin T. Gibbs 5058d2fbde5SJustin T. Gibbs /* 5068d2fbde5SJustin T. Gibbs * We only want to re-select devices if we're in 'top' 5078d2fbde5SJustin T. Gibbs * mode. This is the only mode where the devices selected 5088d2fbde5SJustin T. Gibbs * could actually change. 5098d2fbde5SJustin T. Gibbs */ 5108d2fbde5SJustin T. Gibbs if (hflag > 0) { 5118d2fbde5SJustin T. Gibbs int retval; 5125fc5ae5cSThomas Moestl retval = devstat_selectdevs(&dev_select, &num_selected, 5135fc5ae5cSThomas Moestl &num_selections, 5145fc5ae5cSThomas Moestl &select_generation, 5155fc5ae5cSThomas Moestl generation, 5165fc5ae5cSThomas Moestl cur.dinfo->devices, 5175fc5ae5cSThomas Moestl num_devices, matches, 5185fc5ae5cSThomas Moestl num_matches, 5198d2fbde5SJustin T. Gibbs specified_devices, 5208d2fbde5SJustin T. Gibbs num_devices_specified, 5215fc5ae5cSThomas Moestl select_mode, maxshowdevs, 5225fc5ae5cSThomas Moestl hflag); 5238d2fbde5SJustin T. Gibbs switch(retval) { 5248d2fbde5SJustin T. Gibbs case -1: 5258d2fbde5SJustin T. Gibbs errx(1,"%s", devstat_errbuf); 5268d2fbde5SJustin T. Gibbs break; 5278d2fbde5SJustin T. Gibbs case 1: 528775b0074SGuido van Rooij phdr(); 5298d2fbde5SJustin T. Gibbs headercount = 20; 5308d2fbde5SJustin T. Gibbs break; 5318d2fbde5SJustin T. Gibbs default: 5328d2fbde5SJustin T. Gibbs break; 5338d2fbde5SJustin T. Gibbs } 5348d2fbde5SJustin T. Gibbs } 5358d2fbde5SJustin T. Gibbs 5365dc445bfSKenneth D. Merry if (Tflag > 0) { 537dea673e9SRodney W. Grimes tmp = cur.tk_nin; 538dea673e9SRodney W. Grimes cur.tk_nin -= last.tk_nin; 539dea673e9SRodney W. Grimes last.tk_nin = tmp; 540dea673e9SRodney W. Grimes tmp = cur.tk_nout; 541dea673e9SRodney W. Grimes cur.tk_nout -= last.tk_nout; 542dea673e9SRodney W. Grimes last.tk_nout = tmp; 543dea673e9SRodney W. Grimes } 5445dc445bfSKenneth D. Merry 5457194d335SPoul-Henning Kamp etime = cur.snap_time - last.snap_time; 5465dc445bfSKenneth D. Merry 547dea673e9SRodney W. Grimes if (etime == 0.0) 548dea673e9SRodney W. Grimes etime = 1.0; 5495dc445bfSKenneth D. Merry 5505dc445bfSKenneth D. Merry for (i = 0; i < CPUSTATES; i++) { 5515dc445bfSKenneth D. Merry tmp = cur.cp_time[i]; 5525dc445bfSKenneth D. Merry cur.cp_time[i] -= last.cp_time[i]; 5535dc445bfSKenneth D. Merry last.cp_time[i] = tmp; 5545dc445bfSKenneth D. Merry } 5555dc445bfSKenneth D. Merry 5566c359123SMaxim Konovalov if (xflag == 0 && Tflag > 0) 5575dc445bfSKenneth D. Merry printf("%4.0Lf%5.0Lf", cur.tk_nin / etime, 5588d2fbde5SJustin T. Gibbs cur.tk_nout / etime); 5595dc445bfSKenneth D. Merry 5605fc5ae5cSThomas Moestl devstats(hflag, etime, havelast); 5615dc445bfSKenneth D. Merry 5626c359123SMaxim Konovalov if (xflag == 0) { 5635dc445bfSKenneth D. Merry if (Cflag > 0) 564dea673e9SRodney W. Grimes cpustats(); 5655dc445bfSKenneth D. Merry 5668d2fbde5SJustin T. Gibbs printf("\n"); 5676c359123SMaxim Konovalov } 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 580775b0074SGuido van Rooij /* 581775b0074SGuido van Rooij * Force a header to be prepended to the next output. 582775b0074SGuido van Rooij */ 583775b0074SGuido van Rooij void 584775b0074SGuido van Rooij needhdr(int signo) 585dea673e9SRodney W. Grimes { 586e65cded7SKris Kennaway 587775b0074SGuido van Rooij headercount = 1; 588e65cded7SKris Kennaway } 589e65cded7SKris Kennaway 590e65cded7SKris Kennaway static void 591775b0074SGuido van Rooij phdr(void) 592e65cded7SKris Kennaway { 59347e065b4SMaxim Konovalov int i, printed; 594dea673e9SRodney W. Grimes 5956c359123SMaxim Konovalov /* 5966c359123SMaxim Konovalov * If xflag is set, we need a per-loop header, not a page header, so 5976c359123SMaxim Konovalov * just return. We'll print the header in devstats(). 5986c359123SMaxim Konovalov */ 5996c359123SMaxim Konovalov if (xflag > 0) 6006c359123SMaxim Konovalov return; 6016c359123SMaxim Konovalov 6025dc445bfSKenneth D. Merry if (Tflag > 0) 603dea673e9SRodney W. Grimes (void)printf(" tty"); 6048d2fbde5SJustin T. Gibbs for (i = 0, printed=0;(i < num_devices) && (printed < maxshowdevs);i++){ 6058d2fbde5SJustin T. Gibbs int di; 6068d2fbde5SJustin T. Gibbs if ((dev_select[i].selected != 0) 6078d2fbde5SJustin T. Gibbs && (dev_select[i].selected <= maxshowdevs)) { 6088d2fbde5SJustin T. Gibbs di = dev_select[i].position; 6098d2fbde5SJustin T. Gibbs if (oflag > 0) 6108d2fbde5SJustin T. Gibbs (void)printf("%12.6s%d ", 6118d2fbde5SJustin T. Gibbs cur.dinfo->devices[di].device_name, 6128d2fbde5SJustin T. Gibbs cur.dinfo->devices[di].unit_number); 6138d2fbde5SJustin T. Gibbs else 6148d2fbde5SJustin T. Gibbs printf("%15.6s%d ", 6158d2fbde5SJustin T. Gibbs cur.dinfo->devices[di].device_name, 6168d2fbde5SJustin T. Gibbs cur.dinfo->devices[di].unit_number); 6178d2fbde5SJustin T. Gibbs printed++; 6188d2fbde5SJustin T. Gibbs } 6198d2fbde5SJustin T. Gibbs } 6205dc445bfSKenneth D. Merry if (Cflag > 0) 6218d2fbde5SJustin T. Gibbs (void)printf(" cpu\n"); 6228d2fbde5SJustin T. Gibbs else 6238d2fbde5SJustin T. Gibbs (void)printf("\n"); 6248d2fbde5SJustin T. Gibbs 6255dc445bfSKenneth D. Merry if (Tflag > 0) 6268d2fbde5SJustin T. Gibbs (void)printf(" tin tout"); 6278d2fbde5SJustin T. Gibbs 6288d2fbde5SJustin T. Gibbs for (i=0, printed = 0;(i < num_devices) && (printed < maxshowdevs);i++){ 6298d2fbde5SJustin T. Gibbs if ((dev_select[i].selected != 0) 6308d2fbde5SJustin T. Gibbs && (dev_select[i].selected <= maxshowdevs)) { 6312fa4aaa0SMatthew Dillon if (oflag > 0) { 6328d2fbde5SJustin T. Gibbs if (Iflag == 0) 633dea673e9SRodney W. Grimes (void)printf(" sps tps msps "); 6348d2fbde5SJustin T. Gibbs else 6358d2fbde5SJustin T. Gibbs (void)printf(" blk xfr msps "); 6362fa4aaa0SMatthew Dillon } else { 6378d2fbde5SJustin T. Gibbs if (Iflag == 0) 6388d2fbde5SJustin T. Gibbs printf(" KB/t tps MB/s "); 6398d2fbde5SJustin T. Gibbs else 6408d2fbde5SJustin T. Gibbs printf(" KB/t xfrs MB "); 6412fa4aaa0SMatthew Dillon } 6428d2fbde5SJustin T. Gibbs printed++; 6438d2fbde5SJustin T. Gibbs } 6448d2fbde5SJustin T. Gibbs } 6455dc445bfSKenneth D. Merry if (Cflag > 0) 646936d754aSAndrey A. Chernov (void)printf(" us ni sy in id\n"); 6478d2fbde5SJustin T. Gibbs else 6488d2fbde5SJustin T. Gibbs printf("\n"); 649775b0074SGuido van Rooij 650dea673e9SRodney W. Grimes } 651dea673e9SRodney W. Grimes 6528d2fbde5SJustin T. Gibbs static void 6535dc445bfSKenneth D. Merry devstats(int perf_select, long double etime, int havelast) 654dea673e9SRodney W. Grimes { 65547e065b4SMaxim Konovalov int dn; 6566c359123SMaxim Konovalov long double transfers_per_second, transfers_per_second_read, transfers_per_second_write; 6576c359123SMaxim Konovalov long double kb_per_transfer, mb_per_second, mb_per_second_read, mb_per_second_write; 6588d2fbde5SJustin T. Gibbs u_int64_t total_bytes, total_transfers, total_blocks; 6596c359123SMaxim Konovalov long double busy_pct; 6606c359123SMaxim Konovalov u_int64_t queue_len; 6618d2fbde5SJustin T. Gibbs long double total_mb; 6628d2fbde5SJustin T. Gibbs long double blocks_per_second, ms_per_transaction; 6636c359123SMaxim Konovalov int firstline = 1; 6646c359123SMaxim Konovalov char *devname; 6656c359123SMaxim Konovalov 6666c359123SMaxim Konovalov if (xflag > 0) { 6676c359123SMaxim Konovalov printf(" extended device statistics "); 6686c359123SMaxim Konovalov if (Tflag > 0) 6696c359123SMaxim Konovalov printf(" tty "); 6706c359123SMaxim Konovalov if (Cflag > 0) 6716c359123SMaxim Konovalov printf(" cpu "); 6726c359123SMaxim Konovalov printf("\n"); 6736c359123SMaxim Konovalov printf("device r/s w/s kr/s kw/s wait svc_t %%b "); 6746c359123SMaxim Konovalov if (Tflag > 0) 6756c359123SMaxim Konovalov printf("tin tout "); 6766c359123SMaxim Konovalov if (Cflag > 0) 6776c359123SMaxim Konovalov printf("us ni sy in id "); 6786c359123SMaxim Konovalov printf("\n"); 6796c359123SMaxim Konovalov } 680dea673e9SRodney W. Grimes 6818d2fbde5SJustin T. Gibbs for (dn = 0; dn < num_devices; dn++) { 6828d2fbde5SJustin T. Gibbs int di; 6838d2fbde5SJustin T. Gibbs 6848d2fbde5SJustin T. Gibbs if (((perf_select == 0) && (dev_select[dn].selected == 0)) 6858d2fbde5SJustin T. Gibbs || (dev_select[dn].selected > maxshowdevs)) 686dea673e9SRodney W. Grimes continue; 687dea673e9SRodney W. Grimes 6888d2fbde5SJustin T. Gibbs di = dev_select[dn].position; 689dea673e9SRodney W. Grimes 6905fc5ae5cSThomas Moestl if (devstat_compute_statistics(&cur.dinfo->devices[di], 6915dc445bfSKenneth D. Merry havelast ? &last.dinfo->devices[di] : NULL, etime, 6925fc5ae5cSThomas Moestl DSM_TOTAL_BYTES, &total_bytes, 6935fc5ae5cSThomas Moestl DSM_TOTAL_TRANSFERS, &total_transfers, 6945fc5ae5cSThomas Moestl DSM_TOTAL_BLOCKS, &total_blocks, 6955fc5ae5cSThomas Moestl DSM_KB_PER_TRANSFER, &kb_per_transfer, 6965fc5ae5cSThomas Moestl DSM_TRANSFERS_PER_SECOND, &transfers_per_second, 6976c359123SMaxim Konovalov DSM_TRANSFERS_PER_SECOND_READ, &transfers_per_second_read, 6986c359123SMaxim Konovalov DSM_TRANSFERS_PER_SECOND_WRITE, &transfers_per_second_write, 6995fc5ae5cSThomas Moestl DSM_MB_PER_SECOND, &mb_per_second, 7006c359123SMaxim Konovalov DSM_MB_PER_SECOND_READ, &mb_per_second_read, 7016c359123SMaxim Konovalov DSM_MB_PER_SECOND_WRITE, &mb_per_second_write, 7025fc5ae5cSThomas Moestl DSM_BLOCKS_PER_SECOND, &blocks_per_second, 7035fc5ae5cSThomas Moestl DSM_MS_PER_TRANSACTION, &ms_per_transaction, 7046c359123SMaxim Konovalov DSM_BUSY_PCT, &busy_pct, 7056c359123SMaxim Konovalov DSM_QUEUE_LENGTH, &queue_len, 7065fc5ae5cSThomas Moestl DSM_NONE) != 0) 7078d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 7088d2fbde5SJustin T. Gibbs 7098d2fbde5SJustin T. Gibbs if (perf_select != 0) { 7108d2fbde5SJustin T. Gibbs dev_select[dn].bytes = total_bytes; 7118d2fbde5SJustin T. Gibbs if ((dev_select[dn].selected == 0) 7128d2fbde5SJustin T. Gibbs || (dev_select[dn].selected > maxshowdevs)) 7138d2fbde5SJustin T. Gibbs continue; 7148d2fbde5SJustin T. Gibbs } 7158d2fbde5SJustin T. Gibbs 7166c359123SMaxim Konovalov if (Kflag > 0 || xflag > 0) { 7172fa4aaa0SMatthew Dillon int block_size = cur.dinfo->devices[di].block_size; 718e4d7fe02SKenneth D. Merry total_blocks = total_blocks * (block_size ? 719e4d7fe02SKenneth D. Merry block_size : 512) / 1024; 7202fa4aaa0SMatthew Dillon } 7212fa4aaa0SMatthew Dillon 7226c359123SMaxim Konovalov if (xflag > 0) { 7236c359123SMaxim Konovalov asprintf(&devname, "%s%d", 7246c359123SMaxim Konovalov cur.dinfo->devices[di].device_name, 7256c359123SMaxim Konovalov cur.dinfo->devices[di].unit_number); 7266c359123SMaxim Konovalov /* 7276c359123SMaxim Konovalov * If zflag is set, skip any devices with zero I/O. 7286c359123SMaxim Konovalov */ 7296c359123SMaxim Konovalov if (zflag == 0 || transfers_per_second_read > 0.05 || 7306c359123SMaxim Konovalov transfers_per_second_write > 0.05 || 7316c359123SMaxim Konovalov mb_per_second_read > ((long double).0005)/1024 || 7326c359123SMaxim Konovalov mb_per_second_write > ((long double).0005)/1024 || 7336c359123SMaxim Konovalov busy_pct > 0.5) { 7346c359123SMaxim Konovalov printf("%-8.8s %5.1Lf %5.1Lf %7.1Lf %7.1Lf %4qu %5.1Lf %3.0Lf ", 7356c359123SMaxim Konovalov devname, transfers_per_second_read, 7366c359123SMaxim Konovalov transfers_per_second_write, 7376c359123SMaxim Konovalov mb_per_second_read * 1024, 7386c359123SMaxim Konovalov mb_per_second_write * 1024, queue_len, 7396c359123SMaxim Konovalov ms_per_transaction, busy_pct); 7406c359123SMaxim Konovalov if (firstline) { 7416c359123SMaxim Konovalov /* 7426c359123SMaxim Konovalov * If this is the first device 7436c359123SMaxim Konovalov * we're printing, also print 7446c359123SMaxim Konovalov * CPU or TTY stats if requested. 7456c359123SMaxim Konovalov */ 7466c359123SMaxim Konovalov firstline = 0; 7476c359123SMaxim Konovalov if (Tflag > 0) 7486c359123SMaxim Konovalov printf("%4.0Lf%5.0Lf", 7496c359123SMaxim Konovalov cur.tk_nin / etime, 7506c359123SMaxim Konovalov cur.tk_nout / etime); 7516c359123SMaxim Konovalov if (Cflag > 0) 7526c359123SMaxim Konovalov cpustats(); 7536c359123SMaxim Konovalov } 7546c359123SMaxim Konovalov printf("\n"); 7556c359123SMaxim Konovalov } 7566c359123SMaxim Konovalov } else if (oflag > 0) { 7572fa4aaa0SMatthew Dillon int msdig = (ms_per_transaction < 100.0) ? 1 : 0; 7588d2fbde5SJustin T. Gibbs 7598d2fbde5SJustin T. Gibbs if (Iflag == 0) 7602fa4aaa0SMatthew Dillon printf("%4.0Lf%4.0Lf%5.*Lf ", 7618d2fbde5SJustin T. Gibbs blocks_per_second, 7628d2fbde5SJustin T. Gibbs transfers_per_second, 7632fa4aaa0SMatthew Dillon msdig, 7648d2fbde5SJustin T. Gibbs ms_per_transaction); 765dea673e9SRodney W. Grimes else 7662fa4aaa0SMatthew Dillon printf("%4.1qu%4.1qu%5.*Lf ", 7678d2fbde5SJustin T. Gibbs total_blocks, 7688d2fbde5SJustin T. Gibbs total_transfers, 7692fa4aaa0SMatthew Dillon msdig, 7708d2fbde5SJustin T. Gibbs ms_per_transaction); 7718d2fbde5SJustin T. Gibbs } else { 7728d2fbde5SJustin T. Gibbs if (Iflag == 0) 7738d2fbde5SJustin T. Gibbs printf(" %5.2Lf %3.0Lf %5.2Lf ", 7748d2fbde5SJustin T. Gibbs kb_per_transfer, 7758d2fbde5SJustin T. Gibbs transfers_per_second, 7768d2fbde5SJustin T. Gibbs mb_per_second); 7778d2fbde5SJustin T. Gibbs else { 7788d2fbde5SJustin T. Gibbs total_mb = total_bytes; 7798d2fbde5SJustin T. Gibbs total_mb /= 1024 * 1024; 7808d2fbde5SJustin T. Gibbs 7818d2fbde5SJustin T. Gibbs printf(" %5.2Lf %3.1qu %5.2Lf ", 7828d2fbde5SJustin T. Gibbs kb_per_transfer, 7838d2fbde5SJustin T. Gibbs total_transfers, 7848d2fbde5SJustin T. Gibbs total_mb); 7858d2fbde5SJustin T. Gibbs } 7868d2fbde5SJustin T. Gibbs } 787dea673e9SRodney W. Grimes } 7886c359123SMaxim Konovalov if (xflag > 0 && zflag > 0 && firstline == 1 && 7896c359123SMaxim Konovalov (Tflag > 0 || Cflag > 0)) { 7906c359123SMaxim Konovalov /* 7916c359123SMaxim Konovalov * If zflag is set and we did not print any device 7926c359123SMaxim Konovalov * lines I/O because they were all zero, 7936c359123SMaxim Konovalov * print TTY/CPU stats. 7946c359123SMaxim Konovalov */ 7956c359123SMaxim Konovalov printf("%52s",""); 7966c359123SMaxim Konovalov if (Tflag > 0) 7976c359123SMaxim Konovalov printf("%4.0Lf%5.0Lf", cur.tk_nin / etime, 7986c359123SMaxim Konovalov cur.tk_nout / etime); 7996c359123SMaxim Konovalov if (Cflag > 0) 8006c359123SMaxim Konovalov cpustats(); 8016c359123SMaxim Konovalov printf("\n"); 8026c359123SMaxim Konovalov } 803dea673e9SRodney W. Grimes } 804dea673e9SRodney W. Grimes 8058d2fbde5SJustin T. Gibbs static void 8068d2fbde5SJustin T. Gibbs cpustats(void) 807dea673e9SRodney W. Grimes { 80847e065b4SMaxim Konovalov int state; 809dea673e9SRodney W. Grimes double time; 810dea673e9SRodney W. Grimes 8118d2fbde5SJustin T. Gibbs time = 0.0; 8128d2fbde5SJustin T. Gibbs 813dea673e9SRodney W. Grimes for (state = 0; state < CPUSTATES; ++state) 814dea673e9SRodney W. Grimes time += cur.cp_time[state]; 815dea673e9SRodney W. Grimes for (state = 0; state < CPUSTATES; ++state) 8168fa6b8a8SBruce Evans printf(" %2.0f", 8175dc445bfSKenneth D. Merry rint(100. * cur.cp_time[state] / (time ? time : 1))); 818dea673e9SRodney W. Grimes } 8195fc5ae5cSThomas Moestl 8205dc445bfSKenneth D. Merry static int 8215dc445bfSKenneth D. Merry readvar(kvm_t *kd, const char *name, int nlid, void *ptr, size_t len) 8225fc5ae5cSThomas Moestl { 8235dc445bfSKenneth D. Merry if (kd != NULL) { 8245dc445bfSKenneth D. Merry ssize_t nbytes; 8255dc445bfSKenneth D. Merry 8268d56a4caSJohn Baldwin nbytes = kvm_read(kd, namelist[nlid].n_value, ptr, len); 8275dc445bfSKenneth D. Merry 8288d56a4caSJohn Baldwin if (nbytes < 0) { 8298d56a4caSJohn Baldwin warnx("kvm_read(%s): %s", namelist[nlid].n_name, 8308d56a4caSJohn Baldwin kvm_geterr(kd)); 8315dc445bfSKenneth D. Merry return (1); 8325dc445bfSKenneth D. Merry } 8335dc445bfSKenneth D. Merry if (nbytes != len) { 8348d56a4caSJohn Baldwin warnx("kvm_read(%s): expected %zu bytes, got %zd bytes", 8358d56a4caSJohn Baldwin namelist[nlid].n_name, len, nbytes); 8365dc445bfSKenneth D. Merry return (1); 8375dc445bfSKenneth D. Merry } 8385dc445bfSKenneth D. Merry } else { 8395fc5ae5cSThomas Moestl size_t nlen = len; 8405fc5ae5cSThomas Moestl 8415dc445bfSKenneth D. Merry if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) { 8425dc445bfSKenneth D. Merry warn("sysctl(%s...) failed", name); 8435dc445bfSKenneth D. Merry return (1); 8445dc445bfSKenneth D. Merry } 8455dc445bfSKenneth D. Merry if (nlen != len) { 8465dc445bfSKenneth D. Merry warnx("sysctl(%s...): expected %lu, got %lu", name, 8475fc5ae5cSThomas Moestl (unsigned long)len, (unsigned long)nlen); 8485dc445bfSKenneth D. Merry return (1); 8495dc445bfSKenneth D. Merry } 8505dc445bfSKenneth D. Merry } 8515dc445bfSKenneth D. Merry return (0); 8525fc5ae5cSThomas Moestl } 853