对大型页面使用mmap和madvise [英] Using mmap and madvise for huge pages
问题描述
我想在Linux机器正在使用的大页面上分配内存.我看到使用mmap
和madvise
有两种方法可以做到这一点.
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
.
也就是说,在mmap
调用中使用MAP_HUGETLB
标志-
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);
和带有madvise
调用的MADV_HUGEPAGE
标志-
madvise(base_ptr_, memory_size_, MADV_HUGEPAGE);
有人可以解释两者之间的区别吗?
Could someone explain the difference between the two?
推荐答案
这两个函数执行不同的操作,这在您的上下文中可能无关紧要:
Both functions perform different operations, which may or may not matter in your context:
-
madvise
为与传递给它的区域相对应的所有内存映射设置一个标志,告诉khugepaged
内核线程它可以考虑将所述映射提升为大页面.只有启用了透明的大页面支持(/sys/kernel/mm/transparent_hugepage/enabled
中提供了透明的大页面支持的状态),这才起作用.在大多数发行版中都是这种情况,但可能在嵌入式系统上被禁用.
madvise
sets a flag for all the memory mappings corresponding to the region passed to it, telling thekhugepaged
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
实际上将继续并保留内核内部hugetlbfs
装载中的页面,其状态可以在/sys/kernel/mm/hugepages
下看到.有问题的页面需要在调用mmap
时可用(请参见/proc/meminfo
中的HugePages_Free
),否则mmap
将失败.
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.
这两种机制在内核树中都有自己的doc文件: hugetlbpage.txt 和 transhuge.txt
The two mechanisms have their own doc file in the kernel tree: hugetlbpage.txt and transhuge.txt
这篇关于对大型页面使用mmap和madvise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!