Cgroups is a flexible Linux kernel feature to limit, police and account resources usage. A cgroup is a set of tasks for a subsystems, that is typically a resource controller.
A file system of type cgroup is mounted and all operations are run over it.
Installation
The installation can be done using any of libcgroup, cgmanager or systemd.
Usage
They can be used in multiple ways
- By accessing the cgroup filesystem directly.
- Using the cgm client (part of the cgmanager).
- Via cgcreate, cgexec and cgclassify (part of cgroup-tools).
- Via cgconfig.conf and cgrules.conf (also part of cgroup-tools).
We will use this last option, so install cgroup-tools with
1
|
|
Debian, by default, disables the memory controller, we can enable it adding the
following in /etc/default/grub
1
|
|
So, update Grub
1
|
|
Now the environment and tools are ready, we will define some “roles” in
/etc/cgconfig.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
The maximum value for cpu.shares
is 1000, so 300 is setting as 30% the usage
limit for an application with this “role”. memory.limit_in_bytes
is self
descriptive.
We will associate these “roles” and the applications in /etc/cgrules.conf
1 2 3 4 5 |
|
Now we need apply the rules with the commands
1 2 |
|
Add them in /etc/rc.local
for applying on reboot.
That’s it!
We can check if our new rules were applied using
1 2 3 4 |
|
And we can check the memory usage of a process with
1 2 3 4 |
|