diff mbox series

[3/7] mem/ksm06: Move test code from library to the test

Message ID 20220303145032.21493-4-chrubis@suse.cz
State Accepted
Headers show
Series ksm06 and libnuma cleanups and fixes | expand

Commit Message

Cyril Hrubis March 3, 2022, 2:50 p.m. UTC
There is no point in keeping the test code in the library since ksm06 is
the only test that actually uses it.

Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
 testcases/kernel/mem/ksm/ksm06.c | 82 +++++++++++++++++++++++++++++-
 testcases/kernel/mem/lib/mem.c   | 85 --------------------------------
 2 files changed, 81 insertions(+), 86 deletions(-)

Comments

Richard Palethorpe March 7, 2022, 1:03 p.m. UTC | #1
Hi,

Cyril Hrubis <chrubis@suse.cz> writes:

> There is no point in keeping the test code in the library since ksm06 is
> the only test that actually uses it.
>
> Signed-off-by: Cyril Hrubis <chrubis@suse.cz>

Reviewed-by: Richard Palethorpe <rpalethorpe@suse.com>

> ---
>  testcases/kernel/mem/ksm/ksm06.c | 82 +++++++++++++++++++++++++++++-
>  testcases/kernel/mem/lib/mem.c   | 85 --------------------------------
>  2 files changed, 81 insertions(+), 86 deletions(-)
>
> diff --git a/testcases/kernel/mem/ksm/ksm06.c b/testcases/kernel/mem/ksm/ksm06.c
> index 379236f1f..0f5e4b05d 100644
> --- a/testcases/kernel/mem/ksm/ksm06.c
> +++ b/testcases/kernel/mem/ksm/ksm06.c
> @@ -48,7 +48,87 @@ static char *n_opt;
>  
>  static void test_ksm(void)
>  {
> -	test_ksm_merge_across_nodes(nr_pages);
> +	char **memory;
> +	int i, ret;
> +	int num_nodes, *nodes;
> +	unsigned long length;
> +	unsigned long pagesize;
> +
> +#ifdef HAVE_NUMA_V2
> +	unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
> +#endif
> +
> +	ret = get_allowed_nodes_arr(NH_MEMS, &num_nodes, &nodes);
> +	if (ret != 0)
> +		tst_brk(TBROK|TERRNO, "get_allowed_nodes_arr");
> +	if (num_nodes < 2) {
> +		tst_res(TINFO, "need NUMA system support");
> +		free(nodes);
> +		return;
> +	}
> +
> +	pagesize = sysconf(_SC_PAGE_SIZE);
> +	length = nr_pages * pagesize;
> +
> +	memory = SAFE_MALLOC(num_nodes * sizeof(char *));
> +	for (i = 0; i < num_nodes; i++) {
> +		memory[i] = SAFE_MMAP(NULL, length, PROT_READ|PROT_WRITE,
> +			    MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
> +#ifdef HAVE_DECL_MADV_MERGEABLE
> +		if (madvise(memory[i], length, MADV_MERGEABLE) == -1)
> +			tst_brk(TBROK|TERRNO, "madvise");
> +#endif
> +
> +#ifdef HAVE_NUMA_V2
> +		clean_node(nmask);
> +		set_node(nmask, nodes[i]);
> +		/*
> +		 * Use mbind() to make sure each node contains
> +		 * length size memory.
> +		 */
> +		ret = mbind(memory[i], length, MPOL_BIND, nmask, MAXNODES, 0);
> +		if (ret == -1)
> +			tst_brk(TBROK|TERRNO, "mbind");
> +#endif
> +
> +		memset(memory[i], 10, length);
> +
> +		if (mlock(memory[i], length))
> +			tst_res(TWARN | TERRNO, "mlock() failed");
> +	}
> +
> +	SAFE_FILE_PRINTF(PATH_KSM "sleep_millisecs", "0");
> +	SAFE_FILE_PRINTF(PATH_KSM "pages_to_scan", "%ld",
> +			 nr_pages * num_nodes);
> +	/*
> +	 * merge_across_nodes and max_page_sharing setting can be changed
> +	 * only when there are no ksm shared pages in system, so set run 2
> +	 * to unmerge pages first, then to 1 after changing merge_across_nodes,
> +	 * to remerge according to the new setting.
> +	 */
> +	SAFE_FILE_PRINTF(PATH_KSM "run", "2");
> +	if (access(PATH_KSM "max_page_sharing", F_OK) == 0)
> +		SAFE_FILE_PRINTF(PATH_KSM "max_page_sharing",
> +			"%ld", nr_pages * num_nodes);
> +	tst_res(TINFO, "Start to test KSM with merge_across_nodes=1");
> +	SAFE_FILE_PRINTF(PATH_KSM "merge_across_nodes", "1");
> +	SAFE_FILE_PRINTF(PATH_KSM "run", "1");
> +	ksm_group_check(1, 1, nr_pages * num_nodes - 1, 0, 0, 0,
> +			nr_pages * num_nodes);
> +
> +	SAFE_FILE_PRINTF(PATH_KSM "run", "2");
> +	tst_res(TINFO, "Start to test KSM with merge_across_nodes=0");
> +	SAFE_FILE_PRINTF(PATH_KSM "merge_across_nodes", "0");
> +	SAFE_FILE_PRINTF(PATH_KSM "run", "1");
> +	ksm_group_check(1, num_nodes, nr_pages * num_nodes - num_nodes,
> +			0, 0, 0, nr_pages * num_nodes);
> +
> +	SAFE_FILE_PRINTF(PATH_KSM "run", "2");
> +
> +	for (i = 0; i < num_nodes; i++)
> +		SAFE_MUNMAP(memory[i], length);
> +
> +	free(memory);
>  }
>  
>  static void setup(void)
> diff --git a/testcases/kernel/mem/lib/mem.c b/testcases/kernel/mem/lib/mem.c
> index 102fc5665..090569ebb 100644
> --- a/testcases/kernel/mem/lib/mem.c
> +++ b/testcases/kernel/mem/lib/mem.c
> @@ -519,91 +519,6 @@ void create_same_memory(int size, int num, int unit)
>  				 WEXITSTATUS(status));
>  }
>  
> -void test_ksm_merge_across_nodes(unsigned long nr_pages)
> -{
> -	char **memory;
> -	int i, ret;
> -	int num_nodes, *nodes;
> -	unsigned long length;
> -	unsigned long pagesize;
> -
> -#ifdef HAVE_NUMA_V2
> -	unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
> -#endif
> -
> -	ret = get_allowed_nodes_arr(NH_MEMS, &num_nodes, &nodes);
> -	if (ret != 0)
> -		tst_brk(TBROK|TERRNO, "get_allowed_nodes_arr");
> -	if (num_nodes < 2) {
> -		tst_res(TINFO, "need NUMA system support");
> -		free(nodes);
> -		return;
> -	}
> -
> -	pagesize = sysconf(_SC_PAGE_SIZE);
> -	length = nr_pages * pagesize;
> -
> -	memory = SAFE_MALLOC(num_nodes * sizeof(char *));
> -	for (i = 0; i < num_nodes; i++) {
> -		memory[i] = SAFE_MMAP(NULL, length, PROT_READ|PROT_WRITE,
> -			    MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
> -#ifdef HAVE_DECL_MADV_MERGEABLE
> -		if (madvise(memory[i], length, MADV_MERGEABLE) == -1)
> -			tst_brk(TBROK|TERRNO, "madvise");
> -#endif
> -
> -#ifdef HAVE_NUMA_V2
> -		clean_node(nmask);
> -		set_node(nmask, nodes[i]);
> -		/*
> -		 * Use mbind() to make sure each node contains
> -		 * length size memory.
> -		 */
> -		ret = mbind(memory[i], length, MPOL_BIND, nmask, MAXNODES, 0);
> -		if (ret == -1)
> -			tst_brk(TBROK|TERRNO, "mbind");
> -#endif
> -
> -		memset(memory[i], 10, length);
> -
> -		if (mlock(memory[i], length))
> -			tst_res(TWARN | TERRNO, "mlock() failed");
> -	}
> -
> -	SAFE_FILE_PRINTF(PATH_KSM "sleep_millisecs", "0");
> -	SAFE_FILE_PRINTF(PATH_KSM "pages_to_scan", "%ld",
> -			 nr_pages * num_nodes);
> -	/*
> -	 * merge_across_nodes and max_page_sharing setting can be changed
> -	 * only when there are no ksm shared pages in system, so set run 2
> -	 * to unmerge pages first, then to 1 after changing merge_across_nodes,
> -	 * to remerge according to the new setting.
> -	 */
> -	SAFE_FILE_PRINTF(PATH_KSM "run", "2");
> -	if (access(PATH_KSM "max_page_sharing", F_OK) == 0)
> -		SAFE_FILE_PRINTF(PATH_KSM "max_page_sharing",
> -			"%ld", nr_pages * num_nodes);
> -	tst_res(TINFO, "Start to test KSM with merge_across_nodes=1");
> -	SAFE_FILE_PRINTF(PATH_KSM "merge_across_nodes", "1");
> -	SAFE_FILE_PRINTF(PATH_KSM "run", "1");
> -	ksm_group_check(1, 1, nr_pages * num_nodes - 1, 0, 0, 0,
> -		        nr_pages * num_nodes);
> -
> -	SAFE_FILE_PRINTF(PATH_KSM "run", "2");
> -	tst_res(TINFO, "Start to test KSM with merge_across_nodes=0");
> -	SAFE_FILE_PRINTF(PATH_KSM "merge_across_nodes", "0");
> -	SAFE_FILE_PRINTF(PATH_KSM "run", "1");
> -	ksm_group_check(1, num_nodes, nr_pages * num_nodes - num_nodes,
> -		        0, 0, 0, nr_pages * num_nodes);
> -
> -	SAFE_FILE_PRINTF(PATH_KSM "run", "2");
> -
> -	for (i = 0; i < num_nodes; i++)
> -		SAFE_MUNMAP(memory[i], length);
> -
> -	free(memory);
> -}
> -
>  /* THP */
>  
>  /* cpuset/memcg */
> -- 
> 2.34.1
diff mbox series

Patch

diff --git a/testcases/kernel/mem/ksm/ksm06.c b/testcases/kernel/mem/ksm/ksm06.c
index 379236f1f..0f5e4b05d 100644
--- a/testcases/kernel/mem/ksm/ksm06.c
+++ b/testcases/kernel/mem/ksm/ksm06.c
@@ -48,7 +48,87 @@  static char *n_opt;
 
 static void test_ksm(void)
 {
-	test_ksm_merge_across_nodes(nr_pages);
+	char **memory;
+	int i, ret;
+	int num_nodes, *nodes;
+	unsigned long length;
+	unsigned long pagesize;
+
+#ifdef HAVE_NUMA_V2
+	unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
+#endif
+
+	ret = get_allowed_nodes_arr(NH_MEMS, &num_nodes, &nodes);
+	if (ret != 0)
+		tst_brk(TBROK|TERRNO, "get_allowed_nodes_arr");
+	if (num_nodes < 2) {
+		tst_res(TINFO, "need NUMA system support");
+		free(nodes);
+		return;
+	}
+
+	pagesize = sysconf(_SC_PAGE_SIZE);
+	length = nr_pages * pagesize;
+
+	memory = SAFE_MALLOC(num_nodes * sizeof(char *));
+	for (i = 0; i < num_nodes; i++) {
+		memory[i] = SAFE_MMAP(NULL, length, PROT_READ|PROT_WRITE,
+			    MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
+#ifdef HAVE_DECL_MADV_MERGEABLE
+		if (madvise(memory[i], length, MADV_MERGEABLE) == -1)
+			tst_brk(TBROK|TERRNO, "madvise");
+#endif
+
+#ifdef HAVE_NUMA_V2
+		clean_node(nmask);
+		set_node(nmask, nodes[i]);
+		/*
+		 * Use mbind() to make sure each node contains
+		 * length size memory.
+		 */
+		ret = mbind(memory[i], length, MPOL_BIND, nmask, MAXNODES, 0);
+		if (ret == -1)
+			tst_brk(TBROK|TERRNO, "mbind");
+#endif
+
+		memset(memory[i], 10, length);
+
+		if (mlock(memory[i], length))
+			tst_res(TWARN | TERRNO, "mlock() failed");
+	}
+
+	SAFE_FILE_PRINTF(PATH_KSM "sleep_millisecs", "0");
+	SAFE_FILE_PRINTF(PATH_KSM "pages_to_scan", "%ld",
+			 nr_pages * num_nodes);
+	/*
+	 * merge_across_nodes and max_page_sharing setting can be changed
+	 * only when there are no ksm shared pages in system, so set run 2
+	 * to unmerge pages first, then to 1 after changing merge_across_nodes,
+	 * to remerge according to the new setting.
+	 */
+	SAFE_FILE_PRINTF(PATH_KSM "run", "2");
+	if (access(PATH_KSM "max_page_sharing", F_OK) == 0)
+		SAFE_FILE_PRINTF(PATH_KSM "max_page_sharing",
+			"%ld", nr_pages * num_nodes);
+	tst_res(TINFO, "Start to test KSM with merge_across_nodes=1");
+	SAFE_FILE_PRINTF(PATH_KSM "merge_across_nodes", "1");
+	SAFE_FILE_PRINTF(PATH_KSM "run", "1");
+	ksm_group_check(1, 1, nr_pages * num_nodes - 1, 0, 0, 0,
+			nr_pages * num_nodes);
+
+	SAFE_FILE_PRINTF(PATH_KSM "run", "2");
+	tst_res(TINFO, "Start to test KSM with merge_across_nodes=0");
+	SAFE_FILE_PRINTF(PATH_KSM "merge_across_nodes", "0");
+	SAFE_FILE_PRINTF(PATH_KSM "run", "1");
+	ksm_group_check(1, num_nodes, nr_pages * num_nodes - num_nodes,
+			0, 0, 0, nr_pages * num_nodes);
+
+	SAFE_FILE_PRINTF(PATH_KSM "run", "2");
+
+	for (i = 0; i < num_nodes; i++)
+		SAFE_MUNMAP(memory[i], length);
+
+	free(memory);
 }
 
 static void setup(void)
diff --git a/testcases/kernel/mem/lib/mem.c b/testcases/kernel/mem/lib/mem.c
index 102fc5665..090569ebb 100644
--- a/testcases/kernel/mem/lib/mem.c
+++ b/testcases/kernel/mem/lib/mem.c
@@ -519,91 +519,6 @@  void create_same_memory(int size, int num, int unit)
 				 WEXITSTATUS(status));
 }
 
-void test_ksm_merge_across_nodes(unsigned long nr_pages)
-{
-	char **memory;
-	int i, ret;
-	int num_nodes, *nodes;
-	unsigned long length;
-	unsigned long pagesize;
-
-#ifdef HAVE_NUMA_V2
-	unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
-#endif
-
-	ret = get_allowed_nodes_arr(NH_MEMS, &num_nodes, &nodes);
-	if (ret != 0)
-		tst_brk(TBROK|TERRNO, "get_allowed_nodes_arr");
-	if (num_nodes < 2) {
-		tst_res(TINFO, "need NUMA system support");
-		free(nodes);
-		return;
-	}
-
-	pagesize = sysconf(_SC_PAGE_SIZE);
-	length = nr_pages * pagesize;
-
-	memory = SAFE_MALLOC(num_nodes * sizeof(char *));
-	for (i = 0; i < num_nodes; i++) {
-		memory[i] = SAFE_MMAP(NULL, length, PROT_READ|PROT_WRITE,
-			    MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
-#ifdef HAVE_DECL_MADV_MERGEABLE
-		if (madvise(memory[i], length, MADV_MERGEABLE) == -1)
-			tst_brk(TBROK|TERRNO, "madvise");
-#endif
-
-#ifdef HAVE_NUMA_V2
-		clean_node(nmask);
-		set_node(nmask, nodes[i]);
-		/*
-		 * Use mbind() to make sure each node contains
-		 * length size memory.
-		 */
-		ret = mbind(memory[i], length, MPOL_BIND, nmask, MAXNODES, 0);
-		if (ret == -1)
-			tst_brk(TBROK|TERRNO, "mbind");
-#endif
-
-		memset(memory[i], 10, length);
-
-		if (mlock(memory[i], length))
-			tst_res(TWARN | TERRNO, "mlock() failed");
-	}
-
-	SAFE_FILE_PRINTF(PATH_KSM "sleep_millisecs", "0");
-	SAFE_FILE_PRINTF(PATH_KSM "pages_to_scan", "%ld",
-			 nr_pages * num_nodes);
-	/*
-	 * merge_across_nodes and max_page_sharing setting can be changed
-	 * only when there are no ksm shared pages in system, so set run 2
-	 * to unmerge pages first, then to 1 after changing merge_across_nodes,
-	 * to remerge according to the new setting.
-	 */
-	SAFE_FILE_PRINTF(PATH_KSM "run", "2");
-	if (access(PATH_KSM "max_page_sharing", F_OK) == 0)
-		SAFE_FILE_PRINTF(PATH_KSM "max_page_sharing",
-			"%ld", nr_pages * num_nodes);
-	tst_res(TINFO, "Start to test KSM with merge_across_nodes=1");
-	SAFE_FILE_PRINTF(PATH_KSM "merge_across_nodes", "1");
-	SAFE_FILE_PRINTF(PATH_KSM "run", "1");
-	ksm_group_check(1, 1, nr_pages * num_nodes - 1, 0, 0, 0,
-		        nr_pages * num_nodes);
-
-	SAFE_FILE_PRINTF(PATH_KSM "run", "2");
-	tst_res(TINFO, "Start to test KSM with merge_across_nodes=0");
-	SAFE_FILE_PRINTF(PATH_KSM "merge_across_nodes", "0");
-	SAFE_FILE_PRINTF(PATH_KSM "run", "1");
-	ksm_group_check(1, num_nodes, nr_pages * num_nodes - num_nodes,
-		        0, 0, 0, nr_pages * num_nodes);
-
-	SAFE_FILE_PRINTF(PATH_KSM "run", "2");
-
-	for (i = 0; i < num_nodes; i++)
-		SAFE_MUNMAP(memory[i], length);
-
-	free(memory);
-}
-
 /* THP */
 
 /* cpuset/memcg */