Kai
Kai

Reputation: 1488

Docker --cpus use cpu cores or processors to limit usage?

Docker have --cpus to constrain CPU usage for container.

According to docs, it will

Specify how much of the available CPU resources a container can use. For instance, if the host machine has two CPUs and you set --cpus="1.5", the container is guaranteed at most one and a half of the CPUs.

However, I run machine:

# cat /proc/cpuinfo | grep "cpu cores" | tail -n 1
8
# cat /proc/cpuinfo | grep "processor" | wc -l
16

Does it make sense to set --cpus=8 if I want to set 50% limit to container? Or it will be 100%?

I don't see clear answer neither in Docker documentation nor in cgroups manual.

I saw detailed explanation of differences between physical cpu and virtual cpu and cores here, but it don't clarify what I should use for my limits with Docker.

Upvotes: 2

Views: 4337

Answers (1)

BMitch
BMitch

Reputation: 263627

By default, the process in the container is run with no CPU restrictions and can use all available processor time, competing with other processes running on the Linux host. When setting --cpus this configures the cgroup settings to limit processes inside that container to only use that many shares of CPU time. This is managed by the kernel, but the underlying hardware is still visible in /proc/cpuinfo. Instead you should look at the cgroup settings:

$ docker run -it --rm --cpus 0.75 busybox sh
/proc # cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us
75000
/proc # cat /sys/fs/cgroup/cpu/cpu.cfs_period_us
100000

Contrast with an unlimited container:

$ docker run -it --rm busybox sh
/ # cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us
-1
/ # cat /sys/fs/cgroup/cpu/cpu.cfs_period_us
100000
/ # 

Upvotes: 4

Related Questions