How to Avoid Transparent_Hugepage/Defrag Warning from Mongodb

How to avoid transparent_hugepage/defrag warning from mongodb?

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!

Why does mongodb complain about transparent_hugepage?

Edit: Mongodb have addressed this issue since I wrote this answer. Their recommendation is at https://docs.mongodb.com/master/tutorial/transparent-huge-pages/ and probably ought to be your go-to solution. My original answer will still work, but I'd consider it a hack now that an official solution is available.

Original answer: According to the MongoDB documentation, http://docs.mongodb.org/manual/reference/transparent-huge-pages/, and support, https://jira.mongodb.org/browse/DOCS-2131, transparent_hugepage (THP) is designed to create fewer large memory blocks rather than many small memory blocks in systems with a lot of memory. This is great if your software needs large contiguous memory accesses. For MongoDB, however, regardless of memory available, it requires numerous smaller memory accesses and therefore performs better with THP disabled.

That makes me think either way will work, but you'll get better mongo (or any database) performance with THP off, giving you smaller bites of memory. If you don't have much memory anyway, THP probably ought to be off no matter what you run.

Several ways to do that are outlined in the link above. The most universally applicable appears to be editing rc.local.

$ sudo nano /etc/rc.local

Insert the following lines before the "exit 0" line.

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

Note: redhat-based systems may use "redhat_transparent_hugepage" rather than "transparent_hugepage" and can be checked by:

ls /sys/kernel/mm/*transparent_hugepage*/enabled
cat /sys/kernel/mm/*transparent_hugepage*/enabled

To apply the changes, reboot (which will run rc.local) or:

$ sudo su
# source /etc/rc.local
# service mongod restart
# exit

to properly apply the changes made above

Mongodb startup warnings after update

Adding the following lines preceding exit 0 in /etc/rc.local file with root privileges did the magic. Rebooted the OS after saving the file.Then warnings disappeared in the mongo shell.

Source: MongoDB documentation(http://docs.mongodb.org/manual/reference/transparent-huge-pages/#transparent-huge-pages-thp-settings)

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

How to disable THP on Centos 7 for mongodb

I had the same issue, and solved it by writing this in /etc/rc.local:

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

I also gave execution permissions to /etc/rc.d/rc.local

Now thb are disabled every time I boot.

Can't Write to /sys/kernel/ to disable Transparent Huge Pages (THP) for MongoDB on OVH CentOS 7

Right now I've installed CentOS 7 on OVH. They use /boot/bzImage-3.14.32-xxxx-grs-ipv6-64 that implements grsecurity (https://grsecurity.net) which precludes access to some folders.

The very simple solution to the warnings from MongoDB about huge pages can be solved by replacing the kernel. The procedure for CentOS7 is as follows:

  1. Download required kernel from OVH ftp: ftp://ftp.ovh.net/made-in-ovh/bzImage2 into /boot folder.
  2. Edit /etc/grub2.cfg:

    # linux /boot/bzImage-3.14.32-xxxx-grs-ipv6-64 root=/dev/md1 ro net.ifnames=0

    linux /boot/bzImage-4.8.17-xxxx-std-ipv6-64 root=/dev/md1 ro net.ifnames=0

Here I replaced bzImage-3.14.32-xxxx-grs-ipv6-64 default by bzImage-4.8.17-xxxx-std-ipv6-64 without grs.

Now, reboot and check if the new kernel is ok:

root@ns506846 ~]# uname -r
4.8.17-xxxx-std-ipv6-64

Mongo DB Server Startup Warnings

Had the exact same issues with OVH/Kimsufi due to their custom kernel installed by default.

First, you need to have first the regular ubuntu kernel and not one modified by your hosting company.

Then, you need to disable transparent huge pages to remove the warning and improve memory performance related to memory management:

  1. Add this script as /etc/init.d/disable-transparent-hugepage

    #!/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
  2. Make the script executable sudo chmod 755 /etc/init.d/disable-transparent-hugepage

  3. Register it at boot sudo update-rc.d disable-transparent-hugepage defaults

Ref: https://docs.mongodb.org/v3.0/tutorial/transparent-huge-pages/



Related Topics



Leave a reply



Submit