Skip to contentSkip to conversation

Convert LTP kernel/mem/testcase to New API

PATCH Superseded Li Wang 4 participants 25 messages Apr 10, 2017 – Jul 14, 2017 Library code

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Li Wang

author
#0 lore raw

v2 cover Convert LTP kernel/mem/testcase to New API

v1 --> v2

* take use of safe macro of LTP in testcase

* add new SAFE_READ_MEMINFO macro in
mem/lib: convert to new API

* fix OOM timeout issue by new commit:
mem/oom: fix the timeout issue

Signature · 1 line
2.9.3
author
#1 lore raw

PATCH v2 1/14 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 v2 2/14 mem/oom: convert to new API

In reply to Li Wang
---
 testcases/kernel/mem/oom/oom01.c |  83 ++++++++++---------------
 testcases/kernel/mem/oom/oom02.c |  83 +++++++++----------------
 testcases/kernel/mem/oom/oom03.c | 110 +++++++++++++--------------------
 testcases/kernel/mem/oom/oom04.c |  98 +++++++++++------------------
 testcases/kernel/mem/oom/oom05.c | 130 ++++++++++++++++-----------------------
 5 files changed, 196 insertions(+), 308 deletions(-)
testcases/kernel/mem/oom/oom01.c +32 -51
index e39394b..64a368e 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,42 @@
 #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 = {
+	.tid = "oom01",
+	.needs_root = 1,
+	.forks_child = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_oom,
+};
testcases/kernel/mem/oom/oom02.c +30 -53

120 lines not on this page · raw message

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

150 lines not on this page · raw message

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

142 lines not on this page · raw message

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

179 lines not on this page · raw message

Signature · 1 line
2.9.3
author
#3 lore raw

PATCH v2 3/14 mem/oom: fix the timeout issue

In reply to Li Wang

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

In this patch, we divide the tests into two part:

1. OOM takes original way on small(mem_total <= 100G) RAM, and with setting the .timeout according to the worst spending on 100G system. Here gives double(.timeout = 1200) cost in order to make sure time is long enough.

2. If system RAM is larger than 100G, OOM goes to the cgroup limited way.

---
 testcases/kernel/mem/oom/oom01.c | 12 ++++++++++++
 testcases/kernel/mem/oom/oom02.c | 12 ++++++++++++
 testcases/kernel/mem/oom/oom03.c |  1 +
 testcases/kernel/mem/oom/oom04.c | 11 +++++++++++
 testcases/kernel/mem/oom/oom05.c |  1 +
 5 files changed, 37 insertions(+)
testcases/kernel/mem/oom/oom01.c +12 -0
index 64a368e..056e02a 100644
@@ -29,6 +29,8 @@
 #include <unistd.h>
 #include "mem.h"
 
+static unsigned long mem_total;
+
 static void verify_oom(void)
 {
 #if __WORDSIZE == 32
@@ -50,11 +52,20 @@ static void verify_oom(void)
 
 static void setup(void)
 {
+	mem_total = SAFE_READ_MEMINFO("MemTotal:");
+	if (mem_total > TESTMEM/1024*100L) {
+		tst_res(TINFO, "System RAM is larger than 100G, test in memory cgroup");
+		mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
+		write_memcg();
+	}
+
 	overcommit = get_sys_tune("overcommit_memory");
 }
 
 static void cleanup(void)
 {
+	if (mem_total > TESTMEM/1024*100L)
+		umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
 	set_sys_tune("overcommit_memory", overcommit, 0);
 }
 
@@ -62,6 +73,7 @@ static struct tst_test test = {
 	.tid = "oom01",
 	.needs_root = 1,
 	.forks_child = 1,
+	.timeout = 1200,
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = verify_oom,
testcases/kernel/mem/oom/oom02.c +12 -0
index f315ee2..a1be806 100644
@@ -32,6 +32,8 @@
 #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
 	&& HAVE_MPOL_CONSTANTS
 
+static unsigned long mem_total;
+
 static void verify_oom(void)
 {
 #if __WORDSIZE == 32
@@ -50,6 +52,13 @@ static void verify_oom(void)
 
 static void setup(void)
 {
+	mem_total = SAFE_READ_MEMINFO("MemTotal:");
+	if (mem_total > TESTMEM/1024*100L) {
+		tst_res(TINFO, "System RAM is larger than 100G, test in memory cgroup");
+		mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
+		write_memcg();
+	}
+
 	if (!is_numa(NULL, NH_MEMS, 2))
 		tst_brk(TCONF, "The case need a NUMA system.");
 
@@ -59,6 +68,8 @@ static void setup(void)
 
 static void cleanup(void)
 {
+	if (mem_total > TESTMEM/1024*100L)
+		umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
 	set_sys_tune("overcommit_memory", overcommit, 0);
 }
 
@@ -66,6 +77,7 @@ static struct tst_test test = {
 	.tid = "oom02",
 	.needs_root = 1,
 	.forks_child = 1,
+	.timeout = 1200,
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = verify_oom,
testcases/kernel/mem/oom/oom03.c +1 -0
index d3aed60..48c1ea5 100644
@@ -79,6 +79,7 @@ static struct tst_test test = {
 	.tid = "oom03",
 	.needs_root = 1,
 	.forks_child = 1,
+	.timeout = 1200,
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = verify_oom,
testcases/kernel/mem/oom/oom04.c +11 -0
index afe35b2..179a98a 100644
@@ -32,6 +32,8 @@
 #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
 	&& HAVE_MPOL_CONSTANTS
 
+static unsigned long mem_total;
+
 static void verify_oom(void)
 {
 #if __WORDSIZE == 32
@@ -63,6 +65,12 @@ static void setup(void)
 	overcommit = get_sys_tune("overcommit_memory");
 	set_sys_tune("overcommit_memory", 1, 1);
 
+	mem_total = SAFE_READ_MEMINFO("MemTotal:");
+	if (mem_total > TESTMEM/1024*100L) {
+		tst_res(TINFO, "System RAM is larger than 100G, test in memory cgroup");
+		mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
+		write_memcg();
+	}
 	mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW);
 
 	/*
@@ -80,6 +88,8 @@ static void setup(void)
 
 static void cleanup(void)
 {
+	if (mem_total > TESTMEM/1024*100L)
+		umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
 	set_sys_tune("overcommit_memory", overcommit, 0);
 	umount_mem(CPATH, CPATH_NEW);
 }
@@ -88,6 +98,7 @@ static struct tst_test test = {
 	.tid = "oom04",
 	.needs_root = 1,
 	.forks_child = 1,
+	.timeout = 1200,
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = verify_oom,
testcases/kernel/mem/oom/oom05.c +1 -0
index d97fe7d..39361a9 100644
@@ -116,6 +116,7 @@ static struct tst_test test = {
 	.tid = "oom05",
 	.needs_root = 1,
 	.forks_child = 1,
+	.timeout = 1200,
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = verify_oom,
Signature · 1 line
2.9.3
#4 lore raw

PATCH v2 3/14 mem/oom: fix the timeout issue

Hi!

Quoted 10 lines

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

In this patch, we divide the tests into two part:

1. OOM takes original way on small(mem_total <= 100G) RAM, and with setting the .timeout according to the worst spending on 100G system. Here gives double(.timeout = 1200) cost in order to make sure time is long enough.

2. If system RAM is larger than 100G, OOM goes to the cgroup limited way.

I wonder if it's really OK to limit these tests to 100G of RAM. Since as it is these tests can really stress the system on low memory conditions, if we limit them to run inside of a cgroup we are testing cgroup implementation instead, which is we should probably do as well.

I'm starting to think that OOM tests are special enough so that we can 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.

Jan do you have anything to add?

Quoted 168 lines
---
 testcases/kernel/mem/oom/oom01.c | 12 ++++++++++++
 testcases/kernel/mem/oom/oom02.c | 12 ++++++++++++
 testcases/kernel/mem/oom/oom03.c |  1 +
 testcases/kernel/mem/oom/oom04.c | 11 +++++++++++
 testcases/kernel/mem/oom/oom05.c |  1 +
 5 files changed, 37 insertions(+)
testcases/kernel/mem/oom/oom01.c +12 -0
index 64a368e..056e02a 100644
@@ -29,6 +29,8 @@
 #include <unistd.h>
 #include "mem.h"
 
+static unsigned long mem_total;
+
 static void verify_oom(void)
 {
 #if __WORDSIZE == 32
@@ -50,11 +52,20 @@ static void verify_oom(void)
 
 static void setup(void)
 {
+	mem_total = SAFE_READ_MEMINFO("MemTotal:");
+	if (mem_total > TESTMEM/1024*100L) {
+		tst_res(TINFO, "System RAM is larger than 100G, test in memory cgroup");
+		mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
+		write_memcg();
+	}
+
 	overcommit = get_sys_tune("overcommit_memory");
 }
 
 static void cleanup(void)
 {
+	if (mem_total > TESTMEM/1024*100L)
+		umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
 	set_sys_tune("overcommit_memory", overcommit, 0);
 }
 
@@ -62,6 +73,7 @@ static struct tst_test test = {
 	.tid = "oom01",
 	.needs_root = 1,
 	.forks_child = 1,
+	.timeout = 1200,
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = verify_oom,
testcases/kernel/mem/oom/oom02.c +12 -0
index f315ee2..a1be806 100644
@@ -32,6 +32,8 @@
 #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
 	&& HAVE_MPOL_CONSTANTS
 
+static unsigned long mem_total;
+
 static void verify_oom(void)
 {
 #if __WORDSIZE == 32
@@ -50,6 +52,13 @@ static void verify_oom(void)
 
 static void setup(void)
 {
+	mem_total = SAFE_READ_MEMINFO("MemTotal:");
+	if (mem_total > TESTMEM/1024*100L) {
+		tst_res(TINFO, "System RAM is larger than 100G, test in memory cgroup");
+		mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
+		write_memcg();
+	}
+
 	if (!is_numa(NULL, NH_MEMS, 2))
 		tst_brk(TCONF, "The case need a NUMA system.");
 
@@ -59,6 +68,8 @@ static void setup(void)
 
 static void cleanup(void)
 {
+	if (mem_total > TESTMEM/1024*100L)
+		umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
 	set_sys_tune("overcommit_memory", overcommit, 0);
 }
 
@@ -66,6 +77,7 @@ static struct tst_test test = {
 	.tid = "oom02",
 	.needs_root = 1,
 	.forks_child = 1,
+	.timeout = 1200,
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = verify_oom,
testcases/kernel/mem/oom/oom03.c +1 -0
index d3aed60..48c1ea5 100644
@@ -79,6 +79,7 @@ static struct tst_test test = {
 	.tid = "oom03",
 	.needs_root = 1,
 	.forks_child = 1,
+	.timeout = 1200,
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = verify_oom,
testcases/kernel/mem/oom/oom04.c +11 -0
index afe35b2..179a98a 100644
@@ -32,6 +32,8 @@
 #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
 	&& HAVE_MPOL_CONSTANTS
 
+static unsigned long mem_total;
+
 static void verify_oom(void)
 {
 #if __WORDSIZE == 32
@@ -63,6 +65,12 @@ static void setup(void)
 	overcommit = get_sys_tune("overcommit_memory");
 	set_sys_tune("overcommit_memory", 1, 1);
 
+	mem_total = SAFE_READ_MEMINFO("MemTotal:");
+	if (mem_total > TESTMEM/1024*100L) {
+		tst_res(TINFO, "System RAM is larger than 100G, test in memory cgroup");
+		mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
+		write_memcg();
+	}
 	mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW);
 
 	/*
@@ -80,6 +88,8 @@ static void setup(void)
 
 static void cleanup(void)
 {
+	if (mem_total > TESTMEM/1024*100L)
+		umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
 	set_sys_tune("overcommit_memory", overcommit, 0);
 	umount_mem(CPATH, CPATH_NEW);
 }
@@ -88,6 +98,7 @@ static struct tst_test test = {
 	.tid = "oom04",
 	.needs_root = 1,
 	.forks_child = 1,
+	.timeout = 1200,
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = verify_oom,
testcases/kernel/mem/oom/oom05.c +1 -0
index d97fe7d..39361a9 100644
@@ -116,6 +116,7 @@ static struct tst_test test = {
 	.tid = "oom05",
 	.needs_root = 1,
 	.forks_child = 1,
+	.timeout = 1200,
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = verify_oom,
Signature · 5 lines
2.9.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis
[email protected]
author
#5 lore raw

PATCH v2 3/14 mem/oom: fix the timeout issue

Quoted 10 lines · Cyril Hrubis

Hi!

Quoted 9 lines

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

In this patch, we divide the tests into two part:

1. OOM takes original way on small(mem_total <= 100G) RAM, and with setting the .timeout according to the worst spending on 100G system. Here gives double(.timeout = 1200) cost in order to make sure time is long enough.

Quoted 6 lines
Quoted 1 line

2. If system RAM is larger than 100G, OOM goes to the cgroup limited way.

I wonder if it's really OK to limit these tests to 100G of RAM. Since as it is these tests can really stress the system on low memory conditions, if we limit them to run inside of a cgroup we are testing cgroup implementation instead, which is we should probably do as well.

Sounds reasonable.

Quoted 5 lines

I'm starting to think that OOM tests are special enough so that we can 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.

Have to say this idea is better. it keeps the tests original and simple enough.

I draft a patch for it:
-------------------------

lib/tst_test.c +0 -0
index 16ea64f..94d951b 100644
@@ -917,10 +917,12 @@ 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)
- tst_set_timeout(tst_test->timeout);
- else
+ if (tst_test->timeout) {
+ if (tst_test->timeout > 0)
+ tst_set_timeout(tst_test->timeout);

+       } else {
                tst_set_timeout(300);

+ }

        SAFE_SIGNAL(SIGINT, sigint_handler);
testcases/kernel/mem/oom/oom01.c +1 -0
index 64a368e..9bc5e57 100644
@@ -62,6 +62,7 @@ static struct tst_test test = {
        .tid = "oom01",
        .needs_root = 1,
        .forks_child = 1,
+       .timeout = -1,
        .setup = setup,
        .cleanup = cleanup,
        .test_all = verify_oom,
Quoted 2 lines

Jan do you have anything to add?

Signature · 2 lines
Li Wang
[email protected]
#6 lore raw

PATCH v2 3/14 mem/oom: fix the timeout issue

Review

Quoted 6 lines · Li Wang
(quoted diff) +5 -0
+       if (tst_test->timeout) {
+               if (tst_test->timeout > 0)
+                       tst_set_timeout(tst_test->timeout);
+       } else {
                tst_set_timeout(300);
+       }

Only a minor nit, but it is easier to read without nesting, so we could have:

if (tst_test->timeout > 0) {
tst_set_timeout(tst_test->timeout);
} else if (tst_test->timeout == 0) {
tst_set_timeout(300);
}

Signature · 2 lines
Thank you,
Richard.
author
#7 lore raw

PATCH v2 3/14 mem/oom: fix the timeout issue

Quoted 10 lines · Richard Palethorpe

Review

Quoted 6 lines · Li Wang
(quoted diff) +5 -0
+       if (tst_test->timeout) {
+               if (tst_test->timeout > 0)
+                       tst_set_timeout(tst_test->timeout);
+       } else {
                tst_set_timeout(300);
+       }
Quoted 7 lines

Only a minor nit, but it is easier to read without nesting, so we could have:

if (tst_test->timeout > 0) {
   tst_set_timeout(tst_test->timeout);
} else if (tst_test->timeout == 0) {

tst_set_timeout(300);
}

Yes, seems the brace is also redundant.

Signature · 2 lines
Li Wang
[email protected]
#8 lore raw

PATCH v2 3/14 mem/oom: fix the timeout issue

In reply to Cyril Hrubis

----- Original Message -----

Quoted 18 lines

Hi!

Quoted 11 lines

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

In this patch, we divide the tests into two part:

1. OOM takes original way on small(mem_total <= 100G) RAM, and with setting the .timeout according to the worst spending on 100G system. Here gives double(.timeout = 1200) cost in order to make sure time is long enough.

2. If system RAM is larger than 100G, OOM goes to the cgroup limited way.

I wonder if it's really OK to limit these tests to 100G of RAM. Since as it is these tests can really stress the system on low memory conditions, if we limit them to run inside of a cgroup we are testing cgroup implementation instead, which is we should probably do as well.

Quoted 6 lines

I'm starting to think that OOM tests are special enough so that we can 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.

Jan do you have anything to add?

I'm OK with timeout -1.

I'm thinking if we should introduce a parameter to TCONF on large RAM systems anyway and split mm runtest file into "fast" and "stress". The "fast" one would contain tests that can complete in ~minutes (including oom tests with parameter) and then "stress" one would be for all those slow that can take several hours (oom tests with no parameter) to complete.

Regards,
Jan

author
#9 lore raw

PATCH v2 4/14 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
#10 lore raw

PATCH v2 4/14 mem/ksm: convert to new API

Hi!

testcases/kernel/mem/ksm/ksm01.c line 74

Quoted 10 lines
testcases/kernel/mem/ksm/ksm01.c +1 -9
-	tst_parse_opts(argc, argv, ksm_options, ksm_usage);
-	setup();
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-		check_ksm_options(&size, &num, &unit);
-		create_same_memory(size, num, unit);
-	}
-	cleanup();
-	tst_exit();
+	check_ksm_options(&size, &num, &unit);

There is no need to parse the test options on every test iteration, this should be called once in the test setup.

Moreover passing things around in global variables is messy. What about we (ideally in a separate patch) created a function(s) to parse the KSM options and pass the string values explicitily.

setup(void)

{
	...
	parse_ksm_options(str_size, &size, str_num, &num, str_unit, &unit);
	...
}

Also could we define the ksm_options array in one place instead of repeating it in each test. What about putting it into ksh_common.h in the mem/ksm/ directory and including it in each test?

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

PATCH v2 4/14 mem/ksm: convert to new API

Hi!

Quoted 14 lines

testcases/kernel/mem/ksm/ksm01.c line 74

Quoted 10 lines
testcases/kernel/mem/ksm/ksm01.c +1 -9
-	tst_parse_opts(argc, argv, ksm_options, ksm_usage);
-	setup();
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-		check_ksm_options(&size, &num, &unit);
-		create_same_memory(size, num, unit);
-	}
-	cleanup();
-	tst_exit();
+	check_ksm_options(&size, &num, &unit);

There is no need to parse the test options on every test iteration, this should be called once in the test setup.

Quoted 3 lines

Moreover passing things around in global variables is messy. What about we (ideally in a separate patch) created a function(s) to parse the KSM options and pass the string values explicitily.

Or since we are about to create ksm_common.h anyway, we can add static inline functions to parse the test options there as well. And FYI we do have option parsing helpers in the tst_test.h library that are much better than plain old atoi().

Quoted 17 lines

setup(void)

{
	...
	parse_ksm_options(str_size, &size, str_num, &num, str_unit, &unit);
	...
}

Also could we define the ksm_options array in one place instead of repeating it in each test. What about putting it into ksh_common.h in the mem/ksm/ directory and including it in each test?

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis
[email protected]
author
#12 lore raw

PATCH v2 4/14 mem/ksm: convert to new API

In reply to Cyril Hrubis
Quoted 26 lines · Cyril Hrubis

Hi!

testcases/kernel/mem/ksm/ksm01.c line 74

Quoted 10 lines
testcases/kernel/mem/ksm/ksm01.c +1 -9
-     tst_parse_opts(argc, argv, ksm_options, ksm_usage);
-     setup();
-     for (lc = 0; TEST_LOOPING(lc); lc++) {
-             tst_count = 0;
-             check_ksm_options(&size, &num, &unit);
-             create_same_memory(size, num, unit);
-     }
-     cleanup();
-     tst_exit();
+     check_ksm_options(&size, &num, &unit);

There is no need to parse the test options on every test iteration, this should be called once in the test setup.

Moreover passing things around in global variables is messy. What about we (ideally in a separate patch) created a function(s) to parse the KSM options and pass the string values explicitily.

setup(void)

{
        ...
        parse_ksm_options(str_size, &size, str_num, &num, str_unit, &unit);
        ...
}
Quoted 3 lines

Also could we define the ksm_options array in one place instead of repeating it in each test. What about putting it into ksh_common.h in the mem/ksm/ directory and including it in each test?

No problem. Agree with all these comments.

Let me move the common code to ksm_common.h in a new PATCH.

Signature · 2 lines
Li Wang
[email protected]
author
#13 lore raw

PATCH v2 5/14 mem/thp: convert to new API

In reply to Li Wang
---
 runtest/mm                       |  2 +-
 testcases/kernel/mem/thp/thp01.c | 85 +++++++++++++++----------------------
 testcases/kernel/mem/thp/thp02.c | 89 ++++++++++++--------------------------
 testcases/kernel/mem/thp/thp03.c | 92 +++++++++++++---------------------------
 4 files changed, 92 insertions(+), 176 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 +34 -51

127 lines not on this page · raw message

testcases/kernel/mem/thp/thp02.c +27 -62

135 lines not on this page · raw message

testcases/kernel/mem/thp/thp03.c +30 -62

142 lines not on this page · raw message

Signature · 1 line
2.9.3
#14 lore raw

PATCH v2 5/14 mem/thp: convert to new API

Hi!

Quoted 6 lines
testcases/kernel/mem/thp/thp01.c +1 -0
+	switch (pid = SAFE_FORK()) {
 		case 0:
 			memset(arg, 'c', length - 1);
 			arg[length - 1] = '\0';
@@ -91,29 +72,31 @@ int main(int argc, char **argv)
 			}

testcases/kernel/mem/thp/thp01.c int main(int argc, char **argv) · line 78

Quoted 10 lines
testcases/kernel/mem/thp/thp01.c +2 -4
 		default:
 			if (waitpid(pid, &st, 0) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+				tst_brk(TBROK | TERRNO, "waitpid");
 			if (!WIFEXITED(st) || WEXITSTATUS(st) != 0)
-				tst_brkm(TBROK, cleanup,
-					 "child exited abnormally");
-		}
+				tst_brk(TBROK, "child exited abnormally");
 	}

We do have SAFE_WAITPID() as well, but in this case we can simply call tst_reap_children() that would take care of the exit status as well.

Quoted 110 lines
testcases/kernel/mem/thp/thp01.c +13 -9
-	tst_resm(TPASS, "system didn't crash, pass.");
-	cleanup();
-	tst_exit();
+
+	tst_res(TPASS, "system didn't crash, pass.");
 }
 
 static void setup(void)
 {
 	ps = sysconf(_SC_PAGESIZE);
 	length = 32 * ps;
-	arg = malloc(length);
-	if (arg == NULL)
-		tst_brkm(TBROK | TERRNO, NULL, "malloc");
-
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
+	arg = SAFE_MALLOC(length);
 }
 
 static void cleanup(void)
 {
+	free(arg);
 }
+
+static struct tst_test test = {
+	.tid = "thp01",
+	.needs_root = 1,
+	.forks_child = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = thp_test,
+};
testcases/kernel/mem/thp/thp02.c +9 -43
index eb919aa..45cfbe5 100644
@@ -1,24 +1,15 @@
 /*
- * Copyright (C) 2011  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) 2011-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.
  *
  * thp02 - detect mremap bug when THP is enabled.
  *
@@ -52,36 +43,11 @@
 #include <unistd.h>
 #include "mem.h"
 
-char *TCID = "thp02";
-int TST_TOTAL = 1;
-
 #ifdef HAVE_MREMAP_FIXED
 static int ps;
 static long hps, size;
 static void *p, *p2, *p3, *p4;
 
-static void do_mremap(void);
-
-int main(int argc, char **argv)
-{
-	int lc;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		do_mremap();
-	}
-	tst_resm(TPASS, "Still alive.");
-
-	cleanup();
-	tst_exit();
-
-}
-
 static void do_mremap(void)
 {
 	int i;
@@ -89,11 +55,11 @@ static void do_mremap(void)
 

testcases/kernel/mem/thp/thp02.c static void do_mremap(void) · line 62

Quoted 10 lines
testcases/kernel/mem/thp/thp02.c +3 -3
 	for (i = 0; i < 4; i++) {
 		if (posix_memalign(&p, hps, size))
-			tst_brkm(TBROK | TERRNO, cleanup, "memalign p");
+			tst_brk(TBROK | TERRNO, "memalign p");
 		if (posix_memalign(&p2, hps, size))
-			tst_brkm(TBROK | TERRNO, cleanup, "memalign p2");
+			tst_brk(TBROK | TERRNO, "memalign p2");
 		if (posix_memalign(&p3, hps, size))
-			tst_brkm(TBROK | TERRNO, cleanup, "memalign p3");
+			tst_brk(TBROK | TERRNO, "memalign p3");

The posix_memalign() returns errno hence this will print bogus values with TERRNO. We can either use SAFE_MEMALIGN() or possibly add POSIX_MEMALIGN() to the safe_macros and use that one instead.

Quoted 131 lines
testcases/kernel/mem/thp/thp02.c +15 -16
 		memset(p, 0xff, size);
 		memset(p2, 0xff, size);
@@ -108,37 +74,36 @@ static void do_mremap(void)
 		 */
 		old_addr = p + ps * (i >> 1);
 		new_addr = p3 + ps * (i & 1);
-		tst_resm(TINFO, "mremap %p to %p", old_addr, new_addr);
+		tst_res(TINFO, "mremap %p to %p", old_addr, new_addr);
 
 		p4 = mremap(old_addr, size - ps, size - ps,
 			    MREMAP_FIXED | MREMAP_MAYMOVE, new_addr);
 		if (p4 == MAP_FAILED)
-			tst_brkm(TBROK | TERRNO, cleanup, "mremap");
+			tst_brk(TBROK | TERRNO, "mremap");
 		if (memcmp(p4, p2, size - ps))
-			tst_brkm(TBROK, cleanup, "mremap bug");
+			tst_brk(TBROK, "mremap bug");
 	}
+
+	tst_res(TPASS, "Still alive.");
 }
 
-void setup(void)
+static void setup(void)
 {
 	if (access(PATH_THP, F_OK) == -1)
-		tst_brkm(TCONF, NULL, "THP not enabled in kernel?");
-
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
+		tst_brk(TCONF, "THP not enabled in kernel?");
 
 	ps = sysconf(_SC_PAGESIZE);
-	hps = read_meminfo("Hugepagesize:") * 1024;
+	hps = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 	size = hps * 4;
 }
 
-void cleanup(void)
-{
-}
+static struct tst_test test = {
+	.tid = "thp02",
+	.needs_root = 1,
+	.setup = setup,
+	.test_all = do_mremap,
+};
 
 #else
-int main(void)
-{
-	tst_brkm(TCONF, NULL, "MREMAP_FIXED not present in <sys/mman.h>");
-}
+	TST_TEST_TCONF("MREMAP_FIXED not present in <sys/mman.h>");
 #endif /* HAVE_MREMAP_FIXED */
testcases/kernel/mem/thp/thp03.c +9 -41
index 6b0f829..44541d9 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.
  *
  * thp03 - Case for spliting unaligned memory.
  *       - System will panic if failed.
@@ -46,11 +37,6 @@
 #include <string.h>
 #include <errno.h>
 #include "mem.h"
-#include "safe_macros.h"
-#include "test.h"
-
-char *TCID = "thp03";
-int TST_TOTAL = 1;
 
 #ifdef MADV_MERGEABLE
 
@@ -60,24 +46,6 @@ static long hugepage_size;
 static long unaligned_size;
 static long page_size;
 
-int main(int argc, char **argv)
-{
-	int lc;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		thp_test();
-	}
-	tst_resm(TPASS, "system didn't crash, pass.");
-	cleanup();
-	tst_exit();
-}
-
 static void thp_test(void)
 {
 	void *p;

testcases/kernel/mem/thp/thp03.c static void thp_test(void) · line 56

Quoted 5 lines
testcases/kernel/mem/thp/thp03.c +1 -1
@@ -85,53 +53,53 @@ static void thp_test(void)
 	p = mmap(NULL, unaligned_size, PROT_READ | PROT_WRITE,
 		 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
 	if (p == MAP_FAILED)
-		tst_brkm(TBROK | TERRNO, cleanup, "mmap");
+		tst_brk(TBROK | TERRNO, "mmap");

Use SAFE_MMAP() here instead.

Quoted 16 lines
testcases/kernel/mem/thp/thp03.c +3 -3
 	memset(p, 0x00, unaligned_size);
 	if (mprotect(p, unaligned_size, PROT_NONE) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "mprotect");
+		tst_brk(TBROK | TERRNO, "mprotect");
 
 	if (madvise(p + hugepage_size, page_size, MADV_MERGEABLE) == -1) {
 		if (errno == EINVAL) {
-			tst_brkm(TCONF, cleanup,
+			tst_brk(TCONF,
 			         "MADV_MERGEABLE is not enabled/supported");
 		} else {
-			tst_brkm(TBROK | TERRNO, cleanup, "madvise");
+			tst_brk(TBROK | TERRNO, "madvise");
 		}
 	}
 

testcases/kernel/mem/thp/thp03.c static void thp_test(void) · line 76

Quoted 10 lines
testcases/kernel/mem/thp/thp03.c +2 -4
-	switch (fork()) {
-	case -1:
-		tst_brkm(TBROK | TERRNO, cleanup, "fork");
+	switch (SAFE_FORK()) {
 	case 0:
 		exit(0);
 	default:
 		if (waitpid(-1, NULL, 0) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+			tst_brk(TBROK | TERRNO, "waitpid");

And SAFE_WAITPID() here.

Quoted 30 lines
testcases/kernel/mem/thp/thp03.c +11 -10
 	}
+
+	tst_res(TPASS, "system didn't crash, pass.");
+	munmap(p, unaligned_size);
 }
 
-void setup(void)
+static void setup(void)
 {
 	if (access(PATH_THP, F_OK) == -1)
-		tst_brkm(TCONF, NULL, "THP not enabled in kernel?");
+		tst_brk(TCONF, "THP not enabled in kernel?");
 
-	hugepage_size = read_meminfo("Hugepagesize:") * KB;
+	hugepage_size = SAFE_READ_MEMINFO("Hugepagesize:") * KB;
 	unaligned_size = hugepage_size * 4 - 1;
-	page_size = SAFE_SYSCONF(NULL, _SC_PAGESIZE);
-
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	TEST_PAUSE;
+	page_size = SAFE_SYSCONF(_SC_PAGESIZE);
 }
 
-void cleanup(void)
-{
-}
+static struct tst_test test = {
+	.tid = "thp03",
+	.needs_root = 1,
+	.forks_child = 1,

testcases/kernel/mem/thp/thp03.c static void thp_test(void) · line 103

Quoted 10 lines
testcases/kernel/mem/thp/thp03.c +4 -3
+	.setup = setup,
+	.test_all = thp_test,
+};
 
 #else
-int main(void)
-{
-	tst_brkm(TCONF, NULL, "Kernel doesn't support MADV_MERGEABLE"
+	TST_TEST_TCONF("Kernel doesn't support MADV_MERGEABLE"
 		 " or you need to update your glibc-headers");

Hmm, if we get here the problem was MADV_MERGEABLE missing in system headers. The kernel part is checked at runtime (that is the EINVAL check from madvise()).

And we have lapi/mmap.h with fallback definition for it, so what about we get rid of the ifdefs and include the fallback header lapi/mmap.h instead?

Quoted 2 lines
testcases/kernel/mem/thp/thp03.c +0 -0
 }
 #endif
Signature · 2 lines
Cyril Hrubis
[email protected]
author
#15 lore raw

PATCH v2 5/14 mem/thp: convert to new API

Quoted 12 lines · Cyril Hrubis

Hi!

Quoted 11 lines
testcases/kernel/mem/thp/thp01.c +2 -1
+     switch (pid = SAFE_FORK()) {
              case 0:
                      memset(arg, 'c', length - 1);
                      arg[length - 1] = '\0';
@@ -91,29 +72,31 @@ int main(int argc, char **argv)
                      }
              default:
                      if (waitpid(pid, &st, 0) == -1)
-                             tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+                             tst_brk(TBROK | TERRNO, "waitpid");
                      if (!WIFEXITED(st) || WEXITSTATUS(st) != 0)
Quoted 8 lines

testcases/kernel/mem/thp/thp01.c int main(int argc, char **argv) · line 78

Quoted 5 lines
testcases/kernel/mem/thp/thp01.c +1 -3
-                             tst_brkm(TBROK, cleanup,
-                                      "child exited abnormally");
-             }
+                             tst_brk(TBROK, "child exited abnormally");
      }

We do have SAFE_WAITPID() as well, but in this case we can simply call tst_reap_children() that would take care of the exit status as well.

Great function.

Quoted 11 lines

testcases/kernel/mem/thp/thp02.c static void do_mremap(void) · line 62

Quoted 10 lines
testcases/kernel/mem/thp/thp02.c +3 -3
      for (i = 0; i < 4; i++) {
              if (posix_memalign(&p, hps, size))
-                     tst_brkm(TBROK | TERRNO, cleanup, "memalign p");
+                     tst_brk(TBROK | TERRNO, "memalign p");
              if (posix_memalign(&p2, hps, size))
-                     tst_brkm(TBROK | TERRNO, cleanup, "memalign p2");
+                     tst_brk(TBROK | TERRNO, "memalign p2");
              if (posix_memalign(&p3, hps, size))
-                     tst_brkm(TBROK | TERRNO, cleanup, "memalign p3");
+                     tst_brk(TBROK | TERRNO, "memalign p3");
Quoted 3 lines

The posix_memalign() returns errno hence this will print bogus values with TERRNO. We can either use SAFE_MEMALIGN() or possibly add POSIX_MEMALIGN() to the safe_macros and use that one instead.

Sure, I choose the obsolete one here.
p = SAFE_MEMALIGN(hps, size);

Quoted 11 lines

testcases/kernel/mem/thp/thp03.c static void thp_test(void) · line 103

Quoted 6 lines
testcases/kernel/mem/thp/thp03.c +1 -3
 #else
-int main(void)
-{
-     tst_brkm(TCONF, NULL, "Kernel doesn't support MADV_MERGEABLE"
+     TST_TEST_TCONF("Kernel doesn't support MADV_MERGEABLE"
               " or you need to update your glibc-headers");

Hmm, if we get here the problem was MADV_MERGEABLE missing in system headers. The kernel part is checked at runtime (that is the EINVAL check from madvise()).

Quoted 3 lines

And we have lapi/mmap.h with fallback definition for it, so what about we get rid of the ifdefs and include the fallback header lapi/mmap.h instead?

Good suggestion. And no objection to the rest comments.

Thanks for reviewing patiently.

Signature · 2 lines
Li Wang
[email protected]
author
#16 lore raw

PATCH v2 6/14 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
#17 lore raw

PATCH v2 7/14 mem/hugemmap: convert to new API

In reply to Li Wang
---
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap01.c | 182 +++++--------
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap02.c | 244 +++++++----------
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap04.c | 192 +++++---------
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap05.c | 291 ++++++++-------------
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap06.c |  95 +++----
 testcases/kernel/mem/hugetlb/lib/hugetlb.h         |   2 +
 6 files changed, 369 insertions(+), 637 deletions(-)
testcases/kernel/mem/hugetlb/hugemmap/hugemmap01.c +62 -120
index da8bdb1..4e629f0 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,143 +23,86 @@
  *  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 "test.h"
-#include "hugetlb.h"
-#include "safe_macros.h"
+#include <sys/mount.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());
 }
 
@@ -173,11 +112,14 @@ 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 = {
+	.tid = "hugemmap01",
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.options = options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = test_hugemmap,
+};
testcases/kernel/mem/hugetlb/hugemmap/hugemmap02.c +98 -146

299 lines not on this page · raw message

testcases/kernel/mem/hugetlb/hugemmap/hugemmap04.c +67 -125

252 lines not on this page · raw message

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

508 lines not on this page · raw message

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

173 lines not on this page · raw message

testcases/kernel/mem/hugetlb/lib/hugetlb.h +2 -0
index 1b5cc73..6f5cbb7 100644
@@ -27,9 +27,11 @@
 #include <stdlib.h>
 #include <sys/ipc.h>
 #include <sys/shm.h>
+#include <sys/param.h>
 #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
#18 lore raw

PATCH v2 8/14 mem/hugeshmat: convert to new API

In reply to Li Wang
---
 .../kernel/mem/hugetlb/hugeshmat/hugeshmat01.c     | 179 +++++++++------------
 .../kernel/mem/hugetlb/hugeshmat/hugeshmat02.c     | 133 ++++++---------
 .../kernel/mem/hugetlb/hugeshmat/hugeshmat03.c     | 132 ++++++---------
 .../kernel/mem/hugetlb/hugeshmat/hugeshmat04.c     | 118 +++++---------
 .../kernel/mem/hugetlb/hugeshmat/hugeshmat05.c     |  70 +++-----
 5 files changed, 239 insertions(+), 393 deletions(-)
testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat01.c +72 -107
index cfb4d72..b892d57 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,107 +32,73 @@
  *			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 "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;
@@ -149,17 +108,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;
 	}
 
@@ -182,9 +141,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;
 		}
@@ -196,43 +155,49 @@ 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 = {
+	.tid = "hugeshmat01",
+	.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 +52 -81

194 lines not on this page · raw message

testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat03.c +48 -84

193 lines not on this page · raw message

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

181 lines not on this page · raw message

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

130 lines not on this page · raw message

Signature · 1 line
2.9.3
author
#19 lore raw

PATCH v2 9/14 mem/hugeshmctl: convert to new API

In reply to Li Wang
---
 .../kernel/mem/hugetlb/hugeshmctl/hugeshmctl01.c   | 211 +++++++++------------
 .../kernel/mem/hugetlb/hugeshmctl/hugeshmctl02.c   | 153 ++++++---------
 .../kernel/mem/hugetlb/hugeshmctl/hugeshmctl03.c   | 152 ++++++---------
 3 files changed, 210 insertions(+), 306 deletions(-)
testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl01.c +92 -119
index a54077a..17ef0b0 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,31 +36,14 @@
  *	    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 "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
@@ -92,67 +67,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();
 }
 
 /*
@@ -166,7 +128,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;
 }
@@ -182,7 +144,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
@@ -210,12 +180,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;
 
@@ -229,19 +197,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:
@@ -252,7 +220,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);
 }
@@ -270,12 +238,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;
 	}
 
@@ -286,18 +254,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:
@@ -319,15 +287,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()
  */
@@ -347,21 +321,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");
 }
 
 /*
@@ -371,48 +345,47 @@ 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 = {
+	.tid = "hugeshmctl01",
+	.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 +59 -94

218 lines not on this page · raw message

testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl03.c +59 -93

230 lines not on this page · raw message

Signature · 1 line
2.9.3
author
#20 lore raw

PATCH v2 10/14 mm/hugeshmdt: convert to new API

In reply to Li Wang
---
 .../kernel/mem/hugetlb/hugeshmdt/hugeshmdt01.c     | 124 ++++++++-------------
 1 file changed, 47 insertions(+), 77 deletions(-)
testcases/kernel/mem/hugetlb/hugeshmdt/hugeshmdt01.c +47 -77
index 0ebcca6..dc16b9c 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,29 +34,14 @@
  *			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 "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;
@@ -73,54 +51,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);
+	struct sigaction sa;
 
-	setup();
+	sa.sa_handler = sighandler;
+	sigaction(SIGSEGV, &sa, NULL);
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		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;
 	}
 
@@ -142,9 +109,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");
 }
 
@@ -156,7 +123,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);
 	}
 }
 
@@ -164,41 +131,44 @@ 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 = {
+	.tid = "hugeshmdt01",
+	.needs_root = 1,
+	.options = options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = hugeshmdt_test,
+};
Signature · 1 line
2.9.3
author
#21 lore raw

PATCH v2 11/14 mem/hugeshmget: convert to new API

In reply to Li Wang
---
 .../kernel/mem/hugetlb/hugeshmget/hugeshmget01.c   | 168 ++++++++-------------
 .../kernel/mem/hugetlb/hugeshmget/hugeshmget02.c   | 155 ++++++++-----------
 .../kernel/mem/hugetlb/hugeshmget/hugeshmget03.c   | 118 ++++++---------
 .../kernel/mem/hugetlb/hugeshmget/hugeshmget05.c   | 121 ++++++---------
 4 files changed, 206 insertions(+), 356 deletions(-)
testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget01.c +63 -105
index 2367c4d..d1a3dea 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,128 +34,93 @@
  *	  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 "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 = {
+	.tid = "hugeshmget01",
+	.needs_root = 1,
+	.options = options,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = test_hugeshmget,
+};
testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget02.c +59 -96

219 lines not on this page · raw message

testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget03.c +42 -76

179 lines not on this page · raw message

testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget05.c +42 -79

194 lines not on this page · raw message

Signature · 1 line
2.9.3
author
#22 lore raw

PATCH v2 12/14 mem/cpuset: convert to new API

In reply to Li Wang
---
 testcases/kernel/mem/cpuset/cpuset01.c | 155 ++++++++++++---------------------
 1 file changed, 58 insertions(+), 97 deletions(-)
testcases/kernel/mem/cpuset/cpuset01.c +58 -97
index 558420f..80c2112 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[])
-{
-
-	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)
+static void test_cpuset(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()) {
 	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");
+			tst_brk(TBROK | TERRNO, "set_mempolicy");
 		exit(mem_hog_cpuset(ncpus > 1 ? 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);
-	}
+
+	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);
 }
@@ -160,7 +115,7 @@ static int mem_hog(void)
 			    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
 		if (addr == MAP_FAILED) {
 			ret = 1;
-			tst_resm(TFAIL | TERRNO, "mmap");
+			tst_res(TFAIL | TERRNO, "mmap");
 			break;
 		}
 		memset(addr, 0xF7, pagesize * 10);
@@ -171,26 +126,26 @@ static int mem_hog(void)
 
 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");
+		tst_brk(TBROK | TERRNO, "malloc");
 	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 +155,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 +188,15 @@ static long count_cpu(void)
 	return ncpus;
 }
 
+static struct tst_test test = {
+	.tid = "cpuset01",
+	.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
#23 lore raw

PATCH v2 13/14 mem/swapping: convert to new API

In reply to Li Wang
---
 testcases/kernel/mem/swapping/swapping01.c | 106 +++++++++++------------------
 1 file changed, 38 insertions(+), 68 deletions(-)
testcases/kernel/mem/swapping/swapping01.c +38 -68
index 171edb3..9390e8a 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,39 @@ 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 = {
+	.tid = "swapping01",
+	.needs_root = 1,
+	.test_all = test_swapping,
+};
Signature · 1 line
2.9.3
author
#24 lore raw

PATCH v2 14/14 mem/tunable: convert to new API

In reply to Li Wang

Note: Skip min_free_kbytes on large(total_mem > 100G) system, since
.timeout is only set 1200s.

---
 testcases/kernel/mem/tunable/max_map_count.c     | 107 ++++++---------
 testcases/kernel/mem/tunable/min_free_kbytes.c   | 128 ++++++++----------
 testcases/kernel/mem/tunable/overcommit_memory.c | 164 +++++++++--------------
 3 files changed, 156 insertions(+), 243 deletions(-)
testcases/kernel/mem/tunable/max_map_count.c +38 -69
index 75a5098..421e0ab 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,32 +38,8 @@
  * 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>
@@ -60,47 +50,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 +70,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 +124,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 +162,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 +172,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 +196,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 +54 -74

256 lines not on this page · raw message

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

264 lines not on this page · raw message

Signature · 1 line
2.9.3