2 * blktrace output analysis: generate a timeline & gather statistics
4 * Copyright (C) 2006 Alan D. Brunelle <Alan.Brunelle@hp.com>
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.
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.
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
25 #define INC_STAT(dip, fld) \
27 (dip)->stats. fld ++; \
28 (dip)->all_stats. fld ++; \
31 #define DEC_STAT(dip, fld) \
33 (dip)->stats. fld --; \
34 (dip)->all_stats. fld --; \
37 #define ADD_STAT(dip, fld, val) \
40 (dip)->stats. fld += __v; \
41 (dip)->all_stats. fld += __v; \
44 #define SUB_STAT(dip, fld, val) \
47 (dip)->stats. fld -= __v; \
48 (dip)->all_stats. fld -= __v; \
51 __u64 last_start, iostat_last_stamp;
52 __u64 iostat_interval = 1;
53 char *iostat_name = NULL;
54 FILE *iostat_ofp = NULL;
58 fprintf(iostat_ofp, "Device: rrqm/s wrqm/s r/s w/s "
59 "rsec/s wsec/s rkB/s wkB/s "
60 "avgrq-sz avgqu-sz await svctm %%util Stamp\n");
63 void im2d2c_func(struct io *c_iop, struct io *im_iop)
65 ADD_STAT(c_iop->dip, wait, c_iop->t.time - im_iop->t.time);
68 void iostat_init(void)
70 last_start = (__u64)-1;
75 void update_tot_qusz(struct d_info *dip, double now)
77 dip->stats.tot_qusz += ((now - dip->stats.last_qu_change) *
79 dip->all_stats.tot_qusz += ((now - dip->all_stats.last_qu_change) *
80 dip->all_stats.cur_qusz);
82 dip->stats.last_qu_change = dip->all_stats.last_qu_change = now;
85 void update_idle_time(struct d_info *dip, double now, int force)
87 if (dip->stats.cur_dev == 0 || force) {
88 dip->stats.idle_time += (now - dip->stats.last_dev_change);
89 dip->all_stats.idle_time +=
90 (now - dip->all_stats.last_dev_change);
92 dip->stats.last_dev_change = dip->all_stats.last_dev_change = now;
95 void __dump_stats(__u64 stamp, int all, struct d_info *dip)
99 double dt, nios, avgrq_sz, p_util, nrqm, await, svctm;
100 double now = TO_SEC(stamp);
103 dt = (double)stamp / 1.0e9;
104 sp = &dip->all_stats;
107 dt = (double)(stamp-last_start) / 1.0e9;
111 nios = (double)(sp->ios[0] + sp->ios[1]);
112 nrqm = (double)(sp->rqm[0] + sp->rqm[1]);
113 update_idle_time(dip, now, 1);
114 update_tot_qusz(dip, now);
117 avgrq_sz = (double)(sp->sec[0] + sp->sec[1]) / nios;
118 svctm = TO_MSEC(sp->svctm) / nios;
121 avgrq_sz = svctm = 0.0;
123 await = ((nios + nrqm) > 0.0) ? TO_MSEC(sp->wait) / (nios+nrqm) : 0.0;
124 p_util = (sp->idle_time <= dt) ? 100.0 * (1.0 - (sp->idle_time / dt)) :
128 * For AWAIT: nios should be the same as number of inserts
129 * and we add in nrqm (number of merges), which should give
130 * us the total number of IOs sent to the block IO layer.
132 fprintf(iostat_ofp, "%-11s ", make_dev_hdr(hdr, 11, dip));
133 fprintf(iostat_ofp, "%8.2lf ", (double)sp->rqm[1] / dt);
134 fprintf(iostat_ofp, "%8.2lf ", (double)sp->rqm[0] / dt);
135 fprintf(iostat_ofp, "%7.2lf ", (double)sp->ios[1] / dt);
136 fprintf(iostat_ofp, "%7.2lf ", (double)sp->ios[0] / dt);
137 fprintf(iostat_ofp, "%9.2lf ", (double)sp->sec[1] / dt);
138 fprintf(iostat_ofp, "%9.2lf ", (double)sp->sec[0] / dt);
139 fprintf(iostat_ofp, "%9.2lf ", (double)(sp->sec[1] / 2) / dt);
140 fprintf(iostat_ofp, "%9.2lf ", (double)(sp->sec[0] / 2) / dt);
141 fprintf(iostat_ofp, "%8.2lf ", avgrq_sz);
142 fprintf(iostat_ofp, "%8.2lf ", (double)sp->tot_qusz / dt);
143 fprintf(iostat_ofp, "%7.2lf ", await);
144 fprintf(iostat_ofp, "%7.2lf ", svctm);
145 fprintf(iostat_ofp, "%6.2lf", p_util);
147 fprintf(iostat_ofp, "%8s\n", "TOTAL");
149 fprintf(iostat_ofp, "%8.2lf\n", TO_SEC(stamp));
150 sp->rqm[0] = sp->rqm[1] = 0;
151 sp->ios[0] = sp->ios[1] = 0;
152 sp->sec[0] = sp->sec[1] = 0;
153 sp->wait = sp->svctm = 0;
155 sp->tot_qusz = sp->idle_time = 0.0;
159 void iostat_dump_stats(__u64 stamp, int all)
165 if (devices == NULL) {
168 __list_for_each(p, &all_devs) {
169 dip = list_entry(p, struct d_info, all_head);
170 __dump_stats(stamp, all, dip);
175 unsigned int mjr, mnr;
178 while (p && ((i = sscanf(p, "%u,%u", &mjr, &mnr)) == 2)) {
179 dip = __dip_find((__u32)((mjr << MINORBITS) | mnr));
180 __dump_stats(stamp, all, dip);
186 if (!all && iostat_ofp)
187 fprintf(iostat_ofp, "\n");
190 void iostat_check_time(__u64 stamp)
193 if (last_start == (__u64)-1)
195 else if ((stamp - last_start) >= iostat_interval) {
196 iostat_dump_stats(stamp, 0);
200 iostat_last_stamp = stamp;
204 void iostat_insert(struct io *iop)
206 update_tot_qusz(iop->dip, TO_SEC(iop->t.time));
207 INC_STAT(iop->dip, cur_qusz);
210 void iostat_merge(struct io *iop)
212 INC_STAT(iop->dip, rqm[IOP_RW(iop)]);
215 void iostat_issue(struct io *iop)
217 int rw = IOP_RW(iop);
218 struct d_info *dip = iop->dip;
219 double now = TO_SEC(iop->t.time);
221 INC_STAT(dip, ios[rw]);
222 ADD_STAT(dip, sec[rw], iop->t.bytes >> 9);
224 update_idle_time(dip, now, 0);
225 INC_STAT(dip, cur_dev);
228 void iostat_unissue(struct io *iop)
230 int rw = IOP_RW(iop);
231 struct d_info *dip = iop->dip;
233 DEC_STAT(dip, ios[rw]);
234 SUB_STAT(dip, sec[rw], iop->t.bytes >> 9);
235 DEC_STAT(dip, cur_dev);
238 void iostat_complete(struct io *d_iop, struct io *c_iop)
240 double now = TO_SEC(c_iop->t.time);
241 struct d_info *dip = d_iop->dip;
243 dip_foreach(d_iop, IOP_I, im2d2c_func, 0);
244 dip_foreach(d_iop, IOP_M, im2d2c_func, 0);
246 update_tot_qusz(dip, now);
247 DEC_STAT(dip, cur_qusz);
249 update_idle_time(dip, now, 0);
250 DEC_STAT(dip, cur_dev);
252 ADD_STAT(dip, svctm, c_iop->t.time - d_iop->t.time);