Hi, Cyril
v4 -> v5: updated message. please comment it.
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 <[email protected]>
---
testcases/kernel/mem/include/mem.h | 7 +++
testcases/kernel/mem/lib/mem.c | 81 +++++++++++++++++++++++++++++++++++-
2 files changed, 87 insertions(+), 1 deletions(-)
--
Thanks,
Zhouping Liu
From 069624b1d9d61935455dc512467671dac6d55277 Mon Sep 17 00:00:00 2001
From: Zhouping Liu <[email protected]>
Date: Sat, 10 Sep 2011 00:59:01 +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 <[email protected]>
---
testcases/kernel/mem/include/mem.h | 7 +++
testcases/kernel/mem/lib/mem.c | 81 +++++++++++++++++++++++++++++++++++-
2 files changed, 87 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..0afe933 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,82 @@ 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];
+ 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
------------------------------------------------------------------------------
Why Cloud-Based Security and Archiving Make Sense
Osterman Research conducted this study that outlines how and why cloud
computing security and archiving is rapidly being adopted across the IT
space for its ease of implementation, lower cost, and increased
reliability. Learn more. http://www.accelacomm.com/jaw/sfnl/114/51425301/
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list