[PATCH] Convert to using on-the-fly RB trees, no post-traversal.
[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, "%12s %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, "%-12s %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, "%12s %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, "%-12s %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[12];
99         __output_avg2((FILE *)arg, make_dev_hdr(dev_info, 12, 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[12];
123                 ap->avg = BIT_TIME(ap->total) / (double)ap->n;
124                 __output_avg(odap->ofp, make_dev_hdr(dev_info, 12, 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 void __output_dip_merge_ratio(struct d_info *dip, void *arg)
137 {
138         double blks_avg;
139         char scratch[12];
140         double ratio, q2c_n = dip->avgs.q2c.n, d2c_n = dip->n_ds;
141
142         if (q2c_n > 0.0 && d2c_n > 0.0) {
143                 ratio = q2c_n / d2c_n;
144                 blks_avg = (double)dip->avgs.blks.total / d2c_n;
145                 fprintf((FILE *)arg, 
146                         "%10s | %8llu %8llu %7.1lf | %8llu %8llu %8llu %8llu\n",
147                         make_dev_hdr(scratch, 12, dip),
148                         (unsigned long long)dip->avgs.q2c.n,
149                         (unsigned long long)dip->n_ds,
150                         ratio,
151                         (unsigned long long)dip->avgs.blks.min,
152                         (unsigned long long)blks_avg,
153                         (unsigned long long)dip->avgs.blks.max,
154                         (unsigned long long)dip->avgs.blks.total);
155
156         }
157 }
158
159 void output_dip_merge_ratio(FILE *ofp)
160 {
161         fprintf(ofp, "%10s | %8s %8s %7s | %8s %8s %8s %8s\n", "DEV", "#Q", "#D", "Ratio", "BLKmin", "BLKavg", "BLKmax", "Total");
162         fprintf(ofp, "---------- | -------- -------- ------- | -------- -------- -------- --------\n");
163         dip_foreach_out(__output_dip_merge_ratio, ofp);
164         fprintf(ofp, "\n");
165 }
166
167 #define AVG(a,b) (100.0 * ((double)(a) / (double)(b)))
168 #define CALC_AVG(ap) (ap)->avg = ((ap)->n == 0 ? 0.0 :                    \
169                                                  (BIT_TIME((ap)->total) / \
170                                                         (double)(ap)->n))
171 char *q2i_v_q2C(struct d_info *dip, char *s)
172 {
173         double q2c;
174
175         if (dip->avgs.q2i.n == 0) return " ";
176
177         q2c = dip->avgs.q2i.avg + dip->avgs.i2d.avg + dip->avgs.d2c.avg;
178         sprintf(s, "%5.1lf%%", AVG(dip->avgs.q2i.avg, q2c));
179
180         return s;
181 }
182
183 char *i2d_v_q2C(struct d_info *dip, char *s)
184 {
185         double q2c;
186
187         if (dip->avgs.d2c.n == 0) return " ";
188
189         q2c = dip->avgs.q2i.avg + dip->avgs.i2d.avg + dip->avgs.d2c.avg;
190         sprintf(s, "%5.1lf%%", AVG(dip->avgs.i2d.avg, q2c));
191
192         return s;
193 }
194
195 char *d2c_v_q2C(struct d_info *dip, char *s)
196 {
197         double q2c;
198
199         if (dip->avgs.d2c.n == 0) return " ";
200
201         q2c = dip->avgs.q2i.avg + dip->avgs.i2d.avg + dip->avgs.d2c.avg;
202         sprintf(s, "%5.1lf%%", AVG(dip->avgs.d2c.avg, q2c));
203
204         return s;
205 }
206
207 void __output_dip_prep_ohead(struct d_info *dip, void *arg)
208 {
209         char dev_info[12];
210         char s1[16], s2[16], s3[16];
211
212         if ((dip->avgs.q2i.n > 0 && dip->avgs.i2d.n > 0 &&
213                                                 dip->avgs.d2c.n > 0)) {
214                 CALC_AVG(&dip->avgs.q2i);
215                 CALC_AVG(&dip->avgs.i2d);
216                 CALC_AVG(&dip->avgs.d2c);
217
218                 fprintf((FILE *)arg, "%10s | %6s %6s %6s\n",
219                         make_dev_hdr(dev_info, 12, dip),
220                         q2i_v_q2C(dip, s1), i2d_v_q2C(dip, s2),
221                         d2c_v_q2C(dip, s3));
222         }
223 }
224
225 void output_dip_prep_ohead(FILE *ofp)
226 {
227         fprintf(ofp, "%10s | %6s %6s %6s\n", "DEV", "Q2I", "I2D", "D2C");
228         fprintf(ofp, "---------- | ------ ------ ------\n");
229         dip_foreach_out(__output_dip_prep_ohead, ofp);
230         fprintf(ofp, "\n");
231 }
232
233 void __output_dip_seek_info(struct d_info *dip, void *arg)
234 {
235         double mean;
236         int i, nmodes;
237         long long nseeks;
238         char dev_info[12];
239         long long median;
240         struct mode m;
241         FILE *ofp = arg;
242
243         nseeks = seeki_nseeks(dip->seek_handle);
244         if (nseeks > 0) {
245                 mean = seeki_mean(dip->seek_handle);
246                 median = seeki_median(dip->seek_handle);
247                 nmodes = seeki_mode(dip->seek_handle, &m);
248
249                 fprintf(ofp, "%10s | %15lld %15.1lf %15lld | %lld(%d)",
250                         make_dev_hdr(dev_info, 12, dip), nseeks, mean, median, 
251                         nmodes > 0 ? m.modes[0] : 0, m.most_seeks);
252                 for (i = 1; i < nmodes; i++)
253                         fprintf(ofp, " %lld", m.modes[i]);
254                 fprintf(ofp, "\n");
255         }
256 }
257
258 void output_dip_seek_info(FILE *ofp)
259 {
260         fprintf(ofp, "%10s | %15s %15s %15s | %-15s\n", "DEV", "NSEEKS", 
261                         "MEAN", "MEDIAN", "MODE");
262         fprintf(ofp, "---------- "
263                         "| --------------- --------------- --------------- "
264                         "| ---------------\n");
265         dip_foreach_out(__output_dip_seek_info, ofp);
266         fprintf(ofp, "\n");
267 }
268
269 struct __opa {
270         FILE *ofp;
271         ai_pip_t (*func)(struct p_info *);
272 };
273
274 void __output_pip_avg(struct p_info *pip, void *arg)
275 {
276         struct __opa *opap = arg;
277         ai_pip_t ap = opap->func(pip);
278
279         if (ap->n > 0) {
280                 char proc_name[12];
281                 snprintf(proc_name, 12, pip->name);
282
283                 ap->avg = BIT_TIME(ap->total) / (double)ap->n;
284                 __output_avg(opap->ofp, proc_name, ap);
285         }
286 }
287
288 void output_pip_avg(FILE *ofp, char *hdr, ai_pip_t (*func)(struct p_info *))
289 {
290         struct __opa opa = { .ofp = ofp, .func = func };
291
292         output_hdr(ofp, hdr);
293         pip_foreach_out(__output_pip_avg, &opa);
294         fprintf(ofp, "\n");
295 }
296
297 void output_dip_avgs(FILE *ofp)
298 {
299         output_hdr2(ofp,"Dev");
300         dip_foreach_out(__dip_output_avg2, ofp);
301         fprintf(ofp, "\n");
302 }
303
304 void output_pip_avgs(FILE *ofp)
305 {
306         output_hdr2(ofp,"Exe");
307         pip_foreach_out(__pip_output_avg2, ofp);
308         fprintf(ofp, "\n");
309 }
310
311 int output_avgs(FILE *ofp)
312 {
313         if (exes == NULL || *exes != '\0') {
314                 output_section_hdr(ofp, "Per Process");
315                 output_pip_avg(ofp, "Q2Q", pip_q2q_avg);
316                 output_pip_avg(ofp, "Q2A", pip_q2a_avg);
317                 output_pip_avg(ofp, "Q2I", pip_q2i_avg);
318                 output_pip_avg(ofp, "I2D", pip_i2d_avg);
319                 output_pip_avg(ofp, "D2C", pip_d2c_avg);
320                 output_pip_avg(ofp, "Q2C", pip_q2c_avg);
321         }
322
323         output_section_hdr(ofp, "Per Device");
324         output_dip_avg(ofp, "Q2Q", dip_q2q_avg);
325         output_dip_avg(ofp, "Q2A", dip_q2a_avg);
326         output_dip_avg(ofp, "Q2I", dip_q2i_avg);
327         output_dip_avg(ofp, "I2D", dip_i2d_avg);
328         output_dip_avg(ofp, "D2C", dip_d2c_avg);
329         output_dip_avg(ofp, "Q2C", dip_q2c_avg);
330
331         output_section_hdr(ofp, "All Devices");
332         output_hdr(ofp, "ALL");
333         __output_avg(ofp, "Q2Q", &all_avgs.q2q);
334         __output_avg(ofp, "Q2A", &all_avgs.q2a);
335         __output_avg(ofp, "Q2I", &all_avgs.q2i);
336         __output_avg(ofp, "I2D", &all_avgs.i2d);
337         __output_avg(ofp, "D2C", &all_avgs.d2c);
338         __output_avg(ofp, "Q2C", &all_avgs.q2c);
339
340         if (exes == NULL || *exes != '\0') {
341                 output_section_hdr(ofp, "Per Process (avgs)");
342                 output_pip_avgs(ofp);
343         }
344
345         output_section_hdr(ofp, "Per Device (avgs)");
346         output_dip_avgs(ofp);
347
348         output_section_hdr(ofp, "Device Merge Information");
349         output_dip_merge_ratio(ofp);
350
351         output_section_hdr(ofp, "Device Overhead");
352         output_dip_prep_ohead(ofp);
353
354         if (seek_name) {
355                 output_section_hdr(ofp, "Device Seek Information");
356                 output_dip_seek_info(ofp);
357         }
358
359         return 0;
360 }
361
362 void __output_ranges(FILE *ofp, struct list_head *head_p, float base)
363 {
364         struct range_info *rip;
365         struct list_head *p;
366         float limit = base + 0.4;
367
368         __list_for_each(p, head_p) {
369                 rip = list_entry(p, struct range_info, head);
370                 fprintf(ofp, "%13.9lf %5.1f\n", BIT_TIME(rip->start), base);
371                 fprintf(ofp, "%13.9lf %5.1f\n", BIT_TIME(rip->start), limit);
372                 fprintf(ofp, "%13.9lf %5.1f\n", BIT_TIME(rip->end), limit);
373                 fprintf(ofp, "%13.9lf %5.1f\n", BIT_TIME(rip->end), base);
374         }
375 }
376
377 int output_regions(FILE *ofp, char *header, struct region_info *reg, 
378                           float base)
379 {
380         if (reg->qr_cur != NULL)
381                 list_add_tail(&reg->qr_cur->head, &reg->qranges);
382         if (reg->cr_cur != NULL)
383                 list_add_tail(&reg->cr_cur->head, &reg->cranges);
384
385         if (list_len(&reg->qranges) == 0 && list_len(&reg->cranges) == 0)
386                 return 0;
387
388         fprintf(ofp, "# %16s : q activity\n", header);
389         __output_ranges(ofp, &reg->qranges, base);
390         fprintf(ofp, "\n");
391
392         fprintf(ofp, "# %16s : c activity\n", header);
393         __output_ranges(ofp, &reg->cranges, base + 0.5);
394         fprintf(ofp, "\n");
395
396         return 1;
397 }
398
399 struct __od {
400         FILE *ofp;
401         float base;
402 };
403 void __output_dev(struct d_info *dip, void *arg)
404 {
405         char header[128];
406         struct __od *odp = arg;
407
408         sprintf(header, "%d,%d", MAJOR(dip->device), MINOR(dip->device));
409         if (output_regions(odp->ofp, header, &dip->regions, odp->base))
410                 odp->base += 1.0;
411 }
412
413 float output_devs(FILE *ofp, float base)
414 {
415         struct __od od = { .ofp = ofp, .base = base };
416
417         fprintf(ofp, "# Per device\n" );
418         dip_foreach_out(__output_dev, &od);
419         return od.base;
420 }
421
422 static inline int exe_match(char *exe, char *name)
423 {
424         return (exe == NULL) || (strstr(name, exe) != NULL);
425 }
426
427 struct __op {
428         FILE *ofp;
429         float base;
430 };
431 void __output_procs(struct p_info *pip, void *arg)
432 {
433         struct __op *opp = arg;
434         output_regions(opp->ofp, pip->name, &pip->regions, opp->base);
435         opp->base += 1.0;
436 }
437
438 float output_procs(FILE *ofp, float base)
439 {
440         struct __op op = { .ofp = ofp, .base = base };
441
442         fprintf(ofp, "# Per process\n" );
443         pip_foreach_out(__output_procs, &op);
444         return op.base;
445 }
446
447 int output_ranges(FILE *ofp)
448 {
449         float base = 0.0;
450
451         fprintf(ofp, "# %s\n", "Total System");
452         if (output_regions(ofp, "Total System", &all_regions, base))
453                 base += 1.0;
454
455         if (n_devs > 1)
456                 base = output_devs(ofp, base);
457
458         base = output_procs(ofp, base);
459
460         return 0;
461 }