红联Linux门户
Linux帮助

Linux下Hugepages的配置

发布时间:2014-12-03 09:49:55来源:linux网站作者:wyzxg

Linux虽然没有aix,hp unix那么强悍,但Linux也是非常优秀的,为了提升Linux的性能,它采用了很多io,memory的调度机制,Linux使用内存的方式是采用vm的方式,即Linux把物理内存和swap共同虚拟成内存来对外提供,有时用户看似使用内存,可实际上是使用磁盘,那如何避免使用swap磁盘空间呢?

Linux管理内存的单位是页(pages),一般情况下是4k的page,当我们使用的大内存时(>8G),管理这么大的内存就会给系统造成很大的负担,再加上频繁的pagein/pageout,会成为系统的瓶颈。

1.hugepage介绍
2.实践配置


1.hugepage介绍
hugepage是在Linux2.6内核被引入的,主要提供4k的page和比较大的page的选择当我们访问内存时,首先访问”page table“,然后Linux在通过“page table”的mapping来访问真实物理内存(ram+swap)。为了提升性能,Linux在cpu中申请固定大小的buffer,被称为TLB,TLB中保存有“page table”的部分内容,这也遵循了,让数据尽可能的靠近cpu原则。在TLB中通过hugetlb来指向hugepage。这些被分配的hugepage作为内存文件系统hugetlbfs(类似tmpfs)提供给进程使用。

hugepage特点
Linux系统启动,hugepage就被分配并保留,不会pagein/pageout,除非人为干预,如改变hugepage的配置等;
根据Linux内核的版本和HW的架构,hugepage的大小从2M到256M不等。因为采用大page,所以也减少TLB
和page table的管理压力

为什么使用hugepage

对于大内存(>8G),hugepage对于提高在Linux上的Oracle性能是非常有帮助的
1)Larger Page Size and Less of Pages:减少了HugeTLB 的工作量
2)No Page Table Lookups:因为hugepage是不swappable的,所有就没有page table lookups。
3)No Swapping: 在Linux下,hugepage是不支持swapping
4)No 'kswapd' Operations:在Linux下进程“kswapd”是管理swap的,如果是大内存,那pages的数量就非常大,那“kswapd”就会被频繁的调用,从而会影响性能。


2.实践配置

A. Oracle10g配置

平台:
hw:x86_64
os:RHEL5.4
kernel: 2.6.18-164.el5
db:oracle10.2

1). 用root用户配置文件“/etc/security/limits.conf”,添加memlock,他的大小略小于总内存,例如:64GB,配置如下:

[root@skatedb ~]# vi /etc/security/limits.conf

##skate add
*   soft   memlock    60397977
*   hard   memlock    60397977

##skate add

2).用oracle重新登录,检查memlock的设置是否生效
[oracle@skatedb ~]$ ulimit -l
60397977

3).确定所有的oracle数据库实例都启动,包括asm实例,然后用如下脚本计算参数“vm.nr_hugepages"的大小

------------------hugepages_settings.sh----------------------------

#!/bin/bash
#
# hugepages_settings.sh
#
# Linux bash script to compute values for the
# recommended HugePages/HugeTLB configuration
#
# 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. Before proceeding with the execution please 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}'`

# Initialize the counter
NUM_PG=0

# Cumulative number of pages required to handle the running shared memory segments
for SEG_BYTES in `ipcs -m | awk '{print $5}' | 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" ;;
*) echo "Unrecognized kernel version $KERN. Exiting." ;;
esac

# End
-----------------------hugepages_settings.sh-----------------------

也可以手工计算nr_hugepages>=SGA_Target/Hugepagesize

然后把nr_hugepages追加写入文件“/etc/sysctl.conf”,假如这里计算的nr_hugepages=15000

[root@skatedb ~]# vi /etc/sysctl.conf

##skate add
vm.nr_hugepages=15000

##skate add

使其修改生效

[root@skatedb ~]# sysctl -p

4).停止oracle数据库,reboot服务器

5).检查hugepage是否生效

[root@skatedb ~]# grep HugePages /proc/meminfo
HugePages_Total: 15000
HugePages_Free:   4779
HugePages_Rsvd:   4676

如果HugePages_Total=HugePages_Free,说明HugePages配置没起作用。

B. oracle11g配置

平台:
hw:x86_64
os:RHEL5 (Taroon)
kernel: 2.6.18-194.el5
db:oracle 11.2.0.2.0

oracle11g的新特性是通过启用参数MEMORY_TARGET/MEMORY_MAX_TARGET,启用AMM的oracle11g的所有SGA内存的分配方式都是通过在/dev/shm下创建文件,而已这种方式分配的sga是不启用hugepage的,AMM和hugepage是不兼容的,因为/dev/shm是tmpfs文件系统,而tmpfs文件系统是不支持AMM动态调整sga大小的,所以要想使用hugepage的特性就要关闭AMM特性。两个如何取舍就看你了。

停止oracle11g的AMM
直接把MEMORY_MAX_TARGET和MEMORY_TARGET设置为‘0’,就抱如下的错误

ORA-00843: Parameter not taking MEMORY_MAX_TARGET into account
ORA-00849: SGA_TARGET 35433480192 cannot be set to more than MEMORY_MAX_TARGET 0.
ORA-01078: failure in processing system parameters

如果只设置MEMORY_TARGET=0,hugepage是不起作用的。所以采用不设置这两个参数,就可以关闭oracle11g的AMM了。创建pfile.ora文件,然后在文件里删除两个参数"MEMORY_TARGET/MEMORY_MAX_TARGET"。就是因为这个关闭AMM的地方测试了半天,才通过。

停止了oracle11g的AMM后,在按着oracle10g的配置方法就可以启动hugepage了。