• 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 	/*
89 	 * Some nodes do not contain memory, so use
90 	 * get_allowed_nodes(NH_MEMS) to get a memory
91 	 * node. This operation also applies to Non-NUMA
92 	 * systems.
93 	 */
94 	ret = get_allowed_nodes(NH_MEMS, 1, &memnode);
95 	if (ret < 0)
96 		tst_brk(TBROK, "Failed to get a memory node "
97 			      "using get_allowed_nodes()");
98 
99 	write_cpusets(tst_cg, memnode);
100 	SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
101 	SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
102 }
103 
104 static struct tst_test test = {
105 	.needs_root = 1,
106 	.forks_child = 1,
107 	.max_runtime = TST_UNLIMITED_RUNTIME,
108 	.setup = setup,
109 	.test_all = verify_oom,
110 	.needs_cgroup_ctrls = (const char *const []){
111 		"memory", "cpuset", NULL
112 	},
113 	.save_restore = (const struct tst_path_val[]) {
114 		{"/proc/sys/vm/overcommit_memory", "1", TST_SR_TBROK},
115 		{}
116 	},
117 };
118 
119 #else
120 	TST_TEST_TCONF(NUMA_ERROR_MSG);
121 #endif
122