Introduction to Linux Cgroups

January 11, 2011 Off By David
Object Storage
Grazed from ServerWatch.  Author: Joe Brockmeier.

A number of users made use of cgroups recently by following the four-line script from Lennart Poettering, rather than patching the kernel, to improve Linux desktop performance. Many users (myself included) tried the script but didn’t at the time pay attention to the fact that it was using the cgroups feature.

So what is cgroups? It’s a feature initially developed to limit resource usage in the Linux kernel. Using cgroups, you can tweak the memory, bandwidth, CPU usage and so forth of processes running on your system. Cgroups can also be used to deny access to system resources, and they can help monitor system resources.

Specifically, the kernel has controllers for memory, CPU usage and sets, devices, process control ("freezer") and networking. For example, you can use the cpuacct controller to control CPU usage, and the cpu controller to manage the actual CPUs used. Processes might be able to have 100 percent of two CPUs on a system, but not four.

Right now this is not entirely a straightforward process. Some distros ship with the user-space tools to work with cgroups, others don’t. Ubuntu, for example, doesn’t — so install the cgroup-bin package if you’re on Ubuntu. Then, you must create a configuration file called /etc/cgconfig.conf and specify mount points on the filesystem that correspond with the controllers. For example, you might mount the cpu controller under /dev/cgroups/cpu and the memory controller under /dev/cgroups/memory. Alternatively, you might want to use the blkio subsystem to manage I/O on your system.

Once the controllers are mounted, you must actually create groups that define the parameters of a group. For example, you might want to create a group called "virtual" that has limited access to the blkio subsystem of your machine to ensure VMware doesn’t chew up your disks too badly.

This week, I wanted to just introduce the cgroups feature and concept, since it seems to be less than well known in the Linux community. Next week, I’ll provide a few short examples for using cgroups to manage resources you can adapt to your systems. In the meantime, you might want to look at Red Hat’s Resource Management Guide, which is very complete but also fairly complex. We’ll make it simpler. Stay tuned, it’s going to get fun!