透明大頁


透明大頁介紹

Transparent Huge Pages的一些官方介紹資料:

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]表示(只在MADV_HUGEPAGE標志的VMA中使用THP

 

3:如何HugePages_Total返回0,也意味着標准大頁禁用了(注意傳統/標准大頁和透明大頁的區別)

   透明大頁(THP)管理和標准/傳統大頁(HP)管理都是操作系統為了減少頁表轉換消耗的資源而發布的新特性,雖然ORACLE建議利用大頁機制來提高數據庫的性能,但是ORACLE卻同時建議關閉透明大頁管理。這二者的區別在於大頁的分配機制,標准大頁管理是預分配的方式,而透明大頁管理則是動態分配的方式。

[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/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文件

[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

 

 

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

You must reboot your system for the setting to take effect, or run the following two echo lines to proceed with the install without rebooting:

[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 ~]# 

 

 

小知識點:

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

 

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

Transparent Huge Pages are not available on the 32-bit version of RHEL 6.

 

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

        1.在RAC環境下 透明大頁(Transparent HugePages )會導致異常節點重啟,和性能問題;

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

Transparent HugePages memory is enabled by default with Red Hat Enterprise Linux 6, SUSE Linux Enterprise Server 11, and Oracle Linux 6 with earlier releases of Oracle Linux Unbreakable Enterprise Kernel 2 (UEK2) kernels. Transparent HugePages memory is disabled in later releases of Oracle Linux UEK2 kernels.Transparent HugePages can cause memory allocation delays during runtime. To avoid performance issues, Oracle recommends that you disable Transparent HugePages on all Oracle Database servers. Oracle recommends that you instead use standard HugePages for enhanced performance.Transparent HugePages memory differs from standard HugePages memory because the kernel khugepaged thread allocates memory dynamically during runtime. Standard HugePages memory is pre-allocated at startup, and does not change during runtime.

Starting with RedHat 6, OEL 6, SLES 11 and UEK2 kernels, Transparent HugePages are implemented and enabled (default) in an attempt to improve the memory management. Transparent HugePages are similar to the HugePages that have been available in previous Linux releases. The main difference is that the Transparent HugePages are set up dynamically at run time by the khugepaged thread in kernel while the regular HugePages had to be preallocated at the boot up time. Because Transparent HugePages are known to cause unexpected node reboots and performance problems with RAC, Oracle strongly advises to disable the use of Transparent HugePages. In addition, Transparent Hugepages may cause problems even in a single-instance database environment with unexpected performance problems or delays. As such, Oracle recommends disabling Transparent HugePages on all Database servers running Oracle.

 

4:安裝Vertica Analytic Database時也必須關閉透明大頁功能。

 

參考資料:

https://access.redhat.com/solutions/46111

http://www.soso.io/article/65143.html

https://developers.redhat.com/blog/2014/03/10/examining-huge-pages-or-transparent-huge-pages-performance/

 https://blog.nelhage.com/post/transparent-hugepages

Check THP usage per process

Run the following command to monitor which processes are using THP:

# grep -e AnonHugePages  /proc/*/smaps | awk  '{ if($2>4) print $0} ' |  awk -F "/"  '{print $0; system("ps -fp " $3)} '


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 

https://fangpeishi.com/transparent-hungepages_zh.html -》

禁用透明大頁

tl;dr(長話短說)

“透明大頁(Transparent Hugepages)”是一個Linux 內核特性,它通過提高處理器的內存映射硬件的使用效率(譯注:降低TLB Miss 和page fault,提高TLB 的命中率,這部分基礎知識可以翻下操作系統書)來獲取更好的性能。在絕大多數Linux 發行版中它是被默認啟用的(“enabled=always”)。

透明大頁能讓一些應用程序的性能提高一點點(最好的情況大約是10%,一般在0~3%),但是它會造成很明顯的性能問題(參考:1.mongodb,2.oracle, 3.splunk), 甚至會造成嚴重的內存泄漏(參考:1.digitalocean,2.golang/go).

為了避免這些問題,你應該將運行的服務器設置成enabled=madvise

echo madvise | sudo tee /sys/kernel/mm/transparent_hugepage/enabled

並在服務器的內核命令行上設置transparent_hugepage=madvise(例如在/etc/default/grub 中設置)。

這樣修改之后,應用程序能通過透明大頁提升性能,也能避免上述問題(譯注:設置成madvise,應用程序通過設置MADV_HUGEPAGE標志就能分配到大頁,不需要的程序則不受影響)。

繼續閱讀了解更多細節。

什么是透明大頁?

什么是大頁?

數十年來,處理器和操作系統之間通過使用虛擬內存,在應用程序可見的內存空間(“虛擬內存空間(virtual address space)”)和底層的物理內存之間設置了間接層。間接層不僅保護了應用程序互相不受影響,還有許多強大的特性。

非常多的 x86 處理器都是通過一個叫頁表(page table)的方案實現虛擬內存,這個方案會在內存中存一個非常大的映射表(實際上一個深度不同的樹結構,不過也可以看作是一個稀疏表)。傳統上,x86 處理器中一份頁表條目對應一份4KB 的內存“頁”。

雖然頁表都是存在內存里,但是處理器會緩存一部分頁表條目到處理器的寄存器上,它被稱為TLB 。查看我筆記本上的cpuid(1)(譯注:Linux tool to dump x86 CPUID information about the CPU(s))發現最低等級的TLB 只包含64個條目,每個條目對應一份4KB 的數據頁。現在是2017年,64*4KB 只有四分之一兆字節,遠小於目前使用的大多數應用程序的工作內存。這種大小不匹配的情況意味着占用大量內存的應用程序會周期性的遇到TLB 不命中的情況,從而需要花費很高的代價訪問主內存,只為得到具體的內存地址(譯注:TLB Miss 之后需要訪問內存中的頁表,從而得到具體內存地址)。

為了改進TLB 的效率,x86 及其他處理器長期以來都支持創建“大頁(huge pages)”,大頁的頁表條目能映射一大段的物理內存地址。根據操作系統的配置不同,大多數最近的芯片能夠映射2MB,4MB,甚至1GB 的內存頁。使用大頁意味着TLB 存着更多的數據,對某些特定的任務來說效率更高。

什么是透明大頁?

存在各種頁表管理方式,這意味着操作系統需要決定如何映射地址空間和物理內存。由於應用程序的內存管理接口(例如mmap(2))一直都是基於最小的 4KB 頁,所以內核映射數據必須以4KB 為單位。最簡單和最靈活的(就已支持的內存布局而言)方案是只采用4KB 的頁,應用程序映射內存無法使用大頁。長期以來,這是內核最通用的內存管理策略。

對於需要大量內存並對性能敏感的應用程序(例如某些特定數據庫或者科學計算程序),內核引入hugetlbfs 特性,該特性允許系統管理員通過配置讓特定的應用程序使用大頁。

透明大頁(簡稱“THP”),正如其名,旨在自動為應用程序提供大頁支持,不需要特殊配置(譯注:透明大頁的透明,類似透明代理的透明)。透明大頁通過在后台掃描(使用khugepaged 內核線程)內存映射,嘗試找到或者創建(通過移動相鄰的內存)總共2MB 的連續4KB 映射,用一個大頁來替換這一段內存映射。

有什么問題?

透明大頁運行良好時,特定的測試場景下,可以帶來大約10% 的性能提升。然而,它也會造成至少兩種非常嚴重的故障:

內存泄漏

THP 傾向於創建2MB 的內存映射。然而,這樣做太貪心,即使必要的情況下,也不願意把它們拆分回去。如果一個應用程序映射了一大段內存但是只訪問前面幾個字節,傳統上只會消耗一個4KB 的物理內存頁。THP 開啟的情況下,khugepaged會將4KB 頁擴張到2MB,內存占用量增大512倍(這份Bug 報告 中的例子更糟糕,甚至超過512 倍!)。

這種情況不是假設;Go 語言的GC 就有一個明確的解決方法,Digital Ocean 也記錄了它們是如何處理Redis,THP 和jemalloc遇到的問題。

(譯注:據說3.10 內核透明零頁有泄漏問題,內核只釋放2MB 中第一個4KB 的頁面,剩余的頁面泄漏)

卡頓和高CPU 使用率

應用程序都是分配相對靜態的內存,穩定的狀態下,khugepaged 的工作量是最小的。但是如果存在頻繁映射內存的情況,或者存在生命周期很短的進程,khugepaged 會進行大量的拆分/合並內存區域的工作,毫無意義,存活時間很短。這會引起很高的CPU 使用率,以及較長的卡頓,因為內核被迫得先把2MB 的頁拆分成4KB 的頁,才能執行原本在單頁上效率很高的操作。

因為這些原因,啟用了THP 之后,好幾個應用程序都觀察到30% 的性能下降,甚至更糟。

現在怎么辦?

THP 作者們事先意識到了透明大頁可能有潛在的問題(盡管如今看來,他們低估了問題的嚴重性),所以他們選擇通過 /sys/kernel/mm/transparent_hugepage/enabled 系統文件配置透明大頁。

更重要的是,他們為透明大頁實現了一種可選擇的模式。將/sys/kernel/mm/transparent_hugepage/enabled 設置為madvisekhugepaged默認情況下不會處理內存,除非應用程序使用madvise 系統調用,給特定范圍的內存進行THP 處理。

由於在大多數情況下,只有少數特定的應用程序能通過透明大頁顯著提升性能,所以這是一個兩全其美的選項。這些少數的應用程序可以選擇使用madvise,其余的應用程序不受影響。

所以,我建議每個用戶都把透明大頁配置成madvise,如文章開頭tl;dr所說的。同時我也希望說服主流的發行版默認禁用透明大頁,讓更多的系統管理員和開發者避免踩這些坑。

 

 

 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM