|
CGroup 介绍
CGroup 是 Control Groups 的缩写,是 Linux 内核提供的一种可以限制、记录、隔离进程组 (process groups) 所使用的物力资源 (如 cpu memory i/o 等等) 的机制。2007 年进入 Linux 2.6.24 内核,CGroups 不是全新创造的,它将进程管理从 cpuset 中剥离出来,作者是 Google 的 Paul Menage。CGroups 也是 LXC 为实现虚拟化所使用的资源管理手段。
CGroup 功能及组成
CGroup 是将任意进程进行分组化管理的 Linux 内核功能。CGroup 本身是提供将进程进行分组化管理的功能和接口的基础结构,I/O 或内存的分配控制等具体的资源管理功能是通过这个功能来实现的。这些具体的资源管理功能称为 CGroup 子系统或控制器。CGroup 子系统有控制内存的 Memory 控制器、控制进程调度的 CPU 控制器等。运行中的内核可以使用的 Cgroup 子系统由/proc/cgroup 来确认。
CGroup 提供了一个 CGroup 虚拟文件系统,作为进行分组管理和各子系统设置的用户接口。要使用 CGroup,必须挂载 CGroup 文件系统。这时通过挂载选项指定使用哪个子系统。
Cgroups提供了以下功能:
1.限制进程组可以使用的资源数量(Resource limiting )。比如:memory子系统可以为进程组设定一个memory使用上限,一旦进程组使用的内存达到限额再申请内存,就会出发OOM(out of memory)。
2.进程组的优先级控制(Prioritization )。比如:可以使用cpu子系统为某个进程组分配特定cpu share。
3.记录进程组使用的资源数量(Accounting )。比如:可以使用cpuacct子系统记录某个进程组使用的cpu时间
4.进程组隔离(Isolation)。比如:使用ns子系统可以使不同的进程组使用不同的namespace,以达到隔离的目的,不同的进程组有各自的进程、网络、文件系统挂载空间。
5.进程组控制(Control)。比如:使用freezer子系统可以将进程组挂起和恢复。
1. 安装Cgroup
yum install libcgroup-tools libcgroup-pam libcgroup
2. /etc/cgconfig.conf
group users_normal_limit{
cpu{
# 限制CPU使用率为10%
cpu.cfs_period_us=100000;
cpu.cfs_quota_us=10000;
}
memory{
# 最高总使用内存为100M
memory.limit_in_bytes = 100M;
memory.soft_limit_in_bytes = 80M;
memory.swappiness = 0;
}
blkio{
# /dev/sda 最高磁盘读写IO为1M/秒 (8:0可通过ls -l /dev/sda命令查看得到)
blkio.throttle.read_bps_device="8:0 1024000";
blkio.throttle.write_bps_device="8:0 1024000";
}
}
检查配置是否正确
cgconfigparser -l /etc/cgconfig.conf
3. /etc/cgrules.conf
# 用户组users
@users cpu,memory,blkio users_normal_limit
4. 加入用户组
# 把jues加入受Cgroup限制的users组
usermod -a -G users jues
5. 使用配置生效
# 设置Cgroup服务开机启动
systemctl enable cgconfig
systemctl enable cgred
# 重启Cgroup服务
systemctl restart cgconfig
systemctl restart cgred
测试:
cpu测试:
[root@localhost cgroup]$ matho-primes 0 999999999 > /dev/null
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
14666 jues 20 0 9256 2536 488 R 9.6 0.2 0:04.26 matho-primes
1 root 20 0 193700 4244 1692 S 0.0 0.4 0:06.18 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.49 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
7 root rt 0 0 0 0 S 0.0 0.0 0:00.01 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0.0 0.0 0:00.68 rcu_sched
10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 lru-add-drain
11 root rt 0 0 0 0 S 0.0 0.0 0:00.08 watchdog/0
内存测试:
[root@localhost cgroup]$ dd if=/dev/zero of=1.dat bs=80M count=1
10+0 records in
10+0 records out
838860800 bytes (839 MB) copied, 22.8394 s, 36.7 MB/s
[root@localhost cgroup]$ dd if=/dev/zero of=1.dat bs=100M count=1
Killed
[root@localhost cgroup]$
磁盘IO测试:
# oflag=direct 为跳过内存缓存
[root@localhost cgroup]$ dd if=1.dat of=2.dat bs=10M count=1 oflag=direct
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 11.641 s, 901 kB/s
注意事项
1. CentOS7中默认没有安装mathomatic工具,需要自己下载编译:
# 下载mathomatic源码包
wget https://launchpad.net/mathomatic ... atic-15.6.5.tar.bz2
# 安装解压tar.bz2工具
yum install epel-release
yum install bzip2
# 解压mathomatic
tar xvf mathomatic-15.6.5.tar.bz2
cd mathomatic-15.6.5/primes
# 安装编译工具
yum install gcc
# 编译
make
# 安装
make install
2. Fedora现在没有cgred
systemctl start cgred
Failed to start cgred.service: Unit cgred.service not found.
|
|