2 * blktrace support code for fio
9 #include "blktrace_api.h"
11 #define TRACE_FIFO_SIZE 65536
14 * fifo refill frontend, to avoid reading data in trace sized bites
16 static int refill_fifo(struct thread_data *td, struct fifo *fifo, int fd)
18 char buf[TRACE_FIFO_SIZE];
23 if (total > fifo_room(fifo))
24 total = fifo_room(fifo);
26 ret = read(fd, buf, total);
28 td_verror(td, errno, "read blktrace file");
33 ret = fifo_put(fifo, buf, ret);
39 * Retrieve 'len' bytes from the fifo, refilling if necessary.
41 static int trace_fifo_get(struct thread_data *td, struct fifo *fifo, int fd,
42 void *buf, unsigned int len)
44 if (fifo_len(fifo) < len) {
45 int ret = refill_fifo(td, fifo, fd);
51 return fifo_get(fifo, buf, len);
55 * Just discard the pdu by seeking past it.
57 static int discard_pdu(struct thread_data *td, struct fifo *fifo, int fd,
58 struct blk_io_trace *t)
63 return trace_fifo_get(td, fifo, fd, NULL, t->pdu_len);
67 * Check if this is a blktrace binary data file. We read a single trace
68 * into memory and check for the magic signature.
70 int is_blktrace(const char *filename)
72 struct blk_io_trace t;
75 fd = open(filename, O_RDONLY);
77 perror("open blktrace");
81 ret = read(fd, &t, sizeof(t));
85 perror("read blktrace");
87 } else if (ret != sizeof(t)) {
88 log_err("fio: short read on blktrace file\n");
92 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
99 * Store blk_io_trace data in an ipo for later retrieval.
101 static void store_ipo(struct thread_data *td, unsigned long long offset,
102 unsigned int bytes, int rw, unsigned long long ttime)
104 struct io_piece *ipo = malloc(sizeof(*ipo));
106 memset(ipo, 0, sizeof(*ipo));
107 INIT_LIST_HEAD(&ipo->list);
109 * the 512 is wrong here, it should be the hardware sector size...
111 ipo->offset = offset * 512;
113 ipo->delay = ttime / 1000;
115 ipo->ddir = DDIR_WRITE;
117 ipo->ddir = DDIR_READ;
119 list_add_tail(&ipo->list, &td->io_log_list);
123 * We only care for queue traces, most of the others are side effects
124 * due to internal workings of the block layer.
126 static void handle_trace(struct thread_data *td, struct blk_io_trace *t,
127 unsigned long long ttime, unsigned long *ios,
132 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
134 if (t->action & BLK_TC_ACT(BLK_TC_PC))
138 * should not happen, need to look into that...
143 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
145 if (t->bytes > bs[rw])
149 td->o.size += t->bytes;
150 store_ipo(td, t->sector, t->bytes, rw, ttime);
154 * Load a blktrace file by reading all the blk_io_trace entries, and storing
155 * them as io_pieces like the fio text version would do.
157 int load_blktrace(struct thread_data *td, const char *filename)
159 unsigned long long ttime, delay;
160 struct blk_io_trace t;
161 unsigned long ios[2];
163 unsigned int rw_bs[2];
167 fd = open(filename, O_RDONLY);
169 td_verror(td, errno, "open blktrace file");
173 fifo = fifo_alloc(TRACE_FIFO_SIZE);
180 rw_bs[0] = rw_bs[1] = 0;
182 int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t));
188 else if (ret < (int) sizeof(t)) {
189 log_err("fio: short fifo get\n");
193 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
194 log_err("fio: bad magic in blktrace data: %x\n", t.magic);
197 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
198 log_err("fio: bad blktrace version %d\n", t.magic & 0xff);
201 ret = discard_pdu(td, fifo, fd, &t);
203 td_verror(td, ret, "blktrace lseek");
205 } else if (t.pdu_len != ret) {
206 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
215 delay = t.time - ttime;
216 handle_trace(td, &t, delay, ios, rw_bs);
224 if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) {
225 log_err("fio: found no ios in blktrace data\n");
227 } else if (ios[DDIR_READ] && !ios[DDIR_READ]) {
228 td->o.td_ddir = TD_DDIR_READ;
229 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
230 } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) {
231 td->o.td_ddir = TD_DDIR_WRITE;
232 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
234 td->o.td_ddir = TD_DDIR_RW;
235 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
236 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
240 * We need to do direct/raw ios to the device, to avoid getting
241 * read-ahead in our way.