All of lore.kernel.org
 help / color / mirror / Atom feed
From: Li Wang <liwang@redhat.com>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH 1/2] lib: add tst_request_hugepages
Date: Thu, 28 Nov 2019 16:45:05 +0800	[thread overview]
Message-ID: <20191128084506.7587-1-liwang@redhat.com> (raw)

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.

Signed-off-by: Li Wang <liwang@redhat.com>
---
 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

diff --git a/include/tst_hugepage.h b/include/tst_hugepage.h
new file mode 100644
index 000000000..e06705bd0
--- /dev/null
+++ b/include/tst_hugepage.h
@@ -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 */
+
diff --git a/include/tst_test.h b/include/tst_test.h
index 21c7dfbdb..7ec1ab865 100644
--- a/include/tst_test.h
+++ b/include/tst_test.h
@@ -37,6 +37,7 @@
 #include "tst_coredump.h"
 #include "tst_buffers.h"
 #include "tst_capability.h"
+#include "tst_hugepage.h"
 
 /*
  * Reports testcase result.
diff --git a/lib/tst_hugepage.c b/lib/tst_hugepage.c
new file mode 100644
index 000000000..00cf90843
--- /dev/null
+++ b/lib/tst_hugepage.c
@@ -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");
+		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;
+	}
+
+	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;
+}
-- 
2.20.1


             reply	other threads:[~2019-11-28  8:45 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-28  8:45 Li Wang [this message]
2019-11-28  8:45 ` [LTP] [PATCH 2/2] pkey01: disable hugetlb test if failed to reserve hugepage Li Wang
2019-12-03 10:49 ` [LTP] [PATCH 1/2] lib: add tst_request_hugepages Cyril Hrubis
2019-12-04  6:40   ` Li Wang
2019-12-04  9:26     ` Li Wang
2019-12-04 11:10       ` Cyril Hrubis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191128084506.7587-1-liwang@redhat.com \
    --to=liwang@redhat.com \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.