iostat.c revision 1.25
1/* $OpenBSD: iostat.c,v 1.25 2006/06/27 10:28:25 dlg 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. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33#ifndef lint 34#if 0 35static char sccsid[] = "@(#)iostat.c 8.1 (Berkeley) 6/6/93"; 36#endif 37static char rcsid[] = "$OpenBSD: iostat.c,v 1.25 2006/06/27 10:28:25 dlg Exp $"; 38#endif /* not lint */ 39 40#include <sys/param.h> 41#include <sys/dkstat.h> 42#include <sys/buf.h> 43#include <sys/time.h> 44 45#include <string.h> 46#include <stdlib.h> 47#include <paths.h> 48#include "systat.h" 49#include "extern.h" 50 51#include "dkstats.h" 52extern struct _disk cur, last; 53 54static double etime; 55 56static void numlabels(void); 57 58#define ATIME(x,y) ( \ 59 (double)x[y].tv_sec + \ 60 ((double)x[y].tv_usec / (double)1000000) \ 61 ) 62#define NFMT "%-8.8s %12.0f %14.0f %10.0f %10.0f %10.1f" 63#define SFMT "%6s %14s %14s %10s %10s %10s" 64 65WINDOW * 66openiostat(void) 67{ 68 return (subwin(stdscr, LINES-1-5, 0, 5, 0)); 69} 70 71void 72closeiostat(WINDOW *w) 73{ 74 if (w == NULL) 75 return; 76 wclear(w); 77 wrefresh(w); 78 delwin(w); 79} 80 81int 82initiostat(void) 83{ 84 dkinit(1); 85 dkreadstats(); 86 return (1); 87} 88 89void 90fetchiostat(void) 91{ 92 if (cur.dk_ndrive == 0) 93 return; 94 dkreadstats(); 95} 96 97void 98labeliostat(void) 99{ 100 mvwprintw(wnd, 1, 0, SFMT, "Device", "rbytes", "wbytes", "rtps", 101 "wtps", "msec"); 102} 103 104void 105showiostat(void) 106{ 107 int i; 108 109 dkswap(); 110 111 etime = 0.0; 112 for (i = 0; i < CPUSTATES; i++) { 113 etime += cur.cp_time[i]; 114 } 115 116 if (etime == 0.0) 117 etime = 1.0; 118 119 etime /= (float) hz; 120 121 if (last.dk_ndrive != cur.dk_ndrive) 122 labeliostat(); 123 124 if (cur.dk_ndrive == 0) 125 return; 126 127 numlabels(); 128} 129 130void 131numlabels(void) 132{ 133 double rsum, wsum, rtsum, wtsum, mssum; 134 int row, dn; 135 136 row = 2; 137 wmove(wnd, 0, 0); 138 wclrtoeol(wnd); 139 140 if (cur.dk_ndrive == 0) { 141 mvwaddstr(wnd, row, 0, "No drives attached."); 142 return; 143 } 144 145 rsum = wsum = rtsum = wtsum = mssum = 0.0; 146 147 for (dn = 0; dn < cur.dk_ndrive; dn++) { 148 rsum += cur.dk_rbytes[dn] / etime; 149 wsum += cur.dk_wbytes[dn] /etime; 150 rtsum += cur.dk_rxfer[dn] / etime; 151 wtsum += cur.dk_wxfer[dn] / etime; 152 mssum += ATIME(cur.dk_time, dn) / etime; 153 mvwprintw(wnd, row++, 0, NFMT, 154 cur.dk_name[dn], 155 cur.dk_rbytes[dn] / etime, 156 cur.dk_wbytes[dn] / etime, 157 cur.dk_rxfer[dn] / etime, 158 cur.dk_wxfer[dn] / etime, 159 ATIME(cur.dk_time, dn) / etime); 160 } 161 mvwprintw(wnd, row++, 0, NFMT, 162 "Totals", rsum, wsum, rtsum, wtsum, mssum); 163} 164 165int 166cmdiostat(char *cmd, char *args) 167{ 168 wclear(wnd); 169 labeliostat(); 170 refresh(); 171 return (1); 172} 173