本文主要是介绍cgroup小结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
安装cgroup yum install libcgroup配置cgroup config文件
vim /etc/cgconfig.conf
mount {
cpuset = /cgroup/cpuset;
cpu = /cgroup/cpu;
cpuacct = /cgroup/cpuacct;
memory = /cgroup/memory;
devices = /cgroup/devices;
freezer = /cgroup/freezer;
net_cls = /cgroup/net_cls;
blkio = /cgroup/blkio;
}
group test1 {
memory {
memory.swappiness=0;
memory.limit_in_bytes=3M;
}
}
cpuset = /cgroup/cpuset;
cpu = /cgroup/cpu;
cpuacct = /cgroup/cpuacct;
memory = /cgroup/memory;
devices = /cgroup/devices;
freezer = /cgroup/freezer;
net_cls = /cgroup/net_cls;
blkio = /cgroup/blkio;
}
group test1 {
memory {
memory.swappiness=0;
memory.limit_in_bytes=3M;
}
}
这里只是做一个测试,限制内存为3M
配置rule
vim /etc/cgrules.conf
*:munch memory test1
PS:rule规则可理解为:
任意(*)用户执行命令为munch时候需要遵守memroy空间下test1下面的配置
munch脚本如下:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char** argv) {
int max = -1;
int mb = 0;
char* buffer;
if(argc > 1)
max = atoi(argv[1]);
while((buffer=malloc(1024*1024)) != NULL && mb != max) {
memset(buffer, 0, 1024*1024);
mb++;
printf("Allocated %d MB\n", mb);
sleep(2);
}
while (2 > 1){
sleep (100000);
}
}
#include <stdio.h>
#include <string.h>
int main(int argc, char** argv) {
int max = -1;
int mb = 0;
char* buffer;
if(argc > 1)
max = atoi(argv[1]);
while((buffer=malloc(1024*1024)) != NULL && mb != max) {
memset(buffer, 0, 1024*1024);
mb++;
printf("Allocated %d MB\n", mb);
sleep(2);
}
while (2 > 1){
sleep (100000);
}
}
开启cgconfig和cgred服务
/etc/init.d/cgconfig start
/etc/init.d/cgred start
PS:如果没有开启cgred服务,rules里面的配置不生效,可以用如下命令替换:
cgexec -g memory:test1 /root/munch
关于config文件的配置,可以参考kernel文档
https://www.kernel.org/doc/Documentation/cgroups/
这篇关于cgroup小结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!