Improve responsiveness with cgroups

This article explains how to setup cgroup so that your Gentoo responds better while you're doing or other resource intensive tasks.

It's achieved by putting different interactive shells (and their all subprocesses) in different cgroups. It is based on the setup proposed here: Alternative to 200 lines kernel patch

What's cgroup?
The cgroup subsystem (short for "control groups") is a kernel feature, most commonly to "balance" e.g. disk or network I/O among groups of processes.

More precisely, cgroup is used by various other subsystems in order to apply certain policies to process groups. For example, the Completely Fair Scheduler CFS (CPU scheduler) and the IO scheduler CFQ can use these to schedule more "fairly" among groups of processes so that one group cannot "starve" another group from all resources. Other use cases are bandwidth guarantees for disk and network I/O.

More information under.

Kernel setup
In order to use cgroups for CPU scheduling, you have to enable the following options:

If you also want to use it for disk I/O scheduling, enable the following options, too:

The cgroup filesystem
Beginnig with sys-apps/openrc >= 0.9.6, cgroup filesystem is automatically mounted in the initialization, by.

The following script sets up remaining tasks:

Now create a second script at :

The first script instructs the kernel to call the second script when the cgroup is empty because the last process in it has terminated. It will then delete the cgroup.

Use cgroups automatically
In order to use the cgroup filesystem in the way that was outlined above, every interactive shell has to create its own cgroup. This is done by adding the following code to your file:

You enable this for all users by adding it to. A more modular alternative is to create the directory like and move the code into a dedicated file like. Then every user who decides to use cgroups can include this code into his file by adding the following line:

This is particularly useful if you want to create several such bashrc "modules".

You can also enable it per default for new users by editing accordingly.

Monitoring cgroups
Find out the pid of the tty-cgroup whose tasks you like to watch:

Open another terminal (or screen window) and change directory to the cgroup mount(assuming here):

And run:

Start a -j4 make job (or something similar) in the first tty.

Drawbacks

 * The above solution only works when resource intensive tasks are started from a shell. It does not help when the application is started from a window manager (terminals within a window manager work, though).
 * There is a slight scheduling overhead associated with cgroups. If throughput is more important than interactivity and responsiveness, do not enable it.
 * The above solution only works for bash shell users. You have to provide similar scripts for users of other shells like tcsh or ksh.

Extend for disk I/O scheduling
Currently, the solution presented above only supports the cpu subsystem. In theory, the same solution is applicable to the disk I/O scheduler. However, that subsystem does not support hierarchies. This will change in kernel 2.6.38 when the following patch is applied: [blk-cgroup: Allow creation of hierarchical cgroups]

The script above attempts to handle this automatically by checking the kernel version and altering the mount command accordingly.