Docs HomeMongoDB Manual

Disable Transparent Huge Pages (THP)禁用透明大页面(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.透明大页面(THP)是一种Linux内存管理系统,它通过使用较大的内存页面来减少具有大量内存的机器上的翻译后备缓冲区(TLB)查找的开销。

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.然而,启用THP后,数据库工作负载通常表现不佳,因为它们往往具有稀疏而非连续的内存访问模式。在Linux上运行MongoDB时,应禁用THP以获得最佳性能。

To ensure that THP is disabled before mongod starts, you should create a service file for your platform's initialization system that disables THP at boot. 为了确保在mongod启动之前禁用THP,您应该为平台的初始化系统创建一个服务文件,在启动时禁用THP。Instructions are provided below for both the systemd and the System V init initialization systems.下面提供了systemdSystem V初始化系统的说明。

Additionally, for RHEL / CentOS systems that make use of ktune and tuned performance profiles, you must create a custom tuned profile as well.此外,对于使用ktunetuned性能配置文件的RHEL/CNTOS系统,您还必须创建一个自定义tuned配置文件。

Create a Service File创建服务文件

To create a service file that disables THP, you will use the built-in initialization system for your platform. 要创建禁用THP的服务文件,您将使用平台的内置初始化系统。Recent versions of Linux tend to use systemd (which uses the systemctl command), while older versions of Linux tend to use System V init (which uses the service command). Refer to the documentation for your operating system for more information.最新版本的Linux倾向于使用systemd(使用systemctl命令),而较旧版本的Linux则倾向于使用System V init(使用service命令)。有关详细信息,请参阅操作系统的文档。

Use the initialization system appropriate for your platform:使用适合您的平台的初始化系统:

1

Create the systemd unit file.创建systemd单位文件。

Create the following file at /etc/systemd/system/disable-transparent-huge-pages.service:/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:某些版本的Red Hat Enterprise Linux,以及其他可能基于Red Hat的衍生产品,对启用THP的文件使用不同的路径:

/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.检查系统中正在使用的路径,并相应地更新disable-transparent-huge-pages.service文件。

Note

Prior to version 4.2, MongoDB also checks the THP defrag setting and presents a startup warning if defrag is enabled. 在4.2版本之前,MongoDB还会检查THP分片整理设置,并在启用分片整理时显示启动警告。As long as THP itself is disabled in the systemd unit file, MongoDB is unaffected by the defrag setting. 只要THP本身在systemd单元文件中被禁用,MongoDB就不受分片整理设置的影响。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语句之后向systemd单元文件中添加以下附加行,将分片整理设置为never:

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. 如果在Red Hat或类似系统上,defrag文件的路径可能不同。See the note above for more details, and and update the disable-transparent-huge-pages.service file accordingly.有关更多详细信息,请参阅上面的注释,并相应地更新disable-transparent-huge-pages.service文件。

2

Reload systemd unit files.重新加载systemd单位文件。

Run the following command to reload systemd unit files to make disable-transparent-huge-pages.service available for use:运行以下命令以重新加载systemd单元文件,以使disable-transparent-huge-pages.service可用:

sudo systemctl daemon-reload
3

Start the service.启动服务。

Start the service manually once to ensure that the appropriate THP setting has been changed:手动启动服务一次,以确保适当的THP设置已更改:

sudo systemctl start disable-transparent-huge-pages

Verify that THP has successfully been set to [never] by running the following command:通过运行以下命令验证THP是否已成功设置为[never]

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:在Red Hat Enterprise Linux和其他可能基于Red Hat的衍生产品上,您可能需要使用以下内容:

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:要确保每次系统引导时都应用此设置,请运行以下命令:

sudo systemctl enable disable-transparent-huge-pages
5

Customize tuned / ktune profile, if applicable.自定义调谐/ktune配置文件(如果适用)。

If you are using tuned or ktune on RHEL/ CentOS, you must now also create a custom tuned profile.如果您在RHEL/CNTOS上使用tunedktune,现在还必须创建一个自定义的tuned配置文件。

1

Create the init.d script.创建init.d脚本。

Create the following file at /etc/init.d/disable-transparent-hugepages:/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

Prior to version 4.2, MongoDB also checks the THP defrag setting and presents a startup warning if defrag is enabled. 在4.2版本之前,MongoDB还会检查THP分片整理设置,并在启用分片整理时显示启动警告。As long as THP itself is disabled in the init.d script, MongoDB is unaffected by the defrag setting. 只要在init.d脚本中禁用THP本身,MongoDB就不受分片整理设置的影响。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:但是,为了避免出现此消息,您可以通过在init.d脚本中添加以下行将defrag设置为never,就在unset thp_path语句之前:

echo 'never' | tee ${thp_path}/defrag > /dev/null
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:手动运行脚本一次,以确保适当的THP设置已更改:

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

Verify that THP has successfully been set to [never] by running the following command:通过运行以下命令验证THP是否已成功设置为[never]

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:在Red Hat Enterprise Linux和其他可能基于Red Hat的衍生产品上,您可能需要使用以下内容:

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:要确保每次系统引导时都应用此设置,请为Linux发行版运行以下命令:

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 derivativesRed Hat、CentOS、Amazon Linux及其衍生产品
sudo chkconfig --add disable-transparent-hugepages
5

Customize tuned / ktune profile, if applicable.自定义tuned/ktune配置文件(如果适用)。

If you are using tuned or ktune on RHEL/ CentOS, you must now also create a custom tuned profile.如果您在RHEL/CNTOS上使用tunedktune,现在还必须创建一个自定义的tuned配置文件。

Using tuned and ktune使用tunedktune

Important

If using tuned or ktune, you must also perform the steps in this section after creating the service file above.如果使用tunedktune,您还必须在创建上面的服务文件后执行本节中的步骤。

tuned and ktune are dynamic kernel tuning tools that can affect the transparent huge pages setting on your system. tunedktune是动态内核调优工具,可以影响系统上的透明大页面设置。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.如果您在运行mongod时在RHEL/CNTOS系统上使用tuned / ktune,则必须创建一个自定义的调优配置文件,以确保THP保持禁用状态。

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:此示例使用virtual-guest配置文件作为基础,并使用virtual-guest-no-thp作为新配置文件:

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

Edit ktune.sh.编辑ktune.sh

Edit /etc/tune-profiles/virtual-guest-no-thp/ktune.sh and change the set_transparent_hugepages setting to the following:编辑/etc/tune-profiles/virtual-guest-no-thp/ktune.sh,并将set_transparent_hugepages设置更改为以下内容:

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. 创建一个新目录来保存自定义tuned配置文件。This example inherits from the existing virtual-guest profile, and uses virtual-guest-no-thp as the new profile:此示例继承了现有的virtual-guest配置文件,并使用virtual-guest-no-thp作为新配置文件:

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:创建并编辑/etc/tuned/virtual-guest-no-thp/tuned.conf,使其包含以下内容:

[main]
include=virtual-guest

[vm]
transparent_hugepages=never

This example inherits from the existing virtual-guest profile. 此示例继承自现有的virtual-guest配置文件。Select the profile most appropriate for your system.选择最适合您的系统的配置文件。

3

Enable the new profile.启用新配置文件。

Enable the new profile:启用新配置文件:

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