UNPLUG does the timing stuff, UNPLUG TIMEOUT only does timeout
[blktrace.git] / btt / output.c
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 "globals.h"
23
24 typedef struct avg_info *ai_dip_t;
25 ai_dip_t dip_q2q_dm_avg(struct d_info *dip) { return &dip->avgs.q2q_dm; }
26 ai_dip_t dip_q2a_dm_avg(struct d_info *dip) { return &dip->avgs.q2a_dm; }
27 ai_dip_t dip_q2c_dm_avg(struct d_info *dip) { return &dip->avgs.q2c_dm; }
28
29 ai_dip_t dip_q2q_avg(struct d_info *dip) { return &dip->avgs.q2q; }
30 ai_dip_t dip_q2c_avg(struct d_info *dip) { return &dip->avgs.q2c; }
31 ai_dip_t dip_q2a_avg(struct d_info *dip) { return &dip->avgs.q2a; }
32 ai_dip_t dip_q2g_avg(struct d_info *dip) { return &dip->avgs.q2g; }
33 ai_dip_t dip_g2i_avg(struct d_info *dip) { return &dip->avgs.g2i; }
34 ai_dip_t dip_q2m_avg(struct d_info *dip) { return &dip->avgs.q2m; }
35 ai_dip_t dip_i2d_avg(struct d_info *dip) { return &dip->avgs.i2d; }
36 ai_dip_t dip_d2c_avg(struct d_info *dip) { return &dip->avgs.d2c; }
37
38 typedef struct avg_info *ai_pip_t;
39 ai_pip_t pip_q2q_dm_avg(struct p_info *pip) { return &pip->avgs.q2q_dm; }
40 ai_pip_t pip_q2a_dm_avg(struct p_info *pip) { return &pip->avgs.q2a_dm; }
41 ai_pip_t pip_q2c_dm_avg(struct p_info *pip) { return &pip->avgs.q2c_dm; }
42
43 ai_pip_t pip_q2q_avg(struct p_info *pip) { return &pip->avgs.q2q; }
44 ai_pip_t pip_q2c_avg(struct p_info *pip) { return &pip->avgs.q2c; }
45 ai_pip_t pip_q2a_avg(struct p_info *pip) { return &pip->avgs.q2a; }
46 ai_pip_t pip_q2g_avg(struct p_info *pip) { return &pip->avgs.q2g; }
47 ai_pip_t pip_g2i_avg(struct p_info *pip) { return &pip->avgs.g2i; }
48 ai_pip_t pip_q2m_avg(struct p_info *pip) { return &pip->avgs.q2m; }
49 ai_pip_t pip_i2d_avg(struct p_info *pip) { return &pip->avgs.i2d; }
50 ai_pip_t pip_d2c_avg(struct p_info *pip) { return &pip->avgs.d2c; }
51
52 void output_section_hdr(FILE *ofp, char *hdr)
53 {
54         fprintf(ofp, "==================== ");
55         fprintf(ofp, hdr);
56         fprintf(ofp, " ====================\n\n");
57 }
58
59 void output_hdr(FILE *ofp, char *hdr)
60 {
61         fprintf(ofp, "%15s %13s %13s %13s %11s\n",
62                 hdr, "MIN", "AVG", "MAX", "N" );
63         fprintf(ofp, "--------------- ------------- ------------- ------------- -----------\n");
64 }
65
66 void __output_avg(FILE *ofp, char *hdr, struct avg_info *ap)
67 {
68         if (ap->n > 0) {
69                 ap->avg = BIT_TIME(ap->total) / (double)ap->n;
70                 fprintf(ofp, "%-15s %13.9f %13.9f %13.9f %11d\n", hdr,
71                         BIT_TIME(ap->min), ap->avg, BIT_TIME(ap->max), ap->n);
72         }
73 }
74
75 static inline char *avg2string(struct avg_info *ap, char *string)
76 {
77         if (ap->n > 0)
78                 sprintf(string, "%13.9f", ap->avg);
79         else
80                 sprintf(string, " ");
81         return string;
82 }
83
84 char *make_dev_hdr(char *pad, size_t len, struct d_info *dip)
85 {
86         if (dip->map == NULL)
87                 snprintf(pad, len, "(%3d,%3d)", 
88                          MAJOR(dip->device), MINOR(dip->device));
89         else
90                 snprintf(pad, len, "%s", dip->map->device);
91
92         return pad;
93 }
94
95 struct __oda {
96         FILE *ofp;
97         ai_dip_t (*func)(struct d_info *);
98 };
99 void __output_dip_avg(struct d_info *dip, void *arg)
100 {
101         struct __oda *odap = arg;
102         ai_dip_t ap = odap->func(dip);
103         if (ap->n > 0) {
104                 char dev_info[15];
105                 ap->avg = BIT_TIME(ap->total) / (double)ap->n;
106                 __output_avg(odap->ofp, make_dev_hdr(dev_info, 15, dip), ap);
107         }
108 }
109
110 void output_dip_avg(FILE *ofp, char *hdr, ai_dip_t (*func)(struct d_info *))
111 {
112         struct __oda oda = { .ofp = ofp, .func = func};
113         output_hdr(ofp, hdr);
114         dip_foreach_out(__output_dip_avg, &oda);
115         fprintf(ofp, "\n");
116 }
117
118 struct __q2d {
119         FILE *ofp;
120         void *q2d_all;
121         int n;
122 };
123 void __output_q2d_histo(struct d_info *dip, void *arg)
124 {
125         struct __q2d *q2dp = arg;
126
127         if (q2d_ok(dip->q2d_priv)) {
128                 char scratch[15];
129                 FILE *ofp = q2dp->ofp;
130
131                 fprintf(q2dp->ofp, "%10s | ", make_dev_hdr(scratch, 15, dip));
132                 q2d_display(ofp, dip->q2d_priv);
133                 q2d_acc(q2dp->q2d_all, dip->q2d_priv);
134                 q2dp->n++;
135         }
136 }
137
138 void output_q2d_histo(FILE *ofp)
139 {
140         struct __q2d __q2d = {
141                 .ofp = ofp,
142                 .q2d_all = q2d_init(),
143                 .n = 0
144         };
145
146         fprintf(ofp, "%10s | ", "DEV");
147         q2d_display_header(ofp);
148         fprintf(ofp, "--------- | ");
149         q2d_display_dashes(ofp);
150         dip_foreach_out(__output_q2d_histo, &__q2d);
151
152         if (__q2d.n) {
153                 fprintf(ofp, "========== | ");
154                 q2d_display_dashes(ofp);
155                 fprintf(ofp, "%10s | ", "AVG");
156                 q2d_display(ofp, __q2d.q2d_all);
157                 fprintf(ofp, "\n");
158         }
159 }
160
161 int n_merges = 0;
162 struct {
163         unsigned long long nq, nd, blkmin, blkmax, total;
164 } merge_data;
165 void __output_dip_merge_ratio(struct d_info *dip, void *arg)
166 {
167         double blks_avg;
168         char scratch[15];
169         double ratio, q2c_n, d2c_n;
170
171         if (dip->n_qs == 0 || dip->n_ds == 0)
172                 return;
173         else if (dip->n_qs < dip->n_ds)
174                 dip->n_qs = dip->n_ds;
175
176         q2c_n = dip->n_qs;
177         d2c_n = dip->n_ds;
178         if (q2c_n > 0.0 && d2c_n > 0.0) {
179                 if (q2c_n < d2c_n)
180                         ratio = 1.0;
181                 else
182                         ratio = q2c_n / d2c_n;
183                 blks_avg = (double)dip->avgs.blks.total / d2c_n;
184                 fprintf((FILE *)arg, 
185                         "%10s | %8llu %8llu %7.1lf | %8llu %8llu %8llu %8llu\n",
186                         make_dev_hdr(scratch, 15, dip),
187                         (unsigned long long)dip->n_qs,
188                         (unsigned long long)dip->n_ds,
189                         ratio,
190                         (unsigned long long)dip->avgs.blks.min,
191                         (unsigned long long)blks_avg,
192                         (unsigned long long)dip->avgs.blks.max,
193                         (unsigned long long)dip->avgs.blks.total);
194
195                 if (n_merges++ == 0) {
196                         merge_data.blkmin = dip->avgs.blks.min;
197                         merge_data.blkmax = dip->avgs.blks.max;
198                 }
199
200                 merge_data.nq += dip->n_qs;
201                 merge_data.nd += dip->n_ds;
202                 merge_data.total += dip->avgs.blks.total;
203                 if (dip->avgs.blks.min < merge_data.blkmin)
204                         merge_data.blkmin = dip->avgs.blks.min;
205                 if (dip->avgs.blks.max > merge_data.blkmax)
206                         merge_data.blkmax = dip->avgs.blks.max;
207         }
208 }
209
210 void output_dip_merge_ratio(FILE *ofp)
211 {
212         fprintf(ofp, "%10s | %8s %8s %7s | %8s %8s %8s %8s\n", "DEV", "#Q", "#D", "Ratio", "BLKmin", "BLKavg", "BLKmax", "Total");
213         fprintf(ofp, "---------- | -------- -------- ------- | -------- -------- -------- --------\n");
214         dip_foreach_out(__output_dip_merge_ratio, ofp);
215         if (n_merges > 1) {
216                 fprintf(ofp, "---------- | -------- -------- ------- | -------- -------- -------- --------\n");
217                 fprintf(ofp, "%10s | %8s %8s %7s | %8s %8s %8s %8s\n", "DEV", "#Q", "#D", "Ratio", "BLKmin", "BLKavg", "BLKmax", "Total");
218                 fprintf((FILE *)ofp, 
219                         "%10s | %8llu %8llu %7.1lf | %8llu %8llu %8llu %8llu\n",
220                         "TOTAL", merge_data.nq, merge_data.nd, 
221                         (float)merge_data.nq / (float)merge_data.nd,
222                         merge_data.blkmin, 
223                         merge_data.total / merge_data.nd,
224                         merge_data.blkmax, merge_data.total);
225         }
226         fprintf(ofp, "\n");
227 }
228
229 struct __ohead_data {
230         __u64 total;
231         int n;
232 };
233
234 struct ohead_data {
235         FILE *ofp;
236         struct __ohead_data q2g, g2i, q2m, i2d, d2c;
237 };
238
239 #define AVG(a,b) (100.0 * ((double)(a) / (double)(b)))
240 #define CALC_AVG(ap) (ap)->avg = ((ap)->n == 0 ? 0.0 :                    \
241                                                  (BIT_TIME((ap)->total) / \
242                                                         (double)(ap)->n))
243
244 #define x_v_q2C(fld, dip, s, odp)                                       \
245         double q2c;                                                     \
246         if (dip->avgs. fld .n == 0) return " ";                         \
247         q2c = dip->avgs.g2i.avg + dip->avgs.g2i.avg +                   \
248                                 dip->avgs.i2d.avg + dip->avgs.d2c.avg;  \
249         sprintf(s, "%8.4lf%%", AVG(dip->avgs. fld .avg, q2c));          \
250         odp-> fld .n += dip->avgs. fld .n;                              \
251         odp-> fld .total += dip->avgs. fld .total;                      \
252         return s;
253
254 char *q2g_v_q2C(struct d_info *dip, char *s, struct ohead_data *odp)
255 {
256         x_v_q2C(q2g, dip, s, odp);
257 }
258
259 char *g2i_v_q2C(struct d_info *dip, char *s, struct ohead_data *odp)
260 {
261         x_v_q2C(g2i, dip, s, odp);
262 }
263
264 char *q2m_v_q2C(struct d_info *dip, char *s, struct ohead_data *odp)
265 {
266         x_v_q2C(q2m, dip, s, odp);
267 }
268
269 char *i2d_v_q2C(struct d_info *dip, char *s, struct ohead_data *odp)
270 {
271         x_v_q2C(i2d, dip, s, odp);
272 }
273
274 char *d2c_v_q2C(struct d_info *dip, char *s, struct ohead_data *odp)
275 {
276         x_v_q2C(d2c, dip, s, odp);
277 }
278
279 void __output_dip_prep_ohead(struct d_info *dip, void *arg)
280 {
281         char dev_info[15];
282         char s1[16], s2[16], s3[16], s4[16], s5[16];
283         struct ohead_data *odp = arg;
284
285         if (dip->avgs.q2g.n > 0 && dip->avgs.g2i.n > 0 && 
286                                    dip->avgs.i2d.n > 0 && dip->avgs.d2c.n > 0) {
287                 CALC_AVG(&dip->avgs.q2g);
288                 CALC_AVG(&dip->avgs.g2i);
289                 CALC_AVG(&dip->avgs.q2m);
290                 CALC_AVG(&dip->avgs.i2d);
291                 CALC_AVG(&dip->avgs.d2c);
292
293                 fprintf(odp->ofp, "%10s | %9s %9s %9s %9s %9s\n",
294                         make_dev_hdr(dev_info, 15, dip),
295                         q2g_v_q2C(dip, s1, odp), 
296                         g2i_v_q2C(dip, s2, odp), 
297                         q2m_v_q2C(dip, s3, odp),
298                         i2d_v_q2C(dip, s4, odp),
299                         d2c_v_q2C(dip, s5, odp));
300         }
301 }
302
303 #define OD_AVG(od, fld, q2c)                                            \
304         (od. fld .n == 0) ? (double)0.0 :                               \
305                 (100.0 * ((double)((od). fld . total) / q2c))
306
307 void output_dip_prep_ohead(FILE *ofp)
308 {
309         double q2c;
310         struct ohead_data od;
311
312         memset(&od, 0, sizeof(od));
313         od.ofp = ofp;
314
315         fprintf(ofp, "%10s | %9s %9s %9s %9s %9s\n", 
316                                 "DEV", "Q2G", "G2I", "Q2M", "I2D", "D2C");
317         fprintf(ofp, "---------- | --------- --------- --------- --------- ---------\n");
318         dip_foreach_out(__output_dip_prep_ohead, &od);
319
320         if (od.q2g.n == 0 && od.g2i.n == 0 && od.q2m.n == 0 && 
321                                                 od.i2d.n == 0 && od.d2c.n == 0)
322                 goto out;
323
324         q2c = od.q2g.total + od.g2i.total + od.q2m.total +      
325                                                 od.i2d.total + od.d2c.total;
326         fprintf(ofp, "---------- | --------- --------- --------- --------- ---------\n");
327         fprintf(ofp, "%10s | %8.4lf%% %8.4lf%% %8.4lf%% %8.4lf%% %8.4lf%%\n", "Overall", 
328                         OD_AVG(od, q2g, q2c), OD_AVG(od, g2i, q2c), 
329                         OD_AVG(od, q2m, q2c), OD_AVG(od, i2d, q2c), 
330                         OD_AVG(od, d2c, q2c));
331
332 out:
333         fprintf(ofp, "\n");
334 }
335
336 struct seek_mode_info {
337         struct seek_mode_info *next;
338         long long mode;
339         int nseeks;
340 };
341 struct o_seek_info {
342         long long nseeks, median;
343         double mean;
344         struct seek_mode_info *head;
345 } seek_info;
346 int n_seeks;
347
348 void output_seek_mode_info(FILE *ofp, struct o_seek_info *sip)
349 {
350         struct seek_mode_info *p, *this, *new_list = NULL;
351
352         ASSERT(sip->head != NULL);
353         while ((this = sip->head) != NULL) {
354                 sip->head = this->next;
355                 this->next = NULL;
356
357                 if (new_list == NULL || this->nseeks > new_list->nseeks)
358                         new_list = this;
359                 else if (this->nseeks == new_list->nseeks) {
360                         assert(this->nseeks == new_list->nseeks);
361                         for (p = new_list; p != NULL; p = p->next)
362                                 if (p->mode == this->mode)
363                                         break;
364
365                         if (p)
366                                 this->nseeks += p->nseeks;
367                         else
368                                 this->next = new_list;
369                         new_list = this;
370                 }
371         }
372
373         fprintf(ofp, "%10s | %15lld %15.1lf %15lld | %lld(%d)",
374                 "Average", sip->nseeks, sip->mean / sip->nseeks, 
375                 sip->median / sip->nseeks, new_list->mode, new_list->nseeks);
376
377         for (p = new_list->next; p != NULL; p = p->next)
378                 fprintf(ofp, " %lld(%d)", p->mode, p->nseeks);
379 }
380
381 void add_seek_mode_info(struct o_seek_info *sip, struct mode *mp)
382 {
383         int i;
384         long long *lp = mp->modes;
385         struct seek_mode_info *smip;
386
387         n_seeks++;
388         for (i = 0; i < mp->nmds; i++, lp++) {
389                 for (smip = sip->head; smip; smip = smip->next) {
390                         if (smip->mode == *lp) {
391                                 smip->nseeks += mp->most_seeks;
392                                 break;
393                         }
394                 }
395                 if (!smip) {
396                         struct seek_mode_info *new = malloc(sizeof(*new));
397
398                         new->next = sip->head; 
399                         sip->head = new;
400                         new->mode = *lp;
401                         new->nseeks = mp->most_seeks;
402
403                         add_buf(new);
404                 }
405         }
406 }
407
408 static void do_output_dip_seek_info(struct d_info *dip, FILE *ofp, int is_q2q)
409 {
410         double mean;
411         int i, nmodes;
412         long long nseeks;
413         char dev_info[15];
414         long long median;
415         struct mode m;
416         void *handle = is_q2q ? dip->q2q_handle : dip->seek_handle;
417
418         nseeks = seeki_nseeks(handle);
419         if (nseeks > 0) {
420                 mean = seeki_mean(handle);
421                 median = seeki_median(handle);
422                 nmodes = seeki_mode(handle, &m);
423
424                 fprintf(ofp, "%10s | %15lld %15.1lf %15lld | %lld(%d)",
425                         make_dev_hdr(dev_info, 15, dip), nseeks, mean, median, 
426                         nmodes > 0 ? m.modes[0] : 0, m.most_seeks);
427                 for (i = 1; i < nmodes; i++)
428                         fprintf(ofp, " %lld", m.modes[i]);
429                 fprintf(ofp, "\n");
430
431                 seek_info.nseeks += nseeks;
432                 seek_info.mean += (nseeks * mean);
433                 seek_info.median += (nseeks * median);
434                 add_seek_mode_info(&seek_info, &m);
435                 free(m.modes);
436         }
437 }
438
439 void __output_dip_seek_info(struct d_info *dip, void *arg)
440 {
441         do_output_dip_seek_info(dip, (FILE *)arg, 0);
442 }
443
444 void __output_dip_q2q_seek_info(struct d_info *dip, void *arg)
445 {
446         do_output_dip_seek_info(dip, (FILE *)arg, 1);
447 }
448
449 void output_dip_seek_info(FILE *ofp)
450 {
451         n_seeks = 1;
452         memset(&seek_info, 0, sizeof(seek_info));
453
454         fprintf(ofp, "%10s | %15s %15s %15s | %-15s\n", "DEV", "NSEEKS", 
455                         "MEAN", "MEDIAN", "MODE");
456         fprintf(ofp, "---------- | --------------- --------------- --------------- | ---------------\n");
457         dip_foreach_out(__output_dip_seek_info, ofp);
458         if (n_seeks > 1) {
459                 fprintf(ofp, "---------- | --------------- --------------- --------------- | ---------------\n");
460                 fprintf(ofp, "%10s | %15s %15s %15s | %-15s\n", 
461                         "Overall", "NSEEKS", "MEAN", "MEDIAN", "MODE");
462                 output_seek_mode_info(ofp, &seek_info);
463                 fprintf(ofp, "\n");
464         }
465         fprintf(ofp, "\n");
466 }
467
468 void output_dip_q2q_seek_info(FILE *ofp)
469 {
470         n_seeks = 1;
471         memset(&seek_info, 0, sizeof(seek_info));
472
473         fprintf(ofp, "%10s | %15s %15s %15s | %-15s\n", "DEV", "NSEEKS", 
474                         "MEAN", "MEDIAN", "MODE");
475         fprintf(ofp, "---------- | --------------- --------------- --------------- | ---------------\n");
476         dip_foreach_out(__output_dip_q2q_seek_info, ofp);
477         if (n_seeks > 1) {
478                 fprintf(ofp, "---------- | --------------- --------------- --------------- | ---------------\n");
479                 fprintf(ofp, "%10s | %15s %15s %15s | %-15s\n", 
480                         "Overall", "NSEEKS", "MEAN", "MEDIAN", "MODE");
481                 output_seek_mode_info(ofp, &seek_info);
482                 fprintf(ofp, "\n");
483         }
484         fprintf(ofp, "\n");
485 }
486
487 struct __opa {
488         FILE *ofp;
489         ai_pip_t (*func)(struct p_info *);
490 };
491
492 void __output_pip_avg(struct p_info *pip, void *arg)
493 {
494         struct __opa *opap = arg;
495         ai_pip_t ap = opap->func(pip);
496
497         if (ap->n > 0) {
498                 char proc_name[15];
499                 snprintf(proc_name, 15, pip->name);
500
501                 ap->avg = BIT_TIME(ap->total) / (double)ap->n;
502                 __output_avg(opap->ofp, proc_name, ap);
503         }
504 }
505
506 void output_pip_avg(FILE *ofp, char *hdr, ai_pip_t (*func)(struct p_info *))
507 {
508         struct __opa opa = { .ofp = ofp, .func = func };
509
510         output_hdr(ofp, hdr);
511         pip_foreach_out(__output_pip_avg, &opa);
512         fprintf(ofp, "\n");
513 }
514
515 int n_plugs;
516 struct plug_info {
517         long n_plugs, n_timer_unplugs;
518         double t_percent;
519 } plug_info;
520
521 void __dip_output_plug(struct d_info *dip, void *arg)
522 {
523         char dev_info[15];
524         FILE *ofp = arg;
525         double delta, pct;
526
527         if (dip->nplugs > 0) {
528                 if (dip->is_plugged) dip_unplug(dip->device, dip->end_time);
529                 delta = dip->end_time - dip->start_time;
530                 pct = 100.0 * ((dip->plugged_time / delta) / delta);
531
532                 fprintf(ofp, "%10s | %10d(%10d) | %13.9lf%%\n", 
533                         make_dev_hdr(dev_info, 15, dip), 
534                         dip->nplugs, dip->n_timer_unplugs, pct);
535
536                 n_plugs++;
537                 plug_info.n_plugs += dip->nplugs;
538                 plug_info.n_timer_unplugs += dip->n_timer_unplugs;
539                 plug_info.t_percent += pct;
540         }
541 }
542
543 void __dip_output_plug_all(FILE *ofp, struct plug_info *p)
544 {
545         fprintf(ofp, "---------- | ---------- ----------  | ----------------\n");
546         fprintf(ofp, "%10s | %10s %10s  | %s\n", 
547                 "Overall", "# Plugs", "# Timer Us", "% Time Q Plugged");
548         fprintf(ofp, "%10s | %10ld(%10ld) | %13.9lf%%\n", "Average",
549                 p->n_plugs / n_plugs, p->n_timer_unplugs / n_plugs, 
550                 p->t_percent / n_plugs);
551
552 }
553
554 void output_plug_info(FILE *ofp)
555 {
556         fprintf(ofp, "%10s | %10s %10s  | %s\n", 
557                 "DEV", "# Plugs", "# Timer Us", "% Time Q Plugged");
558         fprintf(ofp, "---------- | ---------- ----------  | ----------------\n");
559         dip_foreach_out(__dip_output_plug, ofp);
560         if (n_plugs > 1)
561                 __dip_output_plug_all(ofp, &plug_info);
562         fprintf(ofp, "\n");
563 }
564
565 int n_actQs;
566 struct actQ_info {
567         __u64 t_qs;
568         __u64 t_act_qs;
569 } actQ_info;
570
571 void __dip_output_actQ(struct d_info *dip, void *arg)
572 {
573         if (dip->n_qs > 0 && !remapper_dev(dip->device)) {
574                 char dev_info[15];
575                 double a_actQs = (double)dip->t_act_q / (double)dip->n_qs;
576
577                 fprintf((FILE *)arg, "%10s | %13.1lf\n", 
578                         make_dev_hdr(dev_info, 15, dip), a_actQs);
579                         
580                 n_actQs++;
581                 actQ_info.t_qs += dip->n_qs;
582                 actQ_info.t_act_qs += dip->t_act_q;
583         }
584 }
585
586 void __dip_output_actQ_all(FILE *ofp, struct actQ_info *p)
587 {
588         fprintf(ofp, "---------- | -------------\n");
589         fprintf(ofp, "%10s | %13s\n", "Overall", "Avgs Reqs @ Q");
590         fprintf(ofp, "%10s | %13.1lf\n", "Average", 
591                 (double)p->t_act_qs / (double)p->t_qs);
592 }
593
594 void output_actQ_info(FILE *ofp)
595 {
596         fprintf(ofp, "%10s | %13s\n", "DEV", "Avg Reqs @ Q");
597         fprintf(ofp, "---------- | -------------\n");
598         dip_foreach_out(__dip_output_actQ, ofp);
599         if (n_actQs > 1)
600                 __dip_output_actQ_all(ofp, &actQ_info);
601         fprintf(ofp, "\n");
602 }
603
604 void output_histos(void)
605 {
606         int i;
607         FILE *ofp;
608         char fname[256];
609
610         if (output_name == NULL) return;
611
612         sprintf(fname, "%s_qhist.dat", output_name);
613         ofp = fopen(fname, "w");
614         if (!ofp) {
615                 perror(fname);
616                 return;
617         }
618
619         fprintf(ofp, "# BTT histogram data\n");
620         fprintf(ofp, "# Q buckets\n");
621         for (i = 0; i < (N_HIST_BKTS-1); i++) 
622                 fprintf(ofp, "%4d %lld\n", (i+1), (long long)q_histo[i]);
623         fprintf(ofp, "\n# Q bucket for > %d\n%4d %lld\n", (int)N_HIST_BKTS-1,
624                 N_HIST_BKTS-1, (long long)q_histo[N_HIST_BKTS-1]);
625         fclose(ofp);
626
627         sprintf(fname, "%s_dhist.dat", output_name);
628         ofp = fopen(fname, "w");
629         if (!ofp) {
630                 perror(fname);
631                 return;
632         }
633         fprintf(ofp, "# D buckets\n");
634         for (i = 0; i < (N_HIST_BKTS-1); i++)
635                 fprintf(ofp, "%4d %lld\n", (i+1), (long long)d_histo[i]);
636         fprintf(ofp, "\n# D bucket for > %d\n%4d %lld\n", (int)N_HIST_BKTS-1,
637                 N_HIST_BKTS-1, (long long)d_histo[N_HIST_BKTS-1]);
638         fclose(ofp);
639 }
640
641 int output_avgs(FILE *ofp)
642 {
643         if (output_all_data) {
644                 if (exes == NULL || *exes != '\0') {
645                         output_section_hdr(ofp, "Per Process");
646                         output_pip_avg(ofp, "Q2Qdm", pip_q2q_dm_avg);
647                         output_pip_avg(ofp, "Q2Adm", pip_q2a_dm_avg);
648                         output_pip_avg(ofp, "Q2Cdm", pip_q2c_dm_avg);
649                         fprintf(ofp, "\n");
650
651                         output_pip_avg(ofp, "Q2Q", pip_q2q_avg);
652                         output_pip_avg(ofp, "Q2A", pip_q2a_avg);
653                         output_pip_avg(ofp, "Q2G", pip_q2g_avg);
654                         output_pip_avg(ofp, "G2I", pip_g2i_avg);
655                         output_pip_avg(ofp, "Q2M", pip_q2m_avg);
656                         output_pip_avg(ofp, "I2D", pip_i2d_avg);
657                         output_pip_avg(ofp, "D2C", pip_d2c_avg);
658                         output_pip_avg(ofp, "Q2C", pip_q2c_avg);
659                 }
660
661                 output_section_hdr(ofp, "Per Device");
662                 output_dip_avg(ofp, "Q2Qdm", dip_q2q_dm_avg);
663                 output_dip_avg(ofp, "Q2Adm", dip_q2a_dm_avg);
664                 output_dip_avg(ofp, "Q2Cdm", dip_q2c_dm_avg);
665                 fprintf(ofp, "\n");
666
667                 output_dip_avg(ofp, "Q2Q", dip_q2q_avg);
668                 output_dip_avg(ofp, "Q2A", dip_q2a_avg);
669                 output_dip_avg(ofp, "Q2G", dip_q2g_avg);
670                 output_dip_avg(ofp, "G2I", dip_g2i_avg);
671                 output_dip_avg(ofp, "Q2M", dip_q2m_avg);
672                 output_dip_avg(ofp, "I2D", dip_i2d_avg);
673                 output_dip_avg(ofp, "D2C", dip_d2c_avg);
674                 output_dip_avg(ofp, "Q2C", dip_q2c_avg);
675         }
676
677         output_section_hdr(ofp, "All Devices");
678         output_hdr(ofp, "ALL");
679         __output_avg(ofp, "Q2Qdm", &all_avgs.q2q_dm);
680         __output_avg(ofp, "Q2Adm", &all_avgs.q2a_dm);
681         __output_avg(ofp, "Q2Cdm", &all_avgs.q2c_dm);
682         fprintf(ofp, "\n");
683
684         __output_avg(ofp, "Q2Q", &all_avgs.q2q);
685         __output_avg(ofp, "Q2A", &all_avgs.q2a);
686         __output_avg(ofp, "Q2G", &all_avgs.q2g);
687         __output_avg(ofp, "G2I", &all_avgs.g2i);
688         __output_avg(ofp, "Q2M", &all_avgs.q2m);
689         __output_avg(ofp, "I2D", &all_avgs.i2d);
690         __output_avg(ofp, "M2D", &all_avgs.m2d);
691         __output_avg(ofp, "D2C", &all_avgs.d2c);
692         __output_avg(ofp, "Q2C", &all_avgs.q2c);
693         fprintf(ofp, "\n");
694
695         output_section_hdr(ofp, "Device Overhead");
696         output_dip_prep_ohead(ofp);
697
698         output_section_hdr(ofp, "Device Merge Information");
699         output_dip_merge_ratio(ofp);
700
701         output_section_hdr(ofp, "Device Q2Q Seek Information");
702         output_dip_q2q_seek_info(ofp);
703
704         output_section_hdr(ofp, "Device D2D Seek Information");
705         output_dip_seek_info(ofp);
706
707         output_section_hdr(ofp, "Plug Information");
708         output_plug_info(ofp);
709
710         output_section_hdr(ofp, "Active Requests At Q Information");
711         output_actQ_info(ofp);
712
713         output_histos();
714
715         if (output_all_data) {
716                 output_section_hdr(ofp, "Q2D Histogram");
717                 output_q2d_histo(ofp);
718         }
719
720         return 0;
721 }
722
723 void __output_ranges(FILE *ofp, struct list_head *head_p, float base)
724 {
725         struct range_info *rip;
726         struct list_head *p;
727         float limit = base + 0.4;
728
729         __list_for_each(p, head_p) {
730                 rip = list_entry(p, struct range_info, head);
731                 fprintf(ofp, "%13.9lf %5.1f\n", BIT_TIME(rip->start), base);
732                 fprintf(ofp, "%13.9lf %5.1f\n", BIT_TIME(rip->start), limit);
733                 fprintf(ofp, "%13.9lf %5.1f\n", BIT_TIME(rip->end), limit);
734                 fprintf(ofp, "%13.9lf %5.1f\n", BIT_TIME(rip->end), base);
735         }
736 }
737
738 int output_regions(FILE *ofp, char *header, struct region_info *reg, 
739                           float base)
740 {
741         if (list_len(&reg->qranges) == 0 && list_len(&reg->cranges) == 0)
742                 return 0;
743
744         fprintf(ofp, "# %16s : q activity\n", header);
745         __output_ranges(ofp, &reg->qranges, base);
746         fprintf(ofp, "\n");
747
748         fprintf(ofp, "# %16s : c activity\n", header);
749         __output_ranges(ofp, &reg->cranges, base + 0.5);
750         fprintf(ofp, "\n");
751
752         return 1;
753 }
754
755 struct __od {
756         FILE *ofp;
757         float base;
758 };
759 void __output_dev(struct d_info *dip, void *arg)
760 {
761         char header[128];
762         struct __od *odp = arg;
763
764         sprintf(header, "%d,%d", MAJOR(dip->device), MINOR(dip->device));
765         if (output_regions(odp->ofp, header, &dip->regions, odp->base))
766                 odp->base += 1.0;
767 }
768
769 float output_devs(FILE *ofp, float base)
770 {
771         struct __od od = { .ofp = ofp, .base = base };
772
773         fprintf(ofp, "# Per device\n" );
774         dip_foreach_out(__output_dev, &od);
775         return od.base;
776 }
777
778 static inline int exe_match(char *exe, char *name)
779 {
780         return (exe == NULL) || (strstr(name, exe) != NULL);
781 }
782
783 struct __op {
784         FILE *ofp;
785         float base;
786 };
787 void __output_procs(struct p_info *pip, void *arg)
788 {
789         struct __op *opp = arg;
790         output_regions(opp->ofp, pip->name, &pip->regions, opp->base);
791         opp->base += 1.0;
792 }
793
794 float output_procs(FILE *ofp, float base)
795 {
796         struct __op op = { .ofp = ofp, .base = base };
797
798         fprintf(ofp, "# Per process\n" );
799         pip_foreach_out(__output_procs, &op);
800         return op.base;
801 }
802
803 int output_ranges(FILE *ofp)
804 {
805         float base = 0.0;
806
807         fprintf(ofp, "# %s\n", "Total System");
808         if (output_regions(ofp, "Total System", &all_regions, base))
809                 base += 1.0;
810
811         if (n_devs > 1)
812                 base = output_devs(ofp, base);
813
814         base = output_procs(ofp, base);
815
816         return 0;
817 }