+static void trace_add_file(struct thread_data *td, __u32 device)
+{
+ static unsigned int last_maj, last_min;
+ unsigned int maj = FMAJOR(device);
+ unsigned int min = FMINOR(device);
+ struct fio_file *f;
+ char dev[256];
+ unsigned int i;
+
+ if (last_maj == maj && last_min == min)
+ return;
+
+ last_maj = maj;
+ last_min = min;
+
+ /*
+ * check for this file in our list
+ */
+ for_each_file(td, f, i)
+ if (f->major == maj && f->minor == min)
+ return;
+
+ strcpy(dev, "/dev");
+ if (lookup_device(dev, maj, min)) {
+ int fileno;
+
+ dprint(FD_BLKTRACE, "add devices %s\n", dev);
+ fileno = add_file(td, dev);
+ trace_add_open_event(td, fileno);
+ }
+}
+
+/*
+ * Store blk_io_trace data in an ipo for later retrieval.
+ */
+static void store_ipo(struct thread_data *td, unsigned long long offset,
+ unsigned int bytes, int rw, unsigned long long ttime)
+{
+ struct io_piece *ipo = malloc(sizeof(*ipo));
+
+ memset(ipo, 0, sizeof(*ipo));
+ INIT_FLIST_HEAD(&ipo->list);
+ /*
+ * the 512 is wrong here, it should be the hardware sector size...
+ */
+ ipo->offset = offset * 512;
+ ipo->len = bytes;
+ ipo->delay = ttime / 1000;
+ if (rw)
+ ipo->ddir = DDIR_WRITE;
+ else
+ ipo->ddir = DDIR_READ;
+
+ dprint(FD_BLKTRACE, "store ddir=%d, off=%llu, len=%lu, delay=%lu\n",
+ ipo->ddir, ipo->offset,
+ ipo->len, ipo->delay);
+ queue_io_piece(td, ipo);
+}
+
+static void handle_trace_notify(struct blk_io_trace *t)
+{
+ switch (t->action) {
+ case BLK_TN_PROCESS:
+ printf("got process notify: %x, %d\n", t->action, t->pid);
+ break;
+ case BLK_TN_TIMESTAMP:
+ printf("got timestamp notify: %x, %d\n", t->action, t->pid);
+ break;
+ default:
+ dprint(FD_BLKTRACE, "unknown trace act %x\n", t->action);
+ break;
+ }
+}
+
+static void handle_trace_fs(struct thread_data *td, struct blk_io_trace *t,
+ unsigned long long ttime, unsigned long *ios,
+ unsigned int *bs)
+{
+ int rw;
+
+ trace_add_file(td, t->device);
+
+ rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
+
+ if (t->bytes > bs[rw])
+ bs[rw] = t->bytes;
+
+ ios[rw]++;
+ td->o.size += t->bytes;
+ store_ipo(td, t->sector, t->bytes, rw, ttime);
+}
+
+/*
+ * We only care for queue traces, most of the others are side effects
+ * due to internal workings of the block layer.
+ */
+static void handle_trace(struct thread_data *td, struct blk_io_trace *t,
+ unsigned long long ttime, unsigned long *ios,
+ unsigned int *bs)
+{
+ if ((t->action & 0xffff) != __BLK_TA_QUEUE)
+ return;
+ if (t->action & BLK_TC_ACT(BLK_TC_PC))
+ return;
+
+ if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
+ handle_trace_notify(t);
+ else
+ handle_trace_fs(td, t, ttime, ios, bs);
+}
+
+/*
+ * Load a blktrace file by reading all the blk_io_trace entries, and storing
+ * them as io_pieces like the fio text version would do.
+ */