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 */ 152*a9cce232SAlfonso Gregory static void usage(void) __dead2; 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"); 176*a9cce232SAlfonso Gregory exit(1); 1778d2fbde5SJustin T. Gibbs } 178dea673e9SRodney W. Grimes 179dea673e9SRodney W. Grimes int 1808d2fbde5SJustin T. Gibbs main(int argc, char **argv) 181dea673e9SRodney W. Grimes { 18247e065b4SMaxim Konovalov int c, i; 18380badfcbSJohn Baldwin int hflag = 0, cflag = 0, wflag = 0, nflag = 0; 1848d2fbde5SJustin T. Gibbs int count = 0, waittime = 0; 1858d2fbde5SJustin T. Gibbs char *memf = NULL, *nlistf = NULL; 1868d2fbde5SJustin T. Gibbs struct devstat_match *matches; 187da61e79cSAlan Somers struct itimerval alarmspec; 1888d2fbde5SJustin T. Gibbs int num_matches = 0; 189dea673e9SRodney W. Grimes char errbuf[_POSIX2_LINE_MAX]; 1905fc5ae5cSThomas Moestl kvm_t *kd = NULL; 191bcc6a3daSKenneth D. Merry long generation; 1928d2fbde5SJustin T. Gibbs int num_devices_specified; 193bcc6a3daSKenneth D. Merry int num_selected, num_selections; 194bcc6a3daSKenneth D. Merry long select_generation; 1958d2fbde5SJustin T. Gibbs char **specified_devices; 1968d2fbde5SJustin T. Gibbs devstat_select_mode select_mode; 1978b10473dSSean Bruno float f; 1985dc445bfSKenneth D. Merry int havelast = 0; 199dea673e9SRodney W. Grimes 2008d2fbde5SJustin T. Gibbs matches = NULL; 2018d2fbde5SJustin T. Gibbs maxshowdevs = 3; 2028d2fbde5SJustin T. Gibbs 2036c359123SMaxim Konovalov while ((c = getopt(argc, argv, "c:CdhIKM:n:N:ot:Tw:xz?")) != -1) { 2048d2fbde5SJustin T. Gibbs switch(c) { 205dea673e9SRodney W. Grimes case 'c': 2068d2fbde5SJustin T. Gibbs cflag++; 2078d2fbde5SJustin T. Gibbs count = atoi(optarg); 2088d2fbde5SJustin T. Gibbs if (count < 1) 2098d2fbde5SJustin T. Gibbs errx(1, "count %d is < 1", count); 2108d2fbde5SJustin T. Gibbs break; 2118d2fbde5SJustin T. Gibbs case 'C': 2128d2fbde5SJustin T. Gibbs Cflag++; 2138d2fbde5SJustin T. Gibbs break; 2148d2fbde5SJustin T. Gibbs case 'd': 2158d2fbde5SJustin T. Gibbs dflag++; 2168d2fbde5SJustin T. Gibbs break; 2178d2fbde5SJustin T. Gibbs case 'h': 2188d2fbde5SJustin T. Gibbs hflag++; 2198d2fbde5SJustin T. Gibbs break; 2208d2fbde5SJustin T. Gibbs case 'I': 2218d2fbde5SJustin T. Gibbs Iflag++; 222dea673e9SRodney W. Grimes break; 223e4d7fe02SKenneth D. Merry case 'K': 224e4d7fe02SKenneth D. Merry Kflag++; 225e4d7fe02SKenneth D. Merry break; 226dea673e9SRodney W. Grimes case 'M': 227dea673e9SRodney W. Grimes memf = optarg; 228dea673e9SRodney W. Grimes break; 2298d2fbde5SJustin T. Gibbs case 'n': 2308d2fbde5SJustin T. Gibbs nflag++; 2318d2fbde5SJustin T. Gibbs maxshowdevs = atoi(optarg); 2328d2fbde5SJustin T. Gibbs if (maxshowdevs < 0) 2335d4a8fa9SPhilippe Charnier errx(1, "number of devices %d is < 0", 2348d2fbde5SJustin T. Gibbs maxshowdevs); 2358d2fbde5SJustin T. Gibbs break; 236dea673e9SRodney W. Grimes case 'N': 237dea673e9SRodney W. Grimes nlistf = optarg; 238dea673e9SRodney W. Grimes break; 2398d2fbde5SJustin T. Gibbs case 'o': 2408d2fbde5SJustin T. Gibbs oflag++; 241dea673e9SRodney W. Grimes break; 2428d2fbde5SJustin T. Gibbs case 't': 2435fc5ae5cSThomas Moestl if (devstat_buildmatch(optarg, &matches, 2448d2fbde5SJustin T. Gibbs &num_matches) != 0) 2458d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 2468d2fbde5SJustin T. Gibbs break; 2478d2fbde5SJustin T. Gibbs case 'T': 2488d2fbde5SJustin T. Gibbs Tflag++; 2498d2fbde5SJustin T. Gibbs break; 2508d2fbde5SJustin T. Gibbs case 'w': 2518d2fbde5SJustin T. Gibbs wflag++; 2528b10473dSSean Bruno f = atof(optarg); 2538b10473dSSean Bruno waittime = f * 1000; 2548d2fbde5SJustin T. Gibbs if (waittime < 1) 2558b10473dSSean Bruno errx(1, "wait time is < 1ms"); 2568d2fbde5SJustin T. Gibbs break; 2576c359123SMaxim Konovalov case 'x': 2586c359123SMaxim Konovalov xflag++; 2596c359123SMaxim Konovalov break; 2606c359123SMaxim Konovalov case 'z': 2616c359123SMaxim Konovalov zflag++; 2626c359123SMaxim Konovalov break; 263dea673e9SRodney W. Grimes default: 264dea673e9SRodney W. Grimes usage(); 265dea673e9SRodney W. Grimes } 2668d2fbde5SJustin T. Gibbs } 2678d2fbde5SJustin T. Gibbs 268dea673e9SRodney W. Grimes argc -= optind; 269dea673e9SRodney W. Grimes argv += optind; 270dea673e9SRodney W. Grimes 2715dc445bfSKenneth D. Merry if (nlistf != NULL || memf != NULL) { 2725fc5ae5cSThomas Moestl kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 2735fc5ae5cSThomas Moestl 2745dc445bfSKenneth D. Merry if (kd == NULL) 2755fc5ae5cSThomas Moestl errx(1, "kvm_openfiles: %s", errbuf); 2765fc5ae5cSThomas Moestl 2775fc5ae5cSThomas Moestl if (kvm_nlist(kd, namelist) == -1) 2785fc5ae5cSThomas Moestl errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 2795fc5ae5cSThomas Moestl } 280dea673e9SRodney W. Grimes 2818d2fbde5SJustin T. Gibbs /* 2828d2fbde5SJustin T. Gibbs * Make sure that the userland devstat version matches the kernel 2838d2fbde5SJustin T. Gibbs * devstat version. If not, exit and print a message informing 2848d2fbde5SJustin T. Gibbs * the user of his mistake. 2858d2fbde5SJustin T. Gibbs */ 2865fc5ae5cSThomas Moestl if (devstat_checkversion(kd) < 0) 2878d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 2888d2fbde5SJustin T. Gibbs 2898d2fbde5SJustin T. Gibbs /* 2905dc445bfSKenneth D. Merry * Make sure Tflag and/or Cflag are set if dflag == 0. If dflag is 2915dc445bfSKenneth D. Merry * greater than 0, they may be 0 or non-zero. 2925dc445bfSKenneth D. Merry */ 2936c359123SMaxim Konovalov if (dflag == 0 && xflag == 0) { 2945dc445bfSKenneth D. Merry Cflag = 1; 2955dc445bfSKenneth D. Merry Tflag = 1; 2965dc445bfSKenneth D. Merry } 2975dc445bfSKenneth D. Merry 298dd9bccc4SMaxim Konovalov /* find out how many devices we have */ 299dd9bccc4SMaxim Konovalov if ((num_devices = devstat_getnumdevs(kd)) < 0) 300dd9bccc4SMaxim Konovalov err(1, "can't get number of devices"); 301dd9bccc4SMaxim Konovalov 3025dc445bfSKenneth D. Merry /* 3038d2fbde5SJustin T. Gibbs * Figure out how many devices we should display. 3048d2fbde5SJustin T. Gibbs */ 3058d2fbde5SJustin T. Gibbs if (nflag == 0) { 306dd9bccc4SMaxim Konovalov if (xflag > 0) 307dd9bccc4SMaxim Konovalov maxshowdevs = num_devices; 308dd9bccc4SMaxim Konovalov else if (oflag > 0) { 3098d2fbde5SJustin T. Gibbs if ((dflag > 0) && (Cflag == 0) && (Tflag == 0)) 3108d2fbde5SJustin T. Gibbs maxshowdevs = 5; 3118d2fbde5SJustin T. Gibbs else if ((dflag > 0) && (Tflag > 0) && (Cflag == 0)) 3128d2fbde5SJustin T. Gibbs maxshowdevs = 5; 3138d2fbde5SJustin T. Gibbs else 3148d2fbde5SJustin T. Gibbs maxshowdevs = 4; 3158d2fbde5SJustin T. Gibbs } else { 3168d2fbde5SJustin T. Gibbs if ((dflag > 0) && (Cflag == 0)) 3178d2fbde5SJustin T. Gibbs maxshowdevs = 4; 3188d2fbde5SJustin T. Gibbs else 3198d2fbde5SJustin T. Gibbs maxshowdevs = 3; 3208d2fbde5SJustin T. Gibbs } 3218d2fbde5SJustin T. Gibbs } 3228d2fbde5SJustin T. Gibbs 3234f6e1368SXin LI cur.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo)); 3245dc445bfSKenneth D. Merry if (cur.dinfo == NULL) 3254f6e1368SXin LI err(1, "calloc failed"); 3265dc445bfSKenneth D. Merry 3274f6e1368SXin LI last.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo)); 3285dc445bfSKenneth D. Merry if (last.dinfo == NULL) 3294f6e1368SXin LI err(1, "calloc failed"); 3308d2fbde5SJustin T. Gibbs 3318d2fbde5SJustin T. Gibbs /* 3328d2fbde5SJustin T. Gibbs * Grab all the devices. We don't look to see if the list has 3338d2fbde5SJustin T. Gibbs * changed here, since it almost certainly has. We only look for 3348d2fbde5SJustin T. Gibbs * errors. 3358d2fbde5SJustin T. Gibbs */ 3365fc5ae5cSThomas Moestl if (devstat_getdevs(kd, &cur) == -1) 3378d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 3388d2fbde5SJustin T. Gibbs 3398d2fbde5SJustin T. Gibbs num_devices = cur.dinfo->numdevs; 3408d2fbde5SJustin T. Gibbs generation = cur.dinfo->generation; 3418d2fbde5SJustin T. Gibbs 3428d2fbde5SJustin T. Gibbs /* 3438d2fbde5SJustin T. Gibbs * If the user specified any devices on the command line, see if 3448d2fbde5SJustin T. Gibbs * they are in the list of devices we have now. 3458d2fbde5SJustin T. Gibbs */ 3465dc445bfSKenneth D. Merry specified_devices = (char **)malloc(sizeof(char *)); 3475dc445bfSKenneth D. Merry if (specified_devices == NULL) 3485dc445bfSKenneth D. Merry err(1, "malloc failed"); 3495dc445bfSKenneth D. Merry 3508d2fbde5SJustin T. Gibbs for (num_devices_specified = 0; *argv; ++argv) { 3518d2fbde5SJustin T. Gibbs if (isdigit(**argv)) 3528d2fbde5SJustin T. Gibbs break; 3538d2fbde5SJustin T. Gibbs num_devices_specified++; 3548d2fbde5SJustin T. Gibbs specified_devices = (char **)realloc(specified_devices, 3558d2fbde5SJustin T. Gibbs sizeof(char *) * 3568d2fbde5SJustin T. Gibbs num_devices_specified); 3575dc445bfSKenneth D. Merry if (specified_devices == NULL) 3585dc445bfSKenneth D. Merry err(1, "realloc failed"); 3595dc445bfSKenneth D. Merry 3608d2fbde5SJustin T. Gibbs specified_devices[num_devices_specified - 1] = *argv; 3618d2fbde5SJustin T. Gibbs 3628d2fbde5SJustin T. Gibbs } 3632fa4aaa0SMatthew Dillon if (nflag == 0 && maxshowdevs < num_devices_specified) 3642fa4aaa0SMatthew Dillon maxshowdevs = num_devices_specified; 3658d2fbde5SJustin T. Gibbs 3668d2fbde5SJustin T. Gibbs dev_select = NULL; 3678d2fbde5SJustin T. Gibbs 3688d2fbde5SJustin T. Gibbs if ((num_devices_specified == 0) && (num_matches == 0)) 3698d2fbde5SJustin T. Gibbs select_mode = DS_SELECT_ADD; 3708d2fbde5SJustin T. Gibbs else 3718d2fbde5SJustin T. Gibbs select_mode = DS_SELECT_ONLY; 3728d2fbde5SJustin T. Gibbs 3738d2fbde5SJustin T. Gibbs /* 3748d2fbde5SJustin T. Gibbs * At this point, selectdevs will almost surely indicate that the 3758d2fbde5SJustin T. Gibbs * device list has changed, so we don't look for return values of 0 3768d2fbde5SJustin T. Gibbs * or 1. If we get back -1, though, there is an error. 3778d2fbde5SJustin T. Gibbs */ 3785fc5ae5cSThomas Moestl if (devstat_selectdevs(&dev_select, &num_selected, 3795fc5ae5cSThomas Moestl &num_selections, &select_generation, generation, 3805fc5ae5cSThomas Moestl cur.dinfo->devices, num_devices, matches, 3815fc5ae5cSThomas Moestl num_matches, specified_devices, 3825fc5ae5cSThomas Moestl num_devices_specified, select_mode, maxshowdevs, 3835fc5ae5cSThomas Moestl hflag) == -1) 3848d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 3858d2fbde5SJustin T. Gibbs 3868d2fbde5SJustin T. Gibbs /* 3878d2fbde5SJustin T. Gibbs * Look for the traditional wait time and count arguments. 3888d2fbde5SJustin T. Gibbs */ 3898d2fbde5SJustin T. Gibbs if (*argv) { 3908b10473dSSean Bruno f = atof(*argv); 3918b10473dSSean Bruno waittime = f * 1000; 3928d2fbde5SJustin T. Gibbs 3938d2fbde5SJustin T. Gibbs /* Let the user know he goofed, but keep going anyway */ 3948d2fbde5SJustin T. Gibbs if (wflag != 0) 3958d2fbde5SJustin T. Gibbs warnx("discarding previous wait interval, using" 3968b10473dSSean Bruno " %g instead", waittime / 1000.0); 3978d2fbde5SJustin T. Gibbs wflag++; 3988d2fbde5SJustin T. Gibbs 3998d2fbde5SJustin T. Gibbs if (*++argv) { 4008d2fbde5SJustin T. Gibbs count = atoi(*argv); 4018d2fbde5SJustin T. Gibbs if (cflag != 0) 4028d2fbde5SJustin T. Gibbs warnx("discarding previous count, using %d" 4038d2fbde5SJustin T. Gibbs " instead", count); 4048d2fbde5SJustin T. Gibbs cflag++; 4058d2fbde5SJustin T. Gibbs } else 4068d2fbde5SJustin T. Gibbs count = -1; 4078d2fbde5SJustin T. Gibbs } 4088d2fbde5SJustin T. Gibbs 4098d2fbde5SJustin T. Gibbs /* 4108d2fbde5SJustin T. Gibbs * If the user specified a count, but not an interval, we default 4118d2fbde5SJustin T. Gibbs * to an interval of 1 second. 4128d2fbde5SJustin T. Gibbs */ 4138d2fbde5SJustin T. Gibbs if ((wflag == 0) && (cflag > 0)) 4148b10473dSSean Bruno waittime = 1 * 1000; 4158d2fbde5SJustin T. Gibbs 4168d2fbde5SJustin T. Gibbs /* 4178d2fbde5SJustin T. Gibbs * If the user specified a wait time, but not a count, we want to 4188d2fbde5SJustin T. Gibbs * go on ad infinitum. This can be redundant if the user uses the 4198d2fbde5SJustin T. Gibbs * traditional method of specifying the wait, since in that case we 4208d2fbde5SJustin T. Gibbs * already set count = -1 above. Oh well. 4218d2fbde5SJustin T. Gibbs */ 4228d2fbde5SJustin T. Gibbs if ((wflag > 0) && (cflag == 0)) 4238d2fbde5SJustin T. Gibbs count = -1; 4248d2fbde5SJustin T. Gibbs 4259e5fbab2SJohn Baldwin bzero(cur.cp_time, sizeof(cur.cp_time)); 4265dc445bfSKenneth D. Merry cur.tk_nout = 0; 4275dc445bfSKenneth D. Merry cur.tk_nin = 0; 4285dc445bfSKenneth D. Merry 4295dc445bfSKenneth D. Merry /* 4307194d335SPoul-Henning Kamp * Set the snap time to the system boot time (ie: zero), so the 4317194d335SPoul-Henning Kamp * stats are calculated since system boot. 4325dc445bfSKenneth D. Merry */ 4337194d335SPoul-Henning Kamp cur.snap_time = 0; 4345dc445bfSKenneth D. Merry 435dea673e9SRodney W. Grimes /* 4368d2fbde5SJustin T. Gibbs * If the user stops the program (control-Z) and then resumes it, 4378d2fbde5SJustin T. Gibbs * print out the header again. 438dea673e9SRodney W. Grimes */ 439775b0074SGuido van Rooij (void)signal(SIGCONT, needhdr); 440dea673e9SRodney W. Grimes 44153fbcd3aSGiorgos Keramidas /* 44253fbcd3aSGiorgos Keramidas * If our standard output is a tty, then install a SIGWINCH handler 44353fbcd3aSGiorgos Keramidas * and set wresized so that our first iteration through the main 44453fbcd3aSGiorgos Keramidas * iostat loop will peek at the terminal's current rows to find out 44553fbcd3aSGiorgos Keramidas * how many lines can fit in a screenful of output. 44653fbcd3aSGiorgos Keramidas */ 44753fbcd3aSGiorgos Keramidas if (isatty(fileno(stdout)) != 0) { 44853fbcd3aSGiorgos Keramidas wresized = 1; 44953fbcd3aSGiorgos Keramidas (void)signal(SIGWINCH, needresize); 45053fbcd3aSGiorgos Keramidas } else { 45153fbcd3aSGiorgos Keramidas wresized = 0; 45253fbcd3aSGiorgos Keramidas wrows = IOSTAT_DEFAULT_ROWS; 45353fbcd3aSGiorgos Keramidas } 45453fbcd3aSGiorgos Keramidas 455da61e79cSAlan Somers /* 456da61e79cSAlan Somers * Register a SIGINT handler so that we can print out final statistics 457da61e79cSAlan Somers * when we get that signal 458da61e79cSAlan Somers */ 459da61e79cSAlan Somers (void)signal(SIGINT, needreturn); 460da61e79cSAlan Somers 461da61e79cSAlan Somers /* 462da61e79cSAlan Somers * Register a SIGALRM handler to implement sleeps if the user uses the 463da61e79cSAlan Somers * -c or -w options 464da61e79cSAlan Somers */ 465da61e79cSAlan Somers (void)signal(SIGALRM, alarm_clock); 466da61e79cSAlan Somers alarmspec.it_interval.tv_sec = waittime / 1000; 467da61e79cSAlan Somers alarmspec.it_interval.tv_usec = 1000 * (waittime % 1000); 468da61e79cSAlan Somers alarmspec.it_value.tv_sec = waittime / 1000; 469da61e79cSAlan Somers alarmspec.it_value.tv_usec = 1000 * (waittime % 1000); 470da61e79cSAlan Somers setitimer(ITIMER_REAL, &alarmspec, NULL); 471da61e79cSAlan Somers 4728d2fbde5SJustin T. Gibbs for (headercount = 1;;) { 4738d2fbde5SJustin T. Gibbs struct devinfo *tmp_dinfo; 4748d2fbde5SJustin T. Gibbs long tmp; 4755dc445bfSKenneth D. Merry long double etime; 476da61e79cSAlan Somers sigset_t sigmask, oldsigmask; 4775dc445bfSKenneth D. Merry 4785dc445bfSKenneth D. Merry if (Tflag > 0) { 4798cb3704aSSergey Kandaurov if ((readvar(kd, "kern.tty_nin", X_TTY_NIN, &cur.tk_nin, 4805dc445bfSKenneth D. Merry sizeof(cur.tk_nin)) != 0) 4818cb3704aSSergey Kandaurov || (readvar(kd, "kern.tty_nout", X_TTY_NOUT, 4825dc445bfSKenneth D. Merry &cur.tk_nout, sizeof(cur.tk_nout))!= 0)) { 4835dc445bfSKenneth D. Merry Tflag = 0; 4845dc445bfSKenneth D. Merry warnx("disabling TTY statistics"); 4855dc445bfSKenneth D. Merry } 4865dc445bfSKenneth D. Merry } 4875dc445bfSKenneth D. Merry 4885dc445bfSKenneth D. Merry if (Cflag > 0) { 48932c9e3bdSJohn Baldwin if (kd == NULL) { 49032c9e3bdSJohn Baldwin if (readvar(kd, "kern.cp_time", 0, 49132c9e3bdSJohn Baldwin &cur.cp_time, sizeof(cur.cp_time)) != 0) 4925dc445bfSKenneth D. Merry Cflag = 0; 49332c9e3bdSJohn Baldwin } else { 49432c9e3bdSJohn Baldwin if (kvm_getcptime(kd, cur.cp_time) < 0) { 49532c9e3bdSJohn Baldwin warnx("kvm_getcptime: %s", 49632c9e3bdSJohn Baldwin kvm_geterr(kd)); 49732c9e3bdSJohn Baldwin Cflag = 0; 4985dc445bfSKenneth D. Merry } 4995dc445bfSKenneth D. Merry } 50032c9e3bdSJohn Baldwin if (Cflag == 0) 50132c9e3bdSJohn Baldwin warnx("disabling CPU time statistics"); 50232c9e3bdSJohn Baldwin } 5038d2fbde5SJustin T. Gibbs 5048d2fbde5SJustin T. Gibbs if (!--headercount) { 505775b0074SGuido van Rooij phdr(); 50653fbcd3aSGiorgos Keramidas if (wresized != 0) 50753fbcd3aSGiorgos Keramidas doresize(); 50853fbcd3aSGiorgos Keramidas headercount = wrows; 509dea673e9SRodney W. Grimes } 5108d2fbde5SJustin T. Gibbs 5118d2fbde5SJustin T. Gibbs tmp_dinfo = last.dinfo; 5128d2fbde5SJustin T. Gibbs last.dinfo = cur.dinfo; 5138d2fbde5SJustin T. Gibbs cur.dinfo = tmp_dinfo; 5148d2fbde5SJustin T. Gibbs 5157194d335SPoul-Henning Kamp last.snap_time = cur.snap_time; 5168d2fbde5SJustin T. Gibbs 5178d2fbde5SJustin T. Gibbs /* 5188d2fbde5SJustin T. Gibbs * Here what we want to do is refresh our device stats. 5195fc5ae5cSThomas Moestl * devstat_getdevs() returns 1 when the device list has changed. 5208d2fbde5SJustin T. Gibbs * If the device list has changed, we want to go through 5218d2fbde5SJustin T. Gibbs * the selection process again, in case a device that we 5228d2fbde5SJustin T. Gibbs * were previously displaying has gone away. 5238d2fbde5SJustin T. Gibbs */ 5245fc5ae5cSThomas Moestl switch (devstat_getdevs(kd, &cur)) { 5258d2fbde5SJustin T. Gibbs case -1: 5268d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 5278d2fbde5SJustin T. Gibbs break; 5288d2fbde5SJustin T. Gibbs case 1: { 5298d2fbde5SJustin T. Gibbs int retval; 5308d2fbde5SJustin T. Gibbs 5318d2fbde5SJustin T. Gibbs num_devices = cur.dinfo->numdevs; 5328d2fbde5SJustin T. Gibbs generation = cur.dinfo->generation; 5335fc5ae5cSThomas Moestl retval = devstat_selectdevs(&dev_select, &num_selected, 5345fc5ae5cSThomas Moestl &num_selections, 5355fc5ae5cSThomas Moestl &select_generation, 5365fc5ae5cSThomas Moestl generation, 5375fc5ae5cSThomas Moestl cur.dinfo->devices, 5385fc5ae5cSThomas Moestl num_devices, matches, 5395fc5ae5cSThomas Moestl num_matches, 5408d2fbde5SJustin T. Gibbs specified_devices, 5418d2fbde5SJustin T. Gibbs num_devices_specified, 5425fc5ae5cSThomas Moestl select_mode, maxshowdevs, 5435fc5ae5cSThomas Moestl hflag); 5448d2fbde5SJustin T. Gibbs switch(retval) { 5458d2fbde5SJustin T. Gibbs case -1: 5468d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 5478d2fbde5SJustin T. Gibbs break; 5488d2fbde5SJustin T. Gibbs case 1: 549775b0074SGuido van Rooij phdr(); 55053fbcd3aSGiorgos Keramidas if (wresized != 0) 55153fbcd3aSGiorgos Keramidas doresize(); 55253fbcd3aSGiorgos Keramidas headercount = wrows; 5538d2fbde5SJustin T. Gibbs break; 5548d2fbde5SJustin T. Gibbs default: 5558d2fbde5SJustin T. Gibbs break; 556dea673e9SRodney W. Grimes } 5578d2fbde5SJustin T. Gibbs break; 5588d2fbde5SJustin T. Gibbs } 5598d2fbde5SJustin T. Gibbs default: 5608d2fbde5SJustin T. Gibbs break; 5618d2fbde5SJustin T. Gibbs } 5628d2fbde5SJustin T. Gibbs 5638d2fbde5SJustin T. Gibbs /* 5648d2fbde5SJustin T. Gibbs * We only want to re-select devices if we're in 'top' 5658d2fbde5SJustin T. Gibbs * mode. This is the only mode where the devices selected 5668d2fbde5SJustin T. Gibbs * could actually change. 5678d2fbde5SJustin T. Gibbs */ 5688d2fbde5SJustin T. Gibbs if (hflag > 0) { 5698d2fbde5SJustin T. Gibbs int retval; 5705fc5ae5cSThomas Moestl retval = devstat_selectdevs(&dev_select, &num_selected, 5715fc5ae5cSThomas Moestl &num_selections, 5725fc5ae5cSThomas Moestl &select_generation, 5735fc5ae5cSThomas Moestl generation, 5745fc5ae5cSThomas Moestl cur.dinfo->devices, 5755fc5ae5cSThomas Moestl num_devices, matches, 5765fc5ae5cSThomas Moestl num_matches, 5778d2fbde5SJustin T. Gibbs specified_devices, 5788d2fbde5SJustin T. Gibbs num_devices_specified, 5795fc5ae5cSThomas Moestl select_mode, maxshowdevs, 5805fc5ae5cSThomas Moestl hflag); 5818d2fbde5SJustin T. Gibbs switch(retval) { 5828d2fbde5SJustin T. Gibbs case -1: 5838d2fbde5SJustin T. Gibbs errx(1,"%s", devstat_errbuf); 5848d2fbde5SJustin T. Gibbs break; 5858d2fbde5SJustin T. Gibbs case 1: 586775b0074SGuido van Rooij phdr(); 58753fbcd3aSGiorgos Keramidas if (wresized != 0) 58853fbcd3aSGiorgos Keramidas doresize(); 58953fbcd3aSGiorgos Keramidas headercount = wrows; 5908d2fbde5SJustin T. Gibbs break; 5918d2fbde5SJustin T. Gibbs default: 5928d2fbde5SJustin T. Gibbs break; 5938d2fbde5SJustin T. Gibbs } 5948d2fbde5SJustin T. Gibbs } 5958d2fbde5SJustin T. Gibbs 5965dc445bfSKenneth D. Merry if (Tflag > 0) { 597dea673e9SRodney W. Grimes tmp = cur.tk_nin; 598dea673e9SRodney W. Grimes cur.tk_nin -= last.tk_nin; 599dea673e9SRodney W. Grimes last.tk_nin = tmp; 600dea673e9SRodney W. Grimes tmp = cur.tk_nout; 601dea673e9SRodney W. Grimes cur.tk_nout -= last.tk_nout; 602dea673e9SRodney W. Grimes last.tk_nout = tmp; 603dea673e9SRodney W. Grimes } 6045dc445bfSKenneth D. Merry 6057194d335SPoul-Henning Kamp etime = cur.snap_time - last.snap_time; 6065dc445bfSKenneth D. Merry 607dea673e9SRodney W. Grimes if (etime == 0.0) 608dea673e9SRodney W. Grimes etime = 1.0; 6095dc445bfSKenneth D. Merry 6105dc445bfSKenneth D. Merry for (i = 0; i < CPUSTATES; i++) { 6115dc445bfSKenneth D. Merry tmp = cur.cp_time[i]; 6125dc445bfSKenneth D. Merry cur.cp_time[i] -= last.cp_time[i]; 6135dc445bfSKenneth D. Merry last.cp_time[i] = tmp; 6145dc445bfSKenneth D. Merry } 6155dc445bfSKenneth D. Merry 6166c359123SMaxim Konovalov if (xflag == 0 && Tflag > 0) 6175dc445bfSKenneth D. Merry printf("%4.0Lf %5.0Lf", cur.tk_nin / etime, 6188d2fbde5SJustin T. Gibbs cur.tk_nout / etime); 6195dc445bfSKenneth D. Merry 6205fc5ae5cSThomas Moestl devstats(hflag, etime, havelast); 6215dc445bfSKenneth D. Merry 6226c359123SMaxim Konovalov if (xflag == 0) { 6235dc445bfSKenneth D. Merry if (Cflag > 0) 624dea673e9SRodney W. Grimes cpustats(); 6255dc445bfSKenneth D. Merry 6268d2fbde5SJustin T. Gibbs printf("\n"); 6276c359123SMaxim Konovalov } 6288d2fbde5SJustin T. Gibbs fflush(stdout); 629dea673e9SRodney W. Grimes 630da61e79cSAlan Somers if ((count >= 0 && --count <= 0) || return_requested) 631dea673e9SRodney W. Grimes break; 6328d2fbde5SJustin T. Gibbs 633da61e79cSAlan Somers /* 634da61e79cSAlan Somers * Use sigsuspend to safely sleep until either signal is 635da61e79cSAlan Somers * received 636da61e79cSAlan Somers */ 637da61e79cSAlan Somers alarm_rang = 0; 638da61e79cSAlan Somers sigemptyset(&sigmask); 639da61e79cSAlan Somers sigaddset(&sigmask, SIGINT); 640da61e79cSAlan Somers sigaddset(&sigmask, SIGALRM); 641da61e79cSAlan Somers sigprocmask(SIG_BLOCK, &sigmask, &oldsigmask); 642da61e79cSAlan Somers while (! (alarm_rang || return_requested) ) { 643da61e79cSAlan Somers sigsuspend(&oldsigmask); 644da61e79cSAlan Somers } 645da61e79cSAlan Somers sigprocmask(SIG_UNBLOCK, &sigmask, NULL); 646da61e79cSAlan Somers 6475fc5ae5cSThomas Moestl havelast = 1; 648dea673e9SRodney W. Grimes } 6498d2fbde5SJustin T. Gibbs 650dea673e9SRodney W. Grimes exit(0); 651dea673e9SRodney W. Grimes } 652dea673e9SRodney W. Grimes 653775b0074SGuido van Rooij /* 654775b0074SGuido van Rooij * Force a header to be prepended to the next output. 655775b0074SGuido van Rooij */ 656775b0074SGuido van Rooij void 657a59c2129SAlan Somers needhdr(int signo __unused) 658dea673e9SRodney W. Grimes { 659e65cded7SKris Kennaway 660775b0074SGuido van Rooij headercount = 1; 661e65cded7SKris Kennaway } 662e65cded7SKris Kennaway 66353fbcd3aSGiorgos Keramidas /* 66453fbcd3aSGiorgos Keramidas * When the terminal is resized, force an update of the maximum number of rows 66553fbcd3aSGiorgos Keramidas * printed between each header repetition. Then force a new header to be 66653fbcd3aSGiorgos Keramidas * prepended to the next output. 66753fbcd3aSGiorgos Keramidas */ 66853fbcd3aSGiorgos Keramidas void 669a59c2129SAlan Somers needresize(int signo __unused) 67053fbcd3aSGiorgos Keramidas { 67153fbcd3aSGiorgos Keramidas 67253fbcd3aSGiorgos Keramidas wresized = 1; 67353fbcd3aSGiorgos Keramidas headercount = 1; 67453fbcd3aSGiorgos Keramidas } 67553fbcd3aSGiorgos Keramidas 67653fbcd3aSGiorgos Keramidas /* 677da61e79cSAlan Somers * Record the alarm so the main loop can break its sleep 678da61e79cSAlan Somers */ 679da61e79cSAlan Somers void 680a59c2129SAlan Somers alarm_clock(int signo __unused) 681da61e79cSAlan Somers { 682da61e79cSAlan Somers alarm_rang = 1; 683da61e79cSAlan Somers } 684da61e79cSAlan Somers 685da61e79cSAlan Somers /* 686da61e79cSAlan Somers * Request that the main loop exit soon 687da61e79cSAlan Somers */ 688da61e79cSAlan Somers void 689a59c2129SAlan Somers needreturn(int signo __unused) 690da61e79cSAlan Somers { 691da61e79cSAlan Somers return_requested = 1; 692da61e79cSAlan Somers } 693da61e79cSAlan Somers 694da61e79cSAlan Somers /* 69553fbcd3aSGiorgos Keramidas * Update the global `wrows' count of terminal rows. 69653fbcd3aSGiorgos Keramidas */ 69753fbcd3aSGiorgos Keramidas void 69853fbcd3aSGiorgos Keramidas doresize(void) 69953fbcd3aSGiorgos Keramidas { 70053fbcd3aSGiorgos Keramidas int status; 70153fbcd3aSGiorgos Keramidas struct winsize w; 70253fbcd3aSGiorgos Keramidas 70353fbcd3aSGiorgos Keramidas for (;;) { 70453fbcd3aSGiorgos Keramidas status = ioctl(fileno(stdout), TIOCGWINSZ, &w); 70553fbcd3aSGiorgos Keramidas if (status == -1 && errno == EINTR) 70653fbcd3aSGiorgos Keramidas continue; 70753fbcd3aSGiorgos Keramidas else if (status == -1) 70853fbcd3aSGiorgos Keramidas err(1, "ioctl"); 70953fbcd3aSGiorgos Keramidas if (w.ws_row > 3) 71053fbcd3aSGiorgos Keramidas wrows = w.ws_row - 3; 71153fbcd3aSGiorgos Keramidas else 71253fbcd3aSGiorgos Keramidas wrows = IOSTAT_DEFAULT_ROWS; 71353fbcd3aSGiorgos Keramidas break; 71453fbcd3aSGiorgos Keramidas } 71553fbcd3aSGiorgos Keramidas 71653fbcd3aSGiorgos Keramidas /* 71753fbcd3aSGiorgos Keramidas * Inhibit doresize() calls until we are rescheduled by SIGWINCH. 71853fbcd3aSGiorgos Keramidas */ 71953fbcd3aSGiorgos Keramidas wresized = 0; 72053fbcd3aSGiorgos Keramidas } 72153fbcd3aSGiorgos Keramidas 722e65cded7SKris Kennaway static void 723775b0074SGuido van Rooij phdr(void) 724e65cded7SKris Kennaway { 72547e065b4SMaxim Konovalov int i, printed; 726ab8ac08eSAdrian Chadd char devbuf[256]; 727dea673e9SRodney W. Grimes 7286c359123SMaxim Konovalov /* 7296c359123SMaxim Konovalov * If xflag is set, we need a per-loop header, not a page header, so 7306c359123SMaxim Konovalov * just return. We'll print the header in devstats(). 7316c359123SMaxim Konovalov */ 7326c359123SMaxim Konovalov if (xflag > 0) 7336c359123SMaxim Konovalov return; 7346c359123SMaxim Konovalov 7355dc445bfSKenneth D. Merry if (Tflag > 0) 736dea673e9SRodney W. Grimes (void)printf(" tty"); 7378d2fbde5SJustin T. Gibbs for (i = 0, printed=0;(i < num_devices) && (printed < maxshowdevs);i++){ 7388d2fbde5SJustin T. Gibbs int di; 7398d2fbde5SJustin T. Gibbs if ((dev_select[i].selected != 0) 7408d2fbde5SJustin T. Gibbs && (dev_select[i].selected <= maxshowdevs)) { 7418d2fbde5SJustin T. Gibbs di = dev_select[i].position; 742ab8ac08eSAdrian Chadd snprintf(devbuf, sizeof(devbuf), "%s%d", 743ab8ac08eSAdrian Chadd cur.dinfo->devices[di].device_name, 744ab8ac08eSAdrian Chadd cur.dinfo->devices[di].unit_number); 7458d2fbde5SJustin T. Gibbs if (oflag > 0) 746ab8ac08eSAdrian Chadd (void)printf("%13.6s ", devbuf); 7478d2fbde5SJustin T. Gibbs else 748ab8ac08eSAdrian Chadd printf("%16.6s ", devbuf); 7498d2fbde5SJustin T. Gibbs printed++; 7508d2fbde5SJustin T. Gibbs } 7518d2fbde5SJustin T. Gibbs } 7525dc445bfSKenneth D. Merry if (Cflag > 0) 7538d2fbde5SJustin T. Gibbs (void)printf(" cpu\n"); 7548d2fbde5SJustin T. Gibbs else 7558d2fbde5SJustin T. Gibbs (void)printf("\n"); 7568d2fbde5SJustin T. Gibbs 7575dc445bfSKenneth D. Merry if (Tflag > 0) 7588d2fbde5SJustin T. Gibbs (void)printf(" tin tout"); 7598d2fbde5SJustin T. Gibbs 7608d2fbde5SJustin T. Gibbs for (i=0, printed = 0;(i < num_devices) && (printed < maxshowdevs);i++){ 7618d2fbde5SJustin T. Gibbs if ((dev_select[i].selected != 0) 7628d2fbde5SJustin T. Gibbs && (dev_select[i].selected <= maxshowdevs)) { 7632fa4aaa0SMatthew Dillon if (oflag > 0) { 7648d2fbde5SJustin T. Gibbs if (Iflag == 0) 765dea673e9SRodney W. Grimes (void)printf(" sps tps msps "); 7668d2fbde5SJustin T. Gibbs else 7678d2fbde5SJustin T. Gibbs (void)printf(" blk xfr msps "); 7682fa4aaa0SMatthew Dillon } else { 7698d2fbde5SJustin T. Gibbs if (Iflag == 0) 7708d2fbde5SJustin T. Gibbs printf(" KB/t tps MB/s "); 7718d2fbde5SJustin T. Gibbs else 7728d2fbde5SJustin T. Gibbs printf(" KB/t xfrs MB "); 7732fa4aaa0SMatthew Dillon } 7748d2fbde5SJustin T. Gibbs printed++; 7758d2fbde5SJustin T. Gibbs } 7768d2fbde5SJustin T. Gibbs } 7775dc445bfSKenneth D. Merry if (Cflag > 0) 778936d754aSAndrey A. Chernov (void)printf(" us ni sy in id\n"); 7798d2fbde5SJustin T. Gibbs else 7808d2fbde5SJustin T. Gibbs printf("\n"); 781775b0074SGuido van Rooij 782dea673e9SRodney W. Grimes } 783dea673e9SRodney W. Grimes 7848d2fbde5SJustin T. Gibbs static void 7855dc445bfSKenneth D. Merry devstats(int perf_select, long double etime, int havelast) 786dea673e9SRodney W. Grimes { 78747e065b4SMaxim Konovalov int dn; 788abf607edSWill Andrews long double transfers_per_second, transfers_per_second_read; 789abf607edSWill Andrews long double transfers_per_second_write; 790abf607edSWill Andrews long double kb_per_transfer, mb_per_second, mb_per_second_read; 791abf607edSWill Andrews long double mb_per_second_write; 7928d2fbde5SJustin T. Gibbs u_int64_t total_bytes, total_transfers, total_blocks; 79330ff1266SMaxim Konovalov u_int64_t total_bytes_read, total_transfers_read; 79430ff1266SMaxim Konovalov u_int64_t total_bytes_write, total_transfers_write; 7953cfa2135SMikolaj Golub long double busy_pct, busy_time; 7966c359123SMaxim Konovalov u_int64_t queue_len; 797abf607edSWill Andrews long double total_mb, blocks_per_second, total_duration; 798abf607edSWill Andrews long double ms_per_other, ms_per_read, ms_per_write, ms_per_transaction; 7996c359123SMaxim Konovalov int firstline = 1; 80001368cfaSAlan Somers char *devicename; 8016c359123SMaxim Konovalov 8026c359123SMaxim Konovalov if (xflag > 0) { 8030484aa6dSAllan Jude if (Cflag > 0) { 8040484aa6dSAllan Jude printf(" cpu\n"); 8050484aa6dSAllan Jude printf(" us ni sy in id\n"); 8060484aa6dSAllan Jude cpustats(); 8070484aa6dSAllan Jude printf("\n"); 8080484aa6dSAllan Jude } 8096c359123SMaxim Konovalov printf(" extended device statistics "); 8106c359123SMaxim Konovalov if (Tflag > 0) 8116c359123SMaxim Konovalov printf(" tty "); 8126c359123SMaxim Konovalov printf("\n"); 8133cfa2135SMikolaj Golub if (Iflag == 0) { 814abf607edSWill Andrews printf("device r/s w/s kr/s kw/s " 815abf607edSWill Andrews " ms/r ms/w ms/o ms/t qlen %%b "); 8163cfa2135SMikolaj Golub } else { 8173cfa2135SMikolaj Golub printf("device r/i w/i kr/i" 8183cfa2135SMikolaj Golub " kw/i qlen tsvc_t/i sb/i "); 8193cfa2135SMikolaj Golub } 8206c359123SMaxim Konovalov if (Tflag > 0) 8216c359123SMaxim Konovalov printf("tin tout "); 8226c359123SMaxim Konovalov printf("\n"); 8236c359123SMaxim Konovalov } 824dea673e9SRodney W. Grimes 8258d2fbde5SJustin T. Gibbs for (dn = 0; dn < num_devices; dn++) { 8268d2fbde5SJustin T. Gibbs int di; 8278d2fbde5SJustin T. Gibbs 8288d2fbde5SJustin T. Gibbs if (((perf_select == 0) && (dev_select[dn].selected == 0)) 8298d2fbde5SJustin T. Gibbs || (dev_select[dn].selected > maxshowdevs)) 830dea673e9SRodney W. Grimes continue; 831dea673e9SRodney W. Grimes 8328d2fbde5SJustin T. Gibbs di = dev_select[dn].position; 833dea673e9SRodney W. Grimes 8345fc5ae5cSThomas Moestl if (devstat_compute_statistics(&cur.dinfo->devices[di], 8355dc445bfSKenneth D. Merry havelast ? &last.dinfo->devices[di] : NULL, etime, 8365fc5ae5cSThomas Moestl DSM_TOTAL_BYTES, &total_bytes, 83730ff1266SMaxim Konovalov DSM_TOTAL_BYTES_READ, &total_bytes_read, 83830ff1266SMaxim Konovalov DSM_TOTAL_BYTES_WRITE, &total_bytes_write, 8395fc5ae5cSThomas Moestl DSM_TOTAL_TRANSFERS, &total_transfers, 84030ff1266SMaxim Konovalov DSM_TOTAL_TRANSFERS_READ, &total_transfers_read, 84130ff1266SMaxim Konovalov DSM_TOTAL_TRANSFERS_WRITE, &total_transfers_write, 8425fc5ae5cSThomas Moestl DSM_TOTAL_BLOCKS, &total_blocks, 8435fc5ae5cSThomas Moestl DSM_KB_PER_TRANSFER, &kb_per_transfer, 8445fc5ae5cSThomas Moestl DSM_TRANSFERS_PER_SECOND, &transfers_per_second, 8456c359123SMaxim Konovalov DSM_TRANSFERS_PER_SECOND_READ, &transfers_per_second_read, 8466c359123SMaxim Konovalov DSM_TRANSFERS_PER_SECOND_WRITE, &transfers_per_second_write, 8475fc5ae5cSThomas Moestl DSM_MB_PER_SECOND, &mb_per_second, 8486c359123SMaxim Konovalov DSM_MB_PER_SECOND_READ, &mb_per_second_read, 8496c359123SMaxim Konovalov DSM_MB_PER_SECOND_WRITE, &mb_per_second_write, 8505fc5ae5cSThomas Moestl DSM_BLOCKS_PER_SECOND, &blocks_per_second, 8515fc5ae5cSThomas Moestl DSM_MS_PER_TRANSACTION, &ms_per_transaction, 852abf607edSWill Andrews DSM_MS_PER_TRANSACTION_READ, &ms_per_read, 853abf607edSWill Andrews DSM_MS_PER_TRANSACTION_WRITE, &ms_per_write, 854abf607edSWill Andrews DSM_MS_PER_TRANSACTION_OTHER, &ms_per_other, 8556c359123SMaxim Konovalov DSM_BUSY_PCT, &busy_pct, 8566c359123SMaxim Konovalov DSM_QUEUE_LENGTH, &queue_len, 8573cfa2135SMikolaj Golub DSM_TOTAL_DURATION, &total_duration, 8583cfa2135SMikolaj Golub DSM_TOTAL_BUSY_TIME, &busy_time, 8595fc5ae5cSThomas Moestl DSM_NONE) != 0) 8608d2fbde5SJustin T. Gibbs errx(1, "%s", devstat_errbuf); 8618d2fbde5SJustin T. Gibbs 8628d2fbde5SJustin T. Gibbs if (perf_select != 0) { 8638d2fbde5SJustin T. Gibbs dev_select[dn].bytes = total_bytes; 8648d2fbde5SJustin T. Gibbs if ((dev_select[dn].selected == 0) 8658d2fbde5SJustin T. Gibbs || (dev_select[dn].selected > maxshowdevs)) 8668d2fbde5SJustin T. Gibbs continue; 8678d2fbde5SJustin T. Gibbs } 8688d2fbde5SJustin T. Gibbs 8696c359123SMaxim Konovalov if (Kflag > 0 || xflag > 0) { 8702fa4aaa0SMatthew Dillon int block_size = cur.dinfo->devices[di].block_size; 871e4d7fe02SKenneth D. Merry total_blocks = total_blocks * (block_size ? 872e4d7fe02SKenneth D. Merry block_size : 512) / 1024; 8732fa4aaa0SMatthew Dillon } 8742fa4aaa0SMatthew Dillon 8756c359123SMaxim Konovalov if (xflag > 0) { 87601368cfaSAlan Somers if (asprintf(&devicename, "%s%d", 8776c359123SMaxim Konovalov cur.dinfo->devices[di].device_name, 878e08b1089SMaxim Konovalov cur.dinfo->devices[di].unit_number) == -1) 879e08b1089SMaxim Konovalov err(1, "asprintf"); 8806c359123SMaxim Konovalov /* 8816c359123SMaxim Konovalov * If zflag is set, skip any devices with zero I/O. 8826c359123SMaxim Konovalov */ 8836c359123SMaxim Konovalov if (zflag == 0 || transfers_per_second_read > 0.05 || 8846c359123SMaxim Konovalov transfers_per_second_write > 0.05 || 8856c359123SMaxim Konovalov mb_per_second_read > ((long double).0005)/1024 || 8866c359123SMaxim Konovalov mb_per_second_write > ((long double).0005)/1024 || 8876c359123SMaxim Konovalov busy_pct > 0.5) { 88830ff1266SMaxim Konovalov if (Iflag == 0) 88961631b24SAlan Somers printf("%-8.8s %7.0Lf %7.0Lf %8.1Lf " 89061631b24SAlan Somers "%8.1Lf %5.0Lf %5.0Lf %5.0Lf %5.0Lf" 891abf607edSWill Andrews " %4" PRIu64 " %3.0Lf ", 89201368cfaSAlan Somers devicename, 89361631b24SAlan Somers transfers_per_second_read, 89461631b24SAlan Somers transfers_per_second_write, 8956c359123SMaxim Konovalov mb_per_second_read * 1024, 896fbe4fc2eSMaxim Konovalov mb_per_second_write * 1024, 89761631b24SAlan Somers ms_per_read, ms_per_write, 89861631b24SAlan Somers ms_per_other, 89961631b24SAlan Somers ms_per_transaction, 900abf607edSWill Andrews queue_len, busy_pct); 90130ff1266SMaxim Konovalov else 9023cfa2135SMikolaj Golub printf("%-8.8s %11.1Lf %11.1Lf " 9033cfa2135SMikolaj Golub "%12.1Lf %12.1Lf %4" PRIu64 9043cfa2135SMikolaj Golub " %10.1Lf %9.1Lf ", 90501368cfaSAlan Somers devicename, 906fbe4fc2eSMaxim Konovalov (long double)total_transfers_read, 90730ff1266SMaxim Konovalov (long double)total_transfers_write, 908fbe4fc2eSMaxim Konovalov (long double) 909fbe4fc2eSMaxim Konovalov total_bytes_read / 1024, 910fbe4fc2eSMaxim Konovalov (long double) 911fbe4fc2eSMaxim Konovalov total_bytes_write / 1024, 912fbe4fc2eSMaxim Konovalov queue_len, 9133cfa2135SMikolaj Golub total_duration, busy_time); 9146c359123SMaxim Konovalov if (firstline) { 9156c359123SMaxim Konovalov /* 9166c359123SMaxim Konovalov * If this is the first device 9176c359123SMaxim Konovalov * we're printing, also print 9186c359123SMaxim Konovalov * CPU or TTY stats if requested. 9196c359123SMaxim Konovalov */ 9206c359123SMaxim Konovalov firstline = 0; 9216c359123SMaxim Konovalov if (Tflag > 0) 9226c359123SMaxim Konovalov printf("%4.0Lf%5.0Lf", 9236c359123SMaxim Konovalov cur.tk_nin / etime, 9246c359123SMaxim Konovalov cur.tk_nout / etime); 9256c359123SMaxim Konovalov } 9266c359123SMaxim Konovalov printf("\n"); 9276c359123SMaxim Konovalov } 92801368cfaSAlan Somers free(devicename); 9296c359123SMaxim Konovalov } else if (oflag > 0) { 930125de826SEdward Tomasz Napierala int msdig = (ms_per_transaction < 99.94) ? 1 : 0; 9318d2fbde5SJustin T. Gibbs 9328d2fbde5SJustin T. Gibbs if (Iflag == 0) 9332fa4aaa0SMatthew Dillon printf("%4.0Lf%4.0Lf%5.*Lf ", 9348d2fbde5SJustin T. Gibbs blocks_per_second, 9358d2fbde5SJustin T. Gibbs transfers_per_second, 9362fa4aaa0SMatthew Dillon msdig, 9378d2fbde5SJustin T. Gibbs ms_per_transaction); 938dea673e9SRodney W. Grimes else 939cfed15e6SDimitry Andric printf("%4.1" PRIu64 "%4.1" PRIu64 "%5.*Lf ", 9408d2fbde5SJustin T. Gibbs total_blocks, 9418d2fbde5SJustin T. Gibbs total_transfers, 9422fa4aaa0SMatthew Dillon msdig, 9438d2fbde5SJustin T. Gibbs ms_per_transaction); 9448d2fbde5SJustin T. Gibbs } else { 9458d2fbde5SJustin T. Gibbs if (Iflag == 0) 94683743daeSEdward Tomasz Napierala printf(" %4.*Lf %4.0Lf %5.*Lf ", 94783743daeSEdward Tomasz Napierala kb_per_transfer >= 100 ? 0 : 1, 9488d2fbde5SJustin T. Gibbs kb_per_transfer, 9498d2fbde5SJustin T. Gibbs transfers_per_second, 95083743daeSEdward Tomasz Napierala mb_per_second >= 1000 ? 0 : 1, 9518d2fbde5SJustin T. Gibbs mb_per_second); 9528d2fbde5SJustin T. Gibbs else { 9538d2fbde5SJustin T. Gibbs total_mb = total_bytes; 9548d2fbde5SJustin T. Gibbs total_mb /= 1024 * 1024; 9558d2fbde5SJustin T. Gibbs 956100b0345SEdward Tomasz Napierala printf(" %4.1Lf %4.1" PRIu64 " %5.2Lf ", 9578d2fbde5SJustin T. Gibbs kb_per_transfer, 9588d2fbde5SJustin T. Gibbs total_transfers, 9598d2fbde5SJustin T. Gibbs total_mb); 9608d2fbde5SJustin T. Gibbs } 9618d2fbde5SJustin T. Gibbs } 962dea673e9SRodney W. Grimes } 9636c359123SMaxim Konovalov if (xflag > 0 && zflag > 0 && firstline == 1 && 9646c359123SMaxim Konovalov (Tflag > 0 || Cflag > 0)) { 9656c359123SMaxim Konovalov /* 9666c359123SMaxim Konovalov * If zflag is set and we did not print any device 9676c359123SMaxim Konovalov * lines I/O because they were all zero, 9686c359123SMaxim Konovalov * print TTY/CPU stats. 9696c359123SMaxim Konovalov */ 9706c359123SMaxim Konovalov printf("%52s",""); 9716c359123SMaxim Konovalov if (Tflag > 0) 9726c359123SMaxim Konovalov printf("%4.0Lf %5.0Lf", cur.tk_nin / etime, 9736c359123SMaxim Konovalov cur.tk_nout / etime); 9746c359123SMaxim Konovalov if (Cflag > 0) 9756c359123SMaxim Konovalov cpustats(); 9766c359123SMaxim Konovalov printf("\n"); 9776c359123SMaxim Konovalov } 978dea673e9SRodney W. Grimes } 979dea673e9SRodney W. Grimes 9808d2fbde5SJustin T. Gibbs static void 9818d2fbde5SJustin T. Gibbs cpustats(void) 982dea673e9SRodney W. Grimes { 98347e065b4SMaxim Konovalov int state; 98401368cfaSAlan Somers double cptime; 985dea673e9SRodney W. Grimes 98601368cfaSAlan Somers cptime = 0.0; 9878d2fbde5SJustin T. Gibbs 988dea673e9SRodney W. Grimes for (state = 0; state < CPUSTATES; ++state) 98901368cfaSAlan Somers cptime += cur.cp_time[state]; 990dea673e9SRodney W. Grimes for (state = 0; state < CPUSTATES; ++state) 9918fa6b8a8SBruce Evans printf(" %2.0f", 99201368cfaSAlan Somers rint(100. * cur.cp_time[state] / (cptime ? cptime : 1))); 993dea673e9SRodney W. Grimes } 9945fc5ae5cSThomas Moestl 9955dc445bfSKenneth D. Merry static int 9965dc445bfSKenneth D. Merry readvar(kvm_t *kd, const char *name, int nlid, void *ptr, size_t len) 9975fc5ae5cSThomas Moestl { 9985dc445bfSKenneth D. Merry if (kd != NULL) { 9995dc445bfSKenneth D. Merry ssize_t nbytes; 10005dc445bfSKenneth D. Merry 10018d56a4caSJohn Baldwin nbytes = kvm_read(kd, namelist[nlid].n_value, ptr, len); 10025dc445bfSKenneth D. Merry 10038d56a4caSJohn Baldwin if (nbytes < 0) { 10048d56a4caSJohn Baldwin warnx("kvm_read(%s): %s", namelist[nlid].n_name, 10058d56a4caSJohn Baldwin kvm_geterr(kd)); 10065dc445bfSKenneth D. Merry return (1); 1007a59c2129SAlan Somers } else if ((size_t)nbytes != len) { 10088d56a4caSJohn Baldwin warnx("kvm_read(%s): expected %zu bytes, got %zd bytes", 10098d56a4caSJohn Baldwin namelist[nlid].n_name, len, nbytes); 10105dc445bfSKenneth D. Merry return (1); 10115dc445bfSKenneth D. Merry } 10125dc445bfSKenneth D. Merry } else { 10135fc5ae5cSThomas Moestl size_t nlen = len; 10145fc5ae5cSThomas Moestl 10155dc445bfSKenneth D. Merry if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) { 10165dc445bfSKenneth D. Merry warn("sysctl(%s...) failed", name); 10175dc445bfSKenneth D. Merry return (1); 10185dc445bfSKenneth D. Merry } 10195dc445bfSKenneth D. Merry if (nlen != len) { 10205dc445bfSKenneth D. Merry warnx("sysctl(%s...): expected %lu, got %lu", name, 10215fc5ae5cSThomas Moestl (unsigned long)len, (unsigned long)nlen); 10225dc445bfSKenneth D. Merry return (1); 10235dc445bfSKenneth D. Merry } 10245dc445bfSKenneth D. Merry } 10255dc445bfSKenneth D. Merry return (0); 10265fc5ae5cSThomas Moestl } 1027