Use cgroups to manage CPU resources

Source: Internet
Author: User

This article describes how to use cgroups to manage CPU resources. Let's talk about the CPU usage of the control process. When running multiple programs on a machine that may consume a large amount of resources, we do not want a program to occupy all the resources, so that other programs cannot run normally, or the system is suspended for maintenance. At this time, cgroups can be used to control the resource usage of the process. CPU resources are described here.

In cgroups, you can use CPU. cfs_period_us and CPU. cfs_quota_us to limit the CPU time that all processes in this group can use in a unit of time. The CFS is short for the completely fair scheduler. CPU. cfs_period_us is the time period. The default value is 100000, that is, milliseconds. CPU. cfs_quota_us indicates the CPU time that can be used during this period. The default value is-1, which means no limit.

Run a CPU-consuming Program

# echo ‘while True: pass‘|python &[1] 1532

The process occupies 100% of the CPU.

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND 77569 root      20   0  112m 3684 1708 R 99.6  0.7   0:30.42 python...

Then let's make some restrictions on this process. Modify the restrictions of the/Foo control group and add the process to the group.

echo 50000 >/sys/fs/cgroup/cpu/foo/cpu.cfs_quota_usecho 77569 >/sys/fs/group/cpu/foo/tasks

As you can see, you only need to write the corresponding file to modify the settings. You only need to write the PID to the tasks file to add the process to the cgroup. Set CPU. cfs_quota_us to 50000, Which is 100000 relative to CPU. cfs_period_us. Next, let's take a look at the effect.

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND 77569 root      20   0  112m 3684 1708 R 50.2  0.7   5:00.31 python...

We can see that the CPU usage of a process has been successfully limited to 50%. The tested virtual machine has only one core. In multi-core scenarios, the values are different. In addition, cfs_quota_us can be larger than cfs_period_us, which is mainly used for multi-core scenarios. When there are n cores, the process in a control group can naturally use up to n times the CPU time.

Another set of CPU. rt_period_us and CPU. rt_runtime_us correspond to the restrictions of real-time processes, which may not be used at ordinary times.

In the CPU subsystem, CPU. Stat is the statistics of resource restrictions made using the previous method. Nr_periods and nr_throttled are the total and restricted periods. Throttled_time is the total CPU usage time that is throttled by the control group.

There is also a CPU. shares, which is also used to limit CPU usage. However, it is quite different from CPU. cfs_quota_us and CPU. cfs_period_us. CPU. shares does not limit the absolute CPU time that a process can use, but controls the quota between groups. For example

/cpu/cpu.shares : 1024/cpu/foo/cpu.shares : 2048

When the processes in both groups are running at full capacity, the CPU occupied by the processes in/foo is twice that of the processes in. If you create another/Foo/bar CPU. shares is also 1024 and there are also processes running at full capacity, the CPU usage ratio of/,/Foo and/Foo/bar is. As mentioned above, all of them are full. If the processes in other control groups are idle, the processes in a group can use all the CPUs. It can be seen that this method can make full use of resources while ensuring fairness.

In addition, you can specify the CPU cores that a process can use. The cpuset subsystem is the CPU core and Memory nodes that can be used by processing processes, and other related configurations. Many configurations in this section are related to NUMA. Cpuset. CPUs and cpuset. MEMS are used to restrict CPU cores and Memory nodes that can be used by processes. In the two parameters, the CPU core and Memory nodes are represented by IDS and separated by commas. For example, 0, 1, and 2. You can also use "-" to indicate the range, for example, 0-3. The two can be used together. For example, "0-, 7 ". Before adding processes, cpuset. CPUs and cpuset. MEMS must be set at the same time and must be compatible; otherwise, errors may occur. For example

# 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

# cat /proc/<pid>/status|grep ‘_allowed_list‘

Verify the effect.

In addition to resource usage restrictions, cgroups also provides resource statistics. You can use it for cloud computing billing. There is a cpuacct subsystem dedicated for CPU resource statistics. Cpuacct. Stat collects statistics on the CPU usage of process user and kernel states in the control group. The unit is user_hz, that is, the number of jiffies and CPU ticking. The number of tick answers per second can be usedgetconf CLK_TCKUsually 100. Divide the value you see into seconds.

Cpuacct. Usage and cpuacct. usage_percpu are the CPU time consumed by processes in the control group, measured in nanoseconds. The latter is counted by CPU.


This article is from the "persistence is victory" blog. For more information, contact the author!

Use cgroups to manage CPU resources

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.