介绍
对于Oracle数据库来说,如果内存较大,通常都会建议开启HugePages来提高性能。如果不清楚HugePages是什么的话,可以简单看下这篇文章(使用hugepage优化内存)。
本脚本使用了Oracle官方HugePage建议值的算法,自动计算出合适的HugePage值,并将其加入到sysctl.conf文件中。
说明
脚本无需配置任何参数,可直接复制使用,执行后会自动备份/etc/sysctl.conf和/etc/security/limits.conf文件到/tmp目录下,然后根据Oracle提供的算法计算合适的HugePage值,并将其写入到/etc/sysctl.conf文件中。
注意,在执行脚本前,Oracle数据库必须启动,并且内存管理模式必须是ASMM模式。执行脚本后,必须手动重启一下服务器。
脚本
#!/bin/bash
#Date:2022-06-14
#Author:Created by b
#Version:1.0
#Describe:Oracle HugePage automatic configuration optimization
TODAY=$(date +%Y%m%d)
function Back_Limit_Config(){
cp /etc/security/limits.conf /tmp/limits.conf.${TODAY}
echo "limits.conf file is backed up to the /TMP directory"
}
function Edit_Limit_Config(){
local memlock=`su - oracle -c "ulimit -a | grep 'max locked memory'" | awk '{print $6}'`
if [ ${memlock} = "unlimited" ]; then
echo "Max Locked Memory has been changed to Unlimited and will not be changed"
else
echo "oracle soft memlock unlimited" >> /etc/security/limits.conf
echo "oracle hard memlock unlimited" >> /etc/security/limits.conf
echo "Max Locked Memory parameter has been changed to Unlimited"
fi
}
function Back_Sysctl_Config(){
cp /etc/sysctl.conf /tmp/sysctl.conf.${TODAY}
echo "sysctl.conf file is backed up to the /TMP directory"
}
function Calculate_Hugepage(){
# Check for the kernel version
KERN=`uname -r | awk -F. '{ printf("%d.%d\n",$1,$2); }'`
# Find out the HugePage size
HPG_SZ=`grep Hugepagesize /proc/meminfo | awk '{print $2}'`
if [ -z "$HPG_SZ" ];then
echo "The hugepages may not be supported in the system where the script is being executed."
exit 1
fi
# Initialize the counter
NUM_PG=0
# Cumulative number of pages required to handle the running shared memory segments
for SEG_BYTES in `ipcs -m | cut -c44-300 | awk '{print $1}' | grep "[0-9][0-9]*"`
do
MIN_PG=`echo "$SEG_BYTES/($HPG_SZ*1024)" | bc -q`
if [ $MIN_PG -gt 0 ]; then
NUM_PG=`echo "$NUM_PG+$MIN_PG+1" | bc -q`
fi
done
RES_BYTES=`echo "$NUM_PG * $HPG_SZ * 1024" | bc -q`
if [ $RES_BYTES -lt 100000000 ]; then
echo "Sorry! There are not enough total of shared memory segments allocated for HugePages configuration."
exit 1
fi
# Configuration sysctl.conf
if [ -z `cat /etc/sysctl.conf | grep -E -o "^vm.nr_hugepages"` ]; then
echo "vm.nr_hugepages = $NUM_PG" >> /etc/sysctl.conf
sysctl -p > /dev/null
echo "Hugepage is not configured in the sysctl.conf file, and parameters have been added. parameter values : "$NUM_PG
else
sed -i '/^vm.nr_hugepages/d' /etc/sysctl.conf
echo "vm.nr_hugepages = $NUM_PG" >> /etc/sysctl.conf
sysctl -p > /dev/null
echo "The hugepage parameter already exists in the sysctl.conf file. Replace the parameter values : "$NUM_PG
fi
}
function main(){
Back_Limit_Config
Edit_Limit_Config
Back_Sysctl_Config
Calculate_Hugepage
}
main
使用
1、创建脚本
新建脚本,将上述内容添加到脚本中,脚本名称自己随意命名。
[root@rac-1 ~]# vim hugepage_automatic.sh
新增内容,然后保存并退出。
2、授权
新增后,授权脚本可执行权限。
[root@rac-1 ~]# chmod +x hugepage_automatic.sh
3、执行脚本
[root@rac-1 ~]# bash hugepage_automatic.sh
4、重启服务器生效
配置好后,重启服务器,让大页生效。
[root@rac-1 ~]# reboot
至此,HugePages就启用了。
评论区