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.以下说明包括systemd和System V init初始化系统的示例。
Additionally, for RHEL and CentOS systems that use 此外,对于使用ktune and tuned performance profiles, you must create a custom tuned profile as well.ktune和tuned性能配置文件的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 要创建禁用THP的服务文件,请使用操作系统的内置初始化系统。最新版本的Linux通常使用systemd,它使用systemctl command. systemctl命令。Older versions of Linux use System V init, which uses the 旧版本的Linux使用System V init,它使用service command. For more information, see the documentation for your operating system.service命令。有关更多信息,请参阅操作系统的文档。
Use the initialization system for your operating system:使用操作系统的初始化系统:
systemd(systemctl)
Create the systemd unit file创建systemd单元文件
systemd unit fileCreate 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 Red Hat Enterprise Linux的某些版本,以及可能基于Red Hat的其他衍生版本,对启用THP的文件使用了不同的路径:enabled file:
/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文件。
Reload systemd unit files重新加载systemd单元文件
systemd unit filesTo 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-reloadStart 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
neverSystem V Init (service)
Create the init.d script创建init.d脚本
init.d scriptCreate 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
;;
esacRun 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
neverConfigure 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发行版运行以下命令:
| Command | |
|---|---|
| Ubuntu | |
| SUSE | |
|
Using tuned and ktune使用tuned和ktune
tuned and ktuneImportant
If you use 如果使用tuned or ktune, perform the steps in this section after creating the service file.tuned或ktune,请在创建服务文件后执行本节中的步骤。
tuned and ktune are kernel tuning utilities that can affect the Transparent Hugepages setting on your system. tuned和ktune是内核调优实用程序,可以影响系统上的透明拥抱设置。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系统上使用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配置文件作为基础,并使用虚拟客户no thp作为新配置文件:
sudo cp -r /etc/tune-profiles/virtual-guest /etc/tune-profiles/virtual-guest-no-thpRed Hat/CentOS 7 and 8
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-thpEdit tuned.conf.编辑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配置文件。选择最适合您系统的配置文件。