2 * blktrace support code for fio
13 #include "blktrace_api.h"
14 #include "oslib/linux-dev-lookup.h"
17 * Just discard the pdu by seeking past it.
19 static int discard_pdu(FILE* f, struct blk_io_trace *t)
24 dprint(FD_BLKTRACE, "discard pdu len %u\n", t->pdu_len);
25 if (fseek(f, t->pdu_len, SEEK_CUR) < 0)
32 * Check if this is a blktrace binary data file. We read a single trace
33 * into memory and check for the magic signature.
35 bool is_blktrace(const char *filename, int *need_swap)
37 struct blk_io_trace t;
40 fd = open(filename, O_RDONLY);
44 ret = read(fd, &t, sizeof(t));
48 perror("read blktrace");
50 } else if (ret != sizeof(t)) {
51 log_err("fio: short read on blktrace file\n");
55 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
61 * Maybe it needs to be endian swapped...
63 t.magic = fio_swap32(t.magic);
64 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
73 #define FMINORMASK ((1U << FMINORBITS) - 1)
74 #define FMAJOR(dev) ((unsigned int) ((dev) >> FMINORBITS))
75 #define FMINOR(dev) ((unsigned int) ((dev) & FMINORMASK))
77 static void trace_add_open_close_event(struct thread_data *td, int fileno, enum file_log_act action)
81 ipo = calloc(1, sizeof(*ipo));
84 ipo->ddir = DDIR_INVAL;
86 ipo->file_action = action;
87 flist_add_tail(&ipo->list, &td->io_log_list);
90 static int trace_add_file(struct thread_data *td, __u32 device)
92 static unsigned int last_maj, last_min, last_fileno;
93 unsigned int maj = FMAJOR(device);
94 unsigned int min = FMINOR(device);
99 if (last_maj == maj && last_min == min)
106 * check for this file in our list
108 for_each_file(td, f, i)
109 if (f->major == maj && f->minor == min) {
110 last_fileno = f->fileno;
115 if (blktrace_lookup_device(td->o.replay_redirect, dev, maj, min)) {
118 if (td->o.replay_redirect)
119 dprint(FD_BLKTRACE, "device lookup: %d/%d\n overridden"
120 " with: %s\n", maj, min,
121 td->o.replay_redirect);
123 dprint(FD_BLKTRACE, "device lookup: %d/%d\n", maj, min);
125 dprint(FD_BLKTRACE, "add devices %s\n", dev);
126 fileno = add_file_exclusive(td, dev);
128 td->files[fileno]->major = maj;
129 td->files[fileno]->minor = min;
130 trace_add_open_close_event(td, fileno, FIO_LOG_OPEN_FILE);
131 last_fileno = fileno;
137 static void t_bytes_align(struct thread_options *o, struct blk_io_trace *t)
139 if (!o->replay_align)
142 t->bytes = (t->bytes + o->replay_align - 1) & ~(o->replay_align - 1);
146 * Store blk_io_trace data in an ipo for later retrieval.
148 static void store_ipo(struct thread_data *td, unsigned long long offset,
149 unsigned int bytes, int rw, unsigned long long ttime,
152 struct io_piece *ipo;
154 ipo = calloc(1, sizeof(*ipo));
157 ipo->offset = offset * 512;
158 if (td->o.replay_scale)
159 ipo->offset = ipo->offset / td->o.replay_scale;
160 ipo_bytes_align(td->o.replay_align, ipo);
162 ipo->delay = ttime / 1000;
164 ipo->ddir = DDIR_WRITE;
166 ipo->ddir = DDIR_READ;
167 ipo->fileno = fileno;
169 dprint(FD_BLKTRACE, "store ddir=%d, off=%llu, len=%lu, delay=%lu\n",
170 ipo->ddir, ipo->offset,
171 ipo->len, ipo->delay);
172 queue_io_piece(td, ipo);
175 static bool handle_trace_notify(struct blk_io_trace *t)
179 dprint(FD_BLKTRACE, "got process notify: %x, %d\n",
182 case BLK_TN_TIMESTAMP:
183 dprint(FD_BLKTRACE, "got timestamp notify: %x, %d\n",
189 dprint(FD_BLKTRACE, "unknown trace act %x\n", t->action);
195 static bool handle_trace_discard(struct thread_data *td,
196 struct blk_io_trace *t,
197 unsigned long long ttime,
198 unsigned long *ios, unsigned long long *bs)
200 struct io_piece *ipo;
203 if (td->o.replay_skip & (1u << DDIR_TRIM))
206 ipo = calloc(1, sizeof(*ipo));
208 fileno = trace_add_file(td, t->device);
211 if (t->bytes > bs[DDIR_TRIM])
212 bs[DDIR_TRIM] = t->bytes;
214 td->o.size += t->bytes;
216 INIT_FLIST_HEAD(&ipo->list);
218 ipo->offset = t->sector * 512;
219 if (td->o.replay_scale)
220 ipo->offset = ipo->offset / td->o.replay_scale;
221 ipo_bytes_align(td->o.replay_align, ipo);
223 ipo->delay = ttime / 1000;
224 ipo->ddir = DDIR_TRIM;
225 ipo->fileno = fileno;
227 dprint(FD_BLKTRACE, "store discard, off=%llu, len=%lu, delay=%lu\n",
228 ipo->offset, ipo->len,
230 queue_io_piece(td, ipo);
234 static void dump_trace(struct blk_io_trace *t)
236 log_err("blktrace: ignoring zero byte trace: action=%x\n", t->action);
239 static bool handle_trace_fs(struct thread_data *td, struct blk_io_trace *t,
240 unsigned long long ttime, unsigned long *ios,
241 unsigned long long *bs)
246 fileno = trace_add_file(td, t->device);
248 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
251 if (td->o.replay_skip & (1u << DDIR_WRITE))
254 if (td->o.replay_skip & (1u << DDIR_READ))
259 if (!fio_did_warn(FIO_WARN_BTRACE_ZERO))
264 if (t->bytes > bs[rw])
268 td->o.size += t->bytes;
269 store_ipo(td, t->sector, t->bytes, rw, ttime, fileno);
273 static bool handle_trace_flush(struct thread_data *td, struct blk_io_trace *t,
274 unsigned long long ttime, unsigned long *ios)
276 struct io_piece *ipo;
279 if (td->o.replay_skip & (1u << DDIR_SYNC))
282 ipo = calloc(1, sizeof(*ipo));
284 fileno = trace_add_file(td, t->device);
286 ipo->delay = ttime / 1000;
287 ipo->ddir = DDIR_SYNC;
288 ipo->fileno = fileno;
291 dprint(FD_BLKTRACE, "store flush delay=%lu\n", ipo->delay);
292 queue_io_piece(td, ipo);
297 * We only care for queue traces, most of the others are side effects
298 * due to internal workings of the block layer.
300 static bool queue_trace(struct thread_data *td, struct blk_io_trace *t,
301 unsigned long *ios, unsigned long long *bs)
303 static unsigned long long last_ttime;
304 unsigned long long delay = 0;
306 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
309 if (!(t->action & BLK_TC_ACT(BLK_TC_NOTIFY))) {
310 if (!last_ttime || td->o.no_stall)
312 else if (td->o.replay_time_scale == 100)
313 delay = t->time - last_ttime;
315 double tmp = t->time - last_ttime;
318 scale = (double) 100.0 / (double) td->o.replay_time_scale;
322 last_ttime = t->time;
325 t_bytes_align(&td->o, t);
327 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
328 return handle_trace_notify(t);
329 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
330 return handle_trace_discard(td, t, delay, ios, bs);
331 else if (t->action & BLK_TC_ACT(BLK_TC_FLUSH))
332 return handle_trace_flush(td, t, delay, ios);
334 return handle_trace_fs(td, t, delay, ios, bs);
337 static void byteswap_trace(struct blk_io_trace *t)
339 t->magic = fio_swap32(t->magic);
340 t->sequence = fio_swap32(t->sequence);
341 t->time = fio_swap64(t->time);
342 t->sector = fio_swap64(t->sector);
343 t->bytes = fio_swap32(t->bytes);
344 t->action = fio_swap32(t->action);
345 t->pid = fio_swap32(t->pid);
346 t->device = fio_swap32(t->device);
347 t->cpu = fio_swap32(t->cpu);
348 t->error = fio_swap16(t->error);
349 t->pdu_len = fio_swap16(t->pdu_len);
352 static bool t_is_write(struct blk_io_trace *t)
354 return (t->action & BLK_TC_ACT(BLK_TC_WRITE | BLK_TC_DISCARD)) != 0;
357 static enum fio_ddir t_get_ddir(struct blk_io_trace *t)
359 if (t->action & BLK_TC_ACT(BLK_TC_READ))
361 else if (t->action & BLK_TC_ACT(BLK_TC_WRITE))
363 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
369 static void depth_inc(struct blk_io_trace *t, int *depth)
373 ddir = t_get_ddir(t);
374 if (ddir != DDIR_INVAL)
378 static void depth_dec(struct blk_io_trace *t, int *depth)
382 ddir = t_get_ddir(t);
383 if (ddir != DDIR_INVAL)
387 static void depth_end(struct blk_io_trace *t, int *this_depth, int *depth)
389 enum fio_ddir ddir = DDIR_INVAL;
391 ddir = t_get_ddir(t);
392 if (ddir != DDIR_INVAL) {
393 depth[ddir] = max(depth[ddir], this_depth[ddir]);
394 this_depth[ddir] = 0;
399 * Load a blktrace file by reading all the blk_io_trace entries, and storing
400 * them as io_pieces like the fio text version would do.
402 bool init_blktrace_read(struct thread_data *td, const char *filename, int need_swap)
406 td->io_log_rfile = fopen(filename, "rb");
407 if (!td->io_log_rfile) {
408 td_verror(td, errno, "open blktrace file");
411 td->io_log_blktrace_swap = need_swap;
414 free_release_files(td);
416 old_state = td_bump_runstate(td, TD_SETTING_UP);
418 if (!read_blktrace(td)) {
422 td_restore_runstate(td, old_state);
424 if (!td->files_index) {
425 log_err("fio: did not find replay device(s)\n");
432 if (td->io_log_rfile) {
433 fclose(td->io_log_rfile);
434 td->io_log_rfile = NULL;
439 bool read_blktrace(struct thread_data* td)
441 struct blk_io_trace t;
442 unsigned long ios[DDIR_RWDIR_SYNC_CNT] = { };
443 unsigned long long rw_bs[DDIR_RWDIR_CNT] = { };
444 unsigned long skipped_writes;
445 FILE *f = td->io_log_rfile;
447 struct fio_file *fiof;
448 int this_depth[DDIR_RWDIR_CNT] = { };
449 int depth[DDIR_RWDIR_CNT] = { };
450 int64_t items_to_fetch = 0;
452 if (td->o.read_iolog_chunked) {
453 items_to_fetch = iolog_items_to_fetch(td);
460 int ret = fread(&t, 1, sizeof(t), f);
463 td_verror(td, errno, "read blktrace file");
465 } else if (feof(f)) {
467 } else if (ret < (int) sizeof(t)) {
468 log_err("fio: iolog short read\n");
472 if (td->io_log_blktrace_swap)
475 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
476 log_err("fio: bad magic in blktrace data: %x\n",
480 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
481 log_err("fio: bad blktrace version %d\n",
485 ret = discard_pdu(f, &t);
487 td_verror(td, -ret, "blktrace lseek");
490 if ((t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) == 0) {
491 if ((t.action & 0xffff) == __BLK_TA_QUEUE)
492 depth_inc(&t, this_depth);
493 else if (((t.action & 0xffff) == __BLK_TA_BACKMERGE) ||
494 ((t.action & 0xffff) == __BLK_TA_FRONTMERGE))
495 depth_dec(&t, this_depth);
496 else if ((t.action & 0xffff) == __BLK_TA_COMPLETE)
497 depth_end(&t, this_depth, depth);
499 if (t_is_write(&t) && read_only) {
505 if (!queue_trace(td, &t, ios, rw_bs))
508 if (td->o.read_iolog_chunked) {
509 td->io_log_current++;
511 if (items_to_fetch == 0)
516 if (td->o.read_iolog_chunked) {
517 td->io_log_highmark = td->io_log_current;
518 td->io_log_checkmark = (td->io_log_highmark + 1) / 2;
519 fio_gettime(&td->io_log_highmark_time, NULL);
523 log_err("fio: %s skips replay of %lu writes due to read-only\n",
524 td->o.name, skipped_writes);
526 if (td->o.read_iolog_chunked) {
527 if (td->io_log_current == 0) {
530 td->o.td_ddir = TD_DDIR_RW;
531 if ((rw_bs[DDIR_READ] > td->o.max_bs[DDIR_READ] ||
532 rw_bs[DDIR_WRITE] > td->o.max_bs[DDIR_WRITE] ||
533 rw_bs[DDIR_TRIM] > td->o.max_bs[DDIR_TRIM]) &&
536 td->o.max_bs[DDIR_READ] = max(td->o.max_bs[DDIR_READ], rw_bs[DDIR_READ]);
537 td->o.max_bs[DDIR_WRITE] = max(td->o.max_bs[DDIR_WRITE], rw_bs[DDIR_WRITE]);
538 td->o.max_bs[DDIR_TRIM] = max(td->o.max_bs[DDIR_TRIM], rw_bs[DDIR_TRIM]);
541 init_io_u_buffers(td);
546 for_each_file(td, fiof, i)
547 trace_add_open_close_event(td, fiof->fileno, FIO_LOG_CLOSE_FILE);
549 fclose(td->io_log_rfile);
550 td->io_log_rfile = NULL;
553 * For stacked devices, we don't always get a COMPLETE event so
554 * the depth grows to insane values. Limit it to something sane(r).
557 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
560 else if (!depth[i] && ios[i])
562 max_depth = max(depth[i], max_depth);
565 if (!ios[DDIR_READ] && !ios[DDIR_WRITE] && !ios[DDIR_TRIM] &&
567 log_err("fio: found no ios in blktrace data\n");
572 if (ios[DDIR_READ]) {
573 td->o.td_ddir |= TD_DDIR_READ;
574 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
576 if (ios[DDIR_WRITE]) {
577 td->o.td_ddir |= TD_DDIR_WRITE;
578 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
580 if (ios[DDIR_TRIM]) {
581 td->o.td_ddir |= TD_DDIR_TRIM;
582 td->o.max_bs[DDIR_TRIM] = rw_bs[DDIR_TRIM];
586 * We need to do direct/raw ios to the device, to avoid getting
587 * read-ahead in our way. But only do so if the minimum block size
588 * is a multiple of 4k, otherwise we don't know if it's safe to do so.
590 if (!fio_option_is_set(&td->o, odirect) && !(td_min_bs(td) & 4095))
594 * If depth wasn't manually set, use probed depth
596 if (!fio_option_is_set(&td->o, iodepth))
597 td->o.iodepth = td->o.iodepth_low = max_depth;
605 static int init_merge_param_list(fio_fp64_t *vals, struct blktrace_cursor *bcs,
606 int nr_logs, int def, size_t off)
610 while (len < FIO_IO_U_LIST_MAX_LEN && vals[len].u.f != 0.0)
613 if (len && len != nr_logs)
616 for (i = 0; i < nr_logs; i++) {
617 int *val = (int *)((char *)&bcs[i] + off);
620 *val = (int)vals[i].u.f;
627 static int find_earliest_io(struct blktrace_cursor *bcs, int nr_logs)
629 __u64 time = ~(__u64)0;
632 for (i = 0; i < nr_logs; i++) {
633 if (bcs[i].t.time < time) {
634 time = bcs[i].t.time;
642 static void merge_finish_file(struct blktrace_cursor *bcs, int i, int *nr_logs)
645 if (bcs[i].iter < bcs[i].nr_iter) {
646 fseek(bcs[i].f, 0, SEEK_SET);
655 /* keep active files contiguous */
656 memmove(&bcs[i], &bcs[*nr_logs], sizeof(bcs[i]));
659 static int read_trace(struct thread_data *td, struct blktrace_cursor *bc)
662 struct blk_io_trace *t = &bc->t;
665 /* read an io trace */
666 ret = fread(&t, 1, sizeof(t), bc->f);
668 td_verror(td, errno, "read blktrace file");
670 } else if (feof(bc->f)) {
672 bc->length = bc->t.time;
674 } else if (ret < (int) sizeof(*t)) {
675 log_err("fio: iolog short read\n");
682 /* skip over actions that fio does not care about */
683 if ((t->action & 0xffff) != __BLK_TA_QUEUE ||
684 t_get_ddir(t) == DDIR_INVAL) {
685 ret = discard_pdu(bc->f, t);
687 td_verror(td, -ret, "blktrace lseek");
693 t->time = (t->time + bc->iter * bc->length) * bc->scalar / 100;
698 static int write_trace(FILE *fp, struct blk_io_trace *t)
700 /* pdu is not used so just write out only the io trace */
702 return fwrite((void *)t, sizeof(*t), 1, fp);
705 int merge_blktrace_iologs(struct thread_data *td)
707 int nr_logs = get_max_str_idx(td->o.read_iolog_file);
708 struct blktrace_cursor *bcs = malloc(sizeof(struct blktrace_cursor) *
710 struct blktrace_cursor *bc;
712 char *str, *ptr, *name, *merge_buf;
715 ret = init_merge_param_list(td->o.merge_blktrace_scalars, bcs, nr_logs,
716 100, offsetof(struct blktrace_cursor,
719 log_err("fio: merge_blktrace_scalars(%d) != nr_logs(%d)\n",
724 ret = init_merge_param_list(td->o.merge_blktrace_iters, bcs, nr_logs,
725 1, offsetof(struct blktrace_cursor,
728 log_err("fio: merge_blktrace_iters(%d) != nr_logs(%d)\n",
733 /* setup output file */
734 merge_fp = fopen(td->o.merge_blktrace_file, "w");
735 merge_buf = malloc(128 * 1024);
738 ret = setvbuf(merge_fp, merge_buf, _IOFBF, 128 * 1024);
742 /* setup input files */
743 str = ptr = strdup(td->o.read_iolog_file);
745 for (i = 0; (name = get_next_str(&ptr)) != NULL; i++) {
746 bcs[i].f = fopen(name, "rb");
748 log_err("fio: could not open file: %s\n", name);
755 if (!is_blktrace(name, &bcs[i].swap)) {
756 log_err("fio: file is not a blktrace: %s\n", name);
761 ret = read_trace(td, &bcs[i]);
766 merge_finish_file(bcs, i, &nr_logs);
774 i = find_earliest_io(bcs, nr_logs);
776 /* skip over the pdu */
777 ret = discard_pdu(bc->f, &bc->t);
779 td_verror(td, -ret, "blktrace lseek");
783 ret = write_trace(merge_fp, &bc->t);
784 ret = read_trace(td, bc);
788 merge_finish_file(bcs, i, &nr_logs);
791 /* set iolog file to read from the newly merged file */
792 td->o.read_iolog_file = td->o.merge_blktrace_file;
797 for (i = 0; i < nr_logs; i++) {