Red Hat Enterprise Linux-6-Resource Management Guide-En-US
Red Hat Enterprise Linux-6-Resource Management Guide-En-US
Martin Prpi
Rdiger Landmann
Douglas Silas
Martin Prpi
Red Hat Engineering Co ntent Services
[email protected] m
Rdiger Landmann
Red Hat Engineering Co ntent Services
[email protected] m
Do uglas Silas
Red Hat Engineering Co ntent Services
[email protected] m
Legal Notice
Copyright 2013 Red Hat, Inc.
T his document is licensed by Red Hat under the Creative Commons Attribution-ShareAlike 3.0 Unported
License. If you distribute this document, or a modified version of it, you must provide attribution to Red
Hat, Inc. and provide a link to the original. If the document is modified, all Red Hat trademarks must be
removed.
Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert, Section
4d of CC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, Red Hat Enterprise Linux, the Shadowman logo, JBoss, MetaMatrix, Fedora, the Infinity Logo,
and RHCE are trademarks of Red Hat, Inc., registered in the United States and other countries.
Linux is the registered trademark of Linus T orvalds in the United States and other countries.
Java is a registered trademark of Oracle and/or its affiliates.
XFS is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States
and/or other countries.
MySQL is a registered trademark of MySQL AB in the United States, the European Union and other
countries.
Node.js is an official trademark of Joyent. Red Hat Software Collections is not formally related to or
endorsed by the official Joyent Node.js open source or commercial project.
T he OpenStack Word Mark and OpenStack Logo are either registered trademarks/service marks or
trademarks/service marks of the OpenStack Foundation, in the United States and other countries and
are used with the OpenStack Foundation's permission. We are not affiliated with, endorsed or
sponsored by the OpenStack Foundation, or the OpenStack community.
All other trademarks are the property of their respective owners.
Abstract
Managing system resources on Red Hat Enterprise Linux 6.
Table of Contents
Table of Contents
.Preface
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4. . . . . . . . . .
1. Document Conventions
4
1.1. T ypographic Conventions
4
1.2. Pull-quote Conventions
5
1.3. Notes and Warnings
6
2. Getting Help and Giving Feedback
6
2.1. Do You Need Help?
6
2.2. We Need Feedback!
7
.Chapter
. . . . . . . . 1.
. . .Introduction
. . . . . . . . . . . . .to
. . .Control
. . . . . . . .Groups
. . . . . . . . (Cgroups)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8. . . . . . . . . .
1.1. How Control Groups Are Organized
8
T he Linux Process Model
8
T he Cgroup Model
8
1.2. Relationships Between Subsystems, Hierarchies, Control Groups and T asks
9
Rule 1
9
Rule 2
10
Rule 3
11
Rule 4
11
1.3. Implications for Resource Management
12
.Chapter
. . . . . . . . 2.
. . .Using
. . . . . . Control
. . . . . . . . Groups
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
............
2.1. T he cgconfig Service
14
2.1.1. T he /etc/cgconfig.conf File
14
2.2. Creating a Hierarchy and Attaching Subsystems
17
Alternative method
17
2.3. Attaching Subsystems to, and Detaching T hem From, an Existing Hierarchy
18
Alternative method
18
2.4. Unmounting a Hierarchy
19
2.5. Creating Control Groups
20
Alternative method
21
2.6. Removing Control Groups
21
2.7. Setting Parameters
21
Alternative method
22
2.8. Moving a Process to a Control Group
23
Alternative method
23
2.8.1. T he cgred Service
23
2.9. Starting a Process in a Control Group
24
Alternative method
25
2.9.1. Starting a Service in a Control Group
25
2.9.2. Process Behavior in the Root Control Group
25
2.10. Generating the /etc/cgconfig.conf File
26
2.10.1. Blacklisting Parameters
28
2.10.2. Whitelisting Parameters
28
2.11. Obtaining Information About Control Groups
28
2.11.1. Finding a Process
28
2.11.2. Finding a Subsystem
29
2.11.3. Finding Hierarchies
29
2.11.4. Finding Control Groups
29
2.11.5. Displaying Parameters of Control Groups
30
2.12. Unloading Control Groups
30
2.13. Using the Notification API
30
2.14. Additional Resources
31
.Chapter
. . . . . . . . 3.
. . .Subsystems
. . . . . . . . . . . . .and
. . . . T. .unable
. . . . . . . Parameters
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .33
...........
3.1. blkio
33
3.1.1. Proportional Weight Division T unable Parameters
33
3.1.2. I/O T hrottling T unable Parameters
34
3.1.3. blkio Common T unable Parameters
35
3.1.4. Example Usage
37
3.2. cpu
39
3.2.1. CFS T unable Parameters
39
3.2.2. RT T unable Parameters
41
3.2.3. Example Usage
41
3.3. cpuacct
42
3.4. cpuset
43
3.5. devices
45
3.6. freezer
46
3.7. memory
47
3.7.1. Example Usage
50
3.8. net_cls
54
3.9. net_prio
54
3.10. ns
55
3.11. perf_event
55
3.12. Common T unable Parameters
56
3.13. Additional Resources
57
.Chapter
........4
. ...Control
. . . . . . . . Group
. . . . . . . Application
. . . . . . . . . . . . Examples
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
............
4.1. Prioritizing Database I/O
59
4.2. Prioritizing Network T raffic
60
4.3. Per-group Division of CPU and Memory Resources
62
Alternative method
65
. . . . . . . . . .History
Revision
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
............
Table of Contents
Preface
1. Document Conventions
T his manual uses several conventions to highlight certain words and phrases and draw attention to
specific pieces of information.
In PDF and paper editions, this manual uses typefaces drawn from the Liberation Fonts set. T he
Liberation Fonts set is also used in HT ML editions if the set is installed on your system. If not, alternative
but equivalent typefaces are displayed. Note: Red Hat Enterprise Linux 5 and later include the Liberation
Fonts set by default.
Preface
Character Map from the main menu bar. Next, choose Search Find from the
Character Map menu bar, type the name of the character in the Search field and click
Next. T he character you sought will be highlighted in the Character T able. Double-click
this highlighted character to place it in the T ext to copy field and then click the Copy
button. Now switch back to your document and choose Edit Paste from the gedit menu
bar.
T he above text includes application names; system-wide menu names and items; application-specific
menu names; and buttons and text found within a GUI interface, all presented in proportional bold and all
distinguishable by context.
Mono-spaced Bold Italic or Proportional Bold Italic
Whether mono-spaced bold or proportional bold, the addition of italics indicates replaceable or variable
text. Italics denotes text you do not input literally or displayed text that changes depending on
circumstance. For example:
T o connect to a remote machine using ssh, type ssh username@ domain.name at a shell
prompt. If the remote machine is exam ple.com and your username on that machine is
john, type ssh john@ exam ple.com .
T he m ount -o rem ount file-system command remounts the named file system. For
example, to remount the /hom e file system, the command is m ount -o rem ount /hom e.
T o see the version of a currently installed package, use the rpm -q package command. It
will return a result as follows: package-version-release.
Note the words in bold italics above: username, domain.name, file-system, package, version and release.
Each word is a placeholder, either for text you enter when issuing a command or for text displayed by
the system.
Aside from standard usage for presenting the title of a work, italics denotes the first use of a new and
important term. For example:
Publican is a DocBook publishing system.
Desktop
Desktop1
documentation
downloads
drafts
images
mss
notes
photos
scripts
stuff
svgs
svn
Source-code listings are also set in m ono-spaced rom an but add syntax highlighting as follows:
Note
Notes are tips, shortcuts or alternative approaches to the task at hand. Ignoring a note should
have no negative consequences, but you might miss out on a trick that makes your life easier.
Important
Important boxes detail things that are easily missed: configuration changes that only apply to the
current session, or services that need restarting before an update will apply. Ignoring a box
labeled Important will not cause data loss but may cause irritation and frustration.
Warning
Warnings should not be ignored. Ignoring warnings will most likely cause data loss.
Preface
[1]
T he Cgroup Model
Cgroups are similar to processes in that:
they are hierarchical, and
child cgroups inherit certain attributes from their parent cgroup.
T he fundamental difference is that many different hierarchies of cgroups can exist simultaneously on a
system. If the Linux process model is a single tree of processes, then the cgroup model is one or more
separate, unconnected trees of tasks (i.e. processes).
Multiple separate hierarchies of cgroups are necessary because each hierarchy is attached to one or
more subsystems. A subsystem [2] represents a single resource, such as CPU time or memory. Red Hat
Enterprise Linux 6 provides ten cgroup subsystems, listed below by name and function.
Available Subsystems in Red Hat Enterprise Linux
blkio this subsystem sets limits on input/output access to and from block devices such as
physical drives (disk, solid state, USB, etc.).
cpu this subsystem uses the scheduler to provide cgroup tasks access to the CPU.
cpuacct this subsystem generates automatic reports on CPU resources used by tasks in a
cgroup.
cpuset this subsystem assigns individual CPUs (on a multicore system) and memory nodes to
tasks in a cgroup.
devices this subsystem allows or denies access to devices by tasks in a cgroup.
Rule 2
Any single subsystem (such as cpu) cannot be attached to more than one hierarchy if one of those
hierarchies has a different subsystem attached to it already.
As a consequence, the cpu subsystem can never be attached to two different hierarchies if one of those
hierarchies already has the memory subsystem attached to it. However, a single subsystem can be
attached to two hierarchies if both of those hierarchies have only that subsystem attached.
Figure 1.2. Rule 2T he numbered bullets represent a time sequence in which the
subsystems are attached.
10
Rule 3
Each time a new hierarchy is created on the systems, all tasks on the system are initially members of the
default cgroup of that hierarchy, which is known as the root cgroup. For any single hierarchy you create,
each task on the system can be a member of exactly one cgroup in that hierarchy. A single task may be
in multiple cgroups, as long as each of those cgroups is in a different hierarchy. As soon as a task
becomes a member of a second cgroup in the same hierarchy, it is removed from the first cgroup in that
hierarchy. At no time is a task ever in two different cgroups in the same hierarchy.
As a consequence, if the cpu and memory subsystems are attached to a hierarchy named cpu_mem_cg,
and the net_cls subsystem is attached to a hierarchy named net, then a running httpd process could
be a member of any one cgroup in cpu_mem_cg, and any one cgroup in net.
The cgroup in cpu_mem_cg that the httpd process is a member of might restrict its CPU time to half of
that allotted to other processes, and limit its memory usage to a maximum of 1024 MB. Additionally, the
cgroup in net that it is a member of might limit its transmission rate to 30 megabytes per second.
When the first hierarchy is created, every task on the system is a member of at least one cgroup: the
root cgroup. When using cgroups, therefore, every system task is always in at least one cgroup.
Rule 4
Any process (task) on the system which forks itself creates a child task. A child task automatically
inherits the cgroup membership of its parent but can be moved to different cgroups as needed. Once
forked, the parent and child processes are completely independent.
As a consequence, consider the httpd task that is a member of the cgroup named half_cpu_1gb_max
in the cpu_and_mem hierarchy, and a member of the cgroup trans_rate_30 in the net hierarchy. When
11
that httpd process forks itself, its child process automatically becomes a member of the
half_cpu_1gb_max cgroup, and the trans_rate_30 cgroup. It inherits the exact same cgroups its
parent task belongs to.
From that point forward, the parent and child tasks are completely independent of each other: changing
the cgroups that one task belongs to does not affect the other. Neither will changing cgroups of a parent
task affect any of its grandchildren in any way. To summarize: any child task always initially inherit
memberships to the exact same cgroups as their parent task, but those memberships can be changed or
removed later.
Figure 1.4 . Rule 4 T he numbered bullets represent a time sequence in which the task forks.
12
[1] The p arent p ro c es s is ab le to alter the enviro nment b efo re p as s ing it to a c hild p ro c es s .
[2] Yo u s ho uld b e aware that s ub s ys tems are als o c alled resource controllers, o r s imp ly controllers, in the libcgroup man p ag es and
o ther d o c umentatio n.
13
14
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 perm issions section is optional. T o define permissions for a group entry, use the
following syntax:
perm {
task {
uid = <task
gid = <task
}
admin {
uid = <admin
gid = <admin
}
}
user>;
group>;
name>;
group>;
15
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/red/daemons/sql
chown root:root /cgroup/red/daemons/sql/*
chown root:sqladmin /cgroup/red/daemons/sql/tasks
echo 0 > /cgroup/red/daemons/cpuset.mems
echo 0 > /cgroup/red/daemons/cpuset.cpus
echo 0 > /cgroup/red/daemons/sql/cpuset.mems
echo 0 > /cgroup/red/daemons/sql/cpuset.cpus
When you install the libcgroup package, a sample configuration file is written to /etc/cgconfig.conf.
T he hash symbols ('#') at the start of each line comment that line out and make it invisible to the
cgconfig service.
16
When cgconfig next starts, it will create the hierarchy and attach the subsystems to it.
T he following example creates a hierarchy called cpu_and_m em and attaches the cpu, cpuset,
cpuacct, and m em ory subsystems to it.
mount {
cpuset
cpu
cpuacct
memory
}
= /cgroup/cpu_and_mem;
= /cgroup/cpu_and_mem;
= /cgroup/cpu_and_mem;
= /cgroup/cpu_and_mem;
Alternative method
You can also use shell commands and utilities to create hierarchies and attach subsystems to them.
Create a mount point for the hierarchy as root. Include the name of the cgroup in the mount point:
~]# mkdir /cgroup/name
For example:
~]# mkdir /cgroup/cpu_and_mem
Next, use the m ount command to mount the hierarchy and simultaneously attach one or more
subsystems. For example:
~]# mount -t cgroup -o subsystems name /cgroup/name
Where subsystems is a comma-separated list of subsystems and name is the name of the hierarchy.
Brief descriptions of all available subsystems are listed in Available Subsystems in Red Hat Enterprise
Linux, and Chapter 3, Subsystems and Tunable Parameters provides a detailed reference.
17
You can list all available subsystems along with their current mount points (i.e. where the hierarchy
they are attached to is mounted) with the lssubsys [3] command:
~]# lssubsys -am
cpu,cpuset,memory /cgroup/cpu_and_mem
net_cls
ns
cpuacct
devices
freezer
blkio
18
Remount the cpu_and_m em hierarchy, using the rem ount option, and include cpuacct in the list of
subsystems:
~]# mount -t cgroup -o remount,cpu,cpuset,cpuacct,memory cpu_and_mem
/cgroup/cpu_and_mem
Analogously, you can detach a subsystem from an existing hierarchy by remounting the hierarchy and
omitting the subsystem name from the -o options. For example, to then detach the cpuacct subsystem,
simply remount and omit it:
~]# mount -t cgroup -o remount,cpu,cpuset,memory cpu_and_mem
/cgroup/cpu_and_mem
For example:
~]# umount /cgroup/cpu_and_mem
If the hierarchy is currently empty (that is, it contains only the root cgroup) the hierarchy is deactivated
when it is unmounted. If the hierarchy contains any other cgroups, the hierarchy remains active in the
kernel even though it is no longer mounted.
T o remove a hierarchy, ensure that all child cgroups are removed before you unmount the hierarchy, or
19
use the cgclear command which can deactivate a hierarchy even when it is not empty refer to
Section 2.12, Unloading Control Groups.
where:
-t (optional) specifies a user (by user ID, uid) and a group (by group ID, gid) to own the tasks
pseudo-file for this cgroup. T his user can add tasks to the cgroup.
Removing tasks
Note that the only way to remove a task from a cgroup is to move it to a different cgroup. T o
move a task, the user must have write access to the destination cgroup; write access to the
source cgroup is unimportant.
-a (optional) specifies a user (by user ID, uid) and a group (by group ID, gid) to own all pseudofiles other than tasks for this cgroup. T his user can modify the access that the tasks in this cgroup
have to system resources.
-g specifies the hierarchy in which the cgroup should be created, as a comma-separated list of
the subsystems associated with those hierarchies. If the subsystems in this list are in different
hierarchies, the group is created in each of these hierarchies. T he list of hierarchies is followed by a
colon and the path to the child group relative to the hierarchy. Do not include the hierarchy mount
point in the path.
For example, the cgroup located in the directory /cgroup/cpu_and_m em /lab1/ is called just
lab1 its path is already uniquely determined because there is at most one hierarchy for a given
subsystem. Note also that the group is controlled by all the subsystems that exist in the hierarchies
in which the cgroup is created, even though these subsystems have not been specified in the
cgcreate command refer to Example 2.5, cgcreate usage.
Because all cgroups in the same hierarchy have the same controllers, the child group has the same
controllers as its parent.
Example 2.5. cgcreate usage
Consider a system where the cpu and m em ory subsystems are mounted together in the
cpu_and_m em hierarchy, and the net_cls controller is mounted in a separate hierarchy called net.
Run the following command:
~]# cgcreate -g cpu,net_cls:/test-subgroup
T he cgcreate command creates two groups named test-subgroup, one in the cpu_and_m em
hierarchy and one in the net hierarchy. T he test-subgroup group in the cpu_and_m em hierarchy
is controlled by the m em ory subsystem, even though it was not specified in the cgcreate command.
20
Alternative method
T o create a child of the cgroup directly, use the m kdir command:
~]# mkdir /cgroup/hierarchy/name/child_name
For example:
~]# mkdir /cgroup/cpuset/lab1/group1
where:
subsystems is a comma-separated list of subsystems.
path is the path to the cgroup relative to the root of the hierarchy.
For example:
~]# cgdelete cpu,net_cls:/test-subgroup
cgdelete can also recursively remove all subgroups with the option -r.
When you delete a cgroup, all its tasks move to its parent group.
where:
parameter is the parameter to be set, which corresponds to the file in the directory of the given
cgroup
value is the value for the parameter
path_to_cgroup is the path to the cgroup relative to the root of the hierarchy. For example, to set
the parameter of the root group (if /cgroup/cpuacct/ exists), run:
cpuacct]# cgset -r cpuacct.usage=0 /
21
Alternatively, because . is relative to the root group (that is, the root group itself) you could also run:
cpuacct]# cgset -r cpuacct.usage=0 .
A trailing slash on the name of the group (for example, cpuacct.usage=0 group1/) is optional.
T he values that you can set with cgset might depend on values set higher in a particular hierarchy. For
example, if group1 is limited to use only CPU 0 on a system, you cannot set group1/subgroup1 to
use CPUs 0 and 1, or to use only CPU 1.
You can also use cgset to copy the parameters of one cgroup into another, existing cgroup. For
example:
~]# cgset --copy-from group1/ group2/
where:
path_to_source_cgroup is the path to the cgroup whose parameters are to be copied, relative to
the root group of the hierarchy
path_to_target_cgroup is the path to the destination cgroup, relative to the root group of the
hierarchy
Ensure that any mandatory parameters for the various subsystems are set before you copy parameters
from one group to another, or the command will fail. For more information on mandatory parameters, refer
to Mandatory parameters.
Alternative method
T o set parameters in a cgroup directly, insert values into the relevant subsystem pseudo-file using the
echo command. For example, this command inserts the value 0-1 into the cpuset.cpus pseudo-file of
the cgroup group1:
~]# echo 0-1 > /cgroup/cpuset/group1/cpuset.cpus
With this value in place, the tasks in this cgroup are restricted to CPUs 0 and 1 on the system.
22
where:
subsystems is a comma-separated list of subsystems, or * to launch the process in the hierarchies
associated with all available subsystems. Note that if cgroups of the same name exist in multiple
hierarchies, the -g option moves the processes in each of those groups. Ensure that the cgroup
exists within each of the hierarchies whose subsystems you specify here.
path_to_cgroup is the path to the cgroup within its hierarchies
pidlist is a space-separated list of process identifier (PIDs)
You can also add the --sticky option before the pid to keep any child processes in the same cgroup.
If you do not set this option and the cgred service is running, child processes will be allocated to
cgroups based on the settings found in /etc/cgrules.conf. T he process itself, however, will remain
in the cgroup in which you started it.
Using cgclassify, you can move several processes simultaneously. For example, this command
moves the processes with PIDs 1701 and 1138 into cgroup group1/:
~]# cgclassify -g cpu,memory:group1 1701 1138
Note that the PIDs to be moved are separated by spaces and that the groups specified should be in
different hierarchies.
Alternative method
T o move a process into a cgroup directly, write its PID to the tasks file of the cgroup. For example, to
move a process with the PID 1701 into a cgroup at /cgroup/lab1/group1/:
~]# echo 1701 > /cgroup/lab1/group1/tasks
devices
/usergroup/staff
T his entry specifies that any processes that belong to the user named m aria access the devices
23
devices
/usergroup/staff/ftp
T he entry now specifies that when the user named m aria uses the ftp command, the process is
automatically moved to the /usergroup/staff/ftp cgroup in the hierarchy that contains the
devices subsystem. Note, however, that the daemon moves the process to the cgroup only after the
appropriate condition is fulfilled. T herefore, the ftp process might run for a short time in the wrong
group. Furthermore, if the process quickly spawns children while in the wrong group, these children
might not be moved.
Entries in the /etc/cgrules.conf file can include the following extra notation:
@ when prefixed to user, indicates a group instead of an individual user. For example, @ adm ins
are all users in the adm ins group.
* represents "all". For example, * in the subsystem field represents all subsystems.
% represents an item the same as the item in the line above. For example:
@adminstaff devices
@labstaff % %
/admingroup
where:
subsystems is a comma-separated list of subsystems, or * to launch the process in the hierarchies
24
associated with all available subsystems. Note that, as with cgset described in Section 2.7, Setting
Parameters, if cgroups of the same name exist in multiple hierarchies, the -g option creates
processes in each of those groups. Ensure that the cgroup exists within each of the hierarchies
whose subsystems you specify here.
path_to_cgroup is the path to the cgroup relative to the hierarchy.
command is the command to run.
arguments are any arguments for the command.
You can also add the --sticky option before the command to keep any child processes in the same
cgroup. If you do not set this option and the cgred daemon is running, child processes will be allocated
to cgroups based on the settings found in /etc/cgrules.conf. T he process itself, however, will
remain in the cgroup in which you started it.
Alternative method
When you start a new process, it inherits the group of its parent process. T herefore, an alternative
method for starting a process in a particular cgroup is to move your shell process to that group (refer to
Section 2.8, Moving a Process to a Control Group), and then launch the process from that shell. For
example:
~]# echo $$ > /cgroup/lab1/group1/tasks
~]# lynx
Note that after exiting lynx, your existing shell is still in the group1 cgroup. T herefore, an even better
way would be:
~]# sh -c "echo \$$ > /cgroup/lab1/group1/tasks && lynx"
25
/rootgroup/ process:
33.33%
/rootgroup/blue/ process: 33.33%
/rootgroup/red/ process: 33.33%
Any other processes placed in subgroups blue and red result in the 33.33% percent of the CPU
assigned to that specific subgroup to be split among the multiple processes in that subgroup.
However, multiple processes placed in the root group cause the CPU resource to be split per process,
rather than per group. For example, if /rootgroup/ contains three processes, /rootgroup/red/
contains one process and /rootgroup/blue/ contains one process, and the cpu.shares option is
set to 1 in all groups, the CPU resource is divided as follows:
/rootgroup/ processes:
20% + 20% + 20%
/rootgroup/blue/ process: 20%
/rootgroup/red/ process: 20%
T herefore, it is recommended to move all processes from the root group to a specific subgroup when
using the blkio and cpu configuration options which divide an available resource based on a weight or
a share (for example, cpu.shares or blkio.weight). T o move all tasks from the root group into a
specific subgroup, you can use the following commands:
rootgroup]# cat tasks >> red/tasks
rootgroup]# echo > tasks
26
mkdir /cgroup/cpu
mount -t cgroup -o cpu cpu /cgroup/cpu
mkdir /cgroup/cpu/lab1
mkdir /cgroup/cpu/lab2
echo 2 > /cgroup/cpu/lab1/cpu.shares
echo 3 > /cgroup/cpu/lab2/cpu.shares
echo 5000000 > /cgroup/cpu/lab1/cpu.rt_period_us
echo 4000000 > /cgroup/cpu/lab1/cpu.rt_runtime_us
mkdir /cgroup/cpuacct
mount -t cgroup -o cpuacct cpuacct /cgroup/cpuacct
T he above commands mounted two subsystems and created two cgroups, for the cpu subsystem,
with specific values for some of their parameters. Executing the cgsnapshot command (with the -s
option and an empty /etc/cgsnapshot_blacklist.conf file
output:
[4] )
~]$ cgsnapshot -s
# Configuration file generated by cgsnapshot
mount {
cpu = /cgroup/cpu;
cpuacct = /cgroup/cpuacct;
}
group lab2 {
cpu {
cpu.rt_period_us="1000000";
cpu.rt_runtime_us="0";
cpu.shares="3";
}
}
group lab1 {
cpu {
cpu.rt_period_us="5000000";
cpu.rt_runtime_us="4000000";
cpu.shares="2";
}
}
T he -s option used in the example above tells cgsnapshot to ignore all warnings in the output file
caused by parameters not being defined in the blacklist or whitelist of the cgsnapshot utility. For
more information on parameter blacklisting, refer to Section 2.10.1, Blacklisting Parameters. For more
information on parameter whitelisting, refer to Section 2.10.2, Whitelisting Parameters.
When not specifying any options, the output generated by cgsnapshot is returned on the standard
output. Use the -f to specify a file to which the output should be redirected. For example:
~]$ cgsnapshot -f ~/test/cgconfig_test.conf
27
By default, there is no whitelist configuration file. T o specify which file to use as a whitelist, use the -w
option. For example:
~]$ cgsnapshot -w ~/test/my_whitelist.conf
Specifying the -t option tells cgsnapshot to generate a configuration with parameters from the whitelist
only.
28
num_cgroups
enabled
1
1
In the example output above, the hierarchy column lists IDs of the existing hierarchies on the system.
Subsystems with the same hierarchy ID are attached to the same hierarchy. T he num _cgroup column
lists the number of existing cgroups in the hierarchy that uses a particular subsystem. T he enabled
column reports a value of 1 if a particular subsystem is enabled, or 0 if it is not.
Or, to find the mount points of particular subsystems, run:
~]$ lssubsys -m subsystems
where subsystems is a list of the subsystems in which you are interested. Note that the lssubsys -m
command returns only the top-level mount point per each hierarchy.
You can restrict the output to a specific hierarchy by specifying a controller and path in the format
controller:path. For example:
~]$ lscgroup cpuset:adminusers
lists only subgroups of the adm inusers cgroup in the hierarchy to which the cpuset subsystem is
attached.
29
where parameter is a pseudo-file that contains values for a subsystem, and list_of_cgroups is a list
of cgroups separated with spaces. For example:
~]$ cgget -r cpuset.cpus -r memory.limit_in_bytes lab1 lab2
displays the values of cpuset.cpus and m em ory.lim it_in_bytes for cgroups lab1 and lab2.
If you do not know the names of the parameters themselves, use a command like:
~]$ cgget -g cpuset /
30
1. Using the eventfd() function, create a file descriptor for event notifications. For more
information, refer to the eventfd(2) man page.
2. T o monitor the m em ory.oom _control file, open it using the open() function. For more
information, refer to the open(2) man page.
3. Use the write() function to write the following arguments to the cgroup.event_control file
of the cgroup whose m em ory.oom _control file you are monitoring:
<event_file_descriptor> <OOM_control_file_descriptor>
where:
event_file_descriptor is used to open the cgroup.event_control file,
and OOM_control_file_descriptor is used to open the respective
m em ory.oom _control file.
For more information on writing to a file, refer to the write(1) man page.
When the above program is started, it will be notified of any OOM situation in the cgroup it is monitoring.
Note that OOM notifications only work in non-root cgroups.
For more information on the m em ory.oom _control tunable parameter, refer to Section 3.7, memory.
For more information on configuring notifications for OOM control, refer to Example 3.3, OOM Control
and Notifications.
31
subsystems.
[3] The l ssub sys c o mmand is o ne o f the utilities p ro vid ed b y the libcgroup p ac kag e. Yo u mus t ins tall libcgroup to us e it: refer to
Chap ter 2, Using Control Groups if yo u are unab le to run l ssub sys.
[4] The cp u.sh ares p arameter is s p ec ified in the /etc/cg sn ap sh o t_b l ackl i st.co n f file b y d efault, whic h wo uld c aus e it to b e
o mitted in the g enerated o utp ut in Examp le 2.6 , Us ing the c g s nap s ho t utility . Thus , fo r the p urp o s es o f the examp le, an emp ty
/etc/cg sn ap sh o t_b l ackl i st.co n f file is us ed .
32
T he value 0,2 is written to the cpuset.cpus pseudofile and therefore limits any tasks whose PIDs are
listed in /cgroup/cpuset/webserver/tasks to use only CPU 0 and CPU 2 on the system.
3.1. blkio
T he Block I/O (blkio) subsystem controls and monitors access to I/O on block devices by tasks in
cgroups. Writing values to some of these pseudofiles limits access or bandwidth, and reading values
from some of these pseudofiles provides information on I/O operations.
T he blkio subsystem offers two policies for controlling access to I/O:
Proportional weight division implemented in the Completely Fair Queuing I/O scheduler, this policy
allows you to set weights to specific cgroups. T his means that each cgroup has a set percentage
(depending on the weight of the cgroup) of all I/O operations reserved. For more information, refer to
Section 3.1.1, Proportional Weight Division T unable Parameters
I/O throttling (Upper limit) this policy is used to set an upper limit for the number of I/O operations
performed by a specific device. T his means that a device can have a limited rate of read or write
operations. For more information, refer to Section 3.1.2, I/O T hrottling T unable Parameters
33
blkio.weight_device
specifies the relative proportion (weight) of I/O access on specific devices available to a cgroup,
in the range 100 to 1000. T he value of this parameter overrides the value of the
blkio.weight parameter for the devices specified. Values take the format
major:minor weight, where major and minor are device types and node numbers specified in
Linux Allocated Devices, otherwise known as the Linux Devices List and available from
http://www.kernel.org/doc/Documentation/devices.txt. For example, to assign a weight of 500 to
a cgroup for access to /dev/sda, run:
echo 8:0 500 > blkio.weight_device
blkio.throttle.read_iops_device
specifies the upper limit on the number of read operations a device can perform. T he rate of the
read operations is specified in operations per second. Entries have three fields: major, minor,
and operations_per_second. Major and minor are device types and node numbers
specified in Linux Allocated Devices, and operations_per_second is the upper limit rate at
which read operations can be performed. For example, to allow the /dev/sda device to perform
a maximum of 10 read operations per second, run:
~]# echo "8:0 10" > /cgroup/blkio/test/blkio.throttle.read_iops_device
blkio.throttle.write_bps_device
specifies the upper limit on the number of write operations a device can perform. T he rate of the
write operations is specified in bytes per second. Entries have three fields: major, minor, and
bytes_per_second. Major and minor are device types and node numbers specified in Linux
Allocated Devices, and bytes_per_second is the upper limit rate at which write operations can
be performed. For example, to allow the /dev/sda device to perform write operations at a
maximum of 10 MBps, run:
34
blkio.throttle.write_iops_device
specifies the upper limit on the number of write operations a device can perform. T he rate of the
write operations is specified in operations per second. Entries have three fields: major, minor,
and operations_per_second. Major and minor are device types and node numbers
specified in Linux Allocated Devices, and operations_per_second is the upper limit rate at
which write operations can be performed. For example, to allow the /dev/sda device to perform
a maximum of 10 write operations per second, run:
~]# echo "8:0 10" >
/cgroup/blkio/test/blkio.throttle.write_iops_device
blkio.throttle.io_serviced
reports the number of I/O operations performed on specific devices by a cgroup as seen by the
throttling policy. Entries have four fields: major, minor, operation, and number. Major and
minor are device types and node numbers specified in Linux Allocated Devices, operation
represents the type of operation (read, write, sync, or async) and number represents the
number of operations.
blkio.throttle.io_service_bytes
reports the number of bytes transferred to or from specific devices by a cgroup. T he only
difference between blkio.io_service_bytes and blkio.throttle.io_service_bytes is
that the former is not updated when the CFQ scheduler is operating on a request queue.
Entries have four fields: major, minor, operation, and bytes. Major and minor are device
types and node numbers specified in Linux Allocated Devices, operation represents the type
of operation (read, write, sync, or async) and bytes is the number of bytes transferred.
35
blkio.avg_queue_size
reports the average queue size for I/O operations by a cgroup, over the entire length of time of
the group's existence. T he queue size is sampled every time a queue for this cgroup gets a
timeslice. Note that this report is available only if CONFIG_DEBUG_BLK_CGROUP=y is set on
the system.
blkio.group_wait_time
reports the total time (in nanoseconds ns) a cgroup spent waiting for a timeslice for one of
its queues. T he report is updated every time a queue for this cgroup gets a timeslice, so if you
read this pseudofile while the cgroup is waiting for a timeslice, the report will not contain time
spent waiting for the operation currently queued. Note that this report is available only if
CONFIG_DEBUG_BLK_CGROUP=y is set on the system.
blkio.empty_time
reports the total time (in nanoseconds ns) a cgroup spent without any pending requests.
T he report is updated every time a queue for this cgroup has a pending request, so if you read
this pseudofile while the cgroup has no pending requests, the report will not contain time spent
in the current empty state. Note that this report is available only if
CONFIG_DEBUG_BLK_CGROUP=y is set on the system.
blkio.idle_time
reports the total time (in nanoseconds ns) the scheduler spent idling for a cgroup in
anticipation of a better request than those requests already in other queues or from other
groups. T he report is updated every time the group is no longer idling, so if you read this
pseudofile while the cgroup is idling, the report will not contain time spent in the current idling
state. Note that this report is available only if CONFIG_DEBUG_BLK_CGROUP=y is set on the
system.
blkio.dequeue
reports the number of times requests for I/O operations by a cgroup were dequeued by specific
devices. Entries have three fields: major, minor, and number. Major and minor are device
types and node numbers specified in Linux Allocated Devices, and number is the number of
requests the group was dequeued. Note that this report is available only if
CONFIG_DEBUG_BLK_CGROUP=y is set on the system.
blkio.io_serviced
reports the number of I/O operations performed on specific devices by a cgroup as seen by the
CFQ scheduler. Entries have four fields: major, minor, operation, and number. Major and
minor are device types and node numbers specified in Linux Allocated Devices, operation
represents the type of operation (read, write, sync, or async) and number represents the
number of operations.
blkio.io_service_bytes
reports the number of bytes transferred to or from specific devices by a cgroup as seen by the
CFQ scheduler. Entries have four fields: major, minor, operation, and bytes. Major and
minor are device types and node numbers specified in Linux Allocated Devices, operation
represents the type of operation (read, write, sync, or async) and bytes is the number of
bytes transferred.
36
blkio.io_service_time
reports the total time between request dispatch and request completion for I/O operations on
specific devices by a cgroup as seen by the CFQ scheduler. Entries have four fields: major,
minor, operation, and time. Major and minor are device types and node numbers specified
in Linux Allocated Devices, operation represents the type of operation (read, write, sync,
or async) and time is the length of time in nanoseconds (ns). T he time is reported in
nanoseconds rather than a larger unit so that this report is meaningful even for solid-state
devices.
blkio.io_wait_time
reports the total time I/O operations on specific devices by a cgroup spent waiting for service in
the scheduler queues. When you interpret this report, note:
the time reported can be greater than the total time elapsed, because the time reported is
the cumulative total of all I/O operations for the cgroup rather than the time that the cgroup
itself spent waiting for I/O operations. T o find the time that the group as a whole has spent
waiting, use the blkio.group_wait_tim e parameter.
if the device has a queue_depth > 1, the time reported only includes the time until the
request is dispatched to the device, not any time spent waiting for service while the device
re-orders requests.
Entries have four fields: major, minor, operation, and time. Major and minor are device
types and node numbers specified in Linux Allocated Devices, operation represents the type
of operation (read, write, sync, or async) and time is the length of time in nanoseconds
(ns). T he time is reported in nanoseconds rather than a larger unit so that this report is
meaningful even for solid-state devices.
blkio.io_merged
reports the number of BIOS requests merged into requests for I/O operations by a cgroup.
Entries have two fields: number and operation. Number is the number of requests, and
operation represents the type of operation (read, write, sync, or async).
blkio.io_queued
reports the number of requests queued for I/O operations by a cgroup. Entries have two fields:
number and operation. Number is the number of requests, and operation represents the
type of operation (read, write, sync, or async).
37
T he above commands create two files (file_1 and file_2) of size 4 GB.
5. For each of the test cgroups, execute a dd command (which reads the contents of a file and
outputs it to the null device) on one of the large files:
~]# cgexec -g blkio:test1 time dd if=file_1 of=/dev/null
~]# cgexec -g blkio:test2 time dd if=file_2 of=/dev/null
Both commands will output their completion time once they have finished.
6. Simultaneously with the two running dd threads, you can monitor the performance in real time
by using the iotop utility. T o install the iotop utility, execute, as root, the yum install
iotop command. T he following is an example of the output as seen in the iotop utility while
running the previously-started dd threads:
Total DISK READ: 83.16 M/s | Total DISK WRITE: 0.00 B/s
TIME TID PRIO USER
DISK READ DISK WRITE SWAPIN
IO
COMMAND
15:18:04 15071 be/4 root
27.64 M/s
0.00 B/s 0.00 % 92.30 % dd
if=file_2 of=/dev/null
15:18:04 15069 be/4 root
55.52 M/s
0.00 B/s 0.00 % 88.48 % dd
if=file_1 of=/dev/null
In order to get the most accurate result in Example 3.1, blkio proportional weight division, prior to the
execution of the dd commands, flush all file system buffers and free pagecache, dentries and inodes
using the following commands:
~]# sync
~]# echo 3 > /proc/sys/vm/drop_caches
Additionally, you can enable group isolation which provides stronger isolation between groups at the
expense of throughput. When group isolation is disabled, fairness can be expected only for a sequential
workload. By default, group isolation is enabled and fairness can be expected for random I/O workloads
38
where <disk_device> stands for the name of the desired device, for example sda.
3.2. cpu
T he cpu subsystem schedules CPU access to cgroups. Access to CPU resources can be scheduled
using two schedulers:
Completely Fair Scheduler (CFS) a proportional share scheduler which divides the CPU time (CPU
bandwidth) proportionately between groups of tasks (cgroups) depending on the priority/weight of
the task or shares assigned to cgroups. For more information about resource limiting using CFS,
refer to Section 3.2.1, CFS T unable Parameters.
Real-Time scheduler (RT) a task scheduler that provides a way to specify the amount of CPU time
that real-time tasks can use. For more information about resource limiting of real-time tasks, refer to
Section 3.2.2, RT T unable Parameters.
39
cpu.stat
reports CPU time statistics using the following values:
nr_periods number of period intervals (as specified in cpu.cfs_period_us) that
have elapsed.
nr_throttled number of times tasks in a cgroup have been throttled (that is, not
allowed to run because they have exhausted all of the available time as specified by their
quota).
throttled_tim e the total time duration (in nanoseconds) for which tasks in a cgroup
have been throttled.
cgroup
CPU
CPU share
100
100% of CPU0
101
100% of CPU1
102
100% of CPU2
103
100% of CPU3
Using relative shares to specify CPU access has two implications on resource management
that should be considered:
Because the CFS does not demand equal usage of CPU, it is hard to predict how much CPU
time a cgroup will be allowed to utilize. When tasks in one cgroup are idle and are not using
any CPU time, this left-over time is collected in a global pool of unused CPU cycles. Other
cgroups are allowed to borrow CPU cycles from this pool.
T he actual amount of CPU time that is available to a cgroup can vary depending on the
number of cgroups that exist on the system. If a cgroup has a relative share of 1000 and
two other cgroups have a relative share of 500, the first cgroup receives 50% of all CPU
time in cases when processes in all cgroups attempt to use 100% of the CPU. However, if
another cgroup is added with a relative share of 1000, the first cgroup is only allowed 33%
of the CPU (the rest of the cgroups receive 16.5%, 16.5%, and 33% of CPU).
40
41
T o limit a cgroup to fully utilize a single CPU, use the following commands:
~]# echo 10000 > /cgroup/cpu/red/cpu.cfs_quota_us
~]# echo 10000 > /cgroup/cpu/red/cpu.cfs_period_us
T o limit a cgroup to utilize 10% of a single CPU, use the following commands:
~]# echo 10000 > /cgroup/cpu/red/cpu.cfs_quota_us
~]# echo 100000 > /cgroup/cpu/red/cpu.cfs_period_us
On a multi-core system, to allow a cgroup to fully utilize two CPU cores, use the following
commands:
~]# echo 200000 > /cgroup/cpu/red/cpu.cfs_quota_us
~]# echo 100000 > /cgroup/cpu/red/cpu.cfs_period_us
3.3. cpuacct
T he CPU Accounting (cpuacct) subsystem generates automatic reports on CPU resources used by
the tasks in a cgroup, including tasks in child groups. T hree reports are available:
cpuacct.usage
reports the total CPU time (in nanoseconds) consumed by all tasks in this cgroup (including
tasks lower in the hierarchy).
Resetting cpuacct.usage
T o reset the value in cpuacct.usage, execute the following command:
~]# echo 0 > /cgroup/cpuacct/cpuacct.usage
cpuacct.stat
reports the user and system CPU time consumed by all tasks in this cgroup (including tasks
lower in the hierarchy) in the following way:
user CPU time consumed by tasks in user mode.
42
3.4. cpuset
T he cpuset subsystem assigns individual CPUs and memory nodes to cgroups. Each cpuset can be
specified according to the following parameters, each one in a separate pseudofile within the cgroup
virtual file system:
Mandatory parameters
Some subsystems have mandatory parameters that must be set before you can move a task into
a cgroup which uses any of those subsystems. For example, before you move a task into a
cgroup which uses the cpuset subsystem, the cpuset.cpus and cpuset.m em s parameters
must be defined for that cgroup.
cpuset.cpus (mandatory)
specifies the CPUs that tasks in this cgroup are permitted to access. T his is a commaseparated list, with dashes ("-") to represent ranges. For example,
0-2,16
43
cpuset.cpu_exclusive
contains a flag (0 or 1) that specifies whether cpusets other than this one and its parents and
children can share the CPUs specified for this cpuset. By default (0), CPUs are not allocated
exclusively to one cpuset.
cpuset.mem_exclusive
contains a flag (0 or 1) that specifies whether other cpusets can share the memory nodes
specified for this cpuset. By default (0), memory nodes are not allocated exclusively to one
cpuset. Reserving memory nodes for the exclusive use of a cpuset (1) is functionally the same
as enabling a memory hardwall with the cpuset.m em _hardwall parameter.
cpuset.mem_hardwall
contains a flag (0 or 1) that specifies whether kernel allocations of memory page and buffer
data should be restricted to the memory nodes specified for this cpuset. By default (0), page
and buffer data is shared across processes belonging to multiple users. With a hardwall
enabled (1), each tasks' user allocation can be kept separate.
cpuset.memory_pressure
a read-only file that contains a running average of the memory pressure created by the
processes in this cpuset. T he value in this pseudofile is automatically updated when
cpuset.m em ory_pressure_enabled is enabled, otherwise, the pseudofile contains the
value 0.
cpuset.memory_pressure_enabled
contains a flag (0 or 1) that specifies whether the system should compute the memory
pressure created by the processes in this cgroup. Computed values are output to
cpuset.m em ory_pressure and represent the rate at which processes attempt to free in-use
memory, reported as an integer value of attempts to reclaim memory per second, multiplied by
1000.
cpuset.memory_spread_page
contains a flag (0 or 1) that specifies whether file system buffers should be spread evenly
across the memory nodes allocated to this cpuset. By default (0), no attempt is made to spread
memory pages for these buffers evenly, and buffers are placed on the same node on which the
process that created them is running.
cpuset.memory_spread_slab
contains a flag (0 or 1) that specifies whether kernel slab caches for file input/output operations
should be spread evenly across the cpuset. By default (0), no attempt is made to spread kernel
slab caches evenly, and slab caches are placed on the same node on which the process that
created them is running.
cpuset.sched_load_balance
contains a flag (0 or 1) that specifies whether the kernel will balance loads across the CPUs in
this cpuset. By default (1), the kernel balances loads by moving processes from overloaded
CPUs to less heavily used CPUs.
44
Note, however, that setting this flag in a cgroup has no effect if load balancing is enabled in any
parent cgroup, as load balancing is already being carried out at a higher level. T herefore, to
disable load balancing in a cgroup, disable load balancing also in each of its parents in the
hierarchy. In this case, you should also consider whether load balancing should be enabled for
any siblings of the cgroup in question.
cpuset.sched_relax_domain_level
contains an integer between -1 and a small positive value, which represents the width of the
range of CPUs across which the kernel should attempt to balance loads. T his value is
meaningless if cpuset.sched_load_balance is disabled.
T he precise effect of this value varies according to system architecture, but the following values
are typical:
Values of cpuset.sched_relax_domain_level
Value
Effect
-1
3.5. devices
T he devices subsystem allows or denies access to devices by tasks in a cgroup.
Technology preview
T he Device Whitelist (devices) subsystem is considered to be a T echnology Preview in Red
Hat Enterprise Linux 6.
Technology preview features are currently not supported under Red Hat Enterprise Linux 6
subscription services, might not be functionally complete, and are generally not suitable for
production use. However, Red Hat includes these features in the operating system as a customer
convenience and to provide the feature with wider exposure. You might find these features useful
in a non-production environment and are also free to provide feedback and functionality
suggestions for a technology preview feature before it becomes fully supported.
45
devices.allow
specifies devices to which tasks in a cgroup have access. Each entry has four fields: type,
major, minor, and access. T he values used in the type, major, and minor fields correspond
to device types and node numbers specified in Linux Allocated Devices, otherwise known as the
Linux Devices List and available from http://www.kernel.org/doc/Documentation/devices.txt.
type
type can have one of the following three values:
a applies to all devices, both character devices and block devices
b specifies a block device
c specifies a character device
major, minor
major and minor are device node numbers specified by Linux Allocated Devices. T he
major and minor numbers are separated by a colon. For example, 8 is the major
number that specifies SCSI disk drives, and the minor number 1 specifies the first
partition on the first SCSI disk drive; therefore 8:1 fully specifies this partition,
corresponding to a file system location of /dev/sda1.
* can stand for all major or all minor device nodes, for example 9:* (all RAID devices)
or * :* (all devices).
access
access is a sequence of one or more of the following letters:
r allows tasks to read from the specified device
w allows tasks to write to the specified device
m allows tasks to create device files that do not yet exist
For example, when access is specified as r, tasks can only read from the specified
device, but when access is specified as rw, tasks can read from and write to the
device.
devices.deny
specifies devices that tasks in a cgroup cannot access. T he syntax of entries is identical with
devices.allow.
devices.list
reports the devices for which access controls have been set for tasks in this cgroup.
3.6. freezer
T he freezer subsystem suspends or resumes tasks in a cgroup.
freezer.state
46
freezer.state is only available in non-root cgroups, and has three possible values:
FROZEN tasks in the cgroup are suspended.
FREEZING the system is in the process of suspending tasks in the cgroup.
T HAWED tasks in the cgroup have resumed.
3.7. memory
T he m em ory subsystem generates automatic reports on memory resources used by the tasks in a
cgroup, and sets limits on memory use by those tasks:
memory.stat
reports a wide range of memory statistics, as described in the following table:
T able 3.2. Values reported by memory.stat
Statistic
Description
cache
rss
m apped_file
pgpgin
pgpgout
swap
active_anon
inactive_anon
active_file
inactive_file
unevictable
hierarchical_m em ory_
lim it
hierarchical_m em sw_l
im it
memory plus swap limit for the hierarchy that contains the
m em ory cgroup, in bytes
47
48
memory.failcnt
reports the number of times that the memory limit has reached the value set in
m em ory.lim it_in_bytes.
memory.memsw.failcnt
reports the number of times that the memory plus swap space limit has reached the value set in
m em ory.m em sw.lim it_in_bytes.
memory.force_empty
when set to 0, empties memory of all pages used by tasks in this cgroup. T his interface can
only be used when the cgroup has no tasks. If memory cannot be freed, it is moved to a parent
cgroup if possible. Use the m em ory.force_em pty parameter before removing a cgroup to
avoid moving out-of-use page caches to its parent cgroup.
memory.swappiness
sets the tendency of the kernel to swap out process memory used by tasks in this cgroup
instead of reclaiming pages from the page cache. T his is the same tendency, calculated the
same way, as set in /proc/sys/vm /swappiness for the system as a whole. T he default
value is 60. Values lower than 60 decrease the kernel's tendency to swap out process
memory, values greater than 60 increase the kernel's tendency to swap out process memory,
and values greater than 100 permit the kernel to swap out pages that are part of the address
49
When the OOM killer is disabled, tasks that attempt to use more memory than they are allowed
are paused until additional memory is freed.
T he m em ory.oom _control file also reports the OOM status of the current cgroup under the
under_oom entry. If the cgroup is out of memory and tasks in it are paused, the under_oom
entry reports the value 1.
T he m em ory.oom _control file is capable of reporting an occurrence of an OOM situation
using the notification API. For more information, refer to Section 2.13, Using the Notification API
and Example 3.3, OOM Control and Notifications.
50
2. Set the amount of memory which tasks in the blue cgroup can use to 100MB:
~]# echo 104857600 > memory.limit_in_bytes
3. Change into the blue directory and make sure the OOM killer is enabled:
~]# cd /cgroup/memory/blue
blue]# cat memory.oom_control
oom_kill_disable 0
under_oom 0
4. Move the current shell process into the tasks file of the blue cgroup so that all other
processes started in this shell are automatically moved to the blue cgroup:
blue]# echo $$ > tasks
5. Start a test program that attempts to allocate a large amount of memory exceeding the limit you
set in Step 2. As soon as the blue cgroup runs out of free memory, the OOM killer kills the test
program and reports Killed to the standard output:
blue]# ~/mem-hog
Killed
51
#include
#include
#include
#include
<stdio.h>
<stdlib.h>
<string.h>
<unistd.h>
#define KB (1024)
#define MB (1024 * KB)
#define GB (1024 * MB)
int main(int argc, char *argv[])
{
char *p;
again:
while ((p = (char *)malloc(GB)))
memset(p, 0, GB);
while ((p = (char *)malloc(MB)))
memset(p, 0, MB);
while ((p = (char *)malloc(KB)))
memset(p, 0,
KB);
sleep(1);
goto again;
return 0;
}
6. Disable the OOM killer and re-run the test program. T his time, the test program remains paused
waiting for additional memory to be freed:
blue]# echo 1 > memory.oom_control
blue]# ~/mem-hog
7. While the test program is paused, note that the under_oom state of the cgroup has changed
to indicate that the cgroup is out of available memory:
~]# cat /cgroup/memory/blue/memory.oom_control
oom_kill_disable 1
under_oom 1
52
#include
#include
#include
#include
#include
#include
#include
#include
<sys/types.h>
<sys/stat.h>
<fcntl.h>
<sys/eventfd.h>
<errno.h>
<string.h>
<stdio.h>
<stdlib.h>
53
10. In a different console, run the m em _hog test program to create an OOM situation to see the
oom _notification program report it on the standard output:
blue]# ~/mem-hog
3.8. net_cls
T he net_cls subsystem tags network packets with a class identifier (classid) that allows the Linux
traffic controller (tc) to identify packets originating from a particular cgroup. T he traffic controller can be
configured to assign different priorities to packets from different cgroups.
net_cls.classid
net_cls.classid contains a single value that indicates a traffic control handle. T he value of
classid read from the net_cls.classid file is presented in the decimal format while the
value to be written to the file is expected in the hexadecimal format. For example, 0x100001
represents the handle conventionally written as 10:1 in the format used by iproute2. In the
net_cls.classid file, it would be represented by the number 104 8577.
T he format for these handles is: 0xAAAABBBB, where AAAA is the major number in hexadecimal
and BBBB is the minor number in hexadecimal. You can omit any leading zeroes; 0x10001 is
the same as 0x00010001, and represents 1:1. T he following is an example of setting a 10:1
handle in the net_cls.classid file:
~]# echo 0x100001 > /cgroup/net_cls/red/net_cls.classid
~]# cat /cgroup/net_cls/red/net_cls.classid
1048577
Refer to the man page for tc to learn how to configure the traffic controller to use the handles that the
net_cls adds to network packets.
3.9. net_prio
T he Network Priority (net_prio) subsystem provides a way to dynamically set the priority of network
traffic per each network interface for applications within various cgroups. A network's priority is a number
assigned to network traffic and used internally by the system and network devices. Network priority is
used to differentiate packets that are sent, queued, or dropped. T he tc command may be used to set a
network's priority (setting the network priority via the tc command is outside the scope of this guide; for
more information, refer to the tc man page).
54
T ypically, an application sets the priority of its traffic via the SO_PRIORIT Y socket option. However,
applications are often not coded to set the priority value, or the application's traffic is site-specific and
does not provide a defined priority.
Using the net_prio subsystem in a cgroup allows an administrator to assign a process to a specific
cgroup which defines the priority of outgoing traffic on a given network interface.
net_prio.prioidx
a read-only file which contains a unique integer value that the kernel uses as an internal
representation of this cgroup.
net_prio.ifpriomap
contains a map of priorities assigned to traffic originating from processes in this group and
leaving the system on various interfaces. T his map is represented by a list of pairs in the form
<network_interface> <priority>:
~]# cat /cgroup/net_prio/iscsi/net_prio.ifpriomap
eth0 5
eth1 4
eth2 6
Contents of the net_prio.ifpriom ap file can be modified by echoing a string into the file
using the above format, for example:
~]# echo "eth0 5" > /cgroup/net_prio/iscsi/net_prio.ifpriomap
T he above command forces any traffic originating from processes belonging to the iscsi
net_prio cgroup, and with traffic outgoing on the eth0 network interface, to have the priority
set to the value 5. T he parent cgroup also has a writable net_prio.ifpriom ap file that can
be used to set a system default priority.
3.10. ns
T he ns subsystem provides a way to group processes into separate namespaces. Within a particular
namespace, processes can interact with each other but are isolated from processes running in other
namespaces. T hese separate namespaces are sometimes referred to as containers when used for
operating-system-level virtualization.
3.11. perf_event
When the perf_event subsystem is attached to a hierarchy, all cgroups in that hierarchy can be used
to group processes and threads which can then be monitored with the perf tool, as opposed to
monitoring each process or thread separately or per-CPU. Cgroups which use the perf_event
subsystem do not contain any special tunable parameters other than the common parameters listed in
Section 3.12, Common T unable Parameters.
For additional information on how tasks in a cgroup can be monitored using the perf tool, refer to the
Red Hat Enterprise Linux Developer Guide, available from
http://access.redhat.com/site/documentation/Red_Hat_Enterprise_Linux/.
55
56
4. T est your configuration to make sure emptied cgroups are properly removed:
cpu]# pwd; ls
/cgroup/cpu
cgroup.event_control cgroup.procs cpu.cfs_period_us
cpu.cfs_quota_us cpu.rt_period_us cpu.rt_runtime_us cpu.shares
cpu.stat libvirt notify_on_release release_agent tasks
cpu]# cat notify_on_release
1
cpu]# cat release_agent
/usr/local/bin/remove-empty-cpu-cgroup.sh
cpu]# mkdir blue; ls
blue cgroup.event_control cgroup.procs cpu.cfs_period_us
cpu.cfs_quota_us cpu.rt_period_us cpu.rt_runtime_us cpu.shares
cpu.stat libvirt notify_on_release release_agent tasks
cpu]# cat blue/notify_on_release
1
cpu]# cgexec -g cpu:blue dd if=/dev/zero of=/dev/null bs=1024k &
[1] 8623
cpu]# cat blue/tasks
8623
cpu]# kill -9 8623
cpu]# ls
cgroup.event_control cgroup.procs cpu.cfs_period_us
cpu.cfs_quota_us cpu.rt_period_us cpu.rt_runtime_us cpu.shares
cpu.stat libvirt notify_on_release release_agent tasks
57
All of the following files are located under the /usr/share/doc/kerneldoc-<kernel_version>/Docum entation/cgroups/ directory (provided by the kernel-doc
package).
blkio subsystem blkio-controller.txt
cpuacct subsystem cpuacct.txt
cpuset subsystem cpusets.txt
devices subsystem devices.txt
freezer subsystem freezer-subsystem .txt
m em ory subsystem m em ory.txt
net_prio subsystem net_prio.txt
Additionally, refer to the following files on further information about the cpu subsystem:
Real-T ime scheduling /usr/share/doc/kerneldoc-<kernel_version>/Docum entation/scheduler/sched-rt-group.txt
CFS scheduling /usr/share/doc/kerneldoc-<kernel_version>/Docum entation/scheduler/sched-bwc.txt
[5] So urc e c o d e p ro vid ed b y Red Hat Eng ineer Franti ek Hrb ata.
[6 ] So urc e c o d e p ro vid ed b y Red Hat Eng ineer Franti ek Hrb ata.
58
T o prioritize the high priority database server, it can be assigned to a cgroup with a high number of
reserved I/O operations, whereas the low priority database server can be assigned to a cgroup with a
low number of reserved I/O operations. T o achieve this, follow the steps in Procedure 4.1, I/O
throughput prioritization, all of which are performed on the host system.
Procedure 4 .1. I/O throughput prioritization
1. Attach the blkio subsystem to the /cgroup/blkio cgroup:
~]# mkdir /cgroup/blkio
~]# mount -t cgroup -o blkio blkio /cgroup/blkio
3. Acquire the PIDs of the processes that represent both virtual guests (in which the database
servers are running) and move them to their specific cgroup. In our example, VM_high represents
a virtual guest running a high priority database server, and VM_low represents a virtual guest
running a low priority database server. For example:
59
4. Set a ratio of 10:1 for the high_prio and low_prio cgroups. Processes in those cgroups (that
is, processes running the virtual guests that have been added to those cgroups in the previous
step) will immediately use only the resources made available to them.
~]# echo 1000 > /cgroup/blkio/high_prio/blkio.weight
~]# echo 100 > /cgroup/blkio/low_prio/blkio.weight
In our example, the low priority cgroup permits the low priority database server to use only about
10% of the I/O operations, whereas the high priority cgroup permits the high priority database
server to use about 90% of the I/O operations.
Figure 4.2, I/O throughput with resource allocation illustrates the outcome of limiting the low priority
database and prioritizing the high priority database. As soon as the database servers are moved to their
appropriate cgroups (around time 75), I/O throughput is divided among both servers with the ratio of
10:1.
Alternatively, block device I/O throttling can be used for the low priority database to limit its number of
read and write operation. For more information on the blkio subsystem, refer to Section 3.1, blkio.
60
3. T o automatically move the nfs services to the nfs_high cgroup, add the following line to the
/etc/sysconfig/nfs file:
CGROUP_DAEMON="net_prio:nfs_high"
T his configuration ensures that nfs service processes are moved to the nfs_high cgroup when
the nfs service is started or restarted. For more information about moving service processes to
cgroups, refer to Section 2.9.1, Starting a Service in a Control Group.
4. T he sm bd daemon does not have a configuration file in the /etc/sysconfig directory. T o
automatically move the sm bd daemon to the sam ba_low cgroup, add the following line to the
/etc/cgrules.conf file:
*:smbd
net_prio
samba_low
Note that this rule moves every sm bd daemon, not only /usr/sbin/sm bd, into the sam ba_low
cgroup.
You can define rules for the nm bd and winbindd daemons to be moved to the sam ba_low
cgroup in a similar way.
5. Start the cgred service to load the configuration from the previous step:
~]# service cgred start
Starting CGroup Rules Engine Daemon:
OK
6. For the purposes of this example, let us assume both services use the eth1 network interface.
Define network priorities for each cgroup, where 1 denotes low priority and 10 denotes high
priority:
~]# echo "eth1 1" > /cgroup/net_prio/samba_low
~]# echo "eth1 10" > /cgroup/net_prio/nfs_high
7. Start the nfs and sm b services and check whether their processes have been moved into the
correct cgroups:
61
OK
[
[
[
[
[
[
OK
OK
OK
OK
OK
OK
]
]
]
]
]
]
Network traffic originating from NFS now has higher priority than traffic originating from Samba.
Similar to Procedure 4.2, Setting Network Priorities for File Sharing Services, the net_prio subsystem
can be used to set network priorities for client applications, for example, Firefox.
For this example to work properly, you must have the libcgroup package installed. Using the
/etc/cgconfig.conf and /etc/cgrules.conf files, you can create a hierarchy and a set of rules
which determine the amount of resources for each user. T o achieve this, follow the steps in
Procedure 4.3, Per-group CPU and memory resource management.
Procedure 4 .3. Per-group CPU and memory resource management
1. In the /etc/cgconfig.conf file, configure the following subsystems to be mounted and
62
cgroups to be created:
mount {
cpu
= /cgroup/cpu_and_mem;
cpuacct = /cgroup/cpu_and_mem;
memory = /cgroup/cpu_and_mem;
}
group finance {
cpu {
cpu.shares="250";
}
cpuacct {
cpuacct.usage="0";
}
memory {
memory.limit_in_bytes="2G";
memory.memsw.limit_in_bytes="3G";
}
}
group sales {
cpu {
cpu.shares="250";
}
cpuacct {
cpuacct.usage="0";
}
memory {
memory.limit_in_bytes="4G";
memory.memsw.limit_in_bytes="6G";
}
}
group engineering {
cpu {
cpu.shares="500";
}
cpuacct {
cpuacct.usage="0";
}
memory {
memory.limit_in_bytes="8G";
memory.memsw.limit_in_bytes="16G";
}
}
When loaded, the above configuration file mounts the cpu, cpuacct, and m em ory subsystems to
a single cpu_and_m em cgroup. For more information on these subsystems, refer to Chapter 3,
Subsystems and Tunable Parameters. Next, it creates a hierarchy in cpu_and_m em which
contains three cgroups: sales, finance, and engineering. In each of these cgroups, custom
parameters are set for each subsystem:
cpu the cpu.shares parameter determines the share of CPU resources available to each
process in all cgroups. Setting the parameter to 250, 250, and 500 in the finance, sales, and
engineering cgroups respectively means that processes started in these groups will split the
resources with a 1:1:2 ratio. Note that when a single process is running, it consumes as much
CPU as necessary no matter which cgroup it is placed in. T he CPU limitation only comes into
effect when two or more processes compete for CPU resources.
63
<controller(s)>
cpu,cpuacct,memory
cpu,cpuacct,memory
cpu,cpuacct,memory
<cgroup>
finance
sales
engineering
T he above configuration creates rules that assign a specific system group (for example,
@finance) the resource controllers it may use (for example, cpu, cpuacct, m em ory) and a
cgroup (for example, finance) which contains all processes originating from that system group.
In our example, when the cgrulesengd daemon, started via the service cgred start
command, detects a process that is started by a user that belongs to the finance system group
(for example, jenn), that process is automatically moved to the
/cgroup/cpu_and_m em /finance/tasks file and is subjected to the resource limitations set
in the finance cgroup.
3. Start the cgconfig service to create the hierarchy of cgroups and set the needed parameters in
all created cgroups:
~]# service cgconfig start
Starting cgconfig service:
OK
Start the cgred service to let the cgrulesengd daemon detect any processes started in system
groups configured in the /etc/cgrules.conf file:
~]# service cgred start
Starting CGroup Rules Engine Daemon:
OK
Note that cgred is the name of the service that starts the cgrulesengd daemon.
4. T o make all of the changes above persistent across reboots, configure the cgconfig and
cgred services to be started by default:
~]# chkconfig cgconfig on
~]# chkconfig cgred on
T o test whether this setup works, execute a CPU or memory intensive process and observe the results,
for example, using the top utility. T o test the CPU resource management, execute the following dd
command under each user:
~]$ dd if=/dev/zero of=/dev/null bs=1024k
64
T he above command reads the /dev/zero and outputs it to the /dev/null in chunks of 1024 KB.
When the top utility is launched, you can see results similar to these:
PID
8201
8202
8199
8200
8197
8198
USER
peter
mike
jenn
john
martin
mark
PR
20
20
20
20
20
20
NI VIRT RES
0 103m 1676
0 103m 1672
0 103m 1676
0 103m 1676
0 103m 1672
0 103m 1672
SHR
556
556
556
556
556
556
S
R
R
R
R
R
R
%CPU %MEM
24.9 0.2
24.9 0.2
12.6 0.2
12.6 0.2
12.6 0.2
12.3 0.2
TIME+
0:04.18
0:03.47
0:02.87
0:02.20
0:05.56
0:04.28
COMMAND
dd
dd
dd
dd
dd
dd
All processes have been correctly assigned to their cgroups and are only allowed to consume CPU
resource made available to them. If all but two processes, which belong to the finance and engineering
cgroups, are stopped, the remaining resources are evenly split between both processes:
PID USER
8202 mike
8200 john
PR
20
20
NI VIRT RES
0 103m 1676
0 103m 1672
TIME+ COMMAND
0:06.35 dd
0:05.08 dd
Alternative method
Because the cgrulesengd daemon moves a process to a cgroup only after the appropriate conditions
set by the rules in /etc/cgrules.conf have been fulfilled, that process may be running for a few
milliseconds in an incorrect cgroup. An alternative way to move processes to their specified cgroups is
to use the pam _cgroup.so PAM module. T his module moves processes to available cgroups
according to rules defined in the /etc/cgrules.conf file. Follow the steps in Procedure 4.4, Using a
PAM module to move processes to cgroups to configure the pam _cgroup.so PAM module.
Procedure 4 .4 . Using a PAM module to move processes to cgroups
1. Install the libcgroup-pam package from the optional Red Hat Enterprise Linux Yum repository:
~]# yum install libcgroup-pam --enablerepo=rhel-6-server-optional-rpms
2. Ensure that the PAM module has been installed and exists:
~]# ls /lib64/security/pam_cgroup.so
/lib64/security/pam_cgroup.so
Note that on 32-bit systems, the module is placed in the /lib/security directory.
3. Add the following line to the /etc/pam .d/su file to use the pam _cgroup.so module each time
the su command is executed:
session
optional
pam_cgroup.so
65
Revision History
Revision 1.0-22.4 05
Rebuild with Publican 4.0.0
Rdiger Landmann
Revision 1.0-22
Version for 6.5 GA release
T ue Nov 19 2013
Yoana Ruseva
Revision 1.0-16
T hu Feb 21 2013
Martin Prpi
Red Hat Enterprise Linux 6.4 GA release of the Resource Management Guide. Includes various fixes
and new content:
- Final use case scenarios 584631
- Documentation for the perf_event controller 807326
- Documentation for common cgroup files 807329
- Documentation for OOM control and the notification API 822400, 822401
- CPU ceiling enforcement documentation 828991
Revision 1.0-7
Wed Jun 20 2012
Martin Prpi
Red Hat Enterprise Linux 6.3 GA release of the Resource Management Guide.
- Added two use cases.
- Added documentation for the net_prio subsystem.
Revision 1.0-6
T ue Dec 6 2011
Martin Prpi
Red Hat Enterprise Linux 6.2 GA release of the Resource Management Guide.
Revision 1.0-5
T hu May 19 2011
Martin Prpi
Red Hat Enterprise Linux 6.1 GA release of the Resource Management Guide.
Revision 1.0-4
T ue Mar 1 2011
Martin Prpi
- Fixed multiple examples BZ #667623, BZ #667676, BZ #667699
- Clarification of the cgclear command BZ #577101
- Clarification of the lssubsystem command BZ #678517
- Freezing a process BZ #677548
Revision 1.0-3
Wed Nov 17 2010
Correct remount example BZ #612805
Rdiger Landmann
Revision 1.0-2
T hu Nov 11 2010
Remove pre-release feedback instructions
Rdiger Landmann
Revision 1.0-1
Wed Nov 10 2010
Corrections from QE BZ #581702 and BZ #612805
Rdiger Landmann
Revision 1.0-0
T ue Nov 9 2010
Feature-complete version for GA
Rdiger Landmann
66