First, install the utilities for managing cgroups; you need to install the {{AUR|libcgroup}} package from the [[AUR]].

First, install the utilities for managing cgroups; you need to install the {{AUR|libcgroup}} package from the [[AUR]].

−

{{Note|Cgroups can be handled natively by [[systemd]], see [http://www.freedesktop.org/software/systemd/man/systemd.cgroup.html this] for more information.}}

+

{{Note|Cgroups can be handled natively by [[systemd]], see [http://www.freedesktop.org/wiki/Software/systemd/ControlGroupInterface/ this] for more information.}}

== Simple usage ==

== Simple usage ==

Revision as of 06:17, 7 December 2013

cgroups (aka control groups) is a Linux kernel feature to limit, police and account the resource usage of certain processes (actually process groups). Compared to other approaches like the 'nice' command or /etc/security/limits.conf, cgroups are more flexible.

Control groups can be used in multiple ways:

create and manage them on the fly using tools like cgcreate, cgexec, cgclassify etc

Contents

Installing

First, install the utilities for managing cgroups; you need to install the libcgroupAUR package from the AUR.

Note: Cgroups can be handled natively by systemd, see this for more information.

Simple usage

Ad-hoc groups

One of the powers of cgroups is that you can create "ad-hoc" groups on the fly. In fact, you can even grant the privileges to create custom groups to regular users. Run this as root (replace $USER with your user name and groupname with the name you want to give to the cgroup):

sudo cgcreate -a $USER -g memory,cpu:groupname

That's it! Now all the tunables in the group groupname are writable by your user:

Note: Comments should begin at the start of a line! The # character for comments must appear as the first character of a line. Else, cgconfigparser will have problem parsing it but will only report cgroup change of group failed as the error.

Useful examples

Matlab

Matlab does not have any protection against taking all your machine's memory or CPU. Launching a large calculation can thus trash your system. Here's the what I have put in /etc/cgconfig.conf to protect from this (replace $USER with your username):

Note: Don't forget to change $USER to the actual username Matlab will be run by!!!

This cgroup will bind Matlab to cores 0 to 5 (I have 8, so Matlab will only see 6) and cap its memory usage to 5 GiB. The "cpu" resource constrain can also be defined to prevent CPU usage, but I find the "cpuset" constrain to be sufficient.