blktrace.c: Use file stream interface instead of fifo
[fio.git] / blktrace.c
CommitLineData
fb7b71a3
JA
1/*
2 * blktrace support code for fio
3 */
4#include <stdio.h>
5#include <stdlib.h>
87a48ada 6#include <unistd.h>
5ab088aa 7#include <errno.h>
8c1fdf04 8
01743ee1 9#include "flist.h"
fb7b71a3 10#include "fio.h"
a3e59412 11#include "blktrace.h"
fb7b71a3 12#include "blktrace_api.h"
984f30c9 13#include "oslib/linux-dev-lookup.h"
fb7b71a3 14
8c1fdf04
JA
15/*
16 * Just discard the pdu by seeking past it.
17 */
5ab088aa 18static int discard_pdu(FILE* f, struct blk_io_trace *t)
fb7b71a3
JA
19{
20 if (t->pdu_len == 0)
21 return 0;
22
bd6f78b2 23 dprint(FD_BLKTRACE, "discard pdu len %u\n", t->pdu_len);
5ab088aa
LS
24 if (fseek(f, t->pdu_len, SEEK_CUR) < 0)
25 return -errno;
26
27 return t->pdu_len;
fb7b71a3
JA
28}
29
8c1fdf04
JA
30/*
31 * Check if this is a blktrace binary data file. We read a single trace
32 * into memory and check for the magic signature.
33 */
b153f94a 34bool is_blktrace(const char *filename, int *need_swap)
fb7b71a3
JA
35{
36 struct blk_io_trace t;
37 int fd, ret;
38
39 fd = open(filename, O_RDONLY);
4dced407 40 if (fd < 0)
b153f94a 41 return false;
fb7b71a3
JA
42
43 ret = read(fd, &t, sizeof(t));
44 close(fd);
45
46 if (ret < 0) {
47 perror("read blktrace");
b153f94a 48 return false;
fb7b71a3
JA
49 } else if (ret != sizeof(t)) {
50 log_err("fio: short read on blktrace file\n");
b153f94a 51 return false;
fb7b71a3
JA
52 }
53
d95b34a6
JA
54 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
55 *need_swap = 0;
b153f94a 56 return true;
d95b34a6
JA
57 }
58
59 /*
60 * Maybe it needs to be endian swapped...
61 */
62 t.magic = fio_swap32(t.magic);
63 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
64 *need_swap = 1;
b153f94a 65 return true;
d95b34a6 66 }
fb7b71a3 67
b153f94a 68 return false;
fb7b71a3
JA
69}
70
c69aa91f
JA
71#define FMINORBITS 20
72#define FMINORMASK ((1U << FMINORBITS) - 1)
73#define FMAJOR(dev) ((unsigned int) ((dev) >> FMINORBITS))
74#define FMINOR(dev) ((unsigned int) ((dev) & FMINORMASK))
eeb9c2aa 75
89ac1d48 76static void trace_add_open_close_event(struct thread_data *td, int fileno, enum file_log_act action)
691c8fb0
JA
77{
78 struct io_piece *ipo;
79
80 ipo = calloc(1, sizeof(*ipo));
0d29de83 81 init_ipo(ipo);
691c8fb0
JA
82
83 ipo->ddir = DDIR_INVAL;
84 ipo->fileno = fileno;
89ac1d48 85 ipo->file_action = action;
01743ee1 86 flist_add_tail(&ipo->list, &td->io_log_list);
691c8fb0
JA
87}
88
6aaf98e9 89static int trace_add_file(struct thread_data *td, __u32 device)
5e6c2067 90{
6aaf98e9 91 static unsigned int last_maj, last_min, last_fileno;
c69aa91f
JA
92 unsigned int maj = FMAJOR(device);
93 unsigned int min = FMINOR(device);
5e6c2067 94 struct fio_file *f;
26e616d0 95 char dev[256];
6aaf98e9 96 unsigned int i;
5e6c2067 97
6aaf98e9 98 if (last_maj == maj && last_min == min)
89ac1d48 99 return last_fileno;
5e6c2067
JA
100
101 last_maj = maj;
102 last_min = min;
103
104 /*
105 * check for this file in our list
106 */
6aaf98e9 107 for_each_file(td, f, i)
89ac1d48
SL
108 if (f->major == maj && f->minor == min) {
109 last_fileno = f->fileno;
6aaf98e9 110 return last_fileno;
89ac1d48 111 }
5e6c2067
JA
112
113 strcpy(dev, "/dev");
40bafa33 114 if (blktrace_lookup_device(td->o.replay_redirect, dev, maj, min)) {
691c8fb0
JA
115 int fileno;
116
40bafa33
JA
117 if (td->o.replay_redirect)
118 dprint(FD_BLKTRACE, "device lookup: %d/%d\n overridden"
119 " with: %s\n", maj, min,
120 td->o.replay_redirect);
121 else
122 dprint(FD_BLKTRACE, "device lookup: %d/%d\n", maj, min);
123
bd6f78b2 124 dprint(FD_BLKTRACE, "add devices %s\n", dev);
49ffb4a2 125 fileno = add_file_exclusive(td, dev);
b53f2c54 126 td->o.open_files++;
5903e7b7
JA
127 td->files[fileno]->major = maj;
128 td->files[fileno]->minor = min;
89ac1d48
SL
129 trace_add_open_close_event(td, fileno, FIO_LOG_OPEN_FILE);
130 last_fileno = fileno;
bd6f78b2 131 }
f01b34ae 132
89ac1d48 133 return last_fileno;
5e6c2067
JA
134}
135
0c63576e
JA
136static void t_bytes_align(struct thread_options *o, struct blk_io_trace *t)
137{
138 if (!o->replay_align)
139 return;
140
141 t->bytes = (t->bytes + o->replay_align - 1) & ~(o->replay_align - 1);
142}
143
8c1fdf04
JA
144/*
145 * Store blk_io_trace data in an ipo for later retrieval.
146 */
fdefd987 147static void store_ipo(struct thread_data *td, unsigned long long offset,
89ac1d48 148 unsigned int bytes, int rw, unsigned long long ttime,
6aaf98e9 149 int fileno)
fdefd987 150{
8812d7f2 151 struct io_piece *ipo;
fdefd987 152
8812d7f2 153 ipo = calloc(1, sizeof(*ipo));
0d29de83
JA
154 init_ipo(ipo);
155
6aaf98e9 156 ipo->offset = offset * 512;
0c63576e
JA
157 if (td->o.replay_scale)
158 ipo->offset = ipo->offset / td->o.replay_scale;
a79f17bf 159 ipo_bytes_align(td->o.replay_align, ipo);
fdefd987 160 ipo->len = bytes;
8c1fdf04 161 ipo->delay = ttime / 1000;
fdefd987
JA
162 if (rw)
163 ipo->ddir = DDIR_WRITE;
164 else
165 ipo->ddir = DDIR_READ;
89ac1d48 166 ipo->fileno = fileno;
fdefd987 167
bd6f78b2
JA
168 dprint(FD_BLKTRACE, "store ddir=%d, off=%llu, len=%lu, delay=%lu\n",
169 ipo->ddir, ipo->offset,
170 ipo->len, ipo->delay);
691c8fb0 171 queue_io_piece(td, ipo);
fdefd987
JA
172}
173
0b9d69ec 174static void handle_trace_notify(struct blk_io_trace *t)
cd991b9e 175{
691c8fb0
JA
176 switch (t->action) {
177 case BLK_TN_PROCESS:
24653680 178 dprint(FD_BLKTRACE, "got process notify: %x, %d\n",
d95b34a6 179 t->action, t->pid);
691c8fb0
JA
180 break;
181 case BLK_TN_TIMESTAMP:
24653680 182 dprint(FD_BLKTRACE, "got timestamp notify: %x, %d\n",
d95b34a6 183 t->action, t->pid);
691c8fb0 184 break;
ff58fced
JA
185 case BLK_TN_MESSAGE:
186 break;
691c8fb0
JA
187 default:
188 dprint(FD_BLKTRACE, "unknown trace act %x\n", t->action);
189 break;
190 }
191}
5b3023b8 192
24653680
JA
193static void handle_trace_discard(struct thread_data *td,
194 struct blk_io_trace *t,
195 unsigned long long ttime,
6aaf98e9 196 unsigned long *ios, unsigned int *bs)
ff58fced 197{
8812d7f2 198 struct io_piece *ipo;
89ac1d48 199 int fileno;
ff58fced 200
d7235efb
JA
201 if (td->o.replay_skip & (1u << DDIR_TRIM))
202 return;
203
8812d7f2 204 ipo = calloc(1, sizeof(*ipo));
0d29de83 205 init_ipo(ipo);
6aaf98e9 206 fileno = trace_add_file(td, t->device);
ff58fced 207
24653680 208 ios[DDIR_TRIM]++;
6aaf98e9
DZ
209 if (t->bytes > bs[DDIR_TRIM])
210 bs[DDIR_TRIM] = t->bytes;
24653680 211
ff58fced
JA
212 td->o.size += t->bytes;
213
ff58fced
JA
214 INIT_FLIST_HEAD(&ipo->list);
215
6aaf98e9 216 ipo->offset = t->sector * 512;
0c63576e
JA
217 if (td->o.replay_scale)
218 ipo->offset = ipo->offset / td->o.replay_scale;
a79f17bf 219 ipo_bytes_align(td->o.replay_align, ipo);
ff58fced
JA
220 ipo->len = t->bytes;
221 ipo->delay = ttime / 1000;
222 ipo->ddir = DDIR_TRIM;
89ac1d48 223 ipo->fileno = fileno;
ff58fced
JA
224
225 dprint(FD_BLKTRACE, "store discard, off=%llu, len=%lu, delay=%lu\n",
226 ipo->offset, ipo->len,
227 ipo->delay);
228 queue_io_piece(td, ipo);
229}
230
19a8064e
JA
231static void dump_trace(struct blk_io_trace *t)
232{
233 log_err("blktrace: ignoring zero byte trace: action=%x\n", t->action);
234}
235
691c8fb0
JA
236static void handle_trace_fs(struct thread_data *td, struct blk_io_trace *t,
237 unsigned long long ttime, unsigned long *ios,
6aaf98e9 238 unsigned int *bs)
691c8fb0
JA
239{
240 int rw;
89ac1d48 241 int fileno;
5b3023b8 242
6aaf98e9 243 fileno = trace_add_file(td, t->device);
5b3023b8
JA
244
245 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
246
d7235efb
JA
247 if (rw) {
248 if (td->o.replay_skip & (1u << DDIR_WRITE))
249 return;
250 } else {
251 if (td->o.replay_skip & (1u << DDIR_READ))
252 return;
253 }
254
19a8064e
JA
255 if (!t->bytes) {
256 if (!fio_did_warn(FIO_WARN_BTRACE_ZERO))
257 dump_trace(t);
258 return;
259 }
c1f22c21 260
6aaf98e9
DZ
261 if (t->bytes > bs[rw])
262 bs[rw] = t->bytes;
5b3023b8
JA
263
264 ios[rw]++;
265 td->o.size += t->bytes;
6aaf98e9 266 store_ipo(td, t->sector, t->bytes, rw, ttime, fileno);
cd991b9e
JA
267}
268
65c42cc8 269static void handle_trace_flush(struct thread_data *td, struct blk_io_trace *t,
811f5421 270 unsigned long long ttime, unsigned long *ios)
65c42cc8
JA
271{
272 struct io_piece *ipo;
65c42cc8
JA
273 int fileno;
274
d7235efb
JA
275 if (td->o.replay_skip & (1u << DDIR_SYNC))
276 return;
277
65c42cc8
JA
278 ipo = calloc(1, sizeof(*ipo));
279 init_ipo(ipo);
6aaf98e9 280 fileno = trace_add_file(td, t->device);
65c42cc8
JA
281
282 ipo->delay = ttime / 1000;
283 ipo->ddir = DDIR_SYNC;
284 ipo->fileno = fileno;
285
811f5421 286 ios[DDIR_SYNC]++;
65c42cc8
JA
287 dprint(FD_BLKTRACE, "store flush delay=%lu\n", ipo->delay);
288 queue_io_piece(td, ipo);
289}
290
691c8fb0
JA
291/*
292 * We only care for queue traces, most of the others are side effects
293 * due to internal workings of the block layer.
294 */
295static void handle_trace(struct thread_data *td, struct blk_io_trace *t,
24653680 296 unsigned long *ios, unsigned int *bs)
691c8fb0 297{
24653680 298 static unsigned long long last_ttime;
0c63576e 299 unsigned long long delay = 0;
24653680 300
691c8fb0
JA
301 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
302 return;
24653680
JA
303
304 if (!(t->action & BLK_TC_ACT(BLK_TC_NOTIFY))) {
6dd7fa77 305 if (!last_ttime || td->o.no_stall)
24653680 306 delay = 0;
6dd7fa77 307 else if (td->o.replay_time_scale == 100)
24653680 308 delay = t->time - last_ttime;
6dd7fa77
JA
309 else {
310 double tmp = t->time - last_ttime;
311 double scale;
312
313 scale = (double) 100.0 / (double) td->o.replay_time_scale;
314 tmp *= scale;
315 delay = tmp;
24653680 316 }
6dd7fa77 317 last_ttime = t->time;
24653680 318 }
691c8fb0 319
0c63576e
JA
320 t_bytes_align(&td->o, t);
321
691c8fb0 322 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
aec2de20 323 handle_trace_notify(t);
ff58fced 324 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
24653680 325 handle_trace_discard(td, t, delay, ios, bs);
65c42cc8 326 else if (t->action & BLK_TC_ACT(BLK_TC_FLUSH))
811f5421 327 handle_trace_flush(td, t, delay, ios);
691c8fb0 328 else
24653680 329 handle_trace_fs(td, t, delay, ios, bs);
691c8fb0
JA
330}
331
d95b34a6
JA
332static void byteswap_trace(struct blk_io_trace *t)
333{
334 t->magic = fio_swap32(t->magic);
335 t->sequence = fio_swap32(t->sequence);
336 t->time = fio_swap64(t->time);
337 t->sector = fio_swap64(t->sector);
338 t->bytes = fio_swap32(t->bytes);
339 t->action = fio_swap32(t->action);
340 t->pid = fio_swap32(t->pid);
341 t->device = fio_swap32(t->device);
342 t->cpu = fio_swap32(t->cpu);
343 t->error = fio_swap16(t->error);
344 t->pdu_len = fio_swap16(t->pdu_len);
345}
346
b153f94a 347static bool t_is_write(struct blk_io_trace *t)
24653680
JA
348{
349 return (t->action & BLK_TC_ACT(BLK_TC_WRITE | BLK_TC_DISCARD)) != 0;
350}
351
a6eaf6c9
JA
352static enum fio_ddir t_get_ddir(struct blk_io_trace *t)
353{
354 if (t->action & BLK_TC_ACT(BLK_TC_READ))
355 return DDIR_READ;
356 else if (t->action & BLK_TC_ACT(BLK_TC_WRITE))
357 return DDIR_WRITE;
358 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
359 return DDIR_TRIM;
360
361 return DDIR_INVAL;
362}
363
364static void depth_inc(struct blk_io_trace *t, int *depth)
365{
366 enum fio_ddir ddir;
367
368 ddir = t_get_ddir(t);
369 if (ddir != DDIR_INVAL)
370 depth[ddir]++;
371}
372
373static void depth_dec(struct blk_io_trace *t, int *depth)
374{
375 enum fio_ddir ddir;
376
377 ddir = t_get_ddir(t);
378 if (ddir != DDIR_INVAL)
379 depth[ddir]--;
380}
381
382static void depth_end(struct blk_io_trace *t, int *this_depth, int *depth)
383{
384 enum fio_ddir ddir = DDIR_INVAL;
385
386 ddir = t_get_ddir(t);
387 if (ddir != DDIR_INVAL) {
388 depth[ddir] = max(depth[ddir], this_depth[ddir]);
389 this_depth[ddir] = 0;
390 }
391}
392
8c1fdf04
JA
393/*
394 * Load a blktrace file by reading all the blk_io_trace entries, and storing
395 * them as io_pieces like the fio text version would do.
396 */
b153f94a 397bool load_blktrace(struct thread_data *td, const char *filename, int need_swap)
fb7b71a3
JA
398{
399 struct blk_io_trace t;
811f5421
JA
400 unsigned long ios[DDIR_RWDIR_SYNC_CNT] = { };
401 unsigned int rw_bs[DDIR_RWDIR_CNT] = { };
402 unsigned long skipped_writes;
5ab088aa
LS
403 FILE *f;
404 int i, old_state, max_depth;
405 struct fio_file *fiof;
811f5421
JA
406 int this_depth[DDIR_RWDIR_CNT] = { };
407 int depth[DDIR_RWDIR_CNT] = { };
fb7b71a3 408
5ab088aa
LS
409 f = fopen(filename, "rb");
410 if (!f) {
fb7b71a3 411 td_verror(td, errno, "open blktrace file");
b153f94a 412 return false;
fb7b71a3
JA
413 }
414
8edd973d 415 old_state = td_bump_runstate(td, TD_SETTING_UP);
5903e7b7 416
6df8adaa 417 td->o.size = 0;
4241ea8f 418 skipped_writes = 0;
fb7b71a3 419 do {
5ab088aa 420 int ret = fread(&t, 1, sizeof(t), f);
fb7b71a3 421
5ab088aa
LS
422 if (ferror(f)) {
423 td_verror(td, errno, "read blktrace file");
8c1fdf04 424 goto err;
5ab088aa 425 } else if (feof(f)) {
e2887563 426 break;
5ab088aa
LS
427 } else if (ret < (int) sizeof(t)) {
428 log_err("fio: iolog short read\n");
fb7b71a3 429 break;
fb7b71a3
JA
430 }
431
d95b34a6
JA
432 if (need_swap)
433 byteswap_trace(&t);
434
fb7b71a3 435 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
5ec10eaa
JA
436 log_err("fio: bad magic in blktrace data: %x\n",
437 t.magic);
8c1fdf04 438 goto err;
fb7b71a3
JA
439 }
440 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
5ec10eaa
JA
441 log_err("fio: bad blktrace version %d\n",
442 t.magic & 0xff);
8c1fdf04 443 goto err;
fb7b71a3 444 }
5ab088aa 445 ret = discard_pdu(f, &t);
f12b323f 446 if (ret < 0) {
874a61d0 447 td_verror(td, -ret, "blktrace lseek");
8c1fdf04 448 goto err;
fb7b71a3 449 }
691c8fb0 450 if ((t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) == 0) {
eb5fdcf1 451 if ((t.action & 0xffff) == __BLK_TA_QUEUE)
a6eaf6c9
JA
452 depth_inc(&t, this_depth);
453 else if (((t.action & 0xffff) == __BLK_TA_BACKMERGE) ||
454 ((t.action & 0xffff) == __BLK_TA_FRONTMERGE))
455 depth_dec(&t, this_depth);
456 else if ((t.action & 0xffff) == __BLK_TA_COMPLETE)
457 depth_end(&t, this_depth, depth);
691c8fb0 458
24653680 459 if (t_is_write(&t) && read_only) {
691c8fb0 460 skipped_writes++;
24653680 461 continue;
64bbb865 462 }
a6edd638 463 }
24653680
JA
464
465 handle_trace(td, &t, ios, rw_bs);
fb7b71a3
JA
466 } while (1);
467
5ab088aa
LS
468 for_each_file(td, fiof, i)
469 trace_add_open_close_event(td, fiof->fileno, FIO_LOG_CLOSE_FILE);
89ac1d48 470
5ab088aa 471 fclose(f);
8c1fdf04 472
8edd973d 473 td_restore_runstate(td, old_state);
5903e7b7 474
f01b34ae
JA
475 if (!td->files_index) {
476 log_err("fio: did not find replay device(s)\n");
b153f94a 477 return false;
f01b34ae
JA
478 }
479
eb5fdcf1
JA
480 /*
481 * For stacked devices, we don't always get a COMPLETE event so
482 * the depth grows to insane values. Limit it to something sane(r).
483 */
a6eaf6c9
JA
484 max_depth = 0;
485 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
486 if (depth[i] > 1024)
487 depth[i] = 1024;
488 else if (!depth[i] && ios[i])
489 depth[i] = 1;
490 max_depth = max(depth[i], max_depth);
491 }
eb5fdcf1 492
4241ea8f 493 if (skipped_writes)
5ec10eaa
JA
494 log_err("fio: %s skips replay of %lu writes due to read-only\n",
495 td->o.name, skipped_writes);
4241ea8f 496
811f5421
JA
497 if (!ios[DDIR_READ] && !ios[DDIR_WRITE] && !ios[DDIR_TRIM] &&
498 !ios[DDIR_SYNC]) {
8c1fdf04 499 log_err("fio: found no ios in blktrace data\n");
b153f94a 500 return false;
252928cb 501 }
502
503 td->o.td_ddir = 0;
504 if (ios[DDIR_READ]) {
505 td->o.td_ddir |= TD_DDIR_READ;
d84f8d49 506 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
252928cb 507 }
508 if (ios[DDIR_WRITE]) {
509 td->o.td_ddir |= TD_DDIR_WRITE;
d84f8d49 510 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
252928cb 511 }
512 if (ios[DDIR_TRIM]) {
513 td->o.td_ddir |= TD_DDIR_TRIM;
24653680 514 td->o.max_bs[DDIR_TRIM] = rw_bs[DDIR_TRIM];
d84f8d49 515 }
8c1fdf04
JA
516
517 /*
518 * We need to do direct/raw ios to the device, to avoid getting
76a00ec6
JA
519 * read-ahead in our way. But only do so if the minimum block size
520 * is a multiple of 4k, otherwise we don't know if it's safe to do so.
8c1fdf04 521 */
76a00ec6 522 if (!fio_option_is_set(&td->o, odirect) && !(td_min_bs(td) & 4095))
a6eaf6c9 523 td->o.odirect = 1;
8c1fdf04 524
eb5fdcf1 525 /*
8a16f59b 526 * If depth wasn't manually set, use probed depth
eb5fdcf1 527 */
8a16f59b 528 if (!fio_option_is_set(&td->o, iodepth))
a6eaf6c9 529 td->o.iodepth = td->o.iodepth_low = max_depth;
eb5fdcf1 530
b153f94a 531 return true;
8c1fdf04 532err:
5ab088aa 533 fclose(f);
b153f94a 534 return false;
fb7b71a3 535}
b9921d1a 536
87a48ada
DZ
537static int init_merge_param_list(fio_fp64_t *vals, struct blktrace_cursor *bcs,
538 int nr_logs, int def, size_t off)
539{
540 int i = 0, len = 0;
541
542 while (len < FIO_IO_U_LIST_MAX_LEN && vals[len].u.f != 0.0)
543 len++;
544
545 if (len && len != nr_logs)
546 return len;
547
548 for (i = 0; i < nr_logs; i++) {
549 int *val = (int *)((char *)&bcs[i] + off);
550 *val = def;
551 if (len)
552 *val = (int)vals[i].u.f;
553 }
554
555 return 0;
556
557}
558
b9921d1a
DZ
559static int find_earliest_io(struct blktrace_cursor *bcs, int nr_logs)
560{
561 __u64 time = ~(__u64)0;
562 int idx = 0, i;
563
564 for (i = 0; i < nr_logs; i++) {
565 if (bcs[i].t.time < time) {
566 time = bcs[i].t.time;
567 idx = i;
568 }
569 }
570
571 return idx;
572}
573
574static void merge_finish_file(struct blktrace_cursor *bcs, int i, int *nr_logs)
575{
55bfd8c8
DZ
576 bcs[i].iter++;
577 if (bcs[i].iter < bcs[i].nr_iter) {
5ab088aa 578 fseek(bcs[i].f, 0, SEEK_SET);
55bfd8c8
DZ
579 return;
580 }
581
b9921d1a
DZ
582 *nr_logs -= 1;
583
584 /* close file */
5ab088aa 585 fclose(bcs[i].f);
b9921d1a
DZ
586
587 /* keep active files contiguous */
588 memmove(&bcs[i], &bcs[*nr_logs], sizeof(bcs[i]));
589}
590
591static int read_trace(struct thread_data *td, struct blktrace_cursor *bc)
592{
593 int ret = 0;
594 struct blk_io_trace *t = &bc->t;
595
596read_skip:
597 /* read an io trace */
5ab088aa
LS
598 ret = fread(&t, 1, sizeof(t), bc->f);
599 if (ferror(bc->f)) {
600 td_verror(td, errno, "read blktrace file");
55bfd8c8 601 return ret;
5ab088aa 602 } else if (feof(bc->f)) {
55bfd8c8
DZ
603 if (!bc->length)
604 bc->length = bc->t.time;
b9921d1a
DZ
605 return ret;
606 } else if (ret < (int) sizeof(*t)) {
5ab088aa 607 log_err("fio: iolog short read\n");
b9921d1a
DZ
608 return -1;
609 }
610
611 if (bc->swap)
612 byteswap_trace(t);
613
614 /* skip over actions that fio does not care about */
615 if ((t->action & 0xffff) != __BLK_TA_QUEUE ||
616 t_get_ddir(t) == DDIR_INVAL) {
5ab088aa 617 ret = discard_pdu(bc->f, t);
b9921d1a 618 if (ret < 0) {
874a61d0 619 td_verror(td, -ret, "blktrace lseek");
b9921d1a 620 return ret;
b9921d1a
DZ
621 }
622 goto read_skip;
623 }
624
55bfd8c8 625 t->time = (t->time + bc->iter * bc->length) * bc->scalar / 100;
87a48ada 626
b9921d1a
DZ
627 return ret;
628}
629
630static int write_trace(FILE *fp, struct blk_io_trace *t)
631{
632 /* pdu is not used so just write out only the io trace */
633 t->pdu_len = 0;
634 return fwrite((void *)t, sizeof(*t), 1, fp);
635}
636
637int merge_blktrace_iologs(struct thread_data *td)
638{
639 int nr_logs = get_max_str_idx(td->o.read_iolog_file);
640 struct blktrace_cursor *bcs = malloc(sizeof(struct blktrace_cursor) *
641 nr_logs);
642 struct blktrace_cursor *bc;
643 FILE *merge_fp;
644 char *str, *ptr, *name, *merge_buf;
645 int i, ret;
646
87a48ada
DZ
647 ret = init_merge_param_list(td->o.merge_blktrace_scalars, bcs, nr_logs,
648 100, offsetof(struct blktrace_cursor,
649 scalar));
650 if (ret) {
651 log_err("fio: merge_blktrace_scalars(%d) != nr_logs(%d)\n",
652 ret, nr_logs);
653 goto err_param;
654 }
655
55bfd8c8
DZ
656 ret = init_merge_param_list(td->o.merge_blktrace_iters, bcs, nr_logs,
657 1, offsetof(struct blktrace_cursor,
658 nr_iter));
659 if (ret) {
660 log_err("fio: merge_blktrace_iters(%d) != nr_logs(%d)\n",
661 ret, nr_logs);
662 goto err_param;
663 }
664
b9921d1a
DZ
665 /* setup output file */
666 merge_fp = fopen(td->o.merge_blktrace_file, "w");
667 merge_buf = malloc(128 * 1024);
c81ab051
BVA
668 if (!merge_buf)
669 goto err_out_file;
b9921d1a
DZ
670 ret = setvbuf(merge_fp, merge_buf, _IOFBF, 128 * 1024);
671 if (ret)
c81ab051 672 goto err_merge_buf;
b9921d1a
DZ
673
674 /* setup input files */
675 str = ptr = strdup(td->o.read_iolog_file);
676 nr_logs = 0;
677 for (i = 0; (name = get_next_str(&ptr)) != NULL; i++) {
5ab088aa
LS
678 bcs[i].f = fopen(name, "rb");
679 if (!bcs[i].f) {
b9921d1a 680 log_err("fio: could not open file: %s\n", name);
5ab088aa 681 ret = -errno;
2ba46d1b 682 free(str);
b9921d1a
DZ
683 goto err_file;
684 }
b9921d1a
DZ
685 nr_logs++;
686
687 if (!is_blktrace(name, &bcs[i].swap)) {
688 log_err("fio: file is not a blktrace: %s\n", name);
2ba46d1b 689 free(str);
b9921d1a
DZ
690 goto err_file;
691 }
692
693 ret = read_trace(td, &bcs[i]);
694 if (ret < 0) {
2ba46d1b 695 free(str);
b9921d1a
DZ
696 goto err_file;
697 } else if (!ret) {
698 merge_finish_file(bcs, i, &nr_logs);
699 i--;
700 }
701 }
702 free(str);
703
704 /* merge files */
705 while (nr_logs) {
706 i = find_earliest_io(bcs, nr_logs);
707 bc = &bcs[i];
708 /* skip over the pdu */
5ab088aa 709 ret = discard_pdu(bc->f, &bc->t);
b9921d1a 710 if (ret < 0) {
874a61d0 711 td_verror(td, -ret, "blktrace lseek");
b9921d1a 712 goto err_file;
b9921d1a
DZ
713 }
714
715 ret = write_trace(merge_fp, &bc->t);
716 ret = read_trace(td, bc);
717 if (ret < 0)
718 goto err_file;
719 else if (!ret)
720 merge_finish_file(bcs, i, &nr_logs);
721 }
722
723 /* set iolog file to read from the newly merged file */
724 td->o.read_iolog_file = td->o.merge_blktrace_file;
725 ret = 0;
726
727err_file:
728 /* cleanup */
729 for (i = 0; i < nr_logs; i++) {
5ab088aa 730 fclose(bcs[i].f);
b9921d1a 731 }
c81ab051
BVA
732err_merge_buf:
733 free(merge_buf);
b9921d1a
DZ
734err_out_file:
735 fflush(merge_fp);
736 fclose(merge_fp);
87a48ada 737err_param:
b9921d1a
DZ
738 free(bcs);
739
740 return ret;
741}