Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change swappiness for docker container

I am using docker to containerize bunch of services. Some times, containerized services heavily swaps. I've changed vm.swappiness to 1 via sysctl on my host system. But docker's memory cgroup still have old (default) value of 60. Therefore, all particular containers' cgroups have same value, as parent.

sysctl vm.swappiness
> vm.swappiness = 1
cat /sys/fs/cgroup/memory/docker/memory.swappiness
> 60
cat /sys/fs/cgroup/memory/docker/${CONTAINER_ID}/memory.swappiness
> 60

All attempts to change swappiness manually (by echoing desired value into memory.swappiness file) fails with permission denied.

Subject: How can I restrict containers swappiness?

I am using ubuntu 12.04 with kernel 3.13, my docker version is 1.1.2 with native execution driver (not lxc) of version 0.2. Kernel is loaded with cgroup_enable=memory swapaccount=1.

like image 807
Viacheslav Kovalev Avatar asked Sep 10 '14 13:09

Viacheslav Kovalev


People also ask

What is Docker memory Swappiness?

See --memory-swap details. --memory-swappiness. By default, the host kernel can swap out a percentage of anonymous pages used by a container. You can set --memory-swappiness to a value between 0 and 100, to tune this percentage.

How do I assign a memory to a Docker container?

Set Maximum Memory Access To limit the maximum amount of memory usage for a container, add the --memory option to the docker run command. Alternatively, you can use the shortcut -m . Within the command, specify how much memory you want to dedicate to that specific container.

How do I change the container restart policy?

Restart policy detailsA restart policy only takes effect after a container starts successfully. In this case, starting successfully means that the container is up for at least 10 seconds and Docker has started monitoring it. This prevents a container which does not start at all from going into a restart loop.


2 Answers

Got it! Docker does not even touch this parameter. memory.swappines for cgroups really changing according /proc/vm/swappiness. All children inherits this value from parent. Docker does not even touch this parameter. Moreover, in some cases (and exactly in my own) there is no ability to write something in memory.swappines. If memory cgroup uses hierarchy, or contains children, all attempts to write something to cgroups memory.swappiness will fail.

Look there. This is from mm/memcontrol.c.

static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
                       struct cftype *cft, u64 val)
{
    struct mem_cgroup *memcg = mem_cgroup_from_css(css);
    struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(&memcg->css));

    if (val > 100 || !parent)
        return -EINVAL;

    mutex_lock(&memcg_create_mutex);

    /* If under hierarchy, only empty-root can set this value */
    if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
        mutex_unlock(&memcg_create_mutex);
        return -EINVAL;
    }

    memcg->swappiness = val;

    mutex_unlock(&memcg_create_mutex);

    return 0;
}
like image 179
Viacheslav Kovalev Avatar answered Sep 20 '22 22:09

Viacheslav Kovalev


If you upgrade to a 3.18 kernel or later, the restriction preventing modification to the cgroup memory.swappiness parameter in child/hierarchy cgroups is removed. The Linux kernel patch which removed this restriction can be seen here: https://github.com/torvalds/linux/commit/3dae7fec5e884a4e72e5416db0894de66f586201

Docker 1.8 will most likely include the following PR (https://github.com/docker/docker/pull/14004) which allows the container to set its own memory.swappiness value allowing user control over this cgroup setting, as long as the Docker daemon host kernel has the patch noted above, or the host kernel is 3.18 or greater.

like image 29
Phil E Avatar answered Sep 21 '22 22:09

Phil E