我想使用cgroup.
到目前为止,我已经完成了以下工作:
我安装了一些软件包:
sudo apt-get install cgroup-bin cgroup-lite cgroup-tools cgroupfs-mount libcgroup1
Run Code Online (Sandbox Code Playgroud)然后我创建了/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;
ns = /cgroup/ns;
blkio = /cgroup/blkio;
}
group limitcpu{
cpu {
cpu.shares = 400;
}
}
group limitmem{
memory {
memory.limit_in_bytes = 512m;
}
}
group limitio{
blkio {
blkio.throttle.read_bps_device = "252:0 2097152";
}
}
group browsers{
cpu {
cpu.shares = 200;
}
memory {
memory.limit_in_bytes = 128m;
}
}
Run Code Online (Sandbox Code Playgroud)
根据此处的指南,假设配置文件位于同一位置并在 Ubuntu 上使用与在 CentOS 上相同的语法。
然后根据该指南,我需要启动cgconfig service. 我试过:
sudo service cgconfig restart
Run Code Online (Sandbox Code Playgroud)
但是,哦不!文件丢失!:
Failed to restart cgconfig.service: Unit cgconfig.service not found.
Run Code Online (Sandbox Code Playgroud)经过一番思考和搜索之后,我尝试了:
? cgconfig.service
Loaded: not-found (Reason: No such file or directory)
Active: inactive (dead)
Run Code Online (Sandbox Code Playgroud)所以看起来我的系统上根本没有任何 cgconfig 服务!
我使用以下方法搜索它:
sudo aptitude search cgconfig
Run Code Online (Sandbox Code Playgroud)
然而,没有cgconfig被发现。
如何cgconfig在我的 Ubuntu 16.04 上安装?
Rdr*_*drk 15
好的,我设法自己做和你做的一样的事情,但有一些变化:
1)我安装了相同的实用程序:
sudo apt-get install cgroup-bin cgroup-lite cgroup-tools cgroupfs-mount libcgroup1
Run Code Online (Sandbox Code Playgroud)
2)我编辑了这样的conf文件:
/etc/init/cgroup-lite.conf
description "mount available cgroup filesystems"
author "Serge Hallyn <serge.hallyn@canonical.com>"
start on mounted MOUNTPOINT=/sys/fs/cgroup
pre-start script
test -x /bin/cgroups-mount || { stop; exit 0; }
test -d /sys/fs/cgroup || { stop; exit 0; }
/bin/cgroups-mount
cgconfigparser -l /etc/cgconfig.conf
end script
post-stop script
if [ -x /bin/cgroups-umount ]
then
/bin/cgroups-umount
fi
end script
Run Code Online (Sandbox Code Playgroud)
/etc/cgconfig.conf
# Since systemd is working well, this section may not be necessary.
# Uncomment if you need it
#
# mount {
# cpuacct = /cgroup/cpuacct;
# memory = /cgroup/memory;
# devices = /cgroup/devices;
# freezer = /cgroup/freezer;
# net_cls = /cgroup/net_cls;
# blkio = /cgroup/blkio;
# cpuset = /cgroup/cpuset;
# cpu = /cgroup/cpu;
# }
group limitcpu{
cpu {
cpu.shares = 400;
}
}
group limitmem{
memory {
memory.limit_in_bytes = 512m;
}
}
group limitio{
blkio {
blkio.throttle.read_bps_device = "252:0 2097152";
}
}
group browsers {
cpu {
# Set the relative share of CPU resources equal to 25%
cpu.shares = "256";
}
memory {
# Allocate at most 512M of memory to tasks
memory.limit_in_bytes = "512m";
# Apply a soft limit of 512 MB to tasks
memory.soft_limit_in_bytes = "384m";
}
}
group media-players {
cpu {
# Set the relative share of CPU resources equal to 25%
cpu.shares = "256";
}
memory {
# Allocate at most 256M of memory to tasks
memory.limit_in_bytes = "256m";
# Apply a soft limit of 196 MB to tasks
memory.soft_limit_in_bytes = "128m";
}
}
cgconfigparser -l /etc/cgconfig.conf
Run Code Online (Sandbox Code Playgroud)
和 /etc/cgrules.conf
user:process subsystems group
[user]:/usr/lib/chromium-browser/chromium-browser cpu,memory browsers
[user]:/usr/bin/clementine cpu,memory media-players
Run Code Online (Sandbox Code Playgroud)
这是一个示例,使用您的用户名而不是 [user]。您可以添加需要限制的应用程序,并定义是希望它们受到 CPU 限制、内存限制还是两者都受到限制。
我编辑了GRUB_CMDLINE_LINUX_DEFAULT以下行/etc/default/grub:
GRUB_CMDLINE_LINUX_DEFAULT="cgroup_enable=memory swapaccount=1"
Run Code Online (Sandbox Code Playgroud)
更新它:
sudo update-grub
Run Code Online (Sandbox Code Playgroud)
3)最后重新启动以应用更改。
这就是我的工作方式。在此之前,我经常遇到多任务 OOM - 使用 Chromium-browser、clementine、sublime-text 和其他使用大量资源的应用程序 - 现在它们运行流畅,我可以更好地进行多任务处理。
我真的希望这可以帮助其他人。
目前使用Ubuntu 16.04 LTS和Intel Dual Core 2.6 Ghz和4 Gb。内存。
小智 10
我遇到了同样的问题。当前的 Ubuntu 发行版中似乎没有内置服务来加载 cgroup 配置文件。
您会在/usr/share/doc/cgroup-tools/examples/cgconfig和 /usr/share/doc/cgroup-tools/examples/cgred找到一些(损坏的?)示例初始化脚本 。
要手动加载您可以使用的配置文件
# Loads /etc/cgconfig.conf
cgconfigparser -l /etc/cgconfig.conf
# Loads /etc/cgrules.conf
cgrulesengd -vvv --logfile=/var/log/cgrulesengd.log
Run Code Online (Sandbox Code Playgroud)
作为一个穷人的解决方案,我自己写了一个 init 脚本,在系统启动时加载这两个文件。
# Loads /etc/cgconfig.conf
cgconfigparser -l /etc/cgconfig.conf
# Loads /etc/cgrules.conf
cgrulesengd -vvv --logfile=/var/log/cgrulesengd.log
Run Code Online (Sandbox Code Playgroud)
将此文件保存在/etc/init.d/cgconf并使用
# make the script executable
chmod 755 /etc/init.d/cgconf
# register the service
update-rc.d cgconf defaults
# start the service
service cgconf start
# check the status
service cgconf status
Run Code Online (Sandbox Code Playgroud)