[LTP] [PATCH 1/3] lib: add tst_available_mem function

Li Wang liwang@redhat.com
Fri Sep 24 09:07:54 CEST 2021


tst_available_mem helps to get the value of MemAvailable
from /proc/meminfo, if no support on older kernels,
return 'MemFree + Cached' for instead.

Signed-off-by: Li Wang <liwang@redhat.com>
---
 include/tst_memutils.h |  6 ++++++
 lib/tst_memutils.c     | 13 +++++++++++++
 2 files changed, 19 insertions(+)

diff --git a/include/tst_memutils.h b/include/tst_memutils.h
index 91dad07cd..f605f544e 100644
--- a/include/tst_memutils.h
+++ b/include/tst_memutils.h
@@ -19,4 +19,10 @@
  */
 void tst_pollute_memory(size_t maxsize, int fillchar);
 
+/*
+ * Read the value of MemAvailable from /proc/meminfo, if no support on
+ * older kernels, return 'MemFree + Cached' for instead.
+ */
+long long tst_available_mem(void);
+
 #endif /* TST_MEMUTILS_H__ */
diff --git a/lib/tst_memutils.c b/lib/tst_memutils.c
index 70b60091f..69077861f 100644
--- a/lib/tst_memutils.c
+++ b/lib/tst_memutils.c
@@ -62,3 +62,16 @@ void tst_pollute_memory(size_t maxsize, int fillchar)
 
 	free(map_blocks);
 }
+
+long long tst_available_mem(void)
+{
+	long long mem_available;
+
+	if (FILE_LINES_SCANF("/proc/meminfo", "MemAvailable: %ld",
+		&mem_available)) {
+		mem_available = SAFE_READ_MEMINFO("MemFree:")
+			+ SAFE_READ_MEMINFO("Cached:");
+	}
+
+	return mem_available;
+}
-- 
2.31.1



More information about the ltp mailing list