Added in running stats for btt
[blktrace.git] / btt / rstats.c
1 /*
2  * blktrace output analysis: generate a timeline & gather statistics
3  *
4  * (C) Copyright 2009 Hewlett-Packard Development Company, L.P.
5  *      Alan D. Brunelle (alan.brunelle@hp.com)
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22 #include "globals.h"
23
24 struct files {
25         FILE *fp;
26         char *nm;
27 };
28
29 struct rstat {
30         struct list_head head;
31         struct files files[2];
32         unsigned long long ios, nblks;
33         long long base_sec;
34 };
35
36 static struct rstat *sys_info;
37 static LIST_HEAD(rstats);
38
39 static int do_open(struct files *fip, char *bn, char *pn)
40 {
41         fip->nm = malloc(sizeof(bn) + 16);
42         sprintf(fip->nm, "%s_%s.dat", bn, pn);
43
44         fip->fp = my_fopen(fip->nm, "w");
45         if (fip->fp) {
46                 add_file(fip->fp, fip->nm);
47                 return 0;
48         }
49
50         free(fip->nm);
51         return -1;
52 }
53
54 static int init_rsip(struct rstat *rsip, char *bn)
55 {
56         rsip->ios = rsip->nblks = 0;
57         if (do_open(&rsip->files[0], bn, "iops_fp") ||
58                             do_open(&rsip->files[1], bn, "mbps_fp"))
59                 return -1;
60
61         list_add_tail(&rsip->head, &rstats);
62         return 0;
63 }
64
65 static void rstat_emit(struct rstat *rsip, double cur)
66 {
67         double mbps;
68
69         /*
70          * I/Os per second is easy: just the ios
71          */
72         fprintf(rsip->files[0].fp, "%lld %llu\n", rsip->base_sec, rsip->ios);
73
74         /*
75          * MB/s we convert blocks to mb...
76          */
77         mbps = ((double)rsip->nblks * 512.0) / (1024.0 * 1024.0);
78         fprintf(rsip->files[1].fp, "%lld %lf\n", rsip->base_sec, mbps);
79
80         rsip->base_sec = (unsigned long long)cur;
81         rsip->ios = rsip->nblks = 0;
82 }
83
84 static void __add(struct rstat *rsip, double cur, unsigned long long nblks)
85 {
86         if (rsip->base_sec < 0)
87                 rsip->base_sec = (long long)cur;
88         else if (((long long)cur - rsip->base_sec) >= 1)
89                 rstat_emit(rsip, cur);
90
91         rsip->ios++;
92         rsip->nblks += nblks;
93 }
94
95 void *rstat_alloc(char *bn)
96 {
97         struct rstat *rsip = malloc(sizeof(*rsip));
98
99         if (!init_rsip(rsip, bn))
100                 return rsip;
101
102         free(rsip);
103         return NULL;
104 }
105
106 void rstat_free(void *ptr)
107 {
108         struct rstat *rsip = ptr;
109
110         rstat_emit(rsip, last_t_seen);
111         list_del(&rsip->head);
112         free(rsip);
113 }
114
115 void rstat_add(void *ptr, double cur, unsigned long long nblks)
116 {
117         if (ptr != NULL)
118                 __add((struct rstat *)ptr, cur, nblks);
119         __add(sys_info, cur, nblks);
120 }
121
122 int rstat_init(void)
123 {
124         sys_info = rstat_alloc("sys");
125         return sys_info != NULL;
126 }
127
128 void rstat_exit(void)
129 {
130         struct list_head *p, *q;
131
132         list_for_each_safe(p, q, &rstats) {
133                 struct rstat *rsip = list_entry(p, struct rstat, head);
134                 rstat_free(rsip);
135         }
136 }