Get rid of reopen_files()
[fio.git] / fio.c
diff --git a/fio.c b/fio.c
index ab84b60a59ca77ace2e9984b4353b530a569ed8c..72cd02b42d5ca36402bc1dda6b32c9350225b58a 100644 (file)
--- a/fio.c
+++ b/fio.c
@@ -656,27 +656,28 @@ static int switch_ioscheduler(struct thread_data *td)
        return 0;
 }
 
-static void clear_io_state(struct thread_data *td)
+static int clear_io_state(struct thread_data *td)
 {
        struct fio_file *f;
-       int i;
+       int i, ret;
 
-       td->ts->stat_io_bytes[0] = td->ts->stat_io_bytes[1] = 0;
+       td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
        td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
        td->zone_bytes = 0;
 
        td->last_was_sync = 0;
 
-       for_each_file(td, f, i) {
-               f->last_completed_pos = 0;
-
-               f->last_pos = 0;
-               if (td->io_ops->flags & FIO_SYNCIO)
-                       lseek(f->fd, SEEK_SET, 0);
+       for_each_file(td, f, i)
+               td_io_close_file(td, f);
 
-               if (f->file_map)
-                       memset(f->file_map, 0, f->num_maps * sizeof(long));
+       ret = 0;
+       for_each_file(td, f, i) {
+               ret = td_io_open_file(td, f);
+               if (ret)
+                       break;
        }
+
+       return ret;
 }
 
 /*
@@ -687,6 +688,7 @@ static void *thread_main(void *data)
 {
        unsigned long long runtime[2];
        struct thread_data *td = data;
+       int clear_state;
 
        if (!td->use_thread)
                setsid();
@@ -734,16 +736,13 @@ static void *thread_main(void *data)
 
        if (!td->create_serialize && setup_files(td))
                goto err;
-       if (open_files(td))
-               goto err;
 
-       /*
-        * Do this late, as some IO engines would like to have the
-        * files setup prior to initializing structures.
-        */
        if (td_io_init(td))
                goto err;
 
+       if (open_files(td))
+               goto err;
+
        if (td->exec_prerun) {
                if (system(td->exec_prerun) < 0)
                        goto err;
@@ -751,17 +750,20 @@ static void *thread_main(void *data)
 
        fio_gettime(&td->epoch, NULL);
        memcpy(&td->timeout_end, &td->epoch, sizeof(td->epoch));
-       getrusage(RUSAGE_SELF, &td->ts->ru_start);
+       getrusage(RUSAGE_SELF, &td->ts.ru_start);
 
        runtime[0] = runtime[1] = 0;
+       clear_state = 0;
        while (td->loops--) {
                fio_gettime(&td->start, NULL);
-               memcpy(&td->ts->stat_sample_time, &td->start, sizeof(td->start));
+               memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
 
                if (td->ratemin)
-                       memcpy(&td->lastrate, &td->ts->stat_sample_time, sizeof(td->lastrate));
+                       memcpy(&td->lastrate, &td->ts.stat_sample_time, sizeof(td->lastrate));
+
+               if (clear_state && clear_io_state(td))
+                       break;
 
-               clear_io_state(td);
                prune_io_piece_log(td);
 
                if (td->io_ops->flags & FIO_CPUIO)
@@ -769,6 +771,8 @@ static void *thread_main(void *data)
                else
                        do_io(td);
 
+               clear_state = 1;
+
                if (td_read(td) && td->io_bytes[DDIR_READ])
                        runtime[DDIR_READ] += utime_since_now(&td->start);
                if (td_write(td) && td->io_bytes[DDIR_WRITE])
@@ -780,7 +784,9 @@ static void *thread_main(void *data)
                if (td->verify == VERIFY_NONE)
                        continue;
 
-               clear_io_state(td);
+               if (clear_io_state(td))
+                       break;
+
                fio_gettime(&td->start, NULL);
 
                do_verify(td);
@@ -792,16 +798,18 @@ static void *thread_main(void *data)
        }
 
        update_rusage_stat(td);
-       fio_gettime(&td->end_time, NULL);
-       td->runtime[0] = runtime[0] / 1000;
-       td->runtime[1] = runtime[1] / 1000;
-
-       if (td->ts->bw_log)
-               finish_log(td, td->ts->bw_log, "bw");
-       if (td->ts->slat_log)
-               finish_log(td, td->ts->slat_log, "slat");
-       if (td->ts->clat_log)
-               finish_log(td, td->ts->clat_log, "clat");
+       td->ts.runtime[0] = runtime[0] / 1000;
+       td->ts.runtime[1] = runtime[1] / 1000;
+       td->ts.total_run_time = mtime_since_now(&td->epoch);
+       td->ts.io_bytes[0] = td->io_bytes[0];
+       td->ts.io_bytes[1] = td->io_bytes[1];
+
+       if (td->ts.bw_log)
+               finish_log(td, td->ts.bw_log, "bw");
+       if (td->ts.slat_log)
+               finish_log(td, td->ts.slat_log, "slat");
+       if (td->ts.clat_log)
+               finish_log(td, td->ts.clat_log, "clat");
        if (td->write_iolog_file)
                write_iolog_close(td);
        if (td->exec_postrun) {