引言
隨著計(jì)算需求規(guī)模的不斷增大,應(yīng)用程序?qū)?nèi)存的需求也越來(lái)越大。為了實(shí)現(xiàn)虛擬內(nèi)存管理機(jī)制,操作系統(tǒng)對(duì)內(nèi)存實(shí)行分頁(yè)管理。自內(nèi)存“分頁(yè)機(jī)制”提出之始,內(nèi)存頁(yè)面的默認(rèn)大小便被設(shè)置為 4096 字節(jié)(4KB),雖然原則上內(nèi)存頁(yè)面大小是可配置的,但絕大多數(shù)的操作系統(tǒng)實(shí)現(xiàn)中仍然采用默認(rèn)的 4KB 頁(yè)面。 4KB 大小的頁(yè)面在“分頁(yè)機(jī)制”提出的時(shí)候是合理的,因?yàn)楫?dāng)時(shí)的內(nèi)存大小不過(guò)幾十兆字節(jié),然而當(dāng)物理內(nèi)存容量增長(zhǎng)到幾 G 甚至幾十 G 的時(shí)候,操作系統(tǒng)仍然以 4KB 大小為頁(yè)面的基本單位,是否依然合理呢?
在 Linux 操作系統(tǒng)上運(yùn)行內(nèi)存需求量較大的應(yīng)用程序時(shí),由于其采用的默認(rèn)頁(yè)面大小為 4KB,因而將會(huì)產(chǎn)生較多 TLB Miss 和缺頁(yè)中斷,從而大大影響應(yīng)用程序的性能。當(dāng)操作系統(tǒng)以 2MB 甚至更大作為分頁(yè)的單位時(shí),將會(huì)大大減少 TLB Miss 和缺頁(yè)中斷的數(shù)量,顯著提高應(yīng)用程序的性能。這也正是 Linux 內(nèi)核引入大頁(yè)面支持的直接原因。好處是很明顯的,假設(shè)應(yīng)用程序需要 2MB 的內(nèi)存,如果操作系統(tǒng)以 4KB 作為分頁(yè)的單位,則需要 512 個(gè)頁(yè)面,進(jìn)而在 TLB 中需要 512 個(gè)表項(xiàng),同時(shí)也需要 512 個(gè)頁(yè)表項(xiàng),操作系統(tǒng)需要經(jīng)歷至少 512 次 TLB Miss 和 512 次缺頁(yè)中斷才能將 2MB 應(yīng)用程序空間全部映射到物理內(nèi)存;然而,當(dāng)操作系統(tǒng)采用 2MB 作為分頁(yè)的基本單位時(shí),只需要一次 TLB Miss 和一次缺頁(yè)中斷,就可以為 2MB 的應(yīng)用程序空間建立虛實(shí)映射,并在運(yùn)行過(guò)程中無(wú)需再經(jīng)歷 TLB Miss 和缺頁(yè)中斷(假設(shè)未發(fā)生 TLB 項(xiàng)替換和 Swap)。
為了能以最小的代價(jià)實(shí)現(xiàn)大頁(yè)面支持,Linux 操作系統(tǒng)采用了基于 hugetlbfs 特殊文件系統(tǒng) 2M 字節(jié)大頁(yè)面支持。這種采用特殊文件系統(tǒng)形式支持大頁(yè)面的方式,使得應(yīng)用程序可以根據(jù)需要靈活地選擇虛存頁(yè)面大小,而不會(huì)被強(qiáng)制使用 2MB 大頁(yè)面。
Redis在Linux中啟動(dòng)時(shí)通常會(huì)報(bào)“WARNING you have Transparent Huge Pages (THP) support enabled in your kernel
”的警告信息,意思是:你使用的是透明大頁(yè),可能導(dǎo)致redis延遲和內(nèi)存使用問(wèn)題。
關(guān)于透明大頁(yè),我們看看官方的介紹
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.
查看是否啟用透明大頁(yè)
1:命令cat /sys/kernel/mm/redhat_transparent_hugepage/enabled 該命令適用于Red Hat Enterprise Linux系統(tǒng)
[root@getlnx06 ~]# more /etc/issue
Red Hat Enterprise Linux Server release 6.6 (Santiago)
Kernel \r on an \m
[root@getlnx06 ~]# cat /sys/kernel/mm/redhat_transparent_hugepage/enabled
[always] madvise never
2:命令cat /sys/kernel/mm/transparent_hugepage/enabled 該命令適用于其它Linux系統(tǒng)
[root@getlnx06 ~]# cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
[root@getlnx06 ~]#
使用命令查看時(shí),如果輸出結(jié)果為[always]表示透明大頁(yè)啟用了。[never]表示透明大頁(yè)禁用、[madvise]表示
3:如何HugePages_Total返回0,也意味著透明大頁(yè)禁用了
[root@getlnx06 ~]# grep -i HugePages_Total /proc/meminfo
HugePages_Total: 0
4:cat /proc/sys/vm/nr_hugepages返回0也意味著透明大頁(yè)禁用了。
[root@getlnx06 ~]# cat /proc/sys/vm/nr_hugepages
0
禁用、啟用透明大頁(yè)功能
方法1:設(shè)置/etc/grub.conf文件,在系統(tǒng)啟動(dòng)是禁用。
[root@getlnx06 ~]# 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:設(shè)置/etc/rc.local文件
[root@getlnx06 ~]# 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
使用上面的配置后必須重啟操作系統(tǒng)才能生效,你也可以運(yùn)行下面命令不用重啟操作系統(tǒng)。
[root@getlnx06 ~]# echo never > /sys/kernel/mm/redhat_transparent_hugepage/enabled
[root@getlnx06 ~]# cat /sys/kernel/mm/redhat_transparent_hugepage/enabled
always madvise [never]
[root@getlnx06 ~]#
小知識(shí)點(diǎn):
1:從RedHat 6, OEL 6, SLES 11 and UEK2 kernels 開(kāi)始,系統(tǒng)缺省會(huì)啟用 Transparent HugePages :用來(lái)提高內(nèi)存管理的性能透明大頁(yè)(Transparent HugePages )和之前版本中的大頁(yè)功能上類(lèi)似。主要的區(qū)別是:Transparent HugePages 可以實(shí)時(shí)配置,不需要重啟才能生效配置;
2:Transparent Huge Pages在32位的RHEL 6中是不支持的。
3: ORACLE官方不建議我們使用RedHat 6, OEL 6, SLES 11 and UEK2 kernels 時(shí)的開(kāi)啟透明大頁(yè)(Transparent HugePages ), 因?yàn)橥该鞔箜?yè)(Transparent HugePages ) 存在一些問(wèn)題:
- 在RAC環(huán)境下 透明大頁(yè)(Transparent HugePages )會(huì)導(dǎo)致異常節(jié)點(diǎn)重啟,和性能問(wèn)題
- 在單機(jī)環(huán)境中,透明大頁(yè)(Transparent HugePages ) 也會(huì)導(dǎo)致一些異常的性能問(wèn)題
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接