Merge branch 'expression-parser'
[fio.git] / blktrace.c
CommitLineData
fb7b71a3
JA
1/*
2 * blktrace support code for fio
3 */
4#include <stdio.h>
5#include <stdlib.h>
5e6c2067
JA
6#include <sys/stat.h>
7#include <dirent.h>
8c1fdf04 8
01743ee1 9#include "flist.h"
fb7b71a3
JA
10#include "fio.h"
11#include "blktrace_api.h"
40bafa33 12#include "lib/linux-dev-lookup.h"
fb7b71a3 13
2da7df1d 14#define TRACE_FIFO_SIZE 8192
e2887563
JA
15
16/*
17 * fifo refill frontend, to avoid reading data in trace sized bites
18 */
19static int refill_fifo(struct thread_data *td, struct fifo *fifo, int fd)
20{
21 char buf[TRACE_FIFO_SIZE];
f12b323f 22 unsigned int total;
e2887563
JA
23 int ret;
24
f12b323f
JA
25 total = sizeof(buf);
26 if (total > fifo_room(fifo))
27 total = fifo_room(fifo);
e2887563 28
f12b323f
JA
29 ret = read(fd, buf, total);
30 if (ret < 0) {
31 td_verror(td, errno, "read blktrace file");
32 return -1;
e2887563
JA
33 }
34
f12b323f
JA
35 if (ret > 0)
36 ret = fifo_put(fifo, buf, ret);
37
bd6f78b2 38 dprint(FD_BLKTRACE, "refill: filled %d bytes\n", ret);
f12b323f 39 return ret;
e2887563
JA
40}
41
42/*
43 * Retrieve 'len' bytes from the fifo, refilling if necessary.
44 */
45static int trace_fifo_get(struct thread_data *td, struct fifo *fifo, int fd,
46 void *buf, unsigned int len)
47{
f12b323f
JA
48 if (fifo_len(fifo) < len) {
49 int ret = refill_fifo(td, fifo, fd);
e2887563 50
f12b323f
JA
51 if (ret < 0)
52 return ret;
53 }
e2887563
JA
54
55 return fifo_get(fifo, buf, len);
56}
57
8c1fdf04
JA
58/*
59 * Just discard the pdu by seeking past it.
60 */
f12b323f
JA
61static int discard_pdu(struct thread_data *td, struct fifo *fifo, int fd,
62 struct blk_io_trace *t)
fb7b71a3
JA
63{
64 if (t->pdu_len == 0)
65 return 0;
66
bd6f78b2 67 dprint(FD_BLKTRACE, "discard pdu len %u\n", t->pdu_len);
f12b323f 68 return trace_fifo_get(td, fifo, fd, NULL, t->pdu_len);
fb7b71a3
JA
69}
70
8c1fdf04
JA
71/*
72 * Check if this is a blktrace binary data file. We read a single trace
73 * into memory and check for the magic signature.
74 */
d95b34a6 75int is_blktrace(const char *filename, int *need_swap)
fb7b71a3
JA
76{
77 struct blk_io_trace t;
78 int fd, ret;
79
80 fd = open(filename, O_RDONLY);
4dced407 81 if (fd < 0)
fb7b71a3 82 return 0;
fb7b71a3
JA
83
84 ret = read(fd, &t, sizeof(t));
85 close(fd);
86
87 if (ret < 0) {
88 perror("read blktrace");
89 return 0;
90 } else if (ret != sizeof(t)) {
91 log_err("fio: short read on blktrace file\n");
92 return 0;
93 }
94
d95b34a6
JA
95 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
96 *need_swap = 0;
fb7b71a3 97 return 1;
d95b34a6
JA
98 }
99
100 /*
101 * Maybe it needs to be endian swapped...
102 */
103 t.magic = fio_swap32(t.magic);
104 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
105 *need_swap = 1;
106 return 1;
107 }
fb7b71a3
JA
108
109 return 0;
110}
111
c69aa91f
JA
112#define FMINORBITS 20
113#define FMINORMASK ((1U << FMINORBITS) - 1)
114#define FMAJOR(dev) ((unsigned int) ((dev) >> FMINORBITS))
115#define FMINOR(dev) ((unsigned int) ((dev) & FMINORMASK))
eeb9c2aa 116
89ac1d48 117static void trace_add_open_close_event(struct thread_data *td, int fileno, enum file_log_act action)
691c8fb0
JA
118{
119 struct io_piece *ipo;
120
121 ipo = calloc(1, sizeof(*ipo));
0d29de83 122 init_ipo(ipo);
691c8fb0
JA
123
124 ipo->ddir = DDIR_INVAL;
125 ipo->fileno = fileno;
89ac1d48 126 ipo->file_action = action;
01743ee1 127 flist_add_tail(&ipo->list, &td->io_log_list);
691c8fb0
JA
128}
129
89ac1d48 130static int trace_add_file(struct thread_data *td, __u32 device)
5e6c2067 131{
89ac1d48 132 static unsigned int last_maj, last_min, last_fileno;
c69aa91f
JA
133 unsigned int maj = FMAJOR(device);
134 unsigned int min = FMINOR(device);
5e6c2067
JA
135 struct fio_file *f;
136 char dev[256];
137 unsigned int i;
138
139 if (last_maj == maj && last_min == min)
89ac1d48 140 return last_fileno;
5e6c2067
JA
141
142 last_maj = maj;
143 last_min = min;
144
145 /*
146 * check for this file in our list
147 */
148 for_each_file(td, f, i)
89ac1d48
SL
149 if (f->major == maj && f->minor == min) {
150 last_fileno = f->fileno;
151 return last_fileno;
152 }
5e6c2067
JA
153
154 strcpy(dev, "/dev");
40bafa33 155 if (blktrace_lookup_device(td->o.replay_redirect, dev, maj, min)) {
691c8fb0
JA
156 int fileno;
157
40bafa33
JA
158 if (td->o.replay_redirect)
159 dprint(FD_BLKTRACE, "device lookup: %d/%d\n overridden"
160 " with: %s\n", maj, min,
161 td->o.replay_redirect);
162 else
163 dprint(FD_BLKTRACE, "device lookup: %d/%d\n", maj, min);
164
bd6f78b2 165 dprint(FD_BLKTRACE, "add devices %s\n", dev);
49ffb4a2 166 fileno = add_file_exclusive(td, dev);
b53f2c54 167 td->o.open_files++;
5903e7b7
JA
168 td->files[fileno]->major = maj;
169 td->files[fileno]->minor = min;
89ac1d48
SL
170 trace_add_open_close_event(td, fileno, FIO_LOG_OPEN_FILE);
171 last_fileno = fileno;
bd6f78b2 172 }
f01b34ae 173
89ac1d48 174 return last_fileno;
5e6c2067
JA
175}
176
8c1fdf04
JA
177/*
178 * Store blk_io_trace data in an ipo for later retrieval.
179 */
fdefd987 180static void store_ipo(struct thread_data *td, unsigned long long offset,
89ac1d48
SL
181 unsigned int bytes, int rw, unsigned long long ttime,
182 int fileno)
fdefd987
JA
183{
184 struct io_piece *ipo = malloc(sizeof(*ipo));
185
0d29de83
JA
186 init_ipo(ipo);
187
a2eea81b
JA
188 /*
189 * the 512 is wrong here, it should be the hardware sector size...
190 */
191 ipo->offset = offset * 512;
fdefd987 192 ipo->len = bytes;
8c1fdf04 193 ipo->delay = ttime / 1000;
fdefd987
JA
194 if (rw)
195 ipo->ddir = DDIR_WRITE;
196 else
197 ipo->ddir = DDIR_READ;
89ac1d48 198 ipo->fileno = fileno;
fdefd987 199
bd6f78b2
JA
200 dprint(FD_BLKTRACE, "store ddir=%d, off=%llu, len=%lu, delay=%lu\n",
201 ipo->ddir, ipo->offset,
202 ipo->len, ipo->delay);
691c8fb0 203 queue_io_piece(td, ipo);
fdefd987
JA
204}
205
0b9d69ec 206static void handle_trace_notify(struct blk_io_trace *t)
cd991b9e 207{
691c8fb0
JA
208 switch (t->action) {
209 case BLK_TN_PROCESS:
24653680 210 dprint(FD_BLKTRACE, "got process notify: %x, %d\n",
d95b34a6 211 t->action, t->pid);
691c8fb0
JA
212 break;
213 case BLK_TN_TIMESTAMP:
24653680 214 dprint(FD_BLKTRACE, "got timestamp notify: %x, %d\n",
d95b34a6 215 t->action, t->pid);
691c8fb0 216 break;
ff58fced
JA
217 case BLK_TN_MESSAGE:
218 break;
691c8fb0
JA
219 default:
220 dprint(FD_BLKTRACE, "unknown trace act %x\n", t->action);
221 break;
222 }
223}
5b3023b8 224
24653680
JA
225static void handle_trace_discard(struct thread_data *td,
226 struct blk_io_trace *t,
227 unsigned long long ttime,
228 unsigned long *ios, unsigned int *bs)
ff58fced
JA
229{
230 struct io_piece *ipo = malloc(sizeof(*ipo));
89ac1d48 231 int fileno;
ff58fced 232
0d29de83 233 init_ipo(ipo);
89ac1d48 234 fileno = trace_add_file(td, t->device);
ff58fced 235
24653680
JA
236 ios[DDIR_TRIM]++;
237 if (t->bytes > bs[DDIR_TRIM])
238 bs[DDIR_TRIM] = t->bytes;
239
ff58fced
JA
240 td->o.size += t->bytes;
241
242 memset(ipo, 0, sizeof(*ipo));
243 INIT_FLIST_HEAD(&ipo->list);
244
245 /*
246 * the 512 is wrong here, it should be the hardware sector size...
247 */
248 ipo->offset = t->sector * 512;
249 ipo->len = t->bytes;
250 ipo->delay = ttime / 1000;
251 ipo->ddir = DDIR_TRIM;
89ac1d48 252 ipo->fileno = fileno;
ff58fced
JA
253
254 dprint(FD_BLKTRACE, "store discard, off=%llu, len=%lu, delay=%lu\n",
255 ipo->offset, ipo->len,
256 ipo->delay);
257 queue_io_piece(td, ipo);
258}
259
691c8fb0
JA
260static void handle_trace_fs(struct thread_data *td, struct blk_io_trace *t,
261 unsigned long long ttime, unsigned long *ios,
262 unsigned int *bs)
263{
264 int rw;
89ac1d48 265 int fileno;
5b3023b8 266
89ac1d48 267 fileno = trace_add_file(td, t->device);
5b3023b8
JA
268
269 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
270
271 if (t->bytes > bs[rw])
272 bs[rw] = t->bytes;
273
274 ios[rw]++;
275 td->o.size += t->bytes;
89ac1d48 276 store_ipo(td, t->sector, t->bytes, rw, ttime, fileno);
cd991b9e
JA
277}
278
691c8fb0
JA
279/*
280 * We only care for queue traces, most of the others are side effects
281 * due to internal workings of the block layer.
282 */
283static void handle_trace(struct thread_data *td, struct blk_io_trace *t,
24653680 284 unsigned long *ios, unsigned int *bs)
691c8fb0 285{
24653680
JA
286 static unsigned long long last_ttime;
287 unsigned long long delay;
288
691c8fb0
JA
289 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
290 return;
24653680
JA
291
292 if (!(t->action & BLK_TC_ACT(BLK_TC_NOTIFY))) {
293 if (!last_ttime || td->o.no_stall) {
294 last_ttime = t->time;
295 delay = 0;
296 } else {
297 delay = t->time - last_ttime;
298 last_ttime = t->time;
299 }
300 }
691c8fb0
JA
301
302 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
aec2de20 303 handle_trace_notify(t);
ff58fced 304 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
24653680 305 handle_trace_discard(td, t, delay, ios, bs);
691c8fb0 306 else
24653680 307 handle_trace_fs(td, t, delay, ios, bs);
691c8fb0
JA
308}
309
d95b34a6
JA
310static void byteswap_trace(struct blk_io_trace *t)
311{
312 t->magic = fio_swap32(t->magic);
313 t->sequence = fio_swap32(t->sequence);
314 t->time = fio_swap64(t->time);
315 t->sector = fio_swap64(t->sector);
316 t->bytes = fio_swap32(t->bytes);
317 t->action = fio_swap32(t->action);
318 t->pid = fio_swap32(t->pid);
319 t->device = fio_swap32(t->device);
320 t->cpu = fio_swap32(t->cpu);
321 t->error = fio_swap16(t->error);
322 t->pdu_len = fio_swap16(t->pdu_len);
323}
324
24653680
JA
325static int t_is_write(struct blk_io_trace *t)
326{
327 return (t->action & BLK_TC_ACT(BLK_TC_WRITE | BLK_TC_DISCARD)) != 0;
328}
329
8c1fdf04
JA
330/*
331 * Load a blktrace file by reading all the blk_io_trace entries, and storing
332 * them as io_pieces like the fio text version would do.
333 */
d95b34a6 334int load_blktrace(struct thread_data *td, const char *filename, int need_swap)
fb7b71a3
JA
335{
336 struct blk_io_trace t;
24653680
JA
337 unsigned long ios[DDIR_RWDIR_CNT], skipped_writes;
338 unsigned int rw_bs[DDIR_RWDIR_CNT];
e2887563 339 struct fifo *fifo;
5903e7b7 340 int fd, i, old_state;
89ac1d48 341 struct fio_file *f;
eb5fdcf1 342 int this_depth, depth;
fb7b71a3
JA
343
344 fd = open(filename, O_RDONLY);
345 if (fd < 0) {
346 td_verror(td, errno, "open blktrace file");
347 return 1;
348 }
349
e2887563
JA
350 fifo = fifo_alloc(TRACE_FIFO_SIZE);
351
8edd973d 352 old_state = td_bump_runstate(td, TD_SETTING_UP);
5903e7b7 353
6df8adaa
JA
354 td->o.size = 0;
355
d84f8d49
JA
356 ios[0] = ios[1] = 0;
357 rw_bs[0] = rw_bs[1] = 0;
4241ea8f 358 skipped_writes = 0;
eb5fdcf1 359 this_depth = depth = 0;
fb7b71a3 360 do {
e2887563 361 int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t));
fb7b71a3 362
e2887563 363 if (ret < 0)
8c1fdf04 364 goto err;
e2887563
JA
365 else if (!ret)
366 break;
367 else if (ret < (int) sizeof(t)) {
368 log_err("fio: short fifo get\n");
fb7b71a3 369 break;
fb7b71a3
JA
370 }
371
d95b34a6
JA
372 if (need_swap)
373 byteswap_trace(&t);
374
fb7b71a3 375 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
5ec10eaa
JA
376 log_err("fio: bad magic in blktrace data: %x\n",
377 t.magic);
8c1fdf04 378 goto err;
fb7b71a3
JA
379 }
380 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
5ec10eaa
JA
381 log_err("fio: bad blktrace version %d\n",
382 t.magic & 0xff);
8c1fdf04 383 goto err;
fb7b71a3 384 }
f12b323f
JA
385 ret = discard_pdu(td, fifo, fd, &t);
386 if (ret < 0) {
fb7b71a3 387 td_verror(td, ret, "blktrace lseek");
8c1fdf04 388 goto err;
f12b323f
JA
389 } else if (t.pdu_len != ret) {
390 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
391 goto err;
fb7b71a3 392 }
691c8fb0 393 if ((t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) == 0) {
eb5fdcf1
JA
394 if ((t.action & 0xffff) == __BLK_TA_QUEUE)
395 this_depth++;
396 else if ((t.action & 0xffff) == __BLK_TA_COMPLETE) {
397 depth = max(depth, this_depth);
398 this_depth = 0;
399 }
691c8fb0 400
24653680 401 if (t_is_write(&t) && read_only) {
691c8fb0 402 skipped_writes++;
24653680 403 continue;
64bbb865 404 }
a6edd638 405 }
24653680
JA
406
407 handle_trace(td, &t, ios, rw_bs);
fb7b71a3
JA
408 } while (1);
409
85a47ca2 410 for (i = 0; i < td->files_index; i++) {
f01b34ae 411 f = td->files[i];
89ac1d48 412 trace_add_open_close_event(td, f->fileno, FIO_LOG_CLOSE_FILE);
85a47ca2 413 }
89ac1d48 414
38470f85 415 fifo_free(fifo);
fb7b71a3 416 close(fd);
8c1fdf04 417
8edd973d 418 td_restore_runstate(td, old_state);
5903e7b7 419
f01b34ae
JA
420 if (!td->files_index) {
421 log_err("fio: did not find replay device(s)\n");
422 return 1;
423 }
424
eb5fdcf1
JA
425 /*
426 * For stacked devices, we don't always get a COMPLETE event so
427 * the depth grows to insane values. Limit it to something sane(r).
428 */
429 if (!depth || depth > 1024)
430 depth = 1024;
431
4241ea8f 432 if (skipped_writes)
5ec10eaa
JA
433 log_err("fio: %s skips replay of %lu writes due to read-only\n",
434 td->o.name, skipped_writes);
4241ea8f 435
8c1fdf04
JA
436 if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) {
437 log_err("fio: found no ios in blktrace data\n");
438 return 1;
24653680 439 } else if (ios[DDIR_READ] && !ios[DDIR_WRITE]) {
8c1fdf04 440 td->o.td_ddir = TD_DDIR_READ;
d84f8d49
JA
441 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
442 } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) {
8c1fdf04 443 td->o.td_ddir = TD_DDIR_WRITE;
d84f8d49
JA
444 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
445 } else {
8c1fdf04 446 td->o.td_ddir = TD_DDIR_RW;
d84f8d49
JA
447 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
448 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
24653680 449 td->o.max_bs[DDIR_TRIM] = rw_bs[DDIR_TRIM];
d84f8d49 450 }
8c1fdf04
JA
451
452 /*
453 * We need to do direct/raw ios to the device, to avoid getting
454 * read-ahead in our way.
455 */
456 td->o.odirect = 1;
457
eb5fdcf1
JA
458 /*
459 * we don't know if this option was set or not. it defaults to 1,
460 * so we'll just guess that we should override it if it's still 1
461 */
462 if (td->o.iodepth != 1)
463 td->o.iodepth = depth;
464
fb7b71a3 465 return 0;
8c1fdf04
JA
466err:
467 close(fd);
38470f85 468 fifo_free(fifo);
8c1fdf04 469 return 1;
fb7b71a3 470}