blob: bcd88b5d4b37d6938a94c1cd0361cce27b428d2c [file] [log] [blame]
Garrett Cooperddf33082011-01-18 01:54:46 -08001/*
Zhouping Liu4849bb32013-03-19 11:40:52 +08002 * Out Of Memory (OOM) for CPUSET
Garrett Cooperddf33082011-01-18 01:54:46 -08003 *
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 */
Caspar Zhang79667fa2012-03-12 14:41:46 +080031
32#include "config.h"
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <errno.h>
36#include <fcntl.h>
37#include <stdio.h>
Zhouping Liu4849bb32013-03-19 11:40:52 +080038#include "numa_helper.h"
Garrett Cooperddf33082011-01-18 01:54:46 -080039#include "test.h"
Caspar Zhang79667fa2012-03-12 14:41:46 +080040#include "mem.h"
Garrett Cooperddf33082011-01-18 01:54:46 -080041
42char *TCID = "oom04";
43int TST_TOTAL = 1;
Garrett Cooperddf33082011-01-18 01:54:46 -080044
45#if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
46 && HAVE_MPOL_CONSTANTS
Zhouping Liu4849bb32013-03-19 11:40:52 +080047
Garrett Cooperddf33082011-01-18 01:54:46 -080048int main(int argc, char *argv[])
49{
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020050 const char *msg;
Caspar Zhanga52957f2012-02-27 17:28:41 +080051 int lc;
Garrett Cooperddf33082011-01-18 01:54:46 -080052
Caspar Zhang05697c42012-02-20 19:20:57 +080053 msg = parse_opts(argc, argv, NULL, NULL);
54 if (msg != NULL)
Garrett Cooper4bab3352011-01-18 01:59:16 -080055 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooperddf33082011-01-18 01:54:46 -080056
Garrett Cooper10e5d882011-01-20 01:29:53 -080057#if __WORDSIZE == 32
Garrett Cooper4bab3352011-01-18 01:59:16 -080058 tst_brkm(TCONF, NULL, "test is not designed for 32-bit system.");
59#endif
Garrett Cooperddf33082011-01-18 01:54:46 -080060
Garrett Cooperddf33082011-01-18 01:54:46 -080061 setup();
62
63 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080064 tst_count = 0;
Garrett Cooperddf33082011-01-18 01:54:46 -080065
Zhouping Liu4849bb32013-03-19 11:40:52 +080066 tst_resm(TINFO, "OOM on CPUSET...");
Jan Stancek30ac84c2014-07-25 09:58:47 +020067 testoom(0, 0, ENOMEM, 1);
Caspar Zhanga52957f2012-02-27 17:28:41 +080068
Zhouping Liu4849bb32013-03-19 11:40:52 +080069 if (is_numa(cleanup)) {
70 /*
71 * Under NUMA system, the migration of cpuset's memory
72 * is in charge of cpuset.memory_migrate, we can write
73 * 1 to cpuset.memory_migrate to enable the migration.
74 */
75 write_cpuset_files(CPATH_NEW,
76 "memory_migrate", "1");
77 tst_resm(TINFO, "OOM on CPUSET with mem migrate:");
Jan Stancek30ac84c2014-07-25 09:58:47 +020078 testoom(0, 0, ENOMEM, 1);
Zhouping Liu3f8c0022012-03-19 18:28:15 +080079 }
Garrett Cooperddf33082011-01-18 01:54:46 -080080 }
81 cleanup();
Caspar Zhange53335c2011-09-15 16:17:40 +080082 tst_exit();
Garrett Cooperddf33082011-01-18 01:54:46 -080083}
84
85void setup(void)
86{
Zhouping Liu630f90d2013-04-29 14:07:17 +080087 int memnode, ret;
88
Garrett Coopera11ad222011-01-18 02:02:55 -080089 tst_require_root(NULL);
Garrett Cooperddf33082011-01-18 01:54:46 -080090 tst_sig(FORK, DEF_HANDLER, cleanup);
91 TEST_PAUSE;
92
Caspar Zhangff74ba52012-02-27 16:13:18 +080093 overcommit = get_sys_tune("overcommit_memory");
94 set_sys_tune("overcommit_memory", 1, 1);
Zhouping Liu4849bb32013-03-19 11:40:52 +080095
Garrett Cooperddf33082011-01-18 01:54:46 -080096 mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW);
Zhouping Liu630f90d2013-04-29 14:07:17 +080097
98 /*
99 * Some nodes do not contain memory, so use
100 * get_allowed_nodes(NH_MEMS) to get a memory
101 * node. This operation also applies to Non-NUMA
102 * systems.
103 */
104 ret = get_allowed_nodes(NH_MEMS, 1, &memnode);
105 if (ret < 0)
106 tst_brkm(TBROK, NULL, "Failed to get a memory node "
107 "using get_allowed_nodes()");
108 write_cpusets(memnode);
Garrett Cooperddf33082011-01-18 01:54:46 -0800109}
110
Garrett Cooperddf33082011-01-18 01:54:46 -0800111void cleanup(void)
112{
Caspar Zhangff74ba52012-02-27 16:13:18 +0800113 set_sys_tune("overcommit_memory", overcommit, 0);
Garrett Cooperddf33082011-01-18 01:54:46 -0800114 umount_mem(CPATH, CPATH_NEW);
Garrett Cooperddf33082011-01-18 01:54:46 -0800115}
116
117#else /* no NUMA */
Garrett Cooperecd667e2011-01-19 01:06:18 -0800118int main(void)
119{
Caspar Zhang05697c42012-02-20 19:20:57 +0800120 tst_brkm(TCONF, NULL, "no NUMA development packages installed.");
Garrett Cooperddf33082011-01-18 01:54:46 -0800121}
122#endif