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
52 static struct tst_test test = {
53 .needs_root = 1,
54 .forks_child = 1,
55 .max_runtime = TST_UNLIMITED_RUNTIME,
56 .test_all = verify_oom,
57 .save_restore = (const struct tst_path_val[]) {
58 {"/proc/sys/vm/overcommit_memory", NULL, TST_SR_TBROK},
59 {}
60 },
61 };
62