禁用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]

相關文章
相關標籤/搜索