Disable Transparent Huge Pages禁用透明大页面 (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. 然而,启用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.下面提供了systemdSystem 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.此外,对于使用ktunetuned性能配置文件的RHEL/CentOS系统,您还必须创建自定义的调优配置文件。

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). 最新版本的Linux倾向于使用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:使用适合您平台的初始化系统:

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的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文件。

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. 只要在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 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.自定义调谐/调谐配置文件(如果适用)。

If you are using tuned or ktune on RHEL/ CentOS, you must now also create a custom tuned profile.如果您在RHEL/CentOS上使用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还会检查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
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.自定义调谐/调谐配置文件(如果适用)。

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

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/CentOS系统上使用tuned/ktune,则必须创建自定义的tuned配置文件,以确保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红帽/CentOS 7和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.编辑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
←  Database Profiler OutputUNIX ulimit Settings →