Linux huge pages不适用于malloc。

huangapple go评论79阅读模式
英文:

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 &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;unistd.h&gt;
#define SIZE 1024*1024*100

int main(void)
{
        char *buf = malloc(SIZE);
        memset(buf, &#39;A&#39;, SIZE);
        for (;;) {
                printf(&quot;%p\n&quot;, 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

huangapple
  • 本文由 发表于 2023年7月20日 20:39:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定