• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Out Of Memory (OOM) for 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) 2010-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 	tst_res(TINFO, "OOM on CPUSET...");
45 	testoom(0, 0, ENOMEM, 1);
46 
47 	if (is_numa(NULL, NH_MEMS, 2) &&
48 	    SAFE_CG_HAS(tst_cg, "cpuset.memory_migrate")) {
49 		/*
50 		 * Under NUMA system, the migration of cpuset's memory
51 		 * is in charge of cpuset.memory_migrate, we can write
52 		 * 1 to cpuset.memory_migrate to enable the migration.
53 		 */
54 		SAFE_CG_PRINT(tst_cg, "cpuset.memory_migrate", "1");
55 
56 		tst_res(TINFO, "OOM on CPUSET with mem migrate:");
57 		testoom(0, 0, ENOMEM, 1);
58 	}
59 }
60 
setup(void)61 static void setup(void)
62 {
63 	int memnode, ret;
64 
65 	if (!is_numa(NULL, NH_MEMS, 1))
66 		tst_brk(TCONF, "requires NUMA with at least 1 node");
67 
68 	/*
69 	 * Some nodes do not contain memory, so use
70 	 * get_allowed_nodes(NH_MEMS) to get a memory
71 	 * node. This operation also applies to Non-NUMA
72 	 * systems.
73 	 */
74 	ret = get_allowed_nodes(NH_MEMS, 1, &memnode);
75 	if (ret < 0)
76 		tst_brk(TBROK, "Failed to get a memory node "
77 			      "using get_allowed_nodes()");
78 	write_cpusets(tst_cg, memnode);
79 	SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
80 }
81 
82 static struct tst_test test = {
83 	.needs_root = 1,
84 	.forks_child = 1,
85 	.max_runtime = TST_UNLIMITED_RUNTIME,
86 	.setup = setup,
87 	.test_all = verify_oom,
88 	.needs_cgroup_ctrls = (const char *const []){ "cpuset", NULL },
89 	.save_restore = (const struct tst_path_val[]) {
90 		{"/proc/sys/vm/overcommit_memory", "1", TST_SR_TBROK},
91 		{}
92 	},
93 };
94 
95 #else
96 	TST_TEST_TCONF(NUMA_ERROR_MSG);
97 #endif
98