CSE Roadmap

Common tier · about 4 hours · 7 parts

Part 3 of 7 · 1 min read

The reserved pool

In plain words: The first way to get huge pages is to ask for them by name. The administrator reserves a pool of huge pages, for example with the file /proc/sys/vm/nr_hugepages, and a program asks for pages from it with mmap and the flag MAP_HUGETLB, or through the hugetlbfs file system. The reserved pages are taken from memory at once, whether or not they are used, and they are never swapped out. This is the method for programs that need guaranteed huge pages, such as large databases.

The state of the pool is in /proc/meminfo. On the test machine:

Line Value
HugePages_Total 0
HugePages_Free 0
Hugepagesize 2048 kB

The pool is empty, so a program that asks for a huge page fails. A test with mmap(..., MAP_ANONYMOUS | MAP_HUGETLB, ...) for 2 MiB returned the error "Cannot allocate memory". The method needs set-up before the program runs, and the program must be written for it. The reason for the effort is certainty: the program knows that it has huge pages and that the memory is there.

Explicit huge page pool

The explicit huge page pool is a number of huge pages that the administrator reserves in advance. Programs take pages from it with MAP_HUGETLB or hugetlbfs, and the pages stay in memory while they are reserved.

Example: writing 128 to /proc/sys/vm/nr_hugepages reserves 128 pages of 2 MiB, which is 256 MiB.

Check yourself: 1. What does the program need before MAP_HUGETLB can work? 2. Why did the test call fail? 3. For what kind of program is the explicit pool a good fit?