Use of simple cgroup

1 Introduction

         Full CGroup Linux Linux Control Group, the Linux kernel is a function for limiting, controlling a separation process resource group (e.g., CPU, memory, disk input and output, etc.). The project was first initiated by Google engineers in 2006 (mainly Paul Menage and Rohit Seth), the earliest name for the process container (process containers). In 2007, as in the Linux kernel, container (container) the term is too wide, in order to avoid confusion, is renamed cgroup, and is incorporated into the version of the kernel to 2.6.24. Then, the other began his development. 

         Linux CGroupCgroup the system can allocate resources for running tasks (processes) user-defined groups - such as a combination of CPU time, system memory, network bandwidth, or these resources. Cgroup can monitor configuration, cgroup refused access to certain resources, even in a running system dynamically configured cgroup.

1.1 The main function

(1) limit the use of resources, such as memory caching restrictions limit the use and file systems.

(2) priority control, CPU utilization and disk IO throughput.

(3) some of the audit or some of the statistics, the main purpose of billing.

(4) Suspend the process resumes execution process.

1.2 cgroups subsystem

Cgroups (Control Groups) based on restriction process, rather than the user , so the process is the same for super-user operation;

cgroups subsystem:

1.blkio limit per block input and output control device. For example: disks, CDs and usb.
2.cpu restrict the use of cpu proportion
3.cpuacct cpu resources to generate reports cgroup task.
When 4.cpuset multi-core cpu cgroup tasks assigned to separate cpu cpu and memory binding process and reduce context switching memory available in the nearest access memory
5.devices allow or deny access to the device.
6.freezer pause and resume cgroup task.
7.memory set memory limits, and memory resources to produce the report.
8.net_cls network packets cgroups may be labeled in the process, the module can then be used tc (traffic control) control data packets.
9.net_prio - This subsystem is used to design the priority of network traffic.

10.ns can make different cgroups following process uses a different namespace.

11.hugetlb - This subsystem are targeted primarily at HugeTLB system limit, which is a large page file system.

bind to a specific process cpu audit

#yum installation
#yum install libcgroup libcgroup-tools  numactl  -y
# Get the number of CPU threads
#grep 'processor' / proc / cpuinfo | sort -u | wc -l
# Get CPU NUMA memory node information
#numactl --hardware
or
#lscpu | grip NUMA

 

# 4 Create a Group Policy setting small nuclear
cgcreate -g cpuset:small
cgset -r cpuset.cpus=0-3 small
cgset -r cpuset.mems=0 small

# Create a Group Policy setting large 8-core 
cgcreate -g cpuset:large
cgset -r cpuset.cpus=0-7 large
cgset -r cpuset.mems=0 large

# Default cpuset.cpus and cpuset.mems are empty; need to set these two values, wherein the reference value of the node number numa mems, the above is found by numactl --hardware 0

Note :

1.cpuset subsystem are two mandatory parameters are defined, cpuset.cpus and cpuset.mems.

cpuset.cpus (mandatory)

This allows the CPU to specify cgroup tasks access. This is a comma-separated list with the format ASCII, use dashes ( "-") on behalf of the range.

cpuset.mems (mandatory)

This allows the specified node memory cgroup tasks accessible. This is a comma-separated list with the format ASCII, use dashes ( "-") on behalf of the range.

2. For multi-threaded program, which does not take effect;
3. have configured in / SYS / FS / cgroup / cpuset directory will appear smal, large directory.

# Run the command

#cgexec -g cpuset: small program run the command
#cgexec -g cpuset: large program run the command

Example # 

# Write a script cpu consumption

we t1.sh
#/bin/bash
x=0
while [ True ];do
    x=$x+1
done;

# Limit the program to run on a fixed number of cpu core                           

#cgexec -g cpuset:small sh t1.sh &

# Run the top command, and then press 1 , you can see the number of cases per cpu core

3 process cpu usage restrictions

3.1 Example 1

#yum installation
#yum install libcgroup libcgroup-tools  numactl  -y
#systemctl status cgconfig.service 
#systemctl start cgconfig.service
# View cgroup mount point (centos7.7), can be found here cgroups subsystem
#lssubsys -am

# Create isolation group
#cd /sys/fs/cgroup/cpu
#mkdir cpu_test
Description : The directory is created automatically generates the following files (Note: The directory can not manually deleted, if you do not join the boot from Kai, will disappear after reboot )
#ls cpu_test/

# Run script of a cpu consumption
#vi t1.sh
#/bin/bash
x=0
while [ True ];do
    x=$x+1
done;
#chmod +x t1.sh
#sh t1.sh  &

#Change parameters
# echo 20000 > /sys/fs/cgroup/cpu/cpu_test/cpu.cfs_quota_us
Note: The default -1 is not limited, now replaced 20,000, to 20,000 is cpu.cfs_quota_us respect cpu.cfs_period_us 100000 20%; cpu utilization restrictions can be appreciated in 20%;

# Find the process number increased cpu tasks inside, looking at top, cpu utilization down quickly
#echo 2938 >> /sys/fs/cgroup/cpu/cpu_test/tasks

Description : In the above case, if the three are written to the process pid tasks, there will be three cases the process of sharing 20% cpu;

3.2 Example 2

Description : This part supplemental Example 1, using the command to create a control group;

# Create a control group
#cgcreate -g cpu:/g1
说明:这个命令会创建/sys/fs/cgroup/cpu/g1目录出来,在这个目录下有各种cgroup cpu的配置文件;

#设置CPU限制参数
#cgset -r cpu.cfs_quota_us=20000 g1
#查看是否设置成功
#cgget -r cpu.cfs_quota_us g1

#启动进程,通过top可以看到cpu占用100%,同时拿到进程pid:2231
#cat t1.sh
#/bin/bash
x=0
while [ True ];do
    x=$x+1
done;
#sh  /tmp/t1.sh  &

#将此进程加入控制群组g1
#cgclassify -g cpu:/g1 2231
#cat /sys/fs/cgroup/cpu/g1/tasks
通过top看,cpu消耗变成了20%
注意:最好将一个进程写在一个控制组内;将多个进程写在一个控制组会共享cpu限制;
如:在一个控制组内写入3个进程,cpu限制使用为20%,每个进程大概会占6%左右;

4 限制内存使用量

#创建控制群组g2
#cgcreate -g memory:g2

#查看默认内存是没有限制的
cgget -r memory.limit_in_bytes g2

#限制内存只有1GB
cgset -r memory.limit_in_bytes=1073741824 g2

#执行/tmp/highmemory.sh,进程号是21127 
#vi /tmp/highmem.sh
#/bin/bash
x="a"
while [ True ];do
    x=$x$x
done;

#将highmemory.sh进程加入g2的控制
#cgclassify -g memory:g2 21127

限制磁盘读取速度

#要控制/dev/sdb的磁盘,通过下述命令查到磁盘驱动号8,16
#ls -l /dev/sdb

#创建控制组,设定8,16磁盘有1MB的读取限制
#cgcreate -g blkio:g1 
#cgset -r blkio.throttle.read_bps_device='8:16 10485760' g1

#启动读取测试命令,拿到pid 14468
#dd if=/dev/sdb of=/dev/null

#通过命令可以看到对磁盘读写速度的消耗
#iotop

#将进程加入g1控制组后,读取速度被限制
#cgclassify -g blkio:g1 14468

-------------------------------------------------------------------------------------------------------------------------

参考链接:

https://www.cnblogs.com/menkeyi/p/10941843.html

https://www.jianshu.com/p/dc3140699e79

https://blog.csdn.net/micklf/article/details/60868649

https://blog.csdn.net/kwame211/article/details/78730705  

 

Guess you like

Origin www.cnblogs.com/llwxhn/p/12558577.html