2 * blktrace support code for fio
11 #include "blktrace_api.h"
12 #include "oslib/linux-dev-lookup.h"
14 #define TRACE_FIFO_SIZE 8192
17 * fifo refill frontend, to avoid reading data in trace sized bites
19 static int refill_fifo(struct thread_data *td, struct fifo *fifo, int fd)
21 char buf[TRACE_FIFO_SIZE];
26 if (total > fifo_room(fifo))
27 total = fifo_room(fifo);
29 ret = read(fd, buf, total);
34 td_verror(td, read_err, "read blktrace file");
39 ret = fifo_put(fifo, buf, ret);
41 dprint(FD_BLKTRACE, "refill: filled %d bytes\n", ret);
46 * Retrieve 'len' bytes from the fifo, refilling if necessary.
48 static int trace_fifo_get(struct thread_data *td, struct fifo *fifo, int fd,
49 void *buf, unsigned int len)
51 if (fifo_len(fifo) < len) {
52 int ret = refill_fifo(td, fifo, fd);
58 return fifo_get(fifo, buf, len);
62 * Just discard the pdu by seeking past it.
64 static int discard_pdu(struct thread_data *td, struct fifo *fifo, int fd,
65 struct blk_io_trace *t)
70 dprint(FD_BLKTRACE, "discard pdu len %u\n", t->pdu_len);
71 return trace_fifo_get(td, fifo, fd, NULL, t->pdu_len);
75 * Check if this is a blktrace binary data file. We read a single trace
76 * into memory and check for the magic signature.
78 bool is_blktrace(const char *filename, int *need_swap)
80 struct blk_io_trace t;
83 fd = open(filename, O_RDONLY);
87 ret = read(fd, &t, sizeof(t));
91 perror("read blktrace");
93 } else if (ret != sizeof(t)) {
94 log_err("fio: short read on blktrace file\n");
98 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
104 * Maybe it needs to be endian swapped...
106 t.magic = fio_swap32(t.magic);
107 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
115 #define FMINORBITS 20
116 #define FMINORMASK ((1U << FMINORBITS) - 1)
117 #define FMAJOR(dev) ((unsigned int) ((dev) >> FMINORBITS))
118 #define FMINOR(dev) ((unsigned int) ((dev) & FMINORMASK))
120 static void trace_add_open_close_event(struct thread_data *td, int fileno, enum file_log_act action)
122 struct io_piece *ipo;
124 ipo = calloc(1, sizeof(*ipo));
127 ipo->ddir = DDIR_INVAL;
128 ipo->fileno = fileno;
129 ipo->file_action = action;
130 flist_add_tail(&ipo->list, &td->io_log_list);
133 static int trace_add_file(struct thread_data *td, __u32 device)
135 static unsigned int last_maj, last_min, last_fileno;
136 unsigned int maj = FMAJOR(device);
137 unsigned int min = FMINOR(device);
142 if (last_maj == maj && last_min == min)
149 * check for this file in our list
151 for_each_file(td, f, i)
152 if (f->major == maj && f->minor == min) {
153 last_fileno = f->fileno;
158 if (blktrace_lookup_device(td->o.replay_redirect, dev, maj, min)) {
161 if (td->o.replay_redirect)
162 dprint(FD_BLKTRACE, "device lookup: %d/%d\n overridden"
163 " with: %s\n", maj, min,
164 td->o.replay_redirect);
166 dprint(FD_BLKTRACE, "device lookup: %d/%d\n", maj, min);
168 dprint(FD_BLKTRACE, "add devices %s\n", dev);
169 fileno = add_file_exclusive(td, dev);
171 td->files[fileno]->major = maj;
172 td->files[fileno]->minor = min;
173 trace_add_open_close_event(td, fileno, FIO_LOG_OPEN_FILE);
174 last_fileno = fileno;
180 static void t_bytes_align(struct thread_options *o, struct blk_io_trace *t)
182 if (!o->replay_align)
185 t->bytes = (t->bytes + o->replay_align - 1) & ~(o->replay_align - 1);
189 * Store blk_io_trace data in an ipo for later retrieval.
191 static void store_ipo(struct thread_data *td, unsigned long long offset,
192 unsigned int bytes, int rw, unsigned long long ttime,
195 struct io_piece *ipo;
197 ipo = calloc(1, sizeof(*ipo));
200 ipo->offset = offset * 512;
201 if (td->o.replay_scale)
202 ipo->offset = ipo->offset / td->o.replay_scale;
203 ipo_bytes_align(td->o.replay_align, ipo);
205 ipo->delay = ttime / 1000;
207 ipo->ddir = DDIR_WRITE;
209 ipo->ddir = DDIR_READ;
210 ipo->fileno = fileno;
212 dprint(FD_BLKTRACE, "store ddir=%d, off=%llu, len=%lu, delay=%lu\n",
213 ipo->ddir, ipo->offset,
214 ipo->len, ipo->delay);
215 queue_io_piece(td, ipo);
218 static void handle_trace_notify(struct blk_io_trace *t)
222 dprint(FD_BLKTRACE, "got process notify: %x, %d\n",
225 case BLK_TN_TIMESTAMP:
226 dprint(FD_BLKTRACE, "got timestamp notify: %x, %d\n",
232 dprint(FD_BLKTRACE, "unknown trace act %x\n", t->action);
237 static void handle_trace_discard(struct thread_data *td,
238 struct blk_io_trace *t,
239 unsigned long long ttime,
240 unsigned long *ios, unsigned int *bs)
242 struct io_piece *ipo;
245 if (td->o.replay_skip & (1u << DDIR_TRIM))
248 ipo = calloc(1, sizeof(*ipo));
250 fileno = trace_add_file(td, t->device);
253 if (t->bytes > bs[DDIR_TRIM])
254 bs[DDIR_TRIM] = t->bytes;
256 td->o.size += t->bytes;
258 INIT_FLIST_HEAD(&ipo->list);
260 ipo->offset = t->sector * 512;
261 if (td->o.replay_scale)
262 ipo->offset = ipo->offset / td->o.replay_scale;
263 ipo_bytes_align(td->o.replay_align, ipo);
265 ipo->delay = ttime / 1000;
266 ipo->ddir = DDIR_TRIM;
267 ipo->fileno = fileno;
269 dprint(FD_BLKTRACE, "store discard, off=%llu, len=%lu, delay=%lu\n",
270 ipo->offset, ipo->len,
272 queue_io_piece(td, ipo);
275 static void dump_trace(struct blk_io_trace *t)
277 log_err("blktrace: ignoring zero byte trace: action=%x\n", t->action);
280 static void handle_trace_fs(struct thread_data *td, struct blk_io_trace *t,
281 unsigned long long ttime, unsigned long *ios,
287 fileno = trace_add_file(td, t->device);
289 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
292 if (td->o.replay_skip & (1u << DDIR_WRITE))
295 if (td->o.replay_skip & (1u << DDIR_READ))
300 if (!fio_did_warn(FIO_WARN_BTRACE_ZERO))
305 if (t->bytes > bs[rw])
309 td->o.size += t->bytes;
310 store_ipo(td, t->sector, t->bytes, rw, ttime, fileno);
313 static void handle_trace_flush(struct thread_data *td, struct blk_io_trace *t,
314 unsigned long long ttime, unsigned long *ios)
316 struct io_piece *ipo;
319 if (td->o.replay_skip & (1u << DDIR_SYNC))
322 ipo = calloc(1, sizeof(*ipo));
324 fileno = trace_add_file(td, t->device);
326 ipo->delay = ttime / 1000;
327 ipo->ddir = DDIR_SYNC;
328 ipo->fileno = fileno;
331 dprint(FD_BLKTRACE, "store flush delay=%lu\n", ipo->delay);
332 queue_io_piece(td, ipo);
336 * We only care for queue traces, most of the others are side effects
337 * due to internal workings of the block layer.
339 static void handle_trace(struct thread_data *td, struct blk_io_trace *t,
340 unsigned long *ios, unsigned int *bs)
342 static unsigned long long last_ttime;
343 unsigned long long delay = 0;
345 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
348 if (!(t->action & BLK_TC_ACT(BLK_TC_NOTIFY))) {
349 if (!last_ttime || td->o.no_stall)
351 else if (td->o.replay_time_scale == 100)
352 delay = t->time - last_ttime;
354 double tmp = t->time - last_ttime;
357 scale = (double) 100.0 / (double) td->o.replay_time_scale;
361 last_ttime = t->time;
364 t_bytes_align(&td->o, t);
366 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
367 handle_trace_notify(t);
368 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
369 handle_trace_discard(td, t, delay, ios, bs);
370 else if (t->action & BLK_TC_ACT(BLK_TC_FLUSH))
371 handle_trace_flush(td, t, delay, ios);
373 handle_trace_fs(td, t, delay, ios, bs);
376 static void byteswap_trace(struct blk_io_trace *t)
378 t->magic = fio_swap32(t->magic);
379 t->sequence = fio_swap32(t->sequence);
380 t->time = fio_swap64(t->time);
381 t->sector = fio_swap64(t->sector);
382 t->bytes = fio_swap32(t->bytes);
383 t->action = fio_swap32(t->action);
384 t->pid = fio_swap32(t->pid);
385 t->device = fio_swap32(t->device);
386 t->cpu = fio_swap32(t->cpu);
387 t->error = fio_swap16(t->error);
388 t->pdu_len = fio_swap16(t->pdu_len);
391 static bool t_is_write(struct blk_io_trace *t)
393 return (t->action & BLK_TC_ACT(BLK_TC_WRITE | BLK_TC_DISCARD)) != 0;
396 static enum fio_ddir t_get_ddir(struct blk_io_trace *t)
398 if (t->action & BLK_TC_ACT(BLK_TC_READ))
400 else if (t->action & BLK_TC_ACT(BLK_TC_WRITE))
402 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
408 static void depth_inc(struct blk_io_trace *t, int *depth)
412 ddir = t_get_ddir(t);
413 if (ddir != DDIR_INVAL)
417 static void depth_dec(struct blk_io_trace *t, int *depth)
421 ddir = t_get_ddir(t);
422 if (ddir != DDIR_INVAL)
426 static void depth_end(struct blk_io_trace *t, int *this_depth, int *depth)
428 enum fio_ddir ddir = DDIR_INVAL;
430 ddir = t_get_ddir(t);
431 if (ddir != DDIR_INVAL) {
432 depth[ddir] = max(depth[ddir], this_depth[ddir]);
433 this_depth[ddir] = 0;
438 * Load a blktrace file by reading all the blk_io_trace entries, and storing
439 * them as io_pieces like the fio text version would do.
441 bool load_blktrace(struct thread_data *td, const char *filename, int need_swap)
443 struct blk_io_trace t;
444 unsigned long ios[DDIR_RWDIR_SYNC_CNT] = { };
445 unsigned int rw_bs[DDIR_RWDIR_CNT] = { };
446 unsigned long skipped_writes;
448 int fd, i, old_state, max_depth;
450 int this_depth[DDIR_RWDIR_CNT] = { };
451 int depth[DDIR_RWDIR_CNT] = { };
453 fd = open(filename, O_RDONLY);
455 td_verror(td, errno, "open blktrace file");
459 fifo = fifo_alloc(TRACE_FIFO_SIZE);
461 old_state = td_bump_runstate(td, TD_SETTING_UP);
466 int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t));
472 else if (ret < (int) sizeof(t)) {
473 log_err("fio: short fifo get\n");
480 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
481 log_err("fio: bad magic in blktrace data: %x\n",
485 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
486 log_err("fio: bad blktrace version %d\n",
490 ret = discard_pdu(td, fifo, fd, &t);
492 td_verror(td, -ret, "blktrace lseek");
494 } else if (t.pdu_len != ret) {
495 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
498 if ((t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) == 0) {
499 if ((t.action & 0xffff) == __BLK_TA_QUEUE)
500 depth_inc(&t, this_depth);
501 else if (((t.action & 0xffff) == __BLK_TA_BACKMERGE) ||
502 ((t.action & 0xffff) == __BLK_TA_FRONTMERGE))
503 depth_dec(&t, this_depth);
504 else if ((t.action & 0xffff) == __BLK_TA_COMPLETE)
505 depth_end(&t, this_depth, depth);
507 if (t_is_write(&t) && read_only) {
513 handle_trace(td, &t, ios, rw_bs);
516 for_each_file(td, f, i)
517 trace_add_open_close_event(td, f->fileno, FIO_LOG_CLOSE_FILE);
522 td_restore_runstate(td, old_state);
524 if (!td->files_index) {
525 log_err("fio: did not find replay device(s)\n");
530 * For stacked devices, we don't always get a COMPLETE event so
531 * the depth grows to insane values. Limit it to something sane(r).
534 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
537 else if (!depth[i] && ios[i])
539 max_depth = max(depth[i], max_depth);
543 log_err("fio: %s skips replay of %lu writes due to read-only\n",
544 td->o.name, skipped_writes);
546 if (!ios[DDIR_READ] && !ios[DDIR_WRITE] && !ios[DDIR_TRIM] &&
548 log_err("fio: found no ios in blktrace data\n");
553 if (ios[DDIR_READ]) {
554 td->o.td_ddir |= TD_DDIR_READ;
555 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
557 if (ios[DDIR_WRITE]) {
558 td->o.td_ddir |= TD_DDIR_WRITE;
559 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
561 if (ios[DDIR_TRIM]) {
562 td->o.td_ddir |= TD_DDIR_TRIM;
563 td->o.max_bs[DDIR_TRIM] = rw_bs[DDIR_TRIM];
567 * We need to do direct/raw ios to the device, to avoid getting
568 * read-ahead in our way. But only do so if the minimum block size
569 * is a multiple of 4k, otherwise we don't know if it's safe to do so.
571 if (!fio_option_is_set(&td->o, odirect) && !(td_min_bs(td) & 4095))
575 * If depth wasn't manually set, use probed depth
577 if (!fio_option_is_set(&td->o, iodepth))
578 td->o.iodepth = td->o.iodepth_low = max_depth;
587 static int init_merge_param_list(fio_fp64_t *vals, struct blktrace_cursor *bcs,
588 int nr_logs, int def, size_t off)
592 while (len < FIO_IO_U_LIST_MAX_LEN && vals[len].u.f != 0.0)
595 if (len && len != nr_logs)
598 for (i = 0; i < nr_logs; i++) {
599 int *val = (int *)((char *)&bcs[i] + off);
602 *val = (int)vals[i].u.f;
609 static int find_earliest_io(struct blktrace_cursor *bcs, int nr_logs)
611 __u64 time = ~(__u64)0;
614 for (i = 0; i < nr_logs; i++) {
615 if (bcs[i].t.time < time) {
616 time = bcs[i].t.time;
624 static void merge_finish_file(struct blktrace_cursor *bcs, int i, int *nr_logs)
627 if (bcs[i].iter < bcs[i].nr_iter) {
628 lseek(bcs[i].fd, 0, SEEK_SET);
635 fifo_free(bcs[i].fifo);
638 /* keep active files contiguous */
639 memmove(&bcs[i], &bcs[*nr_logs], sizeof(bcs[i]));
642 static int read_trace(struct thread_data *td, struct blktrace_cursor *bc)
645 struct blk_io_trace *t = &bc->t;
648 /* read an io trace */
649 ret = trace_fifo_get(td, bc->fifo, bc->fd, t, sizeof(*t));
654 bc->length = bc->t.time;
656 } else if (ret < (int) sizeof(*t)) {
657 log_err("fio: short fifo get\n");
664 /* skip over actions that fio does not care about */
665 if ((t->action & 0xffff) != __BLK_TA_QUEUE ||
666 t_get_ddir(t) == DDIR_INVAL) {
667 ret = discard_pdu(td, bc->fifo, bc->fd, t);
669 td_verror(td, -ret, "blktrace lseek");
671 } else if (t->pdu_len != ret) {
672 log_err("fio: discarded %d of %d\n", ret,
679 t->time = (t->time + bc->iter * bc->length) * bc->scalar / 100;
684 static int write_trace(FILE *fp, struct blk_io_trace *t)
686 /* pdu is not used so just write out only the io trace */
688 return fwrite((void *)t, sizeof(*t), 1, fp);
691 int merge_blktrace_iologs(struct thread_data *td)
693 int nr_logs = get_max_str_idx(td->o.read_iolog_file);
694 struct blktrace_cursor *bcs = malloc(sizeof(struct blktrace_cursor) *
696 struct blktrace_cursor *bc;
698 char *str, *ptr, *name, *merge_buf;
701 ret = init_merge_param_list(td->o.merge_blktrace_scalars, bcs, nr_logs,
702 100, offsetof(struct blktrace_cursor,
705 log_err("fio: merge_blktrace_scalars(%d) != nr_logs(%d)\n",
710 ret = init_merge_param_list(td->o.merge_blktrace_iters, bcs, nr_logs,
711 1, offsetof(struct blktrace_cursor,
714 log_err("fio: merge_blktrace_iters(%d) != nr_logs(%d)\n",
719 /* setup output file */
720 merge_fp = fopen(td->o.merge_blktrace_file, "w");
721 merge_buf = malloc(128 * 1024);
724 ret = setvbuf(merge_fp, merge_buf, _IOFBF, 128 * 1024);
728 /* setup input files */
729 str = ptr = strdup(td->o.read_iolog_file);
731 for (i = 0; (name = get_next_str(&ptr)) != NULL; i++) {
732 bcs[i].fd = open(name, O_RDONLY);
734 log_err("fio: could not open file: %s\n", name);
739 bcs[i].fifo = fifo_alloc(TRACE_FIFO_SIZE);
742 if (!is_blktrace(name, &bcs[i].swap)) {
743 log_err("fio: file is not a blktrace: %s\n", name);
748 ret = read_trace(td, &bcs[i]);
753 merge_finish_file(bcs, i, &nr_logs);
761 i = find_earliest_io(bcs, nr_logs);
763 /* skip over the pdu */
764 ret = discard_pdu(td, bc->fifo, bc->fd, &bc->t);
766 td_verror(td, -ret, "blktrace lseek");
768 } else if (bc->t.pdu_len != ret) {
769 log_err("fio: discarded %d of %d\n", ret,
774 ret = write_trace(merge_fp, &bc->t);
775 ret = read_trace(td, bc);
779 merge_finish_file(bcs, i, &nr_logs);
782 /* set iolog file to read from the newly merged file */
783 td->o.read_iolog_file = td->o.merge_blktrace_file;
788 for (i = 0; i < nr_logs; i++) {
789 fifo_free(bcs[i].fifo);