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);
37 dprint(FD_BLKTRACE, "refill: filled %d bytes\n", ret);
42 * Retrieve 'len' bytes from the fifo, refilling if necessary.
44 static int trace_fifo_get(struct thread_data *td, struct fifo *fifo, int fd,
45 void *buf, unsigned int len)
47 if (fifo_len(fifo) < len) {
48 int ret = refill_fifo(td, fifo, fd);
54 return fifo_get(fifo, buf, len);
58 * Just discard the pdu by seeking past it.
60 static int discard_pdu(struct thread_data *td, struct fifo *fifo, int fd,
61 struct blk_io_trace *t)
66 dprint(FD_BLKTRACE, "discard pdu len %u\n", t->pdu_len);
67 return trace_fifo_get(td, fifo, fd, NULL, t->pdu_len);
71 * Check if this is a blktrace binary data file. We read a single trace
72 * into memory and check for the magic signature.
74 int is_blktrace(const char *filename)
76 struct blk_io_trace t;
79 fd = open(filename, O_RDONLY);
83 ret = read(fd, &t, sizeof(t));
87 perror("read blktrace");
89 } else if (ret != sizeof(t)) {
90 log_err("fio: short read on blktrace file\n");
94 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
100 static int lookup_device(char *path, unsigned int maj, unsigned int min)
111 while ((dir = readdir(D)) != NULL) {
114 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
117 sprintf(full_path, "%s/%s", path, dir->d_name);
118 if (lstat(full_path, &st) == -1) {
123 if (S_ISDIR(st.st_mode)) {
124 found = lookup_device(full_path, maj, min);
126 strcpy(path, full_path);
131 if (!S_ISBLK(st.st_mode))
134 if (maj == major(st.st_rdev) && min == minor(st.st_rdev)) {
135 dprint(FD_BLKTRACE, "device lookup: %d/%d\n", maj, min);
136 strcpy(path, full_path);
146 #define FMINORBITS 20
147 #define FMINORMASK ((1U << FMINORBITS) - 1)
148 #define FMAJOR(dev) ((unsigned int) ((dev) >> FMINORBITS))
149 #define FMINOR(dev) ((unsigned int) ((dev) & FMINORMASK))
151 static void trace_add_open_event(struct thread_data *td, int fileno)
153 struct io_piece *ipo;
155 ipo = calloc(1, sizeof(*ipo));
157 ipo->ddir = DDIR_INVAL;
158 ipo->fileno = fileno;
159 ipo->file_action = FIO_LOG_OPEN_FILE;
160 flist_add_tail(&ipo->list, &td->io_log_list);
163 static void trace_add_file(struct thread_data *td, __u32 device)
165 static unsigned int last_maj, last_min;
166 unsigned int maj = FMAJOR(device);
167 unsigned int min = FMINOR(device);
172 if (last_maj == maj && last_min == min)
179 * check for this file in our list
181 for_each_file(td, f, i)
182 if (f->major == maj && f->minor == min)
186 if (lookup_device(dev, maj, min)) {
189 dprint(FD_BLKTRACE, "add devices %s\n", dev);
190 fileno = add_file(td, dev);
191 trace_add_open_event(td, fileno);
196 * Store blk_io_trace data in an ipo for later retrieval.
198 static void store_ipo(struct thread_data *td, unsigned long long offset,
199 unsigned int bytes, int rw, unsigned long long ttime)
201 struct io_piece *ipo = malloc(sizeof(*ipo));
203 memset(ipo, 0, sizeof(*ipo));
204 INIT_FLIST_HEAD(&ipo->list);
206 * the 512 is wrong here, it should be the hardware sector size...
208 ipo->offset = offset * 512;
210 ipo->delay = ttime / 1000;
212 ipo->ddir = DDIR_WRITE;
214 ipo->ddir = DDIR_READ;
216 dprint(FD_BLKTRACE, "store ddir=%d, off=%llu, len=%lu, delay=%lu\n",
217 ipo->ddir, ipo->offset,
218 ipo->len, ipo->delay);
219 queue_io_piece(td, ipo);
222 static void handle_trace_notify( struct blk_io_trace *t)
226 printf("got process notify: %x, %d\n", t->action, t->pid);
228 case BLK_TN_TIMESTAMP:
229 printf("got timestamp notify: %x, %d\n", t->action, t->pid);
232 dprint(FD_BLKTRACE, "unknown trace act %x\n", t->action);
237 static void handle_trace_fs(struct thread_data *td, struct blk_io_trace *t,
238 unsigned long long ttime, unsigned long *ios,
243 trace_add_file(td, t->device);
245 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
247 if (t->bytes > bs[rw])
251 td->o.size += t->bytes;
252 store_ipo(td, t->sector, t->bytes, rw, ttime);
256 * We only care for queue traces, most of the others are side effects
257 * due to internal workings of the block layer.
259 static void handle_trace(struct thread_data *td, struct blk_io_trace *t,
260 unsigned long long ttime, unsigned long *ios,
263 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
265 if (t->action & BLK_TC_ACT(BLK_TC_PC))
268 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
269 handle_trace_notify(t);
271 handle_trace_fs(td, t, ttime, ios, bs);
275 * Load a blktrace file by reading all the blk_io_trace entries, and storing
276 * them as io_pieces like the fio text version would do.
278 int load_blktrace(struct thread_data *td, const char *filename)
280 unsigned long long ttime, delay;
281 struct blk_io_trace t;
282 unsigned long ios[2], skipped_writes;
284 unsigned int rw_bs[2];
288 fd = open(filename, O_RDONLY);
290 td_verror(td, errno, "open blktrace file");
294 fifo = fifo_alloc(TRACE_FIFO_SIZE);
301 rw_bs[0] = rw_bs[1] = 0;
304 int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t));
310 else if (ret < (int) sizeof(t)) {
311 log_err("fio: short fifo get\n");
315 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
316 log_err("fio: bad magic in blktrace data: %x\n",
320 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
321 log_err("fio: bad blktrace version %d\n",
325 ret = discard_pdu(td, fifo, fd, &t);
327 td_verror(td, ret, "blktrace lseek");
329 } else if (t.pdu_len != ret) {
330 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
333 if ((t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) == 0) {
341 delay = t.time - ttime;
342 if ((t.action & BLK_TC_ACT(BLK_TC_WRITE)) && read_only)
345 handle_trace(td, &t, delay, ios, rw_bs);
351 handle_trace(td, &t, delay, ios, rw_bs);
359 log_err("fio: %s skips replay of %lu writes due to read-only\n",
360 td->o.name, skipped_writes);
362 if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) {
363 log_err("fio: found no ios in blktrace data\n");
365 } else if (ios[DDIR_READ] && !ios[DDIR_READ]) {
366 td->o.td_ddir = TD_DDIR_READ;
367 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
368 } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) {
369 td->o.td_ddir = TD_DDIR_WRITE;
370 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
372 td->o.td_ddir = TD_DDIR_RW;
373 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
374 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
378 * We need to do direct/raw ios to the device, to avoid getting
379 * read-ahead in our way.