#!/bin/bash

CGPATH=/sys/fs/cgroup/test.slice

function stop_test() {
	systemctl stop test.slice
}

trap stop_test SIGINT

cat >/etc/systemd/system/test.slice <<EOD
[Slice]
MemoryMax=800M
EOD
cat >/etc/systemd/system/test-A.slice <<EOD
[Slice]
MemoryLow=200M
EOD

systemctl daemon-reload

# swap is enabled, these memeaters alloc anonymous memory which they don't use
systemd-run --slice=test-A.slice -u B.service -p MemoryLow=300M /root/memeater 200 -1 0
systemd-run --slice=test-A.slice -u C.service -p MemoryLow=100M /root/memeater 200 -1 0
systemd-run --slice=test-A.slice -u D.service -p MemoryLow=0M /root/memeater 200 -1 0
systemd-run --slice=test-A.slice -u E.service -p MemoryLow=1000M /root/memeater 0 -1 0
sleep 3
grep . $CGPATH/memory.current $CGPATH/test-A.slice/memory.current $CGPATH/test-A.slice/*.service/memory.current

# pressure is created by allocating anonymous memory and working with all of it
systemd-run --slice=test.slice   -u pressure.service  /root/memeater 590 -1 590

while sleep 1 ; do
	echo
	grep . $CGPATH/memory.current $CGPATH/pressure.service/memory.current $CGPATH/test-A.slice/memory.current $CGPATH/test-A.slice/*.service/memory.current
done
                                                                         
