aboutsummaryrefslogtreecommitdiff
path: root/btt/iostat.c
blob: 8bcd2e52c5568c7871089888af5b5c8e389bed4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * blktrace output analysis: generate a timeline & gather statistics
 *
 * Copyright (C) 2006 Alan D. Brunelle <Alan.Brunelle@hp.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include <stdio.h>
#include <unistd.h>
#include "globals.h"

#define INC_STAT(dip, fld)						\
	do {								\
		(dip)->stats. fld ++;					\
		(dip)->all_stats. fld ++;				\
	} while (0)

#define DEC_STAT(dip, fld)						\
	do {								\
		(dip)->stats. fld --;					\
		(dip)->all_stats. fld --;				\
	} while (0)

#define ADD_STAT(dip, fld, val)						\
	do {								\
		__u64 __v = (val);					\
		(dip)->stats. fld += __v;				\
		(dip)->all_stats. fld += __v;				\
	} while (0)

#define SUB_STAT(dip, fld, val)						\
	do {								\
		__u64 __v = (val);					\
		(dip)->stats. fld -= __v;				\
		(dip)->all_stats. fld -= __v;				\
	} while (0)

__u64 last_start, iostat_last_stamp;
__u64 iostat_interval = 1000000000;
char *iostat_name = NULL;
FILE *iostat_ofp = NULL;

static void dump_hdr(void)
{
	fprintf(iostat_ofp, "Device:       rrqm/s   wrqm/s     r/s     w/s    "
			    "rsec/s    wsec/s     rkB/s     wkB/s "
			    "avgrq-sz avgqu-sz   await   svctm  %%util   Stamp\n");
}

static void update_tot_qusz(struct d_info *dip, double now)
{
	dip->stats.tot_qusz += ((now - dip->stats.last_qu_change) *
						dip->stats.cur_qusz);
	dip->all_stats.tot_qusz += ((now - dip->all_stats.last_qu_change) *
						dip->all_stats.cur_qusz);

	dip->stats.last_qu_change = dip->all_stats.last_qu_change = now;
}

static void update_idle_time(struct d_info *dip, double now, int force)
{
	if (dip->stats.cur_dev == 0 || force) {
		dip->stats.idle_time += (now - dip->stats.last_dev_change);
		dip->all_stats.idle_time +=
				       (now - dip->all_stats.last_dev_change);
	}
	dip->stats.last_dev_change = dip->all_stats.last_dev_change = now;
}

void __dump_stats(__u64 stamp, int all, struct d_info *dip, struct stats_t *asp)
{
	char hdr[16];
	struct stats *sp;
	double dt, nios, avgrq_sz, p_util, nrqm, await, svctm;
	double now = TO_SEC(stamp);

	if (all) {
		dt = (double)stamp / 1.0e9;
		sp = &dip->all_stats;
	} else {
		dt = (double)(stamp-last_start) / 1.0e9;
		sp = &dip->stats;
	}

	nios = (double)(sp->ios[0] + sp->ios[1]);
	nrqm = (double)(sp->rqm[0] + sp->rqm[1]);
	update_idle_time(dip, now, 1);
	update_tot_qusz(dip, now);

	if (nios > 0.0) {
		avgrq_sz = (double)(sp->sec[0] + sp->sec[1]) / nios;
		svctm = TO_MSEC(sp->svctm) / nios;
	} else
		avgrq_sz = svctm = 0.0;

	await = ((nios + nrqm) > 0.0) ? TO_MSEC(sp->wait) / (nios+nrqm) : 0.0;
	p_util = (sp->idle_time <= dt) ? 100.0 * (1.0 - (sp->idle_time / dt)) :
	                                 0.0;

	/*
	 * For AWAIT: nios should be the same as number of inserts
	 * and we add in nrqm (number of merges), which should give
	 * us the total number of IOs sent to the block IO layer.
	 */
	fprintf(iostat_ofp, "%-11s ", make_dev_hdr(hdr, 11, dip, 1));
	fprintf(iostat_ofp, "%8.2lf ", (double)sp->rqm[1] / dt);
	fprintf(iostat_ofp, "%8.2lf ", (double)sp->rqm[0] / dt);
	fprintf(iostat_ofp, "%7.2lf ", (double)sp->ios[1] / dt);
	fprintf(iostat_ofp, "%7.2lf ", (double)sp->ios[0] / dt);
	fprintf(iostat_ofp, "%9.2lf ", (double)sp->sec[1] / dt);
	fprintf(iostat_ofp, "%9.2lf ", (double)sp->sec[0] / dt);
	fprintf(iostat_ofp, "%9.2lf ", (double)(sp->sec[1] / 2) / dt);
	fprintf(iostat_ofp, "%9.2lf ", (double)(sp->sec[0] / 2) / dt);
	fprintf(iostat_ofp, "%8.2lf ", avgrq_sz);
	fprintf(iostat_ofp, "%8.2lf ", (double)sp->tot_qusz / dt);
	fprintf(iostat_ofp, "%7.2lf ", await);
	fprintf(iostat_ofp, "%7.2lf ", svctm);
	fprintf(iostat_ofp, "%6.2lf", p_util);
	if (all)
		fprintf(iostat_ofp, "%8s\n", "TOTAL");
	else {
		fprintf(iostat_ofp, "%8.2lf\n", TO_SEC(stamp));
		sp->rqm[0] = sp->rqm[1] = 0;
		sp->ios[0] = sp->ios[1] = 0;
		sp->sec[0] = sp->sec[1] = 0;
		sp->wait = sp->svctm = 0;

		sp->tot_qusz = sp->idle_time = 0.0;
	}

	if (asp) {
		int i;

		asp->n += 1.0;
		for (i = 0; i < 2; i++) {
			asp->rqm_s[i] += ((double)sp->rqm[i] / dt);
			asp->ios_s[i] += ((double)sp->ios[i] / dt);
			asp->sec_s[i] += ((double)sp->sec[i] / dt);
		}
		asp->avgrq_sz += avgrq_sz;
		asp->avgqu_sz += (double)sp->tot_qusz / dt;
		asp->await += await;
		asp->svctm += svctm;
		asp->p_util += p_util;
	}
}

static void __dump_stats_t(__u64 stamp, struct stats_t *asp, int all)
{
	if (asp->n < 2.0) return;	// What's the point?

	fprintf(iostat_ofp, "%-11s ", "TOTAL");
	fprintf(iostat_ofp, "%8.2lf ", asp->rqm_s[0]);
	fprintf(iostat_ofp, "%8.2lf ", asp->rqm_s[1]);
	fprintf(iostat_ofp, "%7.2lf ", asp->ios_s[0]);
	fprintf(iostat_ofp, "%7.2lf ", asp->ios_s[1]);
	fprintf(iostat_ofp, "%9.2lf ", asp->sec_s[0]);
	fprintf(iostat_ofp, "%9.2lf ", asp->sec_s[1]);
	fprintf(iostat_ofp, "%9.2lf ", asp->sec_s[0] / 2.0);
	fprintf(iostat_ofp, "%9.2lf ", asp->sec_s[1] / 2.0);
	fprintf(iostat_ofp, "%8.2lf ", asp->avgrq_sz / asp->n);
	fprintf(iostat_ofp, "%8.2lf ", asp->avgqu_sz / asp->n);
	fprintf(iostat_ofp, "%7.2lf ", asp->await / asp->n);
	fprintf(iostat_ofp, "%7.2lf ", asp->svctm / asp->n);
	fprintf(iostat_ofp, "%6.2lf", asp->p_util / asp->n);
	if (all)
		fprintf(iostat_ofp, "%8s\n", "TOTAL");
	else 
		fprintf(iostat_ofp, "%8.2lf\n", TO_SEC(stamp));
}

void iostat_init(void)
{
	last_start = (__u64)-1;
	if (iostat_ofp)
		dump_hdr();
}

void iostat_dump_stats(__u64 stamp, int all)
{
	struct d_info *dip;
	struct stats_t as;

	memset(&as, 0, sizeof(struct stats_t));
	if (all)
		dump_hdr();

	if (devices == NULL) {
		struct list_head *p;

		__list_for_each(p, &all_devs) {
			dip = list_entry(p, struct d_info, all_head);
			__dump_stats(stamp, all, dip, &as);
		}
	} else {
		int i;
		unsigned int mjr, mnr;
		char *p = devices;

		while (p && ((i = sscanf(p, "%u,%u", &mjr, &mnr)) == 2)) {
			dip = __dip_find((__u32)((mjr << MINORBITS) | mnr));
			__dump_stats(stamp, all, dip, &as);

			p = strchr(p, ';');
			if (p) p++;
		}
	}

	__dump_stats_t(stamp, &as, all);

	if (!all && iostat_ofp)
		fprintf(iostat_ofp, "\n");
}

void iostat_check_time(__u64 stamp)
{
	if (iostat_ofp) {
		if (last_start == (__u64)-1)
			last_start = stamp;
		else if ((stamp - last_start) >= iostat_interval) {
			iostat_dump_stats(stamp, 0);
			last_start = stamp;
		}

		iostat_last_stamp = stamp;
	}
}

void iostat_getrq(struct io *iop)
{
	update_tot_qusz(iop->dip, TO_SEC(iop->t.time));
	INC_STAT(iop->dip, cur_qusz);
}

void iostat_merge(struct io *iop)
{
	INC_STAT(iop->dip, rqm[IOP_RW(iop)]);
}

void iostat_issue(struct io *iop)
{
	int rw = IOP_RW(iop);
	struct d_info *dip = iop->dip;
	double now = TO_SEC(iop->t.time);

	INC_STAT(dip, ios[rw]);
	ADD_STAT(dip, sec[rw], iop->t.bytes >> 9);

	update_idle_time(dip, now, 0);
	INC_STAT(dip, cur_dev);
}

void iostat_complete(struct io *q_iop, struct io *c_iop)
{
	double now = TO_SEC(c_iop->t.time);
	struct d_info *dip = q_iop->dip;

	if (q_iop->i_time != (__u64)-1)
		ADD_STAT(c_iop->dip, wait, tdelta(q_iop->i_time,c_iop->t.time));
	else if (q_iop->m_time != (__u64)-1)
		ADD_STAT(c_iop->dip, wait, tdelta(q_iop->m_time,c_iop->t.time));

	update_tot_qusz(dip, now);
	DEC_STAT(dip, cur_qusz);

	update_idle_time(dip, now, 0);
	DEC_STAT(dip, cur_dev);

	ADD_STAT(dip, svctm, tdelta(q_iop->t.time, c_iop->t.time));
}