Oracle 数据库 HugePages 配置详解:提升性能的关键步骤
Oracle 数据库 HugePages 配置详解
- 一、为什么需要配置 HugePages?
- 二、配置 HugePages 的步骤
- 1. 禁用 AMM(自动内存管理)
- 2. 配置 memlock 限制
- 3. 计算 HugePages 数量
- 4. 修改内核参数
- 5. 禁用透明大页(Transparent HugePages)
- 6. 设置 USE_LARGE_PAGES 参数
- 7. 重启服务器并验证
- 三、验证 HugePages 配置
- 1. 操作系统验证
- 2. 日志验证
- 四、常见问题及解决
- 五、总结
在 Oracle 数据库管理中,HugePages(大页) 是优化大内存场景下性能的关键配置。通过减少页表条目和内存碎片,HugePages 能显著提升 SGA(系统全局区)的访问效率。本文结合 Oracle 官方文档,详细讲解 HugePages 的配置步骤、验证方法及常见问题解决方案。
一、为什么需要配置 HugePages?
-
性能优化
- 默认内存页大小为 4KB,当 SGA 较大时(如超过 8GB),页表条目数量激增,导致 CPU 频繁查询页表,增加开销。
- HugePages(通常 2MB)减少页表条目,降低 TLB(Translation Lookaside Buffer)未命中率。
-
避免内存交换(Swap)
- HugePages 锁定在物理内存中,不会被交换到磁盘,避免因内存交换导致的性能下降。
-
减少内核开销
- 大页减少
kswapd
进程的 CPU 消耗,避免内存碎片化。
- 大页减少
二、配置 HugePages 的步骤
1. 禁用 AMM(自动内存管理)
HugePages 与 AMM(MEMORY_TARGET
)不兼容,需先禁用:
show parameter memory
-- 修改参数文件
ALTER SYSTEM SET MEMORY_TARGET=0 SCOPE=SPFILE;
ALTER SYSTEM SET MEMORY_MAX_TARGET=0 SCOPE=SPFILE;
-- 重启数据库
SHUTDOWN IMMEDIATE;
STARTUP;
2. 配置 memlock 限制
编辑 /etc/security/limits.conf
,设置 Oracle 用户的内存锁定限制:
# 添加以下内容
* soft memlock unlimited
* hard memlock unlimited
验证设置生效:
su - oracle
ulimit -l # 应输出 unlimited
3. 计算 HugePages 数量
使用 Oracle 提供的脚本 hugepages_settings.sh
((Doc ID 401749.1):
#!/bin/bash
#
# hugepages_settings.sh
#
# Linux bash script to compute values for the
# recommended HugePages/HugeTLB configuration
# on Oracle Linux
#
# Note: This script does calculation for all shared memory
# segments available when the script is run, no matter it
# is an Oracle RDBMS shared memory segment or not.
#
# This script is provided by Doc ID 401749.1 from My Oracle Support
# http://support.oracle.com
# Welcome text
echo "
This script is provided by Doc ID 401749.1 from My Oracle Support
(http://support.oracle.com) where it is intended to compute values for
the recommended HugePages/HugeTLB configuration for the current shared
memory segments on Oracle Linux. Before proceeding with the execution please note following:
* For ASM instance, it needs to configure ASMM instead of AMM.
* The 'pga_aggregate_target' is outside the SGA and
you should accommodate this while calculating the overall size.
* In case you changes the DB SGA size,
as the new SGA will not fit in the previous HugePages configuration,
it had better disable the whole HugePages,
start the DB with new SGA size and run the script again.
And make sure that:
* Oracle Database instance(s) are up and running
* Oracle Database Automatic Memory Management (AMM) is not setup
(See Doc ID 749851.1)
* The shared memory segments can be listed by command:
# ipcs -m
Press Enter to proceed..."
read
# 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`
# An SGA less than 100MB does not make sense
# Bail out if that is the case
if [ $RES_BYTES -lt 100000000 ]; then
echo "***********"
echo "** ERROR **"
echo "***********"
echo "Sorry! There are not enough total of shared memory segments allocated for
HugePages configuration. HugePages can only be used for shared memory segments
that you can list by command:
# ipcs -m
of a size that can match an Oracle Database SGA. Please make sure that:
* Oracle Database instance is up and running
* Oracle Database Automatic Memory Management (AMM) is not configured"
exit 1
fi
# Finish with results
echo "Recommended setting: vm.nr_hugepages = $NUM_PG";
# End
chmod 775 hugepages_settings.sh
./hugepages_settings.sh
# 输出示例:Recommended setting: vm.nr_hugepages = 1496
或手动计算:
vm.nr_hugepages >= SGA_Target/Hugepagesize(2M) + 冗余(建议 10%)
4. 修改内核参数
编辑 /etc/sysctl.conf
,添加:
vm.nr_hugepages = 1496
vm.hugetlb_shm_group = dba # Oracle 用户组
应用配置:
sysctl -p
5. 禁用透明大页(Transparent HugePages)
透明大页可能导致性能抖动,需禁用:
# 修改 GRUB 配置
sed -i 's/GRUB_CMDLINE_LINUX="/GRUB_CMDLINE_LINUX="transparent_hugepage=never /g' /etc/default/grub
# 判断操作系统是否使用UEFI方式启动
ls -ld /sys/firmware/efi
# 如果上面查询目录存在使用如下方式重建grub配置文件(针对EFI方式)
grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg
# 否则使用如下方式重建grub配置文件(针对BIOS方式)
grub2-mkconfig -o /boot/grub2/grub.cfg
# 重启生效
reboot
# 验证禁用状态
cat /sys/kernel/mm/transparent_hugepage/enabled # 输出应为 [never]
6. 设置 USE_LARGE_PAGES 参数
Oracle 11.2.0.2+ 引入此参数,控制 HugePages 使用策略:
- TRUE:尝试使用 HugePages,若不足则回退到普通页(默认)。
- ONLY:强制仅使用 HugePages,不足则数据库无法启动。
- FALSE:禁用 HugePages。
SYS@db11g> show parameter use_large_pages
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
use_large_pages string TRUE
7. 重启服务器并验证
reboot
三、验证 HugePages 配置
1. 操作系统验证
grep HugePages /proc/meminfo
# 期望输出:
HugePages_Total: 1496 # 配置的总大页数
HugePages_Free: 100 # 剩余大页(数据库启动后应大部分被占用)
HugePages_Rsvd: 100 # 预留大页(若 PRE_PAGE_SGA=false)
2. 日志验证
检查 alert.log
,确认以下内容:
Mon Mar 17 14:29:20 2025
Starting ORACLE instance (normal)
************************ Large Pages Information *******************
Per process system memlock (soft) limit = UNLIMITED
`Total Shared Global Region in Large Pages = 898 MB (100%) `
Large Pages used by this instance: 449 (898 MB)
Large Pages unused system wide = 1611 (3222 MB)
Large Pages configured system wide = 2060 (4120 MB)
Large Page size = 2048 KB
********************************************************************
四、常见问题及解决
问题 | 原因 | 解决方案 |
---|---|---|
ORA-27137: 无法分配大页 | HugePages 不足或 memlock 限制过小 | 增加 vm.nr_hugepages 并检查 memlock |
数据库性能未提升 | HugePages 未生效,SGA 使用普通页 | 检查 USE_LARGE_PAGES 是否设置为 ONLY |
HugePages_Total = HugePages_Free | 数据库未使用 HugePages | 确认 AMM 已禁用,数据库实例已启动 |
透明大页未禁用 | 未更新 GRUB 或未重启 | 执行 grubby 命令并重启服务器 |
五、总结
- 核心配置:禁用 AMM → 设置
memlock
→ 计算 HugePages → 修改sysctl.conf
→ 禁用透明大页 → 设置USE_LARGE_PAGES=TRUE
。 - 验证关键:通过
/proc/meminfo
、v$sgainfo
和alert.log
确认配置生效。 - 性能收益:减少页表开销、避免内存交换,显著提升大内存数据库性能。
通过以上步骤,可确保 Oracle 数据库高效利用 HugePages,适用于 OLTP、数据仓库等高并发场景。配置时需注意版本差异(如 11g 与 12c+ 的默认行为),并定期复查内存配置变化。