Revamp file open/close handling
[fio.git] / engines / mmap.c
index 08bbd991463d4e8546ea40a31d14b9213ffd0d11..27d5d25515f2016526cbca985e8bf98bc356e326 100644 (file)
@@ -37,7 +37,7 @@ static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
        }
 
        if (io_u->error)
-               td_verror(td, io_u->error);
+               td_verror(td, io_u->error, "sync");
 
        return FIO_Q_COMPLETED;
 }
@@ -47,7 +47,7 @@ static int fio_mmapio_init(struct thread_data *td)
        struct fio_file *f;
        int i;
 
-       if (td->ddir == DDIR_READ && !td_rw(td))
+       if (!td_write(td))
                return 0;
 
        /*
@@ -56,7 +56,7 @@ static int fio_mmapio_init(struct thread_data *td)
         */
        for_each_file(td, f, i) {
                if (ftruncate(f->fd, f->file_size) < 0) {
-                       td_verror(td, errno);
+                       td_verror(td, errno, "ftruncate");
                        return 1;
                }
        }
@@ -64,12 +64,72 @@ static int fio_mmapio_init(struct thread_data *td)
        return 0;
 }
 
+static int fio_mmapio_open(struct thread_data *td, struct fio_file *f)
+{
+       int ret, flags;
+
+       ret = generic_open_file(td, f);
+       if (ret)
+               return ret;
+
+       if (td_rw(td))
+               flags = PROT_READ | PROT_WRITE;
+       else if (td_write(td)) {
+               flags = PROT_WRITE;
+
+               if (td->verify != VERIFY_NONE)
+                       flags |= PROT_READ;
+       } else
+               flags = PROT_READ;
+
+       f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset);
+       if (f->mmap == MAP_FAILED) {
+               f->mmap = NULL;
+               td_verror(td, errno, "mmap");
+               goto err;
+       }
+
+       if (file_invalidate_cache(td, f))
+               goto err;
+
+       if (!td_random(td)) {
+               if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) {
+                       td_verror(td, errno, "madvise");
+                       goto err;
+               }
+       } else {
+               if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) {
+                       td_verror(td, errno, "madvise");
+                       goto err;
+               }
+       }
+
+       return 0;
+
+err:
+       if (f->mmap)
+               munmap(f->mmap, f->file_size);
+       generic_close_file(td, f);
+       return 1;
+}
+
+static void fio_mmapio_close(struct thread_data fio_unused *td,
+                            struct fio_file *f)
+{
+       if (f->mmap) {
+               munmap(f->mmap, f->file_size);
+               f->mmap = NULL;
+       }
+}
+
 static struct ioengine_ops ioengine = {
        .name           = "mmap",
        .version        = FIO_IOOPS_VERSION,
        .queue          = fio_mmapio_queue,
        .init           = fio_mmapio_init,
-       .flags          = FIO_SYNCIO | FIO_MMAPIO,
+       .open_file      = fio_mmapio_open,
+       .close_file     = fio_mmapio_close,
+       .flags          = FIO_SYNCIO,
 };
 
 static void fio_init fio_mmapio_register(void)