1 /* $OpenBSD: iostat.c,v 1.11 2001/11/17 19:49:38 deraadt Exp $ */ 2 /* $NetBSD: iostat.c,v 1.10 1996/10/25 18:21:58 scottr Exp $ */ 3 4 /* 5 * Copyright (c) 1996 John M. Vinopal 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed for the NetBSD Project 19 * by John M. Vinopal. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 /*- 37 * Copyright (c) 1986, 1991, 1993 38 * The Regents of the University of California. All rights reserved. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. All advertising materials mentioning features or use of this software 49 * must display the following acknowledgement: 50 * This product includes software developed by the University of 51 * California, Berkeley and its contributors. 52 * 4. Neither the name of the University nor the names of its contributors 53 * may be used to endorse or promote products derived from this software 54 * without specific prior written permission. 55 * 56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 66 * SUCH DAMAGE. 67 */ 68 69 #ifndef lint 70 static char copyright[] = 71 "@(#) Copyright (c) 1986, 1991, 1993\n\ 72 The Regents of the University of California. All rights reserved.\n"; 73 #endif /* not lint */ 74 75 #ifndef lint 76 #if 0 77 static char sccsid[] = "@(#)iostat.c 8.2 (Berkeley) 1/26/94"; 78 #else 79 static char *rcsid = "$NetBSD: iostat.c,v 1.10 1996/10/25 18:21:58 scottr Exp $" 80 ; 81 #endif 82 #endif /* not lint */ 83 84 #include <sys/dkstat.h> 85 #include <sys/time.h> 86 87 #include <err.h> 88 #include <ctype.h> 89 #include <signal.h> 90 #include <stdio.h> 91 #include <stdlib.h> 92 #include <string.h> 93 #include <unistd.h> 94 95 #include "dkstats.h" 96 97 /* Defined in dkstats.c */ 98 extern struct _disk cur; 99 extern int dk_ndrive; 100 101 /* Namelist and memory files. */ 102 char *nlistf, *memf; 103 104 int hz, reps, interval; 105 static int todo = 0; 106 107 volatile sig_atomic_t wantheader; 108 109 #define ISSET(x, a) ((x) & (a)) 110 #define SHOW_CPU 0x0001 111 #define SHOW_TTY 0x0002 112 #define SHOW_STATS_1 0x0004 113 #define SHOW_STATS_2 0x0008 114 #define SHOW_TOTALS 0x0080 115 116 static void cpustats __P((void)); 117 static void disk_stats __P((double)); 118 static void disk_stats2 __P((double)); 119 static void sigheader __P((int)); 120 static void header __P(()); 121 static void usage __P((void)); 122 static void display __P((void)); 123 static void selectdrives __P((int, char **)); 124 125 void dkswap __P((void)); 126 void dkreadstats __P((void)); 127 int dkinit __P((int)); 128 129 int 130 main(argc, argv) 131 int argc; 132 char *argv[]; 133 { 134 int ch, hdrcnt; 135 struct timeval tv; 136 137 while ((ch = getopt(argc, argv, "Cc:dDIM:N:Tw:")) != -1) 138 switch(ch) { 139 case 'c': 140 if ((reps = atoi(optarg)) <= 0) 141 errx(1, "repetition count <= 0."); 142 break; 143 case 'C': 144 todo |= SHOW_CPU; 145 break; 146 case 'd': 147 todo |= SHOW_STATS_1; 148 break; 149 case 'D': 150 todo |= SHOW_STATS_2; 151 break; 152 case 'I': 153 todo |= SHOW_TOTALS; 154 break; 155 case 'M': 156 memf = optarg; 157 break; 158 case 'N': 159 nlistf = optarg; 160 break; 161 case 'T': 162 todo |= SHOW_TTY; 163 break; 164 case 'w': 165 if ((interval = atoi(optarg)) <= 0) 166 errx(1, "interval <= 0."); 167 break; 168 case '?': 169 default: 170 usage(); 171 } 172 argc -= optind; 173 argv += optind; 174 175 if (!ISSET(todo, SHOW_CPU | SHOW_TTY | SHOW_STATS_1 | SHOW_STATS_2)) 176 todo |= SHOW_CPU | SHOW_TTY | SHOW_STATS_1; 177 178 dkinit(0); 179 dkreadstats(); 180 selectdrives(argc, argv); 181 182 tv.tv_sec = interval; 183 tv.tv_usec = 0; 184 185 /* print a new header on sigcont */ 186 (void)signal(SIGCONT, sigheader); 187 188 for (hdrcnt = 1;;) { 189 if (!--hdrcnt || wantheader) { 190 header(); 191 hdrcnt = 20; 192 wantheader = 0; 193 } 194 195 if (!ISSET(todo, SHOW_TOTALS)) 196 dkswap(); 197 display(); 198 199 if (reps >= 0 && --reps <= 0) 200 break; 201 select(0, NULL, NULL, NULL, &tv); 202 dkreadstats(); 203 } 204 exit(0); 205 } 206 207 static void 208 sigheader(signo) 209 int signo; 210 { 211 wantheader = 1; 212 } 213 214 static void 215 header(void) 216 { 217 register int i; 218 219 /* Main Headers. */ 220 if (ISSET(todo, SHOW_TTY)) 221 (void)printf(" tty"); 222 223 if (ISSET(todo, SHOW_STATS_1)) 224 for (i = 0; i < dk_ndrive; i++) 225 if (cur.dk_select[i]) 226 (void)printf(" %14.14s ", cur.dk_name[i]); 227 228 if (ISSET(todo, SHOW_STATS_2)) 229 for (i = 0; i < dk_ndrive; i++) 230 if (cur.dk_select[i]) 231 (void)printf(" %13.13s ", cur.dk_name[i]); 232 233 if (ISSET(todo, SHOW_CPU)) 234 (void)printf(" cpu"); 235 printf("\n"); 236 237 /* Sub-Headers. */ 238 if (ISSET(todo, SHOW_TTY)) 239 printf(" tin tout"); 240 241 if (ISSET(todo, SHOW_STATS_1)) 242 for (i = 0; i < dk_ndrive; i++) 243 if (cur.dk_select[i]) 244 if (ISSET(todo, SHOW_TOTALS)) 245 (void)printf(" KB/t xfr MB "); 246 else 247 (void)printf(" KB/t t/s MB/s "); 248 249 if (ISSET(todo, SHOW_STATS_2)) 250 for (i = 0; i < dk_ndrive; i++) 251 if (cur.dk_select[i]) 252 (void)printf(" KB xfr time "); 253 254 if (ISSET(todo, SHOW_CPU)) 255 (void)printf(" us ni sy in id"); 256 printf("\n"); 257 } 258 259 static void 260 disk_stats(etime) 261 double etime; 262 { 263 register int dn; 264 double atime, mbps; 265 266 for (dn = 0; dn < dk_ndrive; ++dn) { 267 if (!cur.dk_select[dn]) 268 continue; 269 270 /* average Kbytes per transfer. */ 271 if (cur.dk_xfer[dn]) 272 mbps = (cur.dk_bytes[dn] / (1024.0)) / cur.dk_xfer[dn]; 273 else 274 mbps = 0.0; 275 (void)printf(" %5.2f", mbps); 276 277 /* average transfers per second. */ 278 (void)printf(" %3.0f", cur.dk_xfer[dn] / etime); 279 280 /* time busy in disk activity */ 281 atime = (double)cur.dk_time[dn].tv_sec + 282 ((double)cur.dk_time[dn].tv_usec / (double)1000000); 283 284 /* Megabytes per second. */ 285 if (atime != 0.0) 286 mbps = cur.dk_bytes[dn] / (double)(1024 * 1024); 287 else 288 mbps = 0; 289 (void)printf(" %4.2f ", mbps / etime); 290 } 291 } 292 293 static void 294 disk_stats2(etime) 295 double etime; 296 { 297 register int dn; 298 double atime; 299 300 for (dn = 0; dn < dk_ndrive; ++dn) { 301 if (!cur.dk_select[dn]) 302 continue; 303 304 /* average kbytes per second. */ 305 (void)printf(" %4.0f", cur.dk_bytes[dn] / (1024.0) / etime); 306 307 /* average transfers per second. */ 308 (void)printf(" %3.0f", cur.dk_xfer[dn] / etime); 309 310 /* average time busy in disk activity. */ 311 atime = (double)cur.dk_time[dn].tv_sec + 312 ((double)cur.dk_time[dn].tv_usec / (double)1000000); 313 (void)printf(" %4.2f ", atime / etime); 314 } 315 } 316 317 static void 318 cpustats() 319 { 320 register int state; 321 double time; 322 323 time = 0; 324 for (state = 0; state < CPUSTATES; ++state) 325 time += cur.cp_time[state]; 326 if (!time) 327 time = 1.0; 328 /* States are generally never 100% and can use %3.0f. */ 329 for (state = 0; state < CPUSTATES; ++state) 330 printf("%3.0f", 100. * cur.cp_time[state] / time); 331 } 332 333 static void 334 usage() 335 { 336 (void)fprintf(stderr, 337 "usage: iostat [-CdDIT] [-c count] [-M core] [-N system] [-w wait] [drives]\n"); 338 exit(1); 339 } 340 341 static void 342 display() 343 { 344 int i; 345 double etime; 346 347 /* Sum up the elapsed ticks. */ 348 etime = 0.0; 349 for (i = 0; i < CPUSTATES; i++) { 350 etime += cur.cp_time[i]; 351 } 352 if (etime == 0.0) 353 etime = 1.0; 354 /* Convert to seconds. */ 355 etime /= (float)hz; 356 357 /* If we're showing totals only, then don't divide by the 358 * system time. 359 */ 360 if (ISSET(todo, SHOW_TOTALS)) 361 etime = 1.0; 362 363 if (ISSET(todo, SHOW_TTY)) 364 printf("%4.0f %4.0f", cur.tk_nin / etime, cur.tk_nout / etime); 365 366 if (ISSET(todo, SHOW_STATS_1)) 367 disk_stats(etime); 368 369 if (ISSET(todo, SHOW_STATS_2)) 370 disk_stats2(etime); 371 372 if (ISSET(todo, SHOW_CPU)) 373 cpustats(); 374 375 (void)printf("\n"); 376 (void)fflush(stdout); 377 } 378 379 static void 380 selectdrives(argc, argv) 381 int argc; 382 char *argv[]; 383 { 384 int i, ndrives; 385 386 /* 387 * Choose drives to be displayed. Priority goes to (in order) drives 388 * supplied as arguments and default drives. If everything isn't 389 * filled in and there are drives not taken care of, display the first 390 * few that fit. 391 * 392 * The backward compatibility #ifdefs permit the syntax: 393 * iostat [ drives ] [ interval [ count ] ] 394 */ 395 #define BACKWARD_COMPATIBILITY 396 for (ndrives = 0; *argv; ++argv) { 397 #ifdef BACKWARD_COMPATIBILITY 398 if (isdigit(**argv)) 399 break; 400 #endif 401 for (i = 0; i < dk_ndrive; i++) { 402 if (strcmp(cur.dk_name[i], *argv)) 403 continue; 404 cur.dk_select[i] = 1; 405 ++ndrives; 406 } 407 } 408 #ifdef BACKWARD_COMPATIBILITY 409 if (*argv) { 410 interval = atoi(*argv); 411 if (*++argv) 412 reps = atoi(*argv); 413 } 414 #endif 415 416 if (interval) { 417 if (!reps) 418 reps = -1; 419 } else 420 if (reps) 421 interval = 1; 422 423 /* Pick up to 4 drives if none specified. */ 424 if (ndrives == 0) 425 for (i = 0; i < dk_ndrive && ndrives < 4; i++) { 426 if (cur.dk_select[i]) 427 continue; 428 cur.dk_select[i] = 1; 429 ++ndrives; 430 } 431 } 432