How to avoid transparent_hugepage/defrag warning from mongodb?

LinuxMongodbLinux Kernel

Linux Problem Overview


I'm receiving the following warning from mongodb about THP

2015-03-06T21:01:15.526-0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-03-06T21:01:15.526-0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'

But I did manage to turned THP off manually

frederick@UbuntuVirtual:~$ cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
frederick@UbuntuVirtual:~$ cat /sys/kernel/mm/transparent_hugepage/defrag
always madvise [never]

I did the trick by adding transparent_hugepage=never to GRUB_CMDLINE_LINUX_DEFAULT in /etc/default/grub and adding

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

to /etc/rc.local

How on earth can I avoid the warning?

Linux Solutions


Solution 1 - Linux

Official MongoDB documentation gives several solutions for this issue. You can also try this solution, which worked for me:

Note: Try official documentation directives if MongoDB version is greater than 3.0

  1. Open /etc/init.d/mongod file.
    (if no such file you might check /etc/init.d/mongod, /etc/init/mongod.conf files - credit: the below comments)

  2. Add the lines below immediately after chown $DAEMONUSER /var/run/mongodb.pid and before end script.

  3. Restart mongod (service mongod restart).

Here are the lines to add to /etc/init.d/mongod:

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

That's it!

Solution 2 - Linux

MongoDB have updated their recommendation to use an init.d script now: http://docs.mongodb.org/master/tutorial/transparent-huge-pages/

Solution 3 - Linux

For Ubuntu 14.04 using upstart:

Since we are deploying machines with Ansible I don't like modifying rc files or GRUB configs.

I tried using sysfsutils / sysfs.conf but ran into timing issues when starting the services on fast (or slow machines). It looked like sometimes mongod was started before sysfsutils. Sometimes it worked, sometimes it did not.

Since mongod is an upstart process I found that the cleanest solution was to add the file /etc/init/mongod_vm_settings.conf with the following content:

# Ubuntu upstart file at /etc/init/mongod_vm_settings.conf
#
#   This file will set the correct kernel VM settings for MongoDB
#   This file is maintained in Ansible

start on (starting mongod)
script
  echo "never" > /sys/kernel/mm/transparent_hugepage/enabled
  echo "never" > /sys/kernel/mm/transparent_hugepage/defrag
end script

This will run the script just before mongod will be started. Restart mongod (sudo service mongod restart) and done.

Solution 4 - Linux

  1. Open /etc/default/grub

    sudo vi /etc/default/grub

  2. Update
    GRUB_CMDLINE_LINUX_DEFAULT="" to GRUB_CMDLINE_LINUX_DEFAULT="transparent_hugepage=never"

  3. Save file
    :wq (in vi)

  4. Run update-grub

    sudo update-grub

  5. Reboot machine

Update: If you are using a virtual hosting provider, this will work IFF grub boot is supported. DigitalOcean DOES NOT support grub boot.

Solution 5 - Linux

Verified that the defrag is examined without regard to the enabled:

$ cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
$ cat /sys/kernel/mm/transparent_hugepage/defrag
[always] madvise never
$ service mongod start
... (in log) WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'
$ echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag
$ cat /sys/kernel/mm/transparent_hugepage/defrag
always madvise [never]
$ service mongod stop
$ service mongod start
... (no warning in log)

Therefore, the fix to this bug is to first look at transparent_hugepage/enabled, and if it is never, don't bother looking at the irrelevant transparent_hugepage/defrag setting.

Source.

Solution 6 - Linux

Ubuntu 16.04 using systemd:

systemctl edit mongod

Paste the following:

[Service]
PermissionsStartOnly=true
ExecStartPre=/bin/sh -c "echo never > /sys/kernel/mm/transparent_hugepage/enabled"
ExecStartPre=/bin/sh -c "echo never > /sys/kernel/mm/transparent_hugepage/defrag"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFrederick ZhangView Question on Stackoverflow
Solution 1 - LinuxefkanView Answer on Stackoverflow
Solution 2 - Linuxrobbie613View Answer on Stackoverflow
Solution 3 - LinuxWhyhankeeView Answer on Stackoverflow
Solution 4 - LinuxclrhoView Answer on Stackoverflow
Solution 5 - LinuxMichael HorojanskiView Answer on Stackoverflow
Solution 6 - LinuxsnapView Answer on Stackoverflow