[LTP] [PATCH 3/6] memcontrol: Lift out some common definitions into a shared header

Richard Palethorpe rpalethorpe@suse.com
Thu Jan 27 07:12:22 CET 2022


Some simple functions can be shared between tests. The original
selftests share a bit more. However this doesn't make as much sense in
LTP due to library differences.

Signed-off-by: Richard Palethorpe <rpalethorpe@suse.com>
---
 .../kernel/controllers/memcg/memcontrol02.c   | 32 +------------
 .../controllers/memcg/memcontrol_common.h     | 48 +++++++++++++++++++
 2 files changed, 49 insertions(+), 31 deletions(-)
 create mode 100644 testcases/kernel/controllers/memcg/memcontrol_common.h

diff --git a/testcases/kernel/controllers/memcg/memcontrol02.c b/testcases/kernel/controllers/memcg/memcontrol02.c
index 548f36829..d5a24cc0a 100644
--- a/testcases/kernel/controllers/memcg/memcontrol02.c
+++ b/testcases/kernel/controllers/memcg/memcontrol02.c
@@ -23,14 +23,7 @@
  */
 #define _GNU_SOURCE
 
-#include <stdlib.h>
-#include <stdio.h>
-
-#include "tst_test.h"
-#include "tst_cgroup.h"
-
-#define TMPDIR "mntdir"
-#define MB(x) (x << 20)
+#include "memcontrol_common.h"
 
 static size_t page_size;
 static const struct tst_cgroup_group *cg_test;
@@ -38,20 +31,6 @@ static struct tst_cgroup_group *cg_child;
 static int fd;
 static int file_to_all_error = 10;
 
-/*
- * Checks if two given values differ by less than err% of their
- * sum. An extra percent is added for every doubling of the page size
- * to compensate for wastage in page sized allocations.
- */
-static inline int values_close(const ssize_t a,
-			       const ssize_t b,
-			       const ssize_t err)
-{
-	const ssize_t page_adjusted_err = ffs(page_size >> 13) + err;
-
-	return 100 * labs(a - b) <= (a + b) * page_adjusted_err;
-}
-
 static void alloc_anon_50M_check(void)
 {
 	const ssize_t size = MB(50);
@@ -78,15 +57,6 @@ static void alloc_anon_50M_check(void)
 		     current, anon);
 }
 
-static void alloc_pagecache(const int fd, size_t size)
-{
-	char buf[BUFSIZ];
-	size_t i;
-
-	for (i = 0; i < size; i += sizeof(buf))
-		SAFE_WRITE(1, fd, buf, sizeof(buf));
-}
-
 static void alloc_pagecache_50M_check(void)
 {
 	const size_t size = MB(50);
diff --git a/testcases/kernel/controllers/memcg/memcontrol_common.h b/testcases/kernel/controllers/memcg/memcontrol_common.h
new file mode 100644
index 000000000..67f3ca318
--- /dev/null
+++ b/testcases/kernel/controllers/memcg/memcontrol_common.h
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "tst_test.h"
+#include "tst_cgroup.h"
+
+#define TMPDIR "mntdir"
+#define MB(x) (x << 20)
+
+/*
+ * Checks if two given values differ by less than err% of their
+ * sum. An extra percent is added for every doubling of the page size
+ * to compensate for wastage in page sized allocations.
+ */
+static inline int values_close(const ssize_t a,
+			       const ssize_t b,
+			       const ssize_t err)
+{
+	const size_t page_size = SAFE_SYSCONF(_SC_PAGESIZE);
+	const ssize_t page_adjusted_err = ffs(page_size >> 13) + err;
+
+	return 100 * labs(a - b) <= (a + b) * page_adjusted_err;
+}
+
+static inline void alloc_pagecache(const int fd, size_t size)
+{
+	char buf[BUFSIZ];
+	size_t i;
+
+	SAFE_LSEEK(fd, 0, SEEK_END);
+
+	for (i = 0; i < size; i += sizeof(buf))
+		SAFE_WRITE(1, fd, buf, sizeof(buf));
+}
+
+static inline void alloc_anon(const size_t size)
+{
+	const size_t page_size = SAFE_SYSCONF(_SC_PAGESIZE);
+	char *const buf = SAFE_MALLOC(size);
+	size_t i;
+
+	for (i = 0; i < size; i += page_size)
+		buf[i] = 0;
+
+	free(buf);
+}
-- 
2.34.1



More information about the ltp mailing list