Skip to contentSkip to conversation

Add ability to keep memory-mapped files

PATCH In review [email protected] 2 participants 3 messages Aug 4, 2017 – Aug 8, 2017

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Add ability to keep memory-mapped files

From: Stephen Bates <[email protected]>

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.

---
 memory.c         | 8 ++++++--
 thread_options.h | 1 +
 2 files changed, 7 insertions(+), 2 deletions(-)
memory.c +6 -2
index 22a7f5dd..70fbb4ef 100644
@@ -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->o.mmapkeep = 1;
+
 		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->o.mmapkeep)
 				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->o.mmapkeep)
+			unlink(td->o.mmapfile);
 		free(td->o.mmapfile);
 	}
 }
thread_options.h +1 -0
index 72d86cfe..e49c4be6 100644
@@ -54,6 +54,7 @@ struct thread_options {
 	char *opendir;
 	char *ioengine;
 	char *mmapfile;
+	unsigned mmapkeep;
 	enum td_ddir td_ddir;
 	unsigned int rw_seq;
 	unsigned int kb_base;
Signature · 1 line
2.11.0
#1 lore raw
Quoted 6 lines · On 08/04/2017 04:24 PM, [email protected] wrote:

From: Stephen Bates <[email protected]>

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().

Quoted 6 lines

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.

This gets closer, but it doesn't make sense to use a new member in the options structure for this. It's not an option, and as such it's a bad fit. Additionally, options should always have a counter part in the _packed version of the structure, and be converted as well, for the onwire protocol.

I changed this to just use a flag in the thread_data instead:

http://git.kernel.dk/cgit/fio/commit/?id=f9cfc7d44a80638f81810416385136c35ad34658

Signature · 1 line
Jens Axboe