How to use cgroups to manage CPU resources under Linux

  

Through the introduction, we learned that cgroups in Linux can be used to limit cpu resources, so how to achieve it? The following small series will introduce you how to use cgroups to limit cpu resources in Linux.

This time to talk about how to manage cpu resources cgroups. Let me talk about the cpu use of the control process. When running multiple programs that can consume a lot of resources on one machine, we don't want a program to occupy all the resources, causing other programs to fail to run properly, or causing the system to fail to maintain. At this time, cgroups can be used to control the resource consumption of the process. Here is the cpu resource.

In cgroups, you can use cpu.cfs_period_us and cpu.cfs_quota_us to limit the amount of CPU time that all processes in the group can use per unit of time. Here cfs is an abbreviation for Full Fair Scheduler. Cpu.cfs_period_us is the time period. The default is 100000, which is 100 milliseconds. Cpu.cfs_quota_us is the cpu time that can be used during this period. The default is -1, which means no limit.

Running a CPU-consuming program

The code is as follows:

# echo ‘while True: pass’| Python &

[1] 1532

top You can see that this process accounts for 100% of the cpu

code is as follows:

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

1532 root 20 0 112m 3684 1708 R 99.6 0.7 0:30.42 python

. . .

Then come and limit the process. First modify the /foo control group limit, then add the process.

The code is as follows:

echo 50000 》/sys/fs/cgroup/cpu/foo/cpu.cfs_quota_us

echo 1532 》/sys/fs/group/cpu/Foo/tasks

It can be seen that modifying the settings only needs to write the corresponding file. To add the process to the cgroup, just write the pid to the tasks file. Here cpu.cfs_quota_us is set to 50000, which is 50% or 50% relative to cpu.cfs_period_us. Then look at the effect.

The code is as follows:

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

1532 root 20 0 112m 3684 1708 R 50.2 0.7 5:00.31 python< Br>

. . .

As you can see, the CPU usage of the process has been successfully limited to 50%. Here, the virtual machine being tested has only one core. In the case of multicore, the values ​​seen will be different. In addition, cfs_quota_us can also be larger than cfs_period_us, which is mainly for multi-core situations. With n cores, processes in one control group can naturally use up to n times the CPU time.

These two values ​​are limited in the cgroups hierarchy, and the underlying resources cannot exceed the upper level. Specifically, the value of cpu.cfs_period_us in the lower layer cannot be smaller than the value in the upper layer, and the value of cpu.cfs_quota_us cannot be greater than the value in the upper layer.

Another set of cpu.rt_period_us, cpu.rt_runtime_us corresponds to the real-time process limit, usually there will be no chance to use.

In the cpu subsystem, cpu.stat is the resource limit statistics done by the previous method. Nr_periods, nr_throttled are the cycles that have passed in total, and the periods in which they are restricted. The throttled_time is the total CPU usage time that is lost by the control group.

There is also a cpu.shares, which is also used to limit the use of cpu. But there is a big difference with cpu.cfs_quota_us and cpu.cfs_period_us. Cpu.shares is not limiting the absolute cpu time that a process can use, but rather controlling the quota between groups. For example, the

code is as follows:

/cpu/cpu.shares : 1024

/cpu/foo/cpu.shares : 2048

Then when two When the processes in the group are running at full capacity, the processes in /foo can take up twice as many CPUs as the processes in /. If you build a /foo/bar cpu.shares is also 1024, and there are also processes running at full capacity, then the /, /foo, /foo/bar cpu occupancy ratio is 1:2:1. The previous one is about the situation where each is full. If the processes in other control groups are idle, then the process of a certain group can completely use all the CPUs. It can be seen that under normal circumstances, this method can make full use of resources while ensuring fairness.

In addition, you can limit which cpu cores your process can use. The cpuset subsystem is the cpu core and memory nodes that can be used by the processing process, as well as other related configurations. Many of the configurations in this section are related to NUMA. Among them cpuset.cpus, cpuset.mems is used to limit the cpu core and memory nodes that the process can use. The cpu core and memory nodes of these two parameters are represented by id, separated by “, & rdquo;. For example, 0,1,2. You can also use “-” to indicate a range, such as 0-3. Both can be combined. Such as “0-2,6,7”. Before adding a process, cpuset.cpus and cpuset.mems must be set at the same time, and they must be compatible, otherwise an error will occur. For example, the

code is as follows:

# echo 0 》/sys/fs/cgroup/cpuset/foo/cpuset.cpus

# echo 0 》/sys/fs/cgroup /cpuset/foo/cpuset.mems

Thus, processes in /foo can only use cpu0 and memory node 0. Use the

code as follows:

# cat /proc/"pid"/status| Grep ‘_allowed_list’

can verify the effect.

In addition to restricting the use of resources, cgroups also has the function of resource statistics. You can use it for billing for cloud computing. There is a cpuacct subsystem dedicated to cpu resource statistics. Cpuacct.stat counts the CPU usage of the process user mode and kernel mode in the control group. The unit is USER_HZ, which is the jiffies and cpu ticks. The number of ticks per second can be obtained with getconf CLK_TCK, which is usually 100. Dividing the value you see by this value translates to seconds.

cpuacct.usage and cpuacct.usage_percpu are cpu times consumed by processes in this control group, in nanoseconds. The latter is divided into cpu statistics.

PS 2014-4-22

Found in SLES 11 sp2, sp3, corresponding kernel version 3.0.13, 3.0.76, for the cpu subsystem, write pid to cgroup.procs It won't actually take effect, it's ok to write tasks. In other environments, no later version is found on the kernel or later.

The above is how to use cgroups to control cpu resources in Linux. If you run a program that uses too many resources, you can use cgroups. Control cpu resources, give it a try.

Copyright © Windows knowledge All Rights Reserved