見出し画像

Appendix cgroup v2

環境

  • Ubuntu22.04

  • cgroup v2


確認

  • コントロール可能なリソースを確認する

root@lpic303-ubuntu35:~#
root@lpic303-ubuntu35:~# cat /sys/fs/cgroup/cgroup.controllers
cpuset cpu io memory hugetlb pids rdma misc
root@lpic303-ubuntu35:~#
  • cgroup2がマウントされている

root@lpic303-ubuntu35:~#
root@lpic303-ubuntu35:~# mount -l | grep cg
cgroup2 on /sys/fs/cgroup type cgroup2 (rw,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot)
root@lpic303-ubuntu35:~#
  • マウントされているディレクトリを確認する

root@lpic303-ubuntu35:~#
root@lpic303-ubuntu35:~# cd /sys/fs/cgroup/
root@lpic303-ubuntu35:/sys/fs/cgroup#
root@lpic303-ubuntu35:/sys/fs/cgroup# ls -l
合計 0
-r--r--r--  1 root root 0  212 09:59 cgroup.controllers
-rw-r--r--  1 root root 0  212 09:59 cgroup.max.depth
-rw-r--r--  1 root root 0  212 09:59 cgroup.max.descendants
-rw-r--r--  1 root root 0  212 09:59 cgroup.procs
-r--r--r--  1 root root 0  212 09:59 cgroup.stat
-rw-r--r--  1 root root 0  212 09:59 cgroup.subtree_control
-rw-r--r--  1 root root 0  212 09:59 cgroup.threads
-rw-r--r--  1 root root 0  212 09:59 cpu.pressure
-r--r--r--  1 root root 0  212 09:59 cpu.stat
-r--r--r--  1 root root 0  212 09:59 cpuset.cpus.effective
-r--r--r--  1 root root 0  212 09:59 cpuset.mems.effective
drwxr-xr-x  2 root root 0  212 09:59 dev-hugepages.mount
drwxr-xr-x  2 root root 0  212 09:59 dev-mqueue.mount
drwxr-xr-x  2 root root 0  212 09:59 init.scope
-rw-r--r--  1 root root 0  212 09:59 io.cost.model
-rw-r--r--  1 root root 0  212 09:59 io.cost.qos
-rw-r--r--  1 root root 0  212 09:59 io.pressure
-rw-r--r--  1 root root 0  212 09:59 io.prio.class
-r--r--r--  1 root root 0  212 09:59 io.stat
-r--r--r--  1 root root 0  212 09:59 memory.numa_stat
-rw-r--r--  1 root root 0  212 09:59 memory.pressure
-r--r--r--  1 root root 0  212 09:59 memory.stat
-r--r--r--  1 root root 0  212 09:59 misc.capacity
drwxr-xr-x  2 root root 0  212 09:59 proc-sys-fs-binfmt_misc.mount
drwxr-xr-x  2 root root 0  212 09:59 sys-fs-fuse-connections.mount
drwxr-xr-x  2 root root 0  212 09:59 sys-kernel-config.mount
drwxr-xr-x  2 root root 0  212 09:59 sys-kernel-debug.mount
drwxr-xr-x  2 root root 0  212 09:59 sys-kernel-tracing.mount
drwxr-xr-x 44 root root 0  212 10:08 system.slice
drwxr-xr-x  3 root root 0  212 09:58 user.slice
root@lpic303-ubuntu35:/sys/fs/cgroup#

メモリを制限する設定例

1. ディレクトリを作成する

root@lpic303-ubuntu35:~#
root@lpic303-ubuntu35:~# cd /sys/fs/cgroup/
root@lpic303-ubuntu35:/sys/fs/cgroup#
root@lpic303-ubuntu35:/sys/fs/cgroup# mkdir Example-Memory
root@lpic303-ubuntu35:/sys/fs/cgroup#
root@lpic303-ubuntu35:/sys/fs/cgroup# ls -ld Example-Memory/
drwxr-xr-x 2 root root 0  212 10:14 Example-Memory/
root@lpic303-ubuntu35:/sys/fs/cgroup#
root@lpic303-ubuntu35:/sys/fs/cgroup# ls Example-Memory/
cgroup.controllers      cpu.max                cpuset.mems.effective  memory.min
cgroup.events           cpu.max.burst          io.max                 memory.numa_stat
cgroup.freeze           cpu.pressure           io.pressure            memory.oom.group
cgroup.kill             cpu.stat               io.prio.class          memory.pressure
cgroup.max.depth        cpu.uclamp.max         io.stat                memory.stat
cgroup.max.descendants  cpu.uclamp.min         io.weight              memory.swap.current
cgroup.procs            cpu.weight             memory.current         memory.swap.events
cgroup.stat             cpu.weight.nice        memory.events          memory.swap.high
cgroup.subtree_control  cpuset.cpus            memory.events.local    memory.swap.max
cgroup.threads          cpuset.cpus.effective  memory.high            pids.current
cgroup.type             cpuset.cpus.partition  memory.low             pids.events
cpu.idle                cpuset.mems            memory.max             pids.max
root@lpic303-ubuntu35:/sys/fs/cgroup#

2. 制御可能なコントローラーを設定する

root@lpic303-ubuntu35:/sys/fs/cgroup#
root@lpic303-ubuntu35:/sys/fs/cgroup# cd Example-Memory/
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory#
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory# cat cgroup.controllers
cpuset cpu io memory pids
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory#
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory# cat cgroup.subtree_control
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory#
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory# echo "+memory" > cgroup.subtree_control
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory#
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory# cat cgroup.subtree_control
memory
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory#

3. メモリの制限をする

  • memory.max:絶対的なリソース制限(ハードリミット)

  • memory.high:ベストエフォート的なリソース制限(ソフトリミット)

  • memory.min:絶対的に保証するメモリ容量(ハードリミット)

  • memory.low:ベストエフォート的に保証するメモリ容量(ソフトリミット)

root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory# 
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory# ls memory.*
memory.current       memory.low        memory.oom.group     memory.swap.events
memory.events        memory.max        memory.pressure      memory.swap.high
memory.events.local  memory.min        memory.stat          memory.swap.max
memory.high          memory.numa_stat  memory.swap.current
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory#

★"memory.max"のデフォルト値は"max"★
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory#
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory# cat memory.max
max
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory#

★"memory.max"128MBに制限する★
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory#
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory# echo "128M" > memory.max
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory#
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory# cat memory.max
134217728
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory#

★"memory.max"の制限を解除する★
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory#
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory# echo "max" > memory.max
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory#
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory# cat memory.max
max
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory#

制限を適用する

PIDを調べて"cgroup.procs"に登録する

root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory#
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory# echo "1234" > cgroup.procs
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory#

4. cgroupを削除する

root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory#
root@lpic303-ubuntu35:/sys/fs/cgroup/Example-Memory# cd ..
root@lpic303-ubuntu35:/sys/fs/cgroup#
root@lpic303-ubuntu35:/sys/fs/cgroup# rmdir Example-Memory/
root@lpic303-ubuntu35:/sys/fs/cgroup#
root@lpic303-ubuntu35:/sys/fs/cgroup# ls -ld Example-Memory
ls: 'Example-Memory' にアクセスできません: そのようなファイルやディレクトリはありません
root@lpic303-ubuntu35:/sys/fs/cgroup#

参考文献


この記事が気に入ったらサポートをしてみませんか?