Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mmap a 10 GB file and load it into memory

Tags:

c++

linux

mmap

if I want to mmap a 10 GB file and load the whole file into physical memory immediately, how can I do so?
I don't want to use function like mlock because it needs root privileges.
Is there a system call which can satisfy my demand?

(I have more than enough memory.)

like image 443
sean Avatar asked Nov 04 '14 03:11

sean


People also ask

Does mmap load file into memory?

Yes, mmap creates a mapping. It does not normally read the entire content of whatever you have mapped into memory. If you wish to do that you can use the mlock/mlockall system call to force the kernel to read into RAM the content of the mapping, if applicable.

How does mmap work for files?

mmap works by manipulating your process's page table, a data structure your CPU uses to map address spaces. The CPU will translate "virtual" addresses to "physical" ones, and does so according to the page table set up by your kernel. When you access the mapped memory for the first time, your CPU generates a page fault.

Does mmap write to disk?

In this case using mmap will save at least one copy of the data, as the OS can directly write the buffer to disk.

Is mmap slow?

Even though it is important and often used, mmap can be slow and inconsistent in its timing. Mmap maps memory pages directly to bytes on disk. With mmap, whenever there is a pagefault, the kernel pulls the page directly from disk into the program's memory space.


1 Answers

Read the man-page for mmap:

MAP_POPULATE (since Linux 2.5.46)

Populate (prefault) page tables for a mapping. For a file mapping, this causes read-ahead on the file. Later accesses to the mapping will not be blocked by page faults. MAP_POPULATE is supported for private mappings only since Linux 2.6.23

Issue your request, and be prepared for a short wait (unless you exceed the processes limits) (depending on disk-bandwidth and cache).

like image 95
Deduplicator Avatar answered Sep 22 '22 23:09

Deduplicator