56

I use mongodb 3.0 and I find a warning:

MongoDB shell version: 3.0.0
connecting to: test
Server has startup warnings: 
2015-03-13T16:28:29.405+0800 I CONTROL  [initandlisten] 
2015-03-13T16:28:29.406+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2015-03-13T16:28:29.406+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2015-03-13T16:28:29.406+0800 I CONTROL  [initandlisten] 
2015-03-13T16:28:29.407+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-03-13T16:28:29.407+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2015-03-13T16:28:29.407+0800 I CONTROL  [initandlisten]

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

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

I try to use echo, but it's not permanent, after reboot it will be reset.

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

How do I modify /sys/kernel/mm/transparent_hugepage/enabled permanently?

1

7 Answers 7

77

You can

  1. install the sysfsutils package:

    sudo apt install sysfsutils
    
  2. and append a line with that setting to /etc/sysfs.conf:

    kernel/mm/transparent_hugepage/enabled = never
    

This is the cleanest solution, because it keeps all the sysfs configuration in one place instead of relying on custom start-up scripts. The other answers, with the scripts and conditional expressions, are suitable if you don't know through which path the kernel will expose that setting, i. e. if you don't even have a rough idea of the kernel version running on the affected machine.

17
  • 4
    This did nothing for me on Ubuntu 14.04 on an AWS EC2 instance. I don't know much about sysfs.conf but it does seem to be an Ubuntu supported configuration, yet the file didn't exist on our system previously, and adding it and rebooting didn't change the mongo output warnings at all. Also tried clayzermk1's advice below, 'defrag' warning still appeared. The official mongo docs init script did remove both warnings.
    – Neek
    Oct 7, 2015 at 4:56
  • What's the output of cat /sys/kernel/mm/transparent_hugepage/enabled? Does it exist at all? sysfs.conf is normally evaluated at boot by /etc/init.d/sysutils. Is it enabled? What happens, when you invoke /etc/init.d/sysutils start? Oct 7, 2015 at 6:52
  • 3
    I also don't have sysfs.conf file and /etc/init.d/sysutils. ubuntu 16.04
    – HEX
    May 21, 2016 at 13:51
  • 1
    @unhammer: No, the two have a different scopes. sysctl is for settings that you could also achieve with kernel parameters; sysfs.conf is for entries in /sys. Nov 16, 2016 at 9:02
  • 1
    @navossoc: Could you please open a new question if you have a new or follow-up question? The comment section is not suitable or meant for new questions or extended discussion. You’re welcome to send me a comment with a notification to draw my attention to it. Thanks. Mar 5, 2018 at 12:46
26

The MongoDB docs have a few suggestions. http://docs.mongodb.org/manual/reference/transparent-huge-pages/

The "preferred" way is to edit /etc/default/grub and append transparent_hugepage=never to GRUB_CMDLINE_LINUX_DEFAULT then run update-grub to rebuild the GRUB configuration.

The following two-liner will do just that. Make sure to verify the output!

sed -r 's/GRUB_CMDLINE_LINUX_DEFAULT="[a-zA-Z0-9_= ]*/& transparent_hugepage=never/' /etc/default/grub | sudo tee /etc/default/grub
sudo update-grub

Note that update-grub is a wrapper for grub-mkconfig. grub-mkconfig will clobber entries in /etc/default/grub with those from /etc/default/grub.d/*. If by chance you are running on AWS, you will need to instead edit /etc/default/grub.d/50-cloudimg-settings.cfg.

The "alternate" method is to edit /etc/rc.local and add the following before exit 0:

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

Personally, I ended up doing both the "preferred" and "alternate" methods since they aren't mutually exclusive and it eliminates the warning about defrag.

* Worked for me on Ubuntu 14.04, MongoDB 3.0.2, and AWS.

2
  • I tried this at my one-click MongoDB application server on DigitalOcean and it didn't work. Still getting the same warning message.
    – scaryguy
    Aug 27, 2015 at 1:03
  • Thank you for the part about EC2 and 50-cloudimg-settings.cfg May 13, 2017 at 13:42
17

Here's a solution that has been tested to work on Ubuntu 16.04 on AWS EC2. Ubuntu 16.04 uses the systemd init system, and this config file expresses that these settings should be changed before MongoDB boots up.

Create a file named /etc/systemd/system/mongodb-hugepage-fix.service and add the following content:

[Unit]
Description="Disable Transparent Hugepage before MongoDB boots"
#WARN: check service name on your system
# If you are using MongoDB Cloud, service name is "mongodb-mms-automation-agent.service"
Before=mongodb.service      

[Service]
Type=oneshot
ExecStart=/bin/bash -c 'echo never > /sys/kernel/mm/transparent_hugepage/enabled'
ExecStart=/bin/bash -c 'echo never > /sys/kernel/mm/transparent_hugepage/defrag'

[Install]
#WARN: check service name on your system
# If you are using MongoDB Cloud, service name is "mongodb-mms-automation-agent.service"
RequiredBy=mongodb.service

To load the file into systemd:

systemctl daemon-reload

To activate the file as a boot-time dependency of MongoDB

systemctl enable mongodb-hugepage-fix

If you want to activate the changes immediately (before the next boot)

systemctl start mongodb-hugepage-fix
systemctl restart mongod

This solution is not suitable for Ubuntu 14.04, which uses the Upstart init solution instead of systemd.

1
  • 1
    The systemd fix is perfect for modern MongoDB installs, thank you.
    – four43
    Mar 12, 2018 at 19:45
8

Append the following lines below into of /etc/rc.local.

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

This will run when you reboot the server.

4
  • 1
    Is there a config file about this? rc.local is not a good idea, because rc.local will execute at the end of system start up. So mongodb service will still use the default setting.
    – Feng Yu
    Mar 16, 2015 at 14:24
  • 3
    edit the /etc/default/grub file and make changes to the line "GRUB_CMDLINE_LINUX_DEFAULT="transparent_hugepage=never" and run the update-grub command then restart the server
    – PKumar
    Mar 18, 2015 at 7:51
  • 2
    Thanks, it works. But, is there a similar way to modify /sys/kernel/mm/transparent_hugepage/defrag? When I reboot, mongo shell also tells me "** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'."
    – Feng Yu
    Mar 18, 2015 at 13:59
  • @FengYu try this page docs.mongodb.org/manual/tutorial/transparent-huge-pages
    – PKumar
    Oct 20, 2015 at 13:55
1

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

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/mongo_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.

1
  • Note that this solution uses the "Upstart" init system, while Ubuntu 16.04 is based on the systemd init system. Sep 2, 2016 at 16:11
1

After reading a little bit in the warning log I've added these two lines in /etc/sysfs.conf y voilá

kernel/mm/transparent_hugepage/enabled = never
kernel/mm/transparent_hugepage/defrag = never

Reboot the machine after applying these changes.

2
  • 1
    That still requires the sysfsutils package (see my answer). Jan 8, 2018 at 11:21
  • surely it is needed
    – Ulv3r
    May 16, 2018 at 4:55
0

GRUB will change after upgrade, and rc.local will run only after mongo is started, so maybe we should add service mongo restart at the end of rc.local like this

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

wait 1 && servie mongod restart

or maybe someone successfully added the lines above to init script in Ubuntu 14.04?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .