Refactor for_each_td() to catch inappropriate td ptr reuse
[fio.git] / iolog.c
diff --git a/iolog.c b/iolog.c
index 37e799a13ca6c9eb11f9d50f4f2f1faa43010a5f..cc2cbc65ef9b4079db84fdb1b9a4838986b7d37c 100644 (file)
--- a/iolog.c
+++ b/iolog.c
@@ -439,7 +439,7 @@ static bool read_iolog(struct thread_data *td)
        unsigned long long offset;
        unsigned int bytes;
        unsigned long long delay = 0;
-       int reads, writes, waits, fileno = 0, file_action = 0; /* stupid gcc */
+       int reads, writes, trims, waits, fileno = 0, file_action = 0; /* stupid gcc */
        char *rfname, *fname, *act;
        char *str, *p;
        enum fio_ddir rw;
@@ -461,7 +461,7 @@ static bool read_iolog(struct thread_data *td)
        rfname = fname = malloc(256+16);
        act = malloc(256+16);
 
-       syncs = reads = writes = waits = 0;
+       syncs = reads = writes = trims = waits = 0;
        while ((p = fgets(str, 4096, td->io_log_rfile)) != NULL) {
                struct io_piece *ipo;
                int r;
@@ -492,17 +492,25 @@ static bool read_iolog(struct thread_data *td)
                         */
                        if (!strcmp(act, "wait"))
                                rw = DDIR_WAIT;
-                       else if (!strcmp(act, "read"))
+                       else if (!strcmp(act, "read")) {
+                               if (td->o.replay_skip & (1u << DDIR_READ))
+                                       continue;
                                rw = DDIR_READ;
-                       else if (!strcmp(act, "write"))
+                       } else if (!strcmp(act, "write")) {
+                               if (td->o.replay_skip & (1u << DDIR_WRITE))
+                                       continue;
                                rw = DDIR_WRITE;
-                       else if (!strcmp(act, "sync"))
+                       } else if (!strcmp(act, "sync")) {
+                               if (td->o.replay_skip & (1u << DDIR_SYNC))
+                                       continue;
                                rw = DDIR_SYNC;
-                       else if (!strcmp(act, "datasync"))
+                       else if (!strcmp(act, "datasync"))
                                rw = DDIR_DATASYNC;
-                       else if (!strcmp(act, "trim"))
+                       else if (!strcmp(act, "trim")) {
+                               if (td->o.replay_skip & (1u << DDIR_TRIM))
+                                       continue;
                                rw = DDIR_TRIM;
-                       else {
+                       else {
                                log_err("fio: bad iolog file action: %s\n",
                                                                        act);
                                continue;
@@ -544,6 +552,13 @@ static bool read_iolog(struct thread_data *td)
                        if (read_only)
                                continue;
                        writes++;
+               } else if (rw == DDIR_TRIM) {
+                       /*
+                        * Don't add a trim for ro mode
+                        */
+                       if (read_only)
+                               continue;
+                       trims++;
                } else if (rw == DDIR_WAIT) {
                        if (td->o.no_stall)
                                continue;
@@ -620,19 +635,22 @@ static bool read_iolog(struct thread_data *td)
                {
                        io_u_quiesce(td);
                        free_io_mem(td);
-                       init_io_u_buffers(td);
+                       if (init_io_u_buffers(td))
+                               return false;
                }
                return true;
        }
 
-       if (!reads && !writes && !waits)
+       if (!reads && !writes && !waits && !trims)
                return false;
-       else if (reads && !writes)
-               td->o.td_ddir = TD_DDIR_READ;
-       else if (!reads && writes)
-               td->o.td_ddir = TD_DDIR_WRITE;
-       else
-               td->o.td_ddir = TD_DDIR_RW;
+
+       td->o.td_ddir = 0;
+       if (reads)
+               td->o.td_ddir |= TD_DDIR_READ;
+       if (writes)
+               td->o.td_ddir |= TD_DDIR_WRITE;
+       if (trims)
+               td->o.td_ddir |= TD_DDIR_TRIM;
 
        return true;
 }
@@ -1218,7 +1236,7 @@ int iolog_file_inflate(const char *file)
        void *buf;
        FILE *f;
 
-       f = fopen(file, "r");
+       f = fopen(file, "rb");
        if (!f) {
                perror("fopen");
                return 1;
@@ -1300,10 +1318,21 @@ void flush_log(struct io_log *log, bool do_append)
        void *buf;
        FILE *f;
 
+       /*
+        * If log_gz_store is true, we are writing a binary file.
+        * Set the mode appropriately (on all platforms) to avoid issues
+        * on windows (line-ending conversions, etc.)
+        */
        if (!do_append)
-               f = fopen(log->filename, "w");
+               if (log->log_gz_store)
+                       f = fopen(log->filename, "wb");
+               else
+                       f = fopen(log->filename, "w");
        else
-               f = fopen(log->filename, "a");
+               if (log->log_gz_store)
+                       f = fopen(log->filename, "ab");
+               else
+                       f = fopen(log->filename, "a");
        if (!f) {
                perror("fopen log");
                return;
@@ -1574,14 +1603,14 @@ void iolog_compress_exit(struct thread_data *td)
  * Queue work item to compress the existing log entries. We reset the
  * current log to a small size, and reference the existing log in the
  * data that we queue for compression. Once compression has been done,
- * this old log is freed. If called with finish == true, will not return
- * until the log compression has completed, and will flush all previous
- * logs too
+ * this old log is freed. Will not return until the log compression
+ * has completed, and will flush all previous logs too
  */
 static int iolog_flush(struct io_log *log)
 {
        struct iolog_flush_data *data;
 
+       workqueue_flush(&log->td->log_compress_wq);
        data = malloc(sizeof(*data));
        if (!data)
                return 1;
@@ -1846,9 +1875,7 @@ void td_writeout_logs(struct thread_data *td, bool unit_logs)
 
 void fio_writeout_logs(bool unit_logs)
 {
-       struct thread_data *td;
-       int i;
-
-       for_each_td(td, i)
+       for_each_td(td) {
                td_writeout_logs(td, unit_logs);
+       } end_for_each();
 }