Annotation of src/usr.bin/systat/iostat.c, Revision 1.25
1.25 ! dlg 1: /* $OpenBSD: iostat.c,v 1.24 2006/03/31 04:10:59 deraadt Exp $ */
1.3 deraadt 2: /* $NetBSD: iostat.c,v 1.5 1996/05/10 23:16:35 thorpej Exp $ */
1.1 deraadt 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.
1.21 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 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
35: static char sccsid[] = "@(#)iostat.c 8.1 (Berkeley) 6/6/93";
36: #endif
1.25 ! dlg 37: static char rcsid[] = "$OpenBSD: iostat.c,v 1.24 2006/03/31 04:10:59 deraadt Exp $";
1.12 heko 38: #endif /* not lint */
1.1 deraadt 39:
40: #include <sys/param.h>
41: #include <sys/dkstat.h>
42: #include <sys/buf.h>
1.3 deraadt 43: #include <sys/time.h>
1.1 deraadt 44:
45: #include <string.h>
46: #include <stdlib.h>
47: #include <paths.h>
48: #include "systat.h"
49: #include "extern.h"
50:
1.3 deraadt 51: #include "dkstats.h"
1.19 tdeval 52: extern struct _disk cur, last;
1.1 deraadt 53:
1.25 ! dlg 54: static double etime;
1.1 deraadt 55:
1.25 ! dlg 56: static 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"
1.1 deraadt 64:
65: WINDOW *
1.18 deraadt 66: openiostat(void)
1.1 deraadt 67: {
68: return (subwin(stdscr, LINES-1-5, 0, 5, 0));
69: }
70:
71: void
1.18 deraadt 72: closeiostat(WINDOW *w)
1.1 deraadt 73: {
74: if (w == NULL)
75: return;
76: wclear(w);
77: wrefresh(w);
78: delwin(w);
79: }
80:
81: int
1.18 deraadt 82: initiostat(void)
1.1 deraadt 83: {
1.3 deraadt 84: dkinit(1);
85: dkreadstats();
1.9 kstailey 86: return (1);
1.1 deraadt 87: }
88:
89: void
1.18 deraadt 90: fetchiostat(void)
1.1 deraadt 91: {
1.19 tdeval 92: if (cur.dk_ndrive == 0)
1.1 deraadt 93: return;
1.3 deraadt 94: dkreadstats();
1.1 deraadt 95: }
96:
97: void
1.18 deraadt 98: labeliostat(void)
1.1 deraadt 99: {
1.25 ! dlg 100: mvwprintw(wnd, 1, 0, SFMT, "Device", "rbytes", "wbytes", "rtps",
! 101: "wtps", "msec");
1.1 deraadt 102: }
103:
104: void
1.18 deraadt 105: showiostat(void)
1.1 deraadt 106: {
1.25 ! dlg 107: int i;
1.1 deraadt 108:
1.3 deraadt 109: dkswap();
110:
1.25 ! dlg 111: etime = 0.0;
1.18 deraadt 112: for (i = 0; i < CPUSTATES; i++) {
1.3 deraadt 113: etime += cur.cp_time[i];
1.1 deraadt 114: }
1.25 ! dlg 115:
1.1 deraadt 116: if (etime == 0.0)
117: etime = 1.0;
1.25 ! dlg 118:
1.1 deraadt 119: etime /= (float) hz;
1.7 kstailey 120:
1.19 tdeval 121: if (last.dk_ndrive != cur.dk_ndrive)
122: labeliostat();
123:
124: if (cur.dk_ndrive == 0)
1.7 kstailey 125: return;
126:
1.25 ! dlg 127: numlabels();
1.1 deraadt 128: }
129:
1.25 ! dlg 130: void
! 131: numlabels(void)
1.1 deraadt 132: {
1.25 ! dlg 133: double rsum, wsum, rtsum, wtsum, mssum;
! 134: int row, dn;
1.3 deraadt 135:
1.25 ! dlg 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;
1.23 tedu 143: }
1.1 deraadt 144:
1.25 ! dlg 145: rsum = wsum = rtsum = wtsum = mssum = 0.0;
1.1 deraadt 146:
1.25 ! dlg 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);
1.1 deraadt 160: }
1.25 ! dlg 161: mvwprintw(wnd, row++, 0, NFMT,
! 162: "Totals", rsum, wsum, rtsum, wtsum, mssum);
1.1 deraadt 163: }
164:
165: int
1.18 deraadt 166: cmdiostat(char *cmd, char *args)
1.1 deraadt 167: {
168: wclear(wnd);
169: labeliostat();
170: refresh();
171: return (1);
172: }