Hi,
v1->v2: update the message info
please review it, 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 int 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 int 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>
---
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 685b996b408d0886d2b0daf7f319a300fe9a7559 Mon Sep 17 00:00:00 2001
From: Zhouping Liu <z...@redhat.com>
Date: Mon, 29 Aug 2011 12:15:47 +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 int 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 int 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>
---
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..e5e6c44 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 tune);
+long get_sys_tune(char *sys_file);
+void check_sys_tune(char *sys_file, long tune);
+unsigned long read_meminfo(char *item);
#endif
diff --git a/testcases/kernel/mem/lib/mem.c b/testcases/kernel/mem/lib/mem.c
index f3983d4..04ffa24 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 tune)
+{
+ int fd;
+ char buf[BUFSIZ];
+
+ tst_resm(TINFO, "try to set %s to %ld", sys_file, tune);
+
+ fd = open(sys_file, O_WRONLY);
+ if (fd == -1)
+ tst_brkm(TBROK|TERRNO, cleanup, "open");
+ sprintf(buf, "%ld", tune);
+ if (write(fd, buf, strlen(buf)) == -1)
+ tst_brkm(TBROK|TERRNO, cleanup, "write");
+ close(fd);
+}
+
+long get_sys_tune(char *sys_file)
+{
+ int fd;
+ 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 = strtol(buf, &endptr, 10);
+ if (((tune == LONG_MAX || tune == LONG_MIN) && errno == ERANGE) ||
+ (errno != 0 && tune == 0))
+ tst_brkm(TBROK|TERRNO, cleanup, "strtol");
+ 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 tune)
+{
+ long val;
+
+ val = get_sys_tune(sys_file);
+ if (val == tune)
+ tst_resm(TINFO, "confirmed %s = %ld", sys_file, tune);
+ else
+ tst_brkm(TBROK, cleanup, "%s = %ld, is not %ld",
+ sys_file, val, tune);
+}
+
+unsigned long read_meminfo(char *item)
+{
+ FILE *fp;
+ char line[BUFSIZ], buf[BUFSIZ];
+ unsigned 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 %lu ", 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
------------------------------------------------------------------------------
EMC VNX: the world's simplest storage, starting under $10K
The only unified storage solution that offers unified management
Up to 160% more powerful than alternatives and 25% more efficient.
Guaranteed. http://p.sf.net/sfu/emc-vnx-dev2dev
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list