blktrace: only set O_DIRECT if the min bs is a multiple of 4k
[fio.git] / blktrace.c
index 3c23b973d971fb0f75011c54f7d616c64a42e931..bb0bcbe60ac52e2f7f6c1fda34983f704e94ada0 100644 (file)
@@ -9,6 +9,7 @@
 #include "flist.h"
 #include "fio.h"
 #include "blktrace_api.h"
+#include "lib/linux-dev-lookup.h"
 
 #define TRACE_FIFO_SIZE        8192
 
@@ -108,67 +109,6 @@ int is_blktrace(const char *filename, int *need_swap)
        return 0;
 }
 
-static int lookup_device(struct thread_data *td, char *path, unsigned int maj,
-                        unsigned int min)
-{
-       struct dirent *dir;
-       struct stat st;
-       int found = 0;
-       DIR *D;
-
-       D = opendir(path);
-       if (!D)
-               return 0;
-
-       while ((dir = readdir(D)) != NULL) {
-               char full_path[256];
-
-               if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
-                       continue;
-
-               sprintf(full_path, "%s%s%s", path, FIO_OS_PATH_SEPARATOR, dir->d_name);
-               if (lstat(full_path, &st) == -1) {
-                       perror("lstat");
-                       break;
-               }
-
-               if (S_ISDIR(st.st_mode)) {
-                       found = lookup_device(td, full_path, maj, min);
-                       if (found) {
-                               strcpy(path, full_path);
-                               break;
-                       }
-               }
-
-               if (!S_ISBLK(st.st_mode))
-                       continue;
-
-               /*
-                * If replay_redirect is set then always return this device
-                * upon lookup which overrides the device lookup based on
-                * major minor in the actual blktrace
-                */
-               if (td->o.replay_redirect) {
-                       dprint(FD_BLKTRACE, "device lookup: %d/%d\n overridden"
-                                       " with: %s\n", maj, min,
-                                       td->o.replay_redirect);
-                       strcpy(path, td->o.replay_redirect);
-                       found = 1;
-                       break;
-               }
-
-               if (maj == major(st.st_rdev) && min == minor(st.st_rdev)) {
-                       dprint(FD_BLKTRACE, "device lookup: %d/%d\n", maj, min);
-                       strcpy(path, full_path);
-                       found = 1;
-                       break;
-               }
-       }
-
-       closedir(D);
-       return found;
-}
-
 #define FMINORBITS     20
 #define FMINORMASK     ((1U << FMINORBITS) - 1)
 #define FMAJOR(dev)    ((unsigned int) ((dev) >> FMINORBITS))
@@ -212,9 +152,16 @@ static int trace_add_file(struct thread_data *td, __u32 device)
                }
 
        strcpy(dev, "/dev");
-       if (lookup_device(td, dev, maj, min)) {
+       if (blktrace_lookup_device(td->o.replay_redirect, dev, maj, min)) {
                int fileno;
 
+               if (td->o.replay_redirect)
+                       dprint(FD_BLKTRACE, "device lookup: %d/%d\n overridden"
+                                       " with: %s\n", maj, min,
+                                       td->o.replay_redirect);
+               else
+                       dprint(FD_BLKTRACE, "device lookup: %d/%d\n", maj, min);
+
                dprint(FD_BLKTRACE, "add devices %s\n", dev);
                fileno = add_file_exclusive(td, dev);
                td->o.open_files++;
@@ -380,6 +327,47 @@ static int t_is_write(struct blk_io_trace *t)
        return (t->action & BLK_TC_ACT(BLK_TC_WRITE | BLK_TC_DISCARD)) != 0;
 }
 
+static enum fio_ddir t_get_ddir(struct blk_io_trace *t)
+{
+       if (t->action & BLK_TC_ACT(BLK_TC_READ))
+               return DDIR_READ;
+       else if (t->action & BLK_TC_ACT(BLK_TC_WRITE))
+               return DDIR_WRITE;
+       else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
+               return DDIR_TRIM;
+
+       return DDIR_INVAL;
+}
+
+static void depth_inc(struct blk_io_trace *t, int *depth)
+{
+       enum fio_ddir ddir;
+
+       ddir = t_get_ddir(t);
+       if (ddir != DDIR_INVAL)
+               depth[ddir]++;
+}
+
+static void depth_dec(struct blk_io_trace *t, int *depth)
+{
+       enum fio_ddir ddir;
+
+       ddir = t_get_ddir(t);
+       if (ddir != DDIR_INVAL)
+               depth[ddir]--;
+}
+
+static void depth_end(struct blk_io_trace *t, int *this_depth, int *depth)
+{
+       enum fio_ddir ddir = DDIR_INVAL;
+
+       ddir = t_get_ddir(t);
+       if (ddir != DDIR_INVAL) {
+               depth[ddir] = max(depth[ddir], this_depth[ddir]);
+               this_depth[ddir] = 0;
+       }
+}
+
 /*
  * 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.
@@ -392,7 +380,7 @@ int load_blktrace(struct thread_data *td, const char *filename, int need_swap)
        struct fifo *fifo;
        int fd, i, old_state;
        struct fio_file *f;
-       int this_depthdepth;
+       int this_depth[DDIR_RWDIR_CNT], depth[DDIR_RWDIR_CNT], max_depth;
 
        fd = open(filename, O_RDONLY);
        if (fd < 0) {
@@ -406,10 +394,14 @@ int load_blktrace(struct thread_data *td, const char *filename, int need_swap)
 
        td->o.size = 0;
 
-       ios[0] = ios[1] = 0;
-       rw_bs[0] = rw_bs[1] = 0;
+       for (i = 0; i < DDIR_RWDIR_CNT; i++) {
+               ios[i] = 0;
+               rw_bs[i] = 0;
+               this_depth[i] = 0;
+               depth[i] = 0;
+       }
+
        skipped_writes = 0;
-       this_depth = depth = 0;
        do {
                int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t));
 
@@ -445,11 +437,12 @@ int load_blktrace(struct thread_data *td, const char *filename, int need_swap)
                }
                if ((t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) == 0) {
                        if ((t.action & 0xffff) == __BLK_TA_QUEUE)
-                               this_depth++;
-                       else if ((t.action & 0xffff) == __BLK_TA_COMPLETE) {
-                               depth = max(depth, this_depth);
-                               this_depth = 0;
-                       }
+                               depth_inc(&t, this_depth);
+                       else if (((t.action & 0xffff) == __BLK_TA_BACKMERGE) ||
+                               ((t.action & 0xffff) == __BLK_TA_FRONTMERGE))
+                               depth_dec(&t, this_depth);
+                       else if ((t.action & 0xffff) == __BLK_TA_COMPLETE)
+                               depth_end(&t, this_depth, depth);
 
                        if (t_is_write(&t) && read_only) {
                                skipped_writes++;
@@ -479,8 +472,14 @@ int load_blktrace(struct thread_data *td, const char *filename, int need_swap)
         * For stacked devices, we don't always get a COMPLETE event so
         * the depth grows to insane values. Limit it to something sane(r).
         */
-       if (!depth || depth > 1024)
-               depth = 1024;
+       max_depth = 0;
+       for (i = 0; i < DDIR_RWDIR_CNT; i++) {
+               if (depth[i] > 1024)
+                       depth[i] = 1024;
+               else if (!depth[i] && ios[i])
+                       depth[i] = 1;
+               max_depth = max(depth[i], max_depth);
+       }
 
        if (skipped_writes)
                log_err("fio: %s skips replay of %lu writes due to read-only\n",
@@ -504,16 +503,17 @@ int load_blktrace(struct thread_data *td, const char *filename, int need_swap)
 
        /*
         * We need to do direct/raw ios to the device, to avoid getting
-        * read-ahead in our way.
+        * read-ahead in our way. But only do so if the minimum block size
+        * is a multiple of 4k, otherwise we don't know if it's safe to do so.
         */
-       td->o.odirect = 1;
+       if (!fio_option_is_set(&td->o, odirect) && !(td_min_bs(td) & 4095))
+               td->o.odirect = 1;
 
        /*
-        * we don't know if this option was set or not. it defaults to 1,
-        * so we'll just guess that we should override it if it's still 1
+        * If depth wasn't manually set, use probed depth
         */
-       if (td->o.iodepth != 1)
-               td->o.iodepth = depth;
+       if (!fio_option_is_set(&td->o, iodepth))
+               td->o.iodepth = td->o.iodepth_low = max_depth;
 
        return 0;
 err: