1 /*
2 * Out Of Memory (OOM) for Memory Resource Controller
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
38 static int memcg_mounted;
39
verify_oom(void)40 static void verify_oom(void)
41 {
42 #if __WORDSIZE == 32
43 tst_brk(TCONF, "test is not designed for 32-bit system.");
44 #endif
45
46 SAFE_FILE_PRINTF(MEMCG_PATH_NEW "/tasks", "%d", getpid());
47 SAFE_FILE_PRINTF(MEMCG_LIMIT, "%ld", TESTMEM);
48
49 testoom(0, 0, ENOMEM, 1);
50
51 if (access(MEMCG_SW_LIMIT, F_OK) == -1) {
52 if (errno == ENOENT)
53 tst_res(TCONF,
54 "memcg swap accounting is disabled");
55 else
56 tst_brk(TBROK | TERRNO, "access");
57 } else {
58 SAFE_FILE_PRINTF(MEMCG_SW_LIMIT, "%ld", TESTMEM);
59 testoom(0, 1, ENOMEM, 1);
60 }
61
62 /* OOM for MEMCG with mempolicy */
63 if (is_numa(NULL, NH_MEMS, 2)) {
64 tst_res(TINFO, "OOM on MEMCG & mempolicy...");
65 testoom(MPOL_BIND, 0, ENOMEM, 1);
66 testoom(MPOL_INTERLEAVE, 0, ENOMEM, 1);
67 testoom(MPOL_PREFERRED, 0, ENOMEM, 1);
68 }
69 }
70
setup(void)71 static void setup(void)
72 {
73 overcommit = get_sys_tune("overcommit_memory");
74 set_sys_tune("overcommit_memory", 1, 1);
75 mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
76 memcg_mounted = 1;
77 }
78
cleanup(void)79 static void cleanup(void)
80 {
81 if (overcommit != -1)
82 set_sys_tune("overcommit_memory", overcommit, 0);
83 if (memcg_mounted)
84 umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
85 }
86
87 static struct tst_test test = {
88 .needs_root = 1,
89 .forks_child = 1,
90 .timeout = -1,
91 .setup = setup,
92 .cleanup = cleanup,
93 .test_all = verify_oom,
94 };
95
96 #else
97 TST_TEST_TCONF(NUMA_ERROR_MSG);
98 #endif
99