Linux huge pages不适用于malloc。

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

Linux huge pages doesn't work with malloc

问题

我已启用Linux THP以供所有进程使用:

  1. cat /sys/kernel/mm/transparent_hugepage/enabled
  2. [always] madvise never
  3. echo 1024 | tee /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages

但似乎我的示例代码,它分配并使用超过100MB的内存(理论上超过50个2MB的大页面),并未使用大页面,而是使用普通页面:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #define SIZE 1024*1024*100
  6. int main(void)
  7. {
  8. char *buf = malloc(SIZE);
  9. memset(buf, 'A', SIZE);
  10. for (;;) {
  11. printf("%p\n", buf);
  12. sleep(1);
  13. }
  14. }

在启动此示例之前和之后,我看到grep HugePages_ /proc/meminfo显示的是静态图片:

  1. HugePages_Total: 1024
  2. HugePages_Free: 1023
  3. HugePages_Rsvd: 0
  4. HugePages_Surp: 0

free命令显示在示例运行时使用了超过100MB的内存。

Linux THP不起作用的原因是什么?

英文:

I have Linux THP enabled for all processes:

  1. cat /sys/kernel/mm/transparent_hugepage/enabled
  2. [always] madvise never
  3. 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:

  1. #include &lt;stdio.h&gt;
  2. #include &lt;stdlib.h&gt;
  3. #include &lt;string.h&gt;
  4. #include &lt;unistd.h&gt;
  5. #define SIZE 1024*1024*100
  6. int main(void)
  7. {
  8. char *buf = malloc(SIZE);
  9. memset(buf, &#39;A&#39;, SIZE);
  10. for (;;) {
  11. printf(&quot;%p\n&quot;, buf);
  12. sleep(1);
  13. }
  14. }

Before and after starting this example I see the static picture wtih grep HugePages_ /proc/meminfo:

  1. HugePages_Total: 1024
  2. HugePages_Free: 1023
  3. HugePages_Rsvd: 0
  4. 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:

确定