Docker cpu limit analysis

步履不停
Release: 2019-07-02 16:49:03
Original
2511 people have browsed it

Docker cpu limit analysis

This article tests several configuration parameters that limit the use of CPU resources by docker containers. The resource occupancy was analyzed using the top and dstat commands respectively.

package main import ( "flag" "runtime" "fmt" ) func main() { cpunum := flag.Int("cpunum", 0, "cpunum") flag.Parse() fmt.Println("cpunum:", *cpunum) runtime.GOMAXPROCS(*cpunum) for i := 0; i < *cpunum - 1; i++ { go func() { for { } }() } for { } }
Copy after login

Created an image to test CPU occupancy. The image occupies 1 core by default

FROM busybox COPY ./full_cpu /full_cpu RUN chmod +x /full_cpu ENTRYPOINT ["/full_cpu", "-cpunum"] CMD ["1"] docker build -t fangfenghua/cpuuseset . docker push fangfenghua/cpuuseset docker info ... Default Runtime: runc Security Options: seccomp Kernel Version: 3.10.0-229.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 CPUs: 4 Total Memory: 993.3 MiB Name: localhost.localdomain ID: TU6M:E6WM:PZDN:ULJX:EWKS: ...
Copy after login
docker run -it --rm=true fangfenghua/cpuuseset [root@localhost src]# top top - 07:23:52 up 1:23, 2 users, load average: 0.61, 1.12, 1.04 Tasks: 154 total, 3 running, 145 sleeping, 6 stopped, 0 zombie %Cpu(s): 18.0 us, 0.1 sy, 0.0 ni, 81.8 id, 0.0 wa, 0.0 hi, 0.1 si, 0.0 st KiB Mem : 1017144 total, 422120 free, 171676 used, 423348 buff/cache KiB Swap: 1040380 total, 1040284 free, 96 used. 688188 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 20196 root 20 0 3048 720 460 R 101.7 0.1 0:37.56 full_cpu 1 root 20 0 41536 4028 2380 S 0.0 0.4 0:02.60 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.04 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:00.48 ksoftirqd/0 5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H 7 root rt 0 0 0 0 S 0.0 0.0 0:00.69 migration/0 docker run -it --rm=true fangfenghua/cpuuseset 4 top - 07:27:17 up 1:27, 2 users, load average: 2.41, 1.47, 1.18 Tasks: 159 total, 3 running, 145 sleeping, 11 stopped, 0 zombie %Cpu(s): 99.6 us, 0.2 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st KiB Mem : 1017144 total, 402508 free, 190908 used, 423728 buff/cache KiB Swap: 1040380 total, 1040284 free, 96 used. 668608 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 20935 root 20 0 3048 720 452 R 400.0 0.1 0:55.80 full_cpu 1 root 20 0 41620 4088 2380 S 0.0 0.4 0:02.88 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.04 kthreadd
Copy after login

On the Linux system, the parameters that can be used to limit the resource occupancy of the docker container are:

--cpu-period int Limit CPU CFS (Completely Fair Scheduler) period --cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota -c, --cpu-shares int CPU shares (relative weight) --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
Copy after login

docker provides two parameters -cpu-period and -cpu-quota to control the CPU clock cycles that the container can be allocated to. –cpu-period is used to specify how long the container’s use of the CPU should be reallocated, and –cpu-quota is used to specify the maximum amount of time that can be used to run the container during this cycle. Different from –cpu-shares, this configuration specifies an absolute value, and there is no flexibility in it. The container's use of CPU resources will never exceed the configured value.

The units of cpu-period and cpu-quota are microseconds (μs). The minimum value of cpu-period is 1000 microseconds, the maximum value is 1 second (10^6 μs), and the default value is 0.1 seconds (100000 μs). The value of cpu-quota defaults to -1, which means no control.

For example, if the container process needs to use a single CPU for 0.2 seconds every 1 second, you can set cpu-period to 1000000 (that is, 1 second) and cpu-quota to 200000 (0.2 seconds). Of course, in a multi-core situation, if the container process is allowed to fully occupy two CPUs, you can set cpu-period to 100000 (that is, 0.1 seconds) and cpu-quota to 200000 (0.2 seconds).

Use the container image produced in this article to test, cpu-period and cpu-quota parameters.

In the 4-core system used in this article, if you want cpuusetest to occupy two cores, how to configure it? As can be seen from the above analysis, if you set cpu-period to 100000, and expect to occupy 4 cores, you need to set cpu-quota to 4*100000, and if you expect to occupy one core, you can set it to 2* 100000. Let’s test it below:

docker run --name cpuuse -d --cpu-period=100000 --cpu-quota=200000 fangfenghua/cpuusetest 4 top - 07:46:31 up 1:46, 2 users, load average: 0.16, 0.21, 0.51 Tasks: 168 total, 2 running, 142 sleeping, 24 stopped, 0 zombie %Cpu(s): 47.8 us, 0.1 sy, 0.0 ni, 51.9 id, 0.1 wa, 0.0 hi, 0.1 si, 0.0 st KiB Mem : 1017144 total, 364724 free, 227816 used, 424604 buff/cache KiB Swap: 1040380 total, 1040284 free, 96 used. 631052 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 21766 root 20 0 3048 724 464 R 193.3 0.1 1:00.37 full_cpu 1 root 20 0 41620 4088 2380 S 0.0 0.4 0:03.13 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.05 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:00.52 ksoftir top - 07:47:17 up 1:47, 2 users, load average: 0.47, 0.26, 0.51 Tasks: 172 total, 3 running, 144 sleeping, 25 stopped, 0 zombie %Cpu(s): 99.6 us, 0.1 sy, 0.0 ni, 0.3 id, 0.0 wa, 0.0 hi, 0.1 si, 0.0 st KiB Mem : 1017144 total, 358760 free, 233292 used, 425092 buff/cache KiB Swap: 1040380 total, 1040284 free, 96 used. 625180 avail Mem docker run --name cpuuse -d --cpu-period=100000 --cpu-quota=400000 fangfenghua/cpuusetest 4 PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 21976 root 20 0 3048 724 456 R 398.3 0.1 0:16.81 full_cpu 21297 root 20 0 0 0 0 S 0.3 0.0 0:00.08 kworker/0:2 1 root 20 0 41620 4088 2380 S 0.0 0.4 0:03.19 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.05 kthreadd
Copy after login

Use the above two parameters to set precise control of the CPU. There is also a parameter cpu-share, which is a relative value. Suppose you set the cpu-share of container A to 1536 and the cpu-share of container B to 512. So, what is the cpu usage before container B starts?

top - 07:56:10 up 1:56, 2 users, load average: 0.75, 0.36, 0.50 Tasks: 153 total, 3 running, 140 sleeping, 10 stopped, 0 zombie %Cpu(s): 99.7 us, 0.1 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st KiB Mem : 1017144 total, 436300 free, 155616 used, 425228 buff/cache KiB Swap: 1040380 total, 1040284 free, 96 used. 703544 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 22216 root 20 0 3048 720 456 R 399.3 0.1 0:55.03 full_cpu 1 root 20 0 41620 4088 2380 S 0.0 0.4 0:03.29 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.05 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:00.54 ksoftirqd/0
Copy after login

Start container B:

top - 07:57:09 up 1:57, 2 users, load average: 3.55, 1.16, 0.76 Tasks: 162 total, 4 running, 148 sleeping, 10 stopped, 0 zombie %Cpu(s): 99.6 us, 0.2 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st KiB Mem : 1017144 total, 428772 free, 158304 used, 430068 buff/cache KiB Swap: 1040380 total, 1040284 free, 96 used. 700444 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 22216 root 20 0 3048 720 456 R 305.7 0.1 4:40.78 full_cpu 22336 root 20 0 3048 720 460 R 95.3 0.1 0:09.02 full_cpu 1 root 20 0 41620 4088 2380 S 0.0 0.4 0:03.31 systemd
Copy after login

It is not difficult to see from the above test results. When setting the relative value, before container B starts, container A still occupies full CPU, but after container B starts, container occupies 3/4 and container B occupies 1/4.

There is also a parameter cpu-sets, which specifies the core used by the container. Use the above test container to test, specify the container to use 0, 3 cores:

docker run --name cpuuse -d --cpuset-cpus=0,3 fangfenghua/cpuusetest 4
Copy after login

0, 3 core occupancy:

[root@localhost src]# dstat -c -C 0,3 -------cpu0-usage--------------cpu3-usage------ usr sys idl wai hiq siq:usr sys idl wai hiq siq 25 9 66 0 0 0: 12 1 87 0 0 0 100 0 0 0 0 0:100 0 0 0 0 0 99 0 0 0 0 1:100 0 0 0 0 0 99 1 0 0 0 0: 99 1 0 0 0 0 100 0 0 0 0 0:100 0 0 0 0 0 100 0 0 0 0 0:100 0 0 0 0 0
Copy after login

1, 2 core occupancy:

[root@localhost src]# dstat -c -C 1,2 -------cpu1-usage--------------cpu2-usage------ usr sys idl wai hiq siq:usr sys idl wai hiq siq 21 8 71 0 0 0: 10 1 89 0 0 0 0 0 100 0 0 0: 0 0 100 0 0 0 0 0 100 0 0 0: 0 0 100 0 0 0 0 0 100 0 0 0: 0 0 100 0 0 0 0 0 100 0 0 0: 0 0 100 0 0 0
Copy after login

More For more Linux articles, please visit theLinux Tutorialcolumn to learn!

The above is the detailed content of Docker cpu limit analysis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!