天天看點

linux透明大頁記憶體,Linux 關于透明大頁的介紹

Redis在Linux中啟動時通常會報“WARNING you have Transparent Huge Pages (THP) support enabled in your kernel”的警告資訊,意思是:你使用的是透明大頁,可能導緻redis延遲和記憶體使用問題。

關于透明大頁,我們看看官方的介紹

Transparent Huge Pages (THP) are enabled by default in RHEL 6 for all applications. The kernel attempts to allocate hugepages whenever possible and any Linux process will receive 2MB pages if the mmap region is 2MB naturally aligned. The main kernel address space itself is mapped with hugepages, reducing TLB pressure from kernel code. For general information on Hugepages, see: What are Huge Pages and what are the advantages of using them?

The kernel will always attempt to satisfy a memory allocation using hugepages. If no hugepages are available (due to non availability of physically continuous memory for example) the kernel will fall back to the regular 4KB pages. THP are also swappable (unlike hugetlbfs). This is achieved by breaking the huge page to smaller 4KB pages, which are then swapped out normally.

But to use hugepages effectively, the kernel must find physically continuous areas of memory big enough to satisfy the request, and also properly aligned. For this, a khugepaged kernel thread has been added. This thread will occasionally attempt to substitute smaller pages being used currently with a hugepage allocation, thus maximizing THP usage.

In userland, no modifications to the applications are necessary (hence transparent). But there are ways to optimize its use. For applications that want to use hugepages, use of posix_memalign() can also help ensure that large allocations are aligned to huge page (2MB) boundaries.

Also, THP is only enabled for anonymous memory regions. There are plans to add support for tmpfs and page cache. THP tunables are found in the /sys tree under /sys/kernel/mm/redhat_transparent_hugepage.

檢視是否啟用透明大頁

1:指令cat /sys/kernel/mm/redhat_transparent_hugepage/enabled 該指令适用于Red Hat Enterprise Linux系統

[[email protected] ~]# more /etc/issue

Red Hat Enterprise Linux Server release 6.6 (Santiago)

Kernel \r on an \m

[[email protected] ~]# cat /sys/kernel/mm/redhat_transparent_hugepage/enabled

[always] madvise never

2:指令cat /sys/kernel/mm/transparent_hugepage/enabled 該指令适用于其它Linux系統

[r[email protected] ~]# cat /sys/kernel/mm/transparent_hugepage/enabled

always madvise [never]

[[email protected] ~]#

使用指令檢視時,如果輸出結果為[always]表示透明大頁啟用了。[never]表示透明大頁禁用、[madvise]表示

3:如何HugePages_Total傳回0,也意味着透明大頁禁用了

[[email protected] ~]# grep -i HugePages_Total /proc/meminfo

HugePages_Total: 0

4:cat /proc/sys/vm/nr_hugepages傳回0也意味着透明大頁禁用了。

[[email protected] ~]# cat /proc/sys/vm/nr_hugepages

禁用、啟用透明大頁功能

方法1:設定/etc/grub.conf檔案,在系統啟動是禁用。

[[email protected] ~]# vi /etc/grub.conf

# grub.conf generated by anaconda

#

# Note that you do not have to rerun grub after making changes to this file

# NOTICE: You have a /boot partition. This means that

# all kernel and initrd paths are relative to /boot/, eg.

# root (hd0,0)

# kernel /vmlinuz-version ro root=/dev/mapper/VolGroup--LogVol0-LogVol01

# initrd /initrd-[generic-]version.img

#boot=/dev/sda

default=0

timeout=5

splashimage=(hd0,0)/grub/splash.xpm.gz

hiddenmenu

title Red Hat Enterprise Linux 6 (2.6.32-504.el6.x86_64)

root (hd0,0)

kernel /vmlinuz-2.6.32-504.el6.x86_64 ro root=/dev/mapper/VolGroup--LogVol0-LogVol01 rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup-LogVol0/LogVol01 rd_LVM_LV=VolGroup-LogVol0/LogVol00 KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet

initrd /initramfs-2.6.32-504.el6.x86_64.img

transparent_hugepage=never

方法2:設定/etc/rc.local檔案

[[email protected] ~]# vi /etc/rc.local

#!/bin/sh

#

# This script will be executed *after* all the other init scripts.

# You can put your own initialization stuff in here if you don't

# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

if test -f /sys/kernel/mm/redhat_transparent_hugepage/enabled; then

echo never > /sys/kernel/mm/redhat_transparent_hugepage/enabled

fi

使用上面的配置後必須重新開機作業系統才能生效,你也可以運作下面指令不用重新開機作業系統。

[[email protected] ~]# echo never > /sys/kernel/mm/redhat_transparent_hugepage/enabled

[[email protected] ~]# cat /sys/kernel/mm/redhat_transparent_hugepage/enabled

always madvise [never]

[[email protected] ~]#

小知識點:

1:從RedHat 6, OEL 6, SLES 11 and UEK2 kernels 開始,系統預設會啟用 Transparent HugePages :用來提高記憶體管理的性能透明大頁(Transparent HugePages )和之前版本中的大頁功能上類似。主要的差別是:Transparent HugePages 可以實時配置,不需要重新開機才能生效配置;

2:Transparent Huge Pages在32位的RHEL 6中是不支援的。

3: ORACLE官方不建議我們使用RedHat 6, OEL 6, SLES 11 and UEK2 kernels 時的開啟透明大頁(Transparent HugePages ), 因為透明大頁(Transparent HugePages ) 存在一些問題:

在RAC環境下 透明大頁(Transparent HugePages )會導緻異常節點重新開機,和性能問題

在單機環境中,透明大頁(Transparent HugePages ) 也會導緻一些異常的性能問題