Difference between revisions of "Cgroups"

From ArchWiki
Jump to: navigation, search
(Documentation)
m (Persistent group configuration: add template)
(8 intermediate revisions by 7 users not shown)
Line 1: Line 1:
'''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 <tt>/etc/security/limits.conf</tt>, cgroups are infinitely more flexible.
+
[[Category:Kernel]]
 +
[[Category:Virtualization]]
 +
'''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 {{ic|/etc/security/limits.conf}}, cgroups are infinitely more flexible.
  
 
Control groups can be used in multiple ways:
 
Control groups can be used in multiple ways:
Line 8: Line 10:
 
Unfortunately this feature is often underappreciated due to lack of easy "how-to" style documentation. This is an attempt of fixing the problem. :)
 
Unfortunately this feature is often underappreciated due to lack of easy "how-to" style documentation. This is an attempt of fixing the problem. :)
  
Note: I'm only learning to use cgroups as I type this, so don't take everything I say here as pure gold.
+
Note: I'm only learning to use cgroups as I type this, so do not take everything I say here as pure gold.
  
 
== Installing ==
 
== Installing ==
First, install the utilities for managing cgroups; you need the [http://aur.archlinux.org/packages.php?ID=36703 libcgroup package] from AUR. makepkg and install it as usual.
+
First, install the utilities for managing cgroups; you need to install the {{AUR|libcgroup}} package from the [[AUR]].
  
 
Next, you need to define where to mount the cgroup controller virtual file systems. Let's start with the 'cpu' and 'memory' controllers:
 
Next, you need to define where to mount the cgroup controller virtual file systems. Let's start with the 'cpu' and 'memory' controllers:
  
{{File |name=/etc/cgconfig.conf |content=
+
{{hc|/etc/cgconfig.conf |2=
 
mount {
 
mount {
 
   cpu = /mnt/cgroups/cpu;
 
   cpu = /mnt/cgroups/cpu;
Line 31: Line 33:
 
You might also want to run this at boot:
 
You might also want to run this at boot:
  
{{File |name=/etc/rc.conf |content=
+
{{hc|/etc/rc.conf |2=
 
DAEMONS=( ... '''cgconfig''' ...)
 
DAEMONS=( ... '''cgconfig''' ...)
 
}}
 
}}
Line 37: Line 39:
 
== Simple usage ==
 
== Simple usage ==
 
=== Ad-hoc groups ===
 
=== Ad-hoc groups ===
One of the powers of croups 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):
+
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):
  
 
  sudo '''cgcreate''' -a '''$USER''' -g memory,cpu:'''me'''
 
  sudo '''cgcreate''' -a '''$USER''' -g memory,cpu:'''me'''
Line 87: Line 89:
 
=== Persistent group configuration ===
 
=== Persistent group configuration ===
  
If you want your cgroups to be created at boot, you can define them in '''/etc/cgconfig.conf''' instead. For example, the "me" and "me/foo" group definitions would look like this:
+
If you want your cgroups to be created at boot, you can define them in {{ic|/etc/cgconfig.conf}} instead. For example, the "me" and "me/foo" group definitions would look like this:
  
{{File |name=/etc/cgconfig.conf |content=
+
{{hc|/etc/cgconfig.conf |2=
 
group '''me''' {
 
group '''me''' {
 
   perm {
 
   perm {
Line 117: Line 119:
  
 
== Documentation ==
 
== Documentation ==
For information on controllers and what certain switches and tunables mean, refer to [http://www.mjmwired.net/kernel/Documentation/cgroups/ kernel's  Documentation/cgroup] (or install linux-docs and see <tt>/usr/src/linux/Documentation/cgroup</tt>
+
For information on controllers and what certain switches and tunables mean, refer to [http://www.mjmwired.net/kernel/Documentation/cgroups/ kernel's  Documentation/cgroup] (or install linux-docs and see {{ic|/usr/src/linux/Documentation/cgroup}}
  
For commands and configuration files, see relevant man pages, e.g. <tt>man cgcreate</tt> or <tt>man cgrules.conf</tt>
+
For commands and configuration files, see relevant man pages, e.g. {{ic|man cgcreate}} or {{ic|man cgrules.conf}}

Revision as of 23:45, 31 October 2012

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 infinitely more flexible.

Control groups can be used in multiple ways:

  • create and manage them on the fly using tools like cgcreate, cgexec, cgclassify etc
  • the "rules engine daemon", to automatically move certain users/groups/commands to groups (/etc/cgrules.conf and /etc/rc.d/cgred)
  • through other software such as Linux Containers (LXC) virtualization

Unfortunately this feature is often underappreciated due to lack of easy "how-to" style documentation. This is an attempt of fixing the problem. :)

Note: I'm only learning to use cgroups as I type this, so do not take everything I say here as pure gold.

Installing

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

Next, you need to define where to mount the cgroup controller virtual file systems. Let's start with the 'cpu' and 'memory' controllers:

/etc/cgconfig.conf 
mount {
  cpu = /mnt/cgroups/cpu;
  memory = /mnt/cgroups/memory;
}

Then run the following to create these directories and mount the controller file systems:

/etc/rc.d/cgconfig start

Now when you list the controller directory, you should see some files (cgroup tunables) in it:

ls -l /mnt/cgroups/memory

You might also want to run this at boot:

/etc/rc.conf 
DAEMONS=( ... cgconfig ...)

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):

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

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

$ ls -l /mnt/cgroups/memory/me
total 0
-rwxrwxr-x 1 user root 0 Sep 25 00:39 cgroup.event_control
-rwxrwxr-x 1 user root 0 Sep 25 00:39 cgroup.procs
-rwxrwxr-x 1 user root 0 Sep 25 00:39 cpu.rt_period_us
-rwxrwxr-x 1 user root 0 Sep 25 00:39 cpu.rt_runtime_us
-rwxrwxr-x 1 user root 0 Sep 25 00:39 cpu.shares
-rwxrwxr-x 1 user root 0 Sep 25 00:39 notify_on_release
-rwxrwxr-x 1 user root 0 Sep 25 00:39 tasks

Cgroups are hierarchical, so you can create as many subgroups as you like. Let's say that, as a normal user, you want to run a bash shell under a new subgroup called 'foo':

cgcreate -g memory,cpu:me/foo
cgexec   -g memory,cpu:me/foo bash

There we go! Just to make sure:

$ cat /proc/self/cgroup
11:memory:/me/foo
6:cpu:/me/foo

A new subdirectory was created for this group. To limit the memory usage of all processes in this group to 10 MB, run the following:

$ echo 10000000 > /mnt/cgroups/memory/me/foo/memory.limit_in_bytes

Note that the memory limit applies to RAM use only -- once tasks hit this limit, they will begin to swap. But it won't affect the performance of other processes significantly.

Similarly you can change the CPU priority ("shares") of this group. By default all groups have 1024 shares. A group with 100 shares will get a ~10% portion of the CPU time:

$ echo 100 > /mnt/cgroups/cpu/me/foo/cpu.shares

You can find more tunables or statistics by listing the cgroup directory.

You can also change the cgroup of already running processes. To move all 'bash' commands to this group:

$ pidof bash
13244 13266
$ cgclassify -g memory,cpu:me/foo `pidof bash`
$ cat /proc/13244/cgroup
11:memory:/me/foo
6:cpu:/me/foo

Persistent group configuration

If you want your cgroups to be created at boot, you can define them in /etc/cgconfig.conf instead. For example, the "me" and "me/foo" group definitions would look like this:

/etc/cgconfig.conf 
group me {
  perm {
# who can manage limits
    admin {
      uid = $USER;
    }
# who can add tasks to this group
    task {
      uid = $USER;
    }
  }
# create this group in cpu and memory controllers
  cpu { }
  memory { }
}

group me/foo {
  cpu {
    cpu.shares = 100;
  }
  memory {
    memory.limit_in_bytes = 10000000;
  }
}

Documentation

For information on controllers and what certain switches and tunables mean, refer to kernel's Documentation/cgroup (or install linux-docs and see /usr/src/linux/Documentation/cgroup

For commands and configuration files, see relevant man pages, e.g. man cgcreate or man cgrules.conf