简介
CGroup 提供了一个 CGroup 虚拟文件系统,作为进行分组管理和各子系统设置的用户接口。要使用 CGroup,必须挂载 CGroup 文件系统。这时通过挂载选项指定使用哪个子系统
安装
yum install libcgroup libcgroup-tools
启动
sudo service cgconfig start/stop
start up at the boot time.
chkconfig cgroup on
/etc/cgconfig.conf对mysql限制
group foo {
cpu {
cpu.cfs_quota_us = 50000;
cpu.cfs_period_us = 100000;
}
cpuset {
cpuset.cpus = "3";
cpuset.mems = "0";
}
cpuacct{
}
memory {
memory.limit_in_bytes=104857600;
memory.swappiness=0;
# memory.max_usage_in_bytes=104857600;
# memory.oom_control=0;
}
blkio {
blkio.throttle.read_bps_device="8:0 524288";
blkio.throttle.write_bps_device="8:0 524288";
}
}
修改cgrules.conf文件挂载服务,cgroup 的文件系统默认会挂载到 /sys/fs/cgroup/ 目录下。
# /etc/cgrules.conf
#The format of this file is described in cgrules.conf(5)
#manual page.
#
# Example:
#<user> <controllers> <destination>
#@student cpu,memory usergroup/student/
#peter cpu test1/
#% memory test2/
test cpu foo
临时对cpu进行限制(重启失效)
1.运行cpu消耗shell脚本foo
while [ True ];do
x='a'
done;
2.使用top命令查看pid为30791进程cpu占用率接近95%
![](https://img.haomeiwen.com/i1746439/224890a73694e978.png)
3.使用cgroup对pid为30791cpu进行限制到50%
mkdir -p /sys/fs/cgroup/cpu/foo/
echo 50000 > /sys/fs/cgroup/cpu/foo/cpu.cfs_quota_us
echo 100000 > /sys/fs/cgroup/cpu/foo/cpu.cfs_period_us
echo 30791 > /sys/fs/cgroup/cpu/foo/tasks
4.使用top命令查看pid为30791cpu占用率接近50%
![](https://img.haomeiwen.com/i1746439/b7eb422fedda3fd2.png)
网友评论