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$"); 64 65 #ifdef lint 66 static 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 <paths.h> 77 #include <stdlib.h> 78 #include <string.h> 79 80 #include "systat.h" 81 #include "extern.h" 82 #include "devs.h" 83 84 struct statinfo cur, last; 85 86 static int linesperregion; 87 static double etime; 88 static int numbers = 0; /* default display bar graphs */ 89 static int kbpt = 0; /* default ms/seek shown */ 90 91 static int barlabels(int); 92 static void histogram(long double, int, double); 93 static int numlabels(int); 94 static int devstats(int, int, int); 95 static void stat1(int, int); 96 97 WINDOW * 98 openiostat(void) 99 { 100 return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0)); 101 } 102 103 void 104 closeiostat(WINDOW *w) 105 { 106 if (w == NULL) 107 return; 108 wclear(w); 109 wrefresh(w); 110 delwin(w); 111 } 112 113 int 114 initiostat(void) 115 { 116 if ((num_devices = devstat_getnumdevs(NULL)) < 0) 117 return(0); 118 119 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 120 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 121 bzero(cur.dinfo, sizeof(struct devinfo)); 122 bzero(last.dinfo, sizeof(struct devinfo)); 123 124 /* 125 * This value for maxshowdevs (100) is bogus. I'm not sure exactly 126 * how to calculate it, though. 127 */ 128 if (dsinit(100, &cur, &last, NULL) != 1) 129 return(0); 130 131 return(1); 132 } 133 134 void 135 fetchiostat(void) 136 { 137 struct devinfo *tmp_dinfo; 138 size_t len; 139 140 len = sizeof(cur.cp_time); 141 if (sysctlbyname("kern.cp_time", &cur.cp_time, &len, NULL, 0) 142 || len != sizeof(cur.cp_time)) { 143 perror("kern.cp_time"); 144 exit (1); 145 } 146 tmp_dinfo = last.dinfo; 147 last.dinfo = cur.dinfo; 148 cur.dinfo = tmp_dinfo; 149 150 last.snap_time = cur.snap_time; 151 152 /* 153 * Here what we want to do is refresh our device stats. 154 * getdevs() returns 1 when the device list has changed. 155 * If the device list has changed, we want to go through 156 * the selection process again, in case a device that we 157 * were previously displaying has gone away. 158 */ 159 switch (devstat_getdevs(NULL, &cur)) { 160 case -1: 161 errx(1, "%s", devstat_errbuf); 162 break; 163 case 1: 164 cmdiostat("refresh", NULL); 165 break; 166 default: 167 break; 168 } 169 num_devices = cur.dinfo->numdevs; 170 generation = cur.dinfo->generation; 171 172 } 173 174 #define INSET 10 175 176 void 177 labeliostat(void) 178 { 179 int row; 180 181 row = 0; 182 wmove(wnd, row, 0); wclrtobot(wnd); 183 mvwaddstr(wnd, row++, INSET, 184 "/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100"); 185 mvwaddstr(wnd, row++, 0, "cpu user|"); 186 mvwaddstr(wnd, row++, 0, " nice|"); 187 mvwaddstr(wnd, row++, 0, " system|"); 188 mvwaddstr(wnd, row++, 0, "interrupt|"); 189 mvwaddstr(wnd, row++, 0, " idle|"); 190 if (numbers) 191 row = numlabels(row + 1); 192 else 193 row = barlabels(row + 1); 194 } 195 196 static int 197 numlabels(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 237 static int 238 barlabels(int row) 239 { 240 int i; 241 char tmpstr[10]; 242 243 mvwaddstr(wnd, row++, INSET, 244 "/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100"); 245 linesperregion = 2 + kbpt; 246 for (i = 0; i < num_devices; i++) 247 if (dev_select[i].selected) { 248 if (row > wnd->_maxy - linesperregion) 249 break; 250 sprintf(tmpstr, "%s%d", dev_select[i].device_name, 251 dev_select[i].unit_number); 252 mvwprintw(wnd, row++, 0, "%-5.5s MB/s|", 253 tmpstr); 254 mvwaddstr(wnd, row++, 0, " tps|"); 255 if (kbpt) 256 mvwaddstr(wnd, row++, 0, " KB/t|"); 257 } 258 return (row); 259 } 260 261 262 void 263 showiostat(void) 264 { 265 long t; 266 int i, row, _col; 267 268 #define X(fld) t = cur.fld[i]; cur.fld[i] -= last.fld[i]; last.fld[i] = t 269 etime = 0; 270 for(i = 0; i < CPUSTATES; i++) { 271 X(cp_time); 272 etime += cur.cp_time[i]; 273 } 274 if (etime == 0.0) 275 etime = 1.0; 276 etime /= hertz; 277 row = 1; 278 for (i = 0; i < CPUSTATES; i++) 279 stat1(row++, i); 280 if (!numbers) { 281 row += 2; 282 for (i = 0; i < num_devices; i++) 283 if (dev_select[i].selected) { 284 if (row > wnd->_maxy - linesperregion) 285 break; 286 row = devstats(row, INSET, i); 287 } 288 return; 289 } 290 _col = INSET; 291 wmove(wnd, row + linesperregion, 0); 292 wdeleteln(wnd); 293 wmove(wnd, row + 3, 0); 294 winsertln(wnd); 295 for (i = 0; i < num_devices; i++) 296 if (dev_select[i].selected) { 297 if (_col + COLWIDTH >= wnd->_maxx - INSET) { 298 _col = INSET, row += linesperregion + 1; 299 if (row > wnd->_maxy - (linesperregion + 1)) 300 break; 301 wmove(wnd, row + linesperregion, 0); 302 wdeleteln(wnd); 303 wmove(wnd, row + 3, 0); 304 winsertln(wnd); 305 } 306 (void) devstats(row + 3, _col, i); 307 _col += COLWIDTH; 308 } 309 } 310 311 static int 312 devstats(int row, int _col, int dn) 313 { 314 long double transfers_per_second; 315 long double kb_per_transfer, mb_per_second; 316 long double busy_seconds; 317 int di; 318 319 di = dev_select[dn].position; 320 321 busy_seconds = cur.snap_time - last.snap_time; 322 323 if (devstat_compute_statistics(&cur.dinfo->devices[di], 324 &last.dinfo->devices[di], busy_seconds, 325 DSM_KB_PER_TRANSFER, &kb_per_transfer, 326 DSM_TRANSFERS_PER_SECOND, &transfers_per_second, 327 DSM_MB_PER_SECOND, &mb_per_second, DSM_NONE) != 0) 328 errx(1, "%s", devstat_errbuf); 329 330 if (numbers) { 331 mvwprintw(wnd, row, _col, " %5.2Lf %3.0Lf %5.2Lf ", 332 kb_per_transfer, transfers_per_second, 333 mb_per_second); 334 return(row); 335 } 336 wmove(wnd, row++, _col); 337 histogram(mb_per_second, 50, .5); 338 wmove(wnd, row++, _col); 339 histogram(transfers_per_second, 50, .5); 340 if (kbpt) { 341 wmove(wnd, row++, _col); 342 histogram(kb_per_transfer, 50, .5); 343 } 344 345 return(row); 346 347 } 348 349 static void 350 stat1(int row, int o) 351 { 352 int i; 353 double dtime; 354 355 dtime = 0.0; 356 for (i = 0; i < CPUSTATES; i++) 357 dtime += cur.cp_time[i]; 358 if (dtime == 0.0) 359 dtime = 1.0; 360 wmove(wnd, row, INSET); 361 #define CPUSCALE 0.5 362 histogram(100.0 * cur.cp_time[o] / dtime, 50, CPUSCALE); 363 } 364 365 static void 366 histogram(long double val, int colwidth, double scale) 367 { 368 char buf[10]; 369 int k; 370 int v = (int)(val * scale) + 0.5; 371 372 k = MIN(v, colwidth); 373 if (v > colwidth) { 374 snprintf(buf, sizeof(buf), "%5.2Lf", val); 375 k -= strlen(buf); 376 while (k--) 377 waddch(wnd, 'X'); 378 waddstr(wnd, buf); 379 return; 380 } 381 while (k--) 382 waddch(wnd, 'X'); 383 wclrtoeol(wnd); 384 } 385 386 int 387 cmdiostat(const char *cmd, const char *args) 388 { 389 390 if (prefix(cmd, "kbpt")) 391 kbpt = !kbpt; 392 else if (prefix(cmd, "numbers")) 393 numbers = 1; 394 else if (prefix(cmd, "bars")) 395 numbers = 0; 396 else if (!dscmd(cmd, args, 100, &cur)) 397 return (0); 398 wclear(wnd); 399 labeliostat(); 400 refresh(); 401 return (1); 402 } 403