怎样使用cgroup

where to find cgroups document

if you are not familier with cgroup conception,special nouns,you d better
read these documents first.

the kernel document

install

yum install kernel-doc

then go to

/usr/share/doc/kernel-doc-3.10.0/Documentation/cgroups

the first file you need to read is cgroups.txt .
your kernel version may be different.

the redhat document

redhat has a document called resource-management-guide.pdf,

What RPM packages you need before you can use cgroups

you need to install:

yum install libcgroup libcgroup-tools

how to use cgroups

create a new group 'pluto-test':

cgcreate -g cpu:pluto-test

this command will create a new directory under /sys/fs/cgroup/cpu/,
for cpu:pluto-test,cpu means the cpu dir in /sys/fs/cgroup/,
pluto-test means make a new dir pluto-test in /sys/fs/cgroup/cpu.

set cpu attributes

cgset -r cpu.cfs_period_us=100000 cpu:/pluto-test
cgset -r cpu.cfs_quota_us=1000 cpu:/pluto-test

the above '100000' means 100000 microseconds, it is equal to 100 milliseconds, you can also write millisecond:

cgset -r cpu.cfs_period_us=100ms cpu:/pluto-test
cgset -r cpu.cfs_quota_us=1ms cpu:/pluto-test

it means, in each 100ms, that process can only execute 1ms.

the default value of cpu.cfs_quota_fs is -1, means no limit.

those two values are stored at :

  • /sys/fs/cgroup/cpu/pluto-test/cpu.cfs_period_us
  • /sys/fs/cgroup/cpu/pluto-test/cpu.cfs_quota_us

check these values:

cgget -r cpu.cfs_period_us cpu:pluto-test
cgget -r cpu.cfs_period_us cpu:pluto-test

An example for test

create a python script for test, edit abc.py

#!/usr/bin/python
a = 1
while True:
    print(a)
    a += 1

At terminal window one,run top. at terminal window two,run the abc.py,
and you will see a process jump up the top cpu usage rank in a second,
remember his pid.

write this pid to tasks


cgclassify -g cpu:pluto PID

that PID will appear at /sys/fs/cgroup/cpu/pluto-test/tasks .

How to limit memory usage

create a new group under memory subsystem

cgcreate -g memory:neptune

you can also let the new group stays under both cpu and memory subsystems
,

cgcreate -g cpu,memory:neptune

set memory attributes

cgset -r memory.limit_in_bytes=10 memory:/neptune

add a process to this group, for example firefox,

cgclassify -g memory:neptune PidOfFirefox

suddenly when you switch the firefox windows,you find that it has
disappeared, because it exceesed the memory limit.

or you can start a process with this method:

cgexec -g cpu:pluto-test /bin/bash

Notice,this method one not recommanded,because cgclassify is more
intelligent,for example,when using cgclassify,it will

  • automatically reomove the specified PID from other groups.
  • when the process exits,it automatically remove its pids from the group
  • and so on

check whether a process has joined to a group

if a process has joined to a group,for example cpu:pluto-test,two files can
indicate its joining

  • /sys/fs/cgroup/cpu/pluto/tasks

    its PID will be in it.

  • /proc/PID/cgroup

    there will be a line:

    10:cpuacct,cpu:/pluto
    

How to delete a group

cgdelete -g cpu:pluto

attributes introduction

i have not found a document about this.

configuration files

/etc/cgrules.conf

/etc/cgconfig.conf

(systemd) with cgroups

this chapter can be skipped.systemd is not a part of cgroups,redhat
combines systemd with cgroups in redhat 7.

systemd moves the resource management settings from the process level to the
application level by binding the system of cgroup hierarchies with the systemd
unit tree. Therefore, you can manage system resources with systemctl
commands, or by modifying systemd unit files.

From the systemd s perspective, a cgroup is bound to a system unit
configurable with a unit file and manageable with systemd s command-line
utilities. your resource management settings can be transient or persistent.

you can see redhat document resource_management_guide.pdf.

modify a group

use command

syntax:

systemctl set-property ServiceName parameter=value

example:

systemctl set-property httpd.service CPUShares=600 MemoryLimit=500M

then,you will find two file 50-CPUshares.conf,50-MemoryLimit.conf in
/etc/systemd/system/httpd.service.d/ .

The changes are applied instantly, and written into the unit file, so that they
are preserved after reboot. You can change this behavior by passing the
--runtime option, that makes your settings transient:

systemctl set-property --runtime ServiceName property=value

edit unit files

the files 50-CPUShares.conf,50-MemoryLimit.conf,you can also create them
manually,the filename can be any, but you need to run two command mannualy,

systemctl daemon-reload
systemctl restart httpd.service

You can also edit 'httpd.service' directly:

[Service]
...
CPUShares=1500
...

parameters

CPUShares

The default value is 1024. By increasing this number, you assign more CPU time to the unit. It automatically turns CPUAccounting on in the unit file.
Users can thus monitor the usage of the processor with the 'systemd-cgtop' command.
example:

[Service]
CPUShares=1500

CPUQuota

This value expressed in percentage, specifies how much CPU time the unit gets at maximum.
Values higher than 100% indicate that more than one CPU is used. CPUQuota controls the cpu.max
attribute on the unified control group hierarchy, and the legacy cpu.cfs_quota_us attribute. Setting
the value of the CPUQuota parameter automatically turns CPUAccounting on in the unit file. Users
can thus monitor the usage of the processor with the systemd-cgtop command.

example:

[Service]
CPUQuota=20%

MemoryLimit

example:

[Service]
MemoryLimit=1G

你可能感兴趣的:(cgroup)