• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-2017  Red Hat, Inc.
11  *
12  * This program is free software;  you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
20  * the GNU General Public License for more details.
21  */
22 
23 #include "config.h"
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #if HAVE_NUMA_H
30 #include <numa.h>
31 #endif
32 
33 #include "numa_helper.h"
34 #include "mem.h"
35 
36 #ifdef HAVE_NUMA_V2
37 
38 static int memcg_mounted;
39 static int cpuset_mounted;
40 
verify_oom(void)41 static void verify_oom(void)
42 {
43 	int swap_acc_on = 1;
44 
45 #if __WORDSIZE == 32
46 	tst_brk(TCONF, "test is not designed for 32-bit system.");
47 #endif
48 
49 	tst_res(TINFO, "OOM on CPUSET & MEMCG...");
50 	testoom(0, 0, ENOMEM, 1);
51 
52 	/*
53 	 * Under NUMA system, the migration of cpuset's memory
54 	 * is in charge of cpuset.memory_migrate, we can write
55 	 * 1 to cpuset.memory_migrate to enable the migration.
56 	 */
57 	if (is_numa(NULL, NH_MEMS, 2)) {
58 		write_cpuset_files(CPATH_NEW, "memory_migrate", "1");
59 		tst_res(TINFO, "OOM on CPUSET & MEMCG with "
60 				"cpuset.memory_migrate=1");
61 		testoom(0, 0, ENOMEM, 1);
62 	}
63 
64 	if (access(MEMCG_SW_LIMIT, F_OK) == -1) {
65 		if (errno == ENOENT) {
66 			tst_res(TCONF, "memcg swap accounting is disabled");
67 			swap_acc_on = 0;
68 		} else
69 			tst_brk(TBROK|TERRNO, "access");
70 	}
71 
72 	if (swap_acc_on) {
73 		tst_res(TINFO, "OOM on CPUSET & MEMCG with "
74 				"special memswap limitation:");
75 		SAFE_FILE_PRINTF(MEMCG_SW_LIMIT, "%ld", TESTMEM);
76 		testoom(0, 0, ENOMEM, 1);
77 
78 		tst_res(TINFO, "OOM on CPUSET & MEMCG with "
79 				"disabled memswap limitation:");
80 		SAFE_FILE_PRINTF(MEMCG_SW_LIMIT, "-1");
81 		testoom(0, 0, ENOMEM, 1);
82 	}
83 }
84 
setup(void)85 void setup(void)
86 {
87 	int ret, memnode;
88 
89 	if (!is_numa(NULL, NH_MEMS, 1))
90 		tst_brk(TCONF, "requires NUMA with at least 1 node");
91 
92 	overcommit = get_sys_tune("overcommit_memory");
93 	set_sys_tune("overcommit_memory", 1, 1);
94 
95 	mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
96 	memcg_mounted = 1;
97 	mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW);
98 	cpuset_mounted = 1;
99 	write_memcg();
100 
101 	/*
102 	 * Some nodes do not contain memory, so use
103 	 * get_allowed_nodes(NH_MEMS) to get a memory
104 	 * node. This operation also applies to Non-NUMA
105 	 * systems.
106 	 */
107 	ret = get_allowed_nodes(NH_MEMS, 1, &memnode);
108 	if (ret < 0)
109 		tst_brk(TBROK, "Failed to get a memory node "
110 			      "using get_allowed_nodes()");
111 	write_cpusets(memnode);
112 }
113 
cleanup(void)114 void cleanup(void)
115 {
116 	if (overcommit != -1)
117 		set_sys_tune("overcommit_memory", overcommit, 0);
118 	if (cpuset_mounted)
119 		umount_mem(CPATH, CPATH_NEW);
120 	if (memcg_mounted)
121 		umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
122 }
123 
124 static struct tst_test test = {
125 	.needs_root = 1,
126 	.forks_child = 1,
127 	.timeout = -1,
128 	.setup = setup,
129 	.cleanup = cleanup,
130 	.test_all = verify_oom,
131 };
132 
133 #else
134 	TST_TEST_TCONF(NUMA_ERROR_MSG);
135 #endif
136