天天看點

LInux 标準大頁和透明大頁

閱讀目錄

Huge Pages

Transparent Huge Pages

使用Huge pages優點

 使用  Huge pages缺點

Transparent Huge pages存在的問題

如何關閉 Transparent Huge pages ?

Huge pages ( 标準大頁 ) 和  Transparent Huge pages( 透明大頁 )

在 Linux 中大頁分為兩種: Huge pages ( 标準大頁 ) 和  Transparent Huge pages( 透明大頁 ) 。

記憶體是以塊即頁的方式進行管理的,目前大部分系統預設的頁大小為 4096 bytes 即 4K 。 1MB 記憶體等于 256 頁; 1GB 記憶體等于 256000 頁。

CPU 擁有内置的記憶體管理單元,包含這些頁面的清單,每個頁面通過頁表條目引用。當記憶體越來越大的時候, CPU 需要管理這些記憶體頁的成本也就越高,這樣會對作業系統的性能産生影響。

回到頂部

Huge pages  是從 Linux Kernel 2.6 後被引入的,目的是通過使用大頁記憶體來取代傳統的 4kb 記憶體頁面, 以适應越來越大的系統記憶體,讓作業系統可以支援現代硬體架構的大頁面容量功能。

Huge pages  有兩種格式大小: 2MB  和  1GB , 2MB 頁塊大小适合用于 GB 大小的記憶體, 1GB 頁塊大小适合用于 TB 級别的記憶體; 2MB 是預設的頁大小。

Transparent Huge Pages  縮寫  THP ,這個是 RHEL 6 開始引入的一個功能,在 Linux6 上透明大頁是預設啟用的。

由于 Huge pages 很難手動管理,而且通常需要對代碼進行重大的更改才能有效的使用,是以 RHEL 6 開始引入了 Transparent Huge Pages ( THP ), THP 是一個抽象層,能夠自動建立、管理和使用傳統大頁。

THP 為系統管理者和開發人員減少了很多使用傳統大頁的複雜性 ,  因為 THP 的目标是改進性能 ,  是以其它開發人員  ( 來自社群和紅帽 )  已在各種系統、配置、應用程式和負載中對  THP  進行了測試和優化。這樣可讓  THP  的預設設定改進大多數系統配置性能。但是 ,  不建議對資料庫工作負載使用  THP 。

這兩者最大的差別在于 :  标準大頁管理是預配置設定的方式,而透明大頁管理則是動态配置設定的方式。

标準大頁的頁面大小

[root@localhost ~]# grep Hugepagesize /proc/meminfo

Hugepagesize: 2048 kB

注: THP  目前隻能映射異步記憶體區域,比如堆和棧空間

Oracle  官方是推薦我們使用 Huge pages 的,它擁有以下的好處:

Larger Page Size and Less # ofPages: Default page size is 4K whereas the HugeTLB size is 2048K. That meansthe system would need to handle 512 times less pages.

Reduced Page Table Walking:Since a HugePage covers greater contiguous virtual address range than a regularsized page, a probability of getting a TLB hit per TLB entry with HugePages arehigher than with regular pages. This reduces the number of times page tablesare walked to obtain physical address from a virtual address.

Less Overhead for MemoryOperations: On virtual memory systems (any modern OS) each memory operation isactually two abstract memory operations. With HugePages, since there are lessnumber of pages to work on, the possible bottleneck on page table access isclearly avoided.

Less Memory Usage: From theOracle Database perspective, with HugePages, the Linux kernel will use lessmemory to create pagetables to maintain virtual to physical mappings for SGAaddress range, in comparison to regular size pages. This makes more memory tobe available for process-private computations or PGA usage.

No Swapping: We must avoidswapping to happen on Linux OS at all Document 1295478.1. HugePages are notswappable (whereas regular pages are). Therefore there is no page replacementmechanism overhead. HugePages are universally regarded as pinned.

No 'kswapd' Operations: kswapdwill get very busy if there is a very large area to be paged (i.e. 13 millionpage table entries for 50GB memory) and will use an incredible amount of CPUresource. When HugePages are used, kswapd is not involved in managing them. Seealso Document 361670.1

當然使用  Huge pages  也會存在某些缺點:

首先開啟該功能需要進行額外設定,

第二, Huge pages  和 Oracle 11g新 特性 AMM ( Automatic Memory Management )是互相沖突的,但是 ASMM ( Automatic Shared Memory Management )仍然可以繼續使用。

Oracle 官方雖然推薦我們使用  Huge pages ,但是卻建議我們關閉 Transparent Huge pages ,因為透明大頁存在一些問題:

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

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

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

環境:CentOS Linux release 7.8.2003 (Core)

[root@localhost ~]# grep Huge /proc/meminfo

AnonHugePages: 7956480 kB

HugePages_Total: 0

HugePages_Free: 0

HugePages_Rsvd: 0

HugePages_Surp: 0

檢查目前的transparent_hugepage狀态(以下為開啟狀态)

透明大頁關閉方法有兩種,一種是通過指令關閉,一種通過修改grub開機引導檔案關閉:

1)手動臨時關閉

2)開機自動關閉

<code>[root@localhost ~]# vim /etc/rc.d/rc.local</code>

LInux 标準大頁和透明大頁
LInux 标準大頁和透明大頁

<code>授予執行權限</code>

編輯/etc/sysconfig/grub 檔案,在 GRUB_CMDLINE_LINUX 那一行後面追加 transparent_hugepage=never例如:

[root@localhost ~]# cat /etc/sysconfig/grub

LInux 标準大頁和透明大頁
LInux 标準大頁和透明大頁

再使用 grub2-mkconfig 生成grub.cfg配置檔案。

最後重新開機系統使配置生效。

繼續閱讀