前言
记录一下通过cgroup控制进程的CPU配额的实验
一、centos7.9操作系统
环境准备
yum install -y libcgroup-tools.x86_64
service cgconfig start
service cgconfig status
准备测试用脚本
cat > test.sh <<EOF
x=0
while [ True ];do
x=$x+1
done;
EOF
执行脚本,获取进程号,使用top查看进程使用率
chmod a+x test.sh
./test.sh &
开始设置限制
mkdir /sys/fs/cgroup/cpu/test
cd /sys/fs/cgroup/cpu/test
#17281为脚本进程号
echo 17281 > cgroup.procs
# 50000为使用率上限50%
echo "50000" > cpu.cfs_quota_us
使用top查看发现进程使用率已经下降至50%
二、ubuntu20.04
环境准备
apt install -y cgroup-tools
准备测试用脚本
cat > test.sh <<EOF
x=0
while [ True ];do
x=$x+1
done;
EOF
执行脚本,获取进程号,使用top查看进程使用率
chmod a+x test.sh
./test.sh &
开始设置限制
mkdir /sys/fs/cgroup/cpu/test
cd /sys/fs/cgroup/cpu/test
#3557为脚本进程号
echo 3557 > cgroup.procs
# 50000为使用率上限50%
echo "50000" > cpu.cfs_quota_us
使用top查看发现进程使用率已经下降至50%