2 * blktrace support code for fio
12 #include "blktrace_api.h"
13 #include "oslib/linux-dev-lookup.h"
16 * Just discard the pdu by seeking past it.
18 static int discard_pdu(FILE* f, struct blk_io_trace *t)
23 dprint(FD_BLKTRACE, "discard pdu len %u\n", t->pdu_len);
24 if (fseek(f, t->pdu_len, SEEK_CUR) < 0)
31 * Check if this is a blktrace binary data file. We read a single trace
32 * into memory and check for the magic signature.
34 bool is_blktrace(const char *filename, int *need_swap)
36 struct blk_io_trace t;
39 fd = open(filename, O_RDONLY);
43 ret = read(fd, &t, sizeof(t));
47 perror("read blktrace");
49 } else if (ret != sizeof(t)) {
50 log_err("fio: short read on blktrace file\n");
54 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
60 * Maybe it needs to be endian swapped...
62 t.magic = fio_swap32(t.magic);
63 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
72 #define FMINORMASK ((1U << FMINORBITS) - 1)
73 #define FMAJOR(dev) ((unsigned int) ((dev) >> FMINORBITS))
74 #define FMINOR(dev) ((unsigned int) ((dev) & FMINORMASK))
76 static void trace_add_open_close_event(struct thread_data *td, int fileno, enum file_log_act action)
80 ipo = calloc(1, sizeof(*ipo));
83 ipo->ddir = DDIR_INVAL;
85 ipo->file_action = action;
86 flist_add_tail(&ipo->list, &td->io_log_list);
89 static int trace_add_file(struct thread_data *td, __u32 device)
91 static unsigned int last_maj, last_min, last_fileno;
92 unsigned int maj = FMAJOR(device);
93 unsigned int min = FMINOR(device);
98 if (last_maj == maj && last_min == min)
105 * check for this file in our list
107 for_each_file(td, f, i)
108 if (f->major == maj && f->minor == min) {
109 last_fileno = f->fileno;
114 if (blktrace_lookup_device(td->o.replay_redirect, dev, maj, min)) {
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);
122 dprint(FD_BLKTRACE, "device lookup: %d/%d\n", maj, min);
124 dprint(FD_BLKTRACE, "add devices %s\n", dev);
125 fileno = add_file_exclusive(td, dev);
127 td->files[fileno]->major = maj;
128 td->files[fileno]->minor = min;
129 trace_add_open_close_event(td, fileno, FIO_LOG_OPEN_FILE);
130 last_fileno = fileno;
136 static void t_bytes_align(struct thread_options *o, struct blk_io_trace *t)
138 if (!o->replay_align)
141 t->bytes = (t->bytes + o->replay_align - 1) & ~(o->replay_align - 1);
145 * Store blk_io_trace data in an ipo for later retrieval.
147 static void store_ipo(struct thread_data *td, unsigned long long offset,
148 unsigned int bytes, int rw, unsigned long long ttime,
151 struct io_piece *ipo;
153 ipo = calloc(1, sizeof(*ipo));
156 ipo->offset = offset * 512;
157 if (td->o.replay_scale)
158 ipo->offset = ipo->offset / td->o.replay_scale;
159 ipo_bytes_align(td->o.replay_align, ipo);
161 ipo->delay = ttime / 1000;
163 ipo->ddir = DDIR_WRITE;
165 ipo->ddir = DDIR_READ;
166 ipo->fileno = fileno;
168 dprint(FD_BLKTRACE, "store ddir=%d, off=%llu, len=%lu, delay=%lu\n",
169 ipo->ddir, ipo->offset,
170 ipo->len, ipo->delay);
171 queue_io_piece(td, ipo);
174 static void handle_trace_notify(struct blk_io_trace *t)
178 dprint(FD_BLKTRACE, "got process notify: %x, %d\n",
181 case BLK_TN_TIMESTAMP:
182 dprint(FD_BLKTRACE, "got timestamp notify: %x, %d\n",
188 dprint(FD_BLKTRACE, "unknown trace act %x\n", t->action);
193 static void handle_trace_discard(struct thread_data *td,
194 struct blk_io_trace *t,
195 unsigned long long ttime,
196 unsigned long *ios, unsigned int *bs)
198 struct io_piece *ipo;
201 if (td->o.replay_skip & (1u << DDIR_TRIM))
204 ipo = calloc(1, sizeof(*ipo));
206 fileno = trace_add_file(td, t->device);
209 if (t->bytes > bs[DDIR_TRIM])
210 bs[DDIR_TRIM] = t->bytes;
212 td->o.size += t->bytes;
214 INIT_FLIST_HEAD(&ipo->list);
216 ipo->offset = t->sector * 512;
217 if (td->o.replay_scale)
218 ipo->offset = ipo->offset / td->o.replay_scale;
219 ipo_bytes_align(td->o.replay_align, ipo);
221 ipo->delay = ttime / 1000;
222 ipo->ddir = DDIR_TRIM;
223 ipo->fileno = fileno;
225 dprint(FD_BLKTRACE, "store discard, off=%llu, len=%lu, delay=%lu\n",
226 ipo->offset, ipo->len,
228 queue_io_piece(td, ipo);
231 static void dump_trace(struct blk_io_trace *t)
233 log_err("blktrace: ignoring zero byte trace: action=%x\n", t->action);
236 static void handle_trace_fs(struct thread_data *td, struct blk_io_trace *t,
237 unsigned long long ttime, unsigned long *ios,
243 fileno = trace_add_file(td, t->device);
245 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
248 if (td->o.replay_skip & (1u << DDIR_WRITE))
251 if (td->o.replay_skip & (1u << DDIR_READ))
256 if (!fio_did_warn(FIO_WARN_BTRACE_ZERO))
261 if (t->bytes > bs[rw])
265 td->o.size += t->bytes;
266 store_ipo(td, t->sector, t->bytes, rw, ttime, fileno);
269 static void handle_trace_flush(struct thread_data *td, struct blk_io_trace *t,
270 unsigned long long ttime, unsigned long *ios)
272 struct io_piece *ipo;
275 if (td->o.replay_skip & (1u << DDIR_SYNC))
278 ipo = calloc(1, sizeof(*ipo));
280 fileno = trace_add_file(td, t->device);
282 ipo->delay = ttime / 1000;
283 ipo->ddir = DDIR_SYNC;
284 ipo->fileno = fileno;
287 dprint(FD_BLKTRACE, "store flush delay=%lu\n", ipo->delay);
288 queue_io_piece(td, ipo);
292 * We only care for queue traces, most of the others are side effects
293 * due to internal workings of the block layer.
295 static void handle_trace(struct thread_data *td, struct blk_io_trace *t,
296 unsigned long *ios, unsigned int *bs)
298 static unsigned long long last_ttime;
299 unsigned long long delay = 0;
301 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
304 if (!(t->action & BLK_TC_ACT(BLK_TC_NOTIFY))) {
305 if (!last_ttime || td->o.no_stall)
307 else if (td->o.replay_time_scale == 100)
308 delay = t->time - last_ttime;
310 double tmp = t->time - last_ttime;
313 scale = (double) 100.0 / (double) td->o.replay_time_scale;
317 last_ttime = t->time;
320 t_bytes_align(&td->o, t);
322 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
323 handle_trace_notify(t);
324 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
325 handle_trace_discard(td, t, delay, ios, bs);
326 else if (t->action & BLK_TC_ACT(BLK_TC_FLUSH))
327 handle_trace_flush(td, t, delay, ios);
329 handle_trace_fs(td, t, delay, ios, bs);
332 static void byteswap_trace(struct blk_io_trace *t)
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);
347 static bool t_is_write(struct blk_io_trace *t)
349 return (t->action & BLK_TC_ACT(BLK_TC_WRITE | BLK_TC_DISCARD)) != 0;
352 static enum fio_ddir t_get_ddir(struct blk_io_trace *t)
354 if (t->action & BLK_TC_ACT(BLK_TC_READ))
356 else if (t->action & BLK_TC_ACT(BLK_TC_WRITE))
358 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
364 static void depth_inc(struct blk_io_trace *t, int *depth)
368 ddir = t_get_ddir(t);
369 if (ddir != DDIR_INVAL)
373 static void depth_dec(struct blk_io_trace *t, int *depth)
377 ddir = t_get_ddir(t);
378 if (ddir != DDIR_INVAL)
382 static void depth_end(struct blk_io_trace *t, int *this_depth, int *depth)
384 enum fio_ddir ddir = DDIR_INVAL;
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;
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.
397 bool load_blktrace(struct thread_data *td, const char *filename, int need_swap)
399 struct blk_io_trace t;
400 unsigned long ios[DDIR_RWDIR_SYNC_CNT] = { };
401 unsigned int rw_bs[DDIR_RWDIR_CNT] = { };
402 unsigned long skipped_writes;
404 int i, old_state, max_depth;
405 struct fio_file *fiof;
406 int this_depth[DDIR_RWDIR_CNT] = { };
407 int depth[DDIR_RWDIR_CNT] = { };
409 f = fopen(filename, "rb");
411 td_verror(td, errno, "open blktrace file");
415 old_state = td_bump_runstate(td, TD_SETTING_UP);
420 int ret = fread(&t, 1, sizeof(t), f);
423 td_verror(td, errno, "read blktrace file");
425 } else if (feof(f)) {
427 } else if (ret < (int) sizeof(t)) {
428 log_err("fio: iolog short read\n");
435 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
436 log_err("fio: bad magic in blktrace data: %x\n",
440 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
441 log_err("fio: bad blktrace version %d\n",
445 ret = discard_pdu(f, &t);
447 td_verror(td, -ret, "blktrace lseek");
450 if ((t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) == 0) {
451 if ((t.action & 0xffff) == __BLK_TA_QUEUE)
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);
459 if (t_is_write(&t) && read_only) {
465 handle_trace(td, &t, ios, rw_bs);
468 for_each_file(td, fiof, i)
469 trace_add_open_close_event(td, fiof->fileno, FIO_LOG_CLOSE_FILE);
473 td_restore_runstate(td, old_state);
475 if (!td->files_index) {
476 log_err("fio: did not find replay device(s)\n");
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).
485 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
488 else if (!depth[i] && ios[i])
490 max_depth = max(depth[i], max_depth);
494 log_err("fio: %s skips replay of %lu writes due to read-only\n",
495 td->o.name, skipped_writes);
497 if (!ios[DDIR_READ] && !ios[DDIR_WRITE] && !ios[DDIR_TRIM] &&
499 log_err("fio: found no ios in blktrace data\n");
504 if (ios[DDIR_READ]) {
505 td->o.td_ddir |= TD_DDIR_READ;
506 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
508 if (ios[DDIR_WRITE]) {
509 td->o.td_ddir |= TD_DDIR_WRITE;
510 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
512 if (ios[DDIR_TRIM]) {
513 td->o.td_ddir |= TD_DDIR_TRIM;
514 td->o.max_bs[DDIR_TRIM] = rw_bs[DDIR_TRIM];
518 * We need to do direct/raw ios to the device, to avoid getting
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.
522 if (!fio_option_is_set(&td->o, odirect) && !(td_min_bs(td) & 4095))
526 * If depth wasn't manually set, use probed depth
528 if (!fio_option_is_set(&td->o, iodepth))
529 td->o.iodepth = td->o.iodepth_low = max_depth;
537 static int init_merge_param_list(fio_fp64_t *vals, struct blktrace_cursor *bcs,
538 int nr_logs, int def, size_t off)
542 while (len < FIO_IO_U_LIST_MAX_LEN && vals[len].u.f != 0.0)
545 if (len && len != nr_logs)
548 for (i = 0; i < nr_logs; i++) {
549 int *val = (int *)((char *)&bcs[i] + off);
552 *val = (int)vals[i].u.f;
559 static int find_earliest_io(struct blktrace_cursor *bcs, int nr_logs)
561 __u64 time = ~(__u64)0;
564 for (i = 0; i < nr_logs; i++) {
565 if (bcs[i].t.time < time) {
566 time = bcs[i].t.time;
574 static void merge_finish_file(struct blktrace_cursor *bcs, int i, int *nr_logs)
577 if (bcs[i].iter < bcs[i].nr_iter) {
578 fseek(bcs[i].f, 0, SEEK_SET);
587 /* keep active files contiguous */
588 memmove(&bcs[i], &bcs[*nr_logs], sizeof(bcs[i]));
591 static int read_trace(struct thread_data *td, struct blktrace_cursor *bc)
594 struct blk_io_trace *t = &bc->t;
597 /* read an io trace */
598 ret = fread(&t, 1, sizeof(t), bc->f);
600 td_verror(td, errno, "read blktrace file");
602 } else if (feof(bc->f)) {
604 bc->length = bc->t.time;
606 } else if (ret < (int) sizeof(*t)) {
607 log_err("fio: iolog short read\n");
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) {
617 ret = discard_pdu(bc->f, t);
619 td_verror(td, -ret, "blktrace lseek");
625 t->time = (t->time + bc->iter * bc->length) * bc->scalar / 100;
630 static int write_trace(FILE *fp, struct blk_io_trace *t)
632 /* pdu is not used so just write out only the io trace */
634 return fwrite((void *)t, sizeof(*t), 1, fp);
637 int merge_blktrace_iologs(struct thread_data *td)
639 int nr_logs = get_max_str_idx(td->o.read_iolog_file);
640 struct blktrace_cursor *bcs = malloc(sizeof(struct blktrace_cursor) *
642 struct blktrace_cursor *bc;
644 char *str, *ptr, *name, *merge_buf;
647 ret = init_merge_param_list(td->o.merge_blktrace_scalars, bcs, nr_logs,
648 100, offsetof(struct blktrace_cursor,
651 log_err("fio: merge_blktrace_scalars(%d) != nr_logs(%d)\n",
656 ret = init_merge_param_list(td->o.merge_blktrace_iters, bcs, nr_logs,
657 1, offsetof(struct blktrace_cursor,
660 log_err("fio: merge_blktrace_iters(%d) != nr_logs(%d)\n",
665 /* setup output file */
666 merge_fp = fopen(td->o.merge_blktrace_file, "w");
667 merge_buf = malloc(128 * 1024);
670 ret = setvbuf(merge_fp, merge_buf, _IOFBF, 128 * 1024);
674 /* setup input files */
675 str = ptr = strdup(td->o.read_iolog_file);
677 for (i = 0; (name = get_next_str(&ptr)) != NULL; i++) {
678 bcs[i].f = fopen(name, "rb");
680 log_err("fio: could not open file: %s\n", name);
687 if (!is_blktrace(name, &bcs[i].swap)) {
688 log_err("fio: file is not a blktrace: %s\n", name);
693 ret = read_trace(td, &bcs[i]);
698 merge_finish_file(bcs, i, &nr_logs);
706 i = find_earliest_io(bcs, nr_logs);
708 /* skip over the pdu */
709 ret = discard_pdu(bc->f, &bc->t);
711 td_verror(td, -ret, "blktrace lseek");
715 ret = write_trace(merge_fp, &bc->t);
716 ret = read_trace(td, bc);
720 merge_finish_file(bcs, i, &nr_logs);
723 /* set iolog file to read from the newly merged file */
724 td->o.read_iolog_file = td->o.merge_blktrace_file;
729 for (i = 0; i < nr_logs; i++) {