Linux transparent huge pages

How to use, monitor, and disable transparent hugepages in Red Hat Enterprise Linux 6 and 7?

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/transparent_hugepage in RHEL 7 and later RHEL 6 releases. The directory for earlier RHEL 6 releases is /sys/kernel/mm/redhat_transparent_hugepage . The first location will be used further in this document.

The values for /sys/kernel/mm/transparent_hugepage/enabled can be one of the following:

always - always use THP never - disable THP 

khugepaged will be automatically started when transparent_hugepage/enabled is set to «always» or «madvise», and it’ll be automatically shutdown if it’s set to «never». The transparent_hugepage/defrag parameter takes the same values and it controls whether the kernel should make aggressive use of memory compaction to make more hugepages available.

Читайте также:  Проверить запущен ли сервис linux

Check system-wide THP usage

Run the following command to check system-wide THP usage:

# grep AnonHugePages /proc/meminfo AnonHugePages: 632832 kB 

Note: Red Hat Enterprise Linux 6.2 or later publishes additional THP monitoring via /proc/vmstat :

# egrep 'trans|thp' /proc/vmstat nr_anon_transparent_hugepages 2018 thp_fault_alloc 7302 thp_fault_fallback 0 thp_collapse_alloc 401 thp_collapse_alloc_failed 0 thp_split 21 

Check THP usage per process

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

 # awk '/AnonHugePages/ < if($2>4)>' /proc/*/smaps /proc/7519/smaps:AnonHugePages: 305152 kB UID PID PPID C STIME TTY TIME CMD qemu 7519 1 1 08:53 ? 00:00:48 /usr/bin/qemu-system-x86_64 -machine accel=kvm -name rhel7 -S -machine pc-i440fx-1.6,accel=kvm,usb=of /proc/7610/smaps:AnonHugePages: 491520 kB UID PID PPID C STIME TTY TIME CMD qemu 7610 1 2 08:53 ? 00:01:30 /usr/bin/qemu-system-x86_64 -machine accel=kvm -name util6vm -S -machine pc-i440fx-1.6,accel=kvm,usb= /proc/7788/smaps:AnonHugePages: 389120 kB UID PID PPID C STIME TTY TIME CMD qemu 7788 1 1 08:54 ? 00:00:55 /usr/bin/qemu-system-x86_64 -machine accel=kvm -name rhel64eus -S -machine pc-i440fx-1.6,accel=kvm,us 

To disable THP at boot time

Append the following to the kernel command line in grub.conf :

transparent_hugepage=never 

Note: Certain ktune and/or tuned profiles specify to enable THP when they are applied. If the transparent_hugepage=never parameter is set at boot time, but THP does not appear to be disabled after the system is fully booted. Refer to the following article:

To disable THP at run time

Run the following commands to disable THP on-the-fly:

# echo never > /sys/kernel/mm/transparent_hugepage/enabled # echo never > /sys/kernel/mm/transparent_hugepage/defrag 
  • NOTE: Running the above commands will stop only creation and usage of the new THP. The THP which were created and used at the moment the above commands were run would not be disassembled into the regular memory pages. To get rid of THP completely the system should be rebooted with THP disabled at boot time.
  • NOTE: Some third party application install scripts check value of above files and complain even if THP is disabled at boot time using transparent_hugepage=never , this is due to the fact when THP is disabled at boot time, the value of /sys/kernel/mm/transparent_hugepage/defrag will not be changed, however this is expected and system will never go in THP defragmentation code path when it is disabled at boot and THP defrag need not to be disabled separately.

How to tell if Explicit HugePages is enabled or disabled

There can be two types of HugePages in the system: Explicit Huge Pages which are allocated explicitly by vm.nr_hugepages sysctl parameter and Transparent Huge Pages which are allocated automatically by the kernel. See below on how to tell if Explicit HugePages is enabled or disabled.

    Explicit HugePages DISABLED:

      If the value of HugePages_Total is «0» it means HugePages is disabled on the system.
    # grep -i HugePages_Total /proc/meminfo HugePages_Total: 0 
    # cat /proc/sys/vm/nr_hugepages 0 # sysctl vm.nr_hugepages vm.nr_hugepages = 0 
    # grep -i HugePages_Total /proc/meminfo HugePages_Total: 1024 
    # cat /proc/sys/vm/nr_hugepages 1024 # sysctl vm.nr_hugepages vm.nr_hugepages = 1024 

    Comments

    Diagnostic Steps

    Links contained herein to external website(s) are provided for convenience only. Red Hat has not reviewed the links and is not responsible for the content or its availability. The inclusion of any link to an external website does not imply endorsement by Red Hat of the website or their entities, products or services. You agree that Red Hat is not responsible or liable for any loss or expenses that may result due to your use of (or reliance on) the external site or content.

    This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

    Источник

    How to enable or disable transparent (THP) and explicit (nr_hugepages) hugepage and check the status in Linux with examples (explained in detail)

    Running the below commands will stop only creation and usage of the new THP.
    The THP which were created and used at the moment the above commands were run would not be disassembled into the regular memory pages.

    To get rid of THP completely the system should be rebooted with THP disabled at boot time.

    # echo never > /sys/kernel/mm/redhat_transparent_hugepage/enabled
    # echo never > /sys/kernel/mm/redhat_transparent_hugepage/defrag

    «always» means that an application requesting THP will stall on allocation failure and directly reclaim pages and compact memory in an effort to allocate a THP immediately.
    «defer» means that an application will wake kswapd in the background to reclaim pages and wake kcompactd to compact memory so that THP is available in the near future. It’s the responsibility of khugepaged to then install the THP pages later.
    «madvise» will enter direct reclaim like «always» but only for regions that are have used madvise(MADV_HUGEPAGE). This is the default behaviour.
    «never» means do not allocate THP at all

    By default kernel tries to use huge zero page on read page fault to anonymous mapping. It’s possible to disable huge zero page by writing 0 or enable it back by writing 1

    echo 0 >/sys/kernel/mm/transparent_hugepage/use_zero_page
    echo 1 >/sys/kernel/mm/transparent_hugepage/use_zero_page

    How to disable Transparent Hugepages permanently using GRUB2?

    You can change the sysfs boot time defaults of Transparent Hugepage support by passing the parameter » transparent_hugepage=always » or » transparent_hugepage=madvise » or » transparent_hugepage=never » to the kernel command line.

    To disable THP use » transparent_hugepage=never » to the kernel command line in the grub configuration file.
    For my setup I am using GRUB2 (/etc/sysconfig/grub)

    Next rebuild your grub configuration file

    Reboot the node for the changes to take affect. Once the node is up make sure the newly added entry exists in your loaded grub configuration

    # grep transparent_hugepage /proc/cmdline
    BOOT_IMAGE=/vmlinuz-3.10.0-862.6.3.el7.x86_64 root=/dev/mapper/os-root ro novga console=ttyS0,115200 panic=1 numa=off elevator=cfq rd.md.uuid=d265dd3d:9ee4d53a:597b8c08:8201b9af rd.lvm.lv=os/root rd.md.uuid=5398452a:ab1b8e91:4307b53b:5c3cccbd rd.md.uuid=131bc1e7:7c9087c3:03f3ad4a:7cde170c noht biosdevname=0 net.ifnames=0 rhgb quiet transparent_hugepage=never

    So the configuration is correctly loaded.

    How to disable Transparent Hugepages permanently using tuned profile?

    You can either use your existing tuned profile or you can create a new profile.

    Use your existing tuned profile.
    My node is configured with throughput-performance profile, you can check your loaded profile using below command

    Next edit your tuned profile configuration file and append below value under ‘[vm]’

    Next activate your profile

    Create a new customised tuned profile
    With this we will create a customized version of the currently running profile. The customized version will only disable THP.

    Find out which profile is active, create a copy. In the following example we currently use the throughput-performance profile:

    To create customized profile, create a new directory in /etc/tuned directory with desired profile name.

    Then create a new tuned.conf file for myprofile-disablethp, and insert the new tuning info:

    Make the script executable

    How to check if HugePages is disabled?

    In the above steps we disabled the THP to make sure kernel doesnot allocates or reserves any hugepage.

    If the value of HugePages_Total is «0» it means HugePages is disabled on the system.

    This must show that [never] is selected for hugepage as above

    How to check if HugePages is enabled?

    If the value of HugePages_Total is greater than «0», it means HugePages is enabled on the system

    This must «not» show highlighted option as [never].

    How to disable Explicit Transparent Hugepages on runtime?

    nr_hugepages indicates the current number of «persistent» huge pages in the kernel’s huge page pool. «Persistent» huge pages will be returned to the huge page pool when freed by a task.

    When multiple huge page sizes are supported, /proc/sys/vm/nr_hugepages indicates the current number of pre-allocated huge pages of the default size.

    Thus, one can use the following command to dynamically allocate/deallocate default sized persistent huge pages. If the value in /proc/sys/vm/nr_hugepages file or vm.nr_hugepages sysctl parameter is «0» it means HugePages is disabled on the system

    # cat /proc/sys/vm/nr_hugepages
    0

    # sysctl vm.nr_hugepages
    vm.nr_hugepages = 0

    How to disable Explicit Transparent Hugepages permanently?

    To disable the explicit hugepage permanently add below entry in «/etc/sysctl.conf»

    Below command will show the updated value

    How to enable and assign explicit hugepages permanently?

    One allocate persistent huge pages on the kernel boot command line by specifying the » hugepages=N » parameter, where ‘N’ = the number of huge pages requested. This is the most reliable method of allocating huge pages as memory has not yet become fragmented.

    Append «hugepages=N» to » /etc/sysconfig/grub » to GRUB_CMDLINE_LINUX as shown below

    # grub2-mkconfig -o /boot/grub2/grub.cfg
    Generating grub configuration file .
    Found linux image: /boot/vmlinuz-3.10.0-693.21.1.el7.x86_64
    Found initrd image: /boot/initramfs-3.10.0-693.21.1.el7.x86_64.img
    Found linux image: /boot/vmlinuz-0-rescue-fae7c244c3134771a10cc7c3ace3edcb
    Found initrd image: /boot/initramfs-0-rescue-fae7c244c3134771a10cc7c3ace3edcb.img
    done

    Reboot the node for the changes to take affect
    Validate your changes post reboot

    You can also reserve hugepages using sysctl.conf

    Reboot the node for the changes to take affect.

    Источник

Оцените статью
Adblock
detector