In Linux, using C, if I ask for a large amount of memory via malloc
or a similar dynamic allocation mechanism, it is likely that most of the pages backing the returned region won't actually be mapped into the address space of my process.
Instead, a page fault is incurred each time I access one of the allocated pages for the first time, and then kernel will map in the "anonymous" page (consisting entirely of zeros) and return to user space.
For a large region (say 1 GiB) this is a large number of page faults (~260 thousand for 4 KiB pages), and each fault incurs a user-to-kernel-user transition which are especially slow on kernels with Spectre and Meltdown mitigations. For some uses, this page-faulting time might dominate the actual work being done on the buffer.
If I know I'm going to use the entire buffer, is there some way to ask the kernel to map an already mapped region ahead of time?
If I was allocating my own memory using mmap
, the way to do this would be MAP_POPULATE
- but that doesn't work for regions received from malloc
or new
.
There is the madvise
call, but the options there seem mostly to apply to file-backed regions. For example, the madvise(..., MADV_WILLNEED)
call seems promising - from the man page:
MADV_WILLNEED
Expect access in the near future. (Hence, it might be a good idea to read some pages ahead.)
The obvious implication is if the region is file-backed, this call might trigger an asynchronous file read-ahead, or perhaps a synchronous additional read-ahead on subsequent faults. From the description, it isn't clear if it will do anything for anonymous pages, and based on my testing, it doesn't.
mmap
? Especially considering the large allocations you're talking about. – Upbraidingmalloc
and in some cases themalloc
call and code that knows it is time to populate everything are in separate components, andmmap
only applies at themalloc
site, not later. I'm also interested in this problem in other languages where leaving the standard allocation routines and usingmmap
is even less feasible. – Kumamoto