Add strong madvise() hint for cache pruning
[fio.git] / filesetup.c
index 57928133369cdd520a1b89052cbc9362b773ca78..f8f81d2b2a54e9a7692cdb41289ac8c5ff472c2c 100644 (file)
@@ -156,6 +156,9 @@ static int pre_read_file(struct thread_data *td, struct fio_file *f)
        unsigned int bs;
        char *b;
 
+       if (td->io_ops->flags & FIO_PIPEIO)
+               return 0;
+
        if (!fio_file_open(f)) {
                if (td->io_ops->open_file(td, f)) {
                        log_err("fio: cannot pre-read, failed to open file\n");
@@ -246,6 +249,7 @@ static int bdev_size(struct thread_data *td, struct fio_file *f)
        }
 
        f->real_file_size = bytes;
+       td->io_ops->close_file(td, f);
        return 0;
 err:
        td->io_ops->close_file(td, f);
@@ -299,9 +303,12 @@ static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
        /*
         * FIXME: add blockdev flushing too
         */
-       if (f->mmap_ptr)
+       if (f->mmap_ptr) {
                ret = madvise(f->mmap_ptr, f->mmap_sz, MADV_DONTNEED);
-       else if (f->filetype == FIO_TYPE_FILE) {
+#ifdef FIO_MADV_FREE
+               (void) madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
+#endif
+       } else if (f->filetype == FIO_TYPE_FILE) {
                ret = fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
        } else if (f->filetype == FIO_TYPE_BD) {
                ret = blockdev_invalidate_cache(f->fd);
@@ -435,7 +442,7 @@ open_again:
                char buf[FIO_VERROR_SIZE];
                int __e = errno;
 
-               if (errno == EPERM && (flags & FIO_O_NOATIME)) {
+               if (__e == EPERM && (flags & FIO_O_NOATIME)) {
                        flags &= ~FIO_O_NOATIME;
                        goto open_again;
                }
@@ -607,7 +614,7 @@ int setup_files(struct thread_data *td)
                temp_stall_ts = 1;
                if (!terse_output)
                        log_info("%s: Laying out IO file(s) (%u file(s) /"
-                                " %LuMiB)\n", td->o.name, need_extend,
+                                " %LuMB)\n", td->o.name, need_extend,
                                        extend_size >> 20);
 
                for_each_file(td, f, i) {
@@ -709,8 +716,10 @@ void close_files(struct thread_data *td)
        struct fio_file *f;
        unsigned int i;
 
-       for_each_file(td, f, i)
-               td_io_close_file(td, f);
+       for_each_file(td, f, i) {
+               if (fio_file_open(f))
+                       td_io_close_file(td, f);
+       }
 }
 
 void close_and_free_files(struct thread_data *td)
@@ -726,7 +735,9 @@ void close_and_free_files(struct thread_data *td)
                        unlink(f->file_name);
                }
 
-               td_io_close_file(td, f);
+               if (fio_file_open(f))
+                       td_io_close_file(td, f);
+
                remove_file_hash(f);
 
                sfree(f->file_name);
@@ -847,8 +858,10 @@ int put_file(struct thread_data *td, struct fio_file *f)
 
        dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
 
-       if (!fio_file_open(f))
+       if (!fio_file_open(f)) {
+               assert(f->fd == -1);
                return 0;
+       }
 
        assert(f->references);
        if (--f->references)
@@ -865,6 +878,7 @@ int put_file(struct thread_data *td, struct fio_file *f)
 
        td->nr_open_files--;
        fio_file_clear_open(f);
+       assert(f->fd == -1);
        return ret;
 }
 
@@ -1004,6 +1018,7 @@ void dup_files(struct thread_data *td, struct thread_data *org)
                        log_err("fio: smalloc OOM\n");
                        assert(0);
                }
+               __f->fd = -1;
        
                if (f->file_name) {
                        __f->file_name = smalloc_strdup(f->file_name);