b9a53a
From 90dda340e4adeb1126639a849d4f31ae327fdc4b Mon Sep 17 00:00:00 2001
b9a53a
From: Frantisek Sumsal <frantisek@sumsal.cz>
b9a53a
Date: Tue, 25 Jun 2019 23:01:40 +0200
be3800
Subject: [PATCH] test: introduce TEST-36-NUMAPOLICY
b9a53a
b9a53a
(cherry picked from commit 8f65e26508969610ac934d1aadbade8223bfcaac)
b9a53a
b9a53a
Related: #1808940
b9a53a
---
b9a53a
 test/TEST-36-NUMAPOLICY/Makefile     |   1 +
b9a53a
 test/TEST-36-NUMAPOLICY/test.sh      |  51 +++++
b9a53a
 test/TEST-36-NUMAPOLICY/testsuite.sh | 292 +++++++++++++++++++++++++++
b9a53a
 3 files changed, 344 insertions(+)
b9a53a
 create mode 120000 test/TEST-36-NUMAPOLICY/Makefile
b9a53a
 create mode 100755 test/TEST-36-NUMAPOLICY/test.sh
b9a53a
 create mode 100755 test/TEST-36-NUMAPOLICY/testsuite.sh
b9a53a
b9a53a
diff --git a/test/TEST-36-NUMAPOLICY/Makefile b/test/TEST-36-NUMAPOLICY/Makefile
b9a53a
new file mode 120000
b9a53a
index 0000000000..e9f93b1104
b9a53a
--- /dev/null
b9a53a
+++ b/test/TEST-36-NUMAPOLICY/Makefile
b9a53a
@@ -0,0 +1 @@
b9a53a
+../TEST-01-BASIC/Makefile
b9a53a
\ No newline at end of file
b9a53a
diff --git a/test/TEST-36-NUMAPOLICY/test.sh b/test/TEST-36-NUMAPOLICY/test.sh
b9a53a
new file mode 100755
b9a53a
index 0000000000..a0d8623e8e
b9a53a
--- /dev/null
b9a53a
+++ b/test/TEST-36-NUMAPOLICY/test.sh
b9a53a
@@ -0,0 +1,51 @@
b9a53a
+#!/bin/bash
b9a53a
+set -e
b9a53a
+TEST_DESCRIPTION="test MUMAPolicy= and NUMAMask= options"
b9a53a
+TEST_NO_NSPAWN=1
b9a53a
+QEMU_OPTIONS="-numa node,nodeid=0"
b9a53a
+
b9a53a
+. $TEST_BASE_DIR/test-functions
b9a53a
+
b9a53a
+test_setup() {
b9a53a
+    create_empty_image
b9a53a
+    mkdir -p $TESTDIR/root
b9a53a
+    mount ${LOOPDEV}p1 $TESTDIR/root
b9a53a
+
b9a53a
+    (
b9a53a
+        LOG_LEVEL=5
b9a53a
+        eval $(udevadm info --export --query=env --name=${LOOPDEV}p2)
b9a53a
+
b9a53a
+        setup_basic_environment
b9a53a
+        inst_binary mktemp
b9a53a
+
b9a53a
+        # mask some services that we do not want to run in these tests
b9a53a
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-hwdb-update.service
b9a53a
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-journal-catalog-update.service
b9a53a
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-networkd.service
b9a53a
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-networkd.socket
b9a53a
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-resolved.service
b9a53a
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-machined.service
b9a53a
+
b9a53a
+        # setup the testsuite service
b9a53a
+        cat >$initdir/etc/systemd/system/testsuite.service <
b9a53a
+[Unit]
b9a53a
+Description=Testsuite service
b9a53a
+
b9a53a
+[Service]
b9a53a
+ExecStart=/bin/bash -x /testsuite.sh
b9a53a
+Type=oneshot
b9a53a
+StandardOutput=tty
b9a53a
+StandardError=tty
b9a53a
+NotifyAccess=all
b9a53a
+EOF
b9a53a
+        cp testsuite.sh $initdir/
b9a53a
+
b9a53a
+        setup_testsuite
b9a53a
+    ) || return 1
b9a53a
+    setup_nspawn_root
b9a53a
+
b9a53a
+    ddebug "umount $TESTDIR/root"
b9a53a
+    umount $TESTDIR/root
b9a53a
+}
b9a53a
+
b9a53a
+do_test "$@"
b9a53a
diff --git a/test/TEST-36-NUMAPOLICY/testsuite.sh b/test/TEST-36-NUMAPOLICY/testsuite.sh
b9a53a
new file mode 100755
b9a53a
index 0000000000..e15087b137
b9a53a
--- /dev/null
b9a53a
+++ b/test/TEST-36-NUMAPOLICY/testsuite.sh
b9a53a
@@ -0,0 +1,292 @@
b9a53a
+#!/bin/bash
b9a53a
+set -ex
b9a53a
+set -o pipefail
b9a53a
+
b9a53a
+at_exit() {
b9a53a
+    if [ $? -ne 0 ]; then
b9a53a
+        # We're exiting with a non-zero EC, let's dump test artifacts
b9a53a
+        # for easier debugging
b9a53a
+        [ -f "$straceLog" ] && cat "$straceLog"
b9a53a
+        [ -f "$journalLog" ] && cat "$journalLog"
b9a53a
+    fi
b9a53a
+}
b9a53a
+
b9a53a
+trap at_exit EXIT
b9a53a
+
b9a53a
+systemd-analyze log-level debug
b9a53a
+systemd-analyze log-target journal
b9a53a
+
b9a53a
+# Log files
b9a53a
+straceLog='strace.log'
b9a53a
+journalLog='journal.log'
b9a53a
+
b9a53a
+# Systemd config files
b9a53a
+testUnit='numa-test.service'
b9a53a
+testUnitFile="/etc/systemd/system/$testUnit"
b9a53a
+testUnitNUMAConf="$testUnitFile.d/numa.conf"
b9a53a
+
b9a53a
+# Sleep constants (we should probably figure out something better but nothing comes to mind)
b9a53a
+journalSleep=5
b9a53a
+sleepAfterStart=1
b9a53a
+
b9a53a
+startStrace() {
b9a53a
+    coproc strace -qq -p 1 -o $straceLog -e set_mempolicy -s 1024 $1
b9a53a
+}
b9a53a
+
b9a53a
+stopStrace() {
b9a53a
+    kill -s TERM $COPROC_PID
b9a53a
+}
b9a53a
+
b9a53a
+startJournalctl() {
b9a53a
+    coproc journalctl -u init.scope -f > $journalLog
b9a53a
+}
b9a53a
+
b9a53a
+stopJournalctl() {
b9a53a
+    # Wait a few seconds until the messages get properly queued...
b9a53a
+    sleep $journalSleep
b9a53a
+    # ...and then force journald to write them to the backing storage
b9a53a
+    # Also, using journalctl --sync should be better than using SIGRTMIN+1, as
b9a53a
+    # the --sync wait until the synchronization is complete
b9a53a
+    echo "Force journald to write all queued messages"
b9a53a
+    journalctl --sync
b9a53a
+    kill -s TERM $COPROC_PID
b9a53a
+}
b9a53a
+
b9a53a
+checkNUMA() {
b9a53a
+    # NUMA enabled system should have at least NUMA node0
b9a53a
+    test -e /sys/devices/system/node/node0
b9a53a
+}
b9a53a
+
b9a53a
+writePID1NUMAPolicy() {
b9a53a
+    echo [Manager] > $confDir/numa.conf
b9a53a
+    echo NUMAPolicy=$1 >> $confDir/numa.conf
b9a53a
+    echo NUMAMask=$2>> $confDir/numa.conf
b9a53a
+}
b9a53a
+
b9a53a
+writeTestUnit() {
b9a53a
+    echo [Service] > $testUnitFile
b9a53a
+    echo ExecStart=/bin/sleep 3600 >> $testUnitFile
b9a53a
+    mkdir -p $testUnitFile.d/
b9a53a
+}
b9a53a
+
b9a53a
+writeTestUnitNUMAPolicy() {
b9a53a
+    echo [Service] > $testUnitNUMAConf
b9a53a
+    echo NUMAPolicy=$1 >> $testUnitNUMAConf
b9a53a
+    echo NUMAMask=$2>> $testUnitNUMAConf
b9a53a
+    systemctl daemon-reload
b9a53a
+}
b9a53a
+
b9a53a
+pid1ReloadWithStrace() {
b9a53a
+    startStrace
b9a53a
+    systemctl daemon-reload
b9a53a
+    stopStrace
b9a53a
+}
b9a53a
+
b9a53a
+pid1ReloadWithJournal() {
b9a53a
+    startJournalctl
b9a53a
+    systemctl daemon-reload
b9a53a
+    stopJournalctl
b9a53a
+}
b9a53a
+
b9a53a
+pid1StartUnitWithStrace() {
b9a53a
+    startStrace '-f'
b9a53a
+    systemctl start $1
b9a53a
+    sleep $sleepAfterStart
b9a53a
+    stopStrace
b9a53a
+}
b9a53a
+
b9a53a
+pid1StartUnitWithJournal() {
b9a53a
+    startJournalctl
b9a53a
+    systemctl start $1
b9a53a
+    sleep $sleepAfterStart
b9a53a
+    stopJournalctl
b9a53a
+}
b9a53a
+
b9a53a
+pid1StopUnit() {
b9a53a
+    systemctl stop $1
b9a53a
+}
b9a53a
+
b9a53a
+systemctlCheckNUMAProperties() {
b9a53a
+    local LOGFILE="$(mktemp)"
b9a53a
+    systemctl show -p NUMAPolicy $1 > "$LOGFILE"
b9a53a
+    grep "NUMAPolicy=$2" "$LOGFILE"
b9a53a
+
b9a53a
+    > "$LOGFILE"
b9a53a
+
b9a53a
+    if [ -n $3 ]; then
b9a53a
+        systemctl show -p NUMAMask $1 > "$LOGFILE"
b9a53a
+        grep "NUMAMask=$3" "$LOGFILE"
b9a53a
+    fi
b9a53a
+}
b9a53a
+
b9a53a
+checkNUMA
b9a53a
+writeTestUnit
b9a53a
+
b9a53a
+# Create systemd config drop-in directory
b9a53a
+confDir="/etc/systemd/system.conf.d/"
b9a53a
+mkdir -p "$confDir"
b9a53a
+
b9a53a
+echo "PID1 NUMAPolicy support - Default policy w/o mask"
b9a53a
+writePID1NUMAPolicy "default"
b9a53a
+pid1ReloadWithStrace
b9a53a
+# Kernel requires that nodemask argument is set to NULL when setting default policy
b9a53a
+grep "set_mempolicy(MPOL_DEFAULT, NULL" $straceLog
b9a53a
+
b9a53a
+echo "PID1 NUMAPolicy support - Default policy w/ mask"
b9a53a
+writePID1NUMAPolicy "default" "0"
b9a53a
+pid1ReloadWithStrace
b9a53a
+grep "set_mempolicy(MPOL_DEFAULT, NULL" $straceLog
b9a53a
+
b9a53a
+echo "PID1 NUMAPolicy support - Bind policy w/o mask"
b9a53a
+writePID1NUMAPolicy "bind"
b9a53a
+pid1ReloadWithJournal
b9a53a
+grep "Failed to set NUMA memory policy: Invalid argument" $journalLog
b9a53a
+
b9a53a
+echo "PID1 NUMAPolicy support - Bind policy w/ mask"
b9a53a
+writePID1NUMAPolicy "bind" "0"
b9a53a
+pid1ReloadWithStrace
b9a53a
+grep -P "set_mempolicy\(MPOL_BIND, \[0x0*1\]" $straceLog
b9a53a
+
b9a53a
+echo "PID1 NUMAPolicy support - Interleave policy w/o mask"
b9a53a
+writePID1NUMAPolicy "interleave"
b9a53a
+pid1ReloadWithJournal
b9a53a
+grep "Failed to set NUMA memory policy: Invalid argument" $journalLog
b9a53a
+
b9a53a
+echo "PID1 NUMAPolicy support - Interleave policy w/ mask"
b9a53a
+writePID1NUMAPolicy "interleave" "0"
b9a53a
+pid1ReloadWithStrace
b9a53a
+grep -P "set_mempolicy\(MPOL_INTERLEAVE, \[0x0*1\]" $straceLog
b9a53a
+
b9a53a
+echo "PID1 NUMAPolicy support - Preferred policy w/o mask"
b9a53a
+writePID1NUMAPolicy "preferred"
b9a53a
+pid1ReloadWithJournal
b9a53a
+# Preferred policy with empty node mask is actually allowed and should reset allocation policy to default
b9a53a
+! grep "Failed to set NUMA memory policy: Invalid argument" $journalLog
b9a53a
+
b9a53a
+echo "PID1 NUMAPolicy support - Preferred policy w/ mask"
b9a53a
+writePID1NUMAPolicy "preferred" "0"
b9a53a
+pid1ReloadWithStrace
b9a53a
+grep -P "set_mempolicy\(MPOL_PREFERRED, \[0x0*1\]" $straceLog
b9a53a
+
b9a53a
+echo "PID1 NUMAPolicy support - Local policy w/o mask"
b9a53a
+writePID1NUMAPolicy "local"
b9a53a
+pid1ReloadWithStrace
b9a53a
+# Kernel requires that nodemask argument is set to NULL when setting default policy
b9a53a
+grep "set_mempolicy(MPOL_LOCAL, NULL" $straceLog
b9a53a
+
b9a53a
+echo "PID1 NUMAPolicy support - Local policy w/ mask"
b9a53a
+writePID1NUMAPolicy "local" "0"
b9a53a
+pid1ReloadWithStrace
b9a53a
+grep "set_mempolicy(MPOL_LOCAL, NULL" $straceLog
b9a53a
+
b9a53a
+echo "Unit file NUMAPolicy support - Default policy w/o mask"
b9a53a
+writeTestUnitNUMAPolicy "default"
b9a53a
+pid1StartUnitWithStrace $testUnit
b9a53a
+systemctlCheckNUMAProperties $testUnit "default"
b9a53a
+pid1StopUnit $testUnit
b9a53a
+grep "set_mempolicy(MPOL_DEFAULT, NULL" $straceLog
b9a53a
+
b9a53a
+echo "Unit file NUMAPolicy support - Default policy w/ mask"
b9a53a
+writeTestUnitNUMAPolicy "default" "0"
b9a53a
+pid1StartUnitWithStrace $testUnit
b9a53a
+systemctlCheckNUMAProperties $testUnit "default" "0"
b9a53a
+pid1StopUnit $testUnit
b9a53a
+# Maks must be ignored
b9a53a
+grep "set_mempolicy(MPOL_DEFAULT, NULL" $straceLog
b9a53a
+
b9a53a
+echo "Unit file NUMAPolicy support - Bind policy w/o mask"
b9a53a
+writeTestUnitNUMAPolicy "bind"
b9a53a
+pid1StartUnitWithJournal $testUnit
b9a53a
+pid1StopUnit $testUnit
b9a53a
+grep "numa-test.service: Main process exited, code=exited, status=242/NUMA" $journalLog
b9a53a
+
b9a53a
+echo "Unit file NUMAPolicy support - Bind policy w/ mask"
b9a53a
+writeTestUnitNUMAPolicy "bind" "0"
b9a53a
+pid1StartUnitWithStrace $testUnit
b9a53a
+systemctlCheckNUMAProperties $testUnit "bind" "0"
b9a53a
+pid1StopUnit $testUnit
b9a53a
+grep -P "set_mempolicy\(MPOL_BIND, \[0x0*1\]" $straceLog
b9a53a
+
b9a53a
+echo "Unit file NUMAPolicy support - Interleave policy w/o mask"
b9a53a
+writeTestUnitNUMAPolicy "interleave"
b9a53a
+pid1StartUnitWithStrace $testUnit
b9a53a
+pid1StopUnit $testUnit
b9a53a
+grep "numa-test.service: Main process exited, code=exited, status=242/NUMA" $journalLog
b9a53a
+
b9a53a
+echo "Unit file NUMAPolicy support - Interleave policy w/ mask"
b9a53a
+writeTestUnitNUMAPolicy "interleave" "0"
b9a53a
+pid1StartUnitWithStrace $testUnit
b9a53a
+systemctlCheckNUMAProperties $testUnit "interleave" "0"
b9a53a
+pid1StopUnit $testUnit
b9a53a
+grep -P "set_mempolicy\(MPOL_INTERLEAVE, \[0x0*1\]" $straceLog
b9a53a
+
b9a53a
+echo "Unit file NUMAPolicy support - Preferred policy w/o mask"
b9a53a
+writeTestUnitNUMAPolicy "preferred"
b9a53a
+pid1StartUnitWithJournal $testUnit
b9a53a
+systemctlCheckNUMAProperties $testUnit "preferred"
b9a53a
+pid1StopUnit $testUnit
b9a53a
+! grep "numa-test.service: Main process exited, code=exited, status=242/NUMA" $journalLog
b9a53a
+
b9a53a
+echo "Unit file NUMAPolicy support - Preferred policy w/ mask"
b9a53a
+writeTestUnitNUMAPolicy "preferred" "0"
b9a53a
+pid1StartUnitWithStrace $testUnit
b9a53a
+systemctlCheckNUMAProperties $testUnit "preferred" "0"
b9a53a
+pid1StopUnit $testUnit
b9a53a
+grep -P "set_mempolicy\(MPOL_PREFERRED, \[0x0*1\]" $straceLog
b9a53a
+
b9a53a
+echo "Unit file NUMAPolicy support - Local policy w/o mask"
b9a53a
+writeTestUnitNUMAPolicy "local"
b9a53a
+pid1StartUnitWithStrace $testUnit
b9a53a
+systemctlCheckNUMAProperties $testUnit "local"
b9a53a
+pid1StopUnit $testUnit
b9a53a
+grep "set_mempolicy(MPOL_LOCAL, NULL" $straceLog
b9a53a
+
b9a53a
+echo "Unit file NUMAPolicy support - Local policy w/ mask"
b9a53a
+writeTestUnitNUMAPolicy "local" "0"
b9a53a
+pid1StartUnitWithStrace $testUnit
b9a53a
+systemctlCheckNUMAProperties $testUnit "local" "0"
b9a53a
+pid1StopUnit $testUnit
b9a53a
+# Maks must be ignored
b9a53a
+grep "set_mempolicy(MPOL_LOCAL, NULL" $straceLog
b9a53a
+
b9a53a
+echo "systemd-run NUMAPolicy support"
b9a53a
+runUnit='numa-systemd-run-test.service'
b9a53a
+
b9a53a
+systemd-run -p NUMAPolicy=default --unit $runUnit sleep 1000
b9a53a
+systemctlCheckNUMAProperties $runUnit "default"
b9a53a
+pid1StopUnit $runUnit
b9a53a
+
b9a53a
+systemd-run -p NUMAPolicy=default -p NUMAMask=0 --unit $runUnit sleep 1000
b9a53a
+systemctlCheckNUMAProperties $runUnit "default" ""
b9a53a
+pid1StopUnit $runUnit
b9a53a
+
b9a53a
+systemd-run -p NUMAPolicy=bind -p NUMAMask=0 --unit $runUnit sleep 1000
b9a53a
+systemctlCheckNUMAProperties $runUnit "bind" "0"
b9a53a
+pid1StopUnit $runUnit
b9a53a
+
b9a53a
+systemd-run -p NUMAPolicy=interleave -p NUMAMask=0 --unit $runUnit sleep 1000
b9a53a
+systemctlCheckNUMAProperties $runUnit "interleave" "0"
b9a53a
+pid1StopUnit $runUnit
b9a53a
+
b9a53a
+systemd-run -p NUMAPolicy=preferred -p NUMAMask=0 --unit $runUnit sleep 1000
b9a53a
+systemctlCheckNUMAProperties $runUnit "preferred" "0"
b9a53a
+pid1StopUnit $runUnit
b9a53a
+
b9a53a
+systemd-run -p NUMAPolicy=local --unit $runUnit sleep 1000
b9a53a
+systemctlCheckNUMAProperties $runUnit "local"
b9a53a
+pid1StopUnit $runUnit
b9a53a
+
b9a53a
+systemd-run -p NUMAPolicy=local -p NUMAMask=0 --unit $runUnit sleep 1000
b9a53a
+systemctlCheckNUMAProperties $runUnit "local" ""
b9a53a
+pid1StopUnit $runUnit
b9a53a
+
b9a53a
+# Cleanup
b9a53a
+rm -rf $testDir
b9a53a
+rm -rf $confDir
b9a53a
+systemctl daemon-reload
b9a53a
+
b9a53a
+systemd-analyze log-level info
b9a53a
+
b9a53a
+echo OK > /testok
b9a53a
+
b9a53a
+exit 0