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