# inspired by https://gist.github.com/rfc1459/87b8e2d9f35075ce0a98
description "Fix memory cgroup configuration for docker"

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
# NOTE 2: This script depends on the docker upstart script as it is setting up
# cgroups.
start on (started docker)

script
  MEM_USE_HIER="/sys/fs/cgroup/memory/memory.use_hierarchy"
  # 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
  logger -i -t "$UPSTART_JOB" "Setting memory.use_hierarchy to 1"
  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
