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 * $FreeBSD$ 29 */ 30 /* 31 * Copyright (c) 1980, 1992, 1993 32 * The Regents of the University of California. All rights reserved. 33 * 34 * Redistribution and use in source and binary forms, with or without 35 * modification, are permitted provided that the following conditions 36 * are met: 37 * 1. Redistributions of source code must retain the above copyright 38 * notice, this list of conditions and the following disclaimer. 39 * 2. Redistributions in binary form must reproduce the above copyright 40 * notice, this list of conditions and the following disclaimer in the 41 * documentation and/or other materials provided with the distribution. 42 * 3. All advertising materials mentioning features or use of this software 43 * must display the following acknowledgement: 44 * This product includes software developed by the University of 45 * California, Berkeley and its contributors. 46 * 4. Neither the name of the University nor the names of its contributors 47 * may be used to endorse or promote products derived from this software 48 * without specific prior written permission. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60 * SUCH DAMAGE. 61 */ 62 63 #ifndef lint 64 static char sccsid[] = "@(#)iostat.c 8.1 (Berkeley) 6/6/93"; 65 #endif not lint 66 67 #include <sys/param.h> 68 #include <sys/dkstat.h> 69 #include <sys/sysctl.h> 70 71 #include <string.h> 72 #include <stdlib.h> 73 #include <nlist.h> 74 #include <paths.h> 75 #include <devstat.h> 76 #include <err.h> 77 #include "systat.h" 78 #include "extern.h" 79 #include "devs.h" 80 81 struct statinfo cur, last; 82 83 static int linesperregion; 84 static double etime; 85 static int numbers = 0; /* default display bar graphs */ 86 static int kbpt = 0; /* default ms/seek shown */ 87 88 static int barlabels __P((int)); 89 static void histogram __P((long double, int, double)); 90 static int numlabels __P((int)); 91 static int devstats __P((int, int, int)); 92 static void stat1 __P((int, int)); 93 94 WINDOW * 95 openiostat() 96 { 97 return (subwin(stdscr, LINES-1-5, 0, 5, 0)); 98 } 99 100 void 101 closeiostat(w) 102 WINDOW *w; 103 { 104 if (w == NULL) 105 return; 106 wclear(w); 107 wrefresh(w); 108 delwin(w); 109 } 110 111 int 112 initiostat() 113 { 114 if ((num_devices = getnumdevs()) < 0) 115 return(0); 116 117 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 118 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 119 bzero(cur.dinfo, sizeof(struct devinfo)); 120 bzero(last.dinfo, sizeof(struct devinfo)); 121 122 /* 123 * This value for maxshowdevs (100) is bogus. I'm not sure exactly 124 * how to calculate it, though. 125 */ 126 if (dsinit(100, &cur, &last, NULL) != 1) 127 return(0); 128 129 return(1); 130 } 131 132 void 133 fetchiostat() 134 { 135 struct devinfo *tmp_dinfo; 136 size_t len; 137 int err; 138 139 len = sizeof(cur.cp_time); 140 err = sysctlbyname("kern.cp_time", &cur.cp_time, &len, NULL, 0); 141 if (err || 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.busy_time = cur.busy_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 (getdevs(&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 175 void 176 labeliostat() 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 195 static int 196 numlabels(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 237 static int 238 barlabels(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 263 void 264 showiostat() 265 { 266 register long t; 267 register 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 312 static int 313 devstats(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 = compute_etime(cur.busy_time, last.busy_time); 324 325 if (compute_stats(&cur.dinfo->devices[di], &last.dinfo->devices[di], 326 busy_seconds, NULL, NULL, NULL, 327 &kb_per_transfer, &transfers_per_second, 328 &mb_per_second, NULL, NULL) != 0) 329 errx(1, "%s", devstat_errbuf); 330 331 if (numbers) { 332 mvwprintw(wnd, row, col, " %5.2Lf %3.0Lf %5.2Lf ", 333 kb_per_transfer, transfers_per_second, 334 mb_per_second); 335 return(row); 336 } 337 wmove(wnd, row++, col); 338 histogram(mb_per_second, 50, .5); 339 wmove(wnd, row++, col); 340 histogram(transfers_per_second, 50, .5); 341 if (kbpt) { 342 wmove(wnd, row++, col); 343 histogram(kb_per_transfer, 50, .5); 344 } 345 346 return(row); 347 348 } 349 350 static void 351 stat1(row, o) 352 int row, o; 353 { 354 register int i; 355 double time; 356 357 time = 0; 358 for (i = 0; i < CPUSTATES; i++) 359 time += cur.cp_time[i]; 360 if (time == 0.0) 361 time = 1.0; 362 wmove(wnd, row, INSET); 363 #define CPUSCALE 0.5 364 histogram(100.0 * cur.cp_time[o] / time, 50, CPUSCALE); 365 } 366 367 static void 368 histogram(val, colwidth, scale) 369 long double val; 370 int colwidth; 371 double scale; 372 { 373 char buf[10]; 374 register int k; 375 register int v = (int)(val * scale) + 0.5; 376 377 k = MIN(v, colwidth); 378 if (v > colwidth) { 379 snprintf(buf, sizeof(buf), "%5.2Lf", val); 380 k -= strlen(buf); 381 while (k--) 382 waddch(wnd, 'X'); 383 waddstr(wnd, buf); 384 return; 385 } 386 while (k--) 387 waddch(wnd, 'X'); 388 wclrtoeol(wnd); 389 } 390 391 int 392 cmdiostat(cmd, args) 393 char *cmd, *args; 394 { 395 396 if (prefix(cmd, "kbpt")) 397 kbpt = !kbpt; 398 else if (prefix(cmd, "numbers")) 399 numbers = 1; 400 else if (prefix(cmd, "bars")) 401 numbers = 0; 402 else if (!dscmd(cmd, args, 100, &cur)) 403 return (0); 404 wclear(wnd); 405 labeliostat(); 406 refresh(); 407 return (1); 408 } 409