Skip to contentSkip to conversation

lib: add .request_hugepages to reserve hugepage

PATCH Superseded Li Wang 2 participants 4 messages Dec 18, 2019 – Jan 25, 2020 Library code

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH v3 1/2 lib: add .request_hugepages to reserve hugepage

Many of the ltp tests need to use hugepage in their testing, this allows the test can reserve hpages from system only via .request_hugepages = xx.

If set non-zero number of request_hugepages, test will try to reserve the expected number of hugepage for testing in setup phase. If system does not have enough hpage for using, it will try the best to reserve 80% available number of hpages. For a system without hugetlb supporting, variable 'tst_hugepage' will be set to 0.

---

Notes:
v2 --> v3
* rename .needs_hugepages to .request_hugepages
* rename tst_no_hugepage to tst_hugepages
* makes tst_request_hugepages() return tst_hugepages
* call tst_sys_conf_save in tst_hugepage.c

 doc/test-writing-guidelines.txt | 38 ++++++++++++++++++++++
 include/tst_hugepage.h          | 29 +++++++++++++++++
 include/tst_test.h              | 13 ++++++++
 lib/newlib_tests/.gitignore     |  1 +
 lib/newlib_tests/test20.c       | 45 ++++++++++++++++++++++++++
 lib/tst_hugepage.c              | 57 +++++++++++++++++++++++++++++++++
 lib/tst_test.c                  |  6 ++--
 7 files changed, 187 insertions(+), 2 deletions(-)
 create mode 100644 include/tst_hugepage.h
 create mode 100644 lib/newlib_tests/test20.c
 create mode 100644 lib/tst_hugepage.c
doc/test-writing-guidelines.txt +38 -0
index 546bb7a49..7875690b9 100644
@@ -1933,6 +1933,44 @@ specified by the user plus some other heuristics.
 
 For full documentation see the comments in 'include/tst_fuzzy_sync.h'.
 
+2.2.34 Reserving hugepages
+^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Many of the ltp tests need to use hugepage in their testing, this allows the
+test can reserve hugepages from system only via .request_hugepages = xx.
+
+If set non-zero number of request_hugepages, test will try to reserve the
+expected number of hugepage for testing in setup phase. If system does not
+have enough hpage for using, it will try the best to reserve 80% available
+number of hpages. With success test stores the reserved hugepage number in
+'tst_hugepages. For the system without hugetlb supporting, variable
+'tst_hugepages' will be set to 0.
+
+Also, we do cleanup and restore work for the hpages resetting automatically.
+
+[source,c]
+-------------------------------------------------------------------------------
+#include "tst_test.h"
+
+static void run(void)
+{
+	...
+
+	if (tst_hugepages == test.request_hugepages)
+		TEST(do_hpage_test);
+	else
+		...
+	...
+}
+
+struct tst_test test = {
+	...
+	.test_all = run,
+	.request_hugepages = 2,
+	...
+};
+-------------------------------------------------------------------------------
+
 2.3 Writing a testcase in shell
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
include/tst_hugepage.h new file +29 -0
new file mode 100644
index 000000000..10e0eaf47
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019 Red Hat, Inc.
+ */
+
+#ifndef TST_HUGEPAGE__
+#define TST_HUGEPAGE__
+
+#define PATH_HUGEPAGES	"/sys/kernel/mm/hugepages/"
+#define PATH_NR_HPAGES	"/proc/sys/vm/nr_hugepages"
+
+/*
+ * Try the best to request a specified number of huge pages from system,
+ * it will store the reserved hpage number in tst_hugepages.
+ *
+ * Note: this depend on the status of system memory fragmentation.
+ */
+int tst_request_hugepages(int hpages);
+
+/*
+ * This variable is used for recording the number of hugepages which system can
+ * provides. It will be equal to 'hpages' if tst_request_hugepages on success,
+ * otherwise set it to a number of hugepages that we were able to reserve.
+ *
+ * If system does not support hugetlb, then it will be set to 0.
+ */
+extern unsigned int tst_hugepages;
+
+#endif /* TST_HUGEPAGE_H */
include/tst_test.h +13 -0
index 21c7dfbdb..1026a422a 100644
@@ -37,6 +37,7 @@
 #include "tst_coredump.h"
 #include "tst_buffers.h"
 #include "tst_capability.h"
+#include "tst_hugepage.h"
 
 /*
  * Reports testcase result.
@@ -148,6 +149,18 @@ struct tst_test {
 	 */
 	int all_filesystems:1;
 
+	/*
+	 * If set non-zero number of request_hugepages, test will try to reserve the
+	 * expected number of hugepage for testing in setup phase. If system does not
+	 * have enough hpage for using, it will try the best to reserve 80% available
+	 * number of hpages. With success test stores the reserved hugepage number in
+	 * 'tst_hugepages. For the system without hugetlb supporting, variable
+	 * 'tst_hugepages' will be set to 0.
+	 *
+	 * Also, we do cleanup and restore work for the hpages resetting automatically.
+	 */
+	unsigned int request_hugepages;
+
 	/*
 	 * If set non-zero denotes number of test variant, the test is executed
 	 * variants times each time with tst_variant set to different number.
lib/newlib_tests/.gitignore +1 -0
index d4aa4935f..ab3a92c4c 100644
@@ -23,6 +23,7 @@ tst_strstatus
 test17
 test18
 test19
+test20
 tst_expiration_timer
 test_exec
 test_exec_child
lib/newlib_tests/test20.c new file +45 -0

45 lines not on this page · raw message

lib/tst_hugepage.c new file +57 -0

57 lines not on this page · raw message

lib/tst_test.c +4 -2
index 8d7dee2cc..9a24cffc5 100644
@@ -890,6 +890,9 @@ static void do_setup(int argc, char *argv[])
 	if (tst_test->all_filesystems)
 		tst_test->needs_device = 1;
 
+	if (tst_test->request_hugepages)
+		tst_request_hugepages(tst_test->request_hugepages);
+
 	setup_ipc();
 
 	if (tst_test->bufs)
@@ -1006,8 +1009,7 @@ static void do_cleanup(void)
 		tst_rmdir();
 	}
 
-	if (tst_test->save_restore)
-		tst_sys_conf_restore(0);
+	tst_sys_conf_restore(0);
 
 	if (tst_test->restore_wallclock)
 		tst_wallclock_restore();
Signature · 1 line
2.20.1
author
#1 lore raw

PATCH v3 2/2 pkey01: disable hugetlb test if failed to reserve hugepage

To handle the false positive:
pkey01.c:69: BROK: nr_hugepages = 0, but expect 1

---
 testcases/kernel/syscalls/pkeys/pkey01.c | 29 ++++++------------------
 1 file changed, 7 insertions(+), 22 deletions(-)
testcases/kernel/syscalls/pkeys/pkey01.c +7 -22
index fa84e71f3..04f50924c 100644
@@ -36,12 +36,6 @@
 #define PATH_VM_NRHPS "/proc/sys/vm/nr_hugepages"
 
 static int size;
-static int no_hugepage;
-
-static const char * const save_restore[] = {
-	"?/proc/sys/vm/nr_hugepages",
-	NULL,
-};
 
 static struct tcase {
 	unsigned long flags;
@@ -56,22 +50,13 @@ static void setup(void)
 {
 	int i, fd;
 
-	if (access("/sys/kernel/mm/hugepages/", F_OK)) {
-		tst_res(TINFO, "Huge page is not supported");
-		size = getpagesize();
-		no_hugepage = 1;
-	} else {
-		int val;
-		SAFE_FILE_PRINTF(PATH_VM_NRHPS, "%d", 1);
-		SAFE_FILE_SCANF(PATH_VM_NRHPS, "%d", &val);
-		if (val != 1)
-			tst_brk(TBROK, "nr_hugepages = %d, but expect %d",
-					val, 1);
-		size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
-	}
-
 	check_pkey_support();
 
+	if (tst_hugepages == test.request_hugepages)
+		size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
+	else
+		size = getpagesize();
+
 	fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0664);
 	for (i = 0; i < 128; i++)
 		SAFE_WRITE(1, fd, STR, strlen(STR));
@@ -147,7 +132,7 @@ static void pkey_test(struct tcase *tc, struct mmap_param *mpa)
 	int pkey, status;
 	int fd = mpa->fd;
 
-	if (no_hugepage && (mpa->flags & MAP_HUGETLB)) {
+	if (!tst_hugepages && (mpa->flags & MAP_HUGETLB)) {
 		tst_res(TINFO, "Skip test on (%s) buffer", flag_to_str(mpa->flags));
 		return;
 	}
@@ -236,5 +221,5 @@ static struct tst_test test = {
 	.forks_child = 1,
 	.test = verify_pkey,
 	.setup = setup,
-	.save_restore = save_restore,
+	.request_hugepages = 1,
 };
Signature · 1 line
2.20.1
#2 lore raw

PATCH v3 1/2 lib: add .request_hugepages to reserve hugepage

In reply to Li Wang

Hi!

Quoted 58 lines

Many of the ltp tests need to use hugepage in their testing, this allows the test can reserve hpages from system only via .request_hugepages = xx.

If set non-zero number of request_hugepages, test will try to reserve the expected number of hugepage for testing in setup phase. If system does not have enough hpage for using, it will try the best to reserve 80% available number of hpages. For a system without hugetlb supporting, variable 'tst_hugepage' will be set to 0.

---

Notes:
v2 --> v3
* rename .needs_hugepages to .request_hugepages
* rename tst_no_hugepage to tst_hugepages
* makes tst_request_hugepages() return tst_hugepages
* call tst_sys_conf_save in tst_hugepage.c

 doc/test-writing-guidelines.txt | 38 ++++++++++++++++++++++
 include/tst_hugepage.h          | 29 +++++++++++++++++
 include/tst_test.h              | 13 ++++++++
 lib/newlib_tests/.gitignore     |  1 +
 lib/newlib_tests/test20.c       | 45 ++++++++++++++++++++++++++
 lib/tst_hugepage.c              | 57 +++++++++++++++++++++++++++++++++
 lib/tst_test.c                  |  6 ++--
 7 files changed, 187 insertions(+), 2 deletions(-)
 create mode 100644 include/tst_hugepage.h
 create mode 100644 lib/newlib_tests/test20.c
 create mode 100644 lib/tst_hugepage.c
doc/test-writing-guidelines.txt +19 -0
index 546bb7a49..7875690b9 100644
@@ -1933,6 +1933,44 @@ specified by the user plus some other heuristics.
 
 For full documentation see the comments in 'include/tst_fuzzy_sync.h'.
 
+2.2.34 Reserving hugepages
+^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Many of the ltp tests need to use hugepage in their testing, this allows the
+test can reserve hugepages from system only via .request_hugepages = xx.
+
+If set non-zero number of request_hugepages, test will try to reserve the
+expected number of hugepage for testing in setup phase. If system does not
+have enough hpage for using, it will try the best to reserve 80% available
+number of hpages. With success test stores the reserved hugepage number in
+'tst_hugepages. For the system without hugetlb supporting, variable
+'tst_hugepages' will be set to 0.
+
+Also, we do cleanup and restore work for the hpages resetting automatically.
+
+[source,c]
+-------------------------------------------------------------------------------
+#include "tst_test.h"
+

doc/test-writing-guidelines.txt specified by the user plus some other heuristics. · line 1964

Quoted 10 lines
doc/test-writing-guidelines.txt +10 -0
+static void run(void)
+{
+	...
+
+	if (tst_hugepages == test.request_hugepages)
+		TEST(do_hpage_test);
+	else
+		...
+	...
+}

I guess that it would be more instructive to check the value in the test setup() here, because most of the test would just check if tst_hugepages has been set to expected value.

static void setup(void)

{
	if (tst_hugepages != test.requested_hugepages)
		tst_brk(TCONF, "...");
}
Quoted 155 lines
doc/test-writing-guidelines.txt +8 -0
+struct tst_test test = {
+	...
+	.test_all = run,
+	.request_hugepages = 2,
+	...
+};
+-------------------------------------------------------------------------------
+
 2.3 Writing a testcase in shell
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
include/tst_hugepage.h new file +29 -0
new file mode 100644
index 000000000..10e0eaf47
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019 Red Hat, Inc.
+ */
+
+#ifndef TST_HUGEPAGE__
+#define TST_HUGEPAGE__
+
+#define PATH_HUGEPAGES	"/sys/kernel/mm/hugepages/"
+#define PATH_NR_HPAGES	"/proc/sys/vm/nr_hugepages"
+
+/*
+ * Try the best to request a specified number of huge pages from system,
+ * it will store the reserved hpage number in tst_hugepages.
+ *
+ * Note: this depend on the status of system memory fragmentation.
+ */
+int tst_request_hugepages(int hpages);
+
+/*
+ * This variable is used for recording the number of hugepages which system can
+ * provides. It will be equal to 'hpages' if tst_request_hugepages on success,
+ * otherwise set it to a number of hugepages that we were able to reserve.
+ *
+ * If system does not support hugetlb, then it will be set to 0.
+ */
+extern unsigned int tst_hugepages;
+
+#endif /* TST_HUGEPAGE_H */
include/tst_test.h +13 -0
index 21c7dfbdb..1026a422a 100644
@@ -37,6 +37,7 @@
 #include "tst_coredump.h"
 #include "tst_buffers.h"
 #include "tst_capability.h"
+#include "tst_hugepage.h"
 
 /*
  * Reports testcase result.
@@ -148,6 +149,18 @@ struct tst_test {
 	 */
 	int all_filesystems:1;
 
+	/*
+	 * If set non-zero number of request_hugepages, test will try to reserve the
+	 * expected number of hugepage for testing in setup phase. If system does not
+	 * have enough hpage for using, it will try the best to reserve 80% available
+	 * number of hpages. With success test stores the reserved hugepage number in
+	 * 'tst_hugepages. For the system without hugetlb supporting, variable
+	 * 'tst_hugepages' will be set to 0.
+	 *
+	 * Also, we do cleanup and restore work for the hpages resetting automatically.
+	 */
+	unsigned int request_hugepages;
+
 	/*
 	 * If set non-zero denotes number of test variant, the test is executed
 	 * variants times each time with tst_variant set to different number.
lib/newlib_tests/.gitignore +1 -0
index d4aa4935f..ab3a92c4c 100644
@@ -23,6 +23,7 @@ tst_strstatus
 test17
 test18
 test19
+test20
 tst_expiration_timer
 test_exec
 test_exec_child
lib/newlib_tests/test20.c new file +45 -0
new file mode 100644
index 000000000..92e230976
@@ -0,0 +1,45 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019 Li Wang <[email protected]>
+ */
+
+/*
+ * Tests .request_hugepages + .save_restore
+ */
+
+#include "tst_test.h"
+#include "tst_hugepage.h"
+#include "tst_sys_conf.h"
+
+static const char * const save_restore[] = {
+	"!/proc/sys/kernel/numa_balancing",
+	NULL,
+};
+
+static void do_test(void) {
+
+	int val, hpages;
+
+	tst_res(TINFO, "tst_hugepages = %u", tst_hugepages);
+	SAFE_FILE_PRINTF("/proc/sys/kernel/numa_balancing", "1");
+
+	hpages = test.request_hugepages;
+	SAFE_FILE_SCANF(PATH_NR_HPAGES, "%d", &val);
+	if (val != hpages)
+		tst_brk(TBROK, "nr_hugepages = %d, but expect %d", val, hpages);
+	else
+		tst_res(TPASS, "test .needs_hugepges");
+
+	hpages = tst_request_hugepages(3);
+	SAFE_FILE_SCANF(PATH_NR_HPAGES, "%d", &val);
+	if (val != hpages)
+		tst_brk(TBROK, "nr_hugepages = %d, but expect %d", val, hpages);
+	else
+		tst_res(TPASS, "tst_request_hugepages");
+}
+
+static struct tst_test test = {
+	.test_all = do_test,
+	.request_hugepages = 2,
+	.save_restore = save_restore,
+};
lib/tst_hugepage.c new file +9 -0
new file mode 100644
index 000000000..c4385b9c6
@@ -0,0 +1,57 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019 Red Hat, Inc.
+ */
+
+#define TST_NO_DEFAULT_MAIN
+
+#include "tst_test.h"
+#include "tst_hugepage.h"

lib/tst_hugepage.c line 19

Quoted 10 lines
lib/tst_hugepage.c new file +10 -0
+
+unsigned int tst_hugepages;
+
+int tst_request_hugepages(int hpages)
+{
+	int val;
+	long mem_avail, max_hpages;
+
+	if (access(PATH_HUGEPAGES, F_OK)) {
+		tst_res(TCONF, "Huge page is not supported.");

I guess that this is something the test has to decide, i.e. I wouldn't print anything here.

lib/tst_hugepage.c line 26

Quoted 7 lines
lib/tst_hugepage.c new file +7 -0
+		tst_hugepages = 0;
+		goto out;
+	}
+
+	tst_hugepages = hpages;
+
+	SAFE_FILE_PRINTF("/proc/sys/vm/drop_caches", "3");

Shouldn't we drop the caches only if MemAvailable is not present in /proc/meminfo?

Quoted 8 lines
lib/tst_hugepage.c new file +8 -0
+	if (FILE_LINES_SCANF("/proc/meminfo",
+				"MemAvailable: %ld", &mem_avail)) {
+		/*
+		 * Using "MemFree:" on kernel that doesn't have
+		 * "MemAvailable:" in Meminfo
+		 */
+		tst_res(TINFO, "MemAvailable: not found in /proc/meminfo");
+

lib/tst_hugepage.c line 45

Quoted 10 lines
lib/tst_hugepage.c new file +10 -0
+		mem_avail = SAFE_READ_MEMINFO("MemFree:");
+	}
+
+	max_hpages = mem_avail / SAFE_READ_MEMINFO("Hugepagesize:");
+
+	if (hpages > max_hpages) {
+		tst_res(TINFO, "Requested number(%d) of hugepages is too large, "
+				"limiting to 80%% of the max hugepage count %ld",
+				hpages, max_hpages);
+		tst_hugepages = max_hpages * 0.8;

Why 80%?

Also I guess that we should check here that max_hpages * 0.8 > 1, otherwise there is no reason to continue.

Quoted 42 lines
lib/tst_hugepage.c new file +12 -0
+	}
+
+	tst_sys_conf_save("?/proc/sys/vm/nr_hugepages");
+	SAFE_FILE_PRINTF(PATH_NR_HPAGES, "%d", tst_hugepages);
+	SAFE_FILE_SCANF(PATH_NR_HPAGES, "%d", &val);
+	if (val != tst_hugepages)
+		tst_brk(TBROK, "nr_hugepages = %d, but expect %d", val, tst_hugepages);
+
+	tst_res(TINFO, "%d hugepage(s) reserved", tst_hugepages);
+out:
+	return tst_hugepages;
+}
lib/tst_test.c +4 -2
index 8d7dee2cc..9a24cffc5 100644
@@ -890,6 +890,9 @@ static void do_setup(int argc, char *argv[])
 	if (tst_test->all_filesystems)
 		tst_test->needs_device = 1;
 
+	if (tst_test->request_hugepages)
+		tst_request_hugepages(tst_test->request_hugepages);
+
 	setup_ipc();
 
 	if (tst_test->bufs)
@@ -1006,8 +1009,7 @@ static void do_cleanup(void)
 		tst_rmdir();
 	}
 
-	if (tst_test->save_restore)
-		tst_sys_conf_restore(0);
+	tst_sys_conf_restore(0);
 
 	if (tst_test->restore_wallclock)
 		tst_wallclock_restore();
Signature · 5 lines
2.20.1


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

PATCH v3 1/2 lib: add .request_hugepages to reserve hugepage

Quoted 16 lines · Cyril Hrubis

...

doc/test-writing-guidelines.txt specified by the user plus some other heuristics. · line 1964

Quoted 10 lines
doc/test-writing-guidelines.txt +10 -0
+static void run(void)
+{
+     ...
+
+     if (tst_hugepages == test.request_hugepages)
+             TEST(do_hpage_test);
+     else
+             ...
+     ...
+}

I guess that it would be more instructive to check the value in the test setup() here, because most of the test would just check if tst_hugepages has been set to expected value.

Yes, that will fit most situations.

Quoted 7 lines

...

lib/tst_hugepage.c line 19

Quoted 2 lines
lib/tst_hugepage.c new file +2 -0
+     if (access(PATH_HUGEPAGES, F_OK)) {
+             tst_res(TCONF, "Huge page is not supported.");

I guess that this is something the test has to decide, i.e. I wouldn't print anything here.

OK.

Quoted 12 lines

lib/tst_hugepage.c line 26

Quoted 8 lines
lib/tst_hugepage.c new file +7 -0
+             tst_hugepages = 0;
+             goto out;
+     }
+
+     tst_hugepages = hpages;
+
+     SAFE_FILE_PRINTF("/proc/sys/vm/drop_caches", "3");

Shouldn't we drop the caches only if MemAvailable is not present in /proc/meminfo?

Yes, that's right theoretically. But the reason I moved it here is that the huge page reserving easily failed on a small RAM(1~2GB) system because of too much caches.

Quoted 17 lines
Quoted 6 lines
lib/tst_hugepage.c new file +5 -0
+     if (FILE_LINES_SCANF("/proc/meminfo",
+                             "MemAvailable: %ld", &mem_avail)) {
+             /*
+              * Using "MemFree:" on kernel that doesn't have
+              * "MemAvailable:" in Meminfo

lib/tst_hugepage.c

Quoted 10 lines
lib/tst_hugepage.c new file +10 -0
+              */
+             tst_res(TINFO, "MemAvailable: not found in /proc/meminfo");
+
+             mem_avail = SAFE_READ_MEMINFO("MemFree:");
+     }
+
+     max_hpages = mem_avail / SAFE_READ_MEMINFO("Hugepagesize:");
+
+     if (hpages > max_hpages) {
+             tst_res(TINFO, "Requested number(%d) of hugepages is too

large, "

Quoted 7 lines
Quoted 1 line

+ "limiting to 80%% of the max hugepage

count %ld",

lib/tst_hugepage.c line 45

Quoted 2 lines
lib/tst_hugepage.c new file +2 -0
+                             hpages, max_hpages);
+             tst_hugepages = max_hpages * 0.8;

Why 80%?

I choose(base on personal experience) this number to try again, to satisfy some test which does not need precise hpages can still run.

Or, maybe let's remove this part, and limit LTP to make use of a precise number(the at least) of hpage in each test?

Quoted 4 lines

Also I guess that we should check here that max_hpages * 0.8 > 1, otherwise there is no reason to continue.

Agreed.

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20200125/cf9223ca/attachment-0001.htm>