Configuring Transparent HugePages

The Transparent HugePages (THP) feature is enabled by default in Oracle Linux. However, you might still need to access and configure THP according to the system’s needs.

The following sections look at various THP parameters and examples of how they can be configured.

Parameters Used to Configure Transparent HugePages

The following table describes selected parameter settings that can be used when configuring Transparent HugePages (THP).

Table 6-3 Commonly Used THP Parameters

Parameter File Location Value Options
enabled /sys/kernel/mm/transparent_hugepage/enabled Sets THP and its mode, which is one of the following:
  • always (default): THP is enabled in system-wide mode.

    In this setting, the kernel, whenever possible, assigns huge pages to processes using large contiguous virtual memory areas.

  • madvise: THP is enabled in per-process mode.

    In this setting the kernel only assigns huge pages to application processes that explicitly request huge pages through the madvise()system call.

  • disabled: THP is disabled.
defrag /sys/kernel/mm/transparent_hugepage/defrag
Determines how aggressively an application can reclaim pages and defrag memory when THP is unavailable. The following list explains the available options:
  • always: An application requesting THP stalls on allocation failure and directly reclaims pages and compact memory to obtain a THP immediately.

  • defer: An application doesn't stall but continues using small pages. The application requests the kernel daemons kswapd and kcompactd to reclaim pages and compact memory so that THP is available later.
  • defer+madvise:

    Regions using the madvise(MADV_HUGEPAGE) call stall on allocation failure and directly reclaim pages and compact memory to obtain a THP immediately

    However, all other regions request the kernel daemons kswapd and kcompactd to reclaim pages and compact memory so that THP is available later.

  • madvise (default): Regions using the madvise(MADV_HUGEPAGE) call stall on allocation failure and directly reclaim pages and compact memory to obtain a THP immediately.

Configuring Transparent HugePages at Runtime

The following sections show you examples of how you can configure THP at runtime by accessing the THP parameters in the sysfs virtual file system.

Retrieving the Current Status of Transparent HugePages

To see the current setting of THP you can read the /sys/kernel/mm/transparent_hugepage/enabled parameter as shown in the following code sample:
sudo cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never

The value inside the square brackets represents the current setting.

Changing the Current Status of Transparent HugePages

To change the current status of THP, you need to write the preferred settings to /sys/kernel/mm/transparent_hugepage/enabled. The following example shows you how to set the status to always:

  1. Check the current status of THP by reading the enabled parameter.
    sudo cat /sys/kernel/mm/transparent_hugepage/enabled
    always madvise [never]

    The value inside the square brackets represents the current setting.

  2. Set THP mode to always.
    echo always | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
  3. Confirm the change has been successful by reading the enabled parameter.
    sudo cat /sys/kernel/mm/transparent_hugepage/enabled
    [always] madvise never

Note:

Virtual file systems such as sysfs provide a file system interface to items that aren't necessarily stored as files on disk. The sysfs files therefore don't always interact with file commands in the same way that regular physical files on disk would. In the previous example, the echo command used doesn't overwrite /sys/kernel/mm/transparent_hugepage/enabled, as it would if used with a regular file, but instead changes the selected option:
sudo cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
echo always | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
always
sudo cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never

.

Changing the defrag Setting of Transparent HugePages

To change the THP defrag setting you need to write the setting of your choice to /sys/kernel/mm/transparent_hugepage/defrag

Note:

The best defrag setting varies from system to system. Reclaiming pages and memory compaction can increase the number of THP pages available. However, the process also uses CPU time. Therefore, you need to find the correct balance for a specific system.

The following example shows you how to set the defrag setting to madvise.

  1. Check the current value of the defrag parameter:
    sudo cat /sys/kernel/mm/transparent_hugepage/defrag
    [always] defer defer+madvise madvise never

    The value inside square brackets represents the current setting.

  2. Set the /sys/kernel/mm/transparent_hugepage/defrag parameter to madvise:
    echo madvise | sudo tee /sys/kernel/mm/transparent_hugepage/defrag
  3. Confirm the change has worked by reading the defrag parameter.
    sudo cat /sys/kernel/mm/transparent_hugepage/defrag
    always defer defer+madvise [madvise] never