1 /*
2 * Out Of Memory (OOM) for MEMCG and CPUSET
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) 2013 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
32 #include "config.h"
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include "numa_helper.h"
39 #include "test.h"
40 #include "mem.h"
41
42 char *TCID = "oom05";
43 int TST_TOTAL = 1;
44
45 #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
46 && HAVE_MPOL_CONSTANTS
47
main(int argc,char * argv[])48 int main(int argc, char *argv[])
49 {
50 int lc;
51 int swap_acc_on = 1;
52
53 tst_parse_opts(argc, argv, NULL, NULL);
54
55 #if __WORDSIZE == 32
56 tst_brkm(TCONF, NULL, "test is not designed for 32-bit system.");
57 #endif
58
59 setup();
60
61 for (lc = 0; TEST_LOOPING(lc); lc++) {
62 tst_count = 0;
63
64 tst_resm(TINFO, "OOM on CPUSET & MEMCG...");
65 testoom(0, 0, ENOMEM, 1);
66
67 /*
68 * Under NUMA system, the migration of cpuset's memory
69 * is in charge of cpuset.memory_migrate, we can write
70 * 1 to cpuset.memory_migrate to enable the migration.
71 */
72 if (is_numa(cleanup, NH_MEMS, 2)) {
73 write_cpuset_files(CPATH_NEW, "memory_migrate", "1");
74 tst_resm(TINFO, "OOM on CPUSET & MEMCG with "
75 "cpuset.memory_migrate=1");
76 testoom(0, 0, ENOMEM, 1);
77 }
78
79 if (access(MEMCG_SW_LIMIT, F_OK) == -1) {
80 if (errno == ENOENT) {
81 tst_resm(TCONF,
82 "memcg swap accounting is disabled");
83 swap_acc_on = 0;
84 } else
85 tst_brkm(TBROK|TERRNO, cleanup, "access");
86 }
87
88 if (swap_acc_on) {
89 tst_resm(TINFO, "OOM on CPUSET & MEMCG with "
90 "special memswap limitation:");
91 SAFE_FILE_PRINTF(cleanup, MEMCG_SW_LIMIT, "%ld", TESTMEM);
92 testoom(0, 0, ENOMEM, 1);
93
94 tst_resm(TINFO, "OOM on CPUSET & MEMCG with "
95 "disabled memswap limitation:");
96 SAFE_FILE_PRINTF(cleanup, MEMCG_SW_LIMIT, "-1");
97 testoom(0, 0, ENOMEM, 1);
98 }
99 }
100
101 cleanup();
102 tst_exit();
103 }
104
setup(void)105 void setup(void)
106 {
107 int ret, memnode;
108
109 tst_require_root();
110 tst_sig(FORK, DEF_HANDLER, cleanup);
111 TEST_PAUSE;
112
113 if (!is_numa(NULL, NH_MEMS, 1))
114 tst_brkm(TCONF, NULL, "requires NUMA with at least 1 node");
115
116 overcommit = get_sys_tune("overcommit_memory");
117 set_sys_tune("overcommit_memory", 1, 1);
118
119 mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
120 mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW);
121 write_memcg();
122
123 /*
124 * Some nodes do not contain memory, so use
125 * get_allowed_nodes(NH_MEMS) to get a memory
126 * node. This operation also applies to Non-NUMA
127 * systems.
128 */
129 ret = get_allowed_nodes(NH_MEMS, 1, &memnode);
130 if (ret < 0)
131 tst_brkm(TBROK, cleanup, "Failed to get a memory node "
132 "using get_allowed_nodes()");
133 write_cpusets(memnode);
134 }
135
cleanup(void)136 void cleanup(void)
137 {
138 set_sys_tune("overcommit_memory", overcommit, 0);
139 umount_mem(CPATH, CPATH_NEW);
140 umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
141 }
142
143 #else /* no NUMA */
main(void)144 int main(void)
145 {
146 tst_brkm(TCONF, NULL, "no NUMA development packages installed.");
147 }
148 #endif
149