Skip to content

Commit

Permalink
mem: make use of save_restore to simplify code
Browse files Browse the repository at this point in the history
Signed-off-by: Li Wang <liwang@redhat.com>
Reviewed-by: Petr Vorel <pvorel@suse.cz>
  • Loading branch information
wangli5665 committed Jun 23, 2023
1 parent acf4611 commit 8da6a94
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 80 deletions.
1 change: 0 additions & 1 deletion testcases/kernel/mem/include/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ static inline void clean_node(unsigned long *array)
#define MLOCK 2
#define KSM 3

extern long overcommit;
void oom(int testcase, int lite, int retcode, int allow_sigkill);
void testoom(int mempolicy, int lite, int retcode, int allow_sigkill);

Expand Down
2 changes: 0 additions & 2 deletions testcases/kernel/mem/lib/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

/* OOM */

long overcommit = -1;

static int alloc_mem(long int length, int testcase)
{
char *s;
Expand Down
17 changes: 4 additions & 13 deletions testcases/kernel/mem/oom/oom01.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,13 @@ static void verify_oom(void)
testoom(0, 0, ENOMEM, 1);
}

static void setup(void)
{
overcommit = get_sys_tune("overcommit_memory");
}

static void cleanup(void)
{
if (overcommit != -1)
set_sys_tune("overcommit_memory", overcommit, 0);
}

static struct tst_test test = {
.needs_root = 1,
.forks_child = 1,
.max_runtime = TST_UNLIMITED_RUNTIME,
.setup = setup,
.cleanup = cleanup,
.test_all = verify_oom,
.save_restore = (const struct tst_path_val[]) {
{"/proc/sys/vm/overcommit_memory", NULL, TST_SR_TBROK},
{}
},
};
14 changes: 4 additions & 10 deletions testcases/kernel/mem/oom/oom02.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,18 @@ static void setup(void)
{
if (!is_numa(NULL, NH_MEMS, 2))
tst_brk(TCONF, "The case need a NUMA system.");

overcommit = get_sys_tune("overcommit_memory");
set_sys_tune("overcommit_memory", 1, 1);
}

static void cleanup(void)
{
if (overcommit != -1)
set_sys_tune("overcommit_memory", overcommit, 0);
}

static struct tst_test test = {
.needs_root = 1,
.forks_child = 1,
.max_runtime = TST_UNLIMITED_RUNTIME,
.setup = setup,
.cleanup = cleanup,
.test_all = verify_oom,
.save_restore = (const struct tst_path_val[]) {
{"/proc/sys/vm/overcommit_memory", "1", TST_SR_TBROK},
{}
},
};

#else
Expand Down
14 changes: 4 additions & 10 deletions testcases/kernel/mem/oom/oom03.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,21 @@ static void verify_oom(void)

static void setup(void)
{
overcommit = get_sys_tune("overcommit_memory");
set_sys_tune("overcommit_memory", 1, 1);

SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
}

static void cleanup(void)
{
if (overcommit != -1)
set_sys_tune("overcommit_memory", overcommit, 0);
}

static struct tst_test test = {
.needs_root = 1,
.forks_child = 1,
.max_runtime = TST_UNLIMITED_RUNTIME,
.setup = setup,
.cleanup = cleanup,
.test_all = verify_oom,
.needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
.save_restore = (const struct tst_path_val[]) {
{"/proc/sys/vm/overcommit_memory", "1", TST_SR_TBROK},
{}
},
};

#else
Expand Down
14 changes: 4 additions & 10 deletions testcases/kernel/mem/oom/oom04.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ static void setup(void)
if (!is_numa(NULL, NH_MEMS, 1))
tst_brk(TCONF, "requires NUMA with at least 1 node");

overcommit = get_sys_tune("overcommit_memory");
set_sys_tune("overcommit_memory", 1, 1);

/*
* Some nodes do not contain memory, so use
* get_allowed_nodes(NH_MEMS) to get a memory
Expand All @@ -82,20 +79,17 @@ static void setup(void)
SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
}

static void cleanup(void)
{
if (overcommit != -1)
set_sys_tune("overcommit_memory", overcommit, 0);
}

static struct tst_test test = {
.needs_root = 1,
.forks_child = 1,
.max_runtime = TST_UNLIMITED_RUNTIME,
.setup = setup,
.cleanup = cleanup,
.test_all = verify_oom,
.needs_cgroup_ctrls = (const char *const []){ "cpuset", NULL },
.save_restore = (const struct tst_path_val[]) {
{"/proc/sys/vm/overcommit_memory", "1", TST_SR_TBROK},
{}
},
};

#else
Expand Down
14 changes: 4 additions & 10 deletions testcases/kernel/mem/oom/oom05.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ void setup(void)
if (!is_numa(NULL, NH_MEMS, 1))
tst_brk(TCONF, "requires NUMA with at least 1 node");

overcommit = get_sys_tune("overcommit_memory");
set_sys_tune("overcommit_memory", 1, 1);

/*
* Some nodes do not contain memory, so use
* get_allowed_nodes(NH_MEMS) to get a memory
Expand All @@ -104,22 +101,19 @@ void setup(void)
SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
}

void cleanup(void)
{
if (overcommit != -1)
set_sys_tune("overcommit_memory", overcommit, 0);
}

static struct tst_test test = {
.needs_root = 1,
.forks_child = 1,
.max_runtime = TST_UNLIMITED_RUNTIME,
.setup = setup,
.cleanup = cleanup,
.test_all = verify_oom,
.needs_cgroup_ctrls = (const char *const []){
"memory", "cpuset", NULL
},
.save_restore = (const struct tst_path_val[]) {
{"/proc/sys/vm/overcommit_memory", "1", TST_SR_TBROK},
{}
},
};

#else
Expand Down
9 changes: 4 additions & 5 deletions testcases/kernel/mem/tunable/max_map_count.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,16 @@
#define MAX_MAP_COUNT 65536L

static long old_max_map_count = -1;
static long old_overcommit = -1;

static void setup(void)
{
SAFE_ACCESS(PATH_SYSVM "max_map_count", F_OK);

old_max_map_count = get_sys_tune("max_map_count");
old_overcommit = get_sys_tune("overcommit_memory");
set_sys_tune("overcommit_memory", 0, 1);
}

static void cleanup(void)
{
if (old_overcommit != -1)
set_sys_tune("overcommit_memory", old_overcommit, 0);
if (old_max_map_count != -1)
set_sys_tune("max_map_count", old_max_map_count, 0);
}
Expand Down Expand Up @@ -213,4 +208,8 @@ static struct tst_test test = {
.setup = setup,
.cleanup = cleanup,
.test_all = max_map_count_test,
.save_restore = (const struct tst_path_val[]) {
{"/proc/sys/vm/overcommit_memory", "0", TST_SR_TBROK},
{}
},
};
8 changes: 4 additions & 4 deletions testcases/kernel/mem/tunable/min_free_kbytes.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@

volatile int end;
static long default_tune = -1;
static long orig_overcommit = -1;
static unsigned long total_mem;

static void test_tune(unsigned long overcommit_policy);
Expand Down Expand Up @@ -217,15 +216,12 @@ static void setup(void)
total_mem = SAFE_READ_MEMINFO("MemTotal:") + SAFE_READ_MEMINFO("SwapTotal:");

default_tune = get_sys_tune("min_free_kbytes");
orig_overcommit = get_sys_tune("overcommit_memory");
}

static void cleanup(void)
{
if (default_tune != -1)
set_sys_tune("min_free_kbytes", default_tune, 0);
if (orig_overcommit != -1)
set_sys_tune("overcommit_memory", orig_overcommit, 0);
}

static struct tst_test test = {
Expand All @@ -235,4 +231,8 @@ static struct tst_test test = {
.setup = setup,
.cleanup = cleanup,
.test_all = min_free_kbytes_test,
.save_restore = (const struct tst_path_val[]) {
{"/proc/sys/vm/overcommit_memory", NULL, TST_SR_TBROK},
{}
},
};
20 changes: 5 additions & 15 deletions testcases/kernel/mem/tunable/overcommit_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
#define EXPECT_FAIL 1

static char *R_opt;
static long old_overcommit_memory = -1;
static long old_overcommit_ratio = -1;
static long overcommit_ratio;
static long sum_total;
Expand All @@ -90,16 +89,11 @@ static void setup(void)
long mem_total, swap_total;
struct rlimit lim;

if (access(PATH_SYSVM "overcommit_memory", F_OK) == -1 ||
access(PATH_SYSVM "overcommit_ratio", F_OK) == -1)
tst_brk(TCONF, "system doesn't support overcommit_memory");

if (R_opt)
overcommit_ratio = SAFE_STRTOL(R_opt, 0, LONG_MAX);
else
overcommit_ratio = DEFAULT_OVER_RATIO;

old_overcommit_memory = get_sys_tune("overcommit_memory");
old_overcommit_ratio = get_sys_tune("overcommit_ratio");

mem_total = SAFE_READ_MEMINFO("MemTotal:");
Expand Down Expand Up @@ -128,14 +122,6 @@ static void setup(void)
tst_res(TINFO, "TotalBatchSize is %ld kB", total_batch_size);
}

static void cleanup(void)
{
if (old_overcommit_memory != -1)
set_sys_tune("overcommit_memory", old_overcommit_memory, 0);
if (old_overcommit_ratio != -1)
set_sys_tune("overcommit_ratio", old_overcommit_ratio, 0);
}

static void overcommit_memory_test(void)
{

Expand Down Expand Up @@ -269,6 +255,10 @@ static struct tst_test test = {
{}
},
.setup = setup,
.cleanup = cleanup,
.test_all = overcommit_memory_test,
.save_restore = (const struct tst_path_val[]) {
{"/proc/sys/vm/overcommit_memory", NULL, TST_SR_TBROK},
{"/proc/sys/vm/overcommit_ratio", NULL, TST_SR_TBROK},
{}
},
};

0 comments on commit 8da6a94

Please sign in to comment.