mongodb 运行时禁用 Transparent Huge Pages

Transparent Huge Pages 简称 THP,本来是linux为提高系统性能而做的努力,结果反而成了性能的隐患。mongodb的官方文档明确说明要禁止 linux 的 THP。

检查

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

均应该输出如下
always madvise [never]

临时设置

生产环境发现这个问题后,需要先临时设置,然后再永久设置

#echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled
#echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag

永久设置

第一种方法

# vi /etc/rc.local
echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled
echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag

第二种方法

# vi /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
chmod 755 /etc/init.d/disable-transparent-hugepages
chkconfig --add disable-transparent-hugepages

参考:
https://docs.mongodb.com/manual/tutorial/transparent-huge-pages/
https://docs.mongodb.com/v3.2/tutorial/transparent-huge-pages/index.html

你可能感兴趣的:(thp,nosql-mongodb)