iostat.c revision 1.6
1/* $OpenBSD: iostat.c,v 1.6 1997/01/06 18:50:50 maja Exp $ */ 2/* $NetBSD: iostat.c,v 1.5 1996/05/10 23:16:35 thorpej Exp $ */ 3 4/* 5 * Copyright (c) 1980, 1992, 1993 6 * The Regents of the University of California. 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 by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37#ifndef lint 38#if 0 39static char sccsid[] = "@(#)iostat.c 8.1 (Berkeley) 6/6/93"; 40#endif 41static char rcsid[] = "$OpenBSD: iostat.c,v 1.6 1997/01/06 18:50:50 maja Exp $"; 42#endif not lint 43 44#include <sys/param.h> 45#include <sys/dkstat.h> 46#include <sys/buf.h> 47#include <sys/time.h> 48 49#include <string.h> 50#include <stdlib.h> 51#include <nlist.h> 52#include <paths.h> 53#include "systat.h" 54#include "extern.h" 55 56#include "dkstats.h" 57extern struct _disk cur; 58 59static int linesperregion; 60static double etime; 61static int numbers = 0; /* default display bar graphs */ 62static int secs = 0; /* default seconds shown */ 63 64static int barlabels __P((int)); 65static void histogram __P((double, int, double)); 66static int numlabels __P((int)); 67static int stats __P((int, int, int)); 68static void stat1 __P((int, int)); 69 70 71WINDOW * 72openiostat() 73{ 74 return (subwin(stdscr, LINES-1-5, 0, 5, 0)); 75} 76 77void 78closeiostat(w) 79 WINDOW *w; 80{ 81 if (w == NULL) 82 return; 83 wclear(w); 84 wrefresh(w); 85 delwin(w); 86} 87 88int 89initiostat() 90{ 91 dkinit(1); 92 dkreadstats(); 93} 94 95void 96fetchiostat() 97{ 98 if (dk_ndrive == 0) 99 return; 100 dkreadstats(); 101} 102 103#define INSET 10 104 105void 106labeliostat() 107{ 108 int row; 109 110 if (dk_ndrive == 0) { 111 error("No drives defined."); 112 return; 113 } 114 row = 0; 115 wmove(wnd, row, 0); wclrtobot(wnd); 116 mvwaddstr(wnd, row++, INSET, 117 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100"); 118 mvwaddstr(wnd, row++, 0, "cpu user|"); 119 mvwaddstr(wnd, row++, 0, " nice|"); 120 mvwaddstr(wnd, row++, 0, " system|"); 121 mvwaddstr(wnd, row++, 0, "interrupt|"); 122 mvwaddstr(wnd, row++, 0, " idle|"); 123 if (numbers) 124 row = numlabels(row + 1); 125 else 126 row = barlabels(row + 1); 127} 128 129static int 130numlabels(row) 131 int row; 132{ 133 int i, col, regions, ndrives; 134 135#define COLWIDTH 14 136#define DRIVESPERLINE ((wnd->_maxx - INSET) / COLWIDTH) 137 for (ndrives = 0, i = 0; i < dk_ndrive; i++) 138 if (cur.dk_select[i]) 139 ndrives++; 140 regions = howmany(ndrives, DRIVESPERLINE); 141 /* 142 * Deduct -regions for blank line after each scrolling region. 143 */ 144 linesperregion = (wnd->_maxy - row - regions) / regions; 145 /* 146 * Minimum region contains space for two 147 * label lines and one line of statistics. 148 */ 149 if (linesperregion < 3) 150 linesperregion = 3; 151 col = 0; 152 for (i = 0; i < dk_ndrive; i++) 153 if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) { 154 if (col + COLWIDTH >= wnd->_maxx - INSET) { 155 col = 0, row += linesperregion + 1; 156 if (row > wnd->_maxy - (linesperregion + 1)) 157 break; 158 } 159 mvwaddstr(wnd, row, col + 4, cur.dk_name[i]); 160 mvwaddstr(wnd, row + 1, col, "Kps tps sec"); 161 col += COLWIDTH; 162 } 163 if (col) 164 row += linesperregion + 1; 165 return (row); 166} 167 168static int 169barlabels(row) 170 int row; 171{ 172 int i; 173 174 mvwaddstr(wnd, row++, INSET, 175 "/0 /10 /20 /30 /40 /50 /60 /70 /80 /90 /100"); 176 linesperregion = 2 + secs; 177 for (i = 0; i < dk_ndrive; i++) 178 if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) { 179 if (row > wnd->_maxy - linesperregion) 180 break; 181 mvwprintw(wnd, row++, 0, "%4.4s Kps|", cur.dk_name[i]); 182 mvwaddstr(wnd, row++, 0, " tps|"); 183 if (secs) 184 mvwaddstr(wnd, row++, 0, " msec|"); 185 } 186 return (row); 187} 188 189 190void 191showiostat() 192{ 193 register u_int64_t t; 194 register int i, row, col; 195 196 if (dk_ndrive == 0) 197 return; 198 dkswap(); 199 200 etime = 0; 201 for(i = 0; i < CPUSTATES; i++) { 202 etime += cur.cp_time[i]; 203 } 204 if (etime == 0.0) 205 etime = 1.0; 206 etime /= (float) hz; 207 row = 1; 208 209 /* 210 * Interrupt CPU state not calculated yet. 211 */ 212 for (i = 0; i < CPUSTATES; i++) 213 stat1(row++, i); 214 if (!numbers) { 215 row += 2; 216 for (i = 0; i < dk_ndrive; i++) 217 if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) { 218 if (row > wnd->_maxy - linesperregion) 219 break; 220 row = stats(row, INSET, i); 221 } 222 return; 223 } 224 col = 0; 225 wmove(wnd, row + linesperregion, 0); 226 wdeleteln(wnd); 227 wmove(wnd, row + 3, 0); 228 winsertln(wnd); 229 for (i = 0; i < dk_ndrive; i++) 230 if (cur.dk_select[i] /*&& cur.dk_bytes[i] != 0.0*/) { 231 if (col + COLWIDTH >= wnd->_maxx) { 232 col = 0, row += linesperregion + 1; 233 if (row > wnd->_maxy - (linesperregion + 1)) 234 break; 235 wmove(wnd, row + linesperregion, 0); 236 wdeleteln(wnd); 237 wmove(wnd, row + 3, 0); 238 winsertln(wnd); 239 } 240 (void) stats(row + 3, col, i); 241 col += COLWIDTH; 242 } 243} 244 245static int 246stats(row, col, dn) 247 int row, col, dn; 248{ 249 double atime, words; 250 251 /* time busy in disk activity */ 252 atime = (double)cur.dk_time[dn].tv_sec + 253 ((double)cur.dk_time[dn].tv_usec / (double)1000000); 254 255 words = cur.dk_bytes[dn] / 1024.0; /* # of K transferred */ 256 if (numbers) { 257 mvwprintw(wnd, row, col, "%3.0f%4.0f%5.1f", 258 words / etime, cur.dk_xfer[dn] / etime, atime / etime); 259 return (row); 260 } 261 wmove(wnd, row++, col); 262 histogram(words / etime, 50, 0.5); 263 wmove(wnd, row++, col); 264 histogram(cur.dk_xfer[dn] / etime, 50, 0.5); 265 if (secs) { 266 wmove(wnd, row++, col); 267 atime *= 1000; /* In milliseconds */ 268 histogram(atime / etime, 50, 0.5); 269 } 270 return (row); 271} 272 273static void 274stat1(row, o) 275 int row, o; 276{ 277 register int i; 278 double time; 279 280 time = 0; 281 for (i = 0; i < CPUSTATES; i++) 282 time += cur.cp_time[i]; 283 if (time == 0.0) 284 time = 1.0; 285 wmove(wnd, row, INSET); 286#define CPUSCALE 0.5 287 histogram(100.0 * cur.cp_time[o] / time, 50, CPUSCALE); 288} 289 290static void 291histogram(val, colwidth, scale) 292 double val; 293 int colwidth; 294 double scale; 295{ 296 char buf[10]; 297 register int k; 298 register int v = (int)(val * scale) + 0.5; 299 300 k = MIN(v, colwidth); 301 if (v > colwidth) { 302 sprintf(buf, "%4.1f", val); 303 k -= strlen(buf); 304 while (k--) 305 waddch(wnd, 'X'); 306 waddstr(wnd, buf); 307 wclrtoeol(wnd); 308 return; 309 } 310 while (k--) 311 waddch(wnd, 'X'); 312 wclrtoeol(wnd); 313} 314 315int 316cmdiostat(cmd, args) 317 char *cmd, *args; 318{ 319 320 if (prefix(cmd, "secs")) 321 secs = !secs; 322 else if (prefix(cmd, "numbers")) 323 numbers = 1; 324 else if (prefix(cmd, "bars")) 325 numbers = 0; 326 else if (!dkcmd(cmd, args)) 327 return (0); 328 wclear(wnd); 329 labeliostat(); 330 refresh(); 331 return (1); 332} 333