]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
src/test: add unittest for MemoryTHP= 39085/head
authorUsama Arif <usamaarif642@gmail.com>
Sun, 21 Sep 2025 19:59:38 +0000 (20:59 +0100)
committerUsama Arif <usamaarif642@gmail.com>
Tue, 6 Jan 2026 11:26:15 +0000 (03:26 -0800)
This checks if the prctl is set correctly when the property name
is passed to systemd-run.

src/test/meson.build
src/test/test-thp.c [new file with mode: 0644]
test/units/TEST-07-PID1.thp-disable.sh [new file with mode: 0755]

index 6a26eb3f87ce58c0d1f59395cb351c59f37da970..44b6d5e4bd36b5e7cdafda8c90b251a289ba28f6 100644 (file)
@@ -528,6 +528,11 @@ executables += [
                 'conditions' : ['BPF_FRAMEWORK'],
                 'type' : 'manual',
         },
+        core_test_template + {
+                'sources' : files('test-thp.c'),
+                'dependencies' : common_test_dependencies,
+                'type' : 'manual',
+        },
         core_test_template + {
                 'sources' : files('test-cgroup-cpu.c'),
         },
diff --git a/src/test/test-thp.c b/src/test/test-thp.c
new file mode 100644 (file)
index 0000000..047bd26
--- /dev/null
@@ -0,0 +1,56 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include <sys/prctl.h>
+
+#include "tests.h"
+
+#define PR_THP_DISABLE_NOT_SET 0
+#define PR_THP_DISABLE 1
+
+static const char *arg_mode = NULL;
+
+static int intro(void) {
+        int r;
+
+        r = prctl(PR_GET_THP_DISABLE, 0, 0, 0, 0);
+        if (streq_ptr(arg_mode, "no-disable")) {
+                /* Test case: THPs should not be disabled */
+                if (r != PR_THP_DISABLE_NOT_SET) {
+                        log_error("THPs disabled for the process r = %d: %m", r);
+                        return EXIT_FAILURE;
+                }
+        } else if (streq_ptr(arg_mode, "disable")) {
+                /* Test case: THPs should be completely disabled */
+                if (r == PR_THP_DISABLE_NOT_SET)
+                        return log_tests_skipped("Disabling THPs completely for the process not supported");
+
+                if (r != PR_THP_DISABLE) {
+                        log_error("THPs not completely disabled for the process r = %d: %m", r);
+                        return EXIT_FAILURE;
+                }
+        } else if (streq_ptr(arg_mode, "madvise")) {
+                /* Test case: THPs should be only enabled on a madvise basis */
+                if (r == PR_THP_DISABLE_NOT_SET)
+                        return log_tests_skipped("Disabling THPs except for madvise not supported");
+
+                if (r != (PR_THP_DISABLE | PR_THP_DISABLE_EXCEPT_ADVISED)) {
+                        log_error("THPs (except madvise) not completely disabled for the process r = %d: %m", r);
+                        return EXIT_FAILURE;
+                }
+        } else {
+                log_error("Invalid mode: %s (expected: no-disable, disable, or madvise)", strna(arg_mode));
+                return EXIT_FAILURE;
+        }
+
+        return EXIT_SUCCESS;
+}
+
+int main(int argc, char *argv[]) {
+        if (argc != 2) {
+                log_error("Invalid number of args passed to the test %d", argc);
+                return EXIT_FAILURE;
+        }
+        arg_mode = argv[1];
+        test_setup_logging(LOG_DEBUG);
+        return intro();
+}
diff --git a/test/units/TEST-07-PID1.thp-disable.sh b/test/units/TEST-07-PID1.thp-disable.sh
new file mode 100755 (executable)
index 0000000..06626ad
--- /dev/null
@@ -0,0 +1,25 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: LGPL-2.1-or-later
+set -eux
+set -o pipefail
+
+systemd-run --wait \
+        /usr/lib/systemd/tests/unit-tests/manual/test-thp no-disable
+
+systemd-run --wait \
+        -p MemoryTHP=disable \
+        /usr/lib/systemd/tests/unit-tests/manual/test-thp disable
+
+# The following test will always return 77 if at compile time the kernel version
+# is less than 6.18. If it happens don't let the whole test fail
+set +e
+
+systemd-run --wait \
+        -p MemoryTHP=madvise \
+        /usr/lib/systemd/tests/unit-tests/manual/test-thp madvise
+
+if [ $? -eq 77 ]; then
+        exit 0
+fi
+
+set -e