1 /*
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-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 <sys/types.h>
24 #include <sys/stat.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include "lapi/abisize.h"
31 #include "mem.h"
32
verify_oom(void)33 static void verify_oom(void)
34 {
35 #ifdef TST_ABI32
36 tst_brk(TCONF, "test is not designed for 32-bit system.");
37 #endif
38
39 /* we expect mmap to fail before OOM is hit */
40 set_sys_tune("overcommit_memory", 2, 1);
41 oom(NORMAL, 0, ENOMEM, 0);
42
43 /* with overcommit_memory set to 0 or 1 there's no
44 * guarantee that mmap fails before OOM */
45 set_sys_tune("overcommit_memory", 0, 1);
46 oom(NORMAL, 0, ENOMEM, 1);
47
48 set_sys_tune("overcommit_memory", 1, 1);
49 testoom(0, 0, ENOMEM, 1);
50 }
51
setup(void)52 static void setup(void)
53 {
54 overcommit = get_sys_tune("overcommit_memory");
55 }
56
cleanup(void)57 static void cleanup(void)
58 {
59 if (overcommit != -1)
60 set_sys_tune("overcommit_memory", overcommit, 0);
61 }
62
63 static struct tst_test test = {
64 .needs_root = 1,
65 .forks_child = 1,
66 .timeout = -1,
67 .setup = setup,
68 .cleanup = cleanup,
69 .test_all = verify_oom,
70 };
71