2 * blktrace support code for fio
12 #include "blktrace_api.h"
13 #include "oslib/linux-dev-lookup.h"
15 #define TRACE_FIFO_SIZE 8192
18 * fifo refill frontend, to avoid reading data in trace sized bites
20 static int refill_fifo(struct thread_data *td, struct fifo *fifo, int fd)
22 char buf[TRACE_FIFO_SIZE];
27 if (total > fifo_room(fifo))
28 total = fifo_room(fifo);
30 ret = read(fd, buf, total);
32 td_verror(td, errno, "read blktrace file");
37 ret = fifo_put(fifo, buf, ret);
39 dprint(FD_BLKTRACE, "refill: filled %d bytes\n", ret);
44 * Retrieve 'len' bytes from the fifo, refilling if necessary.
46 static int trace_fifo_get(struct thread_data *td, struct fifo *fifo, int fd,
47 void *buf, unsigned int len)
49 if (fifo_len(fifo) < len) {
50 int ret = refill_fifo(td, fifo, fd);
56 return fifo_get(fifo, buf, len);
60 * Just discard the pdu by seeking past it.
62 static int discard_pdu(struct thread_data *td, struct fifo *fifo, int fd,
63 struct blk_io_trace *t)
68 dprint(FD_BLKTRACE, "discard pdu len %u\n", t->pdu_len);
69 return trace_fifo_get(td, fifo, fd, NULL, t->pdu_len);
73 * Check if this is a blktrace binary data file. We read a single trace
74 * into memory and check for the magic signature.
76 bool is_blktrace(const char *filename, int *need_swap)
78 struct blk_io_trace t;
81 fd = open(filename, O_RDONLY);
85 ret = read(fd, &t, sizeof(t));
89 perror("read blktrace");
91 } else if (ret != sizeof(t)) {
92 log_err("fio: short read on blktrace file\n");
96 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
102 * Maybe it needs to be endian swapped...
104 t.magic = fio_swap32(t.magic);
105 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
113 #define FMINORBITS 20
114 #define FMINORMASK ((1U << FMINORBITS) - 1)
115 #define FMAJOR(dev) ((unsigned int) ((dev) >> FMINORBITS))
116 #define FMINOR(dev) ((unsigned int) ((dev) & FMINORMASK))
118 static void trace_add_open_close_event(struct thread_data *td, int fileno, enum file_log_act action)
120 struct io_piece *ipo;
122 ipo = calloc(1, sizeof(*ipo));
125 ipo->ddir = DDIR_INVAL;
126 ipo->fileno = fileno;
127 ipo->file_action = action;
128 flist_add_tail(&ipo->list, &td->io_log_list);
131 static int get_dev_blocksize(const char *dev, unsigned int *bs)
135 fd = open(dev, O_RDONLY);
139 if (ioctl(fd, BLKSSZGET, bs) < 0) {
148 static int trace_add_file(struct thread_data *td, __u32 device,
151 static unsigned int last_maj, last_min, last_fileno, last_bs;
152 unsigned int maj = FMAJOR(device);
153 unsigned int min = FMINOR(device);
158 if (last_maj == maj && last_min == min) {
167 * check for this file in our list
169 for_each_file(td, f, i) {
170 if (f->major == maj && f->minor == min) {
171 last_fileno = f->fileno;
178 if (blktrace_lookup_device(td->o.replay_redirect, dev, maj, min)) {
179 unsigned int this_bs;
182 if (td->o.replay_redirect)
183 dprint(FD_BLKTRACE, "device lookup: %d/%d\n overridden"
184 " with: %s\n", maj, min,
185 td->o.replay_redirect);
187 dprint(FD_BLKTRACE, "device lookup: %d/%d\n", maj, min);
189 dprint(FD_BLKTRACE, "add devices %s\n", dev);
190 fileno = add_file_exclusive(td, dev);
192 if (get_dev_blocksize(dev, &this_bs))
196 td->files[fileno]->major = maj;
197 td->files[fileno]->minor = min;
198 td->files[fileno]->bs = this_bs;
199 trace_add_open_close_event(td, fileno, FIO_LOG_OPEN_FILE);
201 last_fileno = fileno;
210 static void t_bytes_align(struct thread_options *o, struct blk_io_trace *t)
212 if (!o->replay_align)
215 t->bytes = (t->bytes + o->replay_align - 1) & ~(o->replay_align - 1);
219 * Store blk_io_trace data in an ipo for later retrieval.
221 static void store_ipo(struct thread_data *td, unsigned long long offset,
222 unsigned int bytes, int rw, unsigned long long ttime,
223 int fileno, unsigned int bs)
225 struct io_piece *ipo;
227 ipo = calloc(1, sizeof(*ipo));
230 ipo->offset = offset * bs;
231 if (td->o.replay_scale)
232 ipo->offset = ipo->offset / td->o.replay_scale;
233 ipo_bytes_align(td->o.replay_align, ipo);
235 ipo->delay = ttime / 1000;
237 ipo->ddir = DDIR_WRITE;
239 ipo->ddir = DDIR_READ;
240 ipo->fileno = fileno;
242 dprint(FD_BLKTRACE, "store ddir=%d, off=%llu, len=%lu, delay=%lu\n",
243 ipo->ddir, ipo->offset,
244 ipo->len, ipo->delay);
245 queue_io_piece(td, ipo);
248 static void handle_trace_notify(struct blk_io_trace *t)
252 dprint(FD_BLKTRACE, "got process notify: %x, %d\n",
255 case BLK_TN_TIMESTAMP:
256 dprint(FD_BLKTRACE, "got timestamp notify: %x, %d\n",
262 dprint(FD_BLKTRACE, "unknown trace act %x\n", t->action);
267 static void handle_trace_discard(struct thread_data *td,
268 struct blk_io_trace *t,
269 unsigned long long ttime,
270 unsigned long *ios, unsigned int *rw_bs)
272 struct io_piece *ipo;
276 if (td->o.replay_skip & (1u << DDIR_TRIM))
279 ipo = calloc(1, sizeof(*ipo));
281 fileno = trace_add_file(td, t->device, &bs);
284 if (t->bytes > rw_bs[DDIR_TRIM])
285 rw_bs[DDIR_TRIM] = t->bytes;
287 td->o.size += t->bytes;
289 INIT_FLIST_HEAD(&ipo->list);
291 ipo->offset = t->sector * bs;
292 if (td->o.replay_scale)
293 ipo->offset = ipo->offset / td->o.replay_scale;
294 ipo_bytes_align(td->o.replay_align, ipo);
296 ipo->delay = ttime / 1000;
297 ipo->ddir = DDIR_TRIM;
298 ipo->fileno = fileno;
300 dprint(FD_BLKTRACE, "store discard, off=%llu, len=%lu, delay=%lu\n",
301 ipo->offset, ipo->len,
303 queue_io_piece(td, ipo);
306 static void handle_trace_fs(struct thread_data *td, struct blk_io_trace *t,
307 unsigned long long ttime, unsigned long *ios,
314 fileno = trace_add_file(td, t->device, &bs);
316 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
319 if (td->o.replay_skip & (1u << DDIR_WRITE))
322 if (td->o.replay_skip & (1u << DDIR_READ))
328 if (t->bytes > rw_bs[rw])
329 rw_bs[rw] = t->bytes;
332 td->o.size += t->bytes;
333 store_ipo(td, t->sector, t->bytes, rw, ttime, fileno, bs);
336 static void handle_trace_flush(struct thread_data *td, struct blk_io_trace *t,
337 unsigned long long ttime, unsigned long *ios)
339 struct io_piece *ipo;
343 if (td->o.replay_skip & (1u << DDIR_SYNC))
346 ipo = calloc(1, sizeof(*ipo));
348 fileno = trace_add_file(td, t->device, &bs);
350 ipo->delay = ttime / 1000;
351 ipo->ddir = DDIR_SYNC;
352 ipo->fileno = fileno;
355 dprint(FD_BLKTRACE, "store flush delay=%lu\n", ipo->delay);
356 queue_io_piece(td, ipo);
360 * We only care for queue traces, most of the others are side effects
361 * due to internal workings of the block layer.
363 static void handle_trace(struct thread_data *td, struct blk_io_trace *t,
364 unsigned long *ios, unsigned int *bs)
366 static unsigned long long last_ttime;
367 unsigned long long delay = 0;
369 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
372 if (!(t->action & BLK_TC_ACT(BLK_TC_NOTIFY))) {
373 if (!last_ttime || td->o.no_stall)
375 else if (td->o.replay_time_scale == 100)
376 delay = t->time - last_ttime;
378 double tmp = t->time - last_ttime;
381 scale = (double) 100.0 / (double) td->o.replay_time_scale;
385 last_ttime = t->time;
388 t_bytes_align(&td->o, t);
390 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
391 handle_trace_notify(t);
392 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
393 handle_trace_discard(td, t, delay, ios, bs);
394 else if (t->action & BLK_TC_ACT(BLK_TC_FLUSH))
395 handle_trace_flush(td, t, delay, ios);
397 handle_trace_fs(td, t, delay, ios, bs);
400 static void byteswap_trace(struct blk_io_trace *t)
402 t->magic = fio_swap32(t->magic);
403 t->sequence = fio_swap32(t->sequence);
404 t->time = fio_swap64(t->time);
405 t->sector = fio_swap64(t->sector);
406 t->bytes = fio_swap32(t->bytes);
407 t->action = fio_swap32(t->action);
408 t->pid = fio_swap32(t->pid);
409 t->device = fio_swap32(t->device);
410 t->cpu = fio_swap32(t->cpu);
411 t->error = fio_swap16(t->error);
412 t->pdu_len = fio_swap16(t->pdu_len);
415 static bool t_is_write(struct blk_io_trace *t)
417 return (t->action & BLK_TC_ACT(BLK_TC_WRITE | BLK_TC_DISCARD)) != 0;
420 static enum fio_ddir t_get_ddir(struct blk_io_trace *t)
422 if (t->action & BLK_TC_ACT(BLK_TC_READ))
424 else if (t->action & BLK_TC_ACT(BLK_TC_WRITE))
426 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
432 static void depth_inc(struct blk_io_trace *t, int *depth)
436 ddir = t_get_ddir(t);
437 if (ddir != DDIR_INVAL)
441 static void depth_dec(struct blk_io_trace *t, int *depth)
445 ddir = t_get_ddir(t);
446 if (ddir != DDIR_INVAL)
450 static void depth_end(struct blk_io_trace *t, int *this_depth, int *depth)
452 enum fio_ddir ddir = DDIR_INVAL;
454 ddir = t_get_ddir(t);
455 if (ddir != DDIR_INVAL) {
456 depth[ddir] = max(depth[ddir], this_depth[ddir]);
457 this_depth[ddir] = 0;
462 * Load a blktrace file by reading all the blk_io_trace entries, and storing
463 * them as io_pieces like the fio text version would do.
465 bool load_blktrace(struct thread_data *td, const char *filename, int need_swap)
467 struct blk_io_trace t;
468 unsigned long ios[DDIR_RWDIR_SYNC_CNT] = { };
469 unsigned int rw_bs[DDIR_RWDIR_CNT] = { };
470 unsigned long skipped_writes;
472 int fd, i, old_state, max_depth;
474 int this_depth[DDIR_RWDIR_CNT] = { };
475 int depth[DDIR_RWDIR_CNT] = { };
477 fd = open(filename, O_RDONLY);
479 td_verror(td, errno, "open blktrace file");
483 fifo = fifo_alloc(TRACE_FIFO_SIZE);
485 old_state = td_bump_runstate(td, TD_SETTING_UP);
490 int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t));
496 else if (ret < (int) sizeof(t)) {
497 log_err("fio: short fifo get\n");
504 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
505 log_err("fio: bad magic in blktrace data: %x\n",
509 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
510 log_err("fio: bad blktrace version %d\n",
514 ret = discard_pdu(td, fifo, fd, &t);
516 td_verror(td, ret, "blktrace lseek");
518 } else if (t.pdu_len != ret) {
519 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
522 if ((t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) == 0) {
523 if ((t.action & 0xffff) == __BLK_TA_QUEUE)
524 depth_inc(&t, this_depth);
525 else if (((t.action & 0xffff) == __BLK_TA_BACKMERGE) ||
526 ((t.action & 0xffff) == __BLK_TA_FRONTMERGE))
527 depth_dec(&t, this_depth);
528 else if ((t.action & 0xffff) == __BLK_TA_COMPLETE)
529 depth_end(&t, this_depth, depth);
531 if (t_is_write(&t) && read_only) {
537 handle_trace(td, &t, ios, rw_bs);
540 for_each_file(td, f, i)
541 trace_add_open_close_event(td, f->fileno, FIO_LOG_CLOSE_FILE);
546 td_restore_runstate(td, old_state);
548 if (!td->files_index) {
549 log_err("fio: did not find replay device(s)\n");
554 * For stacked devices, we don't always get a COMPLETE event so
555 * the depth grows to insane values. Limit it to something sane(r).
558 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
561 else if (!depth[i] && ios[i])
563 max_depth = max(depth[i], max_depth);
567 log_err("fio: %s skips replay of %lu writes due to read-only\n",
568 td->o.name, skipped_writes);
570 if (!ios[DDIR_READ] && !ios[DDIR_WRITE] && !ios[DDIR_TRIM] &&
572 log_err("fio: found no ios in blktrace data\n");
574 } else if (ios[DDIR_READ] && !ios[DDIR_WRITE]) {
575 td->o.td_ddir = TD_DDIR_READ;
576 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
577 } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) {
578 td->o.td_ddir = TD_DDIR_WRITE;
579 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
581 td->o.td_ddir = TD_DDIR_RW;
582 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
583 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
584 td->o.max_bs[DDIR_TRIM] = rw_bs[DDIR_TRIM];
588 * We need to do direct/raw ios to the device, to avoid getting
589 * read-ahead in our way. But only do so if the minimum block size
590 * is a multiple of 4k, otherwise we don't know if it's safe to do so.
592 if (!fio_option_is_set(&td->o, odirect) && !(td_min_bs(td) & 4095))
596 * If depth wasn't manually set, use probed depth
598 if (!fio_option_is_set(&td->o, iodepth))
599 td->o.iodepth = td->o.iodepth_low = max_depth;