禁用透明大页面(THP)

在本页面

透明大页面(THP)是一种 Linux 内存 Management 系统,它通过使用较大的内存页面来减少具有大量内存的计算机上的转换后备缓冲区(TLB)查找的开销。

但是,数据库工作负载在启用 THP 的情况下通常表现不佳,因为它们往往具有稀疏而不是连续的内存访问模式。在 Linux 上运行 MongoDB 时,应禁用 THP 以获得最佳性能。

为确保在mongod开始之前禁用 THP,应为平台的初始化系统创建一个服务文件,该文件在引导时禁用 THP。以下提供了有关 systemdSystem V init 初始化系统的说明。

另外,对于使用ktunetuned性能概要文件的 RHEL/CentOS 系统,您还必须创建一个自定义tuned概要文件。

创建服务文件

要创建禁用 THP 的服务文件,您将使用平台的内置初始化系统。最新版本的 Linux 倾向于使用 systemd (使用systemctl命令),而较早版本的 Linux 倾向于使用 System V init (使用service命令)。有关更多信息,请参阅您的 os 的文档。

使用适合您平台的初始化系统:

systemd (systemctl)
System V Init (service)

Create the systemd unit file.

Create the following file at /etc/systemd/system/disable-transparent-huge-pages.service :

[Unit]
Description=Disable Transparent Huge Pages (THP)
DefaultDependencies=no
After=sysinit.target local-fs.target
Before=mongod.service

[Service]
Type=oneshot
ExecStart=/bin/sh -c 'echo never | tee /sys/kernel/mm/transparent_hugepage/enabled > /dev/null'

[Install]
WantedBy=basic.target

Note

Some versions of Red Hat Enterprise Linux, and potentially other Red Hat-based derivatives, use a different path for the THP enabled file:

/sys/kernel/mm/redhat_transparent_hugepage/enabled

Check to see which path is in use on your system, and update the disable-transparent-huge-pages.service file accordingly.

Note

Prior to version 4.2, MongoDB also checks the THP defrag setting and presents a startup warning if defrag is enabled. As long as THP itself is disabled in the systemd unit file, MongoDB is unaffected by the defrag setting. However, to avoid this message, you may set defrag to never by adding the following additional line to the systemd unit file, just after the existing ExecStart statement:

ExecStart=/bin/sh -c 'echo never | tee /sys/kernel/mm/transparent_hugepage/defrag > /dev/null'

If on Red Hat or similar, the path to the defrag file might be different. See the note above for more details, and and update the disable-transparent-huge-pages.service file accordingly.

Reload systemd unit files.

Run the following command to reload systemd unit files to make disable-transparent-huge-pages.service available for use:

sudo systemctl daemon-reload

Start the service.

Start the service manually once to ensure that the appropriate THP setting has been changed:

sudo systemctl start disable-transparent-huge-pages

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 and potentially other Red Hat-based derivatives, you may instead need to use the following:

cat /sys/kernel/mm/redhat_transparent_hugepage/enabled

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:

sudo systemctl enable disable-transparent-huge-pages

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.

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

Note

MongoDB 3.6 also checks the THP defrag setting and presents a startup warning if defrag is enabled. As long as THP itself is disabled in the init.d script, MongoDB is unaffected by the defrag setting. However, to avoid this message, you may set defrag to never by adding the following line to the init.d script, just before the unset thp_path statement:

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

Make it executable.

Run the following command to make the script executable:

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

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 and potentially other Red Hat-based derivatives, you may instead need to use the following:

cat /sys/kernel/mm/redhat_transparent_hugepage/enabled

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:

DistributionCommand
Ubuntu and Debiansudo update-rc.d disable-transparent-hugepages defaults
SUSEsudo insserv /etc/init.d/disable-transparent-hugepages
Red Hat, CentOS, Amazon Linux, and derivativessudo chkconfig --add disable-transparent-hugepages

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.

使用已调谐和 ktune

Important

如果使用tunedktune,则还必须在创建上面的服务文件之后执行本节中的步骤。

tunedktune是动态内核调整工具,可以影响系统上透明大页面的设置。如果在运行mongod时在 RHEL/CentOS 系统上使用tuned/ktune,则必须创建一个自定义tuned配置文件以确保 THP 保持禁用状态。

Red Hat/CentOS 6

创建一个新的配置文件。

通过复制相关目录,从现有的配置文件创建新的配置文件。本示例使用virtual-guest配置文件作为基础,并使用virtual-guest-no-thp作为新配置文件:

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

Edit ktune.sh.

编辑/etc/tune-profiles/virtual-guest-no-thp/ktune.sh并将set_transparent_hugepages设置更改为以下内容:

set_transparent_hugepages never

启用新的配置文件。

启用新的配置文件:

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

Red Hat/CentOS 7 和 8

创建一个新的配置文件。

创建一个新目录来保存自定义tuned配置文件。本示例继承自现有的virtual-guest配置文件,并使用virtual-guest-no-thp作为新的配置文件:

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

Edit tuned.conf.

创建并编辑/etc/tuned/virtual-guest-no-thp/tuned.conf,使其包含以下内容:

[main]
include=virtual-guest

[vm]
transparent_hugepages=never

本示例继承自现有的virtual-guest配置文件。选择最适合您的系统的配置文件。

启用新的配置文件。

启用新的配置文件:

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