diff mbox series

swapon01: Improving test with memory limits and swap reporting

Message ID 20240120064033.2547728-1-liwang@redhat.com
State Superseded
Headers show
Series swapon01: Improving test with memory limits and swap reporting | expand

Commit Message

Li Wang Jan. 20, 2024, 6:40 a.m. UTC
This is target to create a more robust and controlled environment to test
the swapon system call. By introducing memory limits through cgroups and
filling memory with a known pattern, the test can better assess swapon
behavior when the system experiences memory pressure.

Additionally, the reporting of "SwapCached" memory before turning off the
swap file provides a clearer understanding of the swap system's state in
response to the test conditions.

Signed-off-by: Li Wang <liwang@redhat.com>
---
 testcases/kernel/syscalls/swapon/swapon01.c | 9 +++++++++
 1 file changed, 9 insertions(+)

Comments

Li Wang Jan. 20, 2024, 6:52 a.m. UTC | #1
On Sat, Jan 20, 2024 at 2:40 PM Li Wang <liwang@redhat.com> wrote:

> This is target to create a more robust and controlled environment to test
> the swapon system call. By introducing memory limits through cgroups and
> filling memory with a known pattern, the test can better assess swapon
> behavior when the system experiences memory pressure.
>
> Additionally, the reporting of "SwapCached" memory before turning off the
> swap file provides a clearer understanding of the swap system's state in
> response to the test conditions.
>
> Signed-off-by: Li Wang <liwang@redhat.com>
> ---
>  testcases/kernel/syscalls/swapon/swapon01.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
>
> diff --git a/testcases/kernel/syscalls/swapon/swapon01.c
> b/testcases/kernel/syscalls/swapon/swapon01.c
> index e1fe50459..faf4a8002 100644
> --- a/testcases/kernel/syscalls/swapon/swapon01.c
> +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> @@ -20,6 +20,7 @@
>
>  #define MNTPOINT       "mntpoint"
>  #define SWAP_FILE      MNTPOINT"/swapfile01"
> +#define TESTMEM                (1UL<<30)
>
>  static void verify_swapon(void)
>  {
> @@ -29,12 +30,19 @@ static void verify_swapon(void)
>                 tst_brk(TBROK | TERRNO,
>                                 "Failed to turn off swapfile, system
> reboot recommended");
>         }
> +
> +       tst_res(TINFO, "SwapCached: %ld Kb",
> SAFE_READ_MEMINFO("SwapCached:"));
>  }
>
>  static void setup(void)
>  {
>         is_swap_supported(SWAP_FILE);
>         make_swapfile(SWAP_FILE, 0);
> +
> +       SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
> +       SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
> +
> +       tst_pollute_memory(TESTMEM, 0x41);
>

Oops, this pollute memory should move to the behind of swapon,
I forget to submit the last changes when formatting the patch.

--- a/testcases/kernel/syscalls/swapon/swapon01.c
+++ b/testcases/kernel/syscalls/swapon/swapon01.c
@@ -26,12 +26,13 @@ static void verify_swapon(void)
 {
        TST_EXP_PASS(tst_syscall(__NR_swapon, SWAP_FILE, 0));

+       tst_pollute_memory(TESTMEM, 0x41);
+       tst_res(TINFO, "SwapCached: %ld Kb",
SAFE_READ_MEMINFO("SwapCached:"));
+
        if (TST_PASS && tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
                tst_brk(TBROK | TERRNO,
                                "Failed to turn off swapfile, system reboot
recommended");
        }
-
-       tst_res(TINFO, "SwapCached: %ld Kb",
SAFE_READ_MEMINFO("SwapCached:"));
 }

 static void setup(void)
@@ -41,8 +42,6 @@ static void setup(void)

        SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
        SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
-
-       tst_pollute_memory(TESTMEM, 0x41);
 }

 static struct tst_test test = {
diff mbox series

Patch

diff --git a/testcases/kernel/syscalls/swapon/swapon01.c b/testcases/kernel/syscalls/swapon/swapon01.c
index e1fe50459..faf4a8002 100644
--- a/testcases/kernel/syscalls/swapon/swapon01.c
+++ b/testcases/kernel/syscalls/swapon/swapon01.c
@@ -20,6 +20,7 @@ 
 
 #define MNTPOINT	"mntpoint"
 #define SWAP_FILE	MNTPOINT"/swapfile01"
+#define TESTMEM		(1UL<<30)
 
 static void verify_swapon(void)
 {
@@ -29,12 +30,19 @@  static void verify_swapon(void)
 		tst_brk(TBROK | TERRNO,
 				"Failed to turn off swapfile, system reboot recommended");
 	}
+
+	tst_res(TINFO, "SwapCached: %ld Kb", SAFE_READ_MEMINFO("SwapCached:"));
 }
 
 static void setup(void)
 {
 	is_swap_supported(SWAP_FILE);
 	make_swapfile(SWAP_FILE, 0);
+
+	SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
+	SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
+
+	tst_pollute_memory(TESTMEM, 0x41);
 }
 
 static struct tst_test test = {
@@ -42,6 +50,7 @@  static struct tst_test test = {
 	.mount_device = 1,
 	.needs_root = 1,
 	.all_filesystems = 1,
+	.needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
 	.test_all = verify_swapon,
 	.setup = setup
 };