全文翻譯於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性能
建立此文件並寫入以下內容 /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
sudo chmod 755 /etc/init.d/disable-transparent-hugepages
ui
發行版 與 命令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
若是你使用了 tuned 或者 ktune (例如,若是你使用的是Red Hat 或者 CentOS 6及更高版本,你必須馬上配置以保護上述設置。
若是你使用了tuned 或者 ktune,除了安裝開機腳本外還必須執行此步驟
tuned與ktune是動態內核調整工具,能夠在Redhat或CentOS上禁用THP。在tuned或者ktune中禁用THP,你須要建立一個新的配置文件去設置THP的狀態爲never
從相關目錄下複製默認配置文件並重命名,本例中,咱們複製 default 並命名爲np-top:
sudo cp -r /etc/tune-profiles/default /etc/tune-profiles/no-thp
編輯 /etc/tune-profiles/no-thp/ktune.sh
增長以下一行:
set_transparent_hugepages never
在此文件的start()塊,return 0以前。
sudo tuned-adm profile no-thp
建立一個新的tuned配置文件目錄:
sudo mkdir /etc/tuned/no-thp
建立並編譯/etc/tuned/no-thp/tuned.conf
,寫入以下內容:
[main] include=virtual-guest [vm] transparent_hugepages=never
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]