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))
136 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
139 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
141 if (t->bytes > bs[rw])
145 td->o.size += t->bytes;
146 store_ipo(td, t->sector, t->bytes, rw, ttime);
150 * Load a blktrace file by reading all the blk_io_trace entries, and storing
151 * them as io_pieces like the fio text version would do.
153 int load_blktrace(struct thread_data *td, const char *filename)
155 unsigned long long ttime, delay;
156 struct blk_io_trace t;
157 unsigned long ios[2];
159 unsigned int rw_bs[2];
163 fd = open(filename, O_RDONLY);
165 td_verror(td, errno, "open blktrace file");
169 fifo = fifo_alloc(TRACE_FIFO_SIZE);
176 rw_bs[0] = rw_bs[1] = 0;
178 int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t));
184 else if (ret < (int) sizeof(t)) {
185 log_err("fio: short fifo get\n");
189 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
190 log_err("fio: bad magic in blktrace data: %x\n", t.magic);
193 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
194 log_err("fio: bad blktrace version %d\n", t.magic & 0xff);
197 ret = discard_pdu(td, fifo, fd, &t);
199 td_verror(td, ret, "blktrace lseek");
201 } else if (t.pdu_len != ret) {
202 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
211 delay = t.time - ttime;
212 handle_trace(td, &t, delay, ios, rw_bs);
220 if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) {
221 log_err("fio: found no ios in blktrace data\n");
223 } else if (ios[DDIR_READ] && !ios[DDIR_READ]) {
224 td->o.td_ddir = TD_DDIR_READ;
225 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
226 } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) {
227 td->o.td_ddir = TD_DDIR_WRITE;
228 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
230 td->o.td_ddir = TD_DDIR_RW;
231 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
232 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
236 * We need to do direct/raw ios to the device, to avoid getting
237 * read-ahead in our way.