Viacheslav Kovalev
Viacheslav Kovalev

Reputation: 1745

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.

Upvotes: 9

Views: 10217

Answers (2)

Phil E
Phil E

Reputation: 1908

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.

Upvotes: 6

Viacheslav Kovalev
Viacheslav Kovalev

Reputation: 1745

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

Upvotes: 7

Related Questions