Search code examples
linuxmemorydockercgroups

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.


Solution

  • 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;
    }