3

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?

UpAndAdam
  • 4,515
  • 3
  • 28
  • 46
NK-cell
  • 1,145
  • 6
  • 19
  • This is likely relevant: [**Why does malloc rely on mmap starting from a certain threshold?**](https://stackoverflow.com/questions/33128587/why-does-malloc-rely-on-mmap-starting-from-a-certain-threshold) – Andrew Henle Jul 20 '23 at 12:53
  • Is your application linked with libhugetlbfs? – stark Jul 20 '23 at 13:06
  • 2
    @stark Nope, and I'm sure it shouldn't. Typical DPDK application that perfectly uses huge pages, has no references to this library just like all DPDK rte_* libraries... – NK-cell Jul 20 '23 at 13:51
  • @stark He's using THP so he doesnt have to link to libhugetlbfs – UpAndAdam Jul 20 '23 at 16:39
  • 1
    did you grep `grep AnonHugePages /proc/meminfo ` thats where the kernel docs say they should be showing up. The values you show are for explicit HugePages from the lib. – UpAndAdam Jul 20 '23 at 16:40
  • @UpAndAdam Great!!! Indeed `AnonHugePages` changes... Can you explain and sketch out it as answer? – NK-cell Jul 21 '23 at 12:58
  • @NK-cell done thanks for confirming that worked. – UpAndAdam Jul 24 '23 at 15:39

1 Answers1

0

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

UpAndAdam
  • 4,515
  • 3
  • 28
  • 46