Skip to contentSkip to conversation

lib: add tst_request_hugepages

PATCH New Li Wang 2 participants 6 messages Nov 28, 2019 – Dec 4, 2019 Library code

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH 1/2 lib: add tst_request_hugepages

Note: more and more tests need to configure hugepage before testing, so this is a start to add some useful hugetlb functions in global library for the whole LTP.

---
 include/tst_hugepage.h | 19 +++++++++++++++++++
 include/tst_test.h     |  1 +
 lib/tst_hugepage.c     | 43 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+)
 create mode 100644 include/tst_hugepage.h
 create mode 100644 lib/tst_hugepage.c
include/tst_hugepage.h new file +19 -0
new file mode 100644
index 000000000..e06705bd0
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019 Red Hat, Inc.
+ */
+
+#ifndef TST_HUGEPAGE__
+#define TST_HUGEPAGE__
+
+/*
+ * Try to request the specified number of huge pages from system.
+ *
+ * Note: this depend on the status of system memory fragmentation.
+ *       0 - reserve fail
+ *       1 - reserve success
+ */
+int tst_request_hugepages(long hpages);
+
+#endif /* TST_HUGEPAGE_H */
+
include/tst_test.h +1 -0
index 21c7dfbdb..7ec1ab865 100644
@@ -37,6 +37,7 @@
 #include "tst_coredump.h"
 #include "tst_buffers.h"
 #include "tst_capability.h"
+#include "tst_hugepage.h"
 
 /*
  * Reports testcase result.
lib/tst_hugepage.c new file +43 -0

43 lines not on this page · raw message

Signature · 1 line
2.20.1
author
#1 lore raw

PATCH 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 | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
testcases/kernel/syscalls/pkeys/pkey01.c +6 -7
index fa84e71f3..b5831af37 100644
@@ -61,13 +61,12 @@ static void setup(void)
 		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;
+		if (tst_request_hugepages(1)) {
+			size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
+		} else {
+			size = getpagesize();
+			no_hugepage = 1;
+		}
 	}
 
 	check_pkey_support();
Signature · 1 line
2.20.1
#2 lore raw

PATCH 1/2 lib: add tst_request_hugepages

In reply to Li Wang

Hi!

Quoted 25 lines

Note: more and more tests need to configure hugepage before testing, so this is a start to add some useful hugetlb functions in global library for the whole LTP.

---
 include/tst_hugepage.h | 19 +++++++++++++++++++
 include/tst_test.h     |  1 +
 lib/tst_hugepage.c     | 43 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+)
 create mode 100644 include/tst_hugepage.h
 create mode 100644 lib/tst_hugepage.c
include/tst_hugepage.h new file +6 -0
new file mode 100644
index 000000000..e06705bd0
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019 Red Hat, Inc.
+ */
+
+#ifndef TST_HUGEPAGE__

include/tst_hugepage.h line 16

Quoted 10 lines
include/tst_hugepage.h new file +10 -0
+#define TST_HUGEPAGE__
+
+/*
+ * Try to request the specified number of huge pages from system.
+ *
+ * Note: this depend on the status of system memory fragmentation.
+ *       0 - reserve fail
+ *       1 - reserve success
+ */
+int tst_request_hugepages(long hpages);

Can we add this to the tst_test structure so that test case use this as:

static struct tst_test test = {
	...
	.needs_hugepages = 1,
	...
}

That way it will be included in the test metadata I'm working on.

Quoted 44 lines
include/tst_hugepage.h new file +2 -0
+#endif /* TST_HUGEPAGE_H */
+
include/tst_test.h +1 -0
index 21c7dfbdb..7ec1ab865 100644
@@ -37,6 +37,7 @@
 #include "tst_coredump.h"
 #include "tst_buffers.h"
 #include "tst_capability.h"
+#include "tst_hugepage.h"
 
 /*
  * Reports testcase result.
lib/tst_hugepage.c new file +24 -0
new file mode 100644
index 000000000..00cf90843
@@ -0,0 +1,43 @@
+// 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"
+
+int tst_request_hugepages(long hpages)
+{
+	int val;
+	long mem_avail, max_hpages;
+
+	if (FILE_LINES_SCANF("/proc/meminfo",
+				"MemAvailable: %ld", &mem_avail)) {
+		/*
+		 * Dropping caches and using "MemFree:" on kernel
+		 * that doesn't have "MemAvailable:" in Meminfo
+		 */
+		tst_res(TINFO, "MemAvailable: not found in /proc/meminfo");
+
+		SAFE_FILE_PRINTF("/proc/sys/vm/drop_caches", "3");

lib/tst_hugepage.c line 34

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, "Request %ld hugepages failed, memory too fragmented? "
+				"The max hugepage available count %ld",
+				hpages, max_hpages);
+		return 0;

I guess that rest of the library functions returns non-zero on failure.

lib/tst_hugepage.c line 42

Quoted 8 lines
lib/tst_hugepage.c new file +8 -0
+	}
+
+	SAFE_FILE_PRINTF("/proc/sys/vm/nr_hugepages", "%ld", hpages);
+	SAFE_FILE_SCANF("/proc/sys/vm/nr_hugepages", "%d", &val);
+	if (val != hpages)
+		tst_brk(TBROK, "nr_hugepages = %d, but expect %ld", val, hpages);
+
+	return 1;

I guess that things will be more complicated than this. If there is already a pool of hugepages allocated on the machine and some are used this will utimatelly fail.

I rember that we changed some tests to increase the nr_hugepages if needed instead of blindly writing values there.

Also this fails to cleanup after itself, we have to restore the system after the test, which is one more reason why we need .needs_hugepages in the tst_test structure because the test library can cleanup after the test with that.

Quoted 7 lines
lib/tst_hugepage.c new file +1 -0
+}
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 1/2 lib: add tst_request_hugepages

Quoted 11 lines · Cyril Hrubis

...

Can we add this to the tst_test structure so that test case use this as:

static struct tst_test test = {
        ...
        .needs_hugepages = 1,
        ...
}

Good point.

Quoted 6 lines

That way it will be included in the test metadata I'm working on.

...

lib/tst_hugepage.c

Quoted 3 lines
lib/tst_hugepage.c new file +3 -0
+
+     if (hpages > max_hpages) {
+             tst_res(TINFO, "Request %ld hugepages failed, memory too
Quoted 7 lines

fragmented? "

lib/tst_hugepage.c line 34

Quoted 3 lines
lib/tst_hugepage.c new file +3 -0
+                             "The max hugepage available count %ld",
+                             hpages, max_hpages);
+             return 0;

I guess that rest of the library functions returns non-zero on failure.

Sure, will reset the value for return.

Quoted 7 lines

lib/tst_hugepage.c

Quoted 7 lines
lib/tst_hugepage.c new file +6 -0
+     }
+
+     SAFE_FILE_PRINTF("/proc/sys/vm/nr_hugepages", "%ld", hpages);
+     SAFE_FILE_SCANF("/proc/sys/vm/nr_hugepages", "%d", &val);
+     if (val != hpages)
+             tst_brk(TBROK, "nr_hugepages = %d, but expect %ld", val,
Quoted 8 lines

hpages);

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+     return 1;

I guess that things will be more complicated than this. If there is already a pool of hugepages allocated on the machine and some are used this will utimatelly fail.

I don't think so, if they're already many huge pages being used, the free memory of the system will firstly tell us the max_hpages is not satisfy for testing, it means we have no chance to arrive here.

Quoted 4 lines

I rember that we changed some tests to increase the nr_hugepages if needed instead of blindly writing values there.

Yes, but that's only fit for the situation which does not need the precise number of pages, we could take 80% of the max_hpages and do our testing in our system.

But in this function, my purpose is to request the least number of pages for the test. e.g if the .needs_hugepages is set to an expected number, we should verify strictly if the system can provide that.

Quoted 6 lines

Also this fails to cleanup after itself, we have to restore the system after the test, which is one more reason why we need .needs_hugepages in the tst_test structure because the test library can cleanup after the test with that.

Agree, I will add the save/restore part for this.

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20191204/d52cedff/attachment.htm>
author
#4 lore raw

PATCH 1/2 lib: add tst_request_hugepages

Quoted 4 lines · Li Wang

...
if the .needs_hugepages is set to an expected number, we should verify strictly if the system can provide that.

Oh, I guess maybe I misunderstand your suggestion here. you mean just to let .needs_hugepages = 1 but not set an expected number, right? if so, that will more easy to achieve.

Quoted 9 lines
Quoted 6 lines

Also this fails to cleanup after itself, we have to restore the system after the test, which is one more reason why we need .needs_hugepages in the tst_test structure because the test library can cleanup after the test with that.

Agree, I will add the save/restore part for this.

Or just go with .save_restore in the testcase?

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20191204/6ae5a7b1/attachment.htm>
#5 lore raw

PATCH 1/2 lib: add tst_request_hugepages

Hi!

Quoted 7 lines
Quoted 3 lines

if the .needs_hugepages is set to an expected number, we should verify strictly if the system can provide that.

Oh, I guess maybe I misunderstand your suggestion here. you mean just to let .needs_hugepages = 1 but not set an expected number, right? if so, that will more easy to achieve.

We do have tests that needs more than 1 hugepage, there are tests that do numa migration so we likely need the needs_hugepages to be unsigned integer that represents the number of required hugepages.

Quoted 10 lines
Quoted 8 lines
Quoted 5 lines

Also this fails to cleanup after itself, we have to restore the system after the test, which is one more reason why we need .needs_hugepages in the tst_test structure because the test library can cleanup after the test with that.

Agree, I will add the save/restore part for this.

Or just go with .save_restore in the testcase?

I guess that best option would be to call tst_sys_conf_save_str() in the test library if we decided to change the nr_hugepages file.

Signature · 2 lines
Cyril Hrubis
[email protected]