2 * blktrace support code for fio
9 #include "blktrace_api.h"
11 #define TRACE_FIFO_SIZE (sizeof(struct blk_io_trace) * 1000)
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];
19 unsigned int total, left;
25 while (total < TRACE_FIFO_SIZE) {
26 left = TRACE_FIFO_SIZE - total;
28 ret = read(fd, ptr, left);
30 td_verror(td, errno, "read blktrace file");
35 fifo_put(fifo, ptr, 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)
51 if (fifo_len(fifo) >= len)
52 return fifo_get(fifo, buf, len);
54 ret = refill_fifo(td, fifo, fd);
58 if (fifo_len(fifo) < len)
61 return fifo_get(fifo, buf, len);
65 * Just discard the pdu by seeking past it.
67 static int discard_pdu(int fd, struct blk_io_trace *t)
72 if (lseek(fd, t->pdu_len, SEEK_CUR) < 0)
79 * Check if this is a blktrace binary data file. We read a single trace
80 * into memory and check for the magic signature.
82 int is_blktrace(const char *filename)
84 struct blk_io_trace t;
87 fd = open(filename, O_RDONLY);
89 perror("open blktrace");
93 ret = read(fd, &t, sizeof(t));
97 perror("read blktrace");
99 } else if (ret != sizeof(t)) {
100 log_err("fio: short read on blktrace file\n");
104 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
111 * Store blk_io_trace data in an ipo for later retrieval.
113 static void store_ipo(struct thread_data *td, unsigned long long offset,
114 unsigned int bytes, int rw, unsigned long long ttime)
116 struct io_piece *ipo = malloc(sizeof(*ipo));
118 memset(ipo, 0, sizeof(*ipo));
119 INIT_LIST_HEAD(&ipo->list);
121 * the 512 is wrong here, it should be the hardware sector size...
123 ipo->offset = offset * 512;
125 ipo->delay = ttime / 1000;
127 ipo->ddir = DDIR_WRITE;
129 ipo->ddir = DDIR_READ;
131 list_add_tail(&ipo->list, &td->io_log_list);
135 * We only care for queue traces, most of the others are side effects
136 * due to internal workings of the block layer.
138 static void handle_trace(struct thread_data *td, struct blk_io_trace *t,
139 unsigned long long ttime, unsigned long *ios,
144 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
146 if (t->action & BLK_TC_ACT(BLK_TC_PC))
150 * should not happen, need to look into that...
155 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
157 if (t->bytes > bs[rw])
161 td->o.size += t->bytes;
162 store_ipo(td, t->sector, t->bytes, rw, ttime);
166 * Load a blktrace file by reading all the blk_io_trace entries, and storing
167 * them as io_pieces like the fio text version would do.
169 int load_blktrace(struct thread_data *td, const char *filename)
171 unsigned long long ttime, delay;
172 struct blk_io_trace t;
173 unsigned long ios[2];
175 unsigned int rw_bs[2];
179 fd = open(filename, O_RDONLY);
181 td_verror(td, errno, "open blktrace file");
185 fifo = fifo_alloc(TRACE_FIFO_SIZE);
192 rw_bs[0] = rw_bs[1] = 0;
194 int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t));
200 else if (ret < (int) sizeof(t)) {
201 log_err("fio: short fifo get\n");
205 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
206 log_err("fio: bad magic in blktrace data\n");
209 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
210 log_err("fio: bad blktrace version %d\n", t.magic & 0xff);
213 ret = discard_pdu(fd, &t);
215 td_verror(td, ret, "blktrace lseek");
224 delay = t.time - ttime;
225 handle_trace(td, &t, delay, ios, rw_bs);
232 if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) {
233 log_err("fio: found no ios in blktrace data\n");
235 } else if (ios[DDIR_READ] && !ios[DDIR_READ]) {
236 td->o.td_ddir = TD_DDIR_READ;
237 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
238 } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) {
239 td->o.td_ddir = TD_DDIR_WRITE;
240 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
242 td->o.td_ddir = TD_DDIR_RW;
243 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
244 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
248 * We need to do direct/raw ios to the device, to avoid getting
249 * read-ahead in our way.