From f9cfc7d44a80638f81810416385136c35ad34658 Mon Sep 17 00:00:00 2001 From: Stephen Bates Date: Tue, 8 Aug 2017 14:26:56 -0600 Subject: [PATCH] Add ability to keep memory-mapped files 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 Changed by me to use a td->flags flag, instead of adding a new member to thread options. Signed-off-by: Jens Axboe --- fio.h | 1 + memory.c | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/fio.h b/fio.h index 39d775c0..da950ef8 100644 --- 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_MMAP_KEEP = 1U << 15, }; enum { diff --git a/memory.c b/memory.c index 22a7f5dd..04dc3be8 100644 --- 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 (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) { @@ -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); - if (td->o.mmapfile) + if (td->o.mmapfile && !(td->flags & TD_F_MMAP_KEEP)) 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); - unlink(td->o.mmapfile); + if (!(td->flags & TD_F_MMAP_KEEP)) + unlink(td->o.mmapfile); free(td->o.mmapfile); } } -- 2.25.1