2 * blktrace support code for fio
11 #include "blktrace_api.h"
13 #define TRACE_FIFO_SIZE 8192
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, int *need_swap)
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 * Maybe it needs to be endian swapped...
102 t.magic = fio_swap32(t.magic);
103 if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
111 static int lookup_device(struct thread_data *td, char *path, unsigned int maj,
123 while ((dir = readdir(D)) != NULL) {
126 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
129 sprintf(full_path, "%s%s%s", path, FIO_OS_PATH_SEPARATOR, dir->d_name);
130 if (lstat(full_path, &st) == -1) {
135 if (S_ISDIR(st.st_mode)) {
136 found = lookup_device(td, full_path, maj, min);
138 strcpy(path, full_path);
143 if (!S_ISBLK(st.st_mode))
147 * If replay_redirect is set then always return this device
148 * upon lookup which overrides the device lookup based on
149 * major minor in the actual blktrace
151 if (td->o.replay_redirect) {
152 dprint(FD_BLKTRACE, "device lookup: %d/%d\n overridden"
153 " with: %s\n", maj, min,
154 td->o.replay_redirect);
155 strcpy(path, td->o.replay_redirect);
160 if (maj == major(st.st_rdev) && min == minor(st.st_rdev)) {
161 dprint(FD_BLKTRACE, "device lookup: %d/%d\n", maj, min);
162 strcpy(path, full_path);
172 #define FMINORBITS 20
173 #define FMINORMASK ((1U << FMINORBITS) - 1)
174 #define FMAJOR(dev) ((unsigned int) ((dev) >> FMINORBITS))
175 #define FMINOR(dev) ((unsigned int) ((dev) & FMINORMASK))
177 static void trace_add_open_close_event(struct thread_data *td, int fileno, enum file_log_act action)
179 struct io_piece *ipo;
181 ipo = calloc(1, sizeof(*ipo));
184 ipo->ddir = DDIR_INVAL;
185 ipo->fileno = fileno;
186 ipo->file_action = action;
187 flist_add_tail(&ipo->list, &td->io_log_list);
190 static int trace_add_file(struct thread_data *td, __u32 device)
192 static unsigned int last_maj, last_min, last_fileno;
193 unsigned int maj = FMAJOR(device);
194 unsigned int min = FMINOR(device);
199 if (last_maj == maj && last_min == min)
206 * check for this file in our list
208 for_each_file(td, f, i)
209 if (f->major == maj && f->minor == min) {
210 last_fileno = f->fileno;
215 if (lookup_device(td, dev, maj, min)) {
218 dprint(FD_BLKTRACE, "add devices %s\n", dev);
219 fileno = add_file_exclusive(td, dev);
221 td->files[fileno]->major = maj;
222 td->files[fileno]->minor = min;
223 trace_add_open_close_event(td, fileno, FIO_LOG_OPEN_FILE);
224 last_fileno = fileno;
231 * Store blk_io_trace data in an ipo for later retrieval.
233 static void store_ipo(struct thread_data *td, unsigned long long offset,
234 unsigned int bytes, int rw, unsigned long long ttime,
237 struct io_piece *ipo = malloc(sizeof(*ipo));
242 * the 512 is wrong here, it should be the hardware sector size...
244 ipo->offset = offset * 512;
246 ipo->delay = ttime / 1000;
248 ipo->ddir = DDIR_WRITE;
250 ipo->ddir = DDIR_READ;
251 ipo->fileno = fileno;
253 dprint(FD_BLKTRACE, "store ddir=%d, off=%llu, len=%lu, delay=%lu\n",
254 ipo->ddir, ipo->offset,
255 ipo->len, ipo->delay);
256 queue_io_piece(td, ipo);
259 static void handle_trace_notify(struct blk_io_trace *t)
263 dprint(FD_BLKTRACE, "got process notify: %x, %d\n",
266 case BLK_TN_TIMESTAMP:
267 dprint(FD_BLKTRACE, "got timestamp notify: %x, %d\n",
273 dprint(FD_BLKTRACE, "unknown trace act %x\n", t->action);
278 static void handle_trace_discard(struct thread_data *td,
279 struct blk_io_trace *t,
280 unsigned long long ttime,
281 unsigned long *ios, unsigned int *bs)
283 struct io_piece *ipo = malloc(sizeof(*ipo));
287 fileno = trace_add_file(td, t->device);
290 if (t->bytes > bs[DDIR_TRIM])
291 bs[DDIR_TRIM] = t->bytes;
293 td->o.size += t->bytes;
295 memset(ipo, 0, sizeof(*ipo));
296 INIT_FLIST_HEAD(&ipo->list);
299 * the 512 is wrong here, it should be the hardware sector size...
301 ipo->offset = t->sector * 512;
303 ipo->delay = ttime / 1000;
304 ipo->ddir = DDIR_TRIM;
305 ipo->fileno = fileno;
307 dprint(FD_BLKTRACE, "store discard, off=%llu, len=%lu, delay=%lu\n",
308 ipo->offset, ipo->len,
310 queue_io_piece(td, ipo);
313 static void handle_trace_fs(struct thread_data *td, struct blk_io_trace *t,
314 unsigned long long ttime, unsigned long *ios,
320 fileno = trace_add_file(td, t->device);
322 rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
324 if (t->bytes > bs[rw])
328 td->o.size += t->bytes;
329 store_ipo(td, t->sector, t->bytes, rw, ttime, fileno);
333 * We only care for queue traces, most of the others are side effects
334 * due to internal workings of the block layer.
336 static void handle_trace(struct thread_data *td, struct blk_io_trace *t,
337 unsigned long *ios, unsigned int *bs)
339 static unsigned long long last_ttime;
340 unsigned long long delay;
342 if ((t->action & 0xffff) != __BLK_TA_QUEUE)
345 if (!(t->action & BLK_TC_ACT(BLK_TC_NOTIFY))) {
346 if (!last_ttime || td->o.no_stall) {
347 last_ttime = t->time;
350 delay = t->time - last_ttime;
351 last_ttime = t->time;
355 if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
356 handle_trace_notify(t);
357 else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
358 handle_trace_discard(td, t, delay, ios, bs);
360 handle_trace_fs(td, t, delay, ios, bs);
363 static void byteswap_trace(struct blk_io_trace *t)
365 t->magic = fio_swap32(t->magic);
366 t->sequence = fio_swap32(t->sequence);
367 t->time = fio_swap64(t->time);
368 t->sector = fio_swap64(t->sector);
369 t->bytes = fio_swap32(t->bytes);
370 t->action = fio_swap32(t->action);
371 t->pid = fio_swap32(t->pid);
372 t->device = fio_swap32(t->device);
373 t->cpu = fio_swap32(t->cpu);
374 t->error = fio_swap16(t->error);
375 t->pdu_len = fio_swap16(t->pdu_len);
378 static int t_is_write(struct blk_io_trace *t)
380 return (t->action & BLK_TC_ACT(BLK_TC_WRITE | BLK_TC_DISCARD)) != 0;
384 * Load a blktrace file by reading all the blk_io_trace entries, and storing
385 * them as io_pieces like the fio text version would do.
387 int load_blktrace(struct thread_data *td, const char *filename, int need_swap)
389 struct blk_io_trace t;
390 unsigned long ios[DDIR_RWDIR_CNT], skipped_writes;
391 unsigned int rw_bs[DDIR_RWDIR_CNT];
393 int fd, i, old_state;
395 int this_depth, depth;
397 fd = open(filename, O_RDONLY);
399 td_verror(td, errno, "open blktrace file");
403 fifo = fifo_alloc(TRACE_FIFO_SIZE);
405 old_state = td_bump_runstate(td, TD_SETTING_UP);
410 rw_bs[0] = rw_bs[1] = 0;
412 this_depth = depth = 0;
414 int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t));
420 else if (ret < (int) sizeof(t)) {
421 log_err("fio: short fifo get\n");
428 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
429 log_err("fio: bad magic in blktrace data: %x\n",
433 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
434 log_err("fio: bad blktrace version %d\n",
438 ret = discard_pdu(td, fifo, fd, &t);
440 td_verror(td, ret, "blktrace lseek");
442 } else if (t.pdu_len != ret) {
443 log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
446 if ((t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) == 0) {
447 if ((t.action & 0xffff) == __BLK_TA_QUEUE)
449 else if ((t.action & 0xffff) == __BLK_TA_COMPLETE) {
450 depth = max(depth, this_depth);
454 if (t_is_write(&t) && read_only) {
460 handle_trace(td, &t, ios, rw_bs);
463 for (i = 0; i < td->files_index; i++) {
465 trace_add_open_close_event(td, f->fileno, FIO_LOG_CLOSE_FILE);
471 td_restore_runstate(td, old_state);
473 if (!td->files_index) {
474 log_err("fio: did not find replay device(s)\n");
479 * For stacked devices, we don't always get a COMPLETE event so
480 * the depth grows to insane values. Limit it to something sane(r).
482 if (!depth || depth > 1024)
486 log_err("fio: %s skips replay of %lu writes due to read-only\n",
487 td->o.name, skipped_writes);
489 if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) {
490 log_err("fio: found no ios in blktrace data\n");
492 } else if (ios[DDIR_READ] && !ios[DDIR_WRITE]) {
493 td->o.td_ddir = TD_DDIR_READ;
494 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
495 } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) {
496 td->o.td_ddir = TD_DDIR_WRITE;
497 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
499 td->o.td_ddir = TD_DDIR_RW;
500 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
501 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
502 td->o.max_bs[DDIR_TRIM] = rw_bs[DDIR_TRIM];
506 * We need to do direct/raw ios to the device, to avoid getting
507 * read-ahead in our way.
512 * we don't know if this option was set or not. it defaults to 1,
513 * so we'll just guess that we should override it if it's still 1
515 if (td->o.iodepth != 1)
516 td->o.iodepth = depth;