dockerlinux-kernelcpu-usagecgroups

Does Docker have support for cgroup v2's cpu.weight knob?


When the docker container was running in v1 we simply passed --cpu-shares values in the run command and as we all know in cgroup v2, we don't have cpu.shares knob available, it was replaced with cpu.weight.

In cgroup v1 the default value for cpu.shares is 1024 and that's the same for --cpu-shares which is a docker switch, to this day, I assumed they both are equal.

Now, in cgroup v2 cpu.shares is replaced with cpu.weight and the default value for cpu.weight is 100000.

My questions: how can we pass cpu.weight when running a docker container? Is it even supported? If so, how can we effectively equate cpu.weight to --cpu-shares?

P.S. I am new to docker and containerization technology so forgive any basic errors I made in the description.


Solution

  • TL;DR: use --cpu-shares with values in the range [2, 262144] as you would do with cgroups v1. Any sane OCI Runtime implementation is going to do the right value conversion from v1 cpu.shares to v2 cpu.weight for you.


    The OCI Runtime Spec was written with cgroups v1 in mind and therefore does not explicitly define what should happen with cgroups v2 in this case. However, a sane OCI Runtime implementation should be able to easily convert from v1 cpu.shares whose range is [2, 262144] to v2 cpu.weight whose range is [1, 10000]. The conversion is simple enough:

    cpu.weight = (1 + ((cpu.shares - 2) * 9999) / 262142)
    

    As far as I can tell, most of the OCI Runtime implementations for Containers listed here do this. It's pretty easy to check by simply looking for the magic value 262142 in the source code.

    If you are on Linux and installed Docker following the install instructions in the Docker docs you are using opencontainers/runc.

    Here's a quick reference list: