hi, Cyril & Garrett
v5 -> v6: updated the sscanf() function in read_meminfo() in order to avoid 
buffer overflow.

please feel free to comment the patch.
BTW: you would miss the patch.

There are several vm tunable files under /proc/sys/vm, I will compose
some cases for testing the basic functions of them. This patch is adding
the read/write/check functions to mem lib so that I can include them in
my testcases.

set_sys_tune():   set a long long integer value to a vm tunable file.
get_sys_tune():   get a long long integer value from a vm tunable file.
check_sys_tune(): check to confirm the value in tunable file meets our
                  expectation.
read_meminfo():   read the special value from /proc/meminfo.

Since most of the values in vm tunable files contain only single
integer, the functions I added only read/write long long values for now.
For those files not storing single interger (e.g. lowmem_reserve_ratio),
these functions will not be used in the case.

Signed-off-by: Zhouping Liu <z...@redhat.com>
---
 runtest/mm                         |    7 +++
 testcases/kernel/mem/include/mem.h |    7 +++
 testcases/kernel/mem/lib/mem.c     |   84 +++++++++++++++++++++++++++++++++++-
 3 files changed, 97 insertions(+), 1 deletions(-)

-- 
Thanks,
Zhouping Liu
From fa3862b43e8050ede39c4ccec387050c53b5c0dd Mon Sep 17 00:00:00 2001
From: Zhouping Liu <z...@redhat.com>
Date: Tue, 15 Nov 2011 20:56:37 +0800
Subject: [PATCH 1/2] mem/lib: add vm tunable file read/write/check func

There are several vm tunable files under /proc/sys/vm, I will compose
some cases for testing the basic functions of them. This patch is adding
the read/write/check functions to mem lib so that I can include them in
my testcases.

set_sys_tune():   set a long long integer value to a vm tunable file.
get_sys_tune():   get a long long integer value from a vm tunable file.
check_sys_tune(): check to confirm the value in tunable file meets our
                  expectation.
read_meminfo():   read the special value from /proc/meminfo.

Since most of the values in vm tunable files contain only single
integer, the functions I added only read/write long long values for now.
For those files not storing single interger (e.g. lowmem_reserve_ratio),
these functions will not be used in the case.

Signed-off-by: Zhouping Liu <z...@redhat.com>
---
 runtest/mm                         |    7 +++
 testcases/kernel/mem/include/mem.h |    7 +++
 testcases/kernel/mem/lib/mem.c     |   84 +++++++++++++++++++++++++++++++++++-
 3 files changed, 97 insertions(+), 1 deletions(-)

diff --git a/runtest/mm b/runtest/mm
index 615f1f1..8f34997 100644
--- a/runtest/mm
+++ b/runtest/mm
@@ -82,3 +82,10 @@ thp01 thp01 -I 120
 
 vma01 vma01
 vma02 vma02
+
+overcommit_memory overcommit_memory
+overcommit_memory overcommit_memory -R 0
+overcommit_memory overcommit_memory -R 30
+overcommit_memory overcommit_memory -R 80
+overcommit_memory overcommit_memory -R 100
+overcommit_memory overcommit_memory -R 200
diff --git a/testcases/kernel/mem/include/mem.h b/testcases/kernel/mem/include/mem.h
index 9e6b72c..22db56e 100644
--- a/testcases/kernel/mem/include/mem.h
+++ b/testcases/kernel/mem/include/mem.h
@@ -17,8 +17,11 @@
 #define MEMCG_PATH_NEW		MEMCG_PATH "/1"
 #define TESTMEM			(1UL<<30)
 #define MB			(1UL<<20)
+#define KB			(1UL<<10)
 #define PATH_SYS_SYSTEM		"/sys/devices/system"
 #define PATH_KSM		"/sys/kernel/mm/ksm/"
+#define PATH_SYSVM		"/proc/sys/vm/"
+#define PATH_MEMINFO		"/proc/meminfo"
 
 char overcommit[BUFSIZ];
 int opt_num, opt_size, opt_unit;
@@ -48,4 +51,8 @@ void create_same_memory(int size, int num, int unit);
 void check_ksm_options(int *size, int *num, int *unit);
 void write_cpusets(void);
 void write_memcg(void);
+void set_sys_tune(char *sys_file, long long tune);
+long long get_sys_tune(char *sys_file);
+void check_sys_tune(char *sys_file, long long tune);
+long long read_meminfo(char *item);
 #endif
diff --git a/testcases/kernel/mem/lib/mem.c b/testcases/kernel/mem/lib/mem.c
index 98fe5cc..07135d8 100644
--- a/testcases/kernel/mem/lib/mem.c
+++ b/testcases/kernel/mem/lib/mem.c
@@ -77,7 +77,7 @@ void write_memcg(void)
 	snprintf(buf, BUFSIZ, "%d", getpid());
 	if (write(fd, buf, strlen(buf)) != strlen(buf))
 		tst_brkm(TBROK|TERRNO, cleanup, "write %s", buf);
-	close(fd);	
+	close(fd);
 }
 
 void write_cpusets(void)
@@ -715,3 +715,85 @@ void check_ksm_options(int *size, int *num, int *unit)
 				"process number cannot be less 3.");
 	}
 }
+
+void set_sys_tune(char *sys_file, long long tune)
+{
+	int fd;
+	char buf[BUFSIZ];
+
+	tst_resm(TINFO, "set %s to %lld", sys_file, tune);
+
+	fd = open(sys_file, O_WRONLY);
+	if (fd == -1)
+		tst_brkm(TBROK|TERRNO, cleanup, "open '%s'", sys_file);
+	if (snprintf(buf, BUFSIZ, "%lld", tune) < 0)
+		tst_brkm(TBROK|TERRNO, cleanup, "snprintf");
+	if (write(fd, buf, strlen(buf)) == -1)
+		tst_brkm(TBROK|TERRNO, cleanup, "write '%s'", sys_file);
+
+	close(fd);
+}
+
+long long get_sys_tune(char *sys_file)
+{
+	int fd;
+	long long tune;
+	char buf[BUFSIZ], *endptr;
+
+	fd = open(sys_file, O_RDONLY);
+	if (fd == -1)
+		tst_brkm(TBROK|TERRNO, cleanup, "open '%s'", sys_file);
+	if (read(fd, buf, BUFSIZ) < 0)
+		tst_brkm(TBROK|TERRNO, cleanup, "read '%s'", sys_file);
+	close(fd);
+
+	tune = strtoll(buf, &endptr, 10);
+	if (tune == LLONG_MAX || tune == LLONG_MIN)
+		tst_brkm(TBROK|TERRNO, cleanup, "strtoll");
+	if (endptr == buf || (*endptr != '\0' && *endptr != '\n'))
+		tst_brkm(TBROK, cleanup, "Invalid tunable: %s", buf);
+
+	return tune;
+}
+
+/*
+ * the function is designed to make sure the value we get from
+ * sys_file is equal to what we set last.
+ */
+void check_sys_tune(char *sys_file, long long tune)
+{
+	long long val;
+
+	val = get_sys_tune(sys_file);
+	if (val == tune)
+		tst_resm(TINFO, "confirmed %s = %lld", sys_file, tune);
+	else
+		tst_brkm(TBROK, cleanup, "%s = %lld, is not %lld",
+				sys_file, val, tune);
+}
+
+long long read_meminfo(char *item)
+{
+	FILE *fp;
+	char line[BUFSIZ], buf[BUFSIZ];
+	char buf2[BUFSIZ];
+	long long val;
+
+	fp = fopen(PATH_MEMINFO, "r");
+	if (fp == NULL)
+		tst_brkm(TBROK|TERRNO, cleanup, "fopen %s", PATH_MEMINFO);
+
+	snprintf(buf2, BUFSIZ, "%%%ds %%lld", BUFSIZ);
+	while (fgets(line, BUFSIZ, fp) != NULL) {
+		if (sscanf(line, buf2, buf, &val) == 2)
+			if (strcmp(buf, item) == 0) {
+				fclose(fp);
+				return val;
+			}
+		continue;
+	}
+	fclose(fp);
+
+	tst_brkm(TBROK, cleanup, "cannot find \"%s\" in %s",
+			item, PATH_MEMINFO);
+}
-- 
1.7.1

------------------------------------------------------------------------------
RSA(R) Conference 2012
Save $700 by Nov 18
Register now
http://p.sf.net/sfu/rsa-sfdev2dev1
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

Reply via email to