2 * Copyright (C) 2013 Fusion-io
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 #include <sys/types.h>
26 #include <asm/types.h>
38 static int past_eof(struct trace *trace, char *cur)
40 if (cur >= trace->fio_start + trace->fio_len)
45 static int parse_fio_line(struct trace *trace, int *time, int *rate, int *dir, int *bs)
47 char *cur = trace->fio_cur;
49 int *res[] = { time, rate, dir, bs, NULL };
53 char *end = index(cur, '\n');
59 tmp = strndup(cur, end - cur);
67 p = strtok(NULL, ",");
77 int next_fio_line(struct trace *trace)
80 char *cur = trace->fio_cur;
82 next = strchr(cur, '\n');
86 if (past_eof(trace, next))
88 trace->fio_cur = next;
92 char *first_fio(struct trace *trace)
94 trace->fio_cur = trace->fio_start;
95 return trace->fio_cur;
98 static void find_last_fio_time(struct trace *trace)
101 int time, rate, dir, bs;
105 if (trace->fio_len == 0)
110 ret = parse_fio_line(trace, &time, &rate, &dir, &bs);
113 if (dir <= 1 && time > last_time)
115 ret = next_fio_line(trace);
119 d = (double)time / 1000;
120 trace->fio_seconds = ceil(d);
124 int read_fio(struct trace *trace, char *trace_name)
131 fd = open(trace_name, O_RDONLY);
135 ret = fstat(fd, &st);
137 fprintf(stderr, "stat failed on %s err %s\n",
138 trace_name, strerror(errno));
141 p = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
142 if (p == MAP_FAILED) {
143 fprintf(stderr, "Unable to mmap trace file %s, err %s\n",
144 trace_name, strerror(errno));
147 trace->fio_start = p;
148 trace->fio_len = st.st_size;
151 find_last_fio_time(trace);
160 struct trace *open_fio_trace(char *path)
165 trace = calloc(1, sizeof(*trace));
167 fprintf(stderr, "unable to allocate memory for trace\n");
171 ret = read_fio(trace, path);
180 int read_fio_event(struct trace *trace, int *time_ret, u64 *bw_ret, int *dir_ret)
182 char *cur = trace->fio_cur;
183 int time, rate, dir, bs;
186 if (past_eof(trace, cur))
189 ret = parse_fio_line(trace, &time, &rate, &dir, &bs);
193 time = floor((double)time / 1000);
195 *bw_ret = (u64)rate * 1024;
201 int add_fio_gld(int time, u64 bw, struct graph_line_data *gld)
205 if (time > gld->max_seconds)
208 gld->data[time].sum += bw;
209 gld->data[time].count++;
211 val = ((double)gld->data[time].sum) / gld->data[time].count;
214 gld->max = ceil(val);