Difference between revisions of "Cgroups"

From ArchWiki
Jump to: navigation, search
(Created page with "'''cgroups''' (aka '''control groups''') is a Linux kernel feature to limit, police and account the resource usage of certain processes (actually process groups). For example, cg...")
 
Line 18: Line 18:
 
Then run the following to create these directories and mount the controller file systems:
 
Then run the following to create these directories and mount the controller file systems:
  
  /etc/rc.d/cgconfig start
+
  /etc/rc.d/'''cgconfig''' start
  
Now when you list the directory, you should see some files in it:
+
Now when you list the controller directory, you should see some files (cgroup tunables) in it:
  
  ls -l /mnt/cgroups/memory
+
  ls -l /mnt/cgroups/'''memory'''
  
 
== Simple usage ==
 
== Simple usage ==
Line 28: Line 28:
 
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 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):
  
  sudo cgcreate -a $USER -g memory,cpu:me
+
  sudo '''cgcreate''' -a '''$USER''' -g memory,cpu:'''me'''
  
 
That's it! Now all the tunables in the group "me" are writable by your user:
 
That's it! Now all the tunables in the group "me" are writable by your user:
  
  ls -l /mnt/cgroups/memory/me
+
  $ ls -l /mnt/cgroups/memory/<b>me</b>
 +
total 0
 +
-rwxrwxr-x 1 me root 0 Sep 25 00:39 cgroup.event_control
 +
-rwxrwxr-x 1 me root 0 Sep 25 00:39 cgroup.procs
 +
-rwxrwxr-x 1 me root 0 Sep 25 00:39 cpu.rt_period_us
 +
-rwxrwxr-x 1 me root 0 Sep 25 00:39 cpu.rt_runtime_us
 +
-rwxrwxr-x 1 me root 0 Sep 25 00:39 cpu.shares
 +
-rwxrwxr-x 1 me root 0 Sep 25 00:39 notify_on_release
 +
-rwxrwxr-x 1 me root 0 Sep 25 00:39 tasks
 +
 
 +
Let's say that, as a normal user, you want to run a '''bash''' shell under a new sub-group called 'foo'. That's simple:
 +
 
 +
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 greated for this group. To limit the memory usage of all processes in this group to '''10 MB''', run the following:
 +
 
 +
$ echo <b>10000000</b> > /mnt/cgroups/memory/me/foo/<b>memory.limit_in_bytes</b>
 +
 
 +
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 <b>100</b> > /mnt/cgroups/cpu/me/foo/<b>cpu.shares</b>
 +
 
 +
You can find more tunables or statistics by listing the cgroup directory.
 +
 
 +
You can also change the cgroup of already running processes:
 +
 
 +
$ pidof bash
 +
13244
 +
$ '''cgclassify''' -g memory,cpu:me/foo `pidof bash`
 +
$ cat /proc/13244/cgroup
 +
11:memory:/me/foo
 +
6:cpu:/me/foo

Revision as of 22:04, 24 September 2010

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

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.

Installing

First, install the utilities for managing cgroups; you need the libcgroup package from AUR. makepkg and install it as usual.

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

Template:File 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

Simple usage

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

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 me root 0 Sep 25 00:39 cgroup.event_control
-rwxrwxr-x 1 me root 0 Sep 25 00:39 cgroup.procs
-rwxrwxr-x 1 me root 0 Sep 25 00:39 cpu.rt_period_us
-rwxrwxr-x 1 me root 0 Sep 25 00:39 cpu.rt_runtime_us
-rwxrwxr-x 1 me root 0 Sep 25 00:39 cpu.shares
-rwxrwxr-x 1 me root 0 Sep 25 00:39 notify_on_release
-rwxrwxr-x 1 me root 0 Sep 25 00:39 tasks

Let's say that, as a normal user, you want to run a bash shell under a new sub-group called 'foo'. That's simple:

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

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