Linux關於透明大頁的使用與禁用介紹(推薦)

概述

隨著計算需求規模的不斷增大,應用程式對記憶體的需求也越來越大。為了實現虛擬記憶體管理機制,作業系統對記憶體實行分頁管理。自記憶體“分頁機制”提出之始,記憶體頁面的預設大小便被設定為 4096 位元組(4KB),雖然原則上記憶體頁面大小是可配置的,但絕大多數的作業系統實現中仍然採用預設的 4KB 頁面。 4KB 大小的頁面在“分頁機制”提出的時候是合理的,因為當時的記憶體大小不過幾十兆位元組,然而當物理記憶體容量增長到幾 G 甚至幾十 G 的時候,作業系統仍然以 4KB 大小為頁面的基本單位,是否依然合理呢?

在 Linux 作業系統上執行記憶體需求量較大的應用程式時,由於其採用的預設頁面大小為 4KB,因而將會產生較多 TLB Miss 和缺頁中斷,從而大大影響應用程式的效能。當作業系統以 2MB 甚至更大作為分頁的單位時,將會大大減少 TLB Miss 和缺頁中斷的數量,顯著提高應用程式的效能。這也正是 Linux 核心引入大頁面支援的直接原因。好處是很明顯的,假設應用程式需要 2MB 的記憶體,如果作業系統以 4KB 作為分頁的單位,則需要 512 個頁面,進而在 TLB 中需要 512 個表項,同時也需要 512 個頁表項,作業系統需要經歷至少 512 次 TLB Miss 和 512 次缺頁中斷才能將 2MB 應用程式空間全部對映到物理記憶體;然而,當作業系統採用 2MB 作為分頁的基本單位時,只需要一次 TLB Miss 和一次缺頁中斷,就可以為 2MB 的應用程式空間建立虛實對映,並在執行過程中無需再經歷 TLB Miss 和缺頁中斷(假設未發生 TLB 項替換和 Swap)。

為了能以最小的代價實現大頁面支援,Linux 作業系統採用了基於 hugetlbfs 特殊檔案系統 2M 位元組大頁面支援。這種採用特殊檔案系統形式支援大頁面的方式,使得應用程式可以根據需要靈活地選擇虛存頁面大小,而不會被強制使用 2MB 大頁面。

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系統。

[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系統

[root@getlnx06 ~]# cat /sys/kernel/mm/transparent_hugepage/enabled always madvise [never] [root@getlnx06 ~]#

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

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

[root@getlnx06 ~]# grep -i HugePages_Total /proc/meminfo HugePages_Total: 0

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

[root@getlnx06 ~]# cat /proc/sys/vm/nr_hugepages 0

禁用、啟用透明大頁功能

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

[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/sdadefault=0timeout=5splashimage=(hd0,0)/grub/splash。xpm。gzhiddenmenutitle 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。imgtransparent_hugepage=never

方法2:設定/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/localif test -f /sys/kernel/mm/redhat_transparent_hugepage/enabled; then echo never > /sys/kernel/mm/redhat_transparent_hugepage/enabledfi

使用上面的配置後必須重啟作業系統才能生效,你也可以執行下面命令不用重啟作業系統。

[root@getlnx06 ~]# echo never > /sys/kernel/mm/redhat_transparent_hugepage/enabled[root@getlnx06 ~]# cat /sys/kernel/mm/redhat_transparent_hugepage/enabledalways madvise [never][root@getlnx06 ~]#

小知識點:

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 ) 也會導致一些異常的效能問題

後面小編會分享更多運維乾貨,感興趣的朋友走一波關注哩~

Linux關於透明大頁的使用與禁用介紹(推薦)