英文:
Linux huge pages doesn't work with malloc
问题
我已启用Linux THP以供所有进程使用:
cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never
echo 1024 | tee /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
但似乎我的示例代码,它分配并使用超过100MB的内存(理论上超过50个2MB的大页面),并未使用大页面,而是使用普通页面:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define SIZE 1024*1024*100
int main(void)
{
char *buf = malloc(SIZE);
memset(buf, 'A', SIZE);
for (;;) {
printf("%p\n", buf);
sleep(1);
}
}
在启动此示例之前和之后,我看到grep HugePages_ /proc/meminfo
显示的是静态图片:
HugePages_Total: 1024
HugePages_Free: 1023
HugePages_Rsvd: 0
HugePages_Surp: 0
但free
命令显示在示例运行时使用了超过100MB的内存。
Linux THP不起作用的原因是什么?
英文:
I have Linux THP enabled for all processes:
cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never
echo 1024 | tee /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
But seems that my example code which allocetes and uses more than 100Mb (supposedly over 50 2MB huge pages) doesn't use huge pages at all and uses regular pages:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define SIZE 1024*1024*100
int main(void)
{
char *buf = malloc(SIZE);
memset(buf, 'A', SIZE);
for (;;) {
printf("%p\n", buf);
sleep(1);
}
}
Before and after starting this example I see the static picture wtih grep HugePages_ /proc/meminfo
:
HugePages_Total: 1024
HugePages_Free: 1023
HugePages_Rsvd: 0
HugePages_Surp: 0
But free
shows that over +100Mb are used while example is running.
What is the reason of Linux THP not working?
答案1
得分: 0
如果您阅读文档,您将发现您执行的命令并未检查THP大页的使用情况,而是检查了显式的大表(源自libhugetlbfs)。
要查找THP大页的使用情况,您应该执行以下操作:
grep AnonHugePages /proc/meminfo
取自https://access.redhat.com/solutions/46111
英文:
If you read the documentation you will discover that the command you performed is not checking THP huge pages usage but instead explicit Huge Tables ( which originates from libhugetlbfs )
To find the usage of THP Huge pages you should instead perform the following:
grep AnonHugePages /proc/meminfo
taken from https://access.redhat.com/solutions/46111
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论