Skip to contentSkip to conversation

Convert LTP mem/testcase to new API

PATCH Superseded Li Wang 2 participants 18 messages Jul 18, 2017 – Aug 2, 2017 Library code

lore
Share & tools
lore

Li Wang

author
#0 lore raw

v3 cover Convert LTP mem/testcase to new API

v2 --> v3

* fix OOM timeout issue by new commit:
mem/oom: disable timeouts for testcase in demand

* move common code to ksm_common.h:
mm/ksm: move the common code to ksm_common.h

* take use of SAFE_MACRO in mem/thp testcases

Signature · 1 line
2.9.3
author
#1 lore raw

PATCH v3 1/15 mem/lib: convert to new API

Add new SAFE_READ_MEMINFO macro to replace function read_meminfo().

---
 include/tst_safe_file_ops.h        |   6 +
 testcases/kernel/mem/include/mem.h |   9 +-
 testcases/kernel/mem/lib/mem.c     | 314 ++++++++++++++++---------------------
 3 files changed, 141 insertions(+), 188 deletions(-)
include/tst_safe_file_ops.h +6 -0
index 4d19877..5c3fea4 100644
@@ -38,6 +38,12 @@
 	file_lines_scanf(__FILE__, __LINE__, NULL, 1,\
 			(path), (fmt), ## __VA_ARGS__)
 
+#define SAFE_READ_MEMINFO(item) \
+       ({long tst_rval; \
+        SAFE_FILE_LINES_SCANF("/proc/meminfo", item " %ld", \
+                        &tst_rval); \
+        tst_rval;})
+
 #define FILE_PRINTF(path, fmt, ...) \
 	file_printf(__FILE__, __LINE__, \
 		    (path), (fmt), ## __VA_ARGS__)
testcases/kernel/mem/include/mem.h +3 -6
index f9522a5..377cc55 100644
@@ -1,7 +1,7 @@
 #ifndef _MEM_H
 #define _MEM_H
 #include "config.h"
-#include "test.h"
+#include "tst_test.h"
 
 #if defined(__powerpc__) || defined(__powerpc64__)
 #define MAXNODES		256
@@ -84,16 +84,13 @@ void mount_mem(char *name, char *fs, char *options, char *path, char *path_new);
 void umount_mem(char *path, char *path_new);
 
 /* shared */
-unsigned int get_a_numa_node(void (*cleanup_fn)(void));
+unsigned int get_a_numa_node(void);
 int  path_exist(const char *path, ...);
-long read_meminfo(char *item);
 void set_sys_tune(char *sys_file, long tune, int check);
 long get_sys_tune(char *sys_file);
-void cleanup(void);
-void setup(void);
 
 void update_shm_size(size_t *shm_size);
 
 /* MMAP */
-int range_is_mapped(void (*cleanup_fn) (void), unsigned long low, unsigned long high);
+int range_is_mapped(unsigned long low, unsigned long high);
 #endif
testcases/kernel/mem/lib/mem.c +132 -182

752 lines not on this page · raw message

Signature · 1 line
2.9.3
author
#2 lore raw

PATCH v3 2/15 mem/oom: convert to new API

In reply to Li Wang
---
 testcases/kernel/mem/oom/oom01.c |  82 ++++++++++---------------
 testcases/kernel/mem/oom/oom02.c |  82 +++++++++----------------
 testcases/kernel/mem/oom/oom03.c | 109 +++++++++++++--------------------
 testcases/kernel/mem/oom/oom04.c |  97 +++++++++++------------------
 testcases/kernel/mem/oom/oom05.c | 129 ++++++++++++++++-----------------------
 5 files changed, 191 insertions(+), 308 deletions(-)
testcases/kernel/mem/oom/oom01.c +31 -51
index e39394b..955642c 100644
@@ -7,27 +7,19 @@
  * behaviours. It simply increase the memory consumption 3G each time
  * until all the available memory is consumed and OOM is triggered.
  *
- * Copyright (C) 2010  Red Hat, Inc.
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public
- * License as published by the Free Software Foundation.
+ * Copyright (C) 2010-2017  Red Hat, Inc.
  *
- * This program is distributed in the hope that it would be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * This program is free software;  you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
- * Further, this software is distributed without any warranty that it
- * is free of the rightful claim of any third person regarding
- * infringement or the like.  Any license provided herein, whether
- * implied or otherwise, applies only to this software file.  Patent
- * licenses, if any, provided herein do not apply to combinations of
- * this program with other software, or any other product whatsoever.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY;  without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+ * the GNU General Public License for more details.
  */
+
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <errno.h>
@@ -35,53 +27,41 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include "test.h"
 #include "mem.h"
 
-char *TCID = "oom01";
-int TST_TOTAL = 1;
-
-int main(int argc, char *argv[])
+static void verify_oom(void)
 {
-	int lc;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
 #if __WORDSIZE == 32
-	tst_brkm(TCONF, NULL, "test is not designed for 32-bit system.");
+	tst_brk(TCONF, "test is not designed for 32-bit system.");
 #endif
 
-	setup();
+	/* we expect mmap to fail before OOM is hit */
+	set_sys_tune("overcommit_memory", 2, 1);
+	oom(NORMAL, 0, ENOMEM, 0);
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
+	/* with overcommit_memory set to 0 or 1 there's no
+	 * guarantee that mmap fails before OOM */
+	set_sys_tune("overcommit_memory", 0, 1);
+	oom(NORMAL, 0, ENOMEM, 1);
 
-		/* we expect mmap to fail before OOM is hit */
-		set_sys_tune("overcommit_memory", 2, 1);
-		oom(NORMAL, 0, ENOMEM, 0);
-
-		/* with overcommit_memory set to 0 or 1 there's no
-		 * guarantee that mmap fails before OOM */
-		set_sys_tune("overcommit_memory", 0, 1);
-		oom(NORMAL, 0, ENOMEM, 1);
-
-		set_sys_tune("overcommit_memory", 1, 1);
-		testoom(0, 0, ENOMEM, 1);
-	}
-	cleanup();
-	tst_exit();
+	set_sys_tune("overcommit_memory", 1, 1);
+	testoom(0, 0, ENOMEM, 1);
 }
 
-void setup(void)
+static void setup(void)
 {
-	tst_require_root();
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
-
 	overcommit = get_sys_tune("overcommit_memory");
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	set_sys_tune("overcommit_memory", overcommit, 0);
 }
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.forks_child = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_oom,
+};
testcases/kernel/mem/oom/oom02.c +29 -53

119 lines not on this page · raw message

testcases/kernel/mem/oom/oom03.c +42 -67

149 lines not on this page · raw message

testcases/kernel/mem/oom/oom04.c +36 -61

141 lines not on this page · raw message

testcases/kernel/mem/oom/oom05.c +53 -76

178 lines not on this page · raw message

Signature · 1 line
2.9.3
author
#3 lore raw

PATCH v3 3/15 mem/oom: disable timeouts for testcase in demand

In reply to Li Wang

OOM spend time is depending on amount of RAM+Swap, it's hard to get a standard to measure how long it takes, so that we cann't setting tst_test->timeout easily.

In this patch, we add functionality to disable timeouts for these, i.e. set the .timeout to -1 and change the test library so that the alarm() is not set in such case.

---
 lib/tst_test.c                   | 4 ++--
 testcases/kernel/mem/oom/oom01.c | 1 +
 testcases/kernel/mem/oom/oom02.c | 1 +
 testcases/kernel/mem/oom/oom03.c | 1 +
 testcases/kernel/mem/oom/oom04.c | 1 +
 testcases/kernel/mem/oom/oom05.c | 1 +
 6 files changed, 7 insertions(+), 2 deletions(-)
lib/tst_test.c +2 -2
index 16ea64f..6c67afe 100644
@@ -917,9 +917,9 @@ void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
 	SAFE_SIGNAL(SIGALRM, alarm_handler);
 	SAFE_SIGNAL(SIGUSR1, heartbeat_handler);
 
-	if (tst_test->timeout)
+	if (tst_test->timeout > 0)
 		tst_set_timeout(tst_test->timeout);
-	else
+	else if (tst_test->timeout == 0)
 		tst_set_timeout(300);
 
 	SAFE_SIGNAL(SIGINT, sigint_handler);
testcases/kernel/mem/oom/oom01.c +1 -0
index 955642c..3d05860 100644
@@ -61,6 +61,7 @@ static void cleanup(void)
 static struct tst_test test = {
 	.needs_root = 1,
 	.forks_child = 1,
+	.timeout = -1,
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = verify_oom,
testcases/kernel/mem/oom/oom02.c +1 -0
index dab8874..3434a34 100644
@@ -65,6 +65,7 @@ static void cleanup(void)
 static struct tst_test test = {
 	.needs_root = 1,
 	.forks_child = 1,
+	.timeout = -1,
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = verify_oom,
testcases/kernel/mem/oom/oom03.c +1 -0
index b384e2b..f883bc0 100644
@@ -78,6 +78,7 @@ static void cleanup(void)
 static struct tst_test test = {
 	.needs_root = 1,
 	.forks_child = 1,
+	.timeout = -1,
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = verify_oom,
testcases/kernel/mem/oom/oom04.c +1 -0
index 8043097..e4e70e2 100644
@@ -87,6 +87,7 @@ static void cleanup(void)
 static struct tst_test test = {
 	.needs_root = 1,
 	.forks_child = 1,
+	.timeout = -1,
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = verify_oom,
testcases/kernel/mem/oom/oom05.c +1 -0
index 600e8d9..d2e631a 100644
@@ -115,6 +115,7 @@ void cleanup(void)
 static struct tst_test test = {
 	.needs_root = 1,
 	.forks_child = 1,
+	.timeout = -1,
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = verify_oom,
Signature · 1 line
2.9.3
author
#4 lore raw

PATCH v3 4/15 mem/ksm: convert to new API

In reply to Li Wang

Recast ksm_options structure to support parameter transfer.

---
 testcases/kernel/mem/include/mem.h |   9 ++-
 testcases/kernel/mem/ksm/ksm01.c   |  88 ++++++++++---------------
 testcases/kernel/mem/ksm/ksm02.c   | 123 ++++++++++++++---------------------
 testcases/kernel/mem/ksm/ksm03.c   |  90 ++++++++++---------------
 testcases/kernel/mem/ksm/ksm04.c   | 121 ++++++++++++++--------------------
 testcases/kernel/mem/ksm/ksm05.c   | 130 +++++++++++++++----------------------
 testcases/kernel/mem/ksm/ksm06.c   | 105 ++++++++++--------------------
 testcases/kernel/mem/lib/mem.c     |  16 ++---
 8 files changed, 266 insertions(+), 416 deletions(-)
testcases/kernel/mem/include/mem.h +4 -5
index 377cc55..bada549 100644
@@ -44,6 +44,10 @@ void testoom(int mempolicy, int lite, int retcode, int allow_sigkill);
 
 #define PATH_KSM		"/sys/kernel/mm/ksm/"
 
+char *opt_numstr, *opt_sizestr, *opt_unitstr;
+
+void create_same_memory(int size, int num, int unit);
+void check_ksm_options(int *size, int *num, int *unit);
 void save_max_page_sharing(void);
 void restore_max_page_sharing(void);
 void test_ksm_merge_across_nodes(unsigned long nr_pages);
@@ -58,11 +62,6 @@ void test_ksm_merge_across_nodes(unsigned long nr_pages);
 #define PATH_SHMMAX		"/proc/sys/kernel/shmmax"
 
 void write_memcg(void);
-void create_same_memory(int size, int num, int unit);
-int  opt_num, opt_size, opt_unit;
-char *opt_numstr, *opt_sizestr, *opt_unitstr;
-void check_ksm_options(int *size, int *num, int *unit);
-void ksm_usage(void);
 
 /* cpuset/memcg */
 
testcases/kernel/mem/ksm/ksm01.c +35 -53

131 lines not on this page · raw message

testcases/kernel/mem/ksm/ksm02.c +50 -73

173 lines not on this page · raw message

testcases/kernel/mem/ksm/ksm03.c +36 -54

132 lines not on this page · raw message

testcases/kernel/mem/ksm/ksm04.c +49 -72

175 lines not on this page · raw message

testcases/kernel/mem/ksm/ksm05.c +51 -79

184 lines not on this page · raw message

testcases/kernel/mem/ksm/ksm06.c +36 -69

159 lines not on this page · raw message

testcases/kernel/mem/lib/mem.c +5 -11
index 053737b..f5d86c7 100644
@@ -266,9 +266,10 @@ static void check(char *path, long int value)
 	snprintf(fullpath, BUFSIZ, PATH_KSM "%s", path);
 	SAFE_FILE_SCANF(fullpath, "%ld", &actual_val);
 
-	tst_res(TINFO, "%s is %ld.", path, actual_val);
 	if (actual_val != value)
 		tst_res(TFAIL, "%s is not %ld.", path, value);
+	else
+		tst_res(TPASS, "%s is %ld.", path, actual_val);
 }
 
 static void wait_ksmd_done(void)
@@ -636,12 +637,12 @@ void test_ksm_merge_across_nodes(unsigned long nr_pages)
 
 void check_ksm_options(int *size, int *num, int *unit)
 {
-	if (opt_size) {
+	if (opt_sizestr) {
 		*size = atoi(opt_sizestr);
 		if (*size < 1)
 			tst_brk(TBROK, "size cannot be less than 1.");
 	}
-	if (opt_unit) {
+	if (opt_unitstr) {
 		*unit = atoi(opt_unitstr);
 		if (*unit > *size)
 			tst_brk(TBROK,
@@ -651,7 +652,7 @@ void check_ksm_options(int *size, int *num, int *unit)
 				 "the remainder of division of size by unit is "
 				 "not zero.");
 	}
-	if (opt_num) {
+	if (opt_numstr) {
 		*num = atoi(opt_numstr);
 		if (*num < 3)
 			tst_brk(TBROK,
@@ -659,13 +660,6 @@ void check_ksm_options(int *size, int *num, int *unit)
 	}
 }
 
-void ksm_usage(void)
-{
-	printf("  -n      Number of processes\n");
-	printf("  -s      Memory allocation size in MB\n");
-	printf("  -u      Memory allocation unit in MB\n");
-}
-
 /* THP */
 
 /* cpuset/memcg */
Signature · 1 line
2.9.3
author
#5 lore raw

PATCH v3 5/15 mm/ksm: move the common code to ksm_common.h

In reply to Li Wang
---
 testcases/kernel/mem/include/mem.h    |  3 ---
 testcases/kernel/mem/ksm/ksm01.c      | 16 +++---------
 testcases/kernel/mem/ksm/ksm02.c      | 16 +++---------
 testcases/kernel/mem/ksm/ksm03.c      | 15 ++---------
 testcases/kernel/mem/ksm/ksm04.c      | 15 ++---------
 testcases/kernel/mem/ksm/ksm05.c      |  1 -
 testcases/kernel/mem/ksm/ksm_common.h | 48 +++++++++++++++++++++++++++++++++++
 testcases/kernel/mem/lib/mem.c        | 25 ------------------
 8 files changed, 58 insertions(+), 81 deletions(-)
 create mode 100644 testcases/kernel/mem/ksm/ksm_common.h
testcases/kernel/mem/include/mem.h +0 -3
index bada549..287f8b3 100644
@@ -44,10 +44,7 @@ void testoom(int mempolicy, int lite, int retcode, int allow_sigkill);
 
 #define PATH_KSM		"/sys/kernel/mm/ksm/"
 
-char *opt_numstr, *opt_sizestr, *opt_unitstr;
-
 void create_same_memory(int size, int num, int unit);
-void check_ksm_options(int *size, int *num, int *unit);
 void save_max_page_sharing(void);
 void restore_max_page_sharing(void);
 void test_ksm_merge_across_nodes(unsigned long nr_pages);
testcases/kernel/mem/ksm/ksm01.c +3 -13
index 849d4d4..ce96983 100644
@@ -57,21 +57,10 @@
 #include <string.h>
 #include <unistd.h>
 #include "mem.h"
-
-static int merge_across_nodes;
-
-static struct tst_option ksm_options[] = {
-	{"n:", &opt_numstr,  "-n       Number of processes"},
-	{"s:", &opt_sizestr, "-s       Memory allocation size in MB"},
-	{"u:", &opt_unitstr, "-u       Memory allocation unit in MB"},
-	{NULL, NULL, NULL}
-};
+#include "ksm_common.h"
 
 static void verify_ksm(void)
 {
-	int size = 128, num = 3, unit = 1;
-
-	check_ksm_options(&size, &num, &unit);
 	create_same_memory(size, num, unit);
 }
 
@@ -82,6 +71,8 @@ static void setup(void)
 
 	save_max_page_sharing();
 
+	parse_ksm_options(opt_sizestr, &size, opt_numstr, &num, opt_unitstr, &unit);
+
 	/*
 	 * kernel commit 90bd6fd introduced a new KSM sysfs knob
 	 * /sys/kernel/mm/ksm/merge_across_nodes, setting it to '0'
@@ -106,7 +97,6 @@ static void cleanup(void)
 }
 
 static struct tst_test test = {
-	.tid = "ksm01",
 	.needs_root = 1,
 	.forks_child = 1,
 	.options = ksm_options,
testcases/kernel/mem/ksm/ksm02.c +3 -13
index eeebfef..6fe75d3 100644
@@ -57,30 +57,19 @@
 #include <stdio.h>
 #include <unistd.h>
 #include "mem.h"
+#include "ksm_common.h"
 
 #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
 	&& HAVE_MPOL_CONSTANTS
 
-static int merge_across_nodes;
-
-static struct tst_option ksm_options[] = {
-	{"n:", &opt_numstr,  "-n       Number of processes"},
-	{"s:", &opt_sizestr, "-s       Memory allocation size in MB"},
-	{"u:", &opt_unitstr, "-u       Memory allocation unit in MB"},
-	{NULL, NULL, NULL}
-};
-
 static void verify_ksm(void)
 {
-	int size = 128, num = 3, unit = 1;
 	unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
 	unsigned int node;
 
 	node = get_a_numa_node();
 	set_node(nmask, node);
 
-	check_ksm_options(&size, &num, &unit);
-
 	if (set_mempolicy(MPOL_BIND, nmask, MAXNODES) == -1) {
 		if (errno != ENOSYS)
 			tst_brk(TBROK | TERRNO, "set_mempolicy");
@@ -111,6 +100,8 @@ static void setup(void)
 		tst_brk(TCONF, "KSM configuration is not enabled");
 	save_max_page_sharing();
 
+	parse_ksm_options(opt_sizestr, &size, opt_numstr, &num, opt_unitstr, &unit);
+
 	if (access(PATH_KSM "merge_across_nodes", F_OK) == 0) {
 		SAFE_FILE_SCANF(PATH_KSM "merge_across_nodes",
 				"%d", &merge_across_nodes);
@@ -121,7 +112,6 @@ static void setup(void)
 }
 
 static struct tst_test test = {
-	.tid = "ksm02",
 	.needs_root = 1,
 	.forks_child = 1,
 	.options = ksm_options,
testcases/kernel/mem/ksm/ksm03.c +2 -13
index 9fd5aac..51cc923 100644
@@ -57,21 +57,10 @@
 #include <string.h>
 #include <unistd.h>
 #include "mem.h"
-
-static int merge_across_nodes;
-
-static struct tst_option ksm_options[] = {
-	{"n:", &opt_numstr,  "-n       Number of processes"},
-	{"s:", &opt_sizestr, "-s       Memory allocation size in MB"},
-	{"u:", &opt_unitstr, "-u       Memory allocation unit in MB"},
-	{NULL, NULL, NULL}
-};
+#include "ksm_common.h"
 
 static void verify_ksm(void)
 {
-	int size = 128, num = 3, unit = 1;
-
-	check_ksm_options(&size, &num, &unit);
 	write_memcg();
 	create_same_memory(size, num, unit);
 }
@@ -88,6 +77,7 @@ static void setup(void)
 	}
 
 	save_max_page_sharing();
+	parse_ksm_options(opt_sizestr, &size, opt_numstr, &num, opt_unitstr, &unit);
 	mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
 }
 
@@ -103,7 +93,6 @@ static void cleanup(void)
 }
 
 static struct tst_test test = {
-	.tid = "ksm03",
 	.needs_root = 1,
 	.forks_child = 1,
 	.options = ksm_options,
testcases/kernel/mem/ksm/ksm04.c +2 -13
index 29a5e2b..1c8bf20 100644
@@ -57,30 +57,19 @@
 #include <stdio.h>
 #include <unistd.h>
 #include "mem.h"
+#include "ksm_common.h"
 
 #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
 	&& HAVE_MPOL_CONSTANTS
 
-static int merge_across_nodes;
-
-static struct tst_option ksm_options[] = {
-	{"n:", &opt_numstr,  "-n       Number of processes"},
-	{"s:", &opt_sizestr, "-s       Memory allocation size in MB"},
-	{"u:", &opt_unitstr, "-u       Memory allocation unit in MB"},
-	{NULL, NULL, NULL}
-};
-
 static void verify_ksm(void)
 {
-	int size = 128, num = 3, unit = 1;
 	unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
 	unsigned int node;
 
 	node = get_a_numa_node();
 	set_node(nmask, node);
 
-	check_ksm_options(&size, &num, &unit);
-
 	write_memcg();
 
 	if (set_mempolicy(MPOL_BIND, nmask, MAXNODES) == -1) {
@@ -120,13 +109,13 @@ static void setup(void)
 	}
 
 	save_max_page_sharing();
+	parse_ksm_options(opt_sizestr, &size, opt_numstr, &num, opt_unitstr, &unit);
 
 	mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW);
 	mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
 }
 
 static struct tst_test test = {
-	.tid = "ksm04",
 	.needs_root = 1,
 	.forks_child = 1,
 	.options = ksm_options,
testcases/kernel/mem/ksm/ksm05.c +0 -1
index 59c8f4b..73be1ed 100644
@@ -116,7 +116,6 @@ static void cleanup(void)
 }
 
 static struct tst_test test = {
-	.tid = "ksm05",
 	.needs_root = 1,
 	.forks_child = 1,
 	.setup = setup,
testcases/kernel/mem/ksm/ksm_common.h new file +48 -0

48 lines not on this page · raw message

testcases/kernel/mem/lib/mem.c +0 -25
index f5d86c7..cae6f1b 100644
@@ -635,31 +635,6 @@ void test_ksm_merge_across_nodes(unsigned long nr_pages)
 	wait_ksmd_done();
 }
 
-void check_ksm_options(int *size, int *num, int *unit)
-{
-	if (opt_sizestr) {
-		*size = atoi(opt_sizestr);
-		if (*size < 1)
-			tst_brk(TBROK, "size cannot be less than 1.");
-	}
-	if (opt_unitstr) {
-		*unit = atoi(opt_unitstr);
-		if (*unit > *size)
-			tst_brk(TBROK,
-				 "unit cannot be greater than size.");
-		if (*size % *unit != 0)
-			tst_brk(TBROK,
-				 "the remainder of division of size by unit is "
-				 "not zero.");
-	}
-	if (opt_numstr) {
-		*num = atoi(opt_numstr);
-		if (*num < 3)
-			tst_brk(TBROK,
-				 "process number cannot be less 3.");
-	}
-}
-
 /* THP */
 
 /* cpuset/memcg */
Signature · 1 line
2.9.3
author
#6 lore raw

PATCH v3 6/15 mem/thp: convert to new API

In reply to Li Wang
---
 runtest/mm                       |   2 +-
 testcases/kernel/mem/thp/thp01.c | 116 ++++++++++++++++-----------------------
 testcases/kernel/mem/thp/thp02.c |  96 ++++++++++----------------------
 testcases/kernel/mem/thp/thp03.c | 106 +++++++++++------------------------
 4 files changed, 110 insertions(+), 210 deletions(-)
runtest/mm +1 -1
index d108d18..fd37f1c 100644
@@ -81,7 +81,7 @@ oom05 oom05
 
 swapping01 swapping01 -i 5
 
-thp01 thp01 -I 120
+thp01 thp01 -I 10
 thp02 thp02
 thp03 thp03
 
testcases/kernel/mem/thp/thp01.c +48 -68

152 lines not on this page · raw message

testcases/kernel/mem/thp/thp02.c +29 -67

140 lines not on this page · raw message

testcases/kernel/mem/thp/thp03.c +32 -74

149 lines not on this page · raw message

Signature · 1 line
2.9.3
author
#7 lore raw

PATCH v3 7/15 mem/hugetlb: convert to new API

In reply to Li Wang
---
 testcases/kernel/mem/hugetlb/lib/hugetlb.c | 19 ++++++++++---------
 testcases/kernel/mem/hugetlb/lib/hugetlb.h |  9 +++++----
 2 files changed, 15 insertions(+), 13 deletions(-)
testcases/kernel/mem/hugetlb/lib/hugetlb.c +10 -9
index 4a9d489..7afc7d4 100644
@@ -32,6 +32,7 @@
  *	rm_shm()
  */
 
+#define TST_NO_DEFAULT_MAIN
 #include <sys/types.h>
 #include <sys/ipc.h>
 #include <sys/shm.h>
@@ -42,14 +43,14 @@
 void check_hugepage(void)
 {
 	if (access(PATH_HUGEPAGES, F_OK))
-		tst_brkm(TCONF, NULL, "Huge page is not supported.");
+		tst_brk(TCONF, "Huge page is not supported.");
 }
 
 /*
  * getipckey() - generates and returns a message key used by the "get"
  *		 calls to create an IPC resource.
  */
-int getipckey(void (*cleanup_fn) (void))
+int getipckey(void)
 {
 	const char a = 'a';
 	int ascii_a = (int)a;
@@ -60,7 +61,7 @@ int getipckey(void (*cleanup_fn) (void))
 
 	curdir = getcwd(curdir, size);
 	if (curdir == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup_fn, "getcwd(curdir)");
+		tst_brk(TBROK | TERRNO, "getcwd(curdir)");
 
 	/*
 	 * Get a Sys V IPC key
@@ -79,7 +80,7 @@ int getipckey(void (*cleanup_fn) (void))
 
 	ipc_key = ftok(curdir, ascii_a + random() % 26);
 	if (ipc_key == -1)
-		tst_brkm(TBROK | TERRNO, cleanup_fn, "ftok");
+		tst_brk(TBROK | TERRNO, __func__);
 
 	return ipc_key;
 }
@@ -87,13 +88,13 @@ int getipckey(void (*cleanup_fn) (void))
 /*
  * getuserid() - return the integer value for the "user" id
  */
-int getuserid(void (*cleanup_fn) (void), char *user)
+int getuserid(char *user)
 {
 	struct passwd *ent;
 
 	ent = getpwnam(user);
 	if (ent == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup_fn, "getpwnam");
+		tst_brk(TBROK | TERRNO, "getpwnam");
 
 	return ent->pw_uid;
 }
@@ -110,8 +111,8 @@ void rm_shm(int shm_id)
 	 * check for # of attaches ?
 	 */
 	if (shmctl(shm_id, IPC_RMID, NULL) == -1) {
-		tst_resm(TINFO, "WARNING: shared memory deletion failed.");
-		tst_resm(TINFO, "This could lead to IPC resource problems.");
-		tst_resm(TINFO, "id = %d", shm_id);
+		tst_res(TINFO, "WARNING: shared memory deletion failed.");
+		tst_res(TINFO, "This could lead to IPC resource problems.");
+		tst_res(TINFO, "id = %d", shm_id);
 	}
 }
testcases/kernel/mem/hugetlb/lib/hugetlb.h +5 -4
index 6b5c61e..1b5cc73 100644
@@ -24,11 +24,12 @@
 #ifndef __HUGETLB_H
 #define __HUGETLB_H
 
+#include <stdlib.h>
 #include <sys/ipc.h>
 #include <sys/shm.h>
 #include <errno.h>
 #include <sys/wait.h>
-#include "test.h"
+#include "tst_test.h"
 
 #define SHM_RD	0400
 #define SHM_WR	0200
@@ -49,12 +50,12 @@ key_t shmkey;			/* an IPC key generated by ftok() */
 
 void check_hugepage(void);
 
-int getipckey(void (*cleanup_fn) (void));
-int getuserid(void (*cleanup_fn) (void), char *user);
+int getipckey(void);
+int getuserid(char *user);
 void rm_shm(int shm_id);
 
 char *nr_opt;
-int sflag;
+char *Hopt;
 long orig_hugepages;
 
 #endif /* hugetlb.h */
Signature · 1 line
2.9.3
author
#8 lore raw

PATCH v3 8/15 mem/hugemmap: convert to new API

In reply to Li Wang
---
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap01.c | 180 +++++--------
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap02.c | 242 +++++++----------
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap04.c | 190 +++++---------
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap05.c | 290 ++++++++-------------
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap06.c |  94 +++----
 testcases/kernel/mem/hugetlb/lib/hugetlb.h         |   1 +
 6 files changed, 363 insertions(+), 634 deletions(-)
testcases/kernel/mem/hugetlb/hugemmap/hugemmap01.c +61 -119
index 71418f1..0d47d12 100644
@@ -1,20 +1,16 @@
 /*
+ * Copyright (c) International Business Machines  Corp., 2004
+ * Copyright (c) Linux Test Project, 2004-2017
  *
- *   Copyright (c) International Business Machines  Corp., 2004
+ * This program is free software;  you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
- *   This program is free software;  you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
- *   the GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program;  if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY;  without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+ * the GNU General Public License for more details.
  */
 
 /*
@@ -27,145 +23,89 @@
  *  mmap() should succeed returning the address of the hugetlb mapped region.
  *  The number of free huge pages should decrease.
  *
- * Algorithm:
- *  Setup:
- *   Setup signal handling.
- *   Pause for SIGUSR1 if option specified.
- *   Create temporary directory.
- *
  * Test:
  *  Loop if the proper options are given.
  *  Execute system call
  *  Check return code, if system call failed (return=-1)
  *  Log the errno and Issue a FAIL message.
- * Cleanup:
- *  Print timing stats if options given
- *  Delete the temporary directory created.
  *
  * HISTORY
  *  04/2004 Written by Robbie Williamson
  */
 
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <sys/mount.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <stdint.h>
 #include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
+#include <sys/mount.h>
 #include <limits.h>
 #include <sys/param.h>
 
-#include "test.h"
-#include "hugetlb.h"
-#include "safe_macros.h"
 #include "mem.h"
+#include "hugetlb.h"
 
-static char TEMPFILE[MAXPATHLEN];
+static struct tst_option options[] = {
+	{"H:", &Hopt,   "-H   /..  Location of hugetlbfs, i.e.  -H /var/hugetlbfs"},
+	{"s:", &nr_opt, "-s   num  Set the number of the been allocated hugepages"},
+	{NULL, NULL, NULL}
+};
 
-char *TCID = "hugemmap01";
-int TST_TOTAL = 1;
 static long *addr;
-static int fildes;
-static char *Hopt;
+static int  fildes;
 static long beforetest;
 static long aftertest;
 static long hugepagesmapped;
 static long hugepages = 128;
+static char TEMPFILE[MAXPATHLEN];
 
-static void help(void);
-
-int main(int ac, char **av)
+static void test_hugemmap(void)
 {
-	int lc;
-	int Hflag = 0;
 	long page_sz = 0;
-	int sflag = 0;
 
-	option_t options[] = {
-		{"H:", &Hflag, &Hopt},
-		{"s:", &sflag, &nr_opt},
-		{NULL, NULL, NULL}
-	};
+	/* Creat a temporary file used for mapping */
+	fildes = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
 
-	tst_parse_opts(ac, av, options, &help);
+	/* Note the number of free huge pages BEFORE testing */
+	beforetest = SAFE_READ_MEMINFO("HugePages_Free:");
 
-	if (!Hflag) {
-		tst_tmpdir();
-		Hopt = tst_get_tmpdir();
-	}
-	if (sflag)
-		hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		/* Creat a temporary file used for mapping */
-		fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666);
-		if (fildes < 0)
-			tst_brkm(TFAIL | TERRNO, cleanup, "open %s failed",
-				 TEMPFILE);
-
-		tst_count = 0;
-
-		/* Note the number of free huge pages BEFORE testing */
-		beforetest = read_meminfo("HugePages_Free:");
-
-		/* Note the size of huge page size BEFORE testing */
-		page_sz = read_meminfo("Hugepagesize:") * 1024;
-
-		addr = mmap(NULL, page_sz, PROT_READ | PROT_WRITE,
-			    MAP_SHARED, fildes, 0);
-		if (addr == MAP_FAILED) {
-			tst_resm(TFAIL | TERRNO, "mmap() Failed on %s",
-				 TEMPFILE);
-			close(fildes);
-			continue;
-		} else {
-			close(fildes);
-			tst_resm(TPASS, "call succeeded");
-			/* force to allocate page and change HugePages_Free */
-			*(int *)addr = 0;
-		}
-
-		/*
-		 * Make sure the number of free huge pages
-		 * AFTER testing decreased
-		 */
-		aftertest = read_meminfo("HugePages_Free:");
-		hugepagesmapped = beforetest - aftertest;
-		if (hugepagesmapped < 1)
-			tst_resm(TWARN, "Number of HUGEPAGES_FREE stayed the"
-				 " same. Okay if multiple copies running due"
-				 " to test collision.");
+	/* Note the size of huge page size BEFORE testing */
+	page_sz = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
+
+	addr = mmap(NULL, page_sz, PROT_READ | PROT_WRITE,
+			MAP_SHARED, fildes, 0);
 
-		/* Clean up things in case we are looping */
-		/* Unmap the mapped memory */
-		if (munmap(addr, page_sz) != 0)
-			tst_brkm(TFAIL | TERRNO, NULL, "munmap failed");
+	if (addr == MAP_FAILED) {
+		tst_res(TFAIL | TERRNO, "mmap() Failed on %s",
+				TEMPFILE);
+	} else {
+		tst_res(TPASS, "call succeeded");
 
-		close(fildes);
+		/* force to allocate page and change HugePages_Free */
+		*(int *)addr = 0;
+		/* Make sure the number of free huge pages AFTER testing decreased */
+		aftertest = SAFE_READ_MEMINFO("HugePages_Free:");
+		hugepagesmapped = beforetest - aftertest;
+		if (hugepagesmapped < 1)
+			tst_res(TWARN, "Number of HUGEPAGES_FREE stayed the"
+					" same. Okay if multiple copies running due"
+					" to test collision.");
+		munmap(addr, page_sz);
 	}
 
-	cleanup();
-	tst_exit();
+	close(fildes);
 }
 
 void setup(void)
 {
-	TEST_PAUSE;
-	tst_require_root();
 	check_hugepage();
+	orig_hugepages = get_sys_tune("nr_hugepages");
+
+	if (!Hopt)
+		Hopt = tst_get_tmpdir();
 	if (mount("none", Hopt, "hugetlbfs", 0, NULL) < 0)
-		tst_brkm(TBROK | TERRNO, NULL, "mount failed on %s", Hopt);
+		tst_brk(TBROK | TERRNO, "mount failed on %s", Hopt);
 
-	orig_hugepages = get_sys_tune("nr_hugepages");
+	if (nr_opt)
+		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
 	set_sys_tune("nr_hugepages", hugepages, 1);
+
 	snprintf(TEMPFILE, sizeof(TEMPFILE), "%s/mmapfile%d", Hopt, getpid());
 }
 
@@ -175,11 +115,13 @@ void cleanup(void)
 	set_sys_tune("nr_hugepages", orig_hugepages, 0);
 
 	umount(Hopt);
-	tst_rmdir();
 }
 
-static void help(void)
-{
-	printf("    -H /..  Location of hugetlbfs, i.e. -H /var/hugetlbfs\n");
-	printf("    -s num  Set the number of the been allocated hugepages\n");
-}
+static struct tst_test test = {
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.options = options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = test_hugemmap,
+};
testcases/kernel/mem/hugetlb/hugemmap/hugemmap02.c +97 -145

300 lines not on this page · raw message

testcases/kernel/mem/hugetlb/hugemmap/hugemmap04.c +66 -124

253 lines not on this page · raw message

testcases/kernel/mem/hugetlb/hugemmap/hugemmap05.c +106 -184

507 lines not on this page · raw message

testcases/kernel/mem/hugetlb/hugemmap/hugemmap06.c +32 -62

172 lines not on this page · raw message

testcases/kernel/mem/hugetlb/lib/hugetlb.h +1 -0
index 1b5cc73..c6d2016 100644
@@ -30,6 +30,7 @@
 #include <errno.h>
 #include <sys/wait.h>
 #include "tst_test.h"
+#include "old_tmpdir.h"
 
 #define SHM_RD	0400
 #define SHM_WR	0200
Signature · 1 line
2.9.3
author
#9 lore raw

PATCH v3 9/15 mem/hugeshmat: convert to new API

In reply to Li Wang
---
 .../kernel/mem/hugetlb/hugeshmat/hugeshmat01.c     | 177 +++++++++------------
 .../kernel/mem/hugetlb/hugeshmat/hugeshmat02.c     | 130 ++++++---------
 .../kernel/mem/hugetlb/hugeshmat/hugeshmat03.c     | 130 ++++++---------
 .../kernel/mem/hugetlb/hugeshmat/hugeshmat04.c     | 117 +++++---------
 .../kernel/mem/hugetlb/hugeshmat/hugeshmat05.c     |  69 +++-----
 5 files changed, 234 insertions(+), 389 deletions(-)
testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat01.c +71 -106
index 3208f5c..a0867a8 100644
@@ -1,26 +1,19 @@
 /*
+ * Copyright (c) International Business Machines  Corp., 2001
+ * Copyright (c) Linux Test Project, 2001-2017
  *
- *   Copyright (c) International Business Machines  Corp., 2001
+ * This program is free software;  you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
- *   This program is free software;  you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
- *   the GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program;  if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY;  without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+ * the GNU General Public License for more details.
  */
 
 /*
- * NAME
- *	hugeshmat01.c
- *
  * DESCRIPTION
  *	hugeshmat01 - test that shmat() works correctly
  *
@@ -39,108 +32,75 @@
  *			issue a FAIL message
  *	call cleanup
  *
- * USAGE:  <for command-line>
- *  hugeshmat01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -f   : Turn off functionality Testing.
- *	       -i n : Execute test n times.
- *	       -I x : Execute test for x seconds.
- *	       -P x : Pause for x seconds between iterations.
- *	       -t   : Turn on syscall timing.
- *
  * HISTORY
  *	03/2001 - Written by Wayne Boyer
  *	04/2004 - Updated by Robbie Williamson
- *
- * RESTRICTIONS
- *	none
  */
 
 #include <limits.h>
 #include "hugetlb.h"
-#include "safe_macros.h"
 #include "mem.h"
+#include "hugetlb.h"
 
-char *TCID = "hugeshmat01";
-int TST_TOTAL = 3;
-
-#define CASE0		10	/* values to write into the shared */
-#define CASE1		20	/* memory location.                */
+#define CASE0 10 /* values to write into the shared */
+#define CASE1 20 /* memory location.                */
 
 static size_t shm_size;
 static int shm_id_1 = -1;
 static void *addr;
 
 static long hugepages = 128;
-static option_t options[] = {
-	{"s:", &sflag, &nr_opt},
+
+static struct tst_option options[] = {
+	{"s:", &nr_opt, "-s   num  Set the number of the been allocated hugepages"},
 	{NULL, NULL, NULL}
 };
 
-struct test_case_t {
+static struct tcase {
 	int *shmid;
 	void *addr;
 	int flags;
-} TC[] = {
+} tcases[] = {
 	/* a straight forward read/write attach */
-	{
-	&shm_id_1, 0, 0},
-	    /*
-	     * an attach using non aligned memory
-	     * -1 will be replaced with an unaligned addr
-	     */
-	{
-	&shm_id_1, (void *)-1, SHM_RND},
-	    /* a read only attach */
-	{
-	&shm_id_1, 0, SHM_RDONLY}
+	{&shm_id_1, 0, 0},
+	/*
+	 * an attach using non aligned memory
+	 * -1 will be replaced with an unaligned addr
+	 */
+	{&shm_id_1, (void *)-1, SHM_RND},
+	/* a read only attach */
+	{&shm_id_1, 0, SHM_RDONLY}
 };
 
-static void check_functionality(int i);
+static void check_functionality(unsigned int i);
 
-int main(int ac, char **av)
+static void verify_hugeshmat(unsigned int i)
 {
-	int lc, i;
-
-	tst_parse_opts(ac, av, options, NULL);
-
-	if (sflag)
-		hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++) {
-			addr = shmat(*(TC[i].shmid), TC[i].addr, TC[i].flags);
-			if (addr == (void *)-1) {
-				tst_brkm(TFAIL | TERRNO, cleanup, "shmat");
-			} else {
-				check_functionality(i);
-			}
-
-			/*
-			 * addr in TC[0] will be used to generate an unaligned
-			 * address for TC[1]
-			 */
-			if (i == 0 && addr != (void *)-1)
-				TC[1].addr = (void *)(((unsigned long)addr &
-						       ~(SHMLBA - 1)) + SHMLBA -
-						      1);
-			if (shmdt(addr) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup, "shmdt");
-		}
+	struct tcase *tc = &tcases[i];
+
+	addr = shmat(*(tc->shmid), tc->addr, tc->flags);
+	if (addr == (void *)-1) {
+		tst_brk(TFAIL | TERRNO, "shmat");
+	} else {
+		check_functionality(i);
 	}
-	cleanup();
-	tst_exit();
+
+	/*
+	 * addr in tcases[0] will be used to generate an unaligned
+	 * address for tcases[1]
+	 */
+	if (i == 0 && addr != (void *)-1)
+		tc[1].addr = (void *)(((unsigned long)addr &
+					~(SHMLBA - 1)) + SHMLBA - 1);
+	if (shmdt(addr) == -1)
+		tst_brk(TBROK | TERRNO, "shmdt");
 }
 
 /*
  * check_functionality - check various conditions to make sure they
  *			 are correct.
  */
-static void check_functionality(int i)
+static void check_functionality(unsigned int i)
 {
 	void *orig_add;
 	int *shared;
@@ -150,17 +110,17 @@ static void check_functionality(int i)
 
 	/* stat the shared memory ID */
 	if (shmctl(shm_id_1, IPC_STAT, &buf) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "shmctl");
+		tst_brk(TBROK | TERRNO, "shmctl");
 
 	/* check the number of attaches */
 	if (buf.shm_nattch != 1) {
-		tst_resm(TFAIL, "# of attaches is incorrect");
+		tst_res(TFAIL, "# of attaches is incorrect");
 		return;
 	}
 
 	/* check the size of the segment */
 	if (buf.shm_segsz != shm_size) {
-		tst_resm(TFAIL, "segment size is incorrect");
+		tst_res(TFAIL, "segment size is incorrect");
 		return;
 	}
 
@@ -183,9 +143,9 @@ static void check_functionality(int i)
 		 * specified in the man page.
 		 */
 		*shared = CASE1;
-		orig_add = addr + ((unsigned long)TC[i].addr % SHMLBA);
-		if (orig_add != TC[i].addr) {
-			tst_resm(TFAIL, "shared memory address is not "
+		orig_add = addr + ((unsigned long)tcases[i].addr % SHMLBA);
+		if (orig_add != tcases[i].addr) {
+			tst_res(TFAIL, "shared memory address is not "
 				 "correct");
 			return;
 		}
@@ -197,43 +157,48 @@ static void check_functionality(int i)
 		 * because shared memory is persistent.
 		 */
 		if (*shared != CASE1) {
-			tst_resm(TFAIL, "shared memory value isn't correct");
+			tst_res(TFAIL, "shared memory value isn't correct");
 			return;
 		}
 		break;
 	}
-	tst_resm(TPASS, "conditions and functionality are correct");
+	tst_res(TPASS, "conditions and functionality are correct");
 }
 
-void setup(void)
+static void setup(void)
 {
 	long hpage_size;
 
-	tst_require_root();
 	check_hugepage();
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-	tst_tmpdir();
+	if (nr_opt)
+		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
 
 	orig_hugepages = get_sys_tune("nr_hugepages");
 	set_sys_tune("nr_hugepages", hugepages, 1);
-	hpage_size = read_meminfo("Hugepagesize:") * 1024;
+	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 
 	shm_size = hpage_size * hugepages / 2;
 	update_shm_size(&shm_size);
-	shmkey = getipckey(cleanup);
+	shmkey = getipckey();
 	shm_id_1 = shmget(shmkey++, shm_size,
 			  SHM_HUGETLB | SHM_RW | IPC_CREAT | IPC_EXCL);
 	if (shm_id_1 == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "shmget");
+		tst_brk(TBROK | TERRNO, "shmget");
 
-	TEST_PAUSE;
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	rm_shm(shm_id_1);
-
 	set_sys_tune("nr_hugepages", orig_hugepages, 0);
-
-	tst_rmdir();
 }
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.options = options,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_hugeshmat,
+	.setup = setup,
+	.cleanup = cleanup,
+};
testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat02.c +51 -79

191 lines not on this page · raw message

testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat03.c +47 -83

191 lines not on this page · raw message

testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c +42 -75

180 lines not on this page · raw message

testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat05.c +23 -46

129 lines not on this page · raw message

Signature · 1 line
2.9.3
author
#10 lore raw

PATCH v3 10/15 mem/hugeshmctl: convert to new API

In reply to Li Wang
---
 .../kernel/mem/hugetlb/hugeshmctl/hugeshmctl01.c   | 210 +++++++++------------
 .../kernel/mem/hugetlb/hugeshmctl/hugeshmctl02.c   | 151 ++++++---------
 .../kernel/mem/hugetlb/hugeshmctl/hugeshmctl03.c   | 149 ++++++---------
 3 files changed, 207 insertions(+), 303 deletions(-)
testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl01.c +91 -119
index f1e9195..0e37b8f 100644
@@ -1,27 +1,19 @@
 /*
+ * Copyright (c) International Business Machines  Corp., 2004
+ * Copyright (c) Linux Test Project, 2004-2017
  *
- *   Copyright (c) International Business Machines  Corp., 2004
+ * This program is free software;  you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
- *   This program is free software;  you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
- *   the GNU General Public License for more details.
- *
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program;  if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY;  without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+ * the GNU General Public License for more details.
  */
 
 /*
- * NAME
- *	hugeshmctl01.c
- *
  * DESCRIPTION
  *	hugeshmctl01 - test the IPC_STAT, IPC_SET and IPC_RMID commands as
  *		   they are used with shmctl()
@@ -44,32 +36,15 @@
  *	    issue a PASS message
  *	call cleanup
  *
- * USAGE:  <for command-line>
- *  hugeshmctl01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -f   : Turn off functionality Testing.
- *	       -i n : Execute test n times.
- *	       -I x : Execute test for x seconds.
- *	       -P x : Pause for x seconds between iterations.
- *	       -t   : Turn on syscall timing.
- *
  * HISTORY
  *	03/2001 - Written by Wayne Boyer
  *	04/2004 - Updated by Robbie Williamson
- *
- * RESTRICTIONS
- *	none
  */
 
 #include <limits.h>
-#include "test.h"
-#include "safe_macros.h"
 #include "mem.h"
 #include "hugetlb.h"
 
-char *TCID = "hugeshmctl01";
-int TST_TOTAL = 4;
-
 #define FIRST		0
 #define SECOND		1
 #define N_ATTACH	4
@@ -93,67 +68,54 @@ static void func_rmid(void);
 static void *set_shmat(void);
 
 static long hugepages = 128;
-static option_t options[] = {
-	{"s:", &sflag, &nr_opt},
+
+static struct tst_option options[] = {
+	{"s:", &nr_opt, "-s   num  Set the number of the been allocated hugepages"},
 	{NULL, NULL, NULL}
 };
 
-struct test_case_t {
+struct tcase {
 	int cmd;
 	void (*func_test) (void);
 	void (*func_setup) (void);
-} TC[] = {
-	{
-	IPC_STAT, func_stat, stat_setup}, {
-	IPC_STAT, func_stat, stat_setup}, {
-	IPC_SET, func_set, set_setup}, {
-	IPC_RMID, func_rmid, NULL}
+} tcases[] = {
+	{IPC_STAT, func_stat, stat_setup},
+	{IPC_STAT, func_stat, stat_setup},
+	{IPC_SET,  func_set,  set_setup},
+	{IPC_RMID, func_rmid, NULL}
 };
 
-int main(int ac, char **av)
+static void test_hugeshmctl(void)
 {
-	int lc, i;
+	unsigned int i;
 
-	tst_parse_opts(ac, av, options, NULL);
+	/* initialize stat_time */
+	stat_time = FIRST;
 
-	if (sflag)
-		hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		/* initialize stat_time */
-		stat_time = FIRST;
+	/*
+	 * Create a shared memory segment with read and write
+	 * permissions.  Do this here instead of in setup()
+	 * so that looping (-i) will work correctly.
+	 */
+	shm_id_1 = shmget(shmkey, shm_size,
+			SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
+	if (shm_id_1 == -1)
+		tst_brk(TBROK | TERRNO, "shmget #main");
 
+	for (i = 0; i < ARRAY_SIZE(tcases); i++) {
 		/*
-		 * Create a shared memory segment with read and write
-		 * permissions.  Do this here instead of in setup()
-		 * so that looping (-i) will work correctly.
+		 * if needed, set up any required conditions by
+		 * calling the appropriate setup function
 		 */
-		shm_id_1 = shmget(shmkey, shm_size,
-				  SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
-		if (shm_id_1 == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "shmget #main");
+		if (tcases[i].func_setup != NULL)
+			(*tcases[i].func_setup) ();
 
-		for (i = 0; i < TST_TOTAL; i++) {
-			/*
-			 * if needed, set up any required conditions by
-			 * calling the appropriate setup function
-			 */
-			if (TC[i].func_setup != NULL)
-				(*TC[i].func_setup) ();
-
-			if (shmctl(shm_id_1, TC[i].cmd, &buf) == -1) {
-				tst_resm(TFAIL | TERRNO, "shmctl #main");
-				continue;
-			}
-			(*TC[i].func_test) ();
+		if (shmctl(shm_id_1, tcases[i].cmd, &buf) == -1) {
+			tst_res(TFAIL | TERRNO, "shmctl #main");
+			continue;
 		}
+		(*tcases[i].func_test) ();
 	}
-	cleanup();
-	tst_exit();
 }
 
 /*
@@ -167,7 +129,7 @@ void *set_shmat(void)
 
 	rval = shmat(shm_id_1, 0, 0);
 	if (rval == (void *)-1)
-		tst_brkm(TBROK | TERRNO, cleanup, "set shmat");
+		tst_brk(TBROK | TERRNO, "set shmat");
 
 	return rval;
 }
@@ -183,7 +145,15 @@ static void stat_setup(void)
 	void *test;
 	pid_t pid;
 	sigset_t newmask, oldmask;
-
+	struct sigaction sa;
+
+	memset (&sa, '\0', sizeof(sa));
+	sa.sa_handler = sighandler;
+	sa.sa_flags = 0;
+	TEST(sigaction(SIGUSR1, &sa, NULL));
+	if (TEST_RETURN == -1)
+		tst_brk(TBROK | TRERRNO,
+				"SIGSEGV signal setup failed");
 	/*
 	 * The first time through, let the children attach the memory.
 	 * The second time through, attach the memory first and let
@@ -211,12 +181,10 @@ static void stat_setup(void)
 	sigemptyset(&newmask);
 	sigaddset(&newmask, SIGUSR1);
 	if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "block SIGUSR1 error");
+		tst_brk(TBROK | TERRNO, "block SIGUSR1 error");
 
 	for (i = 0; i < N_ATTACH; i++) {
-		switch (pid = fork()) {
-		case -1:
-			tst_brkm(TBROK | TERRNO, cleanup, "fork");
+		switch (pid = SAFE_FORK()) {
 		case 0:
 			test = (stat_time == FIRST) ? set_shmat() : set_shared;
 
@@ -230,19 +198,19 @@ static void stat_setup(void)
 			 */
 			rval = sigsuspend(&oldmask);
 			if (rval != -1)
-				tst_brkm(TBROK | TERRNO, cleanup, "sigsuspend");
+				tst_brk(TBROK | TERRNO, "sigsuspend");
 
 			/*
 			 * don't have to block SIGUSR1 any more,
 			 * recover the mask
 			 */
 			if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
-				tst_brkm(TBROK | TERRNO, cleanup,
+				tst_brk(TBROK | TERRNO,
 					 "child sigprocmask");
 
 			/* now we're back - detach the memory and exit */
 			if (shmdt(test) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
+				tst_brk(TBROK | TERRNO,
 					 "shmdt in stat_setup()");
 			exit(0);
 		default:
@@ -253,7 +221,7 @@ static void stat_setup(void)
 
 	/* parent doesn't have to block SIGUSR1, recover the mask */
 	if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
-		tst_brkm(TBROK, cleanup, "parent sigprocmask");
+		tst_brk(TBROK, "parent sigprocmask");
 
 	usleep(250000);
 }
@@ -271,12 +239,12 @@ static void func_stat(void)
 	pid = getpid();
 
 	if (buf.shm_cpid != pid) {
-		tst_resm(TFAIL, "creator pid is incorrect");
+		tst_res(TFAIL, "creator pid is incorrect");
 		goto fail;
 	}
 
 	if (buf.shm_segsz != shm_size) {
-		tst_resm(TFAIL, "segment size is incorrect");
+		tst_res(TFAIL, "segment size is incorrect");
 		goto fail;
 	}
 
@@ -287,18 +255,18 @@ static void func_stat(void)
 	 * that memory so the attaches equal N_ATTACH + stat_time (1).
 	 */
 	if (buf.shm_nattch != N_ATTACH + stat_time) {
-		tst_resm(TFAIL, "# of attaches is incorrect - %lu",
+		tst_res(TFAIL, "# of attaches is incorrect - %lu",
 			 (unsigned long)buf.shm_nattch);
 		goto fail;
 	}
 
 	/* use MODE_MASK to make sure we are comparing the last 9 bits */
 	if ((buf.shm_perm.mode & MODE_MASK) != ((SHM_RW) & MODE_MASK)) {
-		tst_resm(TFAIL, "segment mode is incorrect");
+		tst_res(TFAIL, "segment mode is incorrect");
 		goto fail;
 	}
 
-	tst_resm(TPASS, "pid, size, # of attaches and mode are correct "
+	tst_res(TPASS, "pid, size, # of attaches and mode are correct "
 		 "- pass #%d", stat_time);
 
 fail:
@@ -320,15 +288,21 @@ static void stat_cleanup(void)
 	/* wake up the childern so they can detach the memory and exit */
 	for (i = 0; i < N_ATTACH; i++)
 		if (kill(pid_arr[i], SIGUSR1) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "kill with SIGUSR1");
+			tst_brk(TBROK | TERRNO, "kill with SIGUSR1");
 
 	/* remove the parent's shared memory the second time through */
 	if (stat_time == SECOND)
 		if (shmdt(set_shared) == -1)
-			tst_resm(TBROK | TERRNO, "shmdt in stat_cleanup()");
+			tst_res(TBROK | TERRNO, "shmdt in stat_cleanup()");
 	stat_time++;
 }
 
+static void sighandler(int sig)
+{
+	if (sig != SIGUSR1)
+		tst_res(TFAIL, "received unexpected signal %d", sig);
+}
+
 /*
  * set_setup() - set up for the IPC_SET command with shmctl()
  */
@@ -348,21 +322,21 @@ static void func_set(void)
 {
 	/* first stat the shared memory to get the new data */
 	if (shmctl(shm_id_1, IPC_STAT, &buf) == -1) {
-		tst_resm(TBROK | TERRNO, "shmctl in func_set()");
+		tst_res(TBROK | TERRNO, "shmctl in func_set()");
 		return;
 	}
 
 	if ((buf.shm_perm.mode & MODE_MASK) != ((SHM_RW | NEWMODE) & MODE_MASK)) {
-		tst_resm(TFAIL, "new mode is incorrect");
+		tst_res(TFAIL, "new mode is incorrect");
 		return;
 	}
 
 	if (save_time >= buf.shm_ctime) {
-		tst_resm(TFAIL, "change time is incorrect");
+		tst_res(TFAIL, "change time is incorrect");
 		return;
 	}
 
-	tst_resm(TPASS, "new mode and change time are correct");
+	tst_res(TPASS, "new mode and change time are correct");
 }
 
 /*
@@ -372,48 +346,46 @@ static void func_rmid(void)
 {
 	/* Do another shmctl() - we should get EINVAL */
 	if (shmctl(shm_id_1, IPC_STAT, &buf) != -1)
-		tst_brkm(TBROK, cleanup, "shmctl in func_rmid() "
+		tst_brk(TBROK, "shmctl in func_rmid() "
 			 "succeeded unexpectedly");
 	if (errno != EINVAL)
-		tst_resm(TFAIL | TERRNO, "shmctl in func_rmid() failed "
+		tst_res(TFAIL | TERRNO, "shmctl in func_rmid() failed "
 			 "unexpectedly - expect errno=EINVAL, got");
 	else
-		tst_resm(TPASS, "shmctl in func_rmid() failed as expected, "
+		tst_res(TPASS, "shmctl in func_rmid() failed as expected, "
 			 "shared memory appears to be removed");
 	shm_id_1 = -1;
 }
 
-static void sighandler(int sig)
-{
-	if (sig != SIGUSR1)
-		tst_resm(TFAIL, "received unexpected signal %d", sig);
-}
-
 void setup(void)
 {
 	long hpage_size;
 
-	tst_require_root();
 	check_hugepage();
-	tst_sig(FORK, sighandler, cleanup);
-	tst_tmpdir();
+	if (nr_opt)
+		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
 
 	orig_hugepages = get_sys_tune("nr_hugepages");
 	set_sys_tune("nr_hugepages", hugepages, 1);
-	hpage_size = read_meminfo("Hugepagesize:") * 1024;
+	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 
 	shm_size = hpage_size * hugepages / 2;
 	update_shm_size(&shm_size);
-	shmkey = getipckey(cleanup);
-
-	TEST_PAUSE;
+	shmkey = getipckey();
 }
 
 void cleanup(void)
 {
 	rm_shm(shm_id_1);
-
 	set_sys_tune("nr_hugepages", orig_hugepages, 0);
-
-	tst_rmdir();
 }
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.forks_child = 1,
+	.needs_tmpdir = 1,
+	.options = options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = test_hugeshmctl,
+};
testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl02.c +58 -93

218 lines not on this page · raw message

testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl03.c +58 -91

227 lines not on this page · raw message

Signature · 1 line
2.9.3
author
#11 lore raw

PATCH v3 11/15 mm/hugeshmdt: convert to new API

In reply to Li Wang
---
 .../kernel/mem/hugetlb/hugeshmdt/hugeshmdt01.c     | 122 ++++++++-------------
 1 file changed, 46 insertions(+), 76 deletions(-)
testcases/kernel/mem/hugetlb/hugeshmdt/hugeshmdt01.c +46 -76
index 45ff286..796d6df 100644
@@ -1,26 +1,19 @@
 /*
+ * Copyright (c) International Business Machines  Corp., 2004
+ * Copyright (c) Linux Test Project, 2004-2017
  *
- *   Copyright (c) International Business Machines  Corp., 2004
+ * This program is free software;  you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
- *   This program is free software;  you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
- *   the GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program;  if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY;  without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+ * the GNU General Public License for more details.
  */
 
 /*
- * NAME
- *	hugeshmdt01.c
- *
  * DESCRIPTION
  *	hugeshmdt01 - check that largr shared memory is detached correctly
  *
@@ -41,30 +34,16 @@
  *			issue a FAIL message
  *	call cleanup
  *
- * USAGE:  <for command-line>
- *  hugeshmdt01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -f   : Turn off functionality Testing.
- *	       -I x : Execute test for x seconds.
- *	       -P x : Pause for x seconds between iterations.
- *	       -t   : Turn on syscall timing.
- *
  * HISTORY
  *	03/2001 - Written by Wayne Boyer
  *	04/2004 - Updated by Robbie Williamson
- *
- * RESTRICTIONS
- *	none
  */
 
 #include <setjmp.h>
 #include <limits.h>
 #include "hugetlb.h"
-#include "safe_macros.h"
 #include "mem.h"
-
-char *TCID = "hugeshmdt01";
-int TST_TOTAL = 1;
+#include "hugetlb.h"
 
 static size_t shm_size;
 static int shm_id_1 = -1;
@@ -74,54 +53,43 @@ static int pass;
 static sigjmp_buf env;
 
 static long hugepages = 128;
-static option_t options[] = {
-	{"s:", &sflag, &nr_opt},
+static struct tst_option options[] = {
+	{"s:", &nr_opt, "-s   num  Set the number of the been allocated hugepages"},
 	{NULL, NULL, NULL}
 };
 
 static void check_functionality(void);
 static void sighandler(int sig);
 
-int main(int ac, char **av)
+static void hugeshmdt_test(void)
 {
-	int lc;
-
-	tst_parse_opts(ac, av, options, NULL);
-
-	if (sflag)
-		hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX);
-
-	setup();
+	struct sigaction sa;
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
+	sa.sa_handler = sighandler;
+	sigaction(SIGSEGV, &sa, NULL);
 
-		if (shmdt(shared) == -1) {
-			tst_resm(TFAIL | TERRNO, "shmdt");
-		} else {
-			check_functionality();
-		}
+	if (shmdt(shared) == -1)
+		tst_res(TFAIL | TERRNO, "shmdt");
+	else
+		check_functionality();
 
-		/* reattach the shared memory segment in case we are looping */
-		shared = shmat(shm_id_1, 0, 0);
-		if (shared == (void *)-1)
-			tst_brkm(TBROK | TERRNO, cleanup, "shmat #2: reattach");
+	/* reattach the shared memory segment in case we are looping */
+	shared = shmat(shm_id_1, 0, 0);
+	if (shared == (void *)-1)
+		tst_brk(TBROK | TERRNO, "shmat #2: reattach");
 
-		/* also reset pass */
-		pass = 0;
-	}
-	cleanup();
-	tst_exit();
+	/* also reset pass */
+	pass = 0;
 }
 
 static void check_functionality(void)
 {
 	/* stat the shared memory segment */
 	if (shmctl(shm_id_1, IPC_STAT, &buf) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "shmctl");
+		tst_brk(TBROK | TERRNO, "shmctl");
 
 	if (buf.shm_nattch != 0) {
-		tst_resm(TFAIL, "# of attaches is incorrect");
+		tst_res(TFAIL, "# of attaches is incorrect");
 		return;
 	}
 
@@ -143,9 +111,9 @@ static void check_functionality(void)
 		*shared = 2;
 
 	if (pass)
-		tst_resm(TPASS, "huge shared memory detached correctly");
+		tst_res(TPASS, "huge shared memory detached correctly");
 	else
-		tst_resm(TFAIL, "huge shared memory was not detached "
+		tst_res(TFAIL, "huge shared memory was not detached "
 			 "correctly");
 }
 
@@ -157,7 +125,7 @@ static void sighandler(int sig)
 		pass = 1;
 		siglongjmp(env, 1);
 	} else {
-		tst_brkm(TBROK, cleanup, "unexpected signal received: %d", sig);
+		tst_brk(TBROK, "unexpected signal received: %d", sig);
 	}
 }
 
@@ -165,41 +133,43 @@ void setup(void)
 {
 	long hpage_size;
 
-	tst_require_root();
 	check_hugepage();
-	tst_sig(NOFORK, sighandler, cleanup);
-	tst_tmpdir();
+	if (nr_opt)
+		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
 
 	orig_hugepages = get_sys_tune("nr_hugepages");
 	set_sys_tune("nr_hugepages", hugepages, 1);
-	hpage_size = read_meminfo("Hugepagesize:") * 1024;
+	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 
 	shm_size = hpage_size * hugepages / 2;
 	update_shm_size(&shm_size);
-	shmkey = getipckey(cleanup);
+	shmkey = getipckey();
 
 	/* create a shared memory resource with read and write permissions */
 	shm_id_1 = shmget(shmkey, shm_size,
 			  SHM_HUGETLB | SHM_RW | IPC_CREAT | IPC_EXCL);
 	if (shm_id_1 == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "shmget");
+		tst_brk(TBROK | TERRNO, "shmget");
 
 	/* attach the shared memory segment */
 	shared = shmat(shm_id_1, 0, 0);
 	if (shared == (void *)-1)
-		tst_brkm(TBROK | TERRNO, cleanup, "shmat #1");
+		tst_brk(TBROK | TERRNO, "shmat #1");
 
 	/* give a value to the shared memory integer */
 	*shared = 4;
-
-	TEST_PAUSE;
 }
 
 void cleanup(void)
 {
 	rm_shm(shm_id_1);
-
 	set_sys_tune("nr_hugepages", orig_hugepages, 0);
-
-	tst_rmdir();
 }
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.options = options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = hugeshmdt_test,
+};
Signature · 1 line
2.9.3
author
#12 lore raw

PATCH v3 12/15 mem/hugeshmget: convert to new API

In reply to Li Wang
---
 .../kernel/mem/hugetlb/hugeshmget/hugeshmget01.c   | 166 ++++++++-------------
 .../kernel/mem/hugetlb/hugeshmget/hugeshmget02.c   | 154 ++++++++-----------
 .../kernel/mem/hugetlb/hugeshmget/hugeshmget03.c   | 116 +++++---------
 .../kernel/mem/hugetlb/hugeshmget/hugeshmget05.c   | 117 +++++----------
 4 files changed, 203 insertions(+), 350 deletions(-)
testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget01.c +62 -104
index 9da79c1..0997e8b 100644
@@ -1,26 +1,19 @@
 /*
+ * Copyright (c) International Business Machines  Corp., 2004
+ * Copyright (c) Linux Test Project, 2004-2017
  *
- *   Copyright (c) International Business Machines  Corp., 2004
+ * This program is free software;  you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
- *   This program is free software;  you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
- *   the GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program;  if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY;  without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+ * the GNU General Public License for more details.
  */
 
 /*
- * NAME
- *	hugeshmget01.c
- *
  * DESCRIPTION
  *	hugeshmget01 - test that shmget() correctly creates a large
  *			shared memory segment
@@ -41,129 +34,94 @@
  *	  else issue a PASS message
  *	call cleanup
  *
- * USAGE:  <for command-line>
- *  shmget01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -f   : Turn off functionality Testing.
- *	       -i n : Execute test n times.
- *	       -I x : Execute test for x seconds.
- *	       -P x : Pause for x seconds between iterations.
- *	       -t   : Turn on syscall timing.
- *
  * HISTORY
  *	03/2001 - Written by Wayne Boyer
  *	04/2004 - Updated by Robbie Williamson
- *
- * RESTRICTIONS
- *	none
  */
 
 #include <limits.h>
 #include "hugetlb.h"
-#include "safe_macros.h"
 #include "mem.h"
-
-char *TCID = "hugeshmget01";
-int TST_TOTAL = 1;
+#include "hugetlb.h"
 
 static size_t shm_size;
 static int shm_id_1 = -1;
 
 static long hugepages = 128;
-static option_t options[] = {
-	{"s:", &sflag, &nr_opt},
+static struct tst_option options[] = {
+	{"s:", &nr_opt, "-s   num  Set the number of the been allocated hugepages"},
 	{NULL, NULL, NULL}
 };
 
-int main(int ac, char **av)
+static void test_hugeshmget(void)
 {
-	int lc;
 	struct shmid_ds buf;
 
-	tst_parse_opts(ac, av, options, NULL);
-
-	if (sflag)
-		hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		shm_id_1 = shmget(shmkey, shm_size,
-				  SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
-		if (shm_id_1 == -1) {
-			tst_resm(TFAIL | TERRNO, "shmget");
-		} else {
-			/* do a STAT and check some info */
-			if (shmctl(shm_id_1, IPC_STAT, &buf) == -1) {
-				tst_resm(TBROK | TERRNO,
-					 "shmctl(IPC_STAT)");
-				continue;
-			}
-			/* check the seqment size */
-			if (buf.shm_segsz != shm_size) {
-				tst_resm(TFAIL, "seqment size is not "
-					 "correct");
-				continue;
-			}
-			/* check the pid of the creator */
-			if (buf.shm_cpid != getpid()) {
-				tst_resm(TFAIL, "creator pid is not "
-					 "correct");
-				continue;
-			}
-			/*
-			 * check the mode of the seqment
-			 * mask out all but the lower 9 bits
-			 */
-			if ((buf.shm_perm.mode & MODE_MASK) !=
-			    ((SHM_RW) & MODE_MASK)) {
-				tst_resm(TFAIL, "segment mode is not "
-					 "correct");
-				continue;
-			}
-			/* if we get here, everything looks good */
-			tst_resm(TPASS, "size, pid & mode are correct");
-		}
-
-		/*
-		 * clean up things in case we are looping
-		 */
-		if (shmctl(shm_id_1, IPC_RMID, NULL) == -1)
-			tst_resm(TBROK | TERRNO, "shmctl(IPC_RMID)");
-		else
-			shm_id_1 = -1;
+	/* do a STAT and check some info */
+	if (shmctl(shm_id_1, IPC_STAT, &buf) == -1) {
+		tst_res(TFAIL | TERRNO,
+				"shmctl(IPC_STAT)");
+		return;
+	}
+	/* check the seqment size */
+	if (buf.shm_segsz != shm_size) {
+		tst_res(TFAIL, "seqment size is not "
+				"correct");
+		return;
+	}
+	/* check the pid of the creator */
+	if (buf.shm_cpid != getpid()) {
+		tst_res(TFAIL, "creator pid is not "
+				"correct");
+		return;
+	}
+	/*
+	 * check the mode of the seqment
+	 * mask out all but the lower 9 bits
+	 */
+	if ((buf.shm_perm.mode & MODE_MASK) !=
+			((SHM_RW) & MODE_MASK)) {
+		tst_res(TFAIL, "segment mode is not "
+				"correct");
+		return;
 	}
-	cleanup();
-	tst_exit();
+
+	/* if we get here, everything looks good */
+	tst_res(TPASS, "size, pid & mode are correct");
 }
 
-void setup(void)
+static void setup(void)
 {
 	long hpage_size;
 
-	tst_require_root();
 	check_hugepage();
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-	tst_tmpdir();
+	if (nr_opt)
+		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
 
 	orig_hugepages = get_sys_tune("nr_hugepages");
 	set_sys_tune("nr_hugepages", hugepages, 1);
-	hpage_size = read_meminfo("Hugepagesize:") * 1024;
+	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 
 	shm_size = hpage_size * hugepages / 2;
 	update_shm_size(&shm_size);
-	shmkey = getipckey(cleanup);
+	shmkey = getipckey();
 
-	TEST_PAUSE;
+	shm_id_1 = shmget(shmkey, shm_size,
+			SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
+	if (shm_id_1 == -1)
+		tst_brk(TBROK | TERRNO, "shmget");
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	rm_shm(shm_id_1);
-
 	set_sys_tune("nr_hugepages", orig_hugepages, 0);
-
-	tst_rmdir();
 }
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.options = options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = test_hugeshmget,
+};
testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget02.c +59 -95

219 lines not on this page · raw message

testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget03.c +41 -75

179 lines not on this page · raw message

testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget05.c +41 -76

194 lines not on this page · raw message

Signature · 1 line
2.9.3
author
#13 lore raw

PATCH v3 13/15 mem/cpuset: convert to new API

In reply to Li Wang
---
 testcases/kernel/mem/cpuset/cpuset01.c | 167 ++++++++++++---------------------
 1 file changed, 60 insertions(+), 107 deletions(-)
testcases/kernel/mem/cpuset/cpuset01.c +60 -107
index e6e3c01..a103df4 100644
@@ -1,4 +1,16 @@
 /*
+ * Copyright (C) 2010-2017  Red Hat, Inc.
+ *
+ * This program is free software;  you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY;  without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+ * the GNU General Public License for more details.
+ *
  * Out Of Memory when changing cpuset's mems on NUMA. There was a
  * problem reported upstream that the allocator may see an empty
  * nodemask when changing cpuset's mems.
@@ -6,55 +18,18 @@
  * http://lkml.org/lkml/2010/5/4/79
  * http://lkml.org/lkml/2010/5/4/80
  * This test is based on the reproducers for the above issue.
- *
- * Copyright (C) 2010  Red Hat, Inc.
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public
- * License as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * Further, this software is distributed without any warranty that it
- * is free of the rightful claim of any third person regarding
- * infringement or the like.  Any license provided herein, whether
- * implied or otherwise, applies only to this software file.  Patent
- * licenses, if any, provided herein do not apply to combinations of
- * this program with other software, or any other product whatsoever.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
  */
+
 #include "config.h"
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <sys/mount.h>
-#include <sys/stat.h>
+#include <stdio.h>
 #include <sys/wait.h>
-#include <ctype.h>
-#include <err.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <math.h>
 #if HAVE_NUMAIF_H
 #include <numaif.h>
 #endif
-#include <signal.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
 
-#include "test.h"
 #include "mem.h"
 #include "numa_helper.h"
 
-char *TCID = "cpuset01";
-int TST_TOTAL = 1;
-
 #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
 	&& HAVE_MPOL_CONSTANTS
 volatile int end;
@@ -62,32 +37,13 @@ static int *nodes;
 static int nnodes;
 static long ncpus;
 
-static void testcpuset(void);
 static void sighandler(int signo LTP_ATTRIBUTE_UNUSED);
 static int mem_hog(void);
 static int mem_hog_cpuset(int ntasks);
 static long count_cpu(void);
 
-int main(int argc, char *argv[])
+static void test_cpuset(void)
 {
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	ncpus = count_cpu();
-	if (get_allowed_nodes_arr(NH_MEMS | NH_CPUS, &nnodes, &nodes) < 0)
-		tst_brkm(TBROK | TERRNO, NULL, "get_allowed_nodes_arr");
-	if (nnodes <= 1)
-		tst_brkm(TCONF, NULL, "requires a NUMA system.");
-
-	setup();
-	testcpuset();
-	cleanup();
-	tst_exit();
-}
-
-static void testcpuset(void)
-{
-	int lc;
 	int child, i, status;
 	unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
 	char mems[BUFSIZ], buf[BUFSIZ];
@@ -96,11 +52,11 @@ static void testcpuset(void)
 	write_cpuset_files(CPATH_NEW, "cpus", buf);
 	read_cpuset_files(CPATH, "mems", mems);
 	write_cpuset_files(CPATH_NEW, "mems", mems);
-	SAFE_FILE_PRINTF(cleanup, CPATH_NEW "/tasks", "%d", getpid());
+	SAFE_FILE_PRINTF(CPATH_NEW "/tasks", "%d", getpid());
 
-	switch (child = fork()) {
+	switch (child = SAFE_FORK()) {
 	case -1:
-		tst_brkm(TBROK | TERRNO, cleanup, "fork");
+		tst_brk(TBROK | TERRNO, "fork");
 	case 0:
 		for (i = 0; i < nnodes; i++) {
 			if (nodes[i] >= MAXNODES)
@@ -108,37 +64,36 @@ static void testcpuset(void)
 			set_node(nmask, nodes[i]);
 		}
 		if (set_mempolicy(MPOL_BIND, nmask, MAXNODES) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "set_mempolicy");
-		exit(mem_hog_cpuset(MAX(ncpus, 1)));
-	}
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-		snprintf(buf, BUFSIZ, "%d", nodes[0]);
-		write_cpuset_files(CPATH_NEW, "mems", buf);
-		snprintf(buf, BUFSIZ, "%d", nodes[1]);
-		write_cpuset_files(CPATH_NEW, "mems", buf);
+			tst_brk(TBROK | TERRNO, "set_mempolicy");
+		exit(mem_hog_cpuset(ncpus > 1 ? ncpus : 1));
 	}
 
+	snprintf(buf, BUFSIZ, "%d", nodes[0]);
+	write_cpuset_files(CPATH_NEW, "mems", buf);
+	snprintf(buf, BUFSIZ, "%d", nodes[1]);
+	write_cpuset_files(CPATH_NEW, "mems", buf);
+
 	if (waitpid(child, &status, WUNTRACED | WCONTINUED) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
-	if (WEXITSTATUS(status) != 0)
-		tst_resm(TFAIL, "child exit status is %d", WEXITSTATUS(status));
+		tst_brk(TBROK | TERRNO, "waitpid");
+	if (WEXITSTATUS(status) != 0) {
+		tst_res(TFAIL, "child exit status is %d", WEXITSTATUS(status));
+		return;
+	}
+
+	tst_res(TPASS, "cpuset test pass");
 }
 
-void setup(void)
+static void setup(void)
 {
-	tst_require_root();
-
-	if (tst_kvercmp(2, 6, 32) < 0)
-		tst_brkm(TCONF, NULL, "2.6.32 or greater kernel required");
-
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
-
 	mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW);
+	ncpus = count_cpu();
+	if (get_allowed_nodes_arr(NH_MEMS | NH_CPUS, &nnodes, &nodes) < 0)
+		tst_brk(TBROK | TERRNO, "get_allowed_nodes_arr");
+	if (nnodes <= 1)
+		tst_brk(TCONF, "requires a NUMA system.");
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	umount_mem(CPATH, CPATH_NEW);
 }
@@ -156,41 +111,34 @@ static int mem_hog(void)
 
 	pagesize = getpagesize();
 	while (!end) {
-		addr = mmap(NULL, pagesize * 10, PROT_READ | PROT_WRITE,
+		addr = SAFE_MMAP(NULL, pagesize * 10, PROT_READ | PROT_WRITE,
 			    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
-		if (addr == MAP_FAILED) {
-			ret = 1;
-			tst_resm(TFAIL | TERRNO, "mmap");
-			break;
-		}
 		memset(addr, 0xF7, pagesize * 10);
-		munmap(addr, pagesize * 10);
+		SAFE_MUNMAP(addr, pagesize * 10);
 	}
 	return ret;
 }
 
 static int mem_hog_cpuset(int ntasks)
 {
-	int i, lc, status, ret = 0;
+	int i, status, ret = 0;
 	struct sigaction sa;
 	pid_t *pids;
 
 	if (ntasks <= 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "ntasks is small.");
+		tst_brk(TBROK | TERRNO, "ntasks is small.");
 	sa.sa_handler = sighandler;
 	if (sigemptyset(&sa.sa_mask) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "sigemptyset");
+		tst_brk(TBROK | TERRNO, "sigemptyset");
 	sa.sa_flags = 0;
 	if (sigaction(SIGUSR1, &sa, NULL) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "sigaction");
+		tst_brk(TBROK | TERRNO, "sigaction");
 
-	pids = malloc(sizeof(pid_t) * ntasks);
-	if (pids == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup, "malloc");
+	pids = SAFE_MALLOC(sizeof(pid_t) * ntasks);
 	for (i = 0; i < ntasks; i++) {
 		switch (pids[i] = fork()) {
 		case -1:
-			tst_resm(TFAIL | TERRNO, "fork %d", pids[i]);
+			tst_res(TFAIL | TERRNO, "fork %d", pids[i]);
 			ret = 1;
 			break;
 		case 0:
@@ -200,22 +148,22 @@ static int mem_hog_cpuset(int ntasks)
 			break;
 		}
 	}
-	for (lc = 0; TEST_LOOPING(lc); lc++) ;
+
 	while (i--) {
 		if (kill(pids[i], SIGUSR1) == -1) {
-			tst_resm(TFAIL | TERRNO, "kill %d", pids[i]);
+			tst_res(TFAIL | TERRNO, "kill %d", pids[i]);
 			ret = 1;
 		}
 	}
 	while (waitpid(-1, &status, WUNTRACED | WCONTINUED) > 0) {
 		if (WIFEXITED(status)) {
 			if (WEXITSTATUS(status) != 0) {
-				tst_resm(TFAIL, "child exit status is %d",
+				tst_res(TFAIL, "child exit status is %d",
 					 WEXITSTATUS(status));
 				ret = 1;
 			}
 		} else if (WIFSIGNALED(status)) {
-			tst_resm(TFAIL, "child caught signal %d",
+			tst_res(TFAIL, "child caught signal %d",
 				 WTERMSIG(status));
 			ret = 1;
 		}
@@ -233,9 +181,14 @@ static long count_cpu(void)
 	return ncpus;
 }
 
+static struct tst_test test = {
+	.needs_root = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = test_cpuset,
+	.min_kver = "2.6.32",
+};
+
 #else /* no NUMA */
-int main(void)
-{
-	tst_brkm(TCONF, NULL, "no NUMA development packages installed.");
-}
+	TST_TEST_TCONF("no NUMA development packages installed.");
 #endif
Signature · 1 line
2.9.3
author
#14 lore raw

PATCH v3 14/15 mem/swapping: convert to new API

In reply to Li Wang
---
 testcases/kernel/mem/swapping/swapping01.c | 105 ++++++++++-------------------
 1 file changed, 37 insertions(+), 68 deletions(-)
testcases/kernel/mem/swapping/swapping01.c +37 -68
index 171edb3..8c8d628 100644
@@ -1,24 +1,15 @@
 /*
- * Copyright (C) 2012  Red Hat, Inc.
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public
- * License as published by the Free Software Foundation.
+ * Copyright (C) 2012-2017  Red Hat, Inc.
  *
- * This program is distributed in the hope that it would be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * This program is free software;  you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
- * Further, this software is distributed without any warranty that it
- * is free of the rightful claim of any third person regarding
- * infringement or the like.  Any license provided herein, whether
- * implied or otherwise, applies only to this software file.  Patent
- * licenses, if any, provided herein do not apply to combinations of
- * this program with other software, or any other product whatsoever.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY;  without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+ * the GNU General Public License for more details.
  */
 /*
  * swapping01 - first time swap use results in heavy swapping
@@ -53,12 +44,8 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include "test.h"
 #include "mem.h"
 
-char *TCID = "swapping01";
-int TST_TOTAL = 1;
-
 /* allow swapping 1 * phy_mem in maximum */
 #define COE_DELTA       1
 /* will try to alloc 1.3 * phy_mem */
@@ -74,54 +61,42 @@ static long mem_over;
 static long mem_over_max;
 static pid_t pid;
 
-int main(int argc, char *argv[])
+static void test_swapping(void)
 {
-	int lc;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
 #if __WORDSIZE == 32
-	tst_brkm(TCONF, NULL, "test is not designed for 32-bit system.");
+	tst_brk(TCONF, "test is not designed for 32-bit system.");
 #endif
 
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		init_meminfo();
+	init_meminfo();
 
-		switch (pid = fork()) {
+	switch (pid = fork()) {
 		case -1:
-			tst_brkm(TBROK | TERRNO, cleanup, "fork");
+			tst_brk(TBROK | TERRNO, "fork");
 		case 0:
 			do_alloc();
 			exit(0);
 		default:
 			check_swapping();
-		}
 	}
-	cleanup();
-	tst_exit();
 }
 
 static void init_meminfo(void)
 {
-	swap_free_init = read_meminfo("SwapFree:");
-	if (FILE_LINES_SCANF(cleanup, "/proc/meminfo", "MemAvailable: %ld",
+	swap_free_init = SAFE_READ_MEMINFO("SwapFree:");
+	if (FILE_LINES_SCANF("/proc/meminfo", "MemAvailable: %ld",
 		&mem_available_init)) {
-		mem_available_init = read_meminfo("MemFree:")
-			+ read_meminfo("Cached:");
+		mem_available_init = SAFE_READ_MEMINFO("MemFree:")
+			+ SAFE_READ_MEMINFO("Cached:");
 	}
 	mem_over = mem_available_init * COE_SLIGHT_OVER;
 	mem_over_max = mem_available_init * COE_DELTA;
 
 	/* at least 10MB available physical memory needed */
 	if (mem_available_init < 10240)
-		tst_brkm(TCONF, cleanup, "Not enough available mem to test.");
+		tst_brk(TCONF, "Not enough available mem to test.");
 
 	if (swap_free_init < mem_over_max)
-		tst_brkm(TCONF, cleanup, "Not enough swap space to test.");
+		tst_brk(TCONF, "Not enough swap space to test.");
 }
 
 static void do_alloc(void)
@@ -129,17 +104,17 @@ static void do_alloc(void)
 	long mem_count;
 	void *s;
 
-	tst_resm(TINFO, "available physical memory: %ld MB",
+	tst_res(TINFO, "available physical memory: %ld MB",
 		mem_available_init / 1024);
 	mem_count = mem_available_init + mem_over;
-	tst_resm(TINFO, "try to allocate: %ld MB", mem_count / 1024);
+	tst_res(TINFO, "try to allocate: %ld MB", mem_count / 1024);
 	s = malloc(mem_count * 1024);
 	if (s == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup, "malloc");
+		tst_brk(TBROK | TERRNO, "malloc");
 	memset(s, 1, mem_count * 1024);
-	tst_resm(TINFO, "memory allocated: %ld MB", mem_count / 1024);
+	tst_res(TINFO, "memory allocated: %ld MB", mem_count / 1024);
 	if (raise(SIGSTOP) == -1)
-		tst_brkm(TBROK | TERRNO, tst_exit, "kill");
+		tst_brk(TBROK | TERRNO, "kill");
 	free(s);
 }
 
@@ -150,44 +125,38 @@ static void check_swapping(void)
 
 	/* wait child stop */
 	if (waitpid(pid, &status, WUNTRACED) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+		tst_brk(TBROK | TERRNO, "waitpid");
 	if (!WIFSTOPPED(status))
-		tst_brkm(TBROK, cleanup, "child was not stopped.");
+		tst_brk(TBROK, "child was not stopped.");
 
 	/* Still occupying memory, loop for a while */
 	i = 0;
 	while (i < 10) {
-		swap_free_now = read_meminfo("SwapFree:");
+		swap_free_now = SAFE_READ_MEMINFO("SwapFree:");
 		sleep(1);
-		if (abs(swap_free_now - read_meminfo("SwapFree:")) < 512)
+		if (abs(swap_free_now - SAFE_READ_MEMINFO("SwapFree:")) < 512)
 			break;
 
 		i++;
 	}
 
-	swap_free_now = read_meminfo("SwapFree:");
+	swap_free_now = SAFE_READ_MEMINFO("SwapFree:");
 	swapped = swap_free_init - swap_free_now;
 	if (swapped > mem_over_max) {
 		kill(pid, SIGCONT);
-		tst_brkm(TFAIL, cleanup, "heavy swapping detected: "
+		tst_brk(TFAIL, "heavy swapping detected: "
 				"%ld MB swapped.", swapped / 1024);
 	}
 
-	tst_resm(TPASS, "no heavy swapping detected, %ld MB swapped.",
+	tst_res(TPASS, "no heavy swapping detected, %ld MB swapped.",
 		 swapped / 1024);
 	kill(pid, SIGCONT);
 	/* wait child exit */
 	if (waitpid(pid, &status, 0) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+		tst_brk(TBROK | TERRNO, "waitpid");
 }
 
-void setup(void)
-{
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-}
-
-void cleanup(void)
-{
-}
+static struct tst_test test = {
+	.needs_root = 1,
+	.test_all = test_swapping,
+};
Signature · 1 line
2.9.3
author
#15 lore raw

PATCH v3 15/15 mem/tunable: convert to new API

In reply to Li Wang
---
 testcases/kernel/mem/tunable/max_map_count.c     | 109 ++++++---------
 testcases/kernel/mem/tunable/min_free_kbytes.c   | 130 ++++++++----------
 testcases/kernel/mem/tunable/overcommit_memory.c | 162 +++++++++--------------
 3 files changed, 152 insertions(+), 249 deletions(-)
testcases/kernel/mem/tunable/max_map_count.c +38 -71
index 75a5098..18642f1 100644
@@ -1,4 +1,18 @@
 /*
+ * Copyright (C) 2012-2017  Red Hat, Inc.
+ *
+ * This program is free software;  you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY;  without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+ * the GNU General Public License for more details.
+ *
+ * Description:
+ *
  * The program is designed to test max_map_count tunable file
  *
  * The kernel Documentation say that:
@@ -24,35 +38,9 @@
  * ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0   [vsyscall]
  *
  * so we ignore this line during /proc/[pid]/maps reading.
- *
- * ********************************************************************
- * Copyright (C) 2012 Red Hat, Inc.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public
- * License as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * Further, this software is distributed without any warranty that it
- * is free of the rightful claim of any third person regarding
- * infringement or the like.  Any license provided herein, whether
- * implied or otherwise, applies only to this software file.  Patent
- * licenses, if any, provided herein do not apply to combinations of
- * this program with other software, or any other product whatsoever.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
- *
- * ********************************************************************
  */
+
 #define _GNU_SOURCE
-#include <sys/types.h>
-#include <sys/mman.h>
 #include <sys/wait.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -60,47 +48,19 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/utsname.h>
-#include "test.h"
 #include "mem.h"
 
 #define MAP_COUNT_DEFAULT	1024
 #define MAX_MAP_COUNT		65536L
 
-char *TCID = "max_map_count";
-int TST_TOTAL = 1;
-
 static long old_max_map_count;
 static long old_overcommit;
 static struct utsname un;
 
-static long count_maps(pid_t pid);
-static void max_map_count_test(void);
-
-int main(int argc, char *argv[])
+static void setup(void)
 {
-	int lc;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-		max_map_count_test();
-	}
-
-	cleanup();
-	tst_exit();
-}
-
-void setup(void)
-{
-	tst_require_root();
-
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
-
 	if (access(PATH_SYSVM "max_map_count", F_OK) != 0)
-		tst_brkm(TBROK | TERRNO, NULL,
+		tst_brk(TBROK | TERRNO,
 			 "Can't support to test max_map_count");
 
 	old_max_map_count = get_sys_tune("max_map_count");
@@ -108,10 +68,10 @@ void setup(void)
 	set_sys_tune("overcommit_memory", 2, 1);
 
 	if (uname(&un) != 0)
-		tst_brkm(TBROK | TERRNO, NULL, "uname error");
+		tst_brk(TBROK | TERRNO, "uname error");
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	set_sys_tune("overcommit_memory", old_overcommit, 0);
 	set_sys_tune("max_map_count", old_max_map_count, 0);
@@ -162,7 +122,7 @@ static long count_maps(pid_t pid)
 	snprintf(buf, BUFSIZ, "/proc/%d/maps", pid);
 	fp = fopen(buf, "r");
 	if (fp == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup, "fopen %s", buf);
+		tst_brk(TBROK | TERRNO, "fopen %s", buf);
 	while (getline(&line, &len, fp) != -1) {
 		/* exclude vdso and vsyscall */
 		if (filter_map(line))
@@ -200,7 +160,7 @@ static void max_map_count_test(void)
 	 *    step 1) will be used first.
 	 * Hope OOM-killer can be more stable oneday.
 	 */
-	memfree = read_meminfo("CommitLimit:") - read_meminfo("Committed_AS:");
+	memfree = SAFE_READ_MEMINFO("CommitLimit:") - SAFE_READ_MEMINFO("Committed_AS:");
 	/* 64 used as a bias to make sure no overflow happen */
 	max_iters = memfree / sysconf(_SC_PAGESIZE) * 1024 - 64;
 	if (max_iters > MAX_MAP_COUNT)
@@ -210,24 +170,22 @@ static void max_map_count_test(void)
 	while (max_maps <= max_iters) {
 		set_sys_tune("max_map_count", max_maps, 1);
 
-		switch (pid = tst_fork()) {
-		case -1:
-			tst_brkm(TBROK | TERRNO, cleanup, "fork");
+		switch (pid = SAFE_FORK()) {
 		case 0:
 			while (mmap(NULL, 1, PROT_READ,
 				    MAP_SHARED | MAP_ANONYMOUS, -1, 0)
 			       != MAP_FAILED) ;
 			if (raise(SIGSTOP) != 0)
-				tst_brkm(TBROK | TERRNO, tst_exit, "raise");
+				tst_brk(TBROK | TERRNO, "raise");
 			exit(0);
 		default:
 			break;
 		}
 		/* wait child done mmap and stop */
 		if (waitpid(pid, &status, WUNTRACED) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+			tst_brk(TBROK | TERRNO, "waitpid");
 		if (!WIFSTOPPED(status))
-			tst_brkm(TBROK, cleanup, "child did not stopped");
+			tst_brk(TBROK, "child did not stopped");
 
 		map_count = count_maps(pid);
 		/* Note max_maps will be exceeded by one for
@@ -236,18 +194,27 @@ static void max_map_count_test(void)
 		 * writing this COMMENT!
 		*/
 		if (map_count == (max_maps + 1))
-			tst_resm(TPASS, "%ld map entries in total "
+			tst_res(TPASS, "%ld map entries in total "
 				 "as expected.", max_maps);
 		else
-			tst_resm(TFAIL, "%ld map entries in total, but "
+			tst_res(TFAIL, "%ld map entries in total, but "
 				 "expected %ld entries", map_count, max_maps);
 
 		/* make child continue to exit */
 		if (kill(pid, SIGCONT) != 0)
-			tst_brkm(TBROK | TERRNO, cleanup, "kill");
+			tst_brk(TBROK | TERRNO, "kill");
 		if (waitpid(pid, &status, 0) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+			tst_brk(TBROK | TERRNO, "waitpid");
 
 		max_maps = max_maps << 1;
 	}
 }
+
+static struct tst_test test = {
+	.tid = "max_map_count",
+	.needs_root = 1,
+	.forks_child = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = max_map_count_test,
+};
testcases/kernel/mem/tunable/min_free_kbytes.c +52 -78

261 lines not on this page · raw message

testcases/kernel/mem/tunable/overcommit_memory.c +62 -100

263 lines not on this page · raw message

Signature · 1 line
2.9.3
#16 lore raw

v3 cover Convert LTP mem/testcase to new API

In reply to Li Wang

Hi!
I've pushed this patches, after careful testing and with many changes, thanks.

Most of the changes were more or less cosmetic, such as removing -1 handling after SAFE_FORK(), make use of more SAFE_MACROS() etc.

But there were some problems as well. It introduced a compilation failure on 32bit for min_free_kbytes.c since it removed the total_mem variable that is used only on 32bit.

The ksm06.c cleanup had to be changed, since in a case that merge_across_nodes is missing it attempted to run cleanup (previously there was NULL cleanup pointer for TCONF!) and failed the test. There is probably more unvanted cleanups possibly run for the ksm test, we should have a look at these later.

I've also reverted the change to the thp01 in the runtest file since I belive that the value was reduced only for testing and slipped to the patchset by accident.

I've changed the patch that adds possibility to disable timeouts, mainly so that the API is consistent, which means that the tst_set_timeout() takes -1 as parameter as well + wrote documentation for the change.

Also unsolved is a problem with TCONF tests. At the moment newlib test fails if we pass any parameters to it, since the option parsing runs with the tst_test structure without the option stucture. This fails, for instance, ksm test if they were compiled out since we pass parameters to them in the runtest file. I will have to think about this a bit more and figure out a good solution.

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#17 lore raw

v3 cover Convert LTP mem/testcase to new API

Quoted 3 lines · Cyril Hrubis

Hi!
I've pushed this patches, after careful testing and with many changes, thanks.

Thank you for reviewing so patiently.

Quoted 8 lines

Most of the changes were more or less cosmetic, such as removing -1 handling after SAFE_FORK(), make use of more SAFE_MACROS() etc.

But there were some problems as well. It introduced a compilation failure on 32bit for min_free_kbytes.c since it removed the total_mem variable that is used only on 32bit.

Quoted 5 lines

The ksm06.c cleanup had to be changed, since in a case that merge_across_nodes is missing it attempted to run cleanup (previously there was NULL cleanup pointer for TCONF!) and failed the test. There is probably more unvanted cleanups possibly run for the ksm test, we should have a look at these later.

This worries of cleanup work in ksm06.c is very sensible.

Another situation come to mind is that, if the original value of 'run', 'sleep_millisecs' and 'merge_across_nodes' are all equal 0, but something goes wrong during our test in function test_ksm_merge_across_nodes(nr_pages), and these knobs obviously have been changed to other number, then it attempt to run cleanup() to recovery, but will be failed to restore the values to original, because they are not satisfy the condition(should '> 0' but '== 0') in comparing.

~~~~~~~~~~~~~
static int run = -1;
static int sleep_millisecs = -1;
static int merge_across_nodes = -1;
...

static void cleanup(void)

{
    if (merge_across_nodes > 0) {
        FILE_PRINTF(PATH_KSM "merge_across_nodes",
                "%d", merge_across_nodes);
    }

    if (sleep_millisecs > 0)
        FILE_PRINTF(PATH_KSM "sleep_millisecs", "%d", sleep_millisecs);

    if (run > 0)
        FILE_PRINTF(PATH_KSM "run", "%d", run);

    restore_max_page_sharing();
}

~~~~~~~~~~~~~

My simple patch for the problem is:

$ git diff
testcases/kernel/mem/ksm/ksm06.c +3 -3
index df7cd39..2f5221c 100644
@@ -82,15 +82,15 @@ static void setup(void)
 
 static void cleanup(void)
 {
-       if (merge_across_nodes > 0) {
+       if (merge_across_nodes != -1) {
                FILE_PRINTF(PATH_KSM "merge_across_nodes",
                            "%d", merge_across_nodes);
        }
 
-       if (sleep_millisecs > 0)
+       if (sleep_millisecs != -1)
                FILE_PRINTF(PATH_KSM "sleep_millisecs", "%d", sleep_millisecs);
 
-       if (run > 0)
+       if (run != -1)
                FILE_PRINTF(PATH_KSM "run", "%d", run);
 
        restore_max_page_sharing();

FYI: The default values on my RHEL platform are:

# cat /sys/kernel/mm/ksm/run
0
# cat /sys/kernel/mm/ksm/sleep_millisecs
20
# cat /sys/kernel/mm/ksm/merge_across_nodes
1
Quoted 8 lines

I've also reverted the change to the thp01 in the runtest file since I belive that the value was reduced only for testing and slipped to the patchset by accident.

I've changed the patch that adds possibility to disable timeouts, mainly so that the API is consistent, which means that the tst_set_timeout() takes -1 as parameter as well + wrote documentation for the change.

great, looks better than previous patch.

Quoted 7 lines

Also unsolved is a problem with TCONF tests. At the moment newlib test fails if we pass any parameters to it, since the option parsing runs with the tst_test structure without the option stucture. This fails, for instance, ksm test if they were compiled out since we pass parameters to them in the runtest file. I will have to think about this a bit more and figure out a good solution.

Yes, sounds a little tricky.

Signature · 2 lines
Li Wang
[email protected]