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