iostat.c revision 1.27
1/*	$OpenBSD: iostat.c,v 1.27 2007/02/25 18:21:24 deraadt 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.27 2007/02/25 18:21:24 deraadt 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) ((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"
63
64WINDOW *
65openiostat(void)
66{
67	return (subwin(stdscr, LINES-1-1, 0, 1, 0));
68}
69
70void
71closeiostat(WINDOW *w)
72{
73	if (w == NULL)
74		return;
75	wclear(w);
76	wrefresh(w);
77	delwin(w);
78}
79
80int
81initiostat(void)
82{
83	dkinit(1);
84	dkreadstats();
85	return (1);
86}
87
88void
89fetchiostat(void)
90{
91	if (cur.dk_ndrive == 0)
92		return;
93	dkreadstats();
94}
95
96void
97labeliostat(void)
98{
99	mvwprintw(wnd, 1, 0, SFMT, "Device", "rKBytes", "wKBytes", "rtps",
100	    "wtps", "msec");
101}
102
103void
104showiostat(void)
105{
106	int i;
107
108	dkswap();
109
110	etime = 0.0;
111	for (i = 0; i < CPUSTATES; i++) {
112		etime += cur.cp_time[i];
113	}
114
115	if (etime == 0.0)
116		etime = 1.0;
117
118	etime /= (float) hz;
119
120	if (last.dk_ndrive != cur.dk_ndrive)
121		labeliostat();
122
123	if (cur.dk_ndrive == 0)
124		return;
125
126	numlabels();
127}
128
129void
130numlabels(void)
131{
132	double rsum, wsum, rtsum, wtsum, mssum;
133	int row, dn;
134
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;
142	}
143
144	rsum = wsum = rtsum = wtsum = mssum = 0.0;
145
146	for (dn = 0; dn < cur.dk_ndrive; dn++) {
147		rsum += cur.dk_rbytes[dn] / etime;
148		wsum += cur.dk_wbytes[dn] / etime;
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],
154		    cur.dk_rbytes[dn] / 1024.0 / etime,
155		    cur.dk_wbytes[dn] / 1024.0 / etime,
156		    cur.dk_rxfer[dn] / etime,
157		    cur.dk_wxfer[dn] / etime,
158		    ATIME(cur.dk_time, dn) / etime);
159	}
160	mvwprintw(wnd, row++, 0, NFMT,
161	    "Totals", rsum / 1024.0, wsum / 1024.0, rtsum, wtsum, mssum);
162}
163
164int
165cmdiostat(char *cmd, char *args)
166{
167	wclear(wnd);
168	labeliostat();
169	refresh();
170	return (1);
171}
172