[PATCH] btt: Fixed %utilization computation: idle not being computed right.
[blktrace.git] / btt / iostat.c
CommitLineData
21e47d90
ADB
1/*
2 * blktrace output analysis: generate a timeline & gather statistics
3 *
4 * Copyright (C) 2006 Alan D. Brunelle <Alan.Brunelle@hp.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21#include <stdio.h>
22#include <unistd.h>
23#include "globals.h"
24
21e47d90
ADB
25#define INC_STAT(dip, fld) \
26 do { \
27 (dip)->stats. fld ++; \
28 (dip)->all_stats. fld ++; \
29 } while (0)
30
31#define DEC_STAT(dip, fld) \
32 do { \
33 (dip)->stats. fld --; \
34 (dip)->all_stats. fld --; \
35 } while (0)
36
37#define ADD_STAT(dip, fld, val) \
38 do { \
39 __u64 __v = (val); \
40 (dip)->stats. fld += __v; \
41 (dip)->all_stats. fld += __v; \
42 } while (0)
43
44#define SUB_STAT(dip, fld, val) \
45 do { \
46 __u64 __v = (val); \
47 (dip)->stats. fld -= __v; \
48 (dip)->all_stats. fld -= __v; \
49 } while (0)
50
51__u64 last_start, iostat_last_stamp;
52__u64 iostat_interval = 1;
53char *iostat_name = NULL;
54FILE *iostat_ofp = NULL;
55
56void calc_waits(struct io *d_iop, __u64 c_time)
57{
58 __u64 waits = 0;
59 struct list_head *p;
60 struct io_list *iolp;
61
62 __list_for_each(p, &d_iop->u.d.d_im_head) {
63 iolp = list_entry(p, struct io_list, head);
64 waits += (c_time - iolp->iop->t.time);
65 }
66 ADD_STAT(d_iop->dip, wait, waits);
67}
68
69void dump_hdr(void)
70{
71 fprintf(iostat_ofp, "Device: rrqm/s wrqm/s r/s w/s "
72 "rsec/s wsec/s rkB/s wkB/s "
73 "avgrq-sz avgqu-sz await svctm %%util Stamp\n");
74}
75
76void iostat_init(void)
77{
78 last_start = (__u64)-1;
79 if (iostat_ofp)
80 dump_hdr();
81}
82
83void update_tot_qusz(struct d_info *dip, double now)
84{
85 dip->stats.tot_qusz += ((now - dip->stats.last_qu_change) *
86 dip->stats.cur_qusz);
87 dip->all_stats.tot_qusz += ((now - dip->all_stats.last_qu_change) *
88 dip->all_stats.cur_qusz);
89
90 dip->stats.last_qu_change = dip->all_stats.last_qu_change = now;
91}
92
d216e9ce 93void update_idle_time(struct d_info *dip, double now, int force)
21e47d90 94{
d216e9ce 95 if (dip->stats.cur_dev == 0 || force) {
21e47d90
ADB
96 dip->stats.idle_time += (now - dip->stats.last_dev_change);
97 dip->all_stats.idle_time +=
98 (now - dip->all_stats.last_dev_change);
99 }
100 dip->stats.last_dev_change = dip->all_stats.last_dev_change = now;
101}
102
103void __dump_stats(__u64 stamp, int all, struct d_info *dip)
104{
105 char hdr[16];
106 struct stats *sp;
d216e9ce 107 double dt, nios, avgrq_sz, p_util, nrqm, await, svctm;
21e47d90
ADB
108 double now = TO_SEC(stamp);
109
110 if (all) {
111 dt = (double)stamp / 1.0e9;
112 sp = &dip->all_stats;
113 }
114 else {
115 dt = (double)(stamp-last_start) / 1.0e9;
116 sp = &dip->stats;
117 }
118
119 nios = (double)(sp->ios[0] + sp->ios[1]);
120 nrqm = (double)(sp->rqm[0] + sp->rqm[1]);
d216e9ce
ADB
121 update_idle_time(dip, now, 1);
122 update_tot_qusz(dip, now);
123
21e47d90 124 if (nios > 0.0) {
21e47d90 125 avgrq_sz = (double)(sp->sec[0] + sp->sec[1]) / nios;
d216e9ce
ADB
126 svctm = TO_MSEC(sp->svctm) / nios;
127 }
128 else
129 avgrq_sz = svctm = 0.0;
130
131 await = ((nios + nrqm) > 0.0) ? TO_MSEC(sp->wait) / (nios+nrqm) : 0.0;
132 p_util = (sp->idle_time <= dt) ? 100.0 * (1.0 - (sp->idle_time / dt)) :
133 0.0;
134
135 /*
136 * For AWAIT: nios should be the same as number of inserts
137 * and we add in nrqm (number of merges), which should give
138 * us the total number of IOs sent to the block IO layer.
139 */
140 fprintf(iostat_ofp, "%-11s ", make_dev_hdr(hdr, 11, dip));
141 fprintf(iostat_ofp, "%8.2lf ", (double)sp->rqm[1] / dt);
142 fprintf(iostat_ofp, "%8.2lf ", (double)sp->rqm[0] / dt);
143 fprintf(iostat_ofp, "%7.2lf ", (double)sp->ios[1] / dt);
144 fprintf(iostat_ofp, "%7.2lf ", (double)sp->ios[0] / dt);
145 fprintf(iostat_ofp, "%9.2lf ", (double)sp->sec[1] / dt);
146 fprintf(iostat_ofp, "%9.2lf ", (double)sp->sec[0] / dt);
147 fprintf(iostat_ofp, "%9.2lf ", (double)(sp->sec[1] / 2) / dt);
148 fprintf(iostat_ofp, "%9.2lf ", (double)(sp->sec[0] / 2) / dt);
149 fprintf(iostat_ofp, "%8.2lf ", avgrq_sz);
150 fprintf(iostat_ofp, "%8.2lf ", (double)sp->tot_qusz / dt);
151 fprintf(iostat_ofp, "%7.2lf ", await);
152 fprintf(iostat_ofp, "%7.2lf ", svctm);
153 fprintf(iostat_ofp, "%6.2lf", p_util);
154 if (all)
155 fprintf(iostat_ofp, "%8s\n", "TOTAL");
156 else {
157 fprintf(iostat_ofp, "%8.2lf\n", TO_SEC(stamp));
158 sp->rqm[0] = sp->rqm[1] = 0;
159 sp->ios[0] = sp->ios[1] = 0;
160 sp->sec[0] = sp->sec[1] = 0;
161 sp->wait = sp->svctm = 0;
162
163 sp->tot_qusz = sp->idle_time = 0.0;
21e47d90
ADB
164 }
165}
166
167void iostat_dump_stats(__u64 stamp, int all)
168{
169 struct d_info *dip;
170
171 if (all)
172 dump_hdr();
173 if (devices == NULL) {
174 struct list_head *p;
175
176 __list_for_each(p, &all_devs) {
177 dip = list_entry(p, struct d_info, head);
178 __dump_stats(stamp, all, dip);
179 }
180 }
181 else {
182 int i;
183 unsigned int mjr, mnr;
184 char *p = devices;
185
186 while (p && ((i = sscanf(p, "%u,%u", &mjr, &mnr)) == 2)) {
187 dip = __dip_find((__u32)((mjr << MINORBITS) | mnr));
188 __dump_stats(stamp, all, dip);
189
190 p = strchr(p, ';');
191 if (p) p++;
192 }
193 }
d216e9ce
ADB
194 if (!all && iostat_ofp)
195 fprintf(iostat_ofp, "\n");
196
21e47d90
ADB
197}
198
199void iostat_check_time(__u64 stamp)
200{
201 if (iostat_ofp) {
202 if (last_start == (__u64)-1)
203 last_start = stamp;
204 else if ((stamp - last_start) >= iostat_interval) {
205 iostat_dump_stats(stamp, 0);
206 last_start = stamp;
207 }
208
209 iostat_last_stamp = stamp;
210 }
211}
212
213void iostat_insert(struct io *iop)
214{
215 update_tot_qusz(iop->dip, TO_SEC(iop->t.time));
216 INC_STAT(iop->dip, cur_qusz);
217}
218
219void iostat_merge(struct io *iop)
220{
221 INC_STAT(iop->dip, rqm[IOP_RW(iop)]);
222}
223
224void iostat_issue(struct io *iop)
225{
226 int rw = IOP_RW(iop);
227 struct d_info *dip = iop->dip;
228 double now = TO_SEC(iop->t.time);
229
230 INC_STAT(dip, ios[rw]);
231 ADD_STAT(dip, sec[rw], iop->t.bytes >> 9);
232
d216e9ce 233 update_idle_time(dip, now, 0);
21e47d90
ADB
234 INC_STAT(dip, cur_dev);
235}
236
237void iostat_complete(struct io *iop)
238{
239 struct io *d_iop;
240 struct d_info *dip = iop->dip;
241
242 if ((d_iop = iop->u.c.c_d) != NULL) {
243 double now = TO_SEC(iop->t.time);
244 calc_waits(d_iop, iop->t.time);
245
246 update_tot_qusz(dip, now);
247 DEC_STAT(dip, cur_qusz);
248
d216e9ce 249 update_idle_time(dip, now, 0);
21e47d90
ADB
250 DEC_STAT(dip, cur_dev);
251
252 ADD_STAT(dip, svctm, iop->t.time - d_iop->t.time);
253 }
254}