Disable Transparent Huge Pages (THP)禁用透明大页面(THP)
On this page本页内容
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.下面提供了systemd和System V初始化系统的说明。
Additionally, for RHEL / CentOS systems that make use of 此外,对于使用ktune
and tuned
performance profiles, you must create a custom tuned
profile as well.ktune
和tuned
性能配置文件的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 最新版本的Linux倾向于使用systemd(使用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.systemctl
命令),而较旧版本的Linux则倾向于使用System V init(使用service
命令)。有关详细信息,请参阅操作系统的文档。
Use the initialization system appropriate for your platform:使用适合您的平台的初始化系统:
Create the systemd
unit file.创建systemd
单位文件。
systemd
unit file.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
Some versions of Red Hat Enterprise Linux, and potentially other Red Hat-based derivatives, use a different path for the THP 某些版本的Red Hat Enterprise Linux,以及其他可能基于Red Hat的衍生产品,对启用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.disable-transparent-huge-pages.service
文件。
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 只要THP本身在systemd
unit file, MongoDB is unaffected by the defrag setting. 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 如果在Red Hat或类似系统上,defrag
file might be different. defrag
文件的路径可能不同。See the note above for more details, and and update the 有关更多详细信息,请参阅上面的注释,并相应地更新disable-transparent-huge-pages.service
file accordingly.disable-transparent-huge-pages.service
文件。
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 通过运行以下命令验证THP是否已成功设置为[never]
by running the following command:[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
Create the init.d
script.创建init.d
脚本。
init.d
script.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
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
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 通过运行以下命令验证THP是否已成功设置为[never]
by running the following command:[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
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发行版运行以下命令:
Ubuntu and Debian | sudo update-rc.d disable-transparent-hugepages defaults |
SUSE | sudo insserv /etc/init.d/disable-transparent-hugepages |
sudo chkconfig --add disable-transparent-hugepages |
Using tuned
and ktune
使用tuned
和ktune
tuned
and ktune
If using 如果使用tuned
or ktune
, you must also perform the steps in this section after creating the service file above.tuned
或ktune
,您还必须在创建上面的服务文件后执行本节中的步骤。
tuned
and ktune
are dynamic kernel tuning tools that can affect the transparent huge pages setting on your system. tuned
和ktune
是动态内核调优工具,可以影响系统上的透明大页面设置。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
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
Red Hat/CentOS 7 and 8
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
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.选择最适合您的系统的配置文件。