Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use cgroups to restrict cpu in Linux

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use cgroups to limit cpu in Linux". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use cgroups to limit cpu in Linux".

In cgroups, you can use cpu.cfs_period_us and cpu.cfs_quota_us to limit the cpu time that all processes in the group can use per unit time. Cfs here is an abbreviation for full Fair Scheduler. Cpu.cfs_period_us is the time period, and the default is 100000, or 100 milliseconds. Cpu.cfs_quota_us is the cpu time that can be used during this period. The default is-1, that is, unlimited.

Run a program that consumes cpu

The code is as follows:

# echo 'while True: pass' | python &

[1] 1532

As you can see from top, this process accounts for 100% of cpu.

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 99.6 0.7 0:30.42 python

...

Then put some restrictions on the process. First modify the restrictions of the control group / foo, and 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

As you can see, to modify the settings, you only need to write to the corresponding file, and to add the process to the cgroup, you only need to write the pid to the tasks file in it. Here, cpu.cfs_quota_us is set to 50000, which is 100000 relative to cpu.cfs_period_us. Top again to see 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

...

As you can see, the cpu footprint of the process has been successfully limited to 50%. Here, the virtual machine tested has only one core. In the case of multicore, the values you see will be different. In addition, cfs_quota_us can also be larger than cfs_period_us, mainly for multicore cases. When there are n cores, the processes in a control group can naturally use up to n times the cpu time.

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

Another set of cpu.rt_period_us and cpu.rt_runtime_us corresponds to the limitations of real-time processes, which may not be used in normal times.

In the cpu subsystem, cpu.stat is the statistics of resource constraints done using the previous method. Nr_periods and nr_throttled are the total elapsed cycles and the restricted cycles among them. Throttled_time is the total cpu usage time that has been pinched by the control group.

There is also a cpu.shares, which is also used to limit the use of cpu. But it is quite different from cpu.cfs_quota_us and cpu.cfs_period_us. Instead of limiting the absolute cpu time that a process can use, cpu.shares controls quotas between groups. such as

The code is as follows:

/ cpu/cpu.shares: 1024

/ cpu/foo/cpu.shares: 2048

So when the processes in both groups are running at full capacity, the processes in / foo can occupy twice as much cpu as the processes in /. If the cpu.shares of / foo/bar is also 1024 and there are processes running at full capacity, the cpu utilization ratio of /, / foo, / foo/bar is 1:2:1. What I said above is the situation where they all run full. If the processes in other control groups are idle, the processes in one group can use up all the cpu. It can be seen that under normal circumstances, this approach can make more full use of resources while ensuring fairness.

In addition, you can define which cpu cores can be used by the process. The cpuset subsystem is the cpu core and memory nodes that the processing process can use, as well as other related configurations. Many of the configurations in this section are related to NUMA. Cpuset.cpus and cpuset.mems are used to limit the cpu core and memory nodes that can be used by the process. In these two parameters, the cpu core and memory nodes are represented by id, separated by ",". For example, 0meme1pence2. You can also use "-" to indicate a range, such as 0-3. The two can be used together. Such as "0-2, 6, 7". Before adding a process, cpuset.cpus and cpuset.mems must be set at the same time and 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

In this way, processes in / foo can only use cpu0 and memory node 0. Use

The code is as follows:

# cat / proc//status | grep'_ allowed_list'

The effect can be verified.

In addition to restricting the use of resources, cgroups also has the function of resource statistics. It can be used for cloud computing billing. There is a cpuacct subsystem dedicated to cpu resource statistics. Cpuacct.stat calculates the cpu usage of the process in user mode and kernel mode in the control group, in USER_HZ, that is, the number of jiffies and cpu ticks. Ticks per second can be obtained using getconf CLK_TCK, usually 100. Dividing the value you see by this value can be converted into seconds.

Cpuacct.usage and cpuacct.usage_percpu are the cpu times, in nanoseconds, consumed by processes in this control group. The latter is calculated by cpu.

P.S. 2014-4-22

It is found that writing pid to cgroup.procs will not actually take effect for cpu subsystem in SLES 11 sp2, sp3, corresponding kernel versions 3.0.13,3.0.76, but only tasks. In other environments, it is not found on higher or lower versions of the kernel.

Thank you for your reading, the above is the content of "how to use cgroups to limit cpu in Linux". After the study of this article, I believe you have a deeper understanding of how to use cgroups to limit cpu in Linux, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report