7f7680e6f319fa6147dcb4f42eb1694ded3fd627
[blktrace.git] / btt / bno_dump.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 "globals.h"
22
23 struct bno_dump {
24         FILE *rfp, *wfp, *cfp;
25 };
26
27 static struct file_info *bno_dump_files = NULL;
28
29 static FILE *bno_dump_open(__u32 device, char rwc)
30 {
31         FILE *fp;
32         char *oname;
33         int mjr, mnr;
34
35         mjr = device >> MINORBITS;
36         mnr = device & ((1 << MINORBITS) - 1);
37
38         oname = malloc(strlen(bno_dump_name) + 32);
39         sprintf(oname, "%s_%03d,%03d_%c.dat", bno_dump_name, mjr, mnr, rwc);
40         if ((fp = my_fopen(oname, "w")) == NULL)
41                 perror(oname);
42         else
43                 add_file(&bno_dump_files, fp, oname);
44         return fp;
45 }
46
47 void *bno_dump_init(__u32 device)
48 {
49         struct bno_dump *bdp;
50
51         if (bno_dump_name == NULL) return NULL;
52
53         bdp = malloc(sizeof(*bdp));
54         bdp->rfp = bno_dump_open(device, 'r');
55         bdp->wfp = bno_dump_open(device, 'w');
56         bdp->cfp = bno_dump_open(device, 'c');
57
58         return bdp;
59 }
60
61 void bno_dump_exit(void *param)
62 {
63         /*
64          * Associated files will be auto-cleaned by bno_dump_clean
65          */
66         free(param);
67 }
68
69 static inline void bno_dump_write(FILE *fp, struct io *iop)
70 {
71         fprintf(fp, "%15.9lf %lld %lld\n",
72                 BIT_TIME(iop->t.time),
73                 (long long)BIT_START(iop), (long long)BIT_END(iop));
74 }
75
76 void bno_dump_add(void *handle, struct io *iop)
77 {
78 #       define RW_FP(bdp, iop)  (IOP_READ(iop) ? bdp->rfp : bdp->wfp)
79         struct bno_dump *bdp = handle;
80
81         if (bdp) {
82                 if (RW_FP(bdp, iop))
83                         bno_dump_write(RW_FP(bdp, iop), iop);
84                 if (bdp->cfp)
85                         bno_dump_write(bdp->cfp, iop);
86         }
87 }
88
89 void bno_dump_clean(void)
90 {
91         clean_files(&bno_dump_files);
92 }