實在懶得寫了,cgroup轉一篇,Cgroup 用法,寫得很詳細。對cgroup子系統的分析可參考下面兩篇。html
cgroup 子系統之 net_cls 和 net_priogit
介紹docker的的過程當中,提到lxc利用cgroup來提供資源的限額和控制,本文主要介紹cgroup的用法和操做命令,主要內容來自github
[1]https://access.redhat.com/sit...sql
[2]https://www.kernel.org/doc/Do...docker
cgroup的功能在於將一臺計算機上的資源(CPU,memory, network)進行分片,來防止進程間不利的資源搶佔。centos
Terminologyapi
subsystem
和一組樹形結構的cgroup
. 和cgroup
不一樣,hierarchy
包含的是可管理的subsystem
而非具體參數因而可知,cgroup對資源的管理是一個樹形結構,相似進程。網絡
相同點 - 分層結構,子進程/cgroup繼承父進程/cgroupapp
不一樣點 - 進程是一個單根樹狀結構(pid=0爲根),而cgroup總體來看是一個多樹的森林結構(hierarchy爲根)。
一個典型的hierarchy
掛載目錄以下
/cgroup/ ├── blkio <--------------- hierarchy/root cgroup │ ├── blkio.io_merged <--------------- subsystem parameter ... ... │ ├── blkio.weight │ ├── blkio.weight_device │ ├── cgroup.event_control │ ├── cgroup.procs │ ├── lxc <--------------- cgroup │ │ ├── blkio.io_merged <--------------- subsystem parameter │ │ ├── blkio.io_queued ... ... ... │ │ └── tasks <--------------- task list │ ├── notify_on_release │ ├── release_agent │ └── tasks ...
subsystem列表
RHEL/centos支持的subsystem以下
1.一個hierarchy能夠有多個 subsystem (mount 的時候hierarchy能夠attach多個subsystem)
A single hierarchy can have one or more subsystems attached to it.
eg.
mount -t cgroup -o cpu,cpuset,memory cpu_and_mem /cgroup/cpu_and_mem
2.一個已經被掛載的 subsystem 只能被再次掛載在一個空的 hierarchy 上 (已經mount一個subsystem的hierarchy不能掛載一個已經被其它hierarchy掛載的subsystem)
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.
3.每一個task只能在一同個hierarchy的惟一一個cgroup裏(不能在同一個hierarchy下有超過一個cgroup的tasks裏同時有這個進程的pid)
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 onecgroup 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.
4.子進程在被fork出時自動繼承父進程所在cgroup,可是fork以後就能夠按需調整到其餘cgroup
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.
5.其它
1.掛載subsystem
利用cgconfig服務及其配置文件 /etc/cgconfig.conf
- 服務啓動時自動掛載
subsystem = /cgroup/hierarchy;
命令行操做
mount -t cgroup -o subsystems name /cgroup/name
取消掛載
umount /cgroup/name
eg. 掛載 cpuset, cpu, cpuacct, memory 4個subsystem到/cgroup/cpu_and_mem
目錄(hierarchy)
mount { cpuset = /cgroup/cpu_and_mem; cpu = /cgroup/cpu_and_mem; cpuacct = /cgroup/cpu_and_mem; memory = /cgroup/cpu_and_mem; }
or
mount -t cgroup -o remount,cpu,cpuset,memory cpu_and_mem /cgroup/cpu_and_mem
2. 新建/刪除 cgroup
利用cgconfig服務及其配置文件 /etc/cgconfig.conf
- 服務啓動時自動掛載
group <name> { [<permissions>] <controller> { <param name> = <param value>; … } … }
命令行操做
cgcreate -t uid:gid -a uid:gid -g subsystems:path
mkdir /cgroup/hierarchy/name/child_name
cgdelete subsystems:path
(使用 -r 遞歸刪除)rm -rf /cgroup/hierarchy/name/child_name
(cgconfig service not running)3. 權限管理
利用cgconfig服務及其配置文件 /etc/cgconfig.conf
- 服務啓動時自動掛載
perm { task { uid = <task user>; gid = <task group>; } admin { uid = <admin name>; gid = <admin group>; } }
chown
eg.
group daemons { cpuset { cpuset.mems = 0; cpuset.cpus = 0; } } group daemons/sql { perm { task { uid = root; gid = sqladmin; } admin { uid = root; gid = root; } } cpuset { cpuset.mems = 0; cpuset.cpus = 0; } }
or
~]$ 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
4. cgroup參數設定
cgset -r parameter=value path_to_cgroup
cgset --copy-from path_to_source_cgroup path_to_target_cgroup
echo value > path_to_cgroup/parameter
eg.
cgset -r cpuset.cpus=0-1 group1 cgset --copy-from group1/ group2/ echo 0-1 > /cgroup/cpuset/group1/cpuset.cpus
5. 添加task
cgclassify -g subsystems:path_to_cgroup pidlist
echo pid > path_to_cgroup/tasks
cgexec -g subsystems:path_to_cgroup command arguments
echo 'CGROUP_DAEMON="subsystem:control_group"' >> /etc/sysconfig/
利用cgrulesengd服務初始化,在配置文件/etc/cgrules.conf
中
user<:command> subsystems control_group 其中: +用戶user的全部進程的subsystems限制的group爲control_group +<:command>是可選項,表示對特定命令實行限制 +user能夠用@group表示對特定的 usergroup 而非user +能夠用*表示所有 +%表示和前一行的該項相同
eg.
cgclassify -g cpu,memory:group1 1701 1138 echo -e "1701\n1138" |tee -a /cgroup/cpu/group1/tasks /cgroup/memory/group1/tasks cgexec -g cpu:group1 lynx http://www.redhat.com sh -c "echo \$$ > /cgroup/lab1/group1/tasks && lynx http://www.redhat.com"
經過/etc/cgrules.conf 對特定服務限制
maria devices /usergroup/staff maria:ftp devices /usergroup/staff/ftp @student cpu,memory /usergroup/student/ % memory /test2/
6. 其餘
cgsnapshot會根據當前cgroup狀況生成/etc/cgconfig.conf文件內容
gsnapshot [-s] [-b FILE] [-w FILE] [-f FILE] [controller] -b, --blacklist=FILE Set the blacklist configuration file (default /etc/cgsnapshot_blacklist.conf) -f, --file=FILE Redirect the output to output_file -s, --silent Ignore all warnings -t, --strict Don't show the variables which are not on the whitelist -w, --whitelist=FILE Set the whitelist configuration file (don't used by default)
查看進程在哪一個cgroup
ps -O cgroup 或 cat /proc/<PID>/cgroup
查看subsystem mount狀況
cat /proc/cgroups lssubsys -m <subsystems>
lscgroup
查看cgroup參數值
cgget -r parameter list_of_cgroups cgget -g <controllers>:<path>
更多
common
device_types:node_numbers milliseconds
device_types:node_numbers sector_count
CONFIG_DEBUG_BLK_CGROUP=y
)CONFIG_DEBUG_BLK_CGROUP=y
, 單位ns)CONFIG_DEBUG_BLK_CGROUP=y
, 單位ns)CONFIG_DEBUG_BLK_CGROUP=y
) - device_types:node_numbers number
device_types:node_numbers operation number
device_types:node_numbers operation bytes
device_types:node_numbers operation time
device_types:node_numbers operation time
number operation
number operation
Proportional weight division 策略 - 按比例分配block io資源
I/O throttling (Upper limit) 策略 - 設定IO操做上限
device_types:node_numbers bytes_per_second
blkio.throttle.write_bps_device - device_types:node_numbers bytes_per_second
device_types:node_numbers operations_per_second
blkio.throttle.write_iops_device - device_types:node_numbers operations_per_second
device_types:node_numbers operation operations_per_second
blkio.throttle.io_service_bytes - device_types:node_numbers operation bytes_per_second
CFS(Completely Fair Scheduler)策略 - CPU最大資源限制
RT(Real-Time scheduler)策略 - CPU最小資源限制
兩者配合使用規定cgroup裏的task每cpu.rt_period_us(微秒)必然會執行cpu.rt_runtime_us(微秒)
cpuset.sched_relax_domain_level - 可選 - cpuset.sched_load_balance的策略
設備黑/白名單
memory.stat - 報告cgroup限制狀態
<network_interface> <priority>