Handle end_fsync if a file is closed
[fio.git] / backend.c
index 2362123a5abf57cdccbc12ba15c5d0268ba7b2b9..d1fe9641d88ebaa47489fc10a8f039e9443bbc10 100644 (file)
--- a/backend.c
+++ b/backend.c
@@ -50,6 +50,8 @@
 #include "lib/rand.h"
 #include "memalign.h"
 #include "server.h"
+#include "lib/getrusage.h"
+#include "idletime.h"
 
 static pthread_t disk_util_thread;
 static struct fio_mutex *disk_thread_mutex;
@@ -320,6 +322,21 @@ requeue:
        return 0;
 }
 
+static int fio_file_fsync(struct thread_data *td, struct fio_file *f)
+{
+       int ret;
+
+       if (fio_file_open(f))
+               return fio_io_sync(td, f);
+
+       if (td_io_open_file(td, f))
+               return 1;
+
+       ret = fio_io_sync(td, f);
+       td_io_close_file(td, f);
+       return ret;
+}
+
 static inline void __update_tv_cache(struct thread_data *td)
 {
        fio_gettime(&td->tv_cache, NULL);
@@ -811,7 +828,7 @@ sync_done:
 
                i = td->cur_depth;
                if (i) {
-                       ret = io_u_queued_complete(td, i, NULL);
+                       ret = io_u_queued_complete(td, i, bytes_done);
                        if (td->o.fill_device && td->error == ENOSPC)
                                td->error = 0;
                }
@@ -820,9 +837,11 @@ sync_done:
                        td_set_runstate(td, TD_FSYNCING);
 
                        for_each_file(td, f, i) {
-                               if (!fio_file_open(f))
+                               if (!fio_file_fsync(td, f))
                                        continue;
-                               fio_io_sync(td, f);
+
+                               log_err("fio: end_fsync failed for file %s\n",
+                                                               f->file_name);
                        }
                }
        } else
@@ -1015,8 +1034,19 @@ static int keep_running(struct thread_data *td)
                return 1;
        }
 
-       if (ddir_rw_sum(td->io_bytes) < td->o.size)
+       if (ddir_rw_sum(td->io_bytes) < td->o.size) {
+               uint64_t diff;
+
+               /*
+                * If the difference is less than the minimum IO size, we
+                * are done.
+                */
+               diff = td->o.size - ddir_rw_sum(td->io_bytes);
+               if (diff < td->o.rw_min_bs)
+                       return 0;
+
                return 1;
+       }
 
        return 0;
 }
@@ -1210,11 +1240,7 @@ static void *thread_main(void *data)
        }
 
        fio_gettime(&td->epoch, NULL);
-#ifdef RUSAGE_THREAD
-       getrusage(RUSAGE_THREAD, &td->ru_start);
-#else
-       getrusage(RUSAGE_SELF, &td->ru_start);
-#endif
+       fio_getrusage(&td->ru_start);
        clear_state = 0;
        while (keep_running(td)) {
                uint64_t verify_bytes;
@@ -1502,6 +1528,8 @@ static void run_threads(void)
 
        if (fio_gtod_offload && fio_start_gtod_thread())
                return;
+       
+       fio_idle_prof_init();
 
        set_sig_handlers();
 
@@ -1559,6 +1587,9 @@ static void run_threads(void)
                }
        }
 
+       /* start idle threads before io threads start to run */
+       fio_idle_prof_start();
+
        set_genesis_time();
 
        while (todo) {
@@ -1721,6 +1752,8 @@ static void run_threads(void)
                        usleep(10000);
        }
 
+       fio_idle_prof_stop();
+
        update_io_ticks();
        fio_unpin_memory();
 }