BTT: Clean up output in preparation for first major documentation effort.
[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 int n_merges = 0;
137 struct {
138         unsigned long long nq, nd, blkmin, blkmax, total;
139 } merge_data;
140 void __output_dip_merge_ratio(struct d_info *dip, void *arg)
141 {
142         double blks_avg;
143         char scratch[15];
144         double ratio, q2c_n = dip->avgs.q2c.n, d2c_n = dip->n_ds;
145
146         if (q2c_n > 0.0 && d2c_n > 0.0) {
147                 ratio = q2c_n / d2c_n;
148                 blks_avg = (double)dip->avgs.blks.total / d2c_n;
149                 fprintf((FILE *)arg, 
150                         "%10s | %8llu %8llu %7.1lf | %8llu %8llu %8llu %8llu\n",
151                         make_dev_hdr(scratch, 15, dip),
152                         (unsigned long long)dip->avgs.q2c.n,
153                         (unsigned long long)dip->n_ds,
154                         ratio,
155                         (unsigned long long)dip->avgs.blks.min,
156                         (unsigned long long)blks_avg,
157                         (unsigned long long)dip->avgs.blks.max,
158                         (unsigned long long)dip->avgs.blks.total);
159
160                 if (n_merges++ == 0) {
161                         merge_data.blkmin = dip->avgs.blks.min;
162                         merge_data.blkmax = dip->avgs.blks.max;
163                 }
164
165                 merge_data.nq += dip->avgs.q2c.n;
166                 merge_data.nd += dip->n_ds;
167                 merge_data.total += dip->avgs.blks.total;
168                 if (dip->avgs.blks.min < merge_data.blkmin)
169                         merge_data.blkmin = dip->avgs.blks.min;
170                 if (dip->avgs.blks.max > merge_data.blkmax)
171                         merge_data.blkmax = dip->avgs.blks.max;
172         }
173 }
174
175 void output_dip_merge_ratio(FILE *ofp)
176 {
177         fprintf(ofp, "%10s | %8s %8s %7s | %8s %8s %8s %8s\n", "DEV", "#Q", "#D", "Ratio", "BLKmin", "BLKavg", "BLKmax", "Total");
178         fprintf(ofp, "---------- | -------- -------- ------- | -------- -------- -------- --------\n");
179         dip_foreach_out(__output_dip_merge_ratio, ofp);
180         if (n_merges > 1) {
181                 fprintf(ofp, "---------- | -------- -------- ------- | -------- -------- -------- --------\n");
182                 fprintf(ofp, "%10s | %8s %8s %7s | %8s %8s %8s %8s\n", "DEV", "#Q", "#D", "Ratio", "BLKmin", "BLKavg", "BLKmax", "Total");
183                 fprintf((FILE *)ofp, 
184                         "%10s | %8llu %8llu %7.1lf | %8llu %8llu %8llu %8llu\n",
185                         "TOTAL", merge_data.nq, merge_data.nd, 
186                         (float)merge_data.nq / (float)merge_data.nd,
187                         merge_data.blkmin, 
188                         merge_data.total / merge_data.nd,
189                         merge_data.blkmax, merge_data.total);
190         }
191         fprintf(ofp, "\n");
192 }
193
194 #define AVG(a,b) (100.0 * ((double)(a) / (double)(b)))
195 #define CALC_AVG(ap) (ap)->avg = ((ap)->n == 0 ? 0.0 :                    \
196                                                  (BIT_TIME((ap)->total) / \
197                                                         (double)(ap)->n))
198 char *q2i_v_q2C(struct d_info *dip, char *s)
199 {
200         double q2c;
201
202         if (dip->avgs.q2i.n == 0) return " ";
203
204         q2c = dip->avgs.q2i.avg + dip->avgs.i2d.avg + dip->avgs.d2c.avg;
205         sprintf(s, "%5.1lf%%", AVG(dip->avgs.q2i.avg, q2c));
206
207         return s;
208 }
209
210 char *i2d_v_q2C(struct d_info *dip, char *s)
211 {
212         double q2c;
213
214         if (dip->avgs.d2c.n == 0) return " ";
215
216         q2c = dip->avgs.q2i.avg + dip->avgs.i2d.avg + dip->avgs.d2c.avg;
217         sprintf(s, "%5.1lf%%", AVG(dip->avgs.i2d.avg, q2c));
218
219         return s;
220 }
221
222 char *d2c_v_q2C(struct d_info *dip, char *s)
223 {
224         double q2c;
225
226         if (dip->avgs.d2c.n == 0) return " ";
227
228         q2c = dip->avgs.q2i.avg + dip->avgs.i2d.avg + dip->avgs.d2c.avg;
229         sprintf(s, "%5.1lf%%", AVG(dip->avgs.d2c.avg, q2c));
230
231         return s;
232 }
233
234 void __output_dip_prep_ohead(struct d_info *dip, void *arg)
235 {
236         char dev_info[15];
237         char s1[16], s2[16], s3[16];
238
239         if ((dip->avgs.q2i.n > 0 && dip->avgs.i2d.n > 0 &&
240                                                 dip->avgs.d2c.n > 0)) {
241                 CALC_AVG(&dip->avgs.q2i);
242                 CALC_AVG(&dip->avgs.i2d);
243                 CALC_AVG(&dip->avgs.d2c);
244
245                 fprintf((FILE *)arg, "%10s | %6s %6s %6s\n",
246                         make_dev_hdr(dev_info, 15, dip),
247                         q2i_v_q2C(dip, s1), i2d_v_q2C(dip, s2),
248                         d2c_v_q2C(dip, s3));
249         }
250 }
251
252 void output_dip_prep_ohead(FILE *ofp)
253 {
254         fprintf(ofp, "%10s | %6s %6s %6s\n", "DEV", "Q2I", "I2D", "D2C");
255         fprintf(ofp, "---------- | ------ ------ ------\n");
256         dip_foreach_out(__output_dip_prep_ohead, ofp);
257         fprintf(ofp, "\n");
258 }
259
260 int n_seeks;
261 struct seek_mode_info {
262         struct seek_mode_info *next;
263         long long mode;
264         int nseeks;
265 };
266 struct o_seek_info {
267         long long nseeks, median;
268         double mean;
269         struct seek_mode_info *head;
270 } seek_info = {
271         .nseeks = 0L,
272         .median = 0L,
273         .mean = 0.0,
274         .head = NULL
275 };
276 void output_seek_mode_info(FILE *ofp, struct o_seek_info *sip)
277 {
278         struct seek_mode_info *p, *this, *new_list = NULL;
279
280         ASSERT(sip->head != NULL);
281         while ((this = sip->head) != NULL) {
282                 sip->head = this->next;
283                 if (new_list == NULL) {
284                         this->next = NULL;
285                         new_list = this;
286                         continue;
287                 }
288                 if (this->nseeks < new_list->nseeks) {
289                         free(this);
290                         continue;
291                 }
292                 if (this->nseeks > new_list->nseeks) {
293                         while ((p = new_list) != NULL) {
294                                 new_list = p->next;
295                                 free(p);
296                         }
297                         this->next = NULL;
298                         new_list = this;
299                         continue;
300                 }
301                 for (p = new_list; p; p++) 
302                         if (p->mode == this->mode) {
303                                 this->nseeks += p->nseeks;
304                                 free(p);
305                                 break;
306                         }
307                 if (p)
308                         p->next = new_list;
309         }
310
311         fprintf(ofp, "%10s | %15lld %15.1lf %15lld | %lld(%d)",
312                 "Average", sip->nseeks, sip->mean / sip->nseeks, 
313                 sip->median / sip->nseeks, new_list->mode, new_list->nseeks);
314
315         p = new_list;
316         new_list = p->next;
317         free(p);
318         while((p = new_list) != NULL) {
319                 fprintf(ofp, " %lld", p->mode);
320                 new_list = p->next;
321                 free(p);
322         }
323 }
324 void add_seek_mode_info(struct o_seek_info *sip, struct mode *mp)
325 {
326         int i;
327         long long *lp = mp->modes;
328         struct seek_mode_info *smip;
329
330         n_seeks++;
331         for (i = 0; i < mp->nmds; i++) {
332                 for (smip = sip->head; smip; smip = smip->next) {
333                         if (smip->mode == lp[i]) {
334                                 smip->nseeks += mp->most_seeks;
335                                 break;
336                         }
337                 }
338                 if (!smip) {
339                         struct seek_mode_info *new = malloc(sizeof(*new));
340
341                         new->next = sip->head; 
342                         sip->head = new;
343                         new->mode = lp[i];
344                         new->nseeks = mp->most_seeks;
345                 }
346         }
347 }
348
349 void __output_dip_seek_info(struct d_info *dip, void *arg)
350 {
351         double mean;
352         int i, nmodes;
353         long long nseeks;
354         char dev_info[15];
355         long long median;
356         struct mode m;
357         FILE *ofp = arg;
358
359         nseeks = seeki_nseeks(dip->seek_handle);
360         if (nseeks > 0) {
361                 mean = seeki_mean(dip->seek_handle);
362                 median = seeki_median(dip->seek_handle);
363                 nmodes = seeki_mode(dip->seek_handle, &m);
364
365                 fprintf(ofp, "%10s | %15lld %15.1lf %15lld | %lld(%d)",
366                         make_dev_hdr(dev_info, 15, dip), nseeks, mean, median, 
367                         nmodes > 0 ? m.modes[0] : 0, m.most_seeks);
368                 for (i = 1; i < nmodes; i++)
369                         fprintf(ofp, " %lld", m.modes[i]);
370                 fprintf(ofp, "\n");
371
372                 seek_info.nseeks += nseeks;
373                 seek_info.mean += (nseeks * mean);
374                 seek_info.median += (nseeks * median);
375                 add_seek_mode_info(&seek_info, &m);
376         }
377 }
378
379 void output_dip_seek_info(FILE *ofp)
380 {
381         fprintf(ofp, "%10s | %15s %15s %15s | %-15s\n", "DEV", "NSEEKS", 
382                         "MEAN", "MEDIAN", "MODE");
383         fprintf(ofp, "---------- | --------------- --------------- --------------- | ---------------\n");
384         dip_foreach_out(__output_dip_seek_info, ofp);
385         if (n_seeks > 1) {
386                 fprintf(ofp, "---------- | --------------- --------------- --------------- | ---------------\n");
387                 fprintf(ofp, "%10s | %15s %15s %15s | %-15s\n", 
388                         "Overall", "NSEEKS", "MEAN", "MEDIAN", "MODE");
389                 output_seek_mode_info(ofp, &seek_info);
390                 fprintf(ofp, "\n");
391         }
392         fprintf(ofp, "\n");
393 }
394
395 struct __opa {
396         FILE *ofp;
397         ai_pip_t (*func)(struct p_info *);
398 };
399
400 void __output_pip_avg(struct p_info *pip, void *arg)
401 {
402         struct __opa *opap = arg;
403         ai_pip_t ap = opap->func(pip);
404
405         if (ap->n > 0) {
406                 char proc_name[15];
407                 snprintf(proc_name, 15, pip->name);
408
409                 ap->avg = BIT_TIME(ap->total) / (double)ap->n;
410                 __output_avg(opap->ofp, proc_name, ap);
411         }
412 }
413
414 void output_pip_avg(FILE *ofp, char *hdr, ai_pip_t (*func)(struct p_info *))
415 {
416         struct __opa opa = { .ofp = ofp, .func = func };
417
418         output_hdr(ofp, hdr);
419         pip_foreach_out(__output_pip_avg, &opa);
420         fprintf(ofp, "\n");
421 }
422
423 void output_dip_avgs(FILE *ofp)
424 {
425         output_hdr2(ofp,"Dev");
426         dip_foreach_out(__dip_output_avg2, ofp);
427         fprintf(ofp, "\n");
428 }
429
430 void output_pip_avgs(FILE *ofp)
431 {
432         output_hdr2(ofp,"Exe");
433         pip_foreach_out(__pip_output_avg2, ofp);
434         fprintf(ofp, "\n");
435 }
436
437 int n_plugs;
438 struct plug_info {
439         long n_plugs, n_timer_unplugs;
440         double t_percent;
441 } plug_info;
442
443 void __dip_output_plug(struct d_info *dip, void *arg)
444 {
445         char dev_info[15];
446         FILE *ofp = arg;
447         double delta, pct;
448
449         if (dip->nplugs > 0) {
450                 if (dip->is_plugged) dip_unplug(dip->device, dip->end_time, 0);
451                 delta = dip->end_time - dip->start_time;
452                 pct = 100.0 * ((dip->plugged_time / delta) / delta);
453
454                 fprintf(ofp, "%10s | %10d(%10d) | %13.9lf%%\n", 
455                         make_dev_hdr(dev_info, 15, dip), 
456                         dip->nplugs, dip->n_timer_unplugs, pct);
457
458                 n_plugs++;
459                 plug_info.n_plugs += dip->nplugs;
460                 plug_info.n_timer_unplugs += dip->n_timer_unplugs;
461                 plug_info.t_percent += pct;
462         }
463 }
464
465 void __dip_output_plug_all(FILE *ofp, struct plug_info *p)
466 {
467         fprintf(ofp, "---------- | ---------- ----------  | ----------------\n");
468         fprintf(ofp, "%10s | %10s %10s  | %s\n", 
469                 "DEV", "# Plugs", "# Timer Us", "% Time Q Plugged");
470         fprintf(ofp, "%10s | %10ld(%10ld) | %13.9lf%%\n", "OVERALL",
471                 p->n_plugs / n_plugs, p->n_timer_unplugs / n_plugs, 
472                 p->t_percent / n_plugs);
473
474 }
475
476 void output_plug_info(FILE *ofp)
477 {
478         fprintf(ofp, "%10s | %10s %10s  | %s\n", 
479                 "DEV", "# Plugs", "# Timer Us", "% Time Q Plugged");
480         fprintf(ofp, "---------- | ---------- ----------  | ----------------\n");
481         dip_foreach_out(__dip_output_plug, ofp);
482         if (n_plugs > 1)
483                 __dip_output_plug_all(ofp, &plug_info);
484         fprintf(ofp, "\n");
485 }
486
487 void output_histos(void)
488 {
489         int i;
490         FILE *ofp;
491         char fname[256];
492
493         if (output_name == NULL) return;
494
495         sprintf(fname, "%s_qhist.dat", output_name);
496         ofp = fopen(fname, "w");
497         if (!ofp) {
498                 perror(fname);
499                 return;
500         }
501
502         fprintf(ofp, "# BTT histogram data\n");
503         fprintf(ofp, "# Q buckets\n");
504         for (i = 0; i < (N_HIST_BKTS-1); i++) 
505                 fprintf(ofp, "%4d %lld\n", (i+1), (long long)q_histo[i]);
506         fprintf(ofp, "\n# Q bucket for > %d\n%4d %lld\n", (int)N_HIST_BKTS-1,
507                 N_HIST_BKTS-1, q_histo[N_HIST_BKTS-1]);
508         fclose(ofp);
509
510         sprintf(fname, "%s_dhist.dat", output_name);
511         ofp = fopen(fname, "w");
512         if (!ofp) {
513                 perror(fname);
514                 return;
515         }
516         fprintf(ofp, "# D buckets\n");
517         for (i = 0; i < (N_HIST_BKTS-1); i++)
518                 fprintf(ofp, "%4d %lld\n", (i+1), (long long)d_histo[i]);
519         fprintf(ofp, "\n# D bucket for > %d\n%4d %lld\n", (int)N_HIST_BKTS-1,
520                 N_HIST_BKTS-1, d_histo[N_HIST_BKTS-1]);
521         fclose(ofp);
522 }
523
524 int output_avgs(FILE *ofp)
525 {
526         if (output_all_data) {
527                 if (exes == NULL || *exes != '\0') {
528                         output_section_hdr(ofp, "Per Process");
529                         output_pip_avg(ofp, "Q2Q", pip_q2q_avg);
530                         output_pip_avg(ofp, "Q2A", pip_q2a_avg);
531                         output_pip_avg(ofp, "Q2I", pip_q2i_avg);
532                         output_pip_avg(ofp, "I2D", pip_i2d_avg);
533                         output_pip_avg(ofp, "D2C", pip_d2c_avg);
534                         output_pip_avg(ofp, "Q2C", pip_q2c_avg);
535                 }
536
537                 output_section_hdr(ofp, "Per Device");
538                 output_dip_avg(ofp, "Q2Q", dip_q2q_avg);
539                 output_dip_avg(ofp, "Q2A", dip_q2a_avg);
540                 output_dip_avg(ofp, "Q2I", dip_q2i_avg);
541                 output_dip_avg(ofp, "I2D", dip_i2d_avg);
542                 output_dip_avg(ofp, "D2C", dip_d2c_avg);
543                 output_dip_avg(ofp, "Q2C", dip_q2c_avg);
544         }
545
546         output_section_hdr(ofp, "All Devices");
547         output_hdr(ofp, "ALL");
548         __output_avg(ofp, "Q2Q", &all_avgs.q2q);
549         __output_avg(ofp, "Q2A", &all_avgs.q2a);
550         __output_avg(ofp, "Q2I", &all_avgs.q2i);
551         __output_avg(ofp, "I2D", &all_avgs.i2d);
552         __output_avg(ofp, "D2C", &all_avgs.d2c);
553         __output_avg(ofp, "Q2C", &all_avgs.q2c);
554         fprintf(ofp, "\n");
555
556         output_section_hdr(ofp, "Device Overhead");
557         output_dip_prep_ohead(ofp);
558
559         if (output_all_data) {
560                 if (exes == NULL || *exes != '\0') {
561                         output_section_hdr(ofp, "Per Process (avgs)");
562                         output_pip_avgs(ofp);
563                 }
564
565                 output_section_hdr(ofp, "Per Device (avgs)");
566                 output_dip_avgs(ofp);
567         }
568
569         output_section_hdr(ofp, "Device Merge Information");
570         output_dip_merge_ratio(ofp);
571
572         output_section_hdr(ofp, "Device Seek Information");
573         output_dip_seek_info(ofp);
574
575         output_section_hdr(ofp, "Plug Information");
576         output_plug_info(ofp);
577
578         output_histos();
579
580         return 0;
581 }
582
583 void __output_ranges(FILE *ofp, struct list_head *head_p, float base)
584 {
585         struct range_info *rip;
586         struct list_head *p;
587         float limit = base + 0.4;
588
589         __list_for_each(p, head_p) {
590                 rip = list_entry(p, struct range_info, head);
591                 fprintf(ofp, "%13.9lf %5.1f\n", BIT_TIME(rip->start), base);
592                 fprintf(ofp, "%13.9lf %5.1f\n", BIT_TIME(rip->start), limit);
593                 fprintf(ofp, "%13.9lf %5.1f\n", BIT_TIME(rip->end), limit);
594                 fprintf(ofp, "%13.9lf %5.1f\n", BIT_TIME(rip->end), base);
595         }
596 }
597
598 int output_regions(FILE *ofp, char *header, struct region_info *reg, 
599                           float base)
600 {
601         if (reg->qr_cur != NULL)
602                 list_add_tail(&reg->qr_cur->head, &reg->qranges);
603         if (reg->cr_cur != NULL)
604                 list_add_tail(&reg->cr_cur->head, &reg->cranges);
605
606         if (list_len(&reg->qranges) == 0 && list_len(&reg->cranges) == 0)
607                 return 0;
608
609         fprintf(ofp, "# %16s : q activity\n", header);
610         __output_ranges(ofp, &reg->qranges, base);
611         fprintf(ofp, "\n");
612
613         fprintf(ofp, "# %16s : c activity\n", header);
614         __output_ranges(ofp, &reg->cranges, base + 0.5);
615         fprintf(ofp, "\n");
616
617         return 1;
618 }
619
620 struct __od {
621         FILE *ofp;
622         float base;
623 };
624 void __output_dev(struct d_info *dip, void *arg)
625 {
626         char header[128];
627         struct __od *odp = arg;
628
629         sprintf(header, "%d,%d", MAJOR(dip->device), MINOR(dip->device));
630         if (output_regions(odp->ofp, header, &dip->regions, odp->base))
631                 odp->base += 1.0;
632 }
633
634 float output_devs(FILE *ofp, float base)
635 {
636         struct __od od = { .ofp = ofp, .base = base };
637
638         fprintf(ofp, "# Per device\n" );
639         dip_foreach_out(__output_dev, &od);
640         return od.base;
641 }
642
643 static inline int exe_match(char *exe, char *name)
644 {
645         return (exe == NULL) || (strstr(name, exe) != NULL);
646 }
647
648 struct __op {
649         FILE *ofp;
650         float base;
651 };
652 void __output_procs(struct p_info *pip, void *arg)
653 {
654         struct __op *opp = arg;
655         output_regions(opp->ofp, pip->name, &pip->regions, opp->base);
656         opp->base += 1.0;
657 }
658
659 float output_procs(FILE *ofp, float base)
660 {
661         struct __op op = { .ofp = ofp, .base = base };
662
663         fprintf(ofp, "# Per process\n" );
664         pip_foreach_out(__output_procs, &op);
665         return op.base;
666 }
667
668 int output_ranges(FILE *ofp)
669 {
670         float base = 0.0;
671
672         fprintf(ofp, "# %s\n", "Total System");
673         if (output_regions(ofp, "Total System", &all_regions, base))
674                 base += 1.0;
675
676         if (n_devs > 1)
677                 base = output_devs(ofp, base);
678
679         base = output_procs(ofp, base);
680
681         return 0;
682 }