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. 然而,启用THP时,数据库工作负载的性能通常很差,因为它们往往具有稀疏而不是连续的内存访问模式。When running MongoDB on Linux, THP should be disabled for best performance.在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 init
系统的说明。
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/CentOS系统,您还必须创建自定义的调优配置文件。
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倾向于使用systemctl
command), while older versions of Linux tend to use System V init (which uses the service
command). systemd
(使用systemctl
命令),而旧版本的Linux则倾向于使用System V init
(使用service命令)。Refer to the documentation for your operating system for more information.有关更多信息,请参阅操作系统的文档。
Use the initialization system appropriate for your platform:使用适合您平台的初始化系统:
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
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:enabled
文件使用不同的路径:
/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 只要在systemd
unit file, MongoDB is unaffected by the defrag setting. systemd
单元文件中禁用了THP本身,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
单元文件中添加以下附加行,将分片整理设置为从不:
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 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
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
Prior to version 4.2, MongoDB also checks the THP defrag setting and presents a startup warning if defrag is enabled. 在4.2版之前,MongoDB还会检查THPdefrag设置,并在启用分片整理时显示启动警告。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
脚本中添加以下行,在unset thp_path
语句之前,将defrag设置为never
:
echo 'never' | tee ${thp_path}/defrag > /dev/null
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
To ensure that this setting is applied each time your system boots, run the following command for your Linux distribution:要确保每次系统启动时都应用此设置,请为Linux发行版运行以下命令:
Command | |
---|---|
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 |
tuned
and ktune
tuned
和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/CentOS系统上使用tuned
/ktune
,则必须创建自定义的tuned配置文件,以确保THP保持禁用状态。
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
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
tuned.conf
.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.选择最适合您的系统的配置文件。