iostat.c revision 200420
1/* 2 * Copyright (c) 1998 Kenneth D. Merry. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28/* 29 * Copyright (c) 1980, 1992, 1993 30 * The Regents of the University of California. All rights reserved. 31 * 32 * Redistribution and use in source and binary forms, with or without 33 * modification, are permitted provided that the following conditions 34 * are met: 35 * 1. Redistributions of source code must retain the above copyright 36 * notice, this list of conditions and the following disclaimer. 37 * 2. Redistributions in binary form must reproduce the above copyright 38 * notice, this list of conditions and the following disclaimer in the 39 * documentation and/or other materials provided with the distribution. 40 * 3. All advertising materials mentioning features or use of this software 41 * must display the following acknowledgement: 42 * This product includes software developed by the University of 43 * California, Berkeley and its contributors. 44 * 4. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 */ 60 61#include <sys/cdefs.h> 62 63__FBSDID("$FreeBSD: head/usr.bin/systat/iostat.c 200420 2009-12-11 23:35:38Z delphij $"); 64 65#ifdef lint 66static const char sccsid[] = "@(#)iostat.c 8.1 (Berkeley) 6/6/93"; 67#endif 68 69#include <sys/param.h> 70#include <sys/sysctl.h> 71#include <sys/resource.h> 72 73#include <devstat.h> 74#include <err.h> 75#include <nlist.h> 76#include <stdlib.h> 77#include <string.h> 78 79#include "systat.h" 80#include "extern.h" 81#include "devs.h" 82 83struct statinfo cur, last; 84 85static int linesperregion; 86static double etime; 87static int numbers = 0; /* default display bar graphs */ 88static int kbpt = 0; /* default ms/seek shown */ 89 90static int barlabels(int); 91static void histogram(long double, int, double); 92static int numlabels(int); 93static int devstats(int, int, int); 94static void stat1(int, int); 95 96WINDOW * 97openiostat(void) 98{ 99 return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0)); 100} 101 102void 103closeiostat(WINDOW *w) 104{ 105 if (w == NULL) 106 return; 107 wclear(w); 108 wrefresh(w); 109 delwin(w); 110} 111 112int 113initiostat(void) 114{ 115 if ((num_devices = devstat_getnumdevs(NULL)) < 0) 116 return(0); 117 118 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 119 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 120 bzero(cur.dinfo, sizeof(struct devinfo)); 121 bzero(last.dinfo, sizeof(struct devinfo)); 122 123 /* 124 * This value for maxshowdevs (100) is bogus. I'm not sure exactly 125 * how to calculate it, though. 126 */ 127 if (dsinit(100, &cur, &last, NULL) != 1) 128 return(0); 129 130 return(1); 131} 132 133void 134fetchiostat(void) 135{ 136 struct devinfo *tmp_dinfo; 137 size_t len; 138 139 len = sizeof(cur.cp_time); 140 if (sysctlbyname("kern.cp_time", &cur.cp_time, &len, NULL, 0) 141 || len != sizeof(cur.cp_time)) { 142 perror("kern.cp_time"); 143 exit (1); 144 } 145 tmp_dinfo = last.dinfo; 146 last.dinfo = cur.dinfo; 147 cur.dinfo = tmp_dinfo; 148 149 last.snap_time = cur.snap_time; 150 151 /* 152 * Here what we want to do is refresh our device stats. 153 * getdevs() returns 1 when the device list has changed. 154 * If the device list has changed, we want to go through 155 * the selection process again, in case a device that we 156 * were previously displaying has gone away. 157 */ 158 switch (devstat_getdevs(NULL, &cur)) { 159 case -1: 160 errx(1, "%s", devstat_errbuf); 161 break; 162 case 1: 163 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(void) 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(int row) 197{ 198 int i, _col, regions, ndrives; 199 char tmpstr[10]; 200 201#define COLWIDTH 17 202#define DRIVESPERLINE ((wnd->_maxx - INSET) / COLWIDTH) 203 for (ndrives = 0, i = 0; i < num_devices; i++) 204 if (dev_select[i].selected) 205 ndrives++; 206 regions = howmany(ndrives, DRIVESPERLINE); 207 /* 208 * Deduct -regions for blank line after each scrolling region. 209 */ 210 linesperregion = (wnd->_maxy - row - regions) / regions; 211 /* 212 * Minimum region contains space for two 213 * label lines and one line of statistics. 214 */ 215 if (linesperregion < 3) 216 linesperregion = 3; 217 _col = INSET; 218 for (i = 0; i < num_devices; i++) 219 if (dev_select[i].selected) { 220 if (_col + COLWIDTH >= wnd->_maxx - INSET) { 221 _col = INSET, row += linesperregion + 1; 222 if (row > wnd->_maxy - (linesperregion + 1)) 223 break; 224 } 225 sprintf(tmpstr, "%s%d", dev_select[i].device_name, 226 dev_select[i].unit_number); 227 mvwaddstr(wnd, row, _col + 4, tmpstr); 228 mvwaddstr(wnd, row + 1, _col, " KB/t tps MB/s "); 229 _col += COLWIDTH; 230 } 231 if (_col) 232 row += linesperregion + 1; 233 return (row); 234} 235 236static int 237barlabels(int row) 238{ 239 int i; 240 char tmpstr[10]; 241 242 mvwaddstr(wnd, row++, INSET, 243 "/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100"); 244 linesperregion = 2 + kbpt; 245 for (i = 0; i < num_devices; i++) 246 if (dev_select[i].selected) { 247 if (row > wnd->_maxy - linesperregion) 248 break; 249 sprintf(tmpstr, "%s%d", dev_select[i].device_name, 250 dev_select[i].unit_number); 251 mvwprintw(wnd, row++, 0, "%-5.5s MB/s|", 252 tmpstr); 253 mvwaddstr(wnd, row++, 0, " tps|"); 254 if (kbpt) 255 mvwaddstr(wnd, row++, 0, " KB/t|"); 256 } 257 return (row); 258} 259 260 261void 262showiostat(void) 263{ 264 long t; 265 int i, row, _col; 266 267#define X(fld) t = cur.fld[i]; cur.fld[i] -= last.fld[i]; last.fld[i] = t 268 etime = 0; 269 for(i = 0; i < CPUSTATES; i++) { 270 X(cp_time); 271 etime += cur.cp_time[i]; 272 } 273 if (etime == 0.0) 274 etime = 1.0; 275 etime /= hertz; 276 row = 1; 277 for (i = 0; i < CPUSTATES; i++) 278 stat1(row++, i); 279 if (!numbers) { 280 row += 2; 281 for (i = 0; i < num_devices; i++) 282 if (dev_select[i].selected) { 283 if (row > wnd->_maxy - linesperregion) 284 break; 285 row = devstats(row, INSET, i); 286 } 287 return; 288 } 289 _col = INSET; 290 wmove(wnd, row + linesperregion, 0); 291 wdeleteln(wnd); 292 wmove(wnd, row + 3, 0); 293 winsertln(wnd); 294 for (i = 0; i < num_devices; i++) 295 if (dev_select[i].selected) { 296 if (_col + COLWIDTH >= wnd->_maxx - INSET) { 297 _col = INSET, row += linesperregion + 1; 298 if (row > wnd->_maxy - (linesperregion + 1)) 299 break; 300 wmove(wnd, row + linesperregion, 0); 301 wdeleteln(wnd); 302 wmove(wnd, row + 3, 0); 303 winsertln(wnd); 304 } 305 (void) devstats(row + 3, _col, i); 306 _col += COLWIDTH; 307 } 308} 309 310static int 311devstats(int row, int _col, int dn) 312{ 313 long double transfers_per_second; 314 long double kb_per_transfer, mb_per_second; 315 long double busy_seconds; 316 int di; 317 318 di = dev_select[dn].position; 319 320 busy_seconds = cur.snap_time - last.snap_time; 321 322 if (devstat_compute_statistics(&cur.dinfo->devices[di], 323 &last.dinfo->devices[di], busy_seconds, 324 DSM_KB_PER_TRANSFER, &kb_per_transfer, 325 DSM_TRANSFERS_PER_SECOND, &transfers_per_second, 326 DSM_MB_PER_SECOND, &mb_per_second, DSM_NONE) != 0) 327 errx(1, "%s", devstat_errbuf); 328 329 if (numbers) { 330 mvwprintw(wnd, row, _col, " %5.2Lf %3.0Lf %5.2Lf ", 331 kb_per_transfer, transfers_per_second, 332 mb_per_second); 333 return(row); 334 } 335 wmove(wnd, row++, _col); 336 histogram(mb_per_second, 50, .5); 337 wmove(wnd, row++, _col); 338 histogram(transfers_per_second, 50, .5); 339 if (kbpt) { 340 wmove(wnd, row++, _col); 341 histogram(kb_per_transfer, 50, .5); 342 } 343 344 return(row); 345 346} 347 348static void 349stat1(int row, int o) 350{ 351 int i; 352 double dtime; 353 354 dtime = 0.0; 355 for (i = 0; i < CPUSTATES; i++) 356 dtime += cur.cp_time[i]; 357 if (dtime == 0.0) 358 dtime = 1.0; 359 wmove(wnd, row, INSET); 360#define CPUSCALE 0.5 361 histogram(100.0 * cur.cp_time[o] / dtime, 50, CPUSCALE); 362} 363 364static void 365histogram(long double val, int colwidth, double scale) 366{ 367 char buf[10]; 368 int k; 369 int v = (int)(val * scale) + 0.5; 370 371 k = MIN(v, colwidth); 372 if (v > colwidth) { 373 snprintf(buf, sizeof(buf), "%5.2Lf", val); 374 k -= strlen(buf); 375 while (k--) 376 waddch(wnd, 'X'); 377 waddstr(wnd, buf); 378 return; 379 } 380 while (k--) 381 waddch(wnd, 'X'); 382 wclrtoeol(wnd); 383} 384 385int 386cmdiostat(const char *cmd, const char *args) 387{ 388 389 if (prefix(cmd, "kbpt")) 390 kbpt = !kbpt; 391 else if (prefix(cmd, "numbers")) 392 numbers = 1; 393 else if (prefix(cmd, "bars")) 394 numbers = 0; 395 else if (!dscmd(cmd, args, 100, &cur)) 396 return (0); 397 wclear(wnd); 398 labeliostat(); 399 refresh(); 400 return (1); 401} 402