Linux 关于Transparent Hugepages的介绍

本文详细介绍了Linux系统中的透明大页(THP)功能,包括其默认启用状态、工作原理、性能优势及如何检查和调整配置。文章还探讨了THP与标准大页的区别,以及Oracle和Vertica数据库为何推荐禁用THP。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Linux 关于Transparent Hugepages的介绍

2015-07-23 16:54 by 潇湘隐者, 30497 阅读, 1 评论, 收藏编辑

透明大页介绍

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

clip_image001

 

方法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

clip_image002

 

使用上面的配置后必须重启操作系统才能生效,你也可以运行下面命令不用重启操作系统。

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

<think>好的,我现在需要帮助用户在Linux系统版本9中永久关闭透明大页(transparent_hugepage)功能。根据用户提供的引用内容,我需要先确认现有的解决方案是否适用,并考虑版本差异。 首先,用户提到引用[1]中的方法是通过修改GRUB配置,在`GRUB_CMDLINE_LINUX=`参数后添加`transparent_hugepage=never`,然后更新GRUB。这种方法在CentOS 7.9上有效,但用户现在用的是版本9,可能是RHEL 9或其衍生版如CentOS Stream 9。我需要确认GRUB配置方法在较新版本中是否依然适用。 接下来,引用[2]提到了检查透明大页状态的命令`cat /sys/kernel/mm/transparent_hugepage/enabled`,这在新版本中应该仍然有效。用户可能需要先检查当前状态,再验证配置是否生效。 另外,引用[3]提到了在CentOS 7.9修改各种配置文件,但用户需要的是在版本9中操作。虽然系统版本不同,但GRUB配置文件的位置通常不变,可能在`/etc/default/grub`。不过,需要注意的是,RHEL 9及其衍生版可能默认使用GRUB2,配置步骤类似,但更新GRUB的命令可能不同,例如使用`grub2-mkconfig`。 可能的步骤: 1. 编辑`/etc/default/grub`文件,在`GRUB_CMDLINE_LINUX`行添加参数。 2. 生成新的GRUB配置文件。 3. 重启系统使配置生效。 4. 验证透明大页是否已关闭。 此外,是否有其他方法?比如通过systemd服务或内核参数文件,但GRUB方法更通用。需要确认在版本9中是否有变化,例如是否支持其他配置文件。 需要提醒用户备份原配置文件,避免配置错误导致系统无法启动。另外,检查是否有其他依赖或服务会影响透明大页的设置,比如某些内存管理工具。 最后,生成相关问题需要考虑用户可能遇到的后续问题,如验证步骤、其他禁用方法、临时禁用与永久禁用的区别,以及透明大页对系统性能的影响。</think>### 逐步说明 在Linux系统(版本9)中永久关闭透明大页(Transparent HugePages, THP)功能,可通过以下步骤实现: 1. **修改GRUB配置文件** 打开终端,使用root权限编辑GRUB配置文件`/etc/default/grub`: ```bash sudo vi /etc/default/grub ``` 找到`GRUB_CMDLINE_LINUX`参数,在其末尾添加`transparent_hugepage=never`,例如: ``` GRUB_CMDLINE_LINUX="...原有参数... transparent_hugepage=never" ``` 保存并退出[^1]。 2. **重新生成GRUB配置** 运行以下命令更新GRUB配置(适用于基于RHEL/CentOS 9的系统): ```bash sudo grub2-mkconfig -o /boot/grub2/grub.cfg ``` 对于UEFI系统,可能需要调整输出路径为`/boot/efi/EFI/redhat/grub.cfg`(具体路径根据发行版略有不同)。 3. **重启系统** ```bash sudo reboot ``` 4. **验证是否生效** 重启后执行以下命令检查透明大页状态: ```bash cat /sys/kernel/mm/transparent_hugepage/enabled ``` 若输出为`always madvise [never]`,表示已成功禁用[^2]。 --- ### 其他注意事项 - **临时禁用**:若需临时禁用(重启后失效),可执行: ```bash echo never | sudo tee /sys/kernel/mm/transparent_hugepage/enabled ``` - **内核参数优先级**:通过GRUB设置的参数优先级高于运行时修改,适合永久生效[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值