9

I want to disable transparent_hugepage (THP) on a CentOS 7 EC2 instance, which is enabled by default:

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

This setting can be manually changed:

# echo never > /sys/kernel/mm/transparent_hugepage/enabled
# echo never > /sys/kernel/mm/transparent_hugepage/defrag
# cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
# cat /sys/kernel/mm/transparent_hugepage/defrag
always madvise [never]

... but the changes are lost after reboot.

I tried to put the echo never [...] instruction into my rc.local and cloud.cfg files, but it didn't work.

I also tried to append the setting transparent_hugepage=never to the kernel line of /etc/grub.conf (as explained there), but it didn't work better.

So... how can I disable THP on CentOS 7 running on an AWS EC2 instance ?

edit: changed title... I need to disable THP and THP defrag

vcarel
  • 434
  • 4
  • 13
  • For interest's sake, may I ask why you want to disable THP? – Cameron Kerr May 04 '15 at 10:02
  • 1
    For MongoDB (http://docs.mongodb.org/manual/reference/transparent-huge-pages/) – vcarel May 04 '15 at 10:04
  • After reboot your instasnce will not work if you "append transparent_hugepage=never to the kernel line of /etc/grub.conf" and will need to remove this line from grub file. For this purposes we can attach existing instance to new one and mount partition to the folder – Artem.Borysov Feb 29 '16 at 12:37

5 Answers5

15

The solution is in tuned, as pointed out by @michael-hampton. The tricky part is that the vm plugin can only configure the /sys/kernel/mm/transparent_hugepage/enabled setting.

To disable the /sys/kernel/mm/transparent_hugepage/defrag setting too, I had to create a script that is called by the profile on start.

At the end, the complete solution is:

step 1: Create the directory to hold the custom profile:

mkdir /etc/tuned/custom

step 2: Create the profile /etc/tuned/custom/tuned.conf:

[main]
include=virtual-guest

[vm]
transparent_hugepages=never

[script]
script=script.sh

Note that this profile inherits from virtual-guest, which was my active profile, actually looking appropriate for virtualized server (EC2). You can view your active profile with the command tuned-adm active. If you're curious, you can check out the content of the predefined profiles in /usr/lib/tuned/

step 3: Create the script /etc/tuned/custom/script.sh:

#!/bin/sh

. /usr/lib/tuned/functions

start() {
    echo never > /sys/kernel/mm/transparent_hugepage/defrag
    return 0
}

stop() {
    return 0
}

process $@

Make it executable:

sudo chmod 755 /etc/tuned/custom/script.sh

step 4: Activate the new profile:

tuned-adm profile custom

Now you should get:

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

It will persist after reboot.

vcarel
  • 434
  • 4
  • 13
2

In addition to setting the grub command line, you also need to configure tuned. But not using the instructions you linked to, as they are so full of errors it would take half a day just to explain them all.

Create a custom tuned profile (which I'll call custom), and then set the profile. You will base it on an existing profile, such as virtual-guest if you are running in a virtual machine (EC2 is, of course), or throughput-performance if you are on a physical machine.

Create the directory to hold the custom profile:

mkdir /etc/tuned/custom

Create the custom profile /etc/tuned/custom/tuned.conf, for example:

[main]
include=virtual-guest

[vm]
transparent_hugepages=never

Now set the profile:

tuned-adm profile custom
Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
  • Those instructions full of errors ? It would take half day to explain ? That's what I'm trying to understand. – vcarel May 04 '15 at 09:45
  • I don't understand your answer by the way. Why do I have to configure grub *and* use tuned altogether ? – vcarel May 04 '15 at 09:46
  • 1
    Because it is on by default in the kernel configuration _and_ on by default in the tuned configuration. You need to change both in order for it to be completely effective. – Michael Hampton May 04 '15 at 09:50
  • 1
    Configuring tuned seems enough... I didn't have to change grub config. BTW, is there another tuned setting to disable THP defrag too ? – vcarel May 04 '15 at 12:56
  • @vcarel It is not necessary because transparent hugepages is already disabled! – Michael Hampton May 04 '15 at 18:00
1

Also try this

nano /etc/init.d/disable-transparent-hugepages

#!/bin/sh
### BEGIN INIT INFO
# Provides:          disable-transparent-hugepages
# Required-Start:    $local_fs
# Required-Stop:
# X-Start-Before:    mongod mongodb-mms-automation-agent
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Disable Linux transparent huge pages
# Description:       Disable Linux transparent huge pages, to improve
#                    database performance.
### END INIT INFO

case $1 in
  start)
    if [ -d /sys/kernel/mm/transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/transparent_hugepage
    elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/redhat_transparent_hugepage
    else
      return 0
    fi

    echo 'never' > ${thp_path}/enabled
    echo 'never' > ${thp_path}/defrag

    unset thp_path
    ;;
esac

sudo chmod 755 /etc/init.d/disable-transparent-hugepages

sudo chkconfig --add disable-transparent-hugepages

0

You can edit /etc/rc.local file and add this following command to that file :

echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag

and run chmod +x /etc/rc.d/rc.local to ensure script will be executed during boot. tested on Amazon Linux 2.

Yuda Prawira
  • 117
  • 5
-2

EDIT: the reply above is wrong, because the transparent huge page knobs are missing from sysctl at the moment. Sorry for the noise.


You can put the desired values in /etc/sysctl.conf.

From sysctl.conf(5) man page:

SYSCTL.CONF(5)                                                                                 File Formats                                                                                 SYSCTL.CONF(5)

NAME
       sysctl.conf - sysctl preload/configuration file

DESCRIPTION
       sysctl.conf is a simple file containing sysctl values to be read in and set by sysctl.  The syntax is simply as follows:

              # comment
              ; comment

              token = value

       Note  that  blank  lines  are ignored, and whitespace before and after a token or value is ignored, although a value can contain whitespace within.  Lines which begin with a # or ; are considered
       comments and ignored.

EXAMPLE
              # sysctl.conf sample
              #
                kernel.domainname = example.com
              ; this one has a space which will be written to the sysctl!
                kernel.modprobe = /sbin/mod probe
shodanshok
  • 44,038
  • 6
  • 98
  • 162