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 "numa_helper.h"
34 #include "mem.h"
35
36 #ifdef HAVE_NUMA_V2
37
verify_oom(void)38 static void verify_oom(void)
39 {
40 #if __WORDSIZE == 32
41 tst_brk(TCONF, "test is not designed for 32-bit system.");
42 #endif
43
44 tst_res(TINFO, "OOM on CPUSET...");
45 testoom(0, 0, ENOMEM, 1);
46
47 if (is_numa(NULL, NH_MEMS, 2)) {
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 write_cpuset_files(CPATH_NEW, "memory_migrate", "1");
54 tst_res(TINFO, "OOM on CPUSET with mem migrate:");
55 testoom(0, 0, ENOMEM, 1);
56 }
57 }
58
setup(void)59 static void setup(void)
60 {
61 int memnode, ret;
62
63 if (!is_numa(NULL, NH_MEMS, 1))
64 tst_brk(TCONF, "requires NUMA with at least 1 node");
65
66 overcommit = get_sys_tune("overcommit_memory");
67 set_sys_tune("overcommit_memory", 1, 1);
68
69 mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW);
70
71 /*
72 * Some nodes do not contain memory, so use
73 * get_allowed_nodes(NH_MEMS) to get a memory
74 * node. This operation also applies to Non-NUMA
75 * systems.
76 */
77 ret = get_allowed_nodes(NH_MEMS, 1, &memnode);
78 if (ret < 0)
79 tst_brk(TBROK, "Failed to get a memory node "
80 "using get_allowed_nodes()");
81 write_cpusets(memnode);
82 }
83
cleanup(void)84 static void cleanup(void)
85 {
86 set_sys_tune("overcommit_memory", overcommit, 0);
87 umount_mem(CPATH, CPATH_NEW);
88 }
89
90 static struct tst_test test = {
91 .needs_root = 1,
92 .forks_child = 1,
93 .timeout = -1,
94 .setup = setup,
95 .cleanup = cleanup,
96 .test_all = verify_oom,
97 };
98
99 #else
100 TST_TEST_TCONF("test requires libnuma >= 2 and it's development packages");
101 #endif
102