• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Out Of Memory (OOM) for Memory Resource Controller
3  *
4  * The program is designed to cope with unpredictable like amount and
5  * system physical memory, swap size and other VMM technology like KSM,
6  * memcg, memory hotplug and so on which may affect the OOM
7  * behaviours. It simply increase the memory consumption 3G each time
8  * until all the available memory is consumed and OOM is triggered.
9  *
10  * Copyright (C) 2010  Red Hat, Inc.
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of version 2 of the GNU General Public
13  * License as published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it would be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  * Further, this software is distributed without any warranty that it
20  * is free of the rightful claim of any third person regarding
21  * infringement or the like.  Any license provided herein, whether
22  * implied or otherwise, applies only to this software file.  Patent
23  * licenses, if any, provided herein do not apply to combinations of
24  * this program with other software, or any other product whatsoever.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write the Free Software
28  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29  * 02110-1301, USA.
30  */
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <stdio.h>
36 #include "numa_helper.h"
37 #include "test.h"
38 #include "mem.h"
39 
40 char *TCID = "oom03";
41 int TST_TOTAL = 1;
42 
43 #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
44 	&& HAVE_MPOL_CONSTANTS
45 
main(int argc,char * argv[])46 int main(int argc, char *argv[])
47 {
48 	int lc;
49 
50 	tst_parse_opts(argc, argv, NULL, NULL);
51 
52 #if __WORDSIZE == 32
53 	tst_brkm(TCONF, NULL, "test is not designed for 32-bit system.");
54 #endif
55 
56 	setup();
57 
58 	for (lc = 0; TEST_LOOPING(lc); lc++) {
59 		tst_count = 0;
60 
61 		SAFE_FILE_PRINTF(cleanup, MEMCG_PATH_NEW "/tasks",
62 				 "%d", getpid());
63 		SAFE_FILE_PRINTF(cleanup, MEMCG_LIMIT, "%ld", TESTMEM);
64 
65 		testoom(0, 0, ENOMEM, 1);
66 
67 		if (access(MEMCG_SW_LIMIT, F_OK) == -1) {
68 			if (errno == ENOENT)
69 				tst_resm(TCONF,
70 					 "memcg swap accounting is disabled");
71 			else
72 				tst_brkm(TBROK | TERRNO, cleanup, "access");
73 		} else {
74 			SAFE_FILE_PRINTF(cleanup, MEMCG_SW_LIMIT,
75 					 "%ld", TESTMEM);
76 			testoom(0, 1, ENOMEM, 1);
77 		}
78 
79 		/* OOM for MEMCG with mempolicy */
80 		if (is_numa(cleanup, NH_MEMS, 2)) {
81 			tst_resm(TINFO, "OOM on MEMCG & mempolicy...");
82 			testoom(MPOL_BIND, 0, ENOMEM, 1);
83 			testoom(MPOL_INTERLEAVE, 0, ENOMEM, 1);
84 			testoom(MPOL_PREFERRED, 0, ENOMEM, 1);
85 		}
86 	}
87 	cleanup();
88 	tst_exit();
89 }
90 
setup(void)91 void setup(void)
92 {
93 	tst_require_root();
94 	tst_sig(FORK, DEF_HANDLER, cleanup);
95 	TEST_PAUSE;
96 
97 	overcommit = get_sys_tune("overcommit_memory");
98 	set_sys_tune("overcommit_memory", 1, 1);
99 	mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
100 }
101 
cleanup(void)102 void cleanup(void)
103 {
104 	set_sys_tune("overcommit_memory", overcommit, 0);
105 	umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
106 }
107 
108 #else
main(void)109 int main(void)
110 {
111 	tst_brkm(TCONF, NULL, "no NUMA development packages installed.");
112 }
113 #endif
114