1/*
2 * blktrace output analysis: generate a timeline & gather statistics
3 *
4 * (C) Copyright 2008 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
24struct plat_info {
25	long nl;
26	FILE *fp;
27	double first_ts, last_ts, tl;
28};
29
30void *plat_alloc(char *str)
31{
32	char *oname;
33	struct plat_info *pp;
34
35	if (plat_freq <= 0.0) return NULL;
36
37	pp = malloc(sizeof(*pp));
38	pp->nl = 0;
39	pp->first_ts = pp->last_ts = pp->tl = -1.0;
40
41	oname = malloc(strlen(str) + 32);
42	sprintf(oname, "%s.dat", str);
43	if ((pp->fp = my_fopen(oname, "w")) == NULL) {
44		perror(oname);
45		return NULL;
46	}
47	add_file(pp->fp, oname);
48
49	return pp;
50}
51
52void plat_free(void *info)
53{
54	struct plat_info *pp = info;
55
56	if (pp == NULL) return;
57
58	if (pp->first_ts != -1.0) {
59		double delta = pp->last_ts - pp->first_ts;
60
61		fprintf(pp->fp, "%lf %lf\n",
62			pp->first_ts + (delta / 2), pp->tl / pp->nl);
63	}
64	free(info);
65}
66
67void plat_x2c(void *info, __u64 ts, __u64 latency)
68{
69	double now = TO_SEC(ts);
70	double lat = TO_SEC(latency);
71	struct plat_info *pp = info;
72
73	if (pp == NULL) return;
74
75	if (pp->first_ts == -1.0) {
76		pp->first_ts = pp->last_ts = now;
77		pp->nl = 1;
78		pp->tl = lat;
79	} else if ((now - pp->first_ts) >= plat_freq) {
80		double delta = pp->last_ts - pp->first_ts;
81
82		fprintf(pp->fp, "%lf %lf\n",
83			pp->first_ts + (delta / 2), pp->tl / pp->nl);
84
85		pp->first_ts = pp->last_ts = now;
86		pp->nl = 1;
87		pp->tl = lat;
88	} else {
89		pp->last_ts = now;
90		pp->nl += 1;
91		pp->tl += lat;
92	}
93}
94