Add ability to keep memory-mapped files
authorStephen Bates <sbates@raithlin.com>
Tue, 8 Aug 2017 20:26:56 +0000 (14:26 -0600)
committerJens Axboe <axboe@kernel.dk>
Tue, 8 Aug 2017 20:26:56 +0000 (14:26 -0600)
By default file backed memory mappings are unlink()'ed after use. This
patch keeps the files if they already existed. We don't check for
errors on access() since we will catch them on the open().

Discovered this when doing p2pmem testing and fio kept deleting the
/dev/p2pmem0 files...

Changes since v1
  Altered based on feedback from Jens to avoid using an option and
  test for file existance instead.

Signed-off-by: Stephen Bates <sbates@raithlin.com>
Changed by me to use a td->flags flag, instead of adding a new
member to thread options.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
fio.h
memory.c

diff --git a/fio.h b/fio.h
index 39d775c0677dfadbda23c25b68b55014149b44ff..da950ef84ac2e05070435d37eaea475fce57c11d 100644 (file)
--- a/fio.h
+++ b/fio.h
@@ -87,6 +87,7 @@ enum {
        TD_F_CHILD              = 1U << 12,
        TD_F_NO_PROGRESS        = 1U << 13,
        TD_F_REGROW_LOGS        = 1U << 14,
        TD_F_CHILD              = 1U << 12,
        TD_F_NO_PROGRESS        = 1U << 13,
        TD_F_REGROW_LOGS        = 1U << 14,
+       TD_F_MMAP_KEEP          = 1U << 15,
 };
 
 enum {
 };
 
 enum {
index 22a7f5ddde6c1dc785d9a6f37ad4a16d53cb38ec..04dc3be8bfda1b312298591bff312d8d2b39912a 100644 (file)
--- a/memory.c
+++ b/memory.c
@@ -138,6 +138,9 @@ static int alloc_mem_mmap(struct thread_data *td, size_t total_mem)
        }
 
        if (td->o.mmapfile) {
        }
 
        if (td->o.mmapfile) {
+               if (access(td->o.mmapfile, F_OK) == 0)
+                       td->flags |= TD_F_MMAP_KEEP;
+
                td->mmapfd = open(td->o.mmapfile, O_RDWR|O_CREAT, 0644);
 
                if (td->mmapfd < 0) {
                td->mmapfd = open(td->o.mmapfile, O_RDWR|O_CREAT, 0644);
 
                if (td->mmapfd < 0) {
@@ -169,7 +172,7 @@ static int alloc_mem_mmap(struct thread_data *td, size_t total_mem)
                td->orig_buffer = NULL;
                if (td->mmapfd != 1 && td->mmapfd != -1) {
                        close(td->mmapfd);
                td->orig_buffer = NULL;
                if (td->mmapfd != 1 && td->mmapfd != -1) {
                        close(td->mmapfd);
-                       if (td->o.mmapfile)
+                       if (td->o.mmapfile && !(td->flags & TD_F_MMAP_KEEP))
                                unlink(td->o.mmapfile);
                }
 
                                unlink(td->o.mmapfile);
                }
 
@@ -187,7 +190,8 @@ static void free_mem_mmap(struct thread_data *td, size_t total_mem)
        if (td->o.mmapfile) {
                if (td->mmapfd != -1)
                        close(td->mmapfd);
        if (td->o.mmapfile) {
                if (td->mmapfd != -1)
                        close(td->mmapfd);
-               unlink(td->o.mmapfile);
+               if (!(td->flags & TD_F_MMAP_KEEP))
+                       unlink(td->o.mmapfile);
                free(td->o.mmapfile);
        }
 }
                free(td->o.mmapfile);
        }
 }