Disable Transparent Huge Pages

Keywords: Database Red Hat Linux sudo CentOS

Full text translated into MongoDB official manual I've tried to keep the original version. But the capacity is limited...

  • Boot script

  • Using tunde and ktune

  • Test whether the configuration is in effect

Be careful:
This page describes how to disable Transparent Huge Page on Red Hat Enterprise Linux and entOS 6 and 7. If you need to disable THP on other systems, please refer to the corresponding documentation.

Transparent Huge Pages (THP) is a Linux memory management mechanism that reduces the addressing overhead of Translation Lookaside Buffer (TLB) when using larger memory pages.
However, database systems usually perform poorly when THP is used because they tend to have sparse rather than continuous memory access patterns. You should disable THP on the Linux host again to ensure that MongoDB has the best performance.

Boot script

Important:

If you use tuned or ktune (for example, if you use Red Hat or CentOS 6 or later, you must add additional settings to ensure that THP will not be re-enabled, see Tuned and ktune

1. Create boot scripts

Create this file and write to / etc/init.d/disable-transparent-hugepages as follows:

#!/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' > ${thp_path}/enabled
    echo 'never' > ${thp_path}/defrag

    re='^[0-1]+$'
    if [[ $(cat ${thp_path}/khugepaged/defrag) =~ $re ]]
    then
      # RHEL 7
      echo 0  > ${thp_path}/khugepaged/defrag
    else
      # RHEL 6
      echo 'no' > ${thp_path}/khugepaged/defrag
    fi

    unset re
    unset thp_path
    ;;
esac

2. Give runnable permissions:

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

3. Configure this script to boot up:

Distribution and 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

4. Overlay tuned and ktune settings

If you use tuned or ktune (for example, if you use Red Hat or CentOS 6 or later, you must configure it immediately to protect the above settings).

Use tuned or ktune

If you use tuned or ktune, you must perform this step in addition to installing boot scripts

Tuned and ktune are dynamic kernel tuning tools that can disable THP on Redhat or CentOS. To disable THP in tuned or ktune, you need to create a new configuration file to set the state of THP to never

Red hat and entOS 6

1. Create a new configuration file

Copy the default configuration file from the relevant directory and rename it. In this case, we copy default and name it np-top:

sudo cp -r /etc/tune-profiles/default /etc/tune-profiles/no-thp

2. Editing ktune.sh

Edit/etc/tune-profiles/no-thp/ktune.sh by adding the following line:

set_transparent_hugepages never

The start() block of this file, before return 0.

3. Make this file available

sudo tuned-adm profile no-thp

Red Hat /Cent OS 7

1. Create new files

Create a new tuned configuration file directory:

sudo mkdir /etc/tuned/no-thp

2. Edit tuned.conf file

Create and compile/etc/tuned/no-thp/tuned.conf, and write the following:

[main]
include=virtual-guest

[vm]
transparent_hugepages=never

3. Enabling new documents:

sudo tuned-adm profile no-thp

Test your configuration

Check the THP status with the following commands:

cat /sys/kernel/mm/transparent_hugepage/enabled
cat /sys/kernel/mm/transparent_hugepage/defrag

In Red Hat Enterprise Linux or CentOS or other Red hat Linux-based distributions, you may need to use the following commands:

cat /sys/kernel/mm/redhat_transparent_hugepage/enabled
cat /sys/kernel/mm/redhat_transparent_hugepage/defrag

The output should be

always madvise [never]

Posted by fresh on Mon, 27 May 2019 12:24:32 -0700