禁用Transparent Huge Pages

全文翻译于MongoDB官方手册 ,已经尽力保持原版。但能力有限……html

  • 开机脚本mongodb

  • 使用tunded与ktune数据库

  • 测试配置是否生效bash

注意:
本页描述了如何在Red Hat Enterprise Linux 与CentOS 六、7上禁用Transparent Huge Page,如需在其余的系统上执行禁用THP,请参阅相应文档。ide

Transparent Huge Pages (THP)是一种Linux内存管理机制,减小了Translation Lookaside Buffer (TLB)使用larger memory pages时的寻址开销。
可是数据库系统一般在使用THP的时候性能低下,由于它们倾向于稀疏而非连续的内存访问模式。你应该再Linux主机上禁用THP以确保MongoDB具有最佳性能。工具

开机脚本

重要:
若是你使用了 tuned 或者 ktune (例如,若是你使用的是Red Hat 或者 CentOS 6及更高版本,你必须>附加另外设置以确保THP不会被从新启用,参见 使用tuned与ktune性能

1. 建立开机脚本

建立此文件并写入以下内容 /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' > ${thp_path}/enabled
    echo 'never' > ${thp_path}/defrag

    re='^[0-1]+$'
    if [[ $(cat ${thp_path}/khugepaged/defrag) =~ $re ]]
    then
      # RHEL 7
      echo 0  > ${thp_path}/khugepaged/defrag
    else
      # RHEL 6
      echo 'no' > ${thp_path}/khugepaged/defrag
    fi

    unset re
    unset thp_path
    ;;
esac

2. 赋予可运行权限:

sudo chmod 755 /etc/init.d/disable-transparent-hugepagesui

3.配置此脚本开机启动:

发行版 与 命令spa


Ubuntu 与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 sudo chkconfig --add disable-transparent-hugepages

4. 覆盖tuned与ktune设置

若是你使用了 tuned 或者 ktune (例如,若是你使用的是Red Hat 或者 CentOS 6及更高版本,你必须马上配置以保护上述设置。

使用 tuned 或者 ktune

若是你使用了tuned 或者 ktune,除了安装开机脚本外还必须执行此步骤

tunedktune是动态内核调整工具,能够在Redhat或CentOS上禁用THP。在tuned或者ktune中禁用THP,你须要建立一个新的配置文件去设置THP的状态为never

Redhat与CentOS6

1.建立新的配置文件

从相关目录下复制默认配置文件并重命名,本例中,咱们复制 default 并命名为np-top:

sudo cp -r /etc/tune-profiles/default /etc/tune-profiles/no-thp

2.编辑ktune.sh

编辑 /etc/tune-profiles/no-thp/ktune.sh 增长以下一行:

set_transparent_hugepages never

在此文件的start()块,return 0以前。

3.使此文件可用

sudo tuned-adm profile no-thp

Red Hat /Cent OS 7

1.建立新文件

建立一个新的tuned配置文件目录:

sudo mkdir /etc/tuned/no-thp

2.编辑tuned.conf文件

建立并编译/etc/tuned/no-thp/tuned.conf,写入以下内容:

[main]
include=virtual-guest

[vm]
transparent_hugepages=never

3.使能新文件:

sudo tuned-adm profile no-thp

测试你的配置

使用以下命令检查THP状态:

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

在Red Hat Enterprise Linux或者CentOS或者其余基于Red hat Linux的发行版,你可能须要使用以下命令:

cat /sys/kernel/mm/redhat_transparent_hugepage/enabled
cat /sys/kernel/mm/redhat_transparent_hugepage/defrag

输出结果应该是

always madvise [never]

相关文章
相关标签/搜索