How to have a checkpoint file using mmap which is only synced to disk manually
Anything you write to the memory within a MAP_SHARED
mapping of a file is considered as being written to the file at that time, as surely as if you had used write()
. msync()
in this sense is completely analagous to fsync()
- it merely ensures that changes you have already made to the file are actually pushed out to permanent storage. You can't change this - it's how mmap()
is defined to work.
In general, the safe way to do this is to write a complete consistent copy of the data to a temporary file, sync the temporary file, then atomically rename it over the prior checkpoint file. This is the only way to ensure that a crash between checkpoints doesn't leave you with an inconsistent file. Any solution that does less copying is going to require both a more complicated transaction-log style file format, and be more intrusive to the rest of your application (requiring specific hooks to be invoked in each place that the in-memory state is changed).
mmap
can't be used for this purpose. There's no way to prevent data from being written to disk. In practice, using mlock()
to make the memory unswappable might have a side effect of preventing it from getting written to disk except when you ask for it to be written, but there's no guarantee. Certainly if another process opens the file, it's going to see the copy cached in memory (with your latest changes), not the copy on physical disk. In many ways, what you should do depends on whether you're trying to do synchronization with other processes or just for safety in case of crash or power failure.
If your data size is small, you might try a number of other methods for atomic syncing to disk. One way is to store the entire dataset in a filename and create an empty file by that name, then delete the old file. If 2 files exist at startup (due to extremely unlikely crash time), delete the older one and resume from the newer one. write()
may also be atomic if your data size is smaller than a filesystem block, page size, or disk block, but I don't know of any guarantee to that effect right off. You'd have to do some research.
Another very standard approach that works as long as your data isn't so big that 2 copies won't fit on disk: just create a second copy with a temporary name, then rename()
it over top of the old one. rename()
is always atomic. This is probably the best approach unless you have a reason not to do it that way.
As the other respondents have suggested, I don't think there's a portable way to do what you want without copying. If you're looking to do this in a special-purpose environment where you can control the OS etc, you may be able to do it under Linux with the btrfs filesystem.
btrfs supports a new reflink()
operation which is essentially a copy-on-write filesystem copy. You could reflink()
your file to a temporary on start-up, mmap()
the temporary, then msync()
and reflink()
the temporary back to the original to checkpoint.