2 * blktrace support code for fio
9 #include "blktrace_api.h"
12 * Just discard the pdu by seeking past it.
14 static int discard_pdu(int fd, struct blk_io_trace *t)
19 if (lseek(fd, t->pdu_len, SEEK_CUR) < 0)
26 * Check if this is a blktrace binary data file. We read a single trace
27 * into memory and check for the magic signature.
29 int is_blktrace(const char *filename)
31 struct blk_io_trace t;
34 fd = open(filename, O_RDONLY);
36 perror("open blktrace");
40 ret = read(fd, &t, sizeof(t));
44 perror("read blktrace");
46 } else if (ret != sizeof(t)) {
47 log_err("fio: short read on blktrace file\n");
51 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
58 * Store blk_io_trace data in an ipo for later retrieval.
60 static void store_ipo(struct thread_data *td, unsigned long long offset,
61 unsigned int bytes, int rw, unsigned long long ttime)
63 struct io_piece *ipo = malloc(sizeof(*ipo));
65 memset(ipo, 0, sizeof(*ipo));
66 INIT_LIST_HEAD(&ipo->list);
68 * the 512 is wrong here, it should be the hardware sector size...
70 ipo->offset = offset * 512;
72 ipo->delay = ttime / 1000;
74 ipo->ddir = DDIR_WRITE;
76 ipo->ddir = DDIR_READ;
78 list_add_tail(&ipo->list, &td->io_log_list);
82 * We only care for queue traces, most of the others are side effects
83 * due to internal workings of the block layer.
85 static void handle_trace(struct thread_data *td, struct blk_io_trace *t,
86 unsigned long long ttime, unsigned long *ios,
91 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
93 if (t->action & BLK_TC_ACT(BLK_TC_PC))
97 * should not happen, need to look into that...
102 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
104 if (t->bytes > bs[rw])
108 td->o.size += t->bytes;
109 store_ipo(td, t->sector, t->bytes, rw, ttime);
113 * Load a blktrace file by reading all the blk_io_trace entries, and storing
114 * them as io_pieces like the fio text version would do.
116 int load_blktrace(struct thread_data *td, const char *filename)
118 unsigned long long ttime, delay;
119 struct blk_io_trace t;
120 unsigned long ios[2];
122 unsigned int rw_bs[2];
125 fd = open(filename, O_RDONLY);
127 td_verror(td, errno, "open blktrace file");
136 rw_bs[0] = rw_bs[1] = 0;
139 * Once this is working fully, I'll add a layer between
140 * here and read to cache trace data. Then we can avoid
141 * doing itsy bitsy reads, but instead pull in a larger
142 * chunk of data at the time.
144 int ret = read(fd, &t, sizeof(t));
147 td_verror(td, errno, "read blktrace file");
151 } else if (ret != sizeof(t)) {
152 log_err("fio: short read on blktrace file\n");
156 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
157 log_err("fio: bad magic in blktrace data\n");
160 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
161 log_err("fio: bad blktrace version %d\n", t.magic & 0xff);
164 ret = discard_pdu(fd, &t);
166 td_verror(td, ret, "blktrace lseek");
175 delay = t.time - ttime;
176 handle_trace(td, &t, delay, ios, rw_bs);
183 if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) {
184 log_err("fio: found no ios in blktrace data\n");
186 } else if (ios[DDIR_READ] && !ios[DDIR_READ]) {
187 td->o.td_ddir = TD_DDIR_READ;
188 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
189 } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) {
190 td->o.td_ddir = TD_DDIR_WRITE;
191 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
193 td->o.td_ddir = TD_DDIR_RW;
194 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
195 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
199 * We need to do direct/raw ios to the device, to avoid getting
200 * read-ahead in our way.