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
24 typedef struct avg_info *ai_dip_t;
25 ai_dip_t dip_q2q_avg(struct d_info *dip) { return &dip->avgs.q2q; }
26 ai_dip_t dip_q2c_avg(struct d_info *dip) { return &dip->avgs.q2c; }
27 ai_dip_t dip_q2a_avg(struct d_info *dip) { return &dip->avgs.q2a; }
28 ai_dip_t dip_q2i_avg(struct d_info *dip) { return &dip->avgs.q2i; }
29 ai_dip_t dip_i2d_avg(struct d_info *dip) { return &dip->avgs.i2d; }
30 ai_dip_t dip_d2c_avg(struct d_info *dip) { return &dip->avgs.d2c; }
32 typedef struct avg_info *ai_pip_t;
33 ai_pip_t pip_q2q_avg(struct p_info *pip) { return &pip->avgs.q2q; }
34 ai_pip_t pip_q2c_avg(struct p_info *pip) { return &pip->avgs.q2c; }
35 ai_pip_t pip_q2a_avg(struct p_info *pip) { return &pip->avgs.q2a; }
36 ai_pip_t pip_q2i_avg(struct p_info *pip) { return &pip->avgs.q2i; }
37 ai_pip_t pip_i2d_avg(struct p_info *pip) { return &pip->avgs.i2d; }
38 ai_pip_t pip_d2c_avg(struct p_info *pip) { return &pip->avgs.d2c; }
40 void output_section_hdr(FILE *ofp, char *hdr)
42 fprintf(ofp, "==================== ");
44 fprintf(ofp, " ====================\n\n");
47 void output_hdr(FILE *ofp, char *hdr)
49 fprintf(ofp, "%15s %13s %13s %13s %11s\n",
50 hdr, "MIN", "AVG", "MAX", "N" );
51 fprintf(ofp, "--------------- ------------- ------------- ------------- -----------\n");
54 void __output_avg(FILE *ofp, char *hdr, struct avg_info *ap)
57 ap->avg = BIT_TIME(ap->total) / (double)ap->n;
58 fprintf(ofp, "%-15s %13.9f %13.9f %13.9f %11d\n", hdr,
59 BIT_TIME(ap->min), ap->avg, BIT_TIME(ap->max), ap->n);
63 void output_hdr2(FILE *ofp, char*hdr)
65 fprintf(ofp, "%15s %13s %13s %13s %13s %13s %13s\n", hdr, "Q2Q", "Q2A", "Q2I", "I2D", "D2C", "Q2C");
66 fprintf(ofp, "--------------- ------------- ------------- ------------- ------------- ------------- -------------\n");
69 static inline char *avg2string(struct avg_info *ap, char *string)
72 sprintf(string, "%13.9f", ap->avg);
78 void __output_avg2(FILE *ofp, char *hdr, struct avgs_info *ap)
80 char c1[16], c2[16], c3[16], c4[16], c5[16], c6[16];
82 if (ap->q2q.n > 0 || ap->q2a.n > 0 || ap->q2i.n > 0 ||
83 ap->i2d.n > 0 || ap->d2c.n > 0 || ap->q2c.n > 0) {
84 fprintf(ofp, "%-15s %13s %13s %13s %13s %13s %13s\n", hdr,
85 avg2string(&ap->q2q,c1), avg2string(&ap->q2a,c2),
86 avg2string(&ap->q2i,c3), avg2string(&ap->i2d,c4),
87 avg2string(&ap->d2c,c5), avg2string(&ap->q2c,c6));
91 void __pip_output_avg2(struct p_info *pip, void *arg)
93 __output_avg2((FILE *)arg, pip->name, &pip->avgs);
96 void __dip_output_avg2(struct d_info *dip, void *arg)
99 __output_avg2((FILE *)arg, make_dev_hdr(dev_info, 15, dip), &dip->avgs);
102 char *make_dev_hdr(char *pad, size_t len, struct d_info *dip)
104 if (dip->map == NULL)
105 snprintf(pad, len, "(%3d,%3d)",
106 MAJOR(dip->device), MINOR(dip->device));
108 snprintf(pad, len, "%s", dip->map->device);
115 ai_dip_t (*func)(struct d_info *);
117 void __output_dip_avg(struct d_info *dip, void *arg)
119 struct __oda *odap = arg;
120 ai_dip_t ap = odap->func(dip);
123 ap->avg = BIT_TIME(ap->total) / (double)ap->n;
124 __output_avg(odap->ofp, make_dev_hdr(dev_info, 15, dip), ap);
128 void output_dip_avg(FILE *ofp, char *hdr, ai_dip_t (*func)(struct d_info *))
130 struct __oda oda = { .ofp = ofp, .func = func};
131 output_hdr(ofp, hdr);
132 dip_foreach_out(__output_dip_avg, &oda);
138 unsigned long long nq, nd, blkmin, blkmax, total;
140 void __output_dip_merge_ratio(struct d_info *dip, void *arg)
144 double ratio, q2c_n = dip->avgs.q2c.n, d2c_n = dip->n_ds;
146 if (q2c_n > 0.0 && d2c_n > 0.0) {
147 ratio = q2c_n / d2c_n;
148 blks_avg = (double)dip->avgs.blks.total / d2c_n;
150 "%10s | %8llu %8llu %7.1lf | %8llu %8llu %8llu %8llu\n",
151 make_dev_hdr(scratch, 15, dip),
152 (unsigned long long)dip->avgs.q2c.n,
153 (unsigned long long)dip->n_ds,
155 (unsigned long long)dip->avgs.blks.min,
156 (unsigned long long)blks_avg,
157 (unsigned long long)dip->avgs.blks.max,
158 (unsigned long long)dip->avgs.blks.total);
160 if (n_merges++ == 0) {
161 merge_data.blkmin = dip->avgs.blks.min;
162 merge_data.blkmax = dip->avgs.blks.max;
165 merge_data.nq += dip->avgs.q2c.n;
166 merge_data.nd += dip->n_ds;
167 merge_data.total += dip->avgs.blks.total;
168 if (dip->avgs.blks.min < merge_data.blkmin)
169 merge_data.blkmin = dip->avgs.blks.min;
170 if (dip->avgs.blks.max > merge_data.blkmax)
171 merge_data.blkmax = dip->avgs.blks.max;
175 void output_dip_merge_ratio(FILE *ofp)
177 fprintf(ofp, "%10s | %8s %8s %7s | %8s %8s %8s %8s\n", "DEV", "#Q", "#D", "Ratio", "BLKmin", "BLKavg", "BLKmax", "Total");
178 fprintf(ofp, "---------- | -------- -------- ------- | -------- -------- -------- --------\n");
179 dip_foreach_out(__output_dip_merge_ratio, ofp);
181 fprintf(ofp, "---------- | -------- -------- ------- | -------- -------- -------- --------\n");
182 fprintf(ofp, "%10s | %8s %8s %7s | %8s %8s %8s %8s\n", "DEV", "#Q", "#D", "Ratio", "BLKmin", "BLKavg", "BLKmax", "Total");
184 "%10s | %8llu %8llu %7.1lf | %8llu %8llu %8llu %8llu\n",
185 "TOTAL", merge_data.nq, merge_data.nd,
186 (float)merge_data.nq / (float)merge_data.nd,
188 merge_data.total / merge_data.nd,
189 merge_data.blkmax, merge_data.total);
194 #define AVG(a,b) (100.0 * ((double)(a) / (double)(b)))
195 #define CALC_AVG(ap) (ap)->avg = ((ap)->n == 0 ? 0.0 : \
196 (BIT_TIME((ap)->total) / \
198 char *q2i_v_q2C(struct d_info *dip, char *s)
202 if (dip->avgs.q2i.n == 0) return " ";
204 q2c = dip->avgs.q2i.avg + dip->avgs.i2d.avg + dip->avgs.d2c.avg;
205 sprintf(s, "%5.1lf%%", AVG(dip->avgs.q2i.avg, q2c));
210 char *i2d_v_q2C(struct d_info *dip, char *s)
214 if (dip->avgs.d2c.n == 0) return " ";
216 q2c = dip->avgs.q2i.avg + dip->avgs.i2d.avg + dip->avgs.d2c.avg;
217 sprintf(s, "%5.1lf%%", AVG(dip->avgs.i2d.avg, q2c));
222 char *d2c_v_q2C(struct d_info *dip, char *s)
226 if (dip->avgs.d2c.n == 0) return " ";
228 q2c = dip->avgs.q2i.avg + dip->avgs.i2d.avg + dip->avgs.d2c.avg;
229 sprintf(s, "%5.1lf%%", AVG(dip->avgs.d2c.avg, q2c));
234 void __output_dip_prep_ohead(struct d_info *dip, void *arg)
237 char s1[16], s2[16], s3[16];
239 if ((dip->avgs.q2i.n > 0 && dip->avgs.i2d.n > 0 &&
240 dip->avgs.d2c.n > 0)) {
241 CALC_AVG(&dip->avgs.q2i);
242 CALC_AVG(&dip->avgs.i2d);
243 CALC_AVG(&dip->avgs.d2c);
245 fprintf((FILE *)arg, "%10s | %6s %6s %6s\n",
246 make_dev_hdr(dev_info, 15, dip),
247 q2i_v_q2C(dip, s1), i2d_v_q2C(dip, s2),
252 void output_dip_prep_ohead(FILE *ofp)
254 fprintf(ofp, "%10s | %6s %6s %6s\n", "DEV", "Q2I", "I2D", "D2C");
255 fprintf(ofp, "---------- | ------ ------ ------\n");
256 dip_foreach_out(__output_dip_prep_ohead, ofp);
261 struct seek_mode_info {
262 struct seek_mode_info *next;
267 long long nseeks, median;
269 struct seek_mode_info *head;
276 void output_seek_mode_info(FILE *ofp, struct o_seek_info *sip)
278 struct seek_mode_info *p, *this, *new_list = NULL;
280 ASSERT(sip->head != NULL);
281 while ((this = sip->head) != NULL) {
282 sip->head = this->next;
283 if (new_list == NULL) {
288 if (this->nseeks < new_list->nseeks) {
292 if (this->nseeks > new_list->nseeks) {
293 while ((p = new_list) != NULL) {
301 for (p = new_list; p; p++)
302 if (p->mode == this->mode) {
303 this->nseeks += p->nseeks;
311 fprintf(ofp, "%10s | %15lld %15.1lf %15lld | %lld(%d)",
312 "Average", sip->nseeks, sip->mean / sip->nseeks,
313 sip->median / sip->nseeks, new_list->mode, new_list->nseeks);
318 while((p = new_list) != NULL) {
319 fprintf(ofp, " %lld", p->mode);
324 void add_seek_mode_info(struct o_seek_info *sip, struct mode *mp)
327 long long *lp = mp->modes;
328 struct seek_mode_info *smip;
331 for (i = 0; i < mp->nmds; i++) {
332 for (smip = sip->head; smip; smip = smip->next) {
333 if (smip->mode == lp[i]) {
334 smip->nseeks += mp->most_seeks;
339 struct seek_mode_info *new = malloc(sizeof(*new));
341 new->next = sip->head;
344 new->nseeks = mp->most_seeks;
349 void __output_dip_seek_info(struct d_info *dip, void *arg)
359 nseeks = seeki_nseeks(dip->seek_handle);
361 mean = seeki_mean(dip->seek_handle);
362 median = seeki_median(dip->seek_handle);
363 nmodes = seeki_mode(dip->seek_handle, &m);
365 fprintf(ofp, "%10s | %15lld %15.1lf %15lld | %lld(%d)",
366 make_dev_hdr(dev_info, 15, dip), nseeks, mean, median,
367 nmodes > 0 ? m.modes[0] : 0, m.most_seeks);
368 for (i = 1; i < nmodes; i++)
369 fprintf(ofp, " %lld", m.modes[i]);
372 seek_info.nseeks += nseeks;
373 seek_info.mean += (nseeks * mean);
374 seek_info.median += (nseeks * median);
375 add_seek_mode_info(&seek_info, &m);
379 void output_dip_seek_info(FILE *ofp)
381 fprintf(ofp, "%10s | %15s %15s %15s | %-15s\n", "DEV", "NSEEKS",
382 "MEAN", "MEDIAN", "MODE");
383 fprintf(ofp, "---------- | --------------- --------------- --------------- | ---------------\n");
384 dip_foreach_out(__output_dip_seek_info, ofp);
386 fprintf(ofp, "---------- | --------------- --------------- --------------- | ---------------\n");
387 fprintf(ofp, "%10s | %15s %15s %15s | %-15s\n",
388 "Overall", "NSEEKS", "MEAN", "MEDIAN", "MODE");
389 output_seek_mode_info(ofp, &seek_info);
397 ai_pip_t (*func)(struct p_info *);
400 void __output_pip_avg(struct p_info *pip, void *arg)
402 struct __opa *opap = arg;
403 ai_pip_t ap = opap->func(pip);
407 snprintf(proc_name, 15, pip->name);
409 ap->avg = BIT_TIME(ap->total) / (double)ap->n;
410 __output_avg(opap->ofp, proc_name, ap);
414 void output_pip_avg(FILE *ofp, char *hdr, ai_pip_t (*func)(struct p_info *))
416 struct __opa opa = { .ofp = ofp, .func = func };
418 output_hdr(ofp, hdr);
419 pip_foreach_out(__output_pip_avg, &opa);
423 void output_dip_avgs(FILE *ofp)
425 output_hdr2(ofp,"Dev");
426 dip_foreach_out(__dip_output_avg2, ofp);
430 void output_pip_avgs(FILE *ofp)
432 output_hdr2(ofp,"Exe");
433 pip_foreach_out(__pip_output_avg2, ofp);
439 long n_plugs, n_timer_unplugs;
443 void __dip_output_plug(struct d_info *dip, void *arg)
449 if (dip->nplugs > 0) {
450 if (dip->is_plugged) dip_unplug(dip->device, dip->end_time, 0);
451 delta = dip->end_time - dip->start_time;
452 pct = 100.0 * ((dip->plugged_time / delta) / delta);
454 fprintf(ofp, "%10s | %10d(%10d) | %13.9lf%%\n",
455 make_dev_hdr(dev_info, 15, dip),
456 dip->nplugs, dip->n_timer_unplugs, pct);
459 plug_info.n_plugs += dip->nplugs;
460 plug_info.n_timer_unplugs += dip->n_timer_unplugs;
461 plug_info.t_percent += pct;
465 void __dip_output_plug_all(FILE *ofp, struct plug_info *p)
467 fprintf(ofp, "---------- | ---------- ---------- | ----------------\n");
468 fprintf(ofp, "%10s | %10s %10s | %s\n",
469 "DEV", "# Plugs", "# Timer Us", "% Time Q Plugged");
470 fprintf(ofp, "%10s | %10ld(%10ld) | %13.9lf%%\n", "OVERALL",
471 p->n_plugs / n_plugs, p->n_timer_unplugs / n_plugs,
472 p->t_percent / n_plugs);
476 void output_plug_info(FILE *ofp)
478 fprintf(ofp, "%10s | %10s %10s | %s\n",
479 "DEV", "# Plugs", "# Timer Us", "% Time Q Plugged");
480 fprintf(ofp, "---------- | ---------- ---------- | ----------------\n");
481 dip_foreach_out(__dip_output_plug, ofp);
483 __dip_output_plug_all(ofp, &plug_info);
487 void output_histos(void)
493 if (output_name == NULL) return;
495 sprintf(fname, "%s_qhist.dat", output_name);
496 ofp = fopen(fname, "w");
502 fprintf(ofp, "# BTT histogram data\n");
503 fprintf(ofp, "# Q buckets\n");
504 for (i = 0; i < (N_HIST_BKTS-1); i++)
505 fprintf(ofp, "%4d %lld\n", (i+1), (long long)q_histo[i]);
506 fprintf(ofp, "\n# Q bucket for > %d\n%4d %lld\n", (int)N_HIST_BKTS-1,
507 N_HIST_BKTS-1, q_histo[N_HIST_BKTS-1]);
510 sprintf(fname, "%s_dhist.dat", output_name);
511 ofp = fopen(fname, "w");
516 fprintf(ofp, "# D buckets\n");
517 for (i = 0; i < (N_HIST_BKTS-1); i++)
518 fprintf(ofp, "%4d %lld\n", (i+1), (long long)d_histo[i]);
519 fprintf(ofp, "\n# D bucket for > %d\n%4d %lld\n", (int)N_HIST_BKTS-1,
520 N_HIST_BKTS-1, d_histo[N_HIST_BKTS-1]);
524 int output_avgs(FILE *ofp)
526 if (output_all_data) {
527 if (exes == NULL || *exes != '\0') {
528 output_section_hdr(ofp, "Per Process");
529 output_pip_avg(ofp, "Q2Q", pip_q2q_avg);
530 output_pip_avg(ofp, "Q2A", pip_q2a_avg);
531 output_pip_avg(ofp, "Q2I", pip_q2i_avg);
532 output_pip_avg(ofp, "I2D", pip_i2d_avg);
533 output_pip_avg(ofp, "D2C", pip_d2c_avg);
534 output_pip_avg(ofp, "Q2C", pip_q2c_avg);
537 output_section_hdr(ofp, "Per Device");
538 output_dip_avg(ofp, "Q2Q", dip_q2q_avg);
539 output_dip_avg(ofp, "Q2A", dip_q2a_avg);
540 output_dip_avg(ofp, "Q2I", dip_q2i_avg);
541 output_dip_avg(ofp, "I2D", dip_i2d_avg);
542 output_dip_avg(ofp, "D2C", dip_d2c_avg);
543 output_dip_avg(ofp, "Q2C", dip_q2c_avg);
546 output_section_hdr(ofp, "All Devices");
547 output_hdr(ofp, "ALL");
548 __output_avg(ofp, "Q2Q", &all_avgs.q2q);
549 __output_avg(ofp, "Q2A", &all_avgs.q2a);
550 __output_avg(ofp, "Q2I", &all_avgs.q2i);
551 __output_avg(ofp, "I2D", &all_avgs.i2d);
552 __output_avg(ofp, "D2C", &all_avgs.d2c);
553 __output_avg(ofp, "Q2C", &all_avgs.q2c);
556 output_section_hdr(ofp, "Device Overhead");
557 output_dip_prep_ohead(ofp);
559 if (output_all_data) {
560 if (exes == NULL || *exes != '\0') {
561 output_section_hdr(ofp, "Per Process (avgs)");
562 output_pip_avgs(ofp);
565 output_section_hdr(ofp, "Per Device (avgs)");
566 output_dip_avgs(ofp);
569 output_section_hdr(ofp, "Device Merge Information");
570 output_dip_merge_ratio(ofp);
572 output_section_hdr(ofp, "Device Seek Information");
573 output_dip_seek_info(ofp);
575 output_section_hdr(ofp, "Plug Information");
576 output_plug_info(ofp);
583 void __output_ranges(FILE *ofp, struct list_head *head_p, float base)
585 struct range_info *rip;
587 float limit = base + 0.4;
589 __list_for_each(p, head_p) {
590 rip = list_entry(p, struct range_info, head);
591 fprintf(ofp, "%13.9lf %5.1f\n", BIT_TIME(rip->start), base);
592 fprintf(ofp, "%13.9lf %5.1f\n", BIT_TIME(rip->start), limit);
593 fprintf(ofp, "%13.9lf %5.1f\n", BIT_TIME(rip->end), limit);
594 fprintf(ofp, "%13.9lf %5.1f\n", BIT_TIME(rip->end), base);
598 int output_regions(FILE *ofp, char *header, struct region_info *reg,
601 if (reg->qr_cur != NULL)
602 list_add_tail(®->qr_cur->head, ®->qranges);
603 if (reg->cr_cur != NULL)
604 list_add_tail(®->cr_cur->head, ®->cranges);
606 if (list_len(®->qranges) == 0 && list_len(®->cranges) == 0)
609 fprintf(ofp, "# %16s : q activity\n", header);
610 __output_ranges(ofp, ®->qranges, base);
613 fprintf(ofp, "# %16s : c activity\n", header);
614 __output_ranges(ofp, ®->cranges, base + 0.5);
624 void __output_dev(struct d_info *dip, void *arg)
627 struct __od *odp = arg;
629 sprintf(header, "%d,%d", MAJOR(dip->device), MINOR(dip->device));
630 if (output_regions(odp->ofp, header, &dip->regions, odp->base))
634 float output_devs(FILE *ofp, float base)
636 struct __od od = { .ofp = ofp, .base = base };
638 fprintf(ofp, "# Per device\n" );
639 dip_foreach_out(__output_dev, &od);
643 static inline int exe_match(char *exe, char *name)
645 return (exe == NULL) || (strstr(name, exe) != NULL);
652 void __output_procs(struct p_info *pip, void *arg)
654 struct __op *opp = arg;
655 output_regions(opp->ofp, pip->name, &pip->regions, opp->base);
659 float output_procs(FILE *ofp, float base)
661 struct __op op = { .ofp = ofp, .base = base };
663 fprintf(ofp, "# Per process\n" );
664 pip_foreach_out(__output_procs, &op);
668 int output_ranges(FILE *ofp)
672 fprintf(ofp, "# %s\n", "Total System");
673 if (output_regions(ofp, "Total System", &all_regions, base))
677 base = output_devs(ofp, base);
679 base = output_procs(ofp, base);