Fixed plug/unplug logic in btt
[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 FILE *bno_dump_open(__u32 device, char rwc)
28 {
29         FILE *fp;
30         char *oname;
31         int mjr, mnr;
32
33         mjr = device >> MINORBITS;
34         mnr = device & ((1 << MINORBITS) - 1);
35
36         oname = malloc(strlen(bno_dump_name) + 32);
37         sprintf(oname, "%s_%03d,%03d_%c.dat", bno_dump_name, mjr, mnr, rwc);
38         if ((fp = my_fopen(oname, "w")) == NULL)
39                 perror(oname);
40         else
41                 add_file(fp, oname);
42         return fp;
43 }
44
45 static inline void bno_dump_write(FILE *fp, struct io *iop)
46 {
47         fprintf(fp, "%15.9lf %lld %lld\n", BIT_TIME(iop->t.time),
48                 (long long)BIT_START(iop), (long long)BIT_END(iop));
49 }
50
51 void *bno_dump_alloc(__u32 device)
52 {
53         struct bno_dump *bdp;
54
55         if (bno_dump_name == NULL) return NULL;
56
57         bdp = malloc(sizeof(*bdp));
58         bdp->rfp = bno_dump_open(device, 'r');
59         bdp->wfp = bno_dump_open(device, 'w');
60         bdp->cfp = bno_dump_open(device, 'c');
61
62         return bdp;
63 }
64
65 void bno_dump_free(void *param)
66 {
67         free(param);
68 }
69
70 void bno_dump_add(void *handle, struct io *iop)
71 {
72         struct bno_dump *bdp = handle;
73
74         if (bdp) {
75                 FILE *fp = IOP_READ(iop) ? bdp->rfp : bdp->wfp;
76
77                 if (fp)
78                         bno_dump_write(fp, iop);
79                 if (bdp->cfp)
80                         bno_dump_write(bdp->cfp, iop);
81         }
82 }