2021-03-05 12:36:04 +03:00
#!/usr/bin/env bash
2021-10-17 19:13:06 +03:00
# SPDX-License-Identifier: LGPL-2.1-or-later
2021-04-09 20:39:41 +03:00
set -eux
2018-12-13 19:22:01 +03:00
set -o pipefail
2021-03-05 12:36:04 +03:00
systemd-analyze log-level debug
systemd-analyze log-target console
2018-12-13 19:22:01 +03:00
2021-03-05 12:36:04 +03:00
# Loose checks to ensure the environment has the necessary features for systemd-oomd
2021-04-08 01:09:55 +03:00
[ [ -e /proc/pressure ] ] || echo "no PSI" >>/skipped
2021-04-09 20:49:32 +03:00
cgroup_type = " $( stat -fc %T /sys/fs/cgroup/) "
2021-03-05 12:36:04 +03:00
if [ [ " $cgroup_type " != *"cgroup2" * ] ] && [ [ " $cgroup_type " != *"0x63677270" * ] ] ; then
2021-04-08 01:09:55 +03:00
echo "no cgroup2" >>/skipped
2021-03-05 12:36:04 +03:00
fi
if [ ! -f /usr/lib/systemd/systemd-oomd ] && [ ! -f /lib/systemd/systemd-oomd ] ; then
2021-04-08 01:09:55 +03:00
echo "no oomd" >>/skipped
2021-03-05 12:36:04 +03:00
fi
2021-04-09 20:49:32 +03:00
if [ [ -e /skipped ] ] ; then
exit 0
fi
2018-12-13 19:22:01 +03:00
2021-03-05 12:36:04 +03:00
rm -rf /etc/systemd/system/testsuite-55-testbloat.service.d
2018-12-13 19:22:01 +03:00
2022-01-06 23:37:21 +03:00
# Configure oomd explicitly to avoid conflicts with distro dropins
mkdir -p /etc/systemd/oomd.conf.d/
echo -e "[OOM]\nDefaultMemoryPressureDurationSec=2s" >/etc/systemd/oomd.conf.d/99-oomd-test.conf
mkdir -p /etc/systemd/system/-.slice.d/
echo -e "[Slice]\nManagedOOMSwap=auto" >/etc/systemd/system/-.slice.d/99-oomd-test.conf
mkdir -p /etc/systemd/system/user@.service.d/
echo -e "[Service]\nManagedOOMMemoryPressure=auto\nManagedOOMMemoryPressureLimit=0%" >/etc/systemd/system/user@.service.d/99-oomd-test.conf
2018-12-13 19:22:01 +03:00
2021-07-02 20:04:31 +03:00
mkdir -p /etc/systemd/system/systemd-oomd.service.d/
echo -e "[Service]\nEnvironment=SYSTEMD_LOG_LEVEL=debug" >/etc/systemd/system/systemd-oomd.service.d/debug.conf
systemctl daemon-reload
2022-01-12 14:29:34 +03:00
# enable the service to ensure dbus-org.freedesktop.oom1.service exists
# and D-Bus activation works
systemctl enable systemd-oomd.service
2021-07-02 20:04:31 +03:00
# if oomd is already running for some reasons, then restart it to make sure the above settings to be applied
if systemctl is-active systemd-oomd.service; then
systemctl restart systemd-oomd.service
fi
2021-03-05 12:36:04 +03:00
systemctl start testsuite-55-testchill.service
systemctl start testsuite-55-testbloat.service
2018-12-13 19:22:01 +03:00
2021-03-05 12:36:04 +03:00
# Verify systemd-oomd is monitoring the expected units
2021-11-24 12:02:22 +03:00
# Try to avoid racing the oomctl output check by checking in a loop with a timeout
oomctl_output = $( oomctl)
timeout = " $( date -ud "1 minutes" +%s) "
while [ [ $( date -u +%s) -le $timeout ] ] ; do
if grep "/testsuite-55-workload.slice" <<< " $oomctl_output " ; then
break
fi
oomctl_output = $( oomctl)
sleep 1
done
grep "/testsuite-55-workload.slice" <<< " $oomctl_output "
grep "20.00%" <<< " $oomctl_output "
grep "Default Memory Pressure Duration: 2s" <<< " $oomctl_output "
2018-12-13 19:22:01 +03:00
2021-07-02 20:24:30 +03:00
systemctl status testsuite-55-testchill.service
2021-07-02 20:23:11 +03:00
# systemd-oomd watches for elevated pressure for 2 seconds before acting.
2021-03-05 12:36:04 +03:00
# It can take time to build up pressure so either wait 2 minutes or for the service to fail.
2021-04-09 20:49:32 +03:00
timeout = " $( date -ud "2 minutes" +%s) "
2021-03-05 12:36:04 +03:00
while [ [ $( date -u +%s) -le $timeout ] ] ; do
if ! systemctl status testsuite-55-testbloat.service; then
2018-12-13 19:22:01 +03:00
break
2021-03-05 12:36:04 +03:00
fi
2021-07-02 20:23:11 +03:00
sleep 2
2018-12-13 19:22:01 +03:00
done
2021-03-05 12:36:04 +03:00
# testbloat should be killed and testchill should be fine
if systemctl status testsuite-55-testbloat.service; then exit 42; fi
if ! systemctl status testsuite-55-testchill.service; then exit 24; fi
2021-09-09 18:12:55 +03:00
# Make sure we also work correctly on user units.
2021-11-23 00:12:09 +03:00
systemctl start --machine "testuser@.host" --user testsuite-55-testchill.service
systemctl start --machine "testuser@.host" --user testsuite-55-testbloat.service
2021-09-09 18:12:55 +03:00
# Verify systemd-oomd is monitoring the expected units
2021-11-24 12:02:22 +03:00
# Try to avoid racing the oomctl output check by checking in a loop with a timeout
oomctl_output = $( oomctl)
timeout = " $( date -ud "1 minutes" +%s) "
while [ [ $( date -u +%s) -le $timeout ] ] ; do
if grep -E "/user.slice.*/testsuite-55-workload.slice" <<< " $oomctl_output " ; then
break
fi
oomctl_output = $( oomctl)
sleep 1
done
grep -E "/user.slice.*/testsuite-55-workload.slice" <<< " $oomctl_output "
grep "20.00%" <<< " $oomctl_output "
grep "Default Memory Pressure Duration: 2s" <<< " $oomctl_output "
2021-09-09 18:12:55 +03:00
2021-11-23 00:12:09 +03:00
systemctl --machine "testuser@.host" --user status testsuite-55-testchill.service
2021-09-09 18:12:55 +03:00
# systemd-oomd watches for elevated pressure for 2 seconds before acting.
# It can take time to build up pressure so either wait 2 minutes or for the service to fail.
timeout = " $( date -ud "2 minutes" +%s) "
while [ [ $( date -u +%s) -le $timeout ] ] ; do
2021-11-23 00:12:09 +03:00
if ! systemctl --machine "testuser@.host" --user status testsuite-55-testbloat.service; then
2021-09-09 18:12:55 +03:00
break
fi
sleep 2
done
# testbloat should be killed and testchill should be fine
2021-11-23 00:12:09 +03:00
if systemctl --machine "testuser@.host" --user status testsuite-55-testbloat.service; then exit 42; fi
if ! systemctl --machine "testuser@.host" --user status testsuite-55-testchill.service; then exit 24; fi
2021-09-09 18:12:55 +03:00
2021-03-05 12:36:04 +03:00
# only run this portion of the test if we can set xattrs
if setfattr -n user.xattr_test -v 1 /sys/fs/cgroup/; then
sleep 120 # wait for systemd-oomd kill cool down and elevated memory pressure to come down
mkdir -p /etc/systemd/system/testsuite-55-testbloat.service.d/
2021-04-08 01:09:55 +03:00
echo "[Service]" >/etc/systemd/system/testsuite-55-testbloat.service.d/override.conf
echo "ManagedOOMPreference=avoid" >>/etc/systemd/system/testsuite-55-testbloat.service.d/override.conf
2021-03-05 12:36:04 +03:00
systemctl daemon-reload
systemctl start testsuite-55-testchill.service
systemctl start testsuite-55-testmunch.service
systemctl start testsuite-55-testbloat.service
2021-04-09 20:49:32 +03:00
timeout = " $( date -ud "2 minutes" +%s) "
while [ [ " $( date -u +%s) " -le " $timeout " ] ] ; do
2021-03-05 12:36:04 +03:00
if ! systemctl status testsuite-55-testmunch.service; then
break
fi
2021-09-12 10:02:31 +03:00
sleep 2
2021-03-05 12:36:04 +03:00
done
# testmunch should be killed since testbloat had the avoid xattr on it
if ! systemctl status testsuite-55-testbloat.service; then exit 25; fi
if systemctl status testsuite-55-testmunch.service; then exit 43; fi
if ! systemctl status testsuite-55-testchill.service; then exit 24; fi
fi
systemd-analyze log-level info
2021-04-08 01:09:55 +03:00
echo OK >/testok
2018-12-13 19:22:01 +03:00
exit 0