Skip to content

Instantly share code, notes, and snippets.

@rfc1459
Last active March 12, 2019 14:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rfc1459/87b8e2d9f35075ce0a98 to your computer and use it in GitHub Desktop.
Save rfc1459/87b8e2d9f35075ce0a98 to your computer and use it in GitHub Desktop.
Ubuntu 14.04 memory cgroup fixup for Docker

Fix memory cgroup configuration for lxc-docker on Ubuntu 14.04

Step 1: enable hierarchical memory management

Drop memory-cg.conf into /etc/init. The upstart job will ensure that the memory cgroup is configured with hierarchical stats tracking before systemd-logind and/or docker start creating cgroups.

(Yes, it's mostly a crude hack with an obscure way of syncing with cgroup-lite. That can't be helped, though)

Step 2 (optional): enable swap accounting

Edit /etc/default/grub and append to GRUB_CMDLINE_LINUX_DEFAULT swapaccount=1.

You don't want to add it to GRUB_CMDLINE_LINUX, since it would be applied even while booting recovery mode kernels.

Run update-grub2 afterwards to apply the new configuration.

Step 3: reboot

While it might sound otheros-like, you really have to reboot to pick up the new cgroup configuration, even if you haven't modified the kernel command line. The rationale behind this is that you'd have to stop nearly every service to safely get rid of existing cgroups, then start the new upstart job and restart everything else.

Rebooting is just easier - and it's required if you're enabling swap accounting.

description "Fix memory cgroup configuration for lxc-docker"
author "Matteo Panella <morpheus@level28.org>"
task
# BEWARE: since cgroup handling in Ubuntu is a royal mess, you HAVE to insert
# an explicit dependency on every single job that could create cgroups after
# it's started
start on (starting systemd-logind or starting docker)
script
MEM_USE_HIER="/sys/fs/cgroup/memory/memory.use_hierarchy"
# TL;DR: Explicit sync point with cgroup-lite
status cgroup-lite 2>/dev/null >/dev/null && start wait-for-state WAITER=memory-cg WAIT_FOR=cgroup-lite WAIT_STATE=running 2>/dev/null >/dev/null || :
# Check for the relevant configuration file, abort if it doesn't exist
test -f $MEM_USE_HIER || exit 0
# Check if use_hierarchy is already enabled
test "`cat $MEM_USE_HIER`" = "1" && exit 0 || :
# Enable use_hierarchy - complain loud if we can't
echo 1 > $MEM_USE_HIER || {
logger -is -t "$UPSTART_JOB" "Unable to set memory.use_hierarchy to 1, something has already created a child memory cg"
}
exit 0
end script
@xiaods
Copy link

xiaods commented Mar 18, 2015

not working , @rfc1459 do you have any suggests?

@rfc1459
Copy link
Author

rfc1459 commented Mar 26, 2015

I made some modifications due to various upstart "issues" (misfeatures, actually) but forgot to update the upstart job here.

I'll post the updated job ASAP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment