disable transparent hugepages

146,951

Solution 1

To make options such as this permanent you'll typically add them to the file /etc/sysctl.conf. You can see a full list of the options available using this command:

$ sysctl -a

Example

$ sudo sysctl -a | head -5
kernel.sched_child_runs_first = 0
kernel.sched_min_granularity_ns = 6000000
kernel.sched_latency_ns = 18000000
kernel.sched_wakeup_granularity_ns = 3000000
kernel.sched_shares_ratelimit = 750000

You can look for hugepage in the output like so:

$ sudo sysctl -a | grep hugepage
vm.nr_hugepages = 0
vm.nr_hugepages_mempolicy = 0
vm.hugepages_treat_as_movable = 0
vm.nr_overcommit_hugepages = 0

It's not there?

However looking through the output I did not see transparent_hugepage. Googling a bit more I did come across this Oracle page which discusses this very topic. The page is titled: Configuring HugePages for Oracle on Linux (x86-64).

Specifically on that page they mention how to disable the hugepage feature.

excerpt

The preferred method to disable Transparent HugePages is to add "transparent_hugepage=never" to the kernel boot line in the "/etc/grub.conf" file.

   title Oracle Linux Server (2.6.39-400.24.1.el6uek.x86_64)
            root (hd0,0)
            kernel /vmlinuz-2.6.39-400.24.1.el6uek.x86_64 ro root=/dev/mapper/vg_ol6112-lv_root rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=uk
    LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16  rd_NO_DM rd_LVM_LV=vg_ol6112/lv_swap rd_LVM_LV=vg_ol6112/lv_root rhgb quiet numa=off
    transparent_hugepage=never
            initrd /initramfs-2.6.39-400.24.1.el6uek.x86_64.img

The server must be rebooted for this to take effect.

Alternatively you can add the command to your /etc/rc.local file.

if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
   echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
   echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi

I think I would go with the 2nd option, since the first will be at risk of getting unset when you upgrade from one kernel to the next.

You can confirm that it worked with the following command after rebooting:

$ cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]

Solution 2

I just wanted to add to this question as I was trying to disable transparent hugepages on CentOS v6 in order to enable TokuDB for MariaDB. I added the script mentioned by @slm to /etc/rc.local and it disabled transparent hugepages. However, because of the way startup scripts work in Linux, /etc/rc.local is executed after all the services are started. Therefore, transparent huge pages was being disabled after MariaDB was already started and the TokuDB engine wouldn't initialize. The only other way to disable transparent hugepages is by adding transparent_hugepage=never to the kernel parameter.

I noticed @Rwky's comment You can make the first option survive kernel updates by adding transparent_hugepage=never to the GRUB_CMDLINE_LINUX_DEFAULT option in /etc/default/grub on most distributions. and found out that CentOS doesn't support the /etc/default/grub file and was worried about transparent_hugepage=never disappearing from the kernel parameters when it is updated. But not to worry, CentOS is setup to keep any changes made to the kernel parameters in grub so when it is updated they are kept.

To also add, the proper way to modify the kernel parameters for grub is with grubby. I created this simple script to add transparent_hugepage=never to each kernel with grubby:

#!/bin/sh

if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

for KERNEL in /boot/vmlinuz-*; do
    grubby --update-kernel="$KERNEL" --args='transparent_hugepage=never'
done

Solution 3

All of the above didn't work for me on an EC2 Ubuntu 16.04, but this did:

sudo apt install hugepages
sudo hugeadm --thp-never

Solution 4

Here's an implementation using puppet:

exec { "disable_transparent_hugepage_enabled":
  command => "/bin/echo never > /sys/kernel/mm/transparent_hugepage/enabled",
  unless  => "/bin/grep -c '\[never\]' /sys/kernel/mm/transparent_hugepage/enabled 2>/dev/null",
}

exec { "disable_transparent_hugepage_defrag":
  command => "/bin/echo never > /sys/kernel/mm/transparent_hugepage/defrag",
  unless  => "/bin/grep -c '\[never\]' /sys/kernel/mm/transparent_hugepage/defrag 2>/dev/null",
}

Solution 5

Since the kernel line transparent_hugepage=never only disables half of what I need (both, for annoying mongodb failing/logs), that I didn't persist through systemd startup script but now have: echo never | sudo tee /sys/kernel/mm/transparent_hugepage/enabled. That works in either systemctl boot script (when properly configured one in /etc/systemd/system) or straight from the cli as is.

Share:
146,951

Related videos on Youtube

Ramesh
Author by

Ramesh

Updated on September 18, 2022

Comments

  • Ramesh
    Ramesh almost 2 years

    We are installing SAP HANA in a RAID machine. As part of the installation step, it is mentioned that,

     To disable the usage of transparent hugepages set the kernel settings 
     at runtime with echo never > /sys/kernel/mm/transparent_hugepage/enabled 
    

    So instead of runtime, if I wanted to make this a permanent change, should I add the above line inside /proc/vmstat file?

    • Dejay Clayton
      Dejay Clayton about 9 years
      Note that if you're using some of the solutions mentioned here, on a system that also runs 'tuned', tuned may override those solutions. See here for more info: bugzilla.redhat.com/show_bug.cgi?id=1189868
  • Rwky
    Rwky about 10 years
    You can make the first option survive kernel updates by adding transparent_hugepage=never to the GRUB_CMDLINE_LINUX_DEFAULT option in /etc/default/grub on most distributions.
  • ub3rst4r
    ub3rst4r over 9 years
    If the output of cat /sys/kernel/mm/transparent_hugepage/enabled is [always] madvise never, then the status is always or that it is enabled (note the [] brackets around always)
  • seattlegaucho
    seattlegaucho over 9 years
    I just wanted to add that if you modify the <code>/etc/default/grub</code> file, you need to run grub-mkconfig as 'root' to generate the actual configuration file for grub.
  • zhengyue
    zhengyue about 9 years
    For an EC2 ubuntu instance, I have to modify the /etc/default/grub.d/50-cloudimg-settings.cfg file instead of the /etc/default/grub to make it work.
  • Marki555
    Marki555 about 9 years
    Don't confuse hugepages and transparent hugepages. The latter can cause many issues, mainly high CPU usage while constantly trying to defragment memory and convert normal 4kB pages into huge 2MB pages.
  • Sandeep Singh
    Sandeep Singh about 9 years
    I am using CentOS 6.6. I have modified /etc/rc.local but it is not working for me. Can you please explain the root case to me.
  • ub3rst4r
    ub3rst4r about 9 years
    @s.singh As I stated /etc/rc.local is executed after all the services are started so it needs to be disabled at the kernel level
  • Sandeep Singh
    Sandeep Singh about 9 years
    I tried adding transparent_hugepage=never in /etc/sysctl.conf and restarted the machine but still it is not working. Do I need to run your script ?
  • ub3rst4r
    ub3rst4r about 9 years
    StackExchange comments aren't for support. If your having problems, I suggest you ask a question on of the Stack Exchange websites.
  • MFB
    MFB almost 9 years
    @Rwky doesn't seem to work on EC2 instances, for some reason.
  • nino
    nino almost 9 years
    @Rwky -- when tweaking as per your suggestion - I'd emphasize the next action after saving the file is to execute sudo update-grub to get the new settings "written in stone". +1 for pointing the grub file line.
  • BAR
    BAR almost 9 years
    VERY important to heed @Marki555 warning.
  • 赵伟辰
    赵伟辰 over 8 years
    Could you please expand your instructions to also include the "properly configured script" and steps how to setup everything? Official MongoDB instructions docs.mongodb.org/manual/tutorial/transparent-huge-pages show the old way, while now that systemd is more common, it would be nice to have this in an systemd way.
  • nelaaro
    nelaaro about 7 years
    Exactly what I was looking for.
  • Rwky
    Rwky almost 7 years
    An update for those on EC2, cloudimg adds the file /etc/default/grub.d/50-cloudimg-settings.cfg which overrides the settings in /etc/default/grub adding a file /etc/default/grub.d/99-transparent-hugepage.cfg with the content GRUB_CMDLINE_LINUX_DEFAULT="$GRUB_CMDLINE_LINUX_DEFAULT transparent_hugepage=never" will solve this.
  • peterh
    peterh almost 6 years
    Hi, welcome on the Unix SE! Note, it would be much better if you would also explain, what your commands are doing.
  • Jeow Li Huan
    Jeow Li Huan over 4 years
    After adding a file /etc/default/grub.d/99-transparent-hugepage.cfg with the content GRUB_CMDLINE_LINUX_DEFAULT="$GRUB_CMDLINE_LINUX_DEFAULT transparent_hugepage=never" as @Rwky mentioned, run grub-mkconfig -o /boot/grub/grub.cfg to generate the updated grub.cfg. After a restart, cat /proc/cmdline will contain transparent_hugepage=never
  • SrujanSreepathi
    SrujanSreepathi about 4 years
    This didn't persist the change
  • barrypicker
    barrypicker about 4 years
    are you conflating huge pages with transparent huge pages?
  • Laurent
    Laurent about 4 years
    On Debian Buster, the path to echo is /bin/echo.
  • Xunnamius
    Xunnamius about 3 years
    Thank you for saving me the time :)