blob: f5214ae118e0595bb7eedb2f32c3ce21081e22e8 [file] [log] [blame]
Garrett Cooper0db55072011-01-18 01:33:07 -08001/*
2 * Out Of Memory (OOM)
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 Cooper0db55072011-01-18 01:33:07 -080034#include <fcntl.h>
35#include <stdio.h>
Garrett Cooper0db55072011-01-18 01:33:07 -080036#include <stdlib.h>
37#include <unistd.h>
38#include "test.h"
Caspar Zhang79667fa2012-03-12 14:41:46 +080039#include "mem.h"
Garrett Cooper0db55072011-01-18 01:33:07 -080040
41char *TCID = "oom01";
42int TST_TOTAL = 1;
Garrett Cooper0db55072011-01-18 01:33:07 -080043
44int main(int argc, char *argv[])
45{
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020046 const char *msg;
Caspar Zhangff74ba52012-02-27 16:13:18 +080047 int lc;
Garrett Cooper0db55072011-01-18 01:33:07 -080048
Caspar Zhang05697c42012-02-20 19:20:57 +080049 msg = parse_opts(argc, argv, NULL, NULL);
50 if (msg != NULL)
Garrett Cooper4bab3352011-01-18 01:59:16 -080051 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper0db55072011-01-18 01:33:07 -080052
Garrett Cooper10e5d882011-01-20 01:29:53 -080053#if __WORDSIZE == 32
Garrett Cooper4bab3352011-01-18 01:59:16 -080054 tst_brkm(TCONF, NULL, "test is not designed for 32-bit system.");
55#endif
Garrett Cooper0db55072011-01-18 01:33:07 -080056
57 setup();
58
59 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080060 tst_count = 0;
Caspar Zhangff74ba52012-02-27 16:13:18 +080061
Jan Stancek30ac84c2014-07-25 09:58:47 +020062 /* we expect mmap to fail before OOM is hit */
Caspar Zhangff74ba52012-02-27 16:13:18 +080063 set_sys_tune("overcommit_memory", 2, 1);
Jan Stancek30ac84c2014-07-25 09:58:47 +020064 oom(OVERCOMMIT, 0, ENOMEM, 0);
Garrett Cooper0db55072011-01-18 01:33:07 -080065
Jan Stancek30ac84c2014-07-25 09:58:47 +020066 /* with overcommit_memory set to 0 or 1 there's no
67 * guarantee that mmap fails before OOM */
Caspar Zhangff74ba52012-02-27 16:13:18 +080068 set_sys_tune("overcommit_memory", 0, 1);
Jan Stancek30ac84c2014-07-25 09:58:47 +020069 oom(OVERCOMMIT, 0, ENOMEM, 1);
Garrett Cooper0db55072011-01-18 01:33:07 -080070
Caspar Zhangff74ba52012-02-27 16:13:18 +080071 set_sys_tune("overcommit_memory", 1, 1);
Jan Stancek30ac84c2014-07-25 09:58:47 +020072 testoom(0, 0, ENOMEM, 1);
Garrett Cooper0db55072011-01-18 01:33:07 -080073 }
74 cleanup();
Garrett Coopera11ad222011-01-18 02:02:55 -080075 tst_exit();
Garrett Cooper0db55072011-01-18 01:33:07 -080076}
77
78void setup(void)
79{
Garrett Coopera11ad222011-01-18 02:02:55 -080080 tst_require_root(NULL);
Garrett Cooper0db55072011-01-18 01:33:07 -080081 tst_sig(FORK, DEF_HANDLER, cleanup);
82 TEST_PAUSE;
83
Caspar Zhangff74ba52012-02-27 16:13:18 +080084 overcommit = get_sys_tune("overcommit_memory");
Garrett Cooper0db55072011-01-18 01:33:07 -080085}
86
87void cleanup(void)
88{
Caspar Zhangff74ba52012-02-27 16:13:18 +080089 set_sys_tune("overcommit_memory", overcommit, 0);
Garrett Cooper0db55072011-01-18 01:33:07 -080090}