The madvise() system call allows applications to tell the kernel how it expects to use some mapped or shared memory pages, so that the kernel can choose appropriate read-ahead and caching techniques.

mmap() and madvise()

The mmap() system creates a new mapping in the virtual address space of the calling process. It allows an application to map a file's content into memory pages, which significantly benefits the I/O performance of index files with closed hashing.

Sometimes, the latency of reading the content of a file mapping suddenly rises, since the mmap() just create a virtual address space, but not allocate real memory pages. In Linux, pages are allocated latently, which means that physical pages are allocated (and mapped with virtual address) when you try to access them and causes page faults.

Page fault - A page fault occurs when a program request an address on a page that is not in the current set of memory resident pages. What happens when a page fault occurs is that the thread that experienced the page fault is put into a Wait state while the operating system finds the specific page on disk and restores it to physical memory.

The mmap() and malloc() system calls could both cause page faults. The malloc() allocates virtual address but not real memory pages which will be allocated when the application tries to access it. The mmap() creates a mapping in the virtual address space, while the content mapped to the address residents in the disk, which will be copied from disk to a memory page when the application access it. This's why sometimes the latency of reading a page suddenly suffers.

This kind of latency rise may be resolved by madvise() that gives kernel advice on how it expects to use some mapped or shared memory pages. One quick note here - The kernel is free to ignore these kinds of advice.


电子邮件地址不会被公开。 必填项已用*标注