Fix btt to handle large numbers of output files
[blktrace.git] / btt / aqd.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 <stdlib.h>
23 #include <string.h>
24
25 #include "globals.h"
26
27 static struct file_info *aqd_files = NULL;
28
29 struct aqd_info {
30         FILE *fp;
31         int na;         /* # active */
32 };
33
34 void *aqd_init(char *str)
35 {
36         char *oname;
37         struct aqd_info *ap;
38
39         if (aqd_name == NULL) return NULL;
40
41         ap = malloc(sizeof(*ap));
42         ap->na = 0;
43
44         oname = malloc(strlen(aqd_name) + strlen(str) + 32);
45         sprintf(oname, "%s_%s.dat", aqd_name, str);
46         if ((ap->fp = my_fopen(oname, "w")) == NULL) {
47                 perror(oname);
48                 return NULL;
49         }
50         add_file(&aqd_files, ap->fp, oname);
51
52         return ap;
53
54 }
55
56 void aqd_exit(void *info)
57 {
58         free(info);
59 }
60
61 void aqd_clean(void)
62 {
63         clean_files(&aqd_files);
64 }
65
66 void aqd_issue(void *info, double ts)
67 {
68         if (info) {
69                 struct aqd_info *ap = info;
70
71                 fprintf(ap->fp, "%lf %d\n%lf %d\n", ts, ap->na, ts, ap->na + 1);
72                 ap->na += 1;
73         }
74 }
75
76 void aqd_complete(void *info, double ts)
77 {
78         if (info) {
79                 struct aqd_info *ap = info;
80
81                 if (ap->na > 0) {
82                         fprintf(ap->fp, "%lf %d\n%lf %d\n",
83                                         ts, ap->na, ts, ap->na - 1);
84                         ap->na -= 1;
85                 }
86         }
87 }