Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to asynchronously flush a memory mapped file?

I am using memory mapped files to have read-/write-access to a large number of image files (~10000 x 16 MB) under Windows 7 64bit. My goals are:

  1. Having as much data cached as possible.

  2. Being able to allocate new images and write to those as fast as possible.

Therefore I am using memory mapped files to access the files. Caching works well, but the OS is not flushing dirty pages until I am nearly out of physical memory. Because of that allocating and writing to new files is quite slow once the physical memory is filled.

One solution would be to regularly use FlushViewOfFile(), but this function does not return until the data has been writen to disk.

Is there a way to asynchroniously flush a file mapping? The only solution I found is to Unmap() and MapViewOfFile() again, but using this approach I can not be sure to get the same data pointer again. Can someone suggest a better approach?

Edit: Reading the WINAPI documentation a little longer, it seems that I found a suitable solution to my problem:

Calling VirtualUnlock() on a memory range that is not locked results in a flushing of dirty pages.

like image 458
user1511305 Avatar asked Jul 09 '12 07:07

user1511305


1 Answers

I heard that FlushViewOfFile() function does NOT wait until it physically write to file.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa366563(v=vs.85).aspx

The FlushViewOfFile function does not flush the file metadata, and it does not wait to return until the changes are flushed from the underlying hardware disk cache and physically written to disk.

After call "FlushFileBuffers( ... )" then your data will be physically written to disk.

like image 57
gun9 Avatar answered Oct 29 '22 19:10

gun9