My understanding is that I can keep a file in memory by doing an mmap
on the file and then calling mlock
on the mapped memory.
Is there a way to keep file data in the page cache without doing an mmap? Specifically I want to ensure that when I'm appending data to a file the page I'm writing to never gets evicted.
I realize this is rare, but there are cases where I believe it could occur. For example, the application writes data, waits longer than dirty_writeback_centisecs
(after which the page becomes clean and can be evicted) and then writes more data.
I believe you are a bit wrong in your understanding what
mlock
does. It's intended usage is for:So it asserts that the pages are loaded into RAM and prevents them from being swapped out. There are no guaranties that it prevents write-back of dirty pages mapped from a file (and it actually doesn't, see the experiment bellow).
To hint the kernel that you are going to make some reads from an fd soon there is
posix_fadvise()
, sowill probably load the requested part of the file to the page cache.
I can't claim that for sure, but I suppose that there is actually no way to forbid writing back the dirty pages for a specific file as for now. There might be some way to hint it, but I don't see any either.
An experiment with mmap/mlock