Sergey L.
Sergey L.

Reputation: 22542

Ahead faulting of multiple anonymous pages

I have allocated a large number of pages with mmap/MAP_ANONYMOUS which are used for a list that only grows.

Is there a way to make the kernel intelligently prefault multiple pages for me in advance before they are accessed in Linux?

The MAP_POPULATE flag seems to be a no op for MAP_ANONYMOUS.

I can of cause just loop over a pointer in the style:

for (i = 1; i < num_pages_to_prefault; i++)
    *((char *)pointer_to_current_page + i * sysconf(_SC_PAGE_SIZE)) = 0;

But this is neither thread safe, nor particularly efficient because a new context change is forced for each new page accessed.

What I want is something like

prefault_memory(void * start_address, size_t length);

that will cause only one context switch and prefault a number of pages or leave them untouched if they are not mapped or are already in my resident set.

Upvotes: 1

Views: 324

Answers (1)

Macattack
Macattack

Reputation: 1990

The call you're looking for is madvise() with MADV_WILLNEED.

Upvotes: 4

Related Questions