Product SiteDocumentation Site

Chapter 2. Using Control Groups

2.1. The cgconfig Service
2.1.1. The /etc/cgconfig.conf File
2.2. Creating a Hierarchy and Attaching Subsystems
2.3. Attaching Subsystems to, and Detaching Them From, an Existing Hierarchy
2.4. Unmounting a Hierarchy
2.5. Creating Control Groups
2.6. Removing Control Groups
2.7. Setting Parameters
2.8. Moving a Process to a Control Group
2.8.1. The cgred Daemon
2.9. Starting a Process in a Control Group
2.9.1. Starting a Service in a Control Group
2.9.2. Process Behavior in the Root Control Group
2.10. Generating the /etc/cgconfig.conf File
2.10.1. Blacklisting Parameters
2.10.2. Whitelisting Parameters
2.11. Obtaining Information About Control Groups
2.11.1. Finding a Process
2.11.2. Finding a Subsystem
2.11.3. Finding Hierarchies
2.11.4. Finding Control Groups
2.11.5. Displaying Parameters of Control Groups
2.12. Unloading Control Groups
2.13. Additional Resources
The easiest way to work with cgroups is to install the libcgroup-tools package, which contains a number of cgroup-related command line utilities and their associated man pages. It is possible to mount hierarchies and set cgroup parameters (non-persistently) using shell commands and utilities available on any system. However, using the libcgroup-tools-provided utilities simplifies the process and extends your capabilities. Therefore, this guide focuses on libcgroup-tools commands throughout. In most cases, we have included the equivalent shell commands to help describe the underlying mechanism. However, we recommend that you use the libcgroup-tools commands wherever practical.

Installing the libcgroup-tools package

In order to use cgroups, first ensure the libcgroup-tools package is installed on your system by running, as root:
~]# yum install libcgroup-tools

2.1. The cgconfig Service

The cgconfig service installed with the libcgroup-tools package provides a convenient way to create hierarchies, attach subsystems to hierarchies, and manage cgroups within those hierarchies. We recommend that you use cgconfig to manage hierarchies and cgroups on your system.
The cgconfig service is started by default on Fedora 17. When you start the service with systemctl start, it reads the cgroup configuration file — /etc/cgconfig.conf. Cgroups are therefore recreated from session to session and become persistent. Depending on the contents of the configuration file, cgconfig can create hierarchies, mount necessary file systems, create cgroups, and set subsystem parameters for each group.
The default /etc/cgconfig.conf file installed with the libcgroup-tools package creates and mounts an individual hierarchy for each subsystem, and attaches the subsystems to these hierarchies.
If you stop the cgconfig service (with the systemctl stop cgconfig.service command), it unmounts all the hierarchies that it mounted.

2.1.1. The /etc/cgconfig.conf File

The /etc/cgconfig.conf file contains two major types of entry — mount and group. Mount entries create and mount hierarchies as virtual file systems, and attach subsystems to those hierarchies. Mount entries are defined using the following syntax:
mount {
    <controller> = <path>;
    …
}
See Example 2.1, “Creating a mount entry” for an example usage.
Example 2.1. Creating a mount entry
The following example creates a hierarchy for the cpuset subsystem:
mount {
    cpuset = /cgroup/red;
}
the equivalent of the shell commands:
~]# mkdir /cgroup/red
~]# mount -t cgroup -o cpuset red /cgroup/red

Group entries create cgroups and set subsystem parameters. Group entries are defined using the following syntax:
group <name> {
    [<permissions>]
    <controller> {
        <param name> = <param value>;
        …
    }
    …
}
Note that the permissions section is optional. To define permissions for a group entry, use the following syntax:
perm {
    task {
        uid = <task user>;
        gid = <task group>;
    }
    admin {
       uid = <admin name>;
       gid = <admin group>;
    }
}
Example 2.2. Creating a group entry
The following example creates a cgroup for SQL daemons, with permissions for users in the sqladmin group to add tasks to the cgroup and the root user to modify subsystem parameters:
group daemons/sql {
    perm {
        task {
            uid = root;
            gid = sqladmin;
        } admin {
            uid = root;
            gid = root;
        }
    } cpu {
        cpu.shares = 100;
    }
}
When combined with the example of the mount entry in Example 2.1, “Creating a mount entry”, the equivalent shell commands are:
~]# mkdir -p /cgroup/cpu/daemons/sql
~]# chown root:root /cgroup/cpu/daemons/sql/*
~]# chown root:sqladmin /cgroup/cpu/daemons/sql/tasks
~]# echo 100 > /cgroup/cpu/daemons/sql/cpu.shares

Restart the cgconfig service for the changes to take effect

You must restart the cgconfig service for the changes in the /etc/cgconfig.conf to take effect:
~]# systemctl restart cgconfig.service 
When you install the libcgroup-tools package, a sample configuration file is written to /etc/cgconfig.conf. The hash symbols ('#') at the start of each line comment that line out and make it invisible to the cgconfig service.