2 * blktrace support code for fio
8 #include "blktrace_api.h"
10 static int discard_pdu(int fd, struct blk_io_trace *t)
15 if (lseek(fd, t->pdu_len, SEEK_CUR) < 0)
21 int is_blktrace(const char *filename)
23 struct blk_io_trace t;
26 fd = open(filename, O_RDONLY);
28 perror("open blktrace");
32 ret = read(fd, &t, sizeof(t));
36 perror("read blktrace");
38 } else if (ret != sizeof(t)) {
39 log_err("fio: short read on blktrace file\n");
43 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
49 static void store_ipo(struct thread_data *td, unsigned long long offset,
50 unsigned int bytes, int rw)
52 struct io_piece *ipo = malloc(sizeof(*ipo));
54 memset(ipo, 0, sizeof(*ipo));
55 INIT_LIST_HEAD(&ipo->list);
59 ipo->ddir = DDIR_WRITE;
61 ipo->ddir = DDIR_READ;
63 list_add_tail(&ipo->list, &td->io_log_list);
66 static void handle_trace(struct thread_data *td, struct blk_io_trace *t)
70 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
73 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
74 store_ipo(td, t->sector, t->bytes, rw);
77 int load_blktrace(struct thread_data *td, const char *filename)
79 struct blk_io_trace t;
82 fd = open(filename, O_RDONLY);
84 td_verror(td, errno, "open blktrace file");
89 int ret = read(fd, &t, sizeof(t));
92 td_verror(td, errno, "read blktrace file");
96 } else if (ret != sizeof(t)) {
97 log_err("fio: short read on blktrace file\n");
101 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
102 log_err("fio: bad magic in blktrace data\n");
105 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
106 log_err("fio: bad blktrace version %d\n", t.magic & 0xff);
109 ret = discard_pdu(fd, &t);
111 td_verror(td, ret, "blktrace lseek");
114 handle_trace(td, &t);