iostat.c revision 112288
1112158Sdas/* 2112158Sdas * Copyright (c) 1998 Kenneth D. Merry. 3112158Sdas * All rights reserved. 4112158Sdas * 5112158Sdas * Redistribution and use in source and binary forms, with or without 6112158Sdas * modification, are permitted provided that the following conditions 7112158Sdas * are met: 8112158Sdas * 1. Redistributions of source code must retain the above copyright 9112158Sdas * notice, this list of conditions and the following disclaimer. 10112158Sdas * 2. Redistributions in binary form must reproduce the above copyright 11112158Sdas * notice, this list of conditions and the following disclaimer in the 12112158Sdas * documentation and/or other materials provided with the distribution. 13112158Sdas * 3. The name of the author may not be used to endorse or promote products 14112158Sdas * derived from this software without specific prior written permission. 15112158Sdas * 16112158Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17112158Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18112158Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19112158Sdas * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20112158Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21112158Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22112158Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23112158Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24112158Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25112158Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26112158Sdas * SUCH DAMAGE. 27112158Sdas */ 28112158Sdas/* 29165743Sdas * Copyright (c) 1980, 1992, 1993 30165743Sdas * The Regents of the University of California. All rights reserved. 31112158Sdas * 32112158Sdas * Redistribution and use in source and binary forms, with or without 33112158Sdas * modification, are permitted provided that the following conditions 34112158Sdas * are met: 35112158Sdas * 1. Redistributions of source code must retain the above copyright 36219557Sdas * notice, this list of conditions and the following disclaimer. 37112158Sdas * 2. Redistributions in binary form must reproduce the above copyright 38219557Sdas * notice, this list of conditions and the following disclaimer in the 39112158Sdas * documentation and/or other materials provided with the distribution. 40112158Sdas * 3. All advertising materials mentioning features or use of this software 41112158Sdas * must display the following acknowledgement: 42219557Sdas * This product includes software developed by the University of 43112158Sdas * California, Berkeley and its contributors. 44219557Sdas * 4. Neither the name of the University nor the names of its contributors 45219557Sdas * may be used to endorse or promote products derived from this software 46112158Sdas * without specific prior written permission. 47112158Sdas * 48112158Sdas * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49112158Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50112158Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51112158Sdas * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52112158Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53112158Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54112158Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55112158Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56112158Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57112158Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58165743Sdas * SUCH DAMAGE. 59112158Sdas */ 60112158Sdas 61112158Sdas#include <sys/cdefs.h> 62112158Sdas 63112158Sdas__FBSDID("$FreeBSD: head/usr.bin/systat/iostat.c 112288 2003-03-15 21:59:06Z phk $"); 64112158Sdas 65112158Sdas#ifdef lint 66112158Sdasstatic const char sccsid[] = "@(#)iostat.c 8.1 (Berkeley) 6/6/93"; 67112158Sdas#endif 68112158Sdas 69219557Sdas#include <sys/param.h> 70112158Sdas#include <sys/sysctl.h> 71112158Sdas#include <sys/resource.h> 72112158Sdas 73219557Sdas#include <devstat.h> 74112158Sdas#include <err.h> 75112158Sdas#include <nlist.h> 76112158Sdas#include <paths.h> 77219557Sdas#include <stdlib.h> 78112158Sdas#include <string.h> 79219557Sdas 80112158Sdas#include "systat.h" 81112158Sdas#include "extern.h" 82112158Sdas#include "devs.h" 83112158Sdas 84112158Sdasstruct statinfo cur, last; 85112158Sdas 86112158Sdasstatic int linesperregion; 87219557Sdasstatic double etime; 88219557Sdasstatic int numbers = 0; /* default display bar graphs */ 89112158Sdasstatic int kbpt = 0; /* default ms/seek shown */ 90112158Sdas 91112158Sdasstatic int barlabels(int); 92219557Sdasstatic void histogram(long double, int, double); 93219557Sdasstatic int numlabels(int); 94112158Sdasstatic int devstats(int, int, int); 95112158Sdasstatic void stat1(int, int); 96112158Sdas 97219557SdasWINDOW * 98112158Sdasopeniostat() 99112158Sdas{ 100112158Sdas return (subwin(stdscr, LINES-1-5, 0, 5, 0)); 101112158Sdas} 102112158Sdas 103112158Sdasvoid 104219557Sdascloseiostat(w) 105219557Sdas WINDOW *w; 106219557Sdas{ 107219557Sdas if (w == NULL) 108219557Sdas return; 109219557Sdas wclear(w); 110219557Sdas wrefresh(w); 111112158Sdas delwin(w); 112112158Sdas} 113112158Sdas 114112158Sdasint 115219557Sdasinitiostat() 116112158Sdas{ 117112158Sdas if ((num_devices = devstat_getnumdevs(NULL)) < 0) 118219557Sdas return(0); 119112158Sdas 120219557Sdas cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 121219557Sdas last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 122219557Sdas bzero(cur.dinfo, sizeof(struct devinfo)); 123219557Sdas bzero(last.dinfo, sizeof(struct devinfo)); 124219557Sdas 125219557Sdas /* 126219557Sdas * This value for maxshowdevs (100) is bogus. I'm not sure exactly 127112158Sdas * how to calculate it, though. 128112158Sdas */ 129112158Sdas if (dsinit(100, &cur, &last, NULL) != 1) 130112158Sdas return(0); 131219557Sdas 132112158Sdas return(1); 133112158Sdas} 134219557Sdas 135112158Sdasvoid 136112158Sdasfetchiostat() 137112158Sdas{ 138112158Sdas struct devinfo *tmp_dinfo; 139219557Sdas size_t len; 140219557Sdas 141112158Sdas len = sizeof(cur.cp_time); 142112158Sdas if (sysctlbyname("kern.cp_time", &cur.cp_time, &len, NULL, 0) 143219557Sdas || len != sizeof(cur.cp_time)) { 144219557Sdas perror("kern.cp_time"); 145112158Sdas exit (1); 146112158Sdas } 147219557Sdas tmp_dinfo = last.dinfo; 148219557Sdas last.dinfo = cur.dinfo; 149112158Sdas cur.dinfo = tmp_dinfo; 150112158Sdas 151112158Sdas /* 152112158Sdas * Here what we want to do is refresh our device stats. 153112158Sdas * getdevs() returns 1 when the device list has changed. 154219557Sdas * If the device list has changed, we want to go through 155219557Sdas * the selection process again, in case a device that we 156112158Sdas * were previously displaying has gone away. 157112158Sdas */ 158112158Sdas switch (devstat_getdevs(NULL, &cur)) { 159219557Sdas case -1: 160219557Sdas errx(1, "%s", devstat_errbuf); 161112158Sdas break; 162112158Sdas case 1: 163112158Sdas cmdiostat("refresh", NULL); 164 break; 165 default: 166 break; 167 } 168 num_devices = cur.dinfo->numdevs; 169 generation = cur.dinfo->generation; 170 171} 172 173#define INSET 10 174 175void 176labeliostat() 177{ 178 int row; 179 180 row = 0; 181 wmove(wnd, row, 0); wclrtobot(wnd); 182 mvwaddstr(wnd, row++, INSET, 183 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100"); 184 mvwaddstr(wnd, row++, 0, "cpu user|"); 185 mvwaddstr(wnd, row++, 0, " nice|"); 186 mvwaddstr(wnd, row++, 0, " system|"); 187 mvwaddstr(wnd, row++, 0, "interrupt|"); 188 mvwaddstr(wnd, row++, 0, " idle|"); 189 if (numbers) 190 row = numlabels(row + 1); 191 else 192 row = barlabels(row + 1); 193} 194 195static int 196numlabels(row) 197 int row; 198{ 199 int i, _col, regions, ndrives; 200 char tmpstr[10]; 201 202#define COLWIDTH 17 203#define DRIVESPERLINE ((wnd->_maxx - INSET) / COLWIDTH) 204 for (ndrives = 0, i = 0; i < num_devices; i++) 205 if (dev_select[i].selected) 206 ndrives++; 207 regions = howmany(ndrives, DRIVESPERLINE); 208 /* 209 * Deduct -regions for blank line after each scrolling region. 210 */ 211 linesperregion = (wnd->_maxy - row - regions) / regions; 212 /* 213 * Minimum region contains space for two 214 * label lines and one line of statistics. 215 */ 216 if (linesperregion < 3) 217 linesperregion = 3; 218 _col = INSET; 219 for (i = 0; i < num_devices; i++) 220 if (dev_select[i].selected) { 221 if (_col + COLWIDTH >= wnd->_maxx - INSET) { 222 _col = INSET, row += linesperregion + 1; 223 if (row > wnd->_maxy - (linesperregion + 1)) 224 break; 225 } 226 sprintf(tmpstr, "%s%d", dev_select[i].device_name, 227 dev_select[i].unit_number); 228 mvwaddstr(wnd, row, _col + 4, tmpstr); 229 mvwaddstr(wnd, row + 1, _col, " KB/t tps MB/s "); 230 _col += COLWIDTH; 231 } 232 if (_col) 233 row += linesperregion + 1; 234 return (row); 235} 236 237static int 238barlabels(row) 239 int row; 240{ 241 int i; 242 char tmpstr[10]; 243 244 mvwaddstr(wnd, row++, INSET, 245 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100"); 246 linesperregion = 2 + kbpt; 247 for (i = 0; i < num_devices; i++) 248 if (dev_select[i].selected) { 249 if (row > wnd->_maxy - linesperregion) 250 break; 251 sprintf(tmpstr, "%s%d", dev_select[i].device_name, 252 dev_select[i].unit_number); 253 mvwprintw(wnd, row++, 0, "%-5.5s MB/s|", 254 tmpstr); 255 mvwaddstr(wnd, row++, 0, " tps|"); 256 if (kbpt) 257 mvwaddstr(wnd, row++, 0, " KB/t|"); 258 } 259 return (row); 260} 261 262 263void 264showiostat() 265{ 266 long t; 267 int i, row, _col; 268 269#define X(fld) t = cur.fld[i]; cur.fld[i] -= last.fld[i]; last.fld[i] = t 270 etime = 0; 271 for(i = 0; i < CPUSTATES; i++) { 272 X(cp_time); 273 etime += cur.cp_time[i]; 274 } 275 if (etime == 0.0) 276 etime = 1.0; 277 etime /= hertz; 278 row = 1; 279 for (i = 0; i < CPUSTATES; i++) 280 stat1(row++, i); 281 if (!numbers) { 282 row += 2; 283 for (i = 0; i < num_devices; i++) 284 if (dev_select[i].selected) { 285 if (row > wnd->_maxy - linesperregion) 286 break; 287 row = devstats(row, INSET, i); 288 } 289 return; 290 } 291 _col = INSET; 292 wmove(wnd, row + linesperregion, 0); 293 wdeleteln(wnd); 294 wmove(wnd, row + 3, 0); 295 winsertln(wnd); 296 for (i = 0; i < num_devices; i++) 297 if (dev_select[i].selected) { 298 if (_col + COLWIDTH >= wnd->_maxx - INSET) { 299 _col = INSET, row += linesperregion + 1; 300 if (row > wnd->_maxy - (linesperregion + 1)) 301 break; 302 wmove(wnd, row + linesperregion, 0); 303 wdeleteln(wnd); 304 wmove(wnd, row + 3, 0); 305 winsertln(wnd); 306 } 307 (void) devstats(row + 3, _col, i); 308 _col += COLWIDTH; 309 } 310} 311 312static int 313devstats(row, _col, dn) 314 int row, _col, dn; 315{ 316 long double transfers_per_second; 317 long double kb_per_transfer, mb_per_second; 318 long double busy_seconds; 319 int di; 320 321 di = dev_select[dn].position; 322 323 busy_seconds = cur.snap_time - last.snap_time; 324 325 if (devstat_compute_statistics(&cur.dinfo->devices[di], 326 &last.dinfo->devices[di], busy_seconds, 327 DSM_KB_PER_TRANSFER, &kb_per_transfer, 328 DSM_TRANSFERS_PER_SECOND, &transfers_per_second, 329 DSM_MB_PER_SECOND, &mb_per_second, DSM_NONE) != 0) 330 errx(1, "%s", devstat_errbuf); 331 332 if (numbers) { 333 mvwprintw(wnd, row, _col, " %5.2Lf %3.0Lf %5.2Lf ", 334 kb_per_transfer, transfers_per_second, 335 mb_per_second); 336 return(row); 337 } 338 wmove(wnd, row++, _col); 339 histogram(mb_per_second, 50, .5); 340 wmove(wnd, row++, _col); 341 histogram(transfers_per_second, 50, .5); 342 if (kbpt) { 343 wmove(wnd, row++, _col); 344 histogram(kb_per_transfer, 50, .5); 345 } 346 347 return(row); 348 349} 350 351static void 352stat1(row, o) 353 int row, o; 354{ 355 int i; 356 double dtime; 357 358 dtime = 0.0; 359 for (i = 0; i < CPUSTATES; i++) 360 dtime += cur.cp_time[i]; 361 if (dtime == 0.0) 362 dtime = 1.0; 363 wmove(wnd, row, INSET); 364#define CPUSCALE 0.5 365 histogram(100.0 * cur.cp_time[o] / dtime, 50, CPUSCALE); 366} 367 368static void 369histogram(val, colwidth, scale) 370 long double val; 371 int colwidth; 372 double scale; 373{ 374 char buf[10]; 375 int k; 376 int v = (int)(val * scale) + 0.5; 377 378 k = MIN(v, colwidth); 379 if (v > colwidth) { 380 snprintf(buf, sizeof(buf), "%5.2Lf", val); 381 k -= strlen(buf); 382 while (k--) 383 waddch(wnd, 'X'); 384 waddstr(wnd, buf); 385 return; 386 } 387 while (k--) 388 waddch(wnd, 'X'); 389 wclrtoeol(wnd); 390} 391 392int 393cmdiostat(cmd, args) 394 const char *cmd, *args; 395{ 396 397 if (prefix(cmd, "kbpt")) 398 kbpt = !kbpt; 399 else if (prefix(cmd, "numbers")) 400 numbers = 1; 401 else if (prefix(cmd, "bars")) 402 numbers = 0; 403 else if (!dscmd(cmd, args, 100, &cur)) 404 return (0); 405 wclear(wnd); 406 labeliostat(); 407 refresh(); 408 return (1); 409} 410