All Products
Search
Document Center

Alibaba Cloud Linux:THP reclaim

Last Updated:Mar 18, 2024

The Transparent Huge Page (THP) reclaim feature allows you to resolve the memory issues caused by THPs, such as out of memory (OOM) errors. This topic describes the interfaces that are used to implement the THP reclaim feature and provides examples on how to use the interfaces.

Background information

In Linux operating systems, memory is managed in blocks known as pages. Regular memory pages are 4 KiB in size. THPs are blocks of memory that can be 2 MiB and 1 GiB in size. Applications consume increasing amounts of memory, which results in high address translation overheads. When applications request memory, the kernel dynamically allocates THPs to reduce Translation Lookaside Buffer (TLB) misses and improve application performance.

However, THPs may cause memory bloat. If you enable the THP feature, the kernel allocates and frees 2MiB blocks of memory as THPs. Each THP is equivalent to 512 4KiB pages. THPs may cause high memory fragmentation, which results in memory bloat.

Memory bloat may cause OOM errors. For example, if an application that requests 8 KiB of memory (two 4KiB pages) is allocated a 2MiB THP, the THP includes two 4KiB pages that the application requests and 510 4KiB pages that are filled with zeros, known as zero pages. This represents a significant waste of resident set size (RSS) and may lead to an OOM error.

To resolve memory issues that are caused by THPs, Alibaba Cloud Linux provides the THP reclaim feature for memory control groups. The THP reclaim feature splits THPs into subpages and reclaims zero subpages to prevent OOM errors that are caused by memory bloat. However, the THP reclaim feature may degrade memory performance.

Interfaces

The following table describes the interfaces that are used to implement the THP reclaim feature.

Interface

Description

memory.thp_reclaim

Enables or disables the THP reclaim feature. Valid values:

  • reclaim: enables the THP reclaim feature.

  • swap: This value is reserved for future use.

  • disable: disables the THP reclaim feature.

Default value: disable.

memory.thp_reclaim_stat

Queries the status of the THP reclaim feature. Parameters used in this interface:

  • queue_length: the number of THPs in the queue of each node. If the THP reclaim feature is enabled, THPs are added to a reclaim queue.

  • split_hugepage: the total number of THPs that are split by the THP reclaim feature for each node.

  • reclaim_subpage: the total number of zero subpages that are reclaimed by the THP reclaim feature for each node.

The values of the preceding parameters are listed in ascending order of NUMA node IDs, such as node0 and node1, from left to right.

memory.thp_reclaim_ctrl

Controls how the THP reclaim feature is triggered. Parameters used in this interface:

  • threshold: The maximum number of zero subpages in a THP. If the number of zero subpages in a THP exceeds the threshold value, the THP reclaim feature is triggered. Default value: 16.

  • reclaim: triggers the THP reclaim feature.

/sys/kernel/mm/transparent_hugepage/reclaim

The global interface. If you do not want to configure the THP reclaim feature by memory control group, you can use this interface. Valid values:

  • memcg: Each memory control group uses its memory.thp_reclaim interface configuration. This is the default value.

  • reclaim: forcefully enables the THP reclaim feature for all memory control groups.

  • swap: This value is reserved for future use.

  • disable: forcefully disables the THP reclaim feature for all memory control groups.

Limits

Alibaba Cloud Linux images that contain the following kernel versions support the THP reclaim feature:

  • Alibaba Cloud Linux 2: kernel 4.19.91-24.al7 or later

  • Alibaba Cloud Linux 3: kernel 5.10.134-15.al8 or later

Note

You can run the uname -r command to check the kernel version contained in an image.

Configure the THP reclaim feature

This section describes how to configure the THP reclaim feature. In this example, a memory control group named test is used.

  1. Run the following command to create a memory control group named test:

    sudo mkdir /sys/fs/cgroup/memory/test/
  2. Run the following command to enable the THP reclaim feature for test:

    sudo sh -c 'echo reclaim > /sys/fs/cgroup/memory/test/memory.thp_reclaim'
  3. Run the following command to check whether the THP reclaim feature is enabled for test:

    cat /sys/fs/cgroup/memory/test/memory.thp_reclaim

    The following command output is returned. The settings that take effect are enclosed in brackets ([]). [reclaim] in the command output indicates that the THP reclaim feature is enabled for test.

    image

  4. Run the following command to forcefully enable the THP reclaim feature by using the global interface:

    sudo sh -c 'echo reclaim > /sys/kernel/mm/transparent_hugepage/reclaim'

    Run the following command to forcefully disable the THP reclaim feature:

    sudo sh -c 'echo disable > /sys/kernel/mm/transparent_hugepage/reclaim'
    Note

    If you set the /sys/kernel/mm/transparent_hugepage/reclaim global interface to reclaim or disable, the global interface takes precedence over the memory.thp_reclaim interface. The memory.thp_reclaim interface configurations in memory control groups are not affected.

  5. Run the following command to configure the threshold parameter in the memory.thp_reclaim_ctrl interface to limit the number of zero subpages in a THP for test:

    sudo sh -c 'echo "threshold 32" >  /sys/fs/cgroup/memory/test/memory.thp_reclaim_ctrl'

    In this example, the threshold parameter is set to 32. When the number of zero subpages in a THP exceeds 32, the zero subpage reclaim subfeature of the THP reclaim feature is triggered to reclaim zero subpages.

  6. Trigger the zero subpage reclaim subfeature of the THP reclaim feature.

    After the zero subpage reclaim subfeature of the THP reclaim feature is triggered, the subfeature reclaims excess zero subpages when the number of zero subpages exceeds the value of threshold. Take note of the following items for the reclaim parameter:

    Note

    You can only write reclaim to the memory.thp_reclaim_ctrl interface to trigger the zero subpage reclaim subfeature of the THP reclaim feature. You cannot run the cat command to query the reclaim settings.

    • Run the following command to trigger the zero subpage reclaim subfeature of the THP reclaim feature for the current memory control group:

      sudo sh -c 'echo "reclaim 1" >  /sys/fs/cgroup/memory/test/memory.thp_reclaim_ctrl'
    • Run the following command to recursively trigger the zero subpage reclaim subfeature of the THP reclaim feature for the current memory control group and its child control groups:

      sudo sh -c 'echo "reclaim 2" >  /sys/fs/cgroup/memory/test/memory.thp_reclaim_ctrl'

      You can trigger the zero subpage reclaim subfeature of the THP reclaim feature by writing reclaim to the memory.thp_reclaim_ctrl interface. The THP reclaim feature can be triggered by memory reclamation.

      • If an OOM error occurs, the zero subpage reclaim subfeature of the THP reclaim feature is triggered.

      • If the memcg backend asynchronous reclaim feature is triggered, the zero subpage reclaim subfeature of the THP reclaim feature is triggered. For information about the memcg backend asynchronous reclaim feature, see Memcg backend asynchronous reclaim.

  7. Run the following command to check the status of the THP reclaim feature for test:

    cat /sys/fs/cgroup/memory/test/memory.thp_reclaim_stat

    The following command output is returned:

    queue_length        14
    split_hugepage     523
    reclaim_subpage 256207

    In the preceding command output:

    • queue_length: indicates that 14 THPs are in the queue.

    • split_hugepage: indicates that 523 THPs are split by the THP reclaim feature.

    • reclaim_subpage: indicates that 256,207 zero subpages are reclaimed by the THP reclaim feature.

Test the THP reclaim feature

This section provides sample C code that is used to request THPs for applications. You can use the sample C code to perform a test when the THP reclaim feature is enabled and disabled, and obtain different test results.

  1. Run the following command to set memory.limit_in_bytes to 1 GiB:

    sudo sh -c 'echo 1G > /sys/fs/cgroup/memory/test/memory.limit_in_bytes'

    Run the following command to query the value of memory.limit_in_bytes:

    cat /sys/fs/cgroup/memory/test/memory.limit_in_bytes

    The following command output is returned.

    image

  2. Run the following command to disable the memcg backend asynchronous reclaim feature.

    For information about the memcg backend asynchronous reclaim feature, see Memcg backend asynchronous reclaim.

    sudo sh -c 'echo 0 > /sys/fs/cgroup/memory/test/memory.wmark_ratio'
  3. Run the following sample C code when the THP reclaim feature is enabled and disabled and check the test results:

    gcc -o test <test.c>

    Replace parameters in the <test.c> sample C code with actual values based on your business requirements.

    // The application requests 1 GiB of memory (512 THPs), in which 10 THPs contain zero subpages. 
    #include <stdlib.h>  // For posix_memalign
    #include <string.h>  // For memset
    #include <unistd.h>  // For pause
    
    #define HUGEPAGE_SIZE 4096 * 512
    int main()
    {
            int i, thp = 512;
            char *addr;
            posix_memalign((void **)&addr, HUGEPAGE_SIZE, HUGEPAGE_SIZE * thp);
    
            for (i = 0; i < 10; i++) {
                    memset(addr, 0xc, HUGEPAGE_SIZE >> 1);
                    addr += HUGEPAGE_SIZE;
            }
    
            for (; i < thp; i++) {
                    memset(addr, 0xc, HUGEPAGE_SIZE);
                    addr += HUGEPAGE_SIZE;
            }
    
            pause();
            return 0;
    }

    During the test, you can run the dmesg -wH command on another terminal to detect OOM errors and memory usage in the system. The test results vary based on whether the THP reclaim feature is enabled.

    • If the THP reclaim feature is enabled, the feature splits the previously allocated THPs into subpages and reclaims zero subpages. This reduces memory usage and prevents OOM errors.

    • If the THP reclaim feature is disabled, the system does not split THPs or reclaim zero subpages. As a result, OOM errors may occur. Kernel logs indicate insufficient memory and the details of the processes that may be terminated.

References