Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using mmap and madvise for huge pages

Tags:

I want to allocate memory on the hugepages being used by a Linux machine. I see that there are two ways to do this, using mmap and madvise.

That is, using the MAP_HUGETLB flag with the mmap call -

base_ptr_ = mmap(NULL, memory_size_, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0); 

And the MADV_HUGEPAGE flag with the madvise call -

madvise(base_ptr_, memory_size_, MADV_HUGEPAGE); 

Could someone explain the difference between the two?

like image 1000
ssb Avatar asked May 26 '15 23:05

ssb


People also ask

What is the difference between HugePages and transparent huge pages?

Transparent Hugepages are similar to standard HugePages. However, while standard HugePages allocate memory at startup, Transparent Hugepages memory uses the khugepaged thread in the kernel to allocate memory dynamically during runtime, using swappable HugePages.

How do I allocate a large page?

The administrator can allocate persistent huge pages on the kernel boot command line by specifying the "hugepages=N" parameter, where 'N' = the number of huge pages requested. This is the most reliable method of allocating huge pages as memory has not yet become fragmented.

What is Madvise in transparent huge pages?

tl;dr ︎ “Transparent Hugepages” is a Linux kernel feature intended to improve performance by making more efficient use of your processor's memory-mapping hardware. It is enabled (" enabled=always ") by default in most Linux distributions.


1 Answers

Both functions perform different operations, which may or may not matter in your context:

  • madvise sets a flag for all the memory mappings corresponding to the region passed to it, telling the khugepaged kernel thread that it can consider said mappings for promotion to huge pages. That will only work if transparent hugepage support is enabled (The status of transparent hugepage support is available under /sys/kernel/mm/transparent_hugepage/enabled), which will be the case in most distros, but may be disabled on embedded systems.

  • mmap will actually go ahead and reserve the pages from the kernel's internal hugetlbfs mount, whose status can be seen under /sys/kernel/mm/hugepages. The pages in question need to be available by the time mmap is invoked (see HugePages_Free in /proc/meminfo), or mmap will fail.

The two mechanisms have their own doc file in the kernel tree: hugetlbpage.txt and transhuge.txt

like image 159
Frederik Deweerdt Avatar answered Sep 18 '22 14:09

Frederik Deweerdt