2 * blktrace support code for fio
13 #include "blktrace_api.h"
14 #include "oslib/linux-dev-lookup.h"
16 #define TRACE_FIFO_SIZE 8192
19 * fifo refill frontend, to avoid reading data in trace sized bites
21 static int refill_fifo(struct thread_data *td, struct fifo *fifo, int fd)
23 char buf[TRACE_FIFO_SIZE];
28 if (total > fifo_room(fifo))
29 total = fifo_room(fifo);
31 ret = read(fd, buf, total);
33 td_verror(td, errno, "read blktrace file");
38 ret = fifo_put(fifo, buf, ret);
40 dprint(FD_BLKTRACE, "refill: filled %d bytes\n", ret);
45 * Retrieve 'len' bytes from the fifo, refilling if necessary.
47 static int trace_fifo_get(struct thread_data *td, struct fifo *fifo, int fd,
48 void *buf, unsigned int len)
50 if (fifo_len(fifo) < len) {
51 int ret = refill_fifo(td, fifo, fd);
57 return fifo_get(fifo, buf, len);
61 * Just discard the pdu by seeking past it.
63 static int discard_pdu(struct thread_data *td, struct fifo *fifo, int fd,
64 struct blk_io_trace *t)
69 dprint(FD_BLKTRACE, "discard pdu len %u\n", t->pdu_len);
70 return trace_fifo_get(td, fifo, fd, NULL, t->pdu_len);
74 * Check if this is a blktrace binary data file. We read a single trace
75 * into memory and check for the magic signature.
77 bool is_blktrace(const char *filename, int *need_swap)
79 struct blk_io_trace t;
82 fd = open(filename, O_RDONLY);
86 ret = read(fd, &t, sizeof(t));
90 perror("read blktrace");
92 } else if (ret != sizeof(t)) {
93 log_err("fio: short read on blktrace file\n");
97 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
103 * Maybe it needs to be endian swapped...
105 t.magic = fio_swap32(t.magic);
106 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
114 #define FMINORBITS 20
115 #define FMINORMASK ((1U << FMINORBITS) - 1)
116 #define FMAJOR(dev) ((unsigned int) ((dev) >> FMINORBITS))
117 #define FMINOR(dev) ((unsigned int) ((dev) & FMINORMASK))
119 static void trace_add_open_close_event(struct thread_data *td, int fileno, enum file_log_act action)
121 struct io_piece *ipo;
123 ipo = calloc(1, sizeof(*ipo));
126 ipo->ddir = DDIR_INVAL;
127 ipo->fileno = fileno;
128 ipo->file_action = action;
129 flist_add_tail(&ipo->list, &td->io_log_list);
132 static int get_dev_blocksize(const char *dev, unsigned int *bs)
136 fd = open(dev, O_RDONLY);
140 if (ioctl(fd, BLKSSZGET, bs) < 0) {
149 static int trace_add_file(struct thread_data *td, __u32 device,
152 static unsigned int last_maj, last_min, last_fileno, last_bs;
153 unsigned int maj = FMAJOR(device);
154 unsigned int min = FMINOR(device);
159 if (last_maj == maj && last_min == min) {
168 * check for this file in our list
170 for_each_file(td, f, i) {
171 if (f->major == maj && f->minor == min) {
172 last_fileno = f->fileno;
179 if (blktrace_lookup_device(td->o.replay_redirect, dev, maj, min)) {
180 unsigned int this_bs;
183 if (td->o.replay_redirect)
184 dprint(FD_BLKTRACE, "device lookup: %d/%d\n overridden"
185 " with: %s\n", maj, min,
186 td->o.replay_redirect);
188 dprint(FD_BLKTRACE, "device lookup: %d/%d\n", maj, min);
190 dprint(FD_BLKTRACE, "add devices %s\n", dev);
191 fileno = add_file_exclusive(td, dev);
193 if (get_dev_blocksize(dev, &this_bs))
197 td->files[fileno]->major = maj;
198 td->files[fileno]->minor = min;
199 td->files[fileno]->bs = this_bs;
200 trace_add_open_close_event(td, fileno, FIO_LOG_OPEN_FILE);
202 last_fileno = fileno;
211 static void t_bytes_align(struct thread_options *o, struct blk_io_trace *t)
213 if (!o->replay_align)
216 t->bytes = (t->bytes + o->replay_align - 1) & ~(o->replay_align - 1);
220 * Store blk_io_trace data in an ipo for later retrieval.
222 static void store_ipo(struct thread_data *td, unsigned long long offset,
223 unsigned int bytes, int rw, unsigned long long ttime,
224 int fileno, unsigned int bs)
226 struct io_piece *ipo;
228 ipo = calloc(1, sizeof(*ipo));
231 ipo->offset = offset * bs;
232 if (td->o.replay_scale)
233 ipo->offset = ipo->offset / td->o.replay_scale;
234 ipo_bytes_align(td->o.replay_align, ipo);
236 ipo->delay = ttime / 1000;
238 ipo->ddir = DDIR_WRITE;
240 ipo->ddir = DDIR_READ;
241 ipo->fileno = fileno;
243 dprint(FD_BLKTRACE, "store ddir=%d, off=%llu, len=%lu, delay=%lu\n",
244 ipo->ddir, ipo->offset,
245 ipo->len, ipo->delay);
246 queue_io_piece(td, ipo);
249 static void handle_trace_notify(struct blk_io_trace *t)
253 dprint(FD_BLKTRACE, "got process notify: %x, %d\n",
256 case BLK_TN_TIMESTAMP:
257 dprint(FD_BLKTRACE, "got timestamp notify: %x, %d\n",
263 dprint(FD_BLKTRACE, "unknown trace act %x\n", t->action);
268 static void handle_trace_discard(struct thread_data *td,
269 struct blk_io_trace *t,
270 unsigned long long ttime,
271 unsigned long *ios, unsigned int *rw_bs)
273 struct io_piece *ipo;
277 if (td->o.replay_skip & (1u << DDIR_TRIM))
280 ipo = calloc(1, sizeof(*ipo));
282 fileno = trace_add_file(td, t->device, &bs);
285 if (t->bytes > rw_bs[DDIR_TRIM])
286 rw_bs[DDIR_TRIM] = t->bytes;
288 td->o.size += t->bytes;
290 INIT_FLIST_HEAD(&ipo->list);
292 ipo->offset = t->sector * bs;
293 if (td->o.replay_scale)
294 ipo->offset = ipo->offset / td->o.replay_scale;
295 ipo_bytes_align(td->o.replay_align, ipo);
297 ipo->delay = ttime / 1000;
298 ipo->ddir = DDIR_TRIM;
299 ipo->fileno = fileno;
301 dprint(FD_BLKTRACE, "store discard, off=%llu, len=%lu, delay=%lu\n",
302 ipo->offset, ipo->len,
304 queue_io_piece(td, ipo);
307 static void dump_trace(struct blk_io_trace *t)
309 log_err("blktrace: ignoring zero byte trace: action=%x\n", t->action);
312 static void handle_trace_fs(struct thread_data *td, struct blk_io_trace *t,
313 unsigned long long ttime, unsigned long *ios,
320 fileno = trace_add_file(td, t->device, &bs);
322 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
325 if (td->o.replay_skip & (1u << DDIR_WRITE))
328 if (td->o.replay_skip & (1u << DDIR_READ))
333 if (!fio_did_warn(FIO_WARN_BTRACE_ZERO))
338 if (t->bytes > rw_bs[rw])
339 rw_bs[rw] = t->bytes;
342 td->o.size += t->bytes;
343 store_ipo(td, t->sector, t->bytes, rw, ttime, fileno, bs);
346 static void handle_trace_flush(struct thread_data *td, struct blk_io_trace *t,
347 unsigned long long ttime, unsigned long *ios)
349 struct io_piece *ipo;
353 if (td->o.replay_skip & (1u << DDIR_SYNC))
356 ipo = calloc(1, sizeof(*ipo));
358 fileno = trace_add_file(td, t->device, &bs);
360 ipo->delay = ttime / 1000;
361 ipo->ddir = DDIR_SYNC;
362 ipo->fileno = fileno;
365 dprint(FD_BLKTRACE, "store flush delay=%lu\n", ipo->delay);
366 queue_io_piece(td, ipo);
370 * We only care for queue traces, most of the others are side effects
371 * due to internal workings of the block layer.
373 static void handle_trace(struct thread_data *td, struct blk_io_trace *t,
374 unsigned long *ios, unsigned int *bs)
376 static unsigned long long last_ttime;
377 unsigned long long delay = 0;
379 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
382 if (!(t->action & BLK_TC_ACT(BLK_TC_NOTIFY))) {
383 if (!last_ttime || td->o.no_stall)
385 else if (td->o.replay_time_scale == 100)
386 delay = t->time - last_ttime;
388 double tmp = t->time - last_ttime;
391 scale = (double) 100.0 / (double) td->o.replay_time_scale;
395 last_ttime = t->time;
398 t_bytes_align(&td->o, t);
400 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
401 handle_trace_notify(t);
402 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
403 handle_trace_discard(td, t, delay, ios, bs);
404 else if (t->action & BLK_TC_ACT(BLK_TC_FLUSH))
405 handle_trace_flush(td, t, delay, ios);
407 handle_trace_fs(td, t, delay, ios, bs);
410 static void byteswap_trace(struct blk_io_trace *t)
412 t->magic = fio_swap32(t->magic);
413 t->sequence = fio_swap32(t->sequence);
414 t->time = fio_swap64(t->time);
415 t->sector = fio_swap64(t->sector);
416 t->bytes = fio_swap32(t->bytes);
417 t->action = fio_swap32(t->action);
418 t->pid = fio_swap32(t->pid);
419 t->device = fio_swap32(t->device);
420 t->cpu = fio_swap32(t->cpu);
421 t->error = fio_swap16(t->error);
422 t->pdu_len = fio_swap16(t->pdu_len);
425 static bool t_is_write(struct blk_io_trace *t)
427 return (t->action & BLK_TC_ACT(BLK_TC_WRITE | BLK_TC_DISCARD)) != 0;
430 static enum fio_ddir t_get_ddir(struct blk_io_trace *t)
432 if (t->action & BLK_TC_ACT(BLK_TC_READ))
434 else if (t->action & BLK_TC_ACT(BLK_TC_WRITE))
436 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
442 static void depth_inc(struct blk_io_trace *t, int *depth)
446 ddir = t_get_ddir(t);
447 if (ddir != DDIR_INVAL)
451 static void depth_dec(struct blk_io_trace *t, int *depth)
455 ddir = t_get_ddir(t);
456 if (ddir != DDIR_INVAL)
460 static void depth_end(struct blk_io_trace *t, int *this_depth, int *depth)
462 enum fio_ddir ddir = DDIR_INVAL;
464 ddir = t_get_ddir(t);
465 if (ddir != DDIR_INVAL) {
466 depth[ddir] = max(depth[ddir], this_depth[ddir]);
467 this_depth[ddir] = 0;
472 * Load a blktrace file by reading all the blk_io_trace entries, and storing
473 * them as io_pieces like the fio text version would do.
475 bool load_blktrace(struct thread_data *td, const char *filename, int need_swap)
477 struct blk_io_trace t;
478 unsigned long ios[DDIR_RWDIR_SYNC_CNT] = { };
479 unsigned int rw_bs[DDIR_RWDIR_CNT] = { };
480 unsigned long skipped_writes;
482 int fd, i, old_state, max_depth;
484 int this_depth[DDIR_RWDIR_CNT] = { };
485 int depth[DDIR_RWDIR_CNT] = { };
487 fd = open(filename, O_RDONLY);
489 td_verror(td, errno, "open blktrace file");
493 fifo = fifo_alloc(TRACE_FIFO_SIZE);
495 old_state = td_bump_runstate(td, TD_SETTING_UP);
500 int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t));
506 else if (ret < (int) sizeof(t)) {
507 log_err("fio: short fifo get\n");
514 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
515 log_err("fio: bad magic in blktrace data: %x\n",
519 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
520 log_err("fio: bad blktrace version %d\n",
524 ret = discard_pdu(td, fifo, fd, &t);
526 td_verror(td, ret, "blktrace lseek");
528 } else if (t.pdu_len != ret) {
529 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
532 if ((t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) == 0) {
533 if ((t.action & 0xffff) == __BLK_TA_QUEUE)
534 depth_inc(&t, this_depth);
535 else if (((t.action & 0xffff) == __BLK_TA_BACKMERGE) ||
536 ((t.action & 0xffff) == __BLK_TA_FRONTMERGE))
537 depth_dec(&t, this_depth);
538 else if ((t.action & 0xffff) == __BLK_TA_COMPLETE)
539 depth_end(&t, this_depth, depth);
541 if (t_is_write(&t) && read_only) {
547 handle_trace(td, &t, ios, rw_bs);
550 for_each_file(td, f, i)
551 trace_add_open_close_event(td, f->fileno, FIO_LOG_CLOSE_FILE);
556 td_restore_runstate(td, old_state);
558 if (!td->files_index) {
559 log_err("fio: did not find replay device(s)\n");
564 * For stacked devices, we don't always get a COMPLETE event so
565 * the depth grows to insane values. Limit it to something sane(r).
568 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
571 else if (!depth[i] && ios[i])
573 max_depth = max(depth[i], max_depth);
577 log_err("fio: %s skips replay of %lu writes due to read-only\n",
578 td->o.name, skipped_writes);
580 if (!ios[DDIR_READ] && !ios[DDIR_WRITE] && !ios[DDIR_TRIM] &&
582 log_err("fio: found no ios in blktrace data\n");
584 } else if (ios[DDIR_READ] && !ios[DDIR_WRITE]) {
585 td->o.td_ddir = TD_DDIR_READ;
586 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
587 } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) {
588 td->o.td_ddir = TD_DDIR_WRITE;
589 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
591 td->o.td_ddir = TD_DDIR_RW;
592 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
593 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
594 td->o.max_bs[DDIR_TRIM] = rw_bs[DDIR_TRIM];
598 * We need to do direct/raw ios to the device, to avoid getting
599 * read-ahead in our way. But only do so if the minimum block size
600 * is a multiple of 4k, otherwise we don't know if it's safe to do so.
602 if (!fio_option_is_set(&td->o, odirect) && !(td_min_bs(td) & 4095))
606 * If depth wasn't manually set, use probed depth
608 if (!fio_option_is_set(&td->o, iodepth))
609 td->o.iodepth = td->o.iodepth_low = max_depth;
618 static int init_merge_param_list(fio_fp64_t *vals, struct blktrace_cursor *bcs,
619 int nr_logs, int def, size_t off)
623 while (len < FIO_IO_U_LIST_MAX_LEN && vals[len].u.f != 0.0)
626 if (len && len != nr_logs)
629 for (i = 0; i < nr_logs; i++) {
630 int *val = (int *)((char *)&bcs[i] + off);
633 *val = (int)vals[i].u.f;
640 static int find_earliest_io(struct blktrace_cursor *bcs, int nr_logs)
642 __u64 time = ~(__u64)0;
645 for (i = 0; i < nr_logs; i++) {
646 if (bcs[i].t.time < time) {
647 time = bcs[i].t.time;
655 static void merge_finish_file(struct blktrace_cursor *bcs, int i, int *nr_logs)
658 if (bcs[i].iter < bcs[i].nr_iter) {
659 lseek(bcs[i].fd, 0, SEEK_SET);
666 fifo_free(bcs[i].fifo);
669 /* keep active files contiguous */
670 memmove(&bcs[i], &bcs[*nr_logs], sizeof(bcs[i]));
673 static int read_trace(struct thread_data *td, struct blktrace_cursor *bc)
676 struct blk_io_trace *t = &bc->t;
679 /* read an io trace */
680 ret = trace_fifo_get(td, bc->fifo, bc->fd, t, sizeof(*t));
685 bc->length = bc->t.time;
687 } else if (ret < (int) sizeof(*t)) {
688 log_err("fio: short fifo get\n");
695 /* skip over actions that fio does not care about */
696 if ((t->action & 0xffff) != __BLK_TA_QUEUE ||
697 t_get_ddir(t) == DDIR_INVAL) {
698 ret = discard_pdu(td, bc->fifo, bc->fd, t);
700 td_verror(td, ret, "blktrace lseek");
702 } else if (t->pdu_len != ret) {
703 log_err("fio: discarded %d of %d\n", ret,
710 t->time = (t->time + bc->iter * bc->length) * bc->scalar / 100;
715 static int write_trace(FILE *fp, struct blk_io_trace *t)
717 /* pdu is not used so just write out only the io trace */
719 return fwrite((void *)t, sizeof(*t), 1, fp);
722 int merge_blktrace_iologs(struct thread_data *td)
724 int nr_logs = get_max_str_idx(td->o.read_iolog_file);
725 struct blktrace_cursor *bcs = malloc(sizeof(struct blktrace_cursor) *
727 struct blktrace_cursor *bc;
729 char *str, *ptr, *name, *merge_buf;
732 ret = init_merge_param_list(td->o.merge_blktrace_scalars, bcs, nr_logs,
733 100, offsetof(struct blktrace_cursor,
736 log_err("fio: merge_blktrace_scalars(%d) != nr_logs(%d)\n",
741 ret = init_merge_param_list(td->o.merge_blktrace_iters, bcs, nr_logs,
742 1, offsetof(struct blktrace_cursor,
745 log_err("fio: merge_blktrace_iters(%d) != nr_logs(%d)\n",
750 /* setup output file */
751 merge_fp = fopen(td->o.merge_blktrace_file, "w");
752 merge_buf = malloc(128 * 1024);
753 ret = setvbuf(merge_fp, merge_buf, _IOFBF, 128 * 1024);
757 /* setup input files */
758 str = ptr = strdup(td->o.read_iolog_file);
760 for (i = 0; (name = get_next_str(&ptr)) != NULL; i++) {
761 bcs[i].fd = open(name, O_RDONLY);
763 log_err("fio: could not open file: %s\n", name);
767 bcs[i].fifo = fifo_alloc(TRACE_FIFO_SIZE);
770 if (!is_blktrace(name, &bcs[i].swap)) {
771 log_err("fio: file is not a blktrace: %s\n", name);
775 ret = read_trace(td, &bcs[i]);
779 merge_finish_file(bcs, i, &nr_logs);
787 i = find_earliest_io(bcs, nr_logs);
789 /* skip over the pdu */
790 ret = discard_pdu(td, bc->fifo, bc->fd, &bc->t);
792 td_verror(td, ret, "blktrace lseek");
794 } else if (bc->t.pdu_len != ret) {
795 log_err("fio: discarded %d of %d\n", ret,
800 ret = write_trace(merge_fp, &bc->t);
801 ret = read_trace(td, bc);
805 merge_finish_file(bcs, i, &nr_logs);
808 /* set iolog file to read from the newly merged file */
809 td->o.read_iolog_file = td->o.merge_blktrace_file;
814 for (i = 0; i < nr_logs; i++) {
815 fifo_free(bcs[i].fifo);