开启大页详解

      什么时侯使用大页呢,当你主机的物理内存为64G,设SGA>=32G时,建议开启大页,步骤如下:

1、 关闭Oracle Database 11g中的AMM(Automatic Memory Management),即把两个参数MEMORY_TARGET / MEMORY_MAX_TARGET设为0

[html]  view plain  copy
  1. SQL > show parameter memory_max_target  
  2.   
  3. NAME                                 TYPE        VALUE  
  4. ------------------------------------ ----------- ------------------------------  
  5. memory_max_target                    big integer 0  
  6. SQL > show parameter memory_target  
  7.   
  8. NAME                                 TYPE        VALUE  
  9. ------------------------------------ ----------- ------------------------------  
  10. memory_target                        big integer 0  


2、参考metalink(文档 ID 401749.1)提供的脚本,计算hugepages的大小
[oracle@rac1 ~]$ vi /u01/app/oracle/product/11.2.0/rdbms/admin/hugepages_settings.sh

[html]  view plain  copy
  1. #!/bin/bash  
  2. #  
  3. # hugepages_settings.sh  
  4. #  
  5. # Linux bash script to compute values for the  
  6. # recommended HugePages/HugeTLB configuration  
  7. #  
  8. # Note: This script does calculation for all shared memory  
  9. # segments available when the script is run, no matter it  
  10. # is an Oracle RDBMS shared memory segment or not.  
  11. #  
  12. # This script is provided by Doc ID 401749.1 from My Oracle Support   
  13. # http://support.oracle.com  
  14.   
  15. # Welcome text  
  16. echo "  
  17. This script is provided by Doc ID 401749.1 from My Oracle Support   
  18. (http://support.oracle.com) where it is intended to compute values for   
  19. the recommended HugePages/HugeTLB configuration for the current shared   
  20. memory segments. Before proceeding with the execution please note following:  
  21.  * For ASM instance, it needs to configure ASMM instead of AMM.  
  22.  * The 'pga_aggregate_target' is outside the SGA and   
  23.    you should accommodate this while calculating SGA size.  
  24.  * In case you changes the DB SGA size,   
  25.    as the new SGA will not fit in the previous HugePages configuration,   
  26.    it had better disable the whole HugePages,   
  27.    start the DB with new SGA size and run the script again.  
  28. And make sure that:  
  29.  * Oracle Database instance(s) are up and running  
  30.  * Oracle Database 11g Automatic Memory Management (AMM) is not setup   
  31.    (See Doc ID 749851.1)  
  32.  * The shared memory segments can be listed by command:  
  33.      # ipcs -m  
  34.   
  35.   
  36. Press Enter to proceed..."  
  37.   
  38. read  
  39.   
  40. # Check for the kernel version  
  41. KERN=`uname -r | awk -F. '{ printf("%d.%d\n",$1,$2); }'`  
  42.   
  43. # Find out the HugePage size  
  44. HPG_SZ=`grep Hugepagesize /proc/meminfo | awk '{print $2}'`  
  45. if [ -z "$HPG_SZ" ];then  
  46.     echo "The hugepages may not be supported in the system where the script is being executed."  
  47.     exit 1  
  48. fi  
  49.   
  50. # Initialize the counter  
  51. NUM_PG=0  
  52.   
  53. # Cumulative number of pages required to handle the running shared memory segments  
  54. for SEG_BYTES in `ipcs -m | cut -c44-300 | awk '{print $1}' | grep "[0-9][0-9]*"`  
  55. do  
  56.     MIN_PG=`echo "$SEG_BYTES/($HPG_SZ*1024)" | bc -q`  
  57.     if [ $MIN_PG -gt 0 ]; then  
  58.         NUM_PG=`echo "$NUM_PG+$MIN_PG+1" | bc -q`  
  59.     fi  
  60. done  
  61.   
  62. RES_BYTES=`echo "$NUM_PG * $HPG_SZ * 1024" | bc -q`  
  63.   
  64. # An SGA less than 100MB does not make sense  
  65. # Bail out if that is the case  
  66. if [ $RES_BYTES -lt 100000000 ]; then  
  67.     echo "***********"  
  68.     echo "** ERROR **"  
  69.     echo "***********"  
  70.     echo "Sorry! There are not enough total of shared memory segments allocated for   
  71. HugePages configuration. HugePages can only be used for shared memory segments   
  72. that you can list by command:  
  73.   
  74.     # ipcs -m  
  75.   
  76. of a size that can match an Oracle Database SGA. Please make sure that:  
  77.  * Oracle Database instance is up and running   
  78.  * Oracle Database 11g Automatic Memory Management (AMM) is not configured"  
  79.     exit 1  
  80. fi  
  81.   
  82. # Finish with results  
  83. case $KERN in    '2.2') echo "Kernel version $KERN is not supported. Exiting." ;;  
  84.     '2.4') HUGETLB_POOL=`echo "$NUM_PG*$HPG_SZ/1024" | bc -q`;  
  85.            echo "Recommended setting: vm.hugetlb_pool = $HUGETLB_POOL" ;;  
  86.     '2.6') echo "Recommended setting: vm.nr_hugepages = $NUM_PG" ;;  
  87. esac  
  88.   
  89. # End  

3、对hugepages_settings.sh这个脚本授可执行的权限

[oracle@mydb admin]$  chmod +x hugepages_settings.sh


4、执行hugepages_settings.sh,计算hugepages的值为1028M

[oracle@mydb admin]$ ./hugepages_settings.sh

[html]  view plain  copy
  1. This script is provided by Doc ID 401749.1 from My Oracle Support   
  2. (http://support.oracle.com) where it is intended to compute values for   
  3. the recommended HugePages/HugeTLB configuration for the current shared   
  4. memory segments. Before proceeding with the execution please note following:  
  5.  * For ASM instance, it needs to configure ASMM instead of AMM.  
  6.  * The 'pga_aggregate_target' is outside the SGA and   
  7.    you should accommodate this while calculating SGA size.  
  8.  * In case you changes the DB SGA size,   
  9.    as the new SGA will not fit in the previous HugePages configuration,   
  10.    it had better disable the whole HugePages,   
  11.    start the DB with new SGA size and run the script again.  
  12. And make sure that:  
  13.  * Oracle Database instance(s) are up and running  
  14.  * Oracle Database 11g Automatic Memory Management (AMM) is not setup   
  15.    (See Doc ID 749851.1)  
  16.  * The shared memory segments can be listed by command:  
  17.      # ipcs -m  
  18.   
  19.   
  20. Press Enter to proceed...  
  21.   
  22. Recommended setting: vm.nr_hugepages = 1028  

      得出大页的大小为1028页(注:一页为2M,这个值不可改,1028*2M=2056M),实际上hugepages与参数sga_max_size有关,比sga_max_size的值稍微大一点点(比SGA_MAX_SIZE最少要多加一页,2M的页不要分配超过sga_max_size太多,会造成内存的浪费):

gyj@OCM> show parameter sga_max_size


NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
sga_max_size                         big integer 2G


5、设置hugepages,在内核参数中添加一行,vi /etc/sysctl.conf

vm.nr_hugepages = 1028


6、修改内核参数立即生效
[root@rac1 ~]# sysctl -p

[html]  view plain  copy
  1. net.ipv4.ip_forward = 0  
  2. net.ipv4.conf.default.rp_filter = 1  
  3. net.ipv4.conf.default.accept_source_route = 0  
  4. kernel.sysrq = 0  
  5. kernel.core_uses_pid = 1  
  6. net.ipv4.tcp_syncookies = 1  
  7. kernel.msgmnb = 65536  
  8. kernel.msgmax = 65536  
  9. kernel.shmmax = 68719476736  
  10. kernel.shmall = 4294967296  
  11. fs.file-max = 6815744  
  12. vm.nr_hugepages = 1028  

7、别忘记设定/etc/security/limits.conf文件,以K为单位,必须大于sga_max_size,这里设定为2056000

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

oracle          soft    memlock 2056000
oracle          hard    memlock 2056000


8、检查limits是否正确
[root@rac1 ~]# su - oracle
[oracle@rac1 ~]$ ulimit -l
2056000

9、重启数据库---注原来的orale用户的窗口退到root用户,重新su - oracle

[html]  view plain  copy
  1. SQL > exit  
  2. Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production  
  3. With the Partitioning, OLAP, Data Mining and Real Application Testing options  
  4. [oracle@rac1 ~]$ exit  
  5. logout  
  6.   
  7. You have new mail in /var/spool/mail/root  
  8. [root@rac1 ~]# su -  oracle  
  9. [oracle@rac1 ~]$ sqlplus / as sysdba  
  10.   
  11. SQL*Plus: Release 11.2.0.4.0 Production on Thu Dec 13 14:31:44 2015  
  12.   
  13. Copyright (c) 1982, 2011, Oracle.  All rights reserved.  
  14.   
  15.   
  16. Connected to:  
  17. Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production  
  18. With the Partitioning, OLAP, Data Mining and Real Application Testing options  
  19.   
  20. sys@OCM> shutdown immediate;  
  21. Database closed.  
  22. Database dismounted.  
  23. ORACLE instance shut down.  
  24. sys@OCM> startup  
  25. ORACLE instance started.  
  26.   
  27. Total System Global Area 2137886720 bytes  
  28. Fixed Size                  2230072 bytes  
  29. Variable Size            1409288392 bytes  
  30. Database Buffers          603979776 bytes  
  31. Redo Buffers              122388480 bytes  
  32. Database mounted.  
  33. Database opened.  

10、查看大页,已被使用

[oracle@mydb ~]$ watch -n1 'cat /proc/meminfo |grep -i HugePage'

[html]  view plain  copy
  1. Every 1.0s: cat /proc/meminfo |grep -i HugePage                                                 Thu Dec  13 15:44:23 2015  
  2.   
  3. HugePages_Total:  1028  
  4. HugePages_Free:    869  
  5. HugePages_Rsvd:    842  
  6. Hugepagesize:     2048 kB  

注:
HugePages_Total:  1028    ---总共1028页
HugePages_Free:    869    ---空闲548页,即当前大页被使用了1028-869=159页,即被用了159*2M=118M,小于sga_target。
HugePages_Rsvd:    842    ---操作系统承诺给Oracle预留842页,即842*2M=1684M(1684+118==SGA_MAX_SIZE)
Hugepagesize:     2048 kB --每页是2M,不可修改


使用了hugepage之后,SGA就默认pin在内存里了,那么就不用lock sga了。接下来我们研究一下参数:pre_page_sga,这个参数默认是false,我把它打开。

[html]  view plain  copy
  1. SQL > alter system set pre_page_sga=true scope=spfile;  
  2.   
  3. System altered.  
  4.   
  5. SQL > show parameter sga  
  6.   
  7. NAME                                 TYPE        VALUE  
  8. ------------------------------------ ----------- ------------------------------  
  9. lock_sga                             boolean     FALSE  
  10. pre_page_sga                         boolean     TRUE  
  11. sga_max_size                         big integer 2G  
  12. sga_target                           big integer 1G  

HugePages_Total:  1028    ---总共1028页
HugePages_Free:    548    ---空闲548页,即当前大页被使用了1028-548=480页,即被用了480*2M=960M,约等于sga_target,参数pre_page_sga起作用了。
HugePages_Rsvd:    521    ---操作系统承诺给Oracle预留521页,即521*2M=1042M(理解为sga_max_size-sga_target)
Hugepagesize:     2048 kB --每页是2M,不可修改

参考metalink:USE_LARGE_PAGES To Enable HugePages (文档 ID 1392497.1)

For 11.2.0.2 and further, the Oracle Database Server has added a new parameter that helps managing the hugepages for use by the database. 
The initialization parameter that was added is USE_LARGE_PAGES. 

USE_LARGE_PAGES parameter has these possible values: "true" (default), "only", "false".

1. The default value of "true" preserves the current behavior of trying to use hugepages if they are available on the OS. 

In 11.2.0.2 if there are not enough hugepages, only small pages will be used for SGA memory. This may lead to ORA-4030 errors due to the remaining hugepages going unused and more memory being used by the kernel for page tables. 

In 11.2.0.3 the behavior was changed such that Oracle will now allocate what it can of the SGA in hugepages and if it runs out, it will allocate the rest of the SGA using small pages. With this new behavior additional shared memory segments are an expected side effect. Part of the change is to ensure that each shared memory segment making up the SGA only contains sub-areas with an identical alignment requirement - hence the SGA will spread over more separate SHM segments. In this supported mixed page mode the database will exhaust the available hugepages, before switching to regular sized pages.
 
2. Setting it to "false" means do not use hugepages
 
3. A setting of "only" means do not start up the instance if hugepages cannot be used for the whole memory (to avoid an out-of-memory situation).


补充关于内存申请的OverCommit:

Linux下的OverCommit机制,主要是为了应对可能的异常的大量内存申请对OS本身造成冲击。
Linux有三种OverCommit机制,可以通过:/proc/sys/vm/overcommit_memory来配置,三种配置的具体含义:
0:启发式策略,后果比较严重的Overcommit将不能成功,而轻微的Overcommit将被允许。
1:永远允许Overcommit,这种策略适合那些不能承受内存分配失败的应用,比如某些科学计算应用。
2:永远禁止Overcommit,在这个情况下,系统所能分配的内存不会超过swap+RAM*系数(/proc/sys/vm /overcmmit_ratio,默认50%,你可以调整),如果这么多资源已经用光,那么后面任何尝试申请内存的行为都会返回错误,这通常意味着此时 没法运行任何新程序。

[html]  view plain  copy
  1. [root@rac1 ~]# cd /proc/sys/vm  
  2. [root@rac1 ~]# ls  
  3. block_dump                 flush_mmap_pages      min_free_kbytes     overcommit_memory         swappiness  
  4. dirty_background_ratio     hugetlb_shm_group     min_slab_ratio      overcommit_ratio          swap_token_timeout  
  5. dirty_expire_centisecs     laptop_mode           min_unmapped_ratio  pagecache                 vfs_cache_pressure  
  6. dirty_ratio                legacy_va_layout      mmap_min_addr       page-cluster              zone_reclaim_mode  
  7. dirty_writeback_centisecs  lowmem_reserve_ratio  nr_hugepages        panic_on_oom  
  8. drop_caches                max_map_count         nr_pdflush_threads  percpu_pagelist_fraction  

假设操作系统只有1000M内存,有个应用请求操作系统需要1200M内存,操作系统会承诺给1200M,即由OverCommit承诺,这时还没有真正分配空间。


转载:http://blog.csdn.net/guoyjoe/article/details/17138391http://blog.csdn.net/guoyjoe/article/details/17138391

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值