/proc/sys/vm/overcommit_memory contains a flag that control memory
overcommitment.
- When this flag is 0, the kernel attempts to estimate the amount
  of free memory left when userspace requests more memory.
- When this flag is 1, the kernel pretends there is always enough
  memory until it actually runs out.
- When this flag is 2, the kernel uses a "never overcommit" policy
  that attempts to prevent any overcommit of memory.

This feature can be very useful because there are a lot of programs
that malloc() huge amounts of memory "just-in-case" and don't use
much of it.

The default value is 0.

memory allocation limit =
    swap_total + (mem_total * overcommit_ratio / 100)

The case is designed to test the feature of this tunable file.

Signed-off-by: Zhouping Liu <[email protected]>
---
 testcases/kernel/mem/tunable/Makefile            |   42 ++++
 testcases/kernel/mem/tunable/overcommit_memory.c |  230 ++++++++++++++++++++++
 2 files changed, 272 insertions(+), 0 deletions(-)
 create mode 100644 testcases/kernel/mem/tunable/Makefile
 create mode 100644 testcases/kernel/mem/tunable/overcommit_memory.c

-- 
Thanks,
Zhouping Liu
From 5dcb854baf674b45e85b6a93117d257e971d4795 Mon Sep 17 00:00:00 2001
From: Zhouping Liu <[email protected]>
Date: Wed, 24 Aug 2011 18:37:33 +0800
Subject: [PATCH 2/2] mem/tunable: add overcommit_memory new testcase

/proc/sys/vm/overcommit_memory contains a flag that control memory
overcommitment.
- When this flag is 0, the kernel attempts to estimate the amount
  of free memory left when userspace requests more memory.
- When this flag is 1, the kernel pretends there is always enough
  memory until it actually runs out.
- When this flag is 2, the kernel uses a "never overcommit" policy
  that attempts to prevent any overcommit of memory.

This feature can be very useful because there are a lot of programs
that malloc() huge amounts of memory "just-in-case" and don't use
much of it.

The default value in system is 0.

memory allocation limit =
    swap_total + (mem_total * overcommit_ratio / 100)

The case is desinged to test the feature of this tunable file.

Signed-off-by: Zhouping Liu <[email protected]>
---
 testcases/kernel/mem/tunable/Makefile            |   42 ++++
 testcases/kernel/mem/tunable/overcommit_memory.c |  230 ++++++++++++++++++++++
 2 files changed, 272 insertions(+), 0 deletions(-)
 create mode 100644 testcases/kernel/mem/tunable/Makefile
 create mode 100644 testcases/kernel/mem/tunable/overcommit_memory.c

diff --git a/testcases/kernel/mem/tunable/Makefile b/testcases/kernel/mem/tunable/Makefile
new file mode 100644
index 0000000..8ab52da
--- /dev/null
+++ b/testcases/kernel/mem/tunable/Makefile
@@ -0,0 +1,42 @@
+#
+#  Copyright (C) 2011  Red Hat, Inc.
+#
+#  This program is free software;  you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation; either version 2 of the License, or
+#  (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY;  without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+#  the GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program;  if not, write to the Free Software
+#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+
+top_srcdir              ?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+LDLIBS			+= $(NUMA_LIBS) -lmem
+LIBDIR			:= ../lib
+LIB			:= $(LIBDIR)/libmem.a
+FILTER_OUT_DIRS		:= $(LIBDIR)
+LDFLAGS			+= -L$(LIBDIR)
+
+$(LIBDIR):
+	mkdir -p "$@"
+
+$(LIB): $(LIBDIR)
+	$(MAKE) -C $^ -f "$(abs_srcdir)/$^/Makefile" all
+
+MAKE_DEPS		:= $(LIB)
+
+trunk-clean:: | lib-clean
+
+lib-clean:: $(LIBDIR)
+	$(MAKE) -C $^ -f "$(abs_srcdir)/$^/Makefile" clean
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/mem/tunable/overcommit_memory.c b/testcases/kernel/mem/tunable/overcommit_memory.c
new file mode 100644
index 0000000..057394f
--- /dev/null
+++ b/testcases/kernel/mem/tunable/overcommit_memory.c
@@ -0,0 +1,230 @@
+/*
+ * /proc/sys/vm/overcommit_memory tunable file test
+ *
+ * Author: Zhouping Liu <[email protected]>
+ * Description:
+ * /proc/sys/vm/overcommit_memory contains a flag that control memory
+ * overcommitment.
+ * - When this flag is 0, the kernel attempts to estimate the amount
+ *   of free memory left when userspace requests more memory.
+ * - When this flag is 1, the kernel pretends there is always enough
+ *   memory until it actually runs out.
+ * - When this flag is 2, the kernel uses a "never overcommit" policy
+ *   that attempts to prevent any overcommit of memory.
+ *
+ * This feature can be very useful because there are a lot of programs
+ * that malloc() huge amounts of memory "just-in-case" and don't use
+ * much of it.
+ *
+ * The default value in system is 0.
+ *
+ * memory allocation limit =
+ *     swap_total + (mem_total * overcommit_ratio / 100)
+ *
+ * The case is desinged to test the feature of this tunable file.
+ *
+ * ********************************************************************
+ * Copyright (C) 2011 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Further, this software is distributed without any warranty that it
+ * is free of the rightful claim of any third person regarding
+ * infringement or the like.  Any license provided herein, whether
+ * implied or otherwise, applies only to this software file.  Patent
+ * licenses, if any, provided herein do not apply to combinations of
+ * this program with other software, or any other product whatsoever.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ * ********************************************************************
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include "test.h"
+#include "usctest.h"
+#include "../include/mem.h"
+#define FILE_OVER_MEM		PATH_SYSVM "overcommit_memory"
+#define FILE_OVER_RATIO		PATH_SYSVM "overcommit_ratio"
+#define DEFAULT_OVER_RATIO	50
+#define NORMAL_OVERCOMMIT	0
+#define ENOUGH_OVERCOMMIT	1
+#define NEVER_OVERCOMMIT	2
+#define EXPECT_PASS		0
+#define EXPECT_FAIL		1
+
+char *TCID = "overcommit_memory";
+static long old_overcommit_memory;
+static long old_overcommit_ratio;
+static long overcommit_ratio;
+static unsigned long mem_limit;
+
+static void overcommit_memory_test(void);
+static void set_mem_limit(void);
+static int heavy_malloc(unsigned long size);
+static void alloc_and_check(unsigned long size, int expect_result);
+
+int main(int argc, char *argv[])
+{
+	char *msg;
+	int lc;
+
+	setup();
+
+	msg = parse_opts(argc, argv, NULL, NULL);
+	if (msg != NULL)
+		tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR -%s ", msg);
+
+	for (lc = 0; TEST_LOOPING(lc); lc++) {
+		Tst_count = 0;
+
+		overcommit_memory_test();
+	}
+
+	cleanup();
+	tst_exit();
+}
+
+void setup(void)
+{
+	tst_require_root(NULL);
+
+	tst_sig(NOFORK, DEF_HANDLER, cleanup);
+
+	TEST_PAUSE;
+
+	if (access(FILE_OVER_MEM, F_OK) != 0 ||
+		access(FILE_OVER_RATIO, F_OK) != 0)
+		tst_brkm(TCONF, NULL, "The system "
+			"can't support to test %s", TCID);
+
+	old_overcommit_memory = get_sys_tune(FILE_OVER_MEM);
+	old_overcommit_ratio = get_sys_tune(FILE_OVER_RATIO);
+
+	set_sys_tune(FILE_OVER_RATIO, DEFAULT_OVER_RATIO);
+	check_sys_tune(FILE_OVER_RATIO, DEFAULT_OVER_RATIO);
+	overcommit_ratio = DEFAULT_OVER_RATIO;
+	set_mem_limit();
+}
+
+void cleanup(void)
+{
+	set_sys_tune(FILE_OVER_MEM, old_overcommit_memory);
+	set_sys_tune(FILE_OVER_RATIO, old_overcommit_ratio);
+
+	TEST_CLEANUP;
+}
+
+static void overcommit_memory_test(void)
+{
+	/*
+	 * set overcommit_memory 0 <NORMAL OVERCOMMIT>
+	 * in this mode, you can alloc memory less than or equal the
+	 * limit of memory, because the kernel attempts to estimate
+	 * the amount of free memory left.
+	 */
+	set_sys_tune(FILE_OVER_MEM, NORMAL_OVERCOMMIT);
+	check_sys_tune(FILE_OVER_MEM, NORMAL_OVERCOMMIT);
+	tst_resm(TINFO, "starting test overcommit_memory is %d "
+		"<NORMAL OVERCOMMIT>", NORMAL_OVERCOMMIT);
+	/* mem_limit/2 is less than the size of mem limit */
+	alloc_and_check(mem_limit / 2, EXPECT_PASS);
+	/* mem_limit is equal to the size of mem limit */
+	alloc_and_check(mem_limit, EXPECT_PASS);
+	/* mem_limit*2 is larger than the size of mem limit */
+	alloc_and_check(mem_limit * 2, EXPECT_FAIL);
+
+	/*
+	 * set overcommit_memory 1 <ENOUGH OVERCOMMIT>
+	 * in this mode, you can alloc over the limit of memory,
+	 * because the kernel pretends there is always enough memory
+	 * until it actually runs out.
+	 */
+	set_sys_tune(FILE_OVER_MEM, ENOUGH_OVERCOMMIT);
+	check_sys_tune(FILE_OVER_MEM, ENOUGH_OVERCOMMIT);
+	tst_resm(TINFO, "starting test overcommit_memory is %d "
+		"<ENOUGH OVERCOMMIT>", ENOUGH_OVERCOMMIT);
+	alloc_and_check(mem_limit / 2, EXPECT_PASS);
+	alloc_and_check(mem_limit, EXPECT_PASS);
+	alloc_and_check(mem_limit * 2, EXPECT_PASS);
+
+	/*
+	 * set overcommit_memory 2 <NEVER OVERCOMMIT>
+	 * in this mode, you can just only alloc memory less than
+	 * the limit of memory, because the kernle users a
+	 * "never overcommit" policy that attempts to prevent
+	 * any overcomit of memory.
+	 */
+	set_sys_tune(FILE_OVER_MEM, NEVER_OVERCOMMIT);
+	check_sys_tune(FILE_OVER_MEM, NEVER_OVERCOMMIT);
+	tst_resm(TINFO, "starting test overcommit_memory is %d "
+			"<NEVER OVERCOMMIT>", NEVER_OVERCOMMIT);
+	alloc_and_check(mem_limit / 2, EXPECT_PASS);
+	alloc_and_check(mem_limit, EXPECT_FAIL);
+	alloc_and_check(mem_limit * 2, EXPECT_FAIL);
+}
+
+static void set_mem_limit(void)
+{
+	unsigned long mem_total, swap_total;
+
+	mem_total  = read_meminfo("MemTotal:");
+	tst_resm(TINFO, "MemTotal = %ld kB", mem_total);
+
+	swap_total = read_meminfo("SwapTotal:");
+	tst_resm(TINFO, "SwapTotal = %ld kB", swap_total);
+
+	mem_limit = swap_total + (mem_total * overcommit_ratio / 100UL);
+	tst_resm(TINFO, "The limit of memory is set to %lu kB", mem_limit);
+}
+
+static int heavy_malloc(unsigned long size)
+{
+	char *p;
+
+	p = (char *)malloc(size);
+	if (p != NULL) {
+		tst_resm(TINFO, "malloc %lu Bytes successfully", size);
+		free(p);
+		return 0;
+	} else {
+		tst_resm(TINFO, "malloc %lu Bytes failed", size);
+		return 1;
+	}
+}
+
+static void alloc_and_check(unsigned long size, int expect_result)
+{
+	int result;
+
+	tst_resm(TINFO, "try to alloc memory = %lu kB", size);
+	result = heavy_malloc(size * KB);
+
+	switch (expect_result) {
+	case EXPECT_PASS:
+		if (result == 0)
+			tst_resm(TPASS, "alloc passed as expected");
+		else
+			tst_resm(TFAIL, "alloc failed, expected to pass");
+		break;
+	case EXPECT_FAIL:
+		if (result != 0)
+			tst_resm(TPASS, "alloc failed as expected");
+		else
+			tst_resm(TFAIL, "alloc passed, expected to fail");
+		break;
+	default:
+		tst_brkm(TBROK, cleanup, "Invaild numbler parameter: %d",
+				expect_result);
+	}
+}
-- 
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
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

Reply via email to