blob: 8c11ac2ddc401d3a7ea7f6d910aa56602c9239e0 [file] [log] [blame]
Garrett Cooper6a4f5722011-01-18 01:55:30 -08001/*
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 Red Hat, Inc.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General Public
13 * License as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it would be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * Further, this software is distributed without any warranty that it
20 * is free of the rightful claim of any third person regarding
21 * infringement or the like. Any license provided herein, whether
22 * implied or otherwise, applies only to this software file. Patent
23 * licenses, if any, provided herein do not apply to combinations of
24 * this program with other software, or any other product whatsoever.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 * 02110-1301, USA.
30 */
31#include <sys/types.h>
32#include <sys/stat.h>
Caspar Zhang79667fa2012-03-12 14:41:46 +080033#include <errno.h>
Garrett Cooper6a4f5722011-01-18 01:55:30 -080034#include <fcntl.h>
35#include <stdio.h>
Zhouping Liu4849bb32013-03-19 11:40:52 +080036#include "numa_helper.h"
Garrett Cooper6a4f5722011-01-18 01:55:30 -080037#include "test.h"
Caspar Zhang79667fa2012-03-12 14:41:46 +080038#include "mem.h"
Garrett Cooper6a4f5722011-01-18 01:55:30 -080039
40char *TCID = "oom03";
41int TST_TOTAL = 1;
Garrett Cooper6a4f5722011-01-18 01:55:30 -080042
Zhouping Liu4849bb32013-03-19 11:40:52 +080043#if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
44 && HAVE_MPOL_CONSTANTS
45
Garrett Cooper6a4f5722011-01-18 01:55:30 -080046int main(int argc, char *argv[])
47{
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020048 const char *msg;
Caspar Zhanga52957f2012-02-27 17:28:41 +080049 int lc;
Garrett Cooper6a4f5722011-01-18 01:55:30 -080050
Caspar Zhang05697c42012-02-20 19:20:57 +080051 msg = parse_opts(argc, argv, NULL, NULL);
52 if (msg != NULL)
Garrett Cooper4bab3352011-01-18 01:59:16 -080053 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper6a4f5722011-01-18 01:55:30 -080054
Garrett Cooper10e5d882011-01-20 01:29:53 -080055#if __WORDSIZE == 32
Garrett Cooper4bab3352011-01-18 01:59:16 -080056 tst_brkm(TCONF, NULL, "test is not designed for 32-bit system.");
57#endif
Garrett Cooper6a4f5722011-01-18 01:55:30 -080058
59 setup();
60
61 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080062 tst_count = 0;
Garrett Cooper6a4f5722011-01-18 01:55:30 -080063
Zhouping Liue0963392013-04-23 15:12:55 +080064 SAFE_FILE_PRINTF(cleanup, MEMCG_PATH_NEW "/tasks",
65 "%d", getpid());
66 SAFE_FILE_PRINTF(cleanup, MEMCG_LIMIT, "%ld", TESTMEM);
Caspar Zhanga52957f2012-02-27 17:28:41 +080067
Jan Stancek30ac84c2014-07-25 09:58:47 +020068 testoom(0, 0, ENOMEM, 1);
Garrett Cooper6a4f5722011-01-18 01:55:30 -080069
Zhouping Liu3f8c0022012-03-19 18:28:15 +080070 if (access(MEMCG_SW_LIMIT, F_OK) == -1) {
71 if (errno == ENOENT)
72 tst_resm(TCONF,
Wanlong Gao354ebb42012-12-07 10:10:04 +080073 "memcg swap accounting is disabled");
Zhouping Liu3f8c0022012-03-19 18:28:15 +080074 else
Wanlong Gao354ebb42012-12-07 10:10:04 +080075 tst_brkm(TBROK | TERRNO, cleanup, "access");
Zhouping Liu3f8c0022012-03-19 18:28:15 +080076 } else {
Zhouping Liue0963392013-04-23 15:12:55 +080077 SAFE_FILE_PRINTF(cleanup, MEMCG_SW_LIMIT,
78 "%ld", TESTMEM);
Jan Stancek30ac84c2014-07-25 09:58:47 +020079 testoom(0, 1, ENOMEM, 1);
Zhouping Liu3f8c0022012-03-19 18:28:15 +080080 }
Zhouping Liu4849bb32013-03-19 11:40:52 +080081
82 /* OOM for MEMCG with mempolicy */
83 if (is_numa(cleanup)) {
84 tst_resm(TINFO, "OOM on MEMCG & mempolicy...");
Jan Stancek30ac84c2014-07-25 09:58:47 +020085 testoom(MPOL_BIND, 0, ENOMEM, 1);
86 testoom(MPOL_INTERLEAVE, 0, ENOMEM, 1);
87 testoom(MPOL_PREFERRED, 0, ENOMEM, 1);
Zhouping Liu4849bb32013-03-19 11:40:52 +080088 }
Garrett Cooper6a4f5722011-01-18 01:55:30 -080089 }
90 cleanup();
Garrett Coopera11ad222011-01-18 02:02:55 -080091 tst_exit();
Garrett Cooper6a4f5722011-01-18 01:55:30 -080092}
93
94void setup(void)
95{
Garrett Coopera11ad222011-01-18 02:02:55 -080096 tst_require_root(NULL);
Garrett Cooper6a4f5722011-01-18 01:55:30 -080097 tst_sig(FORK, DEF_HANDLER, cleanup);
98 TEST_PAUSE;
99
Caspar Zhangff74ba52012-02-27 16:13:18 +0800100 overcommit = get_sys_tune("overcommit_memory");
101 set_sys_tune("overcommit_memory", 1, 1);
Garrett Cooper6a4f5722011-01-18 01:55:30 -0800102 mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
103}
104
105void cleanup(void)
106{
Caspar Zhangff74ba52012-02-27 16:13:18 +0800107 set_sys_tune("overcommit_memory", overcommit, 0);
Garrett Cooper6a4f5722011-01-18 01:55:30 -0800108 umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
Garrett Cooper6a4f5722011-01-18 01:55:30 -0800109}
Zhouping Liu4849bb32013-03-19 11:40:52 +0800110
111#else
112int main(void)
113{
114 tst_brkm(TCONF, NULL, "no NUMA development packages installed.");
115}
116#endif