• 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 "lapi/abisize.h"
34 #include "numa_helper.h"
35 #include "mem.h"
36 
37 #ifdef HAVE_NUMA_V2
38 
verify_oom(void)39 static void verify_oom(void)
40 {
41 #ifdef TST_ABI32
42 	tst_brk(TCONF, "test is not designed for 32-bit system.");
43 #endif
44 
45 	tst_res(TINFO, "OOM on CPUSET & MEMCG...");
46 	testoom(0, 0, ENOMEM, 1);
47 
48 	/*
49 	 * Under NUMA system, the migration of cpuset's memory
50 	 * is in charge of cpuset.memory_migrate, we can write
51 	 * 1 to cpuset.memory_migrate to enable the migration.
52 	 */
53 	if (is_numa(NULL, NH_MEMS, 2) &&
54 	    SAFE_CG_HAS(tst_cg, "cpuset.memory_migrate")) {
55 		SAFE_CG_PRINT(tst_cg, "cpuset.memory_migrate", "1");
56 		tst_res(TINFO, "OOM on CPUSET & MEMCG with "
57 				"cpuset.memory_migrate=1");
58 		testoom(0, 0, ENOMEM, 1);
59 	}
60 
61 	if (SAFE_CG_HAS(tst_cg, "memory.swap.max")) {
62 		tst_res(TINFO, "OOM on CPUSET & MEMCG with "
63 				"special memswap limitation:");
64 		if (!TST_CG_VER_IS_V1(tst_cg, "memory"))
65 			SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", MB);
66 		else
67 			SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", TESTMEM + MB);
68 
69 		testoom(0, 1, ENOMEM, 1);
70 
71 		tst_res(TINFO, "OOM on CPUSET & MEMCG with "
72 				"disabled memswap limitation:");
73 		if (TST_CG_VER_IS_V1(tst_cg, "memory"))
74 			SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", ~0UL);
75 		else
76 			SAFE_CG_PRINT(tst_cg, "memory.swap.max", "max");
77 		testoom(0, 0, ENOMEM, 1);
78 	}
79 }
80 
setup(void)81 void setup(void)
82 {
83 	int ret, memnode;
84 
85 	if (!is_numa(NULL, NH_MEMS, 1))
86 		tst_brk(TCONF, "requires NUMA with at least 1 node");
87 
88 	overcommit = get_sys_tune("overcommit_memory");
89 	set_sys_tune("overcommit_memory", 1, 1);
90 
91 	/*
92 	 * Some nodes do not contain memory, so use
93 	 * get_allowed_nodes(NH_MEMS) to get a memory
94 	 * node. This operation also applies to Non-NUMA
95 	 * systems.
96 	 */
97 	ret = get_allowed_nodes(NH_MEMS, 1, &memnode);
98 	if (ret < 0)
99 		tst_brk(TBROK, "Failed to get a memory node "
100 			      "using get_allowed_nodes()");
101 
102 	write_cpusets(tst_cg, memnode);
103 	SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
104 	SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
105 }
106 
cleanup(void)107 void cleanup(void)
108 {
109 	if (overcommit != -1)
110 		set_sys_tune("overcommit_memory", overcommit, 0);
111 }
112 
113 static struct tst_test test = {
114 	.needs_root = 1,
115 	.forks_child = 1,
116 	.max_runtime = TST_UNLIMITED_RUNTIME,
117 	.setup = setup,
118 	.cleanup = cleanup,
119 	.test_all = verify_oom,
120 	.needs_cgroup_ctrls = (const char *const []){
121 		"memory", "cpuset", NULL
122 	},
123 };
124 
125 #else
126 	TST_TEST_TCONF(NUMA_ERROR_MSG);
127 #endif
128