Database Manual / Self-Managed Deployments / Administration / Performance / TCMalloc Performance

Disable Transparent Hugepages (THP) for Self-Managed Deployments禁用自我管理部署的透明拥抱(THP)

Important

Upgraded TCMalloc in MongoDB 8.0在MongoDB 8.0中升级了TCMalloc

Starting in MongoDB 8.0, MongoDB uses an upgraded version of TCMalloc that improves performance with Transparent Hugepages enabled. If you are using MongoDB 8.0 or later, see Enable Transparent Hugepages (THP).从MongoDB 8.0开始,MongoDB使用TCMalloc的升级版本,该版本在启用透明Hugepages的情况下提高了性能。如果您使用的是MongoDB 8.0或更高版本,请参阅启用透明拥抱(THP)

Transparent Hugepages (THP) is a Linux memory management system that reduces the overhead of Translation Lookaside Buffer (TLB) lookups. THP achieves this by combining small pages and making them appear as larger memory pages to the application.透明缓冲区(THP)是一个Linux内存管理系统,可以减少翻译查找缓冲区(TLB)查找的开销。THP通过组合小页面并使其在应用程序中显示为更大的内存页面来实现这一点。

When running MongoDB 7.0 or earlier on Linux, THP should be disabled for best performance. In earlier versions of MongoDB, database workloads often experience decreased performance with THP enabled because they often use non-contiguous, memory access patterns.在Linux上运行MongoDB 7.0或更早版本时,应禁用THP以获得最佳性能。在早期版本的MongoDB中,启用THP后,数据库工作负载的性能通常会降低,因为它们经常使用非连续的内存访问模式。

Note

THP operates at a system level, so it applies to all processes under the system. You cannot enable or disable THP per-process level.THP在系统级别运行,因此它适用于系统下的所有流程。您无法按进程级别启用或禁用THP。

To ensure that THP is disabled before mongod starts, create a service file for your operating system that disables THP at boot. 为了确保在mongod启动之前禁用THP,请为操作系统创建一个服务文件,在启动时禁用THP。The following instructions include examples for both the systemd and the System V init initialization systems.以下说明包括systemdSystem V init初始化系统的示例。

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

Create a Service File创建服务文件

To create a service file that disables THP, use the built-in initialization system for your operating system. Recent versions of Linux typically use systemd, which uses the systemctl command. 要创建禁用THP的服务文件,请使用操作系统的内置初始化系统。最新版本的Linux通常使用systemd,它使用systemctl命令。Older versions of Linux use System V init, which uses the service command. For more information, see the documentation for your operating system.旧版本的Linux使用System V init,它使用service命令。有关更多信息,请参阅操作系统的文档。

Use the initialization system for your operating system:使用操作系统的初始化系统:

systemd(systemctl)
1

Create the systemd unit file创建systemd单元文件

Create the following file and save it at /etc/systemd/system/disable-transparent-huge-pages.service:创建以下文件并将其保存在/etc/systemd/system/disable-transparent-huge-pages.service

[Unit]
Description=Disable Transparent Hugepages (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 && echo never | tee /sys/kernel/mm/transparent_hugepage/defrag > /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

Verify which path is in use on your system and update the disable-transparent-huge-pages.service file accordingly.验证系统上正在使用哪个路径,并相应地更新disable-transparent-huge-pages.service文件。

2

Reload systemd unit files重新加载systemd单元文件

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

sudo systemctl daemon-reload
3

Start the service启动服务

Run:运行:

sudo systemctl start disable-transparent-huge-pages

To verify that the relevant THP settings have changed, run the following command:要验证相关THP设置是否已更改,请运行以下命令:

cat /sys/kernel/mm/transparent_hugepage/enabled && cat /sys/kernel/mm/transparent_hugepage/defrag

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 && cat /sys/kernel/mm/redhat_transparent_hugepage/defrag

The output should resemble the following:输出应类似于以下内容:

never
never
4

Configure your operating system to run it on boot配置操作系统以在启动时运行它

To ensure that this setting is applied each time the operating system starts, run the following command:为确保每次启动操作系统时都应用此设置,请运行以下命令:

sudo systemctl enable disable-transparent-huge-pages
5

(Optional) Customize tuned or ktune profile可选)自定义tuned或ktune配置文件

If you use tuned or ktune profiles on RHEL/ CentOS, you must also create a custom tuned profile.如果在RHEL/CentOS上使用tunedktune配置文件,则还必须创建自定义tuned配置文件。

System V Init (service)
1

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

Create the following file and save it 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 Hugepages
# Description: Disable Linux Transparent Hugepages, 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 /sys/kernel/mm/transparent_hugepage/enabled > /dev/null && echo 'never' | tee /sys/kernel/mm/transparent_hugepage/defrag > /dev/null

unset thp_path
;;
esac
2

Make the script executable

Run:运行:

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

Run the script运行脚本

Run:运行:

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

To verify that the relevant THP settings have changed, run the following command:要验证相关THP设置是否已更改,请运行以下命令:

cat /sys/kernel/mm/transparent_hugepage/enabled && cat /sys/kernel/mm/transparent_hugepage/defrag

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 && cat /sys/kernel/mm/redhat_transparent_hugepage/defrag

The output should resemble the following:输出应类似于以下内容:

never
never
4

Configure your operating system to run it on boot配置操作系统以在启动时运行它

To ensure that this setting is applied each time the operating sytem starts, 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 derivatives红帽、CentOS、亚马逊Linux及其衍生产品
sudo chkconfig --add disable-transparent-hugepages
5

(Optional) Customize tuned or ktune profile可选)自定义tuned或ktune配置文件

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

Using tuned and ktune使用tunedktune

Important

If you use tuned or ktune, perform the steps in this section after creating the service file.如果使用tunedktune,请在创建服务文件后执行本节中的步骤。

tuned and ktune are kernel tuning utilities that can affect the Transparent Hugepages setting on your system. tunedktune是内核调优实用程序,可以影响系统上的透明拥抱设置。If you use tuned or ktune on your RHEL or CentOS system while running mongod, you must create a custom tuned profile to ensure that THP stays disabled.如果在运行mongod时在RHEL或CentOS系统上使用tunedktune,则必须创建自定义调优配置文件以确保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配置文件作为基础,并使用虚拟客户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. This example inherits from the existing virtual-guest profile, and uses virtual-guest-no-thp as the new profile:创建一个新目录来保存自定义tuned的配置文件。此示例继承了现有的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:

[main]
include=virtual-guest

[vm]
transparent_hugepages=never

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

3

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

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

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