Linux关于透明大页的使用与禁用介绍

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 ⼤页⾯。
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
禁⽤、启⽤透明⼤页功能
⽅法1:设置/f⽂件,在系统启动是禁⽤。
[root@getlnx06 ~]# vi /f
# f 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/
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-LogVo    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
使⽤上⾯的配置后必须重启操作系统才能⽣效,你也可以运⾏下⾯命令不⽤重启操作系统。
[root@getlnx06 ~]# echo never > /sys/kernel/mm/redhat_transparent_hugepage/enabled
[root@getlnx06 ~]# cat /sys/kernel/mm/redhat_transparent_hugepage/enabled
三星w539
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中是不⽀持的。
紫海胆
3: ORACLE官⽅不建议我们使⽤RedHat 6, OEL 6, SLES 11 and UEK2 kernels 时的开启透明⼤页(Transparent HugePages ),因为透明⼤页(Transparent HugePages )存在⼀些问题:
在RAC环境下透明⼤页(Transparent HugePages )会导致异常节点重启,和性能问题
在单机环境中,透明⼤页(Transparent HugePages )也会导致⼀些异常的性能问题
总结
以上就是这篇⽂章的全部内容了,希望本⽂的内容对⼤家的学习或者⼯作具有⼀定的参考学习价值,谢谢⼤家对的⽀持。如果你想了解更多相关内容请查看下⾯相关链接

本文发布于:2024-09-22 12:43:03,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/231423.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:内存   透明   需要   程序   重启   查看   性能   单位
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议