Navigation

Disable Transparent Huge Pages (THP)

Transparent Huge Pages (THP) is a Linux memory management system that reduces the overhead of Translation Lookaside Buffer (TLB) lookups on machines with large amounts of memory by using larger memory pages.

However, database workloads often perform poorly with THP enabled, because they tend to have sparse rather than contiguous memory access patterns. When running MongoDB on Linux, THP should be disabled for best performance.

To ensure that THP is disabled before mongod starts, you should create a new init.d script to apply the appropriate THP setting, and configure it to run at boot. Additionally, for RHEL / CentOS systems that make use of ktune and tuned performance profiles, you must create a custom tuned profile as well.

Init Script

1

Create the init.d script.

Create the following file at /etc/init.d/disable-transparent-hugepages:

#!/bin/bash
### BEGIN INIT INFO
# Provides:          disable-transparent-hugepages
# Required-Start:    $local_fs
# Required-Stop:
# X-Start-Before:    mongod mongodb-mms-automation-agent
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Disable Linux transparent huge pages
# Description:       Disable Linux transparent huge pages, to improve
#                    database performance.
### END INIT INFO

case $1 in
  start)
    if [ -d /sys/kernel/mm/transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/transparent_hugepage
    elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/redhat_transparent_hugepage
    else
      return 0
    fi

    echo 'never' | tee ${thp_path}/enabled > /dev/null

    unset thp_path
    ;;
esac
2

Make it executable.

Run the following command to make the script executable:

sudo chmod 755 /etc/init.d/disable-transparent-hugepages
3

Run the script.

Run the script manually once to ensure that the appropriate THP setting has been changed:

sudo /etc/init.d/disable-transparent-hugepages start

Verify that THP has successfully been set to [never] by running the following command:

cat /sys/kernel/mm/transparent_hugepage/enabled

On Red Hat Enterprise Linux, CentOS, and potentially other Red Hat-based derivatives, you may instead need to use the following:

cat /sys/kernel/mm/redhat_transparent_hugepage/enabled
4

Configure your operating system to run it on boot.

To ensure that this setting is applied each time your system boots, run the following command for your Linux distribution:

Distribution Command
Ubuntu and Debian
sudo update-rc.d disable-transparent-hugepages defaults
SUSE
sudo insserv /etc/init.d/disable-transparent-hugepages
Red Hat, CentOS, Amazon Linux, and derivatives
sudo chkconfig --add disable-transparent-hugepages
5

Customize tuned / ktune profile, if applicable.

If you are using tuned or ktune on RHEL/ CentOS, you must now also create a custom tuned profile.

Using tuned and ktune

Important

If using tuned or ktune, you must also perform the steps in this section after installing the init script.

tuned and ktune are dynamic kernel tuning tools that can affect the transparent huge pages setting on your system. If you are using tuned / ktune on your RHEL / CentOS system while running mongod, you must create a custom tuned profile to ensure that THP remains disabled.

Red Hat/CentOS 6

1

Create a new profile.

Create a new profile from an existing profile by copying the relevant directory. This example uses the virtual-guest profile as the base, and uses virtual-guest-no-thp as the new profile:

sudo cp -r /etc/tune-profiles/virtual-guest /etc/tune-profiles/virtual-guest-no-thp
2

Edit ktune.sh.

Edit /etc/tune-profiles/virtual-guest-no-thp/ktune.sh and change the set_transparent_hugepages setting to the following:

set_transparent_hugepages never
3

Enable the new profile.

Enable the new profile:

sudo tuned-adm profile virtual-guest-no-thp

Red Hat/CentOS 7 and 8

1

Create a new profile.

Create a new directory to hold the custom tuned profile. This example inherits from the existing virtual-guest profile, and uses virtual-guest-no-thp as the new profile:

sudo mkdir /etc/tuned/virtual-guest-no-thp
2

Edit tuned.conf.

Create and edit /etc/tuned/virtual-guest-no-thp/tuned.conf so that it contains the following:

[main]
include=virtual-guest

[vm]
transparent_hugepages=never

This example inherits from the existing virtual-guest profile. Select the profile most appropriate for your system.

5

Enable the new profile.

Enable the new profile:

sudo tuned-adm profile virtual-guest-no-thp