对大型页面使用mmap和madvise [英] Using mmap and madvise for huge pages

查看:665
本文介绍了对大型页面使用mmap和madvise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Linux机器正在使用的大页面上分配内存.我看到使用mmapmadvise有两种方法可以做到这一点.

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 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实际上将继续并保留内核内部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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆