2 * blktrace support code for fio
11 #include "blktrace_api.h"
13 #define TRACE_FIFO_SIZE 65536
16 * fifo refill frontend, to avoid reading data in trace sized bites
18 static int refill_fifo(struct thread_data *td, struct fifo *fifo, int fd)
20 char buf[TRACE_FIFO_SIZE];
25 if (total > fifo_room(fifo))
26 total = fifo_room(fifo);
28 ret = read(fd, buf, total);
30 td_verror(td, errno, "read blktrace file");
35 ret = fifo_put(fifo, buf, ret);
41 * Retrieve 'len' bytes from the fifo, refilling if necessary.
43 static int trace_fifo_get(struct thread_data *td, struct fifo *fifo, int fd,
44 void *buf, unsigned int len)
46 if (fifo_len(fifo) < len) {
47 int ret = refill_fifo(td, fifo, fd);
53 return fifo_get(fifo, buf, len);
57 * Just discard the pdu by seeking past it.
59 static int discard_pdu(struct thread_data *td, struct fifo *fifo, int fd,
60 struct blk_io_trace *t)
65 return trace_fifo_get(td, fifo, fd, NULL, t->pdu_len);
69 * Check if this is a blktrace binary data file. We read a single trace
70 * into memory and check for the magic signature.
72 int is_blktrace(const char *filename)
74 struct blk_io_trace t;
77 fd = open(filename, O_RDONLY);
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)
98 static int lookup_device(char *path, unsigned int maj, unsigned int min)
109 while ((dir = readdir(D)) != NULL) {
112 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
115 sprintf(full_path, "%s/%s", path, dir->d_name);
116 if (lstat(full_path, &st) == -1) {
121 if (S_ISDIR(st.st_mode)) {
122 found = lookup_device(full_path, maj, min);
124 strcpy(path, full_path);
129 if (!S_ISBLK(st.st_mode))
132 if (maj == major(st.st_rdev) && min == minor(st.st_rdev)) {
133 strcpy(path, full_path);
143 #define FMINORBITS 20
144 #define FMINORMASK ((1U << FMINORBITS) - 1)
145 #define FMAJOR(dev) ((unsigned int) ((dev) >> FMINORBITS))
146 #define FMINOR(dev) ((unsigned int) ((dev) & FMINORMASK))
148 static void trace_add_file(struct thread_data *td, __u32 device)
150 static unsigned int last_maj, last_min;
151 unsigned int maj = FMAJOR(device);
152 unsigned int min = FMINOR(device);
157 if (last_maj == maj && last_min == min)
164 * check for this file in our list
166 for_each_file(td, f, i)
167 if (f->major == maj && f->minor == min)
171 if (lookup_device(dev, maj, min))
176 * Store blk_io_trace data in an ipo for later retrieval.
178 static void store_ipo(struct thread_data *td, unsigned long long offset,
179 unsigned int bytes, int rw, unsigned long long ttime)
181 struct io_piece *ipo = malloc(sizeof(*ipo));
183 memset(ipo, 0, sizeof(*ipo));
184 INIT_LIST_HEAD(&ipo->list);
186 * the 512 is wrong here, it should be the hardware sector size...
188 ipo->offset = offset * 512;
190 ipo->delay = ttime / 1000;
192 ipo->ddir = DDIR_WRITE;
194 ipo->ddir = DDIR_READ;
196 list_add_tail(&ipo->list, &td->io_log_list);
200 * We only care for queue traces, most of the others are side effects
201 * due to internal workings of the block layer.
203 static void handle_trace(struct thread_data *td, struct blk_io_trace *t,
204 unsigned long long ttime, unsigned long *ios,
209 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
211 if (t->action & BLK_TC_ACT(BLK_TC_PC))
213 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
216 trace_add_file(td, t->device);
218 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
220 if (t->bytes > bs[rw])
224 td->o.size += t->bytes;
225 store_ipo(td, t->sector, t->bytes, rw, ttime);
229 * Load a blktrace file by reading all the blk_io_trace entries, and storing
230 * them as io_pieces like the fio text version would do.
232 int load_blktrace(struct thread_data *td, const char *filename)
234 unsigned long long ttime, delay;
235 struct blk_io_trace t;
236 unsigned long ios[2];
238 unsigned int rw_bs[2];
242 fd = open(filename, O_RDONLY);
244 td_verror(td, errno, "open blktrace file");
248 fifo = fifo_alloc(TRACE_FIFO_SIZE);
255 rw_bs[0] = rw_bs[1] = 0;
257 int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t));
263 else if (ret < (int) sizeof(t)) {
264 log_err("fio: short fifo get\n");
268 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
269 log_err("fio: bad magic in blktrace data: %x\n", t.magic);
272 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
273 log_err("fio: bad blktrace version %d\n", t.magic & 0xff);
276 ret = discard_pdu(td, fifo, fd, &t);
278 td_verror(td, ret, "blktrace lseek");
280 } else if (t.pdu_len != ret) {
281 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
290 delay = t.time - ttime;
291 handle_trace(td, &t, delay, ios, rw_bs);
299 if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) {
300 log_err("fio: found no ios in blktrace data\n");
302 } else if (ios[DDIR_READ] && !ios[DDIR_READ]) {
303 td->o.td_ddir = TD_DDIR_READ;
304 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
305 } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) {
306 td->o.td_ddir = TD_DDIR_WRITE;
307 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
309 td->o.td_ddir = TD_DDIR_RW;
310 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
311 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
315 * We need to do direct/raw ios to the device, to avoid getting
316 * read-ahead in our way.