Hi,
v2 -> v3: modified the variable's type.
please review it again, any comments are welcome.

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 value to a vm tunable file.
get_sys_tune():   get a 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 <[email protected]>
---
 testcases/kernel/mem/include/mem.h |    7 +++
 testcases/kernel/mem/lib/mem.c     |   80 +++++++++++++++++++++++++++++++++++-
 2 files changed, 86 insertions(+), 1 deletions(-)



-- 
Thanks,
Zhouping Liu
From ee9b4bdb1aec74bf1c24dd9dc1c5c46ead9dd140 Mon Sep 17 00:00:00 2001
From: Zhouping Liu <[email protected]>
Date: Tue, 30 Aug 2011 16:40:32 +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 value to a vm tunable file.
get_sys_tune():   get a 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 <[email protected]>
---
 testcases/kernel/mem/include/mem.h |    7 +++
 testcases/kernel/mem/lib/mem.c     |   80 +++++++++++++++++++++++++++++++++++-
 2 files changed, 86 insertions(+), 1 deletions(-)

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 f3983d4..f7d9d44 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)
@@ -712,3 +712,81 @@ 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");
+	sprintf(buf, "%lld", tune);
+	if (write(fd, buf, strlen(buf)) == -1)
+		tst_brkm(TBROK|TERRNO, cleanup, "write");
+	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");
+	if (read(fd, buf, BUFSIZ) < 0)
+		tst_brkm(TBROK|TERRNO, cleanup, "read");
+	close(fd);
+
+	tune = strtoll(buf, &endptr, 10);
+	if (((tune == LONG_MAX || tune == LONG_MIN) && errno == ERANGE) ||
+			(errno != 0 && tune == 0))
+		tst_brkm(TBROK|TERRNO, cleanup, "strtoll");
+	if (endptr == buf || (*endptr != '\0' && *endptr != '\n'))
+		tst_brkm(TBROK, cleanup, "Invalid number parameter: %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];
+	long long val;
+
+	fp = fopen(PATH_MEMINFO, "r");
+	if (fp == NULL)
+		tst_brkm(TBROK|TERRNO, cleanup, "fopen %s", PATH_MEMINFO);
+	while (fgets(line, BUFSIZ, fp) != NULL) {
+		if (sscanf(line, "%s %lld ", 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.6

------------------------------------------------------------------------------
Special Offer -- Download ArcSight Logger for FREE!
Finally, a world-class log management solution at an even better 
price-free! And you'll get a free "Love Thy Logs" t-shirt when you
download Logger. Secure your free ArcSight Logger TODAY!
http://p.sf.net/sfu/arcsisghtdev2dev
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

Reply via email to