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