blktrace support: fixups/cleanups/improvements
[fio.git] / blktrace.c
CommitLineData
fb7b71a3
JA
1/*
2 * blktrace support code for fio
3 */
4#include <stdio.h>
5#include <stdlib.h>
8c1fdf04 6
fb7b71a3
JA
7#include "list.h"
8#include "fio.h"
9#include "blktrace_api.h"
10
f12b323f 11#define TRACE_FIFO_SIZE 65536
e2887563
JA
12
13/*
14 * fifo refill frontend, to avoid reading data in trace sized bites
15 */
16static int refill_fifo(struct thread_data *td, struct fifo *fifo, int fd)
17{
18 char buf[TRACE_FIFO_SIZE];
f12b323f 19 unsigned int total;
e2887563
JA
20 int ret;
21
f12b323f
JA
22 total = sizeof(buf);
23 if (total > fifo_room(fifo))
24 total = fifo_room(fifo);
e2887563 25
f12b323f
JA
26 ret = read(fd, buf, total);
27 if (ret < 0) {
28 td_verror(td, errno, "read blktrace file");
29 return -1;
e2887563
JA
30 }
31
f12b323f
JA
32 if (ret > 0)
33 ret = fifo_put(fifo, buf, ret);
34
35 return ret;
e2887563
JA
36}
37
38/*
39 * Retrieve 'len' bytes from the fifo, refilling if necessary.
40 */
41static int trace_fifo_get(struct thread_data *td, struct fifo *fifo, int fd,
42 void *buf, unsigned int len)
43{
f12b323f
JA
44 if (fifo_len(fifo) < len) {
45 int ret = refill_fifo(td, fifo, fd);
e2887563 46
f12b323f
JA
47 if (ret < 0)
48 return ret;
49 }
e2887563
JA
50
51 return fifo_get(fifo, buf, len);
52}
53
8c1fdf04
JA
54/*
55 * Just discard the pdu by seeking past it.
56 */
f12b323f
JA
57static int discard_pdu(struct thread_data *td, struct fifo *fifo, int fd,
58 struct blk_io_trace *t)
fb7b71a3
JA
59{
60 if (t->pdu_len == 0)
61 return 0;
62
f12b323f 63 return trace_fifo_get(td, fifo, fd, NULL, t->pdu_len);
fb7b71a3
JA
64}
65
8c1fdf04
JA
66/*
67 * Check if this is a blktrace binary data file. We read a single trace
68 * into memory and check for the magic signature.
69 */
fb7b71a3
JA
70int is_blktrace(const char *filename)
71{
72 struct blk_io_trace t;
73 int fd, ret;
74
75 fd = open(filename, O_RDONLY);
76 if (fd < 0) {
77 perror("open blktrace");
78 return 0;
79 }
80
81 ret = read(fd, &t, sizeof(t));
82 close(fd);
83
84 if (ret < 0) {
85 perror("read blktrace");
86 return 0;
87 } else if (ret != sizeof(t)) {
88 log_err("fio: short read on blktrace file\n");
89 return 0;
90 }
91
92 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
93 return 1;
94
95 return 0;
96}
97
8c1fdf04
JA
98/*
99 * Store blk_io_trace data in an ipo for later retrieval.
100 */
fdefd987 101static void store_ipo(struct thread_data *td, unsigned long long offset,
8c1fdf04 102 unsigned int bytes, int rw, unsigned long long ttime)
fdefd987
JA
103{
104 struct io_piece *ipo = malloc(sizeof(*ipo));
105
106 memset(ipo, 0, sizeof(*ipo));
107 INIT_LIST_HEAD(&ipo->list);
a2eea81b
JA
108 /*
109 * the 512 is wrong here, it should be the hardware sector size...
110 */
111 ipo->offset = offset * 512;
fdefd987 112 ipo->len = bytes;
8c1fdf04 113 ipo->delay = ttime / 1000;
fdefd987
JA
114 if (rw)
115 ipo->ddir = DDIR_WRITE;
116 else
117 ipo->ddir = DDIR_READ;
118
119 list_add_tail(&ipo->list, &td->io_log_list);
120}
121
8c1fdf04
JA
122/*
123 * We only care for queue traces, most of the others are side effects
124 * due to internal workings of the block layer.
125 */
126static void handle_trace(struct thread_data *td, struct blk_io_trace *t,
d84f8d49
JA
127 unsigned long long ttime, unsigned long *ios,
128 unsigned int *bs)
fb7b71a3 129{
fdefd987
JA
130 int rw;
131
132 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
133 return;
a2eea81b
JA
134 if (t->action & BLK_TC_ACT(BLK_TC_PC))
135 return;
136
137 /*
138 * should not happen, need to look into that...
139 */
140 if (!t->bytes)
141 return;
fdefd987 142
e7a7d70b 143 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
d84f8d49
JA
144
145 if (t->bytes > bs[rw])
146 bs[rw] = t->bytes;
147
8c1fdf04 148 ios[rw]++;
6df8adaa 149 td->o.size += t->bytes;
8c1fdf04 150 store_ipo(td, t->sector, t->bytes, rw, ttime);
fb7b71a3
JA
151}
152
8c1fdf04
JA
153/*
154 * Load a blktrace file by reading all the blk_io_trace entries, and storing
155 * them as io_pieces like the fio text version would do.
156 */
fb7b71a3
JA
157int load_blktrace(struct thread_data *td, const char *filename)
158{
a61eddec 159 unsigned long long ttime, delay;
fb7b71a3 160 struct blk_io_trace t;
8c1fdf04 161 unsigned long ios[2];
a61eddec 162 unsigned int cpu;
d84f8d49 163 unsigned int rw_bs[2];
e2887563 164 struct fifo *fifo;
fb7b71a3
JA
165 int fd;
166
167 fd = open(filename, O_RDONLY);
168 if (fd < 0) {
169 td_verror(td, errno, "open blktrace file");
170 return 1;
171 }
172
e2887563
JA
173 fifo = fifo_alloc(TRACE_FIFO_SIZE);
174
6df8adaa
JA
175 td->o.size = 0;
176
a61eddec 177 cpu = 0;
d84f8d49
JA
178 ttime = 0;
179 ios[0] = ios[1] = 0;
180 rw_bs[0] = rw_bs[1] = 0;
fb7b71a3 181 do {
e2887563 182 int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t));
fb7b71a3 183
e2887563 184 if (ret < 0)
8c1fdf04 185 goto err;
e2887563
JA
186 else if (!ret)
187 break;
188 else if (ret < (int) sizeof(t)) {
189 log_err("fio: short fifo get\n");
fb7b71a3 190 break;
fb7b71a3
JA
191 }
192
193 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
f12b323f 194 log_err("fio: bad magic in blktrace data: %x\n", t.magic);
8c1fdf04 195 goto err;
fb7b71a3
JA
196 }
197 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
198 log_err("fio: bad blktrace version %d\n", t.magic & 0xff);
8c1fdf04 199 goto err;
fb7b71a3 200 }
f12b323f
JA
201 ret = discard_pdu(td, fifo, fd, &t);
202 if (ret < 0) {
fb7b71a3 203 td_verror(td, ret, "blktrace lseek");
8c1fdf04 204 goto err;
f12b323f
JA
205 } else if (t.pdu_len != ret) {
206 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
207 goto err;
fb7b71a3 208 }
a61eddec 209 if (!ttime) {
8c1fdf04 210 ttime = t.time;
a61eddec
JA
211 cpu = t.cpu;
212 }
213 delay = 0;
214 if (cpu == t.cpu)
215 delay = t.time - ttime;
d84f8d49 216 handle_trace(td, &t, delay, ios, rw_bs);
8c1fdf04 217 ttime = t.time;
a61eddec 218 cpu = t.cpu;
fb7b71a3
JA
219 } while (1);
220
38470f85 221 fifo_free(fifo);
fb7b71a3 222 close(fd);
8c1fdf04
JA
223
224 if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) {
225 log_err("fio: found no ios in blktrace data\n");
226 return 1;
d84f8d49 227 } else if (ios[DDIR_READ] && !ios[DDIR_READ]) {
8c1fdf04 228 td->o.td_ddir = TD_DDIR_READ;
d84f8d49
JA
229 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
230 } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) {
8c1fdf04 231 td->o.td_ddir = TD_DDIR_WRITE;
d84f8d49
JA
232 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
233 } else {
8c1fdf04 234 td->o.td_ddir = TD_DDIR_RW;
d84f8d49
JA
235 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
236 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
237 }
8c1fdf04
JA
238
239 /*
240 * We need to do direct/raw ios to the device, to avoid getting
241 * read-ahead in our way.
242 */
243 td->o.odirect = 1;
244
fb7b71a3 245 return 0;
8c1fdf04
JA
246err:
247 close(fd);
38470f85 248 fifo_free(fifo);
8c1fdf04 249 return 1;
fb7b71a3 250}