2023年12月6日发(作者:)

systemd限制服务进程的内存最大使用量的例子

systemd 限制服务进程的内存最大使用量的例子

此实验在SLES12sp3通过

创建一个内存测试的cgroup

linux-d4xo-2:~ # cgcreate -g memory:test

linux-d4xo-2:~ # ls /sys/fs/cgroup/memory/test

_children t _limit_in_bytes _stat _limit_in_bytes

_in_bytes tasks

_control _empty _usage_in_bytes _control

_hierarchy

_in_bytes _charge_at_immigrate re_level ness

notify_on_release

设置内存最大使用量32M,超过后oom-kill此进程

linux-d4xo-2:~ # cat /sys/fs/cgroup/memory/test/_in_bytes

9223372

linux-d4xo-2:~ # cat /sys/fs/cgroup/memory/test/_control

oom_kill_disable 0

under_oom 0

linux-d4xo-2:~ # echo 32M > /sys/fs/cgroup/memory/test/_in_bytes

linux-d4xo-2:~ # cat /sys/fs/cgroup/memory/test/_in_bytes

33554432

linux-d4xo-2:~ # cat /sys/fs/cgroup/memory/test/_in_bytes

0

启动一个消耗内存的进程, 观察到内存使用量增长以及failcnt增长,达到oom-kill的条件后此进程被杀掉,可以看到最大内

存使用量达到限制值。linux-d4xo-2:~ # cgexec -g memory:test /root/temp/sh_scripts/useup_ &

[1] 12059

linux-d4xo-2:~ # cat /sys/fs/cgroup/memory/test/t

691289

linux-d4xo-2:~ # cat /sys/fs/cgroup/memory/test/_in_bytes

33538048

linux-d4xo-2:~ # cat /sys/fs/cgroup/memory/test/_usage_in_bytes

33554432

linux-d4xo-2:~ # cat /sys/fs/cgroup/memory/test/_in_bytes

33480704

linux-d4xo-2:~ # cat /sys/fs/cgroup/memory/test/t

934111

linux-d4xo-2:~ # cat /sys/fs/cgroup/memory/test/tasks

12059

linux-d4xo-2:~ # cat /sys/fs/cgroup/memory/test/t

1268327

linux-d4xo-2:~ # cat /sys/fs/cgroup/memory/test/t

1354078

linux-d4xo-2:~ # cat /sys/fs/cgroup/memory/test/_in_bytes

33472512

linux-d4xo-2:~ # cat /sys/fs/cgroup/memory/test/

[1]+ Killed cgexec -g memory:test /root/temp/sh_scripts/useup_

linux-d4xo-2:~ # cat /sys/fs/cgroup/memory/test/t

1398345

linux-d4xo-2:~ # cat /sys/fs/cgroup/memory/test/_in_bytes

0

linux-d4xo-2:~ # cat /sys/fs/cgroup/memory/test/_usage_in_bytes

33554432

安装和启动memtest服务,可以看到对应的cgroup的限制以及进程号的变化,说明确实发生了oom并且有重新启动了服务

进程。

linux-d4xo-2:~/temp/systemd_cgroup # cp e /etc/systemd/system/

linux-d4xo-2:~/temp/systemd_cgroup # systemctl status e

● e - Systemd memmory resource limit test

Loaded: loaded (/etc/systemd/system/e; disabled; vendor preset: disabled)

Active: inactive (dead)

linux-d4xo-2:~/temp/systemd_cgroup # systemctl start e

linux-d4xo-2:~/temp/systemd_cgroup # systemctl status e

● e - Systemd memmory resource limit test

Loaded: loaded (/etc/systemd/system/e; disabled; vendor preset: disabled)

Active: active (running) since Wed 2021-01-13 13:13:36 CST; 2s ago

Main PID: 12415 (useup_)

Tasks: 2 (limit: 512)

Memory: 400.0K (limit: 32.0M)

CGroup: //e

├─12415 /bin/bash /root/temp/sh_scripts/useup_

└─12448 sleep 1Jan 13 13:13:36 linux-d4xo-2 systemd[1]: Started Systemd memmory resource limit -d4xo-2:~/temp/systemd_cgroup # ls /sys/fs/cgroup/memory//e/_children t _limit_in_bytes _stat _limit_in__in_bytes _control _empty _usage_in_bytes _control _ _in_bytes _charge_at_immigrate re_level nessnotify_on_releaselinux-d4xo-2:~/temp/systemd_cgroup # cat /sys/fs/cgroup/memory//e/_in_bytes33554432linux-d4xo-2:~/temp/systemd_cgroup # cat/sys/fs/cgroup/memory//e/_in_bytes33538048linux-d4xo-2:~/temp/systemd_cgroup # cat/sys/fs/cgroup/memory//e/_usage_in_bytes33554432linux-d4xo-2:~/temp/systemd_cgroup # cat /sys/fs/cgroup/memory//e/t321996linux-d4xo-2:~/temp/systemd_cgroup # systemctl status e● e - Systemd memmory resource limit testLoaded: loaded (/etc/systemd/system/e; disabled; vendor preset: disabled)Active: active (running) since Wed 2021-01-13 13:14:48 CST; 38s agoMain PID: 12473 (useup_)Tasks: 1 (limit: 512)Memory: 31.8M (limit: 32.0M)CGroup: //e└─12473 /bin/bash /root/temp/sh_scripts/useup_ 13 13:14:48 linux-d4xo-2 systemd[1]: e: Service hold-off time over, scheduling 13 13:14:48 linux-d4xo-2 systemd[1]: Stopped Systemd memmory resource limit 13 13:14:48 linux-d4xo-2 systemd[1]: Started Systemd memmory resource limit -d4xo-2:~/temp/systemd_cgroup # systemctl status e● e - Systemd memmory resource limit testLoaded: loaded (/etc/systemd/system/e; disabled; vendor preset: disabled)Active: active (running) since Wed 2021-01-13 13:14:48 CST; 1min 9s agoMain PID: 12473 (useup_)Tasks: 1 (limit: 512)Memory: 31.9M (limit: 32.0M)CGroup: //e└─12473 /bin/bash /root/temp/sh_scripts/useup_ 13 13:14:48 linux-d4xo-2 systemd[1]: e: Service hold-off time over, scheduling 13 13:14:48 linux-d4xo-2 systemd[1]: Stopped Systemd memmory resource limit 13 13:14:48 linux-d4xo-2 systemd[1]: Started Systemd memmory resource limit -d4xo-2:~/temp/systemd_cgroup # cat /sys/fs/cgroup/memory//e/t175667linux-d4xo-2:~/temp/systemd_cgroup # systemctl status e● e - Systemd memmory resource limit testLoaded: loaded (/etc/systemd/system/e; disabled; vendor preset: disabled)Active: active (running) since Wed 2021-01-13 13:15:58 CST; 29s agoMain PID: 12549 (useup_)Tasks: 1 (limit: 512)Memory: 31.9M (limit: 32.0M)CGroup: //e└─12549 /bin/bash /root/temp/sh_scripts/useup_ 13 13:15:58 linux-d4xo-2 systemd[1]: e: Service hold-off time over, scheduling restart.

Jan 13 13:15:58 linux-d4xo-2 systemd[1]: Stopped Systemd memmory resource limit test.

Jan 13 13:15:58 linux-d4xo-2 systemd[1]: Started Systemd memmory resource limit test.

linux-d4xo-2:~/temp/systemd_cgroup # systemctl stop e

linux-d4xo-2:~/temp/systemd_cgroup # systemctl status e -l

● e - Systemd memmory resource limit test

Loaded: loaded (/etc/systemd/system/e; disabled; vendor preset: disabled)

Active: inactive (dead)

Jan 13 13:15:58 linux-d4xo-2 systemd[1]: Stopped Systemd memmory resource limit test.

Jan 13 13:15:58 linux-d4xo-2 systemd[1]: Started Systemd memmory resource limit test.

Jan 13 13:17:10 linux-d4xo-2 systemd[1]: e: Main process exited, code=killed, status=9/KILL

Jan 13 13:17:10 linux-d4xo-2 systemd[1]: e: Unit entered failed state.

Jan 13 13:17:10 linux-d4xo-2 systemd[1]: e: Failed with result ‘signal’.

Jan 13 13:17:11 linux-d4xo-2 systemd[1]: e: Service hold-off time over, scheduling restart.

Jan 13 13:17:11 linux-d4xo-2 systemd[1]: Stopped Systemd memmory resource limit test.

Jan 13 13:17:11 linux-d4xo-2 systemd[1]: Started Systemd memmory resource limit test.

Jan 13 13:17:48 linux-d4xo-2 systemd[1]: Stopping Systemd memmory resource limit test…

Jan 13 13:17:48 linux-d4xo-2 systemd[1]: Stopped Systemd memmory resource limit test.

systemd内存限制测试用的service文件和不断消耗内存的shell脚本

linux-d4xo-2:~/temp/systemd_cgroup # cat e

[Unit]

Description=Systemd memmory resource limit test

[Service]

ExecStart=/root/temp/sh_scripts/useup_

ExecReload=/bin/kill -HUP $MAINPID

KillMode=process

Restart=always

MemoryLimit=32M

[Install]

WantedBy=

linux-d4xo-2:~/temp/systemd_cgroup # cat /root/temp/sh_scripts/useup_

#!/bin/bash

x=“hahahadadadadada”

while [ True ];do

x=

x

x

x

x

x

x

x

x

x

x

sleep 1

done;

这个例子里swap还会用到,直到swap耗尽,才发生oom-kill

linux-d4xo-2:~/temp/systemd_cgroup # systemctl status e -l

● e - Systemd memmory resource limit test

Loaded: loaded (/etc/systemd/system/e; disabled; vendor preset: disabled)

Active: inactive (dead)Jan 13 13:15:58 linux-d4xo-2 systemd[1]: Stopped Systemd memmory resource limit 13 13:15:58 linux-d4xo-2 systemd[1]: Started Systemd memmory resource limit 13 13:17:10 linux-d4xo-2 systemd[1]: e: Main process exited, code=killed, status=9/KILLJan 13 13:17:10 linux-d4xo-2 systemd[1]: e: Unit entered failed 13 13:17:10 linux-d4xo-2 systemd[1]: e: Failed with result ‘signal’.Jan 13 13:17:11 linux-d4xo-2 systemd[1]: e: Service hold-off time over, scheduling 13 13:17:11 linux-d4xo-2 systemd[1]: Stopped Systemd memmory resource limit 13 13:17:11 linux-d4xo-2 systemd[1]: Started Systemd memmory resource limit 13 13:17:48 linux-d4xo-2 systemd[1]: Stopping Systemd memmory resource limit test…Jan 13 13:17:48 linux-d4xo-2 systemd[1]: Stopped Systemd memmory resource limit -d4xo-2:~/temp/systemd_cgroup # systemctl start elinux-d4xo-2:~/temp/systemd_cgroup # systemctl status e● e - Systemd memmory resource limit testLoaded: loaded (/etc/systemd/system/e; disabled; vendor preset: disabled)Active: active (running) since Wed 2021-01-13 13:28:49 CST; 4s agoMain PID: 12698 (useup_)Tasks: 2 (limit: 512)Memory: 3.8M (limit: 32.0M)CGroup: //e├─12698 /bin/bash /root/temp/sh_scripts/useup_└─12708 sleep 1Jan 13 13:28:49 linux-d4xo-2 systemd[1]: Started Systemd memmory resource limit -d4xo-2:~/temp/systemd_cgroup # systemctl status e -l● e - Systemd memmory resource limit testLoaded: loaded (/etc/systemd/system/e; disabled; vendor preset: disabled)Active: active (running) since Wed 2021-01-13 13:28:49 CST; 8s agoMain PID: 12698 (useup_)Tasks: 1 (limit: 512)Memory: 31.9M (limit: 32.0M)CGroup: //e└─12698 /bin/bash /root/temp/sh_scripts/useup_ 13 13:28:49 linux-d4xo-2 systemd[1]: Started Systemd memmory resource limit -d4xo-2:~/temp/systemd_cgroup # free -mtotal used free shared buffers cachedMem: 1980 1244 736 10 2 917-/+ buffers/cache: 324 1656Swap: 1088 286 802linux-d4xo-2:~/temp/systemd_cgroup # free -mtotal used free shared buffers cachedMem: 1980 1244 736 10 2 919-/+ buffers/cache: 323 1657Swap: 1088 193 895linux-d4xo-2:~/temp/systemd_cgroup # systemctl status e -l● e - Systemd memmory resource limit testLoaded: loaded (/etc/systemd/system/e; disabled; vendor preset: disabled)Active: active (running) since Wed 2021-01-13 13:28:49 CST; 30s agoMain PID: 12698 (useup_)Tasks: 1 (limit: 512)Memory: 31.9M (limit: 32.0M)CGroup: //e└─12698 /bin/bash /root/temp/sh_scripts/useup_ 13 13:28:49 linux-d4xo-2 systemd[1]: Started Systemd memmory resource limit -d4xo-2:~/temp/systemd_cgroup # free -mtotal used free shared buffers cachedMem: 1980 1245 735 10 2 899-/+ buffers/cache: 343 1637Swap: 1088 595 493linux-d4xo-2:~/temp/systemd_cgroup # free -mtotal used free shared buffers cachedMem: 1980 1282 698 10 2 936-/+ buffers/cache: 344 1636Swap: 1088 916 172linux-d4xo-2:~/temp/systemd_cgroup # free -mtotal used free shared buffers cachedMem: 1980 1302 678 10 2 955-/+ buffers/cache: 344 1636Swap: 1088 992 96linux-d4xo-2:~/temp/systemd_cgroup # free -mtotal used free shared buffers cachedMem: 1980 1297 683 10 2 951-/+ buffers/cache: 344 1636Swap: 1088 916 172linux-d4xo-2:~/temp/systemd_cgroup # free -mtotal used free shared buffers cachedMem: 1980 1299 680 10 2 953-/+ buffers/cache: 344 1636Swap: 1088 963 125linux-d4xo-2:~/temp/systemd_cgroup # free -mtotal used free shared buffers cachedMem: 1980 1309 671 10 2 963-/+ buffers/cache: 344 1636Swap: 1088 1058 30linux-d4xo-2:~/temp/systemd_cgroup # free -mtotal used free shared buffers cachedMem: 1980 1336 644 10 2 989-/+ buffers/cache: 344 1636Swap: 1088 1068 20linux-d4xo-2:~/temp/systemd_cgroup # free -mtotal used free shared buffers cachedMem: 1980 1212 768 10 2 899-/+ buffers/cache: 310 1670Swap: 1088 0 1088linux-d4xo-2:~/temp/systemd_cgroup # systemctl status e -l● e - Systemd memmory resource limit testLoaded: loaded (/etc/systemd/system/e; disabled; vendor preset: disabled)Active: active (running) since Wed 2021-01-13 13:29:59 CST; 7s agoMain PID: 12761 (useup_)Tasks: 1 (limit: 512)Memory: 31.9M (limit: 32.0M)CGroup: //e└─12761 /bin/bash /root/temp/sh_scripts/useup_ 13 13:29:59 linux-d4xo-2 systemd[1]: e: Service hold-off time over, scheduling 13 13:29:59 linux-d4xo-2 systemd[1]: Stopped Systemd memmory resource limit 13 13:29:59 linux-d4xo-2 systemd[1]: Started Systemd memmory resource limit test.限制swappiness

新建一个目录,

限制swappiness=0

在e中设置Slice=,之后memtest不在使用swap

linux-d4xo-2:~/temp/systemd_cgroup # cat e

[Unit]

Description=Systemd memmory resource limit test

[Service]

ExecStart=/root/temp/sh_scripts/useup_

ExecReload=/bin/kill -HUP $MAINPID

KillMode=process

Restart=always

MemoryLimit=32M

Slice=

[Install]

WantedBy=

linux-d4xo-2:~/temp/systemd_cgroup # mkdir /sys/fs/cgroup/memory//

linux-d4xo-2:~/temp/systemd_cgroup # cat /sys/fs/cgroup/memory//ness

60

linux-d4xo-2:~/temp/systemd_cgroup # cat /sys/fs/cgroup/memory//_in_bytes

9223372

linux-d4xo-2:~/temp/systemd_cgroup # echo 0 > /sys/fs/cgroup/memory//ness

linux-d4xo-2:~/temp/systemd_cgroup # cat /sys/fs/cgroup/memory//ness

0

linux-d4xo-2:~/temp/systemd_cgroup # systemctl status e

● e - Systemd memmory resource limit test

Loaded: loaded (/etc/systemd/system/e; disabled; vendor preset: disabled)

Active: inactive (dead)

Jan 13 13:29:59 linux-d4xo-2 systemd[1]: e: Unit entered failed state.

Jan 13 13:29:59 linux-d4xo-2 systemd[1]: e: Failed with result ‘signal’.

Jan 13 13:29:59 linux-d4xo-2 systemd[1]: e: Service hold-off time over, scheduling restart.

Jan 13 13:29:59 linux-d4xo-2 systemd[1]: Stopped Systemd memmory resource limit test.

Jan 13 13:29:59 linux-d4xo-2 systemd[1]: Started Systemd memmory resource limit test.

Jan 13 13:30:15 linux-d4xo-2 systemd[1]: Stopping Systemd memmory resource limit test…

Jan 13 13:30:15 linux-d4xo-2 systemd[1]: Stopped Systemd memmory resource limit test.

Jan 13 14:02:44 linux-d4xo-2 systemd[1]: Started Systemd memmory resource limit test.

Jan 13 14:03:32 linux-d4xo-2 systemd[1]: Stopping Systemd memmory resource limit test…

Jan 13 14:03:32 linux-d4xo-2 systemd[1]: Stopped Systemd memmory resource limit test.

linux-d4xo-2:~/temp/systemd_cgroup # systemctl start e

linux-d4xo-2:~/temp/systemd_cgroup # systemctl status e

● e - Systemd memmory resource limit test

Loaded: loaded (/etc/systemd/system/e; disabled; vendor preset: disabled)

Active: active (running) since Wed 2021-01-13 14:08:29 CST; 3s ago

Main PID: 13284 (useup_)

Tasks: 2 (limit: 512)

Memory: 824.0K (limit: 32.0M)

CGroup: //e

├─13284 /bin/bash /root/temp/sh_scripts/useup_

└─13294 sleep 1Jan 13 14:08:29 linux-d4xo-2 systemd[1]: Started Systemd memmory resource limit test.

linux-d4xo-2:~/temp/systemd_cgroup # cat /sys/fs/cgroup/memory//ness

0

linux-d4xo-2:~/temp/systemd_cgroup # cat /sys/fs/cgroup/memory//e/ness

0

linux-d4xo-2:~/temp/systemd_cgroup # cat

/sys/fs/cgroup/memory//e/_in_bytes

33554432

linux-d4xo-2:~/temp/systemd_cgroup # free -m

total used free shared buffers cached

Mem: 1980 1228 751 10 2 916

-/+ buffers/cache: 310 1670

Swap: 1088 0 1088

linux-d4xo-2:~/temp/systemd_cgroup # free -m

total used free shared buffers cached

Mem: 1980 1231 748 10 2 916

-/+ buffers/cache: 313 1667

Swap: 1088 0 1088

linux-d4xo-2:~/temp/systemd_cgroup # free -m

total used free shared buffers cached

Mem: 1980 1228 752 10 2 916

-/+ buffers/cache: 310 1670

Swap: 1088 0 1088

linux-d4xo-2:~/temp/systemd_cgroup # cat

/sys/fs/cgroup/memory//e/_in_bytes

475136

linux-d4xo-2:~/temp/systemd_cgroup # cat

/sys/fs/cgroup/memory//e/_in_bytes

745472

linux-d4xo-2:~/temp/systemd_cgroup # systemctl status e

● e - Systemd memmory resource limit test

Loaded: loaded (/etc/systemd/system/e; disabled; vendor preset: disabled)

Active: active (running) since Wed 2021-01-13 14:09:18 CST; 3s ago

Main PID: 13425 (useup_)

Tasks: 2 (limit: 512)

Memory: 780.0K (limit: 32.0M)

CGroup: //e

├─13425 /bin/bash /root/temp/sh_scripts/useup_

└─13432 sleep 1

Jan 13 14:09:18 linux-d4xo-2 systemd[1]: Started Systemd memmory resource limit test.

总结

其它systemd的资源限制也可采用类似方法加到service文件中,参考 man ce-control.

systemd未提供接口的资源限制也可以直接修改对应的cgroup文件。