Annotation of src/usr.bin/systat/iostat.c, Revision 1.27
1.27 ! deraadt 1: /* $OpenBSD: iostat.c,v 1.26 2006/06/29 21:17:27 dlg 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.27 ! deraadt 37: static char rcsid[] = "$OpenBSD: iostat.c,v 1.26 2006/06/29 21:17:27 dlg 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:
1.26 dlg 58: #define ATIME(x,y) ((double)x[y].tv_sec + \
59: ((double)x[y].tv_usec / (double)1000000))
60:
61: #define NFMT "%-8.8s %14.0f %14.0f %10.0f %10.0f %10.1f"
62: #define SFMT "%-8.8s %14s %14s %10s %10s %10s"
1.1 deraadt 63:
64: WINDOW *
1.18 deraadt 65: openiostat(void)
1.1 deraadt 66: {
1.27 ! deraadt 67: return (subwin(stdscr, LINES-1-1, 0, 1, 0));
1.1 deraadt 68: }
69:
70: void
1.18 deraadt 71: closeiostat(WINDOW *w)
1.1 deraadt 72: {
73: if (w == NULL)
74: return;
75: wclear(w);
76: wrefresh(w);
77: delwin(w);
78: }
79:
80: int
1.18 deraadt 81: initiostat(void)
1.1 deraadt 82: {
1.3 deraadt 83: dkinit(1);
84: dkreadstats();
1.9 kstailey 85: return (1);
1.1 deraadt 86: }
87:
88: void
1.18 deraadt 89: fetchiostat(void)
1.1 deraadt 90: {
1.19 tdeval 91: if (cur.dk_ndrive == 0)
1.1 deraadt 92: return;
1.3 deraadt 93: dkreadstats();
1.1 deraadt 94: }
95:
96: void
1.18 deraadt 97: labeliostat(void)
1.1 deraadt 98: {
1.26 dlg 99: mvwprintw(wnd, 1, 0, SFMT, "Device", "rKBytes", "wKBytes", "rtps",
1.25 dlg 100: "wtps", "msec");
1.1 deraadt 101: }
102:
103: void
1.18 deraadt 104: showiostat(void)
1.1 deraadt 105: {
1.25 dlg 106: int i;
1.1 deraadt 107:
1.3 deraadt 108: dkswap();
109:
1.25 dlg 110: etime = 0.0;
1.18 deraadt 111: for (i = 0; i < CPUSTATES; i++) {
1.3 deraadt 112: etime += cur.cp_time[i];
1.1 deraadt 113: }
1.25 dlg 114:
1.1 deraadt 115: if (etime == 0.0)
116: etime = 1.0;
1.25 dlg 117:
1.1 deraadt 118: etime /= (float) hz;
1.7 kstailey 119:
1.19 tdeval 120: if (last.dk_ndrive != cur.dk_ndrive)
121: labeliostat();
122:
123: if (cur.dk_ndrive == 0)
1.7 kstailey 124: return;
125:
1.25 dlg 126: numlabels();
1.1 deraadt 127: }
128:
1.25 dlg 129: void
130: numlabels(void)
1.1 deraadt 131: {
1.25 dlg 132: double rsum, wsum, rtsum, wtsum, mssum;
133: int row, dn;
1.3 deraadt 134:
1.25 dlg 135: row = 2;
136: wmove(wnd, 0, 0);
137: wclrtoeol(wnd);
138:
139: if (cur.dk_ndrive == 0) {
140: mvwaddstr(wnd, row, 0, "No drives attached.");
141: return;
1.23 tedu 142: }
1.1 deraadt 143:
1.25 dlg 144: rsum = wsum = rtsum = wtsum = mssum = 0.0;
1.1 deraadt 145:
1.25 dlg 146: for (dn = 0; dn < cur.dk_ndrive; dn++) {
147: rsum += cur.dk_rbytes[dn] / etime;
1.26 dlg 148: wsum += cur.dk_wbytes[dn] / etime;
1.25 dlg 149: rtsum += cur.dk_rxfer[dn] / etime;
150: wtsum += cur.dk_wxfer[dn] / etime;
151: mssum += ATIME(cur.dk_time, dn) / etime;
152: mvwprintw(wnd, row++, 0, NFMT,
153: cur.dk_name[dn],
1.26 dlg 154: cur.dk_rbytes[dn] / 1024.0 / etime,
155: cur.dk_wbytes[dn] / 1024.0 / etime,
1.25 dlg 156: cur.dk_rxfer[dn] / etime,
157: cur.dk_wxfer[dn] / etime,
158: ATIME(cur.dk_time, dn) / etime);
1.1 deraadt 159: }
1.25 dlg 160: mvwprintw(wnd, row++, 0, NFMT,
1.26 dlg 161: "Totals", rsum / 1024.0, wsum / 1024.0, rtsum, wtsum, mssum);
1.1 deraadt 162: }
163:
164: int
1.18 deraadt 165: cmdiostat(char *cmd, char *args)
1.1 deraadt 166: {
167: wclear(wnd);
168: labeliostat();
169: refresh();
170: return (1);
171: }