Linux开启hugepages

Hugepages概述

HugePages是集成到Linux2.6内核版本中的一项功能,启用HugePages可以让操作系统支持大于默认值(4K)的内存页面。使用大内存页可以减少访问页表所需的系统资源量来提高系统性能。HugePages支持32位或者64位,大小从2M到256M不等。对于Oracle数据库,使用HugePages可以减少操作系统对内存页状态的维护,并增加Translation Lookaside Buffer (TLB) 的命中率。

HugePages限制

  • 自动内存管理(AMM)和HugePages不兼容,要使用HugePages需要禁用AMM
  • 如果您在32位环境中使用VLM,则无法将HugePages用于数据库缓冲区缓存
  • 除非系统管理员通过修改可用页面数量或修改池大小来更改HugePages配置,否则HugePages不会在系统启动后进行分配或释放。如果系统启动期间所需的空间未在内存中保留,则HugePages分配失败。

前提条件

  • Oracle实例处于open状态
  • Oracle非AMM自动内存管理
  • 安装bc依赖包

获取建议值

创建脚本hugepages_settings.sh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/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 SGA 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 11g 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 11g Automatic Memory Management (AMM) is not configured"
    exit 1
fi

#Finish with results
case $KERN in
    '2.4') HUGETLB_POOL=`echo "$NUM_PG*$HPG_SZ/1024" | bc -q`;
           echo "Recommended setting: vm.hugetlb_pool = $HUGETLB_POOL" ;;
    '2.6') echo "Recommended setting: vm.nr_hugepages = $NUM_PG" ;;
    '3.8') echo "Recommended setting: vm.nr_hugepages = $NUM_PG" ;;
    '3.10') echo "Recommended setting: vm.nr_hugepages = $NUM_PG" ;;
    '4.1') echo "Recommended setting: vm.nr_hugepages = $NUM_PG" ;;
    '4.14') echo "Recommended setting: vm.nr_hugepages = $NUM_PG" ;;
    *) echo "Kernel version $KERN is not supported by this script (yet). Exiting." ;;
esac

#End

执行脚本

1
2
$ ./hugepages_settings.sh
Recommended setting: vm.nr_hugepages = 5124

设置hugepages

limit配置(/etc/security/limits.conf)

1
2
oracle   soft  memlock  10493952
oracle   hard  memlock  10493952

Tips: 该值需要大于nr.hugepages和hugepagessize相乘,或者从alert日志中获取建议值

内核配置(/etc/sysctl.conf)

1
echo "vm.hugepages = 5124" >> /etc/sysctl.conf

重启后查看分配的大页情况

1
cat /proc/meminfo
comments powered by Disqus