18a16b7a1SPedro F. Giffuni /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 45dc445bfSKenneth D. Merry * Copyright (c) 1997, 1998, 2000, 2001 Kenneth D. Merry 58d2fbde5SJustin T. Gibbs * All rights reserved. 68d2fbde5SJustin T. Gibbs * 78d2fbde5SJustin T. Gibbs * Redistribution and use in source and binary forms, with or without 88d2fbde5SJustin T. Gibbs * modification, are permitted provided that the following conditions 98d2fbde5SJustin T. Gibbs * are met: 108d2fbde5SJustin T. Gibbs * 1. Redistributions of source code must retain the above copyright 118d2fbde5SJustin T. Gibbs * notice, this list of conditions and the following disclaimer. 128d2fbde5SJustin T. Gibbs * 2. Redistributions in binary form must reproduce the above copyright 138d2fbde5SJustin T. Gibbs * notice, this list of conditions and the following disclaimer in the 148d2fbde5SJustin T. Gibbs * documentation and/or other materials provided with the distribution. 158d2fbde5SJustin T. Gibbs * 3. The name of the author may not be used to endorse or promote products 168d2fbde5SJustin T. Gibbs * derived from this software without specific prior written permission. 178d2fbde5SJustin T. Gibbs * 188d2fbde5SJustin T. Gibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 198d2fbde5SJustin T. Gibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 208d2fbde5SJustin T. Gibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 218d2fbde5SJustin T. Gibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 228d2fbde5SJustin T. Gibbs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 238d2fbde5SJustin T. Gibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 248d2fbde5SJustin T. Gibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 258d2fbde5SJustin T. Gibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 268d2fbde5SJustin T. Gibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 278d2fbde5SJustin T. Gibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 288d2fbde5SJustin T. Gibbs * SUCH DAMAGE. 298743d3dcSPhilippe Charnier * 308743d3dcSPhilippe Charnier * $FreeBSD$ 318d2fbde5SJustin T. Gibbs */ 328d2fbde5SJustin T. Gibbs /* 338d2fbde5SJustin T. Gibbs * Parts of this program are derived from the original FreeBSD iostat 348d2fbde5SJustin T. Gibbs * program: 358d2fbde5SJustin T. Gibbs */ 36dea673e9SRodney W. Grimes /*- 37dea673e9SRodney W. Grimes * Copyright (c) 1986, 1991, 1993 38dea673e9SRodney W. Grimes * The Regents of the University of California. All rights reserved. 39dea673e9SRodney W. Grimes * 40dea673e9SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 41dea673e9SRodney W. Grimes * modification, are permitted provided that the following conditions 42dea673e9SRodney W. Grimes * are met: 43dea673e9SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 44dea673e9SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 45dea673e9SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 46dea673e9SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 47dea673e9SRodney W. Grimes * documentation and/or other materials provided with the distribution. 48dea673e9SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 49dea673e9SRodney W. Grimes * may be used to endorse or promote products derived from this software 50dea673e9SRodney W. Grimes * without specific prior written permission. 51dea673e9SRodney W. Grimes * 52dea673e9SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 53dea673e9SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 54dea673e9SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 55dea673e9SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 56dea673e9SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 57dea673e9SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 58dea673e9SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59dea673e9SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60dea673e9SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61dea673e9SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62dea673e9SRodney W. Grimes * SUCH DAMAGE. 63dea673e9SRodney W. Grimes */ 648d2fbde5SJustin T. Gibbs /* 658d2fbde5SJustin T. Gibbs * Ideas for the new iostat statistics output modes taken from the NetBSD 668d2fbde5SJustin T. Gibbs * version of iostat: 678d2fbde5SJustin T. Gibbs */ 688d2fbde5SJustin T. Gibbs /* 698d2fbde5SJustin T. Gibbs * Copyright (c) 1996 John M. Vinopal 708d2fbde5SJustin T. Gibbs * All rights reserved. 718d2fbde5SJustin T. Gibbs * 728d2fbde5SJustin T. Gibbs * Redistribution and use in source and binary forms, with or without 738d2fbde5SJustin T. Gibbs * modification, are permitted provided that the following conditions 748d2fbde5SJustin T. Gibbs * are met: 758d2fbde5SJustin T. Gibbs * 1. Redistributions of source code must retain the above copyright 768d2fbde5SJustin T. Gibbs * notice, this list of conditions and the following disclaimer. 778d2fbde5SJustin T. Gibbs * 2. Redistributions in binary form must reproduce the above copyright 788d2fbde5SJustin T. Gibbs * notice, this list of conditions and the following disclaimer in the 798d2fbde5SJustin T. Gibbs * documentation and/or other materials provided with the distribution. 808d2fbde5SJustin T. Gibbs * 3. All advertising materials mentioning features or use of this software 818d2fbde5SJustin T. Gibbs * must display the following acknowledgement: 828d2fbde5SJustin T. Gibbs * This product includes software developed for the NetBSD Project 838d2fbde5SJustin T. Gibbs * by John M. Vinopal. 848d2fbde5SJustin T. Gibbs * 4. The name of the author may not be used to endorse or promote products 858d2fbde5SJustin T. Gibbs * derived from this software without specific prior written permission. 868d2fbde5SJustin T. Gibbs * 878d2fbde5SJustin T. Gibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 888d2fbde5SJustin T. Gibbs * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 898d2fbde5SJustin T. Gibbs * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 908d2fbde5SJustin T. Gibbs * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 918d2fbde5SJustin T. Gibbs * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 928d2fbde5SJustin T. Gibbs * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 938d2fbde5SJustin T. Gibbs * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 948d2fbde5SJustin T. Gibbs * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 958d2fbde5SJustin T. Gibbs * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 968d2fbde5SJustin T. Gibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 978d2fbde5SJustin T. Gibbs * SUCH DAMAGE. 988d2fbde5SJustin T. Gibbs */ 99dea673e9SRodney W. Grimes 100dea673e9SRodney W. Grimes 101dea673e9SRodney W. Grimes #include <sys/param.h> 1028d2fbde5SJustin T. Gibbs #include <sys/errno.h> 103190ff54eSPoul-Henning Kamp #include <sys/resource.h> 1045fc5ae5cSThomas Moestl #include <sys/sysctl.h> 105dea673e9SRodney W. Grimes 106dea673e9SRodney W. Grimes #include <ctype.h> 107cfed15e6SDimitry Andric #include <devstat.h> 108cfed15e6SDimitry Andric #include <err.h> 109dea673e9SRodney W. Grimes #include <fcntl.h> 110cfed15e6SDimitry Andric #include <inttypes.h> 111dea673e9SRodney W. Grimes #include <kvm.h> 112cfed15e6SDimitry Andric #include <limits.h> 113cfed15e6SDimitry Andric #include <math.h> 1142757068eSBruce Evans #include <nlist.h> 115da61e79cSAlan Somers #include <signal.h> 116dea673e9SRodney W. Grimes #include <stdio.h> 117dea673e9SRodney W. Grimes #include <stdlib.h> 118dea673e9SRodney W. Grimes #include <string.h> 11953fbcd3aSGiorgos Keramidas #include <termios.h> 120dea673e9SRodney W. Grimes #include <unistd.h> 121dea673e9SRodney W. Grimes 122a59c2129SAlan Somers static struct nlist namelist[] = { 1238cb3704aSSergey Kandaurov #define X_TTY_NIN 0 124a59c2129SAlan Somers { .n_name = "_tty_nin", 125a59c2129SAlan Somers .n_type = 0, .n_other = 0, .n_desc = 0, .n_value = 0 }, 1268cb3704aSSergey Kandaurov #define X_TTY_NOUT 1 127a59c2129SAlan Somers { .n_name = "_tty_nout", 128a59c2129SAlan Somers .n_type = 0, .n_other = 0, .n_desc = 0, .n_value = 0 }, 12932c9e3bdSJohn Baldwin #define X_BOOTTIME 2 130a59c2129SAlan Somers { .n_name = "_boottime", 131a59c2129SAlan Somers .n_type = 0, .n_other = 0, .n_desc = 0, .n_value = 0 }, 13232c9e3bdSJohn Baldwin #define X_END 2 133a59c2129SAlan Somers { .n_name = NULL, 134a59c2129SAlan Somers .n_type = 0, .n_other = 0, .n_desc = 0, .n_value = 0 }, 135dea673e9SRodney W. Grimes }; 136dea673e9SRodney W. Grimes 13753fbcd3aSGiorgos Keramidas #define IOSTAT_DEFAULT_ROWS 20 /* Traditional default `wrows' */ 13853fbcd3aSGiorgos Keramidas 139a59c2129SAlan Somers static struct statinfo cur, last; 140a59c2129SAlan Somers static int num_devices; 141a59c2129SAlan Somers static struct device_selection *dev_select; 142a59c2129SAlan Somers static int maxshowdevs; 143a59c2129SAlan Somers static volatile sig_atomic_t headercount; 144a59c2129SAlan Somers static volatile sig_atomic_t wresized; /* Tty resized, when non-zero. */ 145a59c2129SAlan Somers static volatile sig_atomic_t alarm_rang; 146a59c2129SAlan Somers static volatile sig_atomic_t return_requested; 147a59c2129SAlan Somers static unsigned short wrows; /* Current number of tty rows. */ 148a59c2129SAlan Somers static int dflag = 0, Iflag = 0, Cflag = 0, Tflag = 0, oflag = 0, Kflag = 0; 149a59c2129SAlan Somers static int xflag = 0, zflag = 0; 150dea673e9SRodney W. Grimes 1518d2fbde5SJustin T. Gibbs /* local function declarations */ 1528d2fbde5SJustin T. Gibbs static void usage(void); 153775b0074SGuido van Rooij static void needhdr(int signo); 15453fbcd3aSGiorgos Keramidas static void needresize(int signo); 155da61e79cSAlan Somers static void needreturn(int signo); 156da61e79cSAlan Somers static void alarm_clock(int signo); 15753fbcd3aSGiorgos Keramidas static void doresize(void); 158775b0074SGuido van Rooij static void phdr(void); 1595dc445bfSKenneth D. Merry static void devstats(int perf_select, long double etime, int havelast); 1608d2fbde5SJustin T. Gibbs static void cpustats(void); 1615dc445bfSKenneth D. Merry static int readvar(kvm_t *kd, const char *name, int nlid, void *ptr, 1625dc445bfSKenneth D. Merry size_t len); 163dea673e9SRodney W. Grimes 1648d2fbde5SJustin T. Gibbs static void 1658d2fbde5SJustin T. Gibbs usage(void) 1668d2fbde5SJustin T. Gibbs { 1678d2fbde5SJustin T. Gibbs /* 1688d2fbde5SJustin T. Gibbs * We also support the following 'traditional' syntax: 1698d2fbde5SJustin T. Gibbs * iostat [drives] [wait [count]] 1708d2fbde5SJustin T. Gibbs * This isn't mentioned in the man page, or the usage statement, 1718d2fbde5SJustin T. Gibbs * but it is supported. 1728d2fbde5SJustin T. Gibbs */ 1736c359123SMaxim Konovalov fprintf(stderr, "usage: iostat [-CdhIKoTxz?] [-c count] [-M core]" 1748743d3dcSPhilippe Charnier " [-n devs] [-N system]\n" 1758743d3dcSPhilippe Charnier "\t [-t type,if,pass] [-w wait] [drives]\n"); 1768d2fbde5SJustin T. Gibbs } 177dea673e9SRodney W. Grimes 178dea673e9SRodney W. Grimes int 1798d2fbde5SJustin T. Gibbs main(int argc, char **argv) 180dea673e9SRodney W. Grimes { 18147e065b4SMaxim Konovalov int c, i; 182*80badfcbSJohn Baldwin int hflag = 0, cflag = 0, wflag = 0, nflag = 0; 1838d2fbde5SJustin T. Gibbs int count = 0, waittime = 0; 1848d2fbde5SJustin T. Gibbs char *memf = NULL, *nlistf = NULL; 1858d2fbde5SJustin T. Gibbs struct devstat_match *matches; 186da61e79cSAlan Somers struct itimerval alarmspec; 1878d2fbde5SJustin T. Gibbs int num_matches = 0; 188dea673e9SRodney W. Grimes char errbuf[_POSIX2_LINE_MAX]; 1895fc5ae5cSThomas Moestl kvm_t *kd = NULL; 190bcc6a3daSKenneth D. Merry long generation; 1918d2fbde5SJustin T. Gibbs int num_devices_specified; 192bcc6a3daSKenneth D. Merry int num_selected, num_selections; 193bcc6a3daSKenneth D. Merry long select_generation; 1948d2fbde5SJustin T. Gibbs char **specified_devices; 1958d2fbde5SJustin T. Gibbs devstat_select_mode select_mode; 1968b10473dSSean Bruno float f; 1975dc445bfSKenneth D. Merry int havelast = 0; 198dea673e9SRodney W. Grimes 1998d2fbde5SJustin T. Gibbs matches = NULL; 2008d2fbde5SJustin T. Gibbs maxshowdevs = 3; 2018d2fbde5SJustin T. Gibbs 2026c359123SMaxim Konovalov while ((c = getopt(argc, argv, "c:CdhIKM:n:N:ot:Tw:xz?")) != -1) { 2038d2fbde5SJustin T. Gibbs switch(c) { 204dea673e9SRodney W. Grimes case 'c': 2058d2fbde5SJustin T. Gibbs cflag++; 2068d2fbde5SJustin T. Gibbs count = atoi(optarg); 2078d2fbde5SJustin T. Gibbs if (count < 1) 2088d2fbde5SJustin T. Gibbs errx(1, "count %d is < 1", count); 2098d2fbde5SJustin T. Gibbs break; 2108d2fbde5SJustin T. Gibbs case 'C': 2118d2fbde5SJustin T. Gibbs Cflag++; 2128d2fbde5SJustin T. Gibbs break; 2138d2fbde5SJustin T. Gibbs case 'd': 2148d2fbde5SJustin T. Gibbs dflag++; 2158d2fbde5SJustin T. Gibbs break; 2168d2fbde5SJustin T. Gibbs case 'h': 2178d2fbde5SJustin T. Gibbs hflag++; 2188d2fbde5SJustin T. Gibbs break; 2198d2fbde5SJustin T. Gibbs case 'I': 2208d2fbde5SJustin T. Gibbs Iflag++; 221dea673e9SRodney W. Grimes break; 222e4d7fe02SKenneth D. Merry case 'K': 223e4d7fe02SKenneth D. Merry Kflag++; 224e4d7fe02SKenneth D. Merry break; 225dea673e9SRodney W. Grimes case 'M': 226dea673e9SRodney W. Grimes memf = optarg; 227dea673e9SRodney W. Grimes break; 2288d2fbde5SJustin T. Gibbs case 'n': 2298d2fbde5SJustin T. Gibbs nflag++; 2308d2fbde5SJustin T. Gibbs maxshowdevs = atoi(optarg); 2318d2fbde5SJustin T. Gibbs if (maxshowdevs < 0) 2325d4a8fa9SPhilippe Charnier errx(1, "number of devices %d is < 0", 2338d2fbde5SJustin T. Gibbs maxshowdevs); 2348d2fbde5SJustin T. Gibbs break; 235dea673e9SRodney W. Grimes case 'N': 236dea673e9SRodney W. Grimes nlistf = optarg; 237dea673e9SRodney W. Grimes break; 2388d2fbde5SJustin T. Gibbs case 'o': 2398d2fbde5SJustin T. Gibbs oflag++; 240dea673e9SRodney W. Grimes break; 2418d2fbde5SJustin T. Gibbs case 't': 2425fc5ae5cSThomas Moestl if (devstat_buildmatch(optarg, &matches, 2438d2fbde5SJustin T. Gibbs &num_matches) != 0) 2448d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 2458d2fbde5SJustin T. Gibbs break; 2468d2fbde5SJustin T. Gibbs case 'T': 2478d2fbde5SJustin T. Gibbs Tflag++; 2488d2fbde5SJustin T. Gibbs break; 2498d2fbde5SJustin T. Gibbs case 'w': 2508d2fbde5SJustin T. Gibbs wflag++; 2518b10473dSSean Bruno f = atof(optarg); 2528b10473dSSean Bruno waittime = f * 1000; 2538d2fbde5SJustin T. Gibbs if (waittime < 1) 2548b10473dSSean Bruno errx(1, "wait time is < 1ms"); 2558d2fbde5SJustin T. Gibbs break; 2566c359123SMaxim Konovalov case 'x': 2576c359123SMaxim Konovalov xflag++; 2586c359123SMaxim Konovalov break; 2596c359123SMaxim Konovalov case 'z': 2606c359123SMaxim Konovalov zflag++; 2616c359123SMaxim Konovalov break; 262dea673e9SRodney W. Grimes default: 263dea673e9SRodney W. Grimes usage(); 2648743d3dcSPhilippe Charnier exit(1); 2658d2fbde5SJustin T. Gibbs break; 266dea673e9SRodney W. Grimes } 2678d2fbde5SJustin T. Gibbs } 2688d2fbde5SJustin T. Gibbs 269dea673e9SRodney W. Grimes argc -= optind; 270dea673e9SRodney W. Grimes argv += optind; 271dea673e9SRodney W. Grimes 2725dc445bfSKenneth D. Merry if (nlistf != NULL || memf != NULL) { 2735fc5ae5cSThomas Moestl kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 2745fc5ae5cSThomas Moestl 2755dc445bfSKenneth D. Merry if (kd == NULL) 2765fc5ae5cSThomas Moestl errx(1, "kvm_openfiles: %s", errbuf); 2775fc5ae5cSThomas Moestl 2785fc5ae5cSThomas Moestl if (kvm_nlist(kd, namelist) == -1) 2795fc5ae5cSThomas Moestl errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 2805fc5ae5cSThomas Moestl } 281dea673e9SRodney W. Grimes 2828d2fbde5SJustin T. Gibbs /* 2838d2fbde5SJustin T. Gibbs * Make sure that the userland devstat version matches the kernel 2848d2fbde5SJustin T. Gibbs * devstat version. If not, exit and print a message informing 2858d2fbde5SJustin T. Gibbs * the user of his mistake. 2868d2fbde5SJustin T. Gibbs */ 2875fc5ae5cSThomas Moestl if (devstat_checkversion(kd) < 0) 2888d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 2898d2fbde5SJustin T. Gibbs 2908d2fbde5SJustin T. Gibbs /* 2915dc445bfSKenneth D. Merry * Make sure Tflag and/or Cflag are set if dflag == 0. If dflag is 2925dc445bfSKenneth D. Merry * greater than 0, they may be 0 or non-zero. 2935dc445bfSKenneth D. Merry */ 2946c359123SMaxim Konovalov if (dflag == 0 && xflag == 0) { 2955dc445bfSKenneth D. Merry Cflag = 1; 2965dc445bfSKenneth D. Merry Tflag = 1; 2975dc445bfSKenneth D. Merry } 2985dc445bfSKenneth D. Merry 299dd9bccc4SMaxim Konovalov /* find out how many devices we have */ 300dd9bccc4SMaxim Konovalov if ((num_devices = devstat_getnumdevs(kd)) < 0) 301dd9bccc4SMaxim Konovalov err(1, "can't get number of devices"); 302dd9bccc4SMaxim Konovalov 3035dc445bfSKenneth D. Merry /* 3048d2fbde5SJustin T. Gibbs * Figure out how many devices we should display. 3058d2fbde5SJustin T. Gibbs */ 3068d2fbde5SJustin T. Gibbs if (nflag == 0) { 307dd9bccc4SMaxim Konovalov if (xflag > 0) 308dd9bccc4SMaxim Konovalov maxshowdevs = num_devices; 309dd9bccc4SMaxim Konovalov else if (oflag > 0) { 3108d2fbde5SJustin T. Gibbs if ((dflag > 0) && (Cflag == 0) && (Tflag == 0)) 3118d2fbde5SJustin T. Gibbs maxshowdevs = 5; 3128d2fbde5SJustin T. Gibbs else if ((dflag > 0) && (Tflag > 0) && (Cflag == 0)) 3138d2fbde5SJustin T. Gibbs maxshowdevs = 5; 3148d2fbde5SJustin T. Gibbs else 3158d2fbde5SJustin T. Gibbs maxshowdevs = 4; 3168d2fbde5SJustin T. Gibbs } else { 3178d2fbde5SJustin T. Gibbs if ((dflag > 0) && (Cflag == 0)) 3188d2fbde5SJustin T. Gibbs maxshowdevs = 4; 3198d2fbde5SJustin T. Gibbs else 3208d2fbde5SJustin T. Gibbs maxshowdevs = 3; 3218d2fbde5SJustin T. Gibbs } 3228d2fbde5SJustin T. Gibbs } 3238d2fbde5SJustin T. Gibbs 3244f6e1368SXin LI cur.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo)); 3255dc445bfSKenneth D. Merry if (cur.dinfo == NULL) 3264f6e1368SXin LI err(1, "calloc failed"); 3275dc445bfSKenneth D. Merry 3284f6e1368SXin LI last.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo)); 3295dc445bfSKenneth D. Merry if (last.dinfo == NULL) 3304f6e1368SXin LI err(1, "calloc failed"); 3318d2fbde5SJustin T. Gibbs 3328d2fbde5SJustin T. Gibbs /* 3338d2fbde5SJustin T. Gibbs * Grab all the devices. We don't look to see if the list has 3348d2fbde5SJustin T. Gibbs * changed here, since it almost certainly has. We only look for 3358d2fbde5SJustin T. Gibbs * errors. 3368d2fbde5SJustin T. Gibbs */ 3375fc5ae5cSThomas Moestl if (devstat_getdevs(kd, &cur) == -1) 3388d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 3398d2fbde5SJustin T. Gibbs 3408d2fbde5SJustin T. Gibbs num_devices = cur.dinfo->numdevs; 3418d2fbde5SJustin T. Gibbs generation = cur.dinfo->generation; 3428d2fbde5SJustin T. Gibbs 3438d2fbde5SJustin T. Gibbs /* 3448d2fbde5SJustin T. Gibbs * If the user specified any devices on the command line, see if 3458d2fbde5SJustin T. Gibbs * they are in the list of devices we have now. 3468d2fbde5SJustin T. Gibbs */ 3475dc445bfSKenneth D. Merry specified_devices = (char **)malloc(sizeof(char *)); 3485dc445bfSKenneth D. Merry if (specified_devices == NULL) 3495dc445bfSKenneth D. Merry err(1, "malloc failed"); 3505dc445bfSKenneth D. Merry 3518d2fbde5SJustin T. Gibbs for (num_devices_specified = 0; *argv; ++argv) { 3528d2fbde5SJustin T. Gibbs if (isdigit(**argv)) 3538d2fbde5SJustin T. Gibbs break; 3548d2fbde5SJustin T. Gibbs num_devices_specified++; 3558d2fbde5SJustin T. Gibbs specified_devices = (char **)realloc(specified_devices, 3568d2fbde5SJustin T. Gibbs sizeof(char *) * 3578d2fbde5SJustin T. Gibbs num_devices_specified); 3585dc445bfSKenneth D. Merry if (specified_devices == NULL) 3595dc445bfSKenneth D. Merry err(1, "realloc failed"); 3605dc445bfSKenneth D. Merry 3618d2fbde5SJustin T. Gibbs specified_devices[num_devices_specified - 1] = *argv; 3628d2fbde5SJustin T. Gibbs 3638d2fbde5SJustin T. Gibbs } 3642fa4aaa0SMatthew Dillon if (nflag == 0 && maxshowdevs < num_devices_specified) 3652fa4aaa0SMatthew Dillon maxshowdevs = num_devices_specified; 3668d2fbde5SJustin T. Gibbs 3678d2fbde5SJustin T. Gibbs dev_select = NULL; 3688d2fbde5SJustin T. Gibbs 3698d2fbde5SJustin T. Gibbs if ((num_devices_specified == 0) && (num_matches == 0)) 3708d2fbde5SJustin T. Gibbs select_mode = DS_SELECT_ADD; 3718d2fbde5SJustin T. Gibbs else 3728d2fbde5SJustin T. Gibbs select_mode = DS_SELECT_ONLY; 3738d2fbde5SJustin T. Gibbs 3748d2fbde5SJustin T. Gibbs /* 3758d2fbde5SJustin T. Gibbs * At this point, selectdevs will almost surely indicate that the 3768d2fbde5SJustin T. Gibbs * device list has changed, so we don't look for return values of 0 3778d2fbde5SJustin T. Gibbs * or 1. If we get back -1, though, there is an error. 3788d2fbde5SJustin T. Gibbs */ 3795fc5ae5cSThomas Moestl if (devstat_selectdevs(&dev_select, &num_selected, 3805fc5ae5cSThomas Moestl &num_selections, &select_generation, generation, 3815fc5ae5cSThomas Moestl cur.dinfo->devices, num_devices, matches, 3825fc5ae5cSThomas Moestl num_matches, specified_devices, 3835fc5ae5cSThomas Moestl num_devices_specified, select_mode, maxshowdevs, 3845fc5ae5cSThomas Moestl hflag) == -1) 3858d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 3868d2fbde5SJustin T. Gibbs 3878d2fbde5SJustin T. Gibbs /* 3888d2fbde5SJustin T. Gibbs * Look for the traditional wait time and count arguments. 3898d2fbde5SJustin T. Gibbs */ 3908d2fbde5SJustin T. Gibbs if (*argv) { 3918b10473dSSean Bruno f = atof(*argv); 3928b10473dSSean Bruno waittime = f * 1000; 3938d2fbde5SJustin T. Gibbs 3948d2fbde5SJustin T. Gibbs /* Let the user know he goofed, but keep going anyway */ 3958d2fbde5SJustin T. Gibbs if (wflag != 0) 3968d2fbde5SJustin T. Gibbs warnx("discarding previous wait interval, using" 3978b10473dSSean Bruno " %g instead", waittime / 1000.0); 3988d2fbde5SJustin T. Gibbs wflag++; 3998d2fbde5SJustin T. Gibbs 4008d2fbde5SJustin T. Gibbs if (*++argv) { 4018d2fbde5SJustin T. Gibbs count = atoi(*argv); 4028d2fbde5SJustin T. Gibbs if (cflag != 0) 4038d2fbde5SJustin T. Gibbs warnx("discarding previous count, using %d" 4048d2fbde5SJustin T. Gibbs " instead", count); 4058d2fbde5SJustin T. Gibbs cflag++; 4068d2fbde5SJustin T. Gibbs } else 4078d2fbde5SJustin T. Gibbs count = -1; 4088d2fbde5SJustin T. Gibbs } 4098d2fbde5SJustin T. Gibbs 4108d2fbde5SJustin T. Gibbs /* 4118d2fbde5SJustin T. Gibbs * If the user specified a count, but not an interval, we default 4128d2fbde5SJustin T. Gibbs * to an interval of 1 second. 4138d2fbde5SJustin T. Gibbs */ 4148d2fbde5SJustin T. Gibbs if ((wflag == 0) && (cflag > 0)) 4158b10473dSSean Bruno waittime = 1 * 1000; 4168d2fbde5SJustin T. Gibbs 4178d2fbde5SJustin T. Gibbs /* 4188d2fbde5SJustin T. Gibbs * If the user specified a wait time, but not a count, we want to 4198d2fbde5SJustin T. Gibbs * go on ad infinitum. This can be redundant if the user uses the 4208d2fbde5SJustin T. Gibbs * traditional method of specifying the wait, since in that case we 4218d2fbde5SJustin T. Gibbs * already set count = -1 above. Oh well. 4228d2fbde5SJustin T. Gibbs */ 4238d2fbde5SJustin T. Gibbs if ((wflag > 0) && (cflag == 0)) 4248d2fbde5SJustin T. Gibbs count = -1; 4258d2fbde5SJustin T. Gibbs 4269e5fbab2SJohn Baldwin bzero(cur.cp_time, sizeof(cur.cp_time)); 4275dc445bfSKenneth D. Merry cur.tk_nout = 0; 4285dc445bfSKenneth D. Merry cur.tk_nin = 0; 4295dc445bfSKenneth D. Merry 4305dc445bfSKenneth D. Merry /* 4317194d335SPoul-Henning Kamp * Set the snap time to the system boot time (ie: zero), so the 4327194d335SPoul-Henning Kamp * stats are calculated since system boot. 4335dc445bfSKenneth D. Merry */ 4347194d335SPoul-Henning Kamp cur.snap_time = 0; 4355dc445bfSKenneth D. Merry 436dea673e9SRodney W. Grimes /* 4378d2fbde5SJustin T. Gibbs * If the user stops the program (control-Z) and then resumes it, 4388d2fbde5SJustin T. Gibbs * print out the header again. 439dea673e9SRodney W. Grimes */ 440775b0074SGuido van Rooij (void)signal(SIGCONT, needhdr); 441dea673e9SRodney W. Grimes 44253fbcd3aSGiorgos Keramidas /* 44353fbcd3aSGiorgos Keramidas * If our standard output is a tty, then install a SIGWINCH handler 44453fbcd3aSGiorgos Keramidas * and set wresized so that our first iteration through the main 44553fbcd3aSGiorgos Keramidas * iostat loop will peek at the terminal's current rows to find out 44653fbcd3aSGiorgos Keramidas * how many lines can fit in a screenful of output. 44753fbcd3aSGiorgos Keramidas */ 44853fbcd3aSGiorgos Keramidas if (isatty(fileno(stdout)) != 0) { 44953fbcd3aSGiorgos Keramidas wresized = 1; 45053fbcd3aSGiorgos Keramidas (void)signal(SIGWINCH, needresize); 45153fbcd3aSGiorgos Keramidas } else { 45253fbcd3aSGiorgos Keramidas wresized = 0; 45353fbcd3aSGiorgos Keramidas wrows = IOSTAT_DEFAULT_ROWS; 45453fbcd3aSGiorgos Keramidas } 45553fbcd3aSGiorgos Keramidas 456da61e79cSAlan Somers /* 457da61e79cSAlan Somers * Register a SIGINT handler so that we can print out final statistics 458da61e79cSAlan Somers * when we get that signal 459da61e79cSAlan Somers */ 460da61e79cSAlan Somers (void)signal(SIGINT, needreturn); 461da61e79cSAlan Somers 462da61e79cSAlan Somers /* 463da61e79cSAlan Somers * Register a SIGALRM handler to implement sleeps if the user uses the 464da61e79cSAlan Somers * -c or -w options 465da61e79cSAlan Somers */ 466da61e79cSAlan Somers (void)signal(SIGALRM, alarm_clock); 467da61e79cSAlan Somers alarmspec.it_interval.tv_sec = waittime / 1000; 468da61e79cSAlan Somers alarmspec.it_interval.tv_usec = 1000 * (waittime % 1000); 469da61e79cSAlan Somers alarmspec.it_value.tv_sec = waittime / 1000; 470da61e79cSAlan Somers alarmspec.it_value.tv_usec = 1000 * (waittime % 1000); 471da61e79cSAlan Somers setitimer(ITIMER_REAL, &alarmspec, NULL); 472da61e79cSAlan Somers 4738d2fbde5SJustin T. Gibbs for (headercount = 1;;) { 4748d2fbde5SJustin T. Gibbs struct devinfo *tmp_dinfo; 4758d2fbde5SJustin T. Gibbs long tmp; 4765dc445bfSKenneth D. Merry long double etime; 477da61e79cSAlan Somers sigset_t sigmask, oldsigmask; 4785dc445bfSKenneth D. Merry 4795dc445bfSKenneth D. Merry if (Tflag > 0) { 4808cb3704aSSergey Kandaurov if ((readvar(kd, "kern.tty_nin", X_TTY_NIN, &cur.tk_nin, 4815dc445bfSKenneth D. Merry sizeof(cur.tk_nin)) != 0) 4828cb3704aSSergey Kandaurov || (readvar(kd, "kern.tty_nout", X_TTY_NOUT, 4835dc445bfSKenneth D. Merry &cur.tk_nout, sizeof(cur.tk_nout))!= 0)) { 4845dc445bfSKenneth D. Merry Tflag = 0; 4855dc445bfSKenneth D. Merry warnx("disabling TTY statistics"); 4865dc445bfSKenneth D. Merry } 4875dc445bfSKenneth D. Merry } 4885dc445bfSKenneth D. Merry 4895dc445bfSKenneth D. Merry if (Cflag > 0) { 49032c9e3bdSJohn Baldwin if (kd == NULL) { 49132c9e3bdSJohn Baldwin if (readvar(kd, "kern.cp_time", 0, 49232c9e3bdSJohn Baldwin &cur.cp_time, sizeof(cur.cp_time)) != 0) 4935dc445bfSKenneth D. Merry Cflag = 0; 49432c9e3bdSJohn Baldwin } else { 49532c9e3bdSJohn Baldwin if (kvm_getcptime(kd, cur.cp_time) < 0) { 49632c9e3bdSJohn Baldwin warnx("kvm_getcptime: %s", 49732c9e3bdSJohn Baldwin kvm_geterr(kd)); 49832c9e3bdSJohn Baldwin Cflag = 0; 4995dc445bfSKenneth D. Merry } 5005dc445bfSKenneth D. Merry } 50132c9e3bdSJohn Baldwin if (Cflag == 0) 50232c9e3bdSJohn Baldwin warnx("disabling CPU time statistics"); 50332c9e3bdSJohn Baldwin } 5048d2fbde5SJustin T. Gibbs 5058d2fbde5SJustin T. Gibbs if (!--headercount) { 506775b0074SGuido van Rooij phdr(); 50753fbcd3aSGiorgos Keramidas if (wresized != 0) 50853fbcd3aSGiorgos Keramidas doresize(); 50953fbcd3aSGiorgos Keramidas headercount = wrows; 510dea673e9SRodney W. Grimes } 5118d2fbde5SJustin T. Gibbs 5128d2fbde5SJustin T. Gibbs tmp_dinfo = last.dinfo; 5138d2fbde5SJustin T. Gibbs last.dinfo = cur.dinfo; 5148d2fbde5SJustin T. Gibbs cur.dinfo = tmp_dinfo; 5158d2fbde5SJustin T. Gibbs 5167194d335SPoul-Henning Kamp last.snap_time = cur.snap_time; 5178d2fbde5SJustin T. Gibbs 5188d2fbde5SJustin T. Gibbs /* 5198d2fbde5SJustin T. Gibbs * Here what we want to do is refresh our device stats. 5205fc5ae5cSThomas Moestl * devstat_getdevs() returns 1 when the device list has changed. 5218d2fbde5SJustin T. Gibbs * If the device list has changed, we want to go through 5228d2fbde5SJustin T. Gibbs * the selection process again, in case a device that we 5238d2fbde5SJustin T. Gibbs * were previously displaying has gone away. 5248d2fbde5SJustin T. Gibbs */ 5255fc5ae5cSThomas Moestl switch (devstat_getdevs(kd, &cur)) { 5268d2fbde5SJustin T. Gibbs case -1: 5278d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 5288d2fbde5SJustin T. Gibbs break; 5298d2fbde5SJustin T. Gibbs case 1: { 5308d2fbde5SJustin T. Gibbs int retval; 5318d2fbde5SJustin T. Gibbs 5328d2fbde5SJustin T. Gibbs num_devices = cur.dinfo->numdevs; 5338d2fbde5SJustin T. Gibbs generation = cur.dinfo->generation; 5345fc5ae5cSThomas Moestl retval = devstat_selectdevs(&dev_select, &num_selected, 5355fc5ae5cSThomas Moestl &num_selections, 5365fc5ae5cSThomas Moestl &select_generation, 5375fc5ae5cSThomas Moestl generation, 5385fc5ae5cSThomas Moestl cur.dinfo->devices, 5395fc5ae5cSThomas Moestl num_devices, matches, 5405fc5ae5cSThomas Moestl num_matches, 5418d2fbde5SJustin T. Gibbs specified_devices, 5428d2fbde5SJustin T. Gibbs num_devices_specified, 5435fc5ae5cSThomas Moestl select_mode, maxshowdevs, 5445fc5ae5cSThomas Moestl hflag); 5458d2fbde5SJustin T. Gibbs switch(retval) { 5468d2fbde5SJustin T. Gibbs case -1: 5478d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 5488d2fbde5SJustin T. Gibbs break; 5498d2fbde5SJustin T. Gibbs case 1: 550775b0074SGuido van Rooij phdr(); 55153fbcd3aSGiorgos Keramidas if (wresized != 0) 55253fbcd3aSGiorgos Keramidas doresize(); 55353fbcd3aSGiorgos Keramidas headercount = wrows; 5548d2fbde5SJustin T. Gibbs break; 5558d2fbde5SJustin T. Gibbs default: 5568d2fbde5SJustin T. Gibbs break; 557dea673e9SRodney W. Grimes } 5588d2fbde5SJustin T. Gibbs break; 5598d2fbde5SJustin T. Gibbs } 5608d2fbde5SJustin T. Gibbs default: 5618d2fbde5SJustin T. Gibbs break; 5628d2fbde5SJustin T. Gibbs } 5638d2fbde5SJustin T. Gibbs 5648d2fbde5SJustin T. Gibbs /* 5658d2fbde5SJustin T. Gibbs * We only want to re-select devices if we're in 'top' 5668d2fbde5SJustin T. Gibbs * mode. This is the only mode where the devices selected 5678d2fbde5SJustin T. Gibbs * could actually change. 5688d2fbde5SJustin T. Gibbs */ 5698d2fbde5SJustin T. Gibbs if (hflag > 0) { 5708d2fbde5SJustin T. Gibbs int retval; 5715fc5ae5cSThomas Moestl retval = devstat_selectdevs(&dev_select, &num_selected, 5725fc5ae5cSThomas Moestl &num_selections, 5735fc5ae5cSThomas Moestl &select_generation, 5745fc5ae5cSThomas Moestl generation, 5755fc5ae5cSThomas Moestl cur.dinfo->devices, 5765fc5ae5cSThomas Moestl num_devices, matches, 5775fc5ae5cSThomas Moestl num_matches, 5788d2fbde5SJustin T. Gibbs specified_devices, 5798d2fbde5SJustin T. Gibbs num_devices_specified, 5805fc5ae5cSThomas Moestl select_mode, maxshowdevs, 5815fc5ae5cSThomas Moestl hflag); 5828d2fbde5SJustin T. Gibbs switch(retval) { 5838d2fbde5SJustin T. Gibbs case -1: 5848d2fbde5SJustin T. Gibbs errx(1,"%s", devstat_errbuf); 5858d2fbde5SJustin T. Gibbs break; 5868d2fbde5SJustin T. Gibbs case 1: 587775b0074SGuido van Rooij phdr(); 58853fbcd3aSGiorgos Keramidas if (wresized != 0) 58953fbcd3aSGiorgos Keramidas doresize(); 59053fbcd3aSGiorgos Keramidas headercount = wrows; 5918d2fbde5SJustin T. Gibbs break; 5928d2fbde5SJustin T. Gibbs default: 5938d2fbde5SJustin T. Gibbs break; 5948d2fbde5SJustin T. Gibbs } 5958d2fbde5SJustin T. Gibbs } 5968d2fbde5SJustin T. Gibbs 5975dc445bfSKenneth D. Merry if (Tflag > 0) { 598dea673e9SRodney W. Grimes tmp = cur.tk_nin; 599dea673e9SRodney W. Grimes cur.tk_nin -= last.tk_nin; 600dea673e9SRodney W. Grimes last.tk_nin = tmp; 601dea673e9SRodney W. Grimes tmp = cur.tk_nout; 602dea673e9SRodney W. Grimes cur.tk_nout -= last.tk_nout; 603dea673e9SRodney W. Grimes last.tk_nout = tmp; 604dea673e9SRodney W. Grimes } 6055dc445bfSKenneth D. Merry 6067194d335SPoul-Henning Kamp etime = cur.snap_time - last.snap_time; 6075dc445bfSKenneth D. Merry 608dea673e9SRodney W. Grimes if (etime == 0.0) 609dea673e9SRodney W. Grimes etime = 1.0; 6105dc445bfSKenneth D. Merry 6115dc445bfSKenneth D. Merry for (i = 0; i < CPUSTATES; i++) { 6125dc445bfSKenneth D. Merry tmp = cur.cp_time[i]; 6135dc445bfSKenneth D. Merry cur.cp_time[i] -= last.cp_time[i]; 6145dc445bfSKenneth D. Merry last.cp_time[i] = tmp; 6155dc445bfSKenneth D. Merry } 6165dc445bfSKenneth D. Merry 6176c359123SMaxim Konovalov if (xflag == 0 && Tflag > 0) 6185dc445bfSKenneth D. Merry printf("%4.0Lf %5.0Lf", cur.tk_nin / etime, 6198d2fbde5SJustin T. Gibbs cur.tk_nout / etime); 6205dc445bfSKenneth D. Merry 6215fc5ae5cSThomas Moestl devstats(hflag, etime, havelast); 6225dc445bfSKenneth D. Merry 6236c359123SMaxim Konovalov if (xflag == 0) { 6245dc445bfSKenneth D. Merry if (Cflag > 0) 625dea673e9SRodney W. Grimes cpustats(); 6265dc445bfSKenneth D. Merry 6278d2fbde5SJustin T. Gibbs printf("\n"); 6286c359123SMaxim Konovalov } 6298d2fbde5SJustin T. Gibbs fflush(stdout); 630dea673e9SRodney W. Grimes 631da61e79cSAlan Somers if ((count >= 0 && --count <= 0) || return_requested) 632dea673e9SRodney W. Grimes break; 6338d2fbde5SJustin T. Gibbs 634da61e79cSAlan Somers /* 635da61e79cSAlan Somers * Use sigsuspend to safely sleep until either signal is 636da61e79cSAlan Somers * received 637da61e79cSAlan Somers */ 638da61e79cSAlan Somers alarm_rang = 0; 639da61e79cSAlan Somers sigemptyset(&sigmask); 640da61e79cSAlan Somers sigaddset(&sigmask, SIGINT); 641da61e79cSAlan Somers sigaddset(&sigmask, SIGALRM); 642da61e79cSAlan Somers sigprocmask(SIG_BLOCK, &sigmask, &oldsigmask); 643da61e79cSAlan Somers while (! (alarm_rang || return_requested) ) { 644da61e79cSAlan Somers sigsuspend(&oldsigmask); 645da61e79cSAlan Somers } 646da61e79cSAlan Somers sigprocmask(SIG_UNBLOCK, &sigmask, NULL); 647da61e79cSAlan Somers 6485fc5ae5cSThomas Moestl havelast = 1; 649dea673e9SRodney W. Grimes } 6508d2fbde5SJustin T. Gibbs 651dea673e9SRodney W. Grimes exit(0); 652dea673e9SRodney W. Grimes } 653dea673e9SRodney W. Grimes 654775b0074SGuido van Rooij /* 655775b0074SGuido van Rooij * Force a header to be prepended to the next output. 656775b0074SGuido van Rooij */ 657775b0074SGuido van Rooij void 658a59c2129SAlan Somers needhdr(int signo __unused) 659dea673e9SRodney W. Grimes { 660e65cded7SKris Kennaway 661775b0074SGuido van Rooij headercount = 1; 662e65cded7SKris Kennaway } 663e65cded7SKris Kennaway 66453fbcd3aSGiorgos Keramidas /* 66553fbcd3aSGiorgos Keramidas * When the terminal is resized, force an update of the maximum number of rows 66653fbcd3aSGiorgos Keramidas * printed between each header repetition. Then force a new header to be 66753fbcd3aSGiorgos Keramidas * prepended to the next output. 66853fbcd3aSGiorgos Keramidas */ 66953fbcd3aSGiorgos Keramidas void 670a59c2129SAlan Somers needresize(int signo __unused) 67153fbcd3aSGiorgos Keramidas { 67253fbcd3aSGiorgos Keramidas 67353fbcd3aSGiorgos Keramidas wresized = 1; 67453fbcd3aSGiorgos Keramidas headercount = 1; 67553fbcd3aSGiorgos Keramidas } 67653fbcd3aSGiorgos Keramidas 67753fbcd3aSGiorgos Keramidas /* 678da61e79cSAlan Somers * Record the alarm so the main loop can break its sleep 679da61e79cSAlan Somers */ 680da61e79cSAlan Somers void 681a59c2129SAlan Somers alarm_clock(int signo __unused) 682da61e79cSAlan Somers { 683da61e79cSAlan Somers alarm_rang = 1; 684da61e79cSAlan Somers } 685da61e79cSAlan Somers 686da61e79cSAlan Somers /* 687da61e79cSAlan Somers * Request that the main loop exit soon 688da61e79cSAlan Somers */ 689da61e79cSAlan Somers void 690a59c2129SAlan Somers needreturn(int signo __unused) 691da61e79cSAlan Somers { 692da61e79cSAlan Somers return_requested = 1; 693da61e79cSAlan Somers } 694da61e79cSAlan Somers 695da61e79cSAlan Somers /* 69653fbcd3aSGiorgos Keramidas * Update the global `wrows' count of terminal rows. 69753fbcd3aSGiorgos Keramidas */ 69853fbcd3aSGiorgos Keramidas void 69953fbcd3aSGiorgos Keramidas doresize(void) 70053fbcd3aSGiorgos Keramidas { 70153fbcd3aSGiorgos Keramidas int status; 70253fbcd3aSGiorgos Keramidas struct winsize w; 70353fbcd3aSGiorgos Keramidas 70453fbcd3aSGiorgos Keramidas for (;;) { 70553fbcd3aSGiorgos Keramidas status = ioctl(fileno(stdout), TIOCGWINSZ, &w); 70653fbcd3aSGiorgos Keramidas if (status == -1 && errno == EINTR) 70753fbcd3aSGiorgos Keramidas continue; 70853fbcd3aSGiorgos Keramidas else if (status == -1) 70953fbcd3aSGiorgos Keramidas err(1, "ioctl"); 71053fbcd3aSGiorgos Keramidas if (w.ws_row > 3) 71153fbcd3aSGiorgos Keramidas wrows = w.ws_row - 3; 71253fbcd3aSGiorgos Keramidas else 71353fbcd3aSGiorgos Keramidas wrows = IOSTAT_DEFAULT_ROWS; 71453fbcd3aSGiorgos Keramidas break; 71553fbcd3aSGiorgos Keramidas } 71653fbcd3aSGiorgos Keramidas 71753fbcd3aSGiorgos Keramidas /* 71853fbcd3aSGiorgos Keramidas * Inhibit doresize() calls until we are rescheduled by SIGWINCH. 71953fbcd3aSGiorgos Keramidas */ 72053fbcd3aSGiorgos Keramidas wresized = 0; 72153fbcd3aSGiorgos Keramidas } 72253fbcd3aSGiorgos Keramidas 723e65cded7SKris Kennaway static void 724775b0074SGuido van Rooij phdr(void) 725e65cded7SKris Kennaway { 72647e065b4SMaxim Konovalov int i, printed; 727ab8ac08eSAdrian Chadd char devbuf[256]; 728dea673e9SRodney W. Grimes 7296c359123SMaxim Konovalov /* 7306c359123SMaxim Konovalov * If xflag is set, we need a per-loop header, not a page header, so 7316c359123SMaxim Konovalov * just return. We'll print the header in devstats(). 7326c359123SMaxim Konovalov */ 7336c359123SMaxim Konovalov if (xflag > 0) 7346c359123SMaxim Konovalov return; 7356c359123SMaxim Konovalov 7365dc445bfSKenneth D. Merry if (Tflag > 0) 737dea673e9SRodney W. Grimes (void)printf(" tty"); 7388d2fbde5SJustin T. Gibbs for (i = 0, printed=0;(i < num_devices) && (printed < maxshowdevs);i++){ 7398d2fbde5SJustin T. Gibbs int di; 7408d2fbde5SJustin T. Gibbs if ((dev_select[i].selected != 0) 7418d2fbde5SJustin T. Gibbs && (dev_select[i].selected <= maxshowdevs)) { 7428d2fbde5SJustin T. Gibbs di = dev_select[i].position; 743ab8ac08eSAdrian Chadd snprintf(devbuf, sizeof(devbuf), "%s%d", 744ab8ac08eSAdrian Chadd cur.dinfo->devices[di].device_name, 745ab8ac08eSAdrian Chadd cur.dinfo->devices[di].unit_number); 7468d2fbde5SJustin T. Gibbs if (oflag > 0) 747ab8ac08eSAdrian Chadd (void)printf("%13.6s ", devbuf); 7488d2fbde5SJustin T. Gibbs else 749ab8ac08eSAdrian Chadd printf("%16.6s ", devbuf); 7508d2fbde5SJustin T. Gibbs printed++; 7518d2fbde5SJustin T. Gibbs } 7528d2fbde5SJustin T. Gibbs } 7535dc445bfSKenneth D. Merry if (Cflag > 0) 7548d2fbde5SJustin T. Gibbs (void)printf(" cpu\n"); 7558d2fbde5SJustin T. Gibbs else 7568d2fbde5SJustin T. Gibbs (void)printf("\n"); 7578d2fbde5SJustin T. Gibbs 7585dc445bfSKenneth D. Merry if (Tflag > 0) 7598d2fbde5SJustin T. Gibbs (void)printf(" tin tout"); 7608d2fbde5SJustin T. Gibbs 7618d2fbde5SJustin T. Gibbs for (i=0, printed = 0;(i < num_devices) && (printed < maxshowdevs);i++){ 7628d2fbde5SJustin T. Gibbs if ((dev_select[i].selected != 0) 7638d2fbde5SJustin T. Gibbs && (dev_select[i].selected <= maxshowdevs)) { 7642fa4aaa0SMatthew Dillon if (oflag > 0) { 7658d2fbde5SJustin T. Gibbs if (Iflag == 0) 766dea673e9SRodney W. Grimes (void)printf(" sps tps msps "); 7678d2fbde5SJustin T. Gibbs else 7688d2fbde5SJustin T. Gibbs (void)printf(" blk xfr msps "); 7692fa4aaa0SMatthew Dillon } else { 7708d2fbde5SJustin T. Gibbs if (Iflag == 0) 7718d2fbde5SJustin T. Gibbs printf(" KB/t tps MB/s "); 7728d2fbde5SJustin T. Gibbs else 7738d2fbde5SJustin T. Gibbs printf(" KB/t xfrs MB "); 7742fa4aaa0SMatthew Dillon } 7758d2fbde5SJustin T. Gibbs printed++; 7768d2fbde5SJustin T. Gibbs } 7778d2fbde5SJustin T. Gibbs } 7785dc445bfSKenneth D. Merry if (Cflag > 0) 779936d754aSAndrey A. Chernov (void)printf(" us ni sy in id\n"); 7808d2fbde5SJustin T. Gibbs else 7818d2fbde5SJustin T. Gibbs printf("\n"); 782775b0074SGuido van Rooij 783dea673e9SRodney W. Grimes } 784dea673e9SRodney W. Grimes 7858d2fbde5SJustin T. Gibbs static void 7865dc445bfSKenneth D. Merry devstats(int perf_select, long double etime, int havelast) 787dea673e9SRodney W. Grimes { 78847e065b4SMaxim Konovalov int dn; 789abf607edSWill Andrews long double transfers_per_second, transfers_per_second_read; 790abf607edSWill Andrews long double transfers_per_second_write; 791abf607edSWill Andrews long double kb_per_transfer, mb_per_second, mb_per_second_read; 792abf607edSWill Andrews long double mb_per_second_write; 7938d2fbde5SJustin T. Gibbs u_int64_t total_bytes, total_transfers, total_blocks; 79430ff1266SMaxim Konovalov u_int64_t total_bytes_read, total_transfers_read; 79530ff1266SMaxim Konovalov u_int64_t total_bytes_write, total_transfers_write; 7963cfa2135SMikolaj Golub long double busy_pct, busy_time; 7976c359123SMaxim Konovalov u_int64_t queue_len; 798abf607edSWill Andrews long double total_mb, blocks_per_second, total_duration; 799abf607edSWill Andrews long double ms_per_other, ms_per_read, ms_per_write, ms_per_transaction; 8006c359123SMaxim Konovalov int firstline = 1; 80101368cfaSAlan Somers char *devicename; 8026c359123SMaxim Konovalov 8036c359123SMaxim Konovalov if (xflag > 0) { 8040484aa6dSAllan Jude if (Cflag > 0) { 8050484aa6dSAllan Jude printf(" cpu\n"); 8060484aa6dSAllan Jude printf(" us ni sy in id\n"); 8070484aa6dSAllan Jude cpustats(); 8080484aa6dSAllan Jude printf("\n"); 8090484aa6dSAllan Jude } 8106c359123SMaxim Konovalov printf(" extended device statistics "); 8116c359123SMaxim Konovalov if (Tflag > 0) 8126c359123SMaxim Konovalov printf(" tty "); 8136c359123SMaxim Konovalov printf("\n"); 8143cfa2135SMikolaj Golub if (Iflag == 0) { 815abf607edSWill Andrews printf("device r/s w/s kr/s kw/s " 816abf607edSWill Andrews " ms/r ms/w ms/o ms/t qlen %%b "); 8173cfa2135SMikolaj Golub } else { 8183cfa2135SMikolaj Golub printf("device r/i w/i kr/i" 8193cfa2135SMikolaj Golub " kw/i qlen tsvc_t/i sb/i "); 8203cfa2135SMikolaj Golub } 8216c359123SMaxim Konovalov if (Tflag > 0) 8226c359123SMaxim Konovalov printf("tin tout "); 8236c359123SMaxim Konovalov printf("\n"); 8246c359123SMaxim Konovalov } 825dea673e9SRodney W. Grimes 8268d2fbde5SJustin T. Gibbs for (dn = 0; dn < num_devices; dn++) { 8278d2fbde5SJustin T. Gibbs int di; 8288d2fbde5SJustin T. Gibbs 8298d2fbde5SJustin T. Gibbs if (((perf_select == 0) && (dev_select[dn].selected == 0)) 8308d2fbde5SJustin T. Gibbs || (dev_select[dn].selected > maxshowdevs)) 831dea673e9SRodney W. Grimes continue; 832dea673e9SRodney W. Grimes 8338d2fbde5SJustin T. Gibbs di = dev_select[dn].position; 834dea673e9SRodney W. Grimes 8355fc5ae5cSThomas Moestl if (devstat_compute_statistics(&cur.dinfo->devices[di], 8365dc445bfSKenneth D. Merry havelast ? &last.dinfo->devices[di] : NULL, etime, 8375fc5ae5cSThomas Moestl DSM_TOTAL_BYTES, &total_bytes, 83830ff1266SMaxim Konovalov DSM_TOTAL_BYTES_READ, &total_bytes_read, 83930ff1266SMaxim Konovalov DSM_TOTAL_BYTES_WRITE, &total_bytes_write, 8405fc5ae5cSThomas Moestl DSM_TOTAL_TRANSFERS, &total_transfers, 84130ff1266SMaxim Konovalov DSM_TOTAL_TRANSFERS_READ, &total_transfers_read, 84230ff1266SMaxim Konovalov DSM_TOTAL_TRANSFERS_WRITE, &total_transfers_write, 8435fc5ae5cSThomas Moestl DSM_TOTAL_BLOCKS, &total_blocks, 8445fc5ae5cSThomas Moestl DSM_KB_PER_TRANSFER, &kb_per_transfer, 8455fc5ae5cSThomas Moestl DSM_TRANSFERS_PER_SECOND, &transfers_per_second, 8466c359123SMaxim Konovalov DSM_TRANSFERS_PER_SECOND_READ, &transfers_per_second_read, 8476c359123SMaxim Konovalov DSM_TRANSFERS_PER_SECOND_WRITE, &transfers_per_second_write, 8485fc5ae5cSThomas Moestl DSM_MB_PER_SECOND, &mb_per_second, 8496c359123SMaxim Konovalov DSM_MB_PER_SECOND_READ, &mb_per_second_read, 8506c359123SMaxim Konovalov DSM_MB_PER_SECOND_WRITE, &mb_per_second_write, 8515fc5ae5cSThomas Moestl DSM_BLOCKS_PER_SECOND, &blocks_per_second, 8525fc5ae5cSThomas Moestl DSM_MS_PER_TRANSACTION, &ms_per_transaction, 853abf607edSWill Andrews DSM_MS_PER_TRANSACTION_READ, &ms_per_read, 854abf607edSWill Andrews DSM_MS_PER_TRANSACTION_WRITE, &ms_per_write, 855abf607edSWill Andrews DSM_MS_PER_TRANSACTION_OTHER, &ms_per_other, 8566c359123SMaxim Konovalov DSM_BUSY_PCT, &busy_pct, 8576c359123SMaxim Konovalov DSM_QUEUE_LENGTH, &queue_len, 8583cfa2135SMikolaj Golub DSM_TOTAL_DURATION, &total_duration, 8593cfa2135SMikolaj Golub DSM_TOTAL_BUSY_TIME, &busy_time, 8605fc5ae5cSThomas Moestl DSM_NONE) != 0) 8618d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 8628d2fbde5SJustin T. Gibbs 8638d2fbde5SJustin T. Gibbs if (perf_select != 0) { 8648d2fbde5SJustin T. Gibbs dev_select[dn].bytes = total_bytes; 8658d2fbde5SJustin T. Gibbs if ((dev_select[dn].selected == 0) 8668d2fbde5SJustin T. Gibbs || (dev_select[dn].selected > maxshowdevs)) 8678d2fbde5SJustin T. Gibbs continue; 8688d2fbde5SJustin T. Gibbs } 8698d2fbde5SJustin T. Gibbs 8706c359123SMaxim Konovalov if (Kflag > 0 || xflag > 0) { 8712fa4aaa0SMatthew Dillon int block_size = cur.dinfo->devices[di].block_size; 872e4d7fe02SKenneth D. Merry total_blocks = total_blocks * (block_size ? 873e4d7fe02SKenneth D. Merry block_size : 512) / 1024; 8742fa4aaa0SMatthew Dillon } 8752fa4aaa0SMatthew Dillon 8766c359123SMaxim Konovalov if (xflag > 0) { 87701368cfaSAlan Somers if (asprintf(&devicename, "%s%d", 8786c359123SMaxim Konovalov cur.dinfo->devices[di].device_name, 879e08b1089SMaxim Konovalov cur.dinfo->devices[di].unit_number) == -1) 880e08b1089SMaxim Konovalov err(1, "asprintf"); 8816c359123SMaxim Konovalov /* 8826c359123SMaxim Konovalov * If zflag is set, skip any devices with zero I/O. 8836c359123SMaxim Konovalov */ 8846c359123SMaxim Konovalov if (zflag == 0 || transfers_per_second_read > 0.05 || 8856c359123SMaxim Konovalov transfers_per_second_write > 0.05 || 8866c359123SMaxim Konovalov mb_per_second_read > ((long double).0005)/1024 || 8876c359123SMaxim Konovalov mb_per_second_write > ((long double).0005)/1024 || 8886c359123SMaxim Konovalov busy_pct > 0.5) { 88930ff1266SMaxim Konovalov if (Iflag == 0) 89061631b24SAlan Somers printf("%-8.8s %7.0Lf %7.0Lf %8.1Lf " 89161631b24SAlan Somers "%8.1Lf %5.0Lf %5.0Lf %5.0Lf %5.0Lf" 892abf607edSWill Andrews " %4" PRIu64 " %3.0Lf ", 89301368cfaSAlan Somers devicename, 89461631b24SAlan Somers transfers_per_second_read, 89561631b24SAlan Somers transfers_per_second_write, 8966c359123SMaxim Konovalov mb_per_second_read * 1024, 897fbe4fc2eSMaxim Konovalov mb_per_second_write * 1024, 89861631b24SAlan Somers ms_per_read, ms_per_write, 89961631b24SAlan Somers ms_per_other, 90061631b24SAlan Somers ms_per_transaction, 901abf607edSWill Andrews queue_len, busy_pct); 90230ff1266SMaxim Konovalov else 9033cfa2135SMikolaj Golub printf("%-8.8s %11.1Lf %11.1Lf " 9043cfa2135SMikolaj Golub "%12.1Lf %12.1Lf %4" PRIu64 9053cfa2135SMikolaj Golub " %10.1Lf %9.1Lf ", 90601368cfaSAlan Somers devicename, 907fbe4fc2eSMaxim Konovalov (long double)total_transfers_read, 90830ff1266SMaxim Konovalov (long double)total_transfers_write, 909fbe4fc2eSMaxim Konovalov (long double) 910fbe4fc2eSMaxim Konovalov total_bytes_read / 1024, 911fbe4fc2eSMaxim Konovalov (long double) 912fbe4fc2eSMaxim Konovalov total_bytes_write / 1024, 913fbe4fc2eSMaxim Konovalov queue_len, 9143cfa2135SMikolaj Golub total_duration, busy_time); 9156c359123SMaxim Konovalov if (firstline) { 9166c359123SMaxim Konovalov /* 9176c359123SMaxim Konovalov * If this is the first device 9186c359123SMaxim Konovalov * we're printing, also print 9196c359123SMaxim Konovalov * CPU or TTY stats if requested. 9206c359123SMaxim Konovalov */ 9216c359123SMaxim Konovalov firstline = 0; 9226c359123SMaxim Konovalov if (Tflag > 0) 9236c359123SMaxim Konovalov printf("%4.0Lf%5.0Lf", 9246c359123SMaxim Konovalov cur.tk_nin / etime, 9256c359123SMaxim Konovalov cur.tk_nout / etime); 9266c359123SMaxim Konovalov } 9276c359123SMaxim Konovalov printf("\n"); 9286c359123SMaxim Konovalov } 92901368cfaSAlan Somers free(devicename); 9306c359123SMaxim Konovalov } else if (oflag > 0) { 931125de826SEdward Tomasz Napierala int msdig = (ms_per_transaction < 99.94) ? 1 : 0; 9328d2fbde5SJustin T. Gibbs 9338d2fbde5SJustin T. Gibbs if (Iflag == 0) 9342fa4aaa0SMatthew Dillon printf("%4.0Lf%4.0Lf%5.*Lf ", 9358d2fbde5SJustin T. Gibbs blocks_per_second, 9368d2fbde5SJustin T. Gibbs transfers_per_second, 9372fa4aaa0SMatthew Dillon msdig, 9388d2fbde5SJustin T. Gibbs ms_per_transaction); 939dea673e9SRodney W. Grimes else 940cfed15e6SDimitry Andric printf("%4.1" PRIu64 "%4.1" PRIu64 "%5.*Lf ", 9418d2fbde5SJustin T. Gibbs total_blocks, 9428d2fbde5SJustin T. Gibbs total_transfers, 9432fa4aaa0SMatthew Dillon msdig, 9448d2fbde5SJustin T. Gibbs ms_per_transaction); 9458d2fbde5SJustin T. Gibbs } else { 9468d2fbde5SJustin T. Gibbs if (Iflag == 0) 94783743daeSEdward Tomasz Napierala printf(" %4.*Lf %4.0Lf %5.*Lf ", 94883743daeSEdward Tomasz Napierala kb_per_transfer >= 100 ? 0 : 1, 9498d2fbde5SJustin T. Gibbs kb_per_transfer, 9508d2fbde5SJustin T. Gibbs transfers_per_second, 95183743daeSEdward Tomasz Napierala mb_per_second >= 1000 ? 0 : 1, 9528d2fbde5SJustin T. Gibbs mb_per_second); 9538d2fbde5SJustin T. Gibbs else { 9548d2fbde5SJustin T. Gibbs total_mb = total_bytes; 9558d2fbde5SJustin T. Gibbs total_mb /= 1024 * 1024; 9568d2fbde5SJustin T. Gibbs 957100b0345SEdward Tomasz Napierala printf(" %4.1Lf %4.1" PRIu64 " %5.2Lf ", 9588d2fbde5SJustin T. Gibbs kb_per_transfer, 9598d2fbde5SJustin T. Gibbs total_transfers, 9608d2fbde5SJustin T. Gibbs total_mb); 9618d2fbde5SJustin T. Gibbs } 9628d2fbde5SJustin T. Gibbs } 963dea673e9SRodney W. Grimes } 9646c359123SMaxim Konovalov if (xflag > 0 && zflag > 0 && firstline == 1 && 9656c359123SMaxim Konovalov (Tflag > 0 || Cflag > 0)) { 9666c359123SMaxim Konovalov /* 9676c359123SMaxim Konovalov * If zflag is set and we did not print any device 9686c359123SMaxim Konovalov * lines I/O because they were all zero, 9696c359123SMaxim Konovalov * print TTY/CPU stats. 9706c359123SMaxim Konovalov */ 9716c359123SMaxim Konovalov printf("%52s",""); 9726c359123SMaxim Konovalov if (Tflag > 0) 9736c359123SMaxim Konovalov printf("%4.0Lf %5.0Lf", cur.tk_nin / etime, 9746c359123SMaxim Konovalov cur.tk_nout / etime); 9756c359123SMaxim Konovalov if (Cflag > 0) 9766c359123SMaxim Konovalov cpustats(); 9776c359123SMaxim Konovalov printf("\n"); 9786c359123SMaxim Konovalov } 979dea673e9SRodney W. Grimes } 980dea673e9SRodney W. Grimes 9818d2fbde5SJustin T. Gibbs static void 9828d2fbde5SJustin T. Gibbs cpustats(void) 983dea673e9SRodney W. Grimes { 98447e065b4SMaxim Konovalov int state; 98501368cfaSAlan Somers double cptime; 986dea673e9SRodney W. Grimes 98701368cfaSAlan Somers cptime = 0.0; 9888d2fbde5SJustin T. Gibbs 989dea673e9SRodney W. Grimes for (state = 0; state < CPUSTATES; ++state) 99001368cfaSAlan Somers cptime += cur.cp_time[state]; 991dea673e9SRodney W. Grimes for (state = 0; state < CPUSTATES; ++state) 9928fa6b8a8SBruce Evans printf(" %2.0f", 99301368cfaSAlan Somers rint(100. * cur.cp_time[state] / (cptime ? cptime : 1))); 994dea673e9SRodney W. Grimes } 9955fc5ae5cSThomas Moestl 9965dc445bfSKenneth D. Merry static int 9975dc445bfSKenneth D. Merry readvar(kvm_t *kd, const char *name, int nlid, void *ptr, size_t len) 9985fc5ae5cSThomas Moestl { 9995dc445bfSKenneth D. Merry if (kd != NULL) { 10005dc445bfSKenneth D. Merry ssize_t nbytes; 10015dc445bfSKenneth D. Merry 10028d56a4caSJohn Baldwin nbytes = kvm_read(kd, namelist[nlid].n_value, ptr, len); 10035dc445bfSKenneth D. Merry 10048d56a4caSJohn Baldwin if (nbytes < 0) { 10058d56a4caSJohn Baldwin warnx("kvm_read(%s): %s", namelist[nlid].n_name, 10068d56a4caSJohn Baldwin kvm_geterr(kd)); 10075dc445bfSKenneth D. Merry return (1); 1008a59c2129SAlan Somers } else if ((size_t)nbytes != len) { 10098d56a4caSJohn Baldwin warnx("kvm_read(%s): expected %zu bytes, got %zd bytes", 10108d56a4caSJohn Baldwin namelist[nlid].n_name, len, nbytes); 10115dc445bfSKenneth D. Merry return (1); 10125dc445bfSKenneth D. Merry } 10135dc445bfSKenneth D. Merry } else { 10145fc5ae5cSThomas Moestl size_t nlen = len; 10155fc5ae5cSThomas Moestl 10165dc445bfSKenneth D. Merry if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) { 10175dc445bfSKenneth D. Merry warn("sysctl(%s...) failed", name); 10185dc445bfSKenneth D. Merry return (1); 10195dc445bfSKenneth D. Merry } 10205dc445bfSKenneth D. Merry if (nlen != len) { 10215dc445bfSKenneth D. Merry warnx("sysctl(%s...): expected %lu, got %lu", name, 10225fc5ae5cSThomas Moestl (unsigned long)len, (unsigned long)nlen); 10235dc445bfSKenneth D. Merry return (1); 10245dc445bfSKenneth D. Merry } 10255dc445bfSKenneth D. Merry } 10265dc445bfSKenneth D. Merry return (0); 10275fc5ae5cSThomas Moestl } 1028