Tuesday, October 5, 2021

Disabling Transparent Huge Pages for Improved Performance

Transparent Huge Pages (THP) is a memory management feature in the Linux kernel that aims to improve system performance by utilizing large memory pages. However, in some cases, THP can actually cause performance issues or compatibility problems with certain applications. If you're experiencing such issues, disabling THP might be a viable solution. In this blog post, we'll explore how to disable THP both at runtime and during boot time, as well as how to create a "pool" of huge pages in a single node.

First, let's check the current status of THP on your system. You can use the following commands to get the relevant information:

$ cat /proc/meminfo | grep AnonHuge
AnonHugePages:   3536896 kB

$ ps -aux | grep huge
root          38  0.0  0.0      0     0 ?        SN   May04   0:03 [khugepaged]
tigergr+ 2438456  0.0  0.0 114284   972 pts/0    S+   14:08   0:00 grep --color=auto huge

The output shows the current amount of AnonHugePages and any processes related to huge pages.

To disable THP at runtime, you can use the following commands as the root user:

# echo never > /sys/kernel/mm/transparent_hugepage/enabled
# echo never > /sys/kernel/mm/transparent_hugepage/defrag

These commands set the enabled and defrag parameters of the transparent_hugepage interface to "never," effectively disabling THP.

If you want to disable THP during boot time, you'll need to modify the GRUB configuration. Open the /etc/default/grub file and locate the GRUB_CMDLINE_LINUX_DEFAULT line. Add transparent_hugepage=never to the list of parameters within the quotation marks. The line should look something like this:

GRUB_CMDLINE_LINUX_DEFAULT="(...) transparent_hugepage=never"

Save the file and update the GRUB configuration using the appropriate command for your system (e.g., update-grub or grub-mkconfig -o /boot/grub/grub.cfg).

Now, let's move on to creating a "pool" of huge pages in a single node. This can be useful for specific applications that benefit from using large contiguous memory regions. To create a pool of huge pages, follow these steps:

  1. Set the vm.nr_hugepages parameter to 0 using the sysctl command:
# sysctl -w vm.nr_hugepages=0
  1. Specify the number of huge pages you want to allocate by writing to the nr_hugepages file in the corresponding node's directory. For example, to allocate 10 huge pages (each 1048576kB in size) in node0:
# echo 10 > /sys/devices/system/node/node0/hugepages/hugepages=1048576kB/nr_hugepages
  1. Verify the allocation by checking the status of the node's huge pages using the numastat command:
# numastat -cm | egrep 'Node|Huge'

The output will display information about the allocated huge pages in the specified node.

By disabling THP or creating a pool of huge pages, you can fine-tune your system's memory management to better suit your specific requirements. Whether you're experiencing performance issues or looking to optimize memory usage for certain applications, these techniques provide flexibility and control over THP.