• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "mem.h"
31 
verify_oom(void)32 static void verify_oom(void)
33 {
34 #if __WORDSIZE == 32
35 	tst_brk(TCONF, "test is not designed for 32-bit system.");
36 #endif
37 
38 	/* we expect mmap to fail before OOM is hit */
39 	set_sys_tune("overcommit_memory", 2, 1);
40 	oom(NORMAL, 0, ENOMEM, 0);
41 
42 	/* with overcommit_memory set to 0 or 1 there's no
43 	 * guarantee that mmap fails before OOM */
44 	set_sys_tune("overcommit_memory", 0, 1);
45 	oom(NORMAL, 0, ENOMEM, 1);
46 
47 	set_sys_tune("overcommit_memory", 1, 1);
48 	testoom(0, 0, ENOMEM, 1);
49 }
50 
setup(void)51 static void setup(void)
52 {
53 	overcommit = get_sys_tune("overcommit_memory");
54 }
55 
cleanup(void)56 static void cleanup(void)
57 {
58 	if (overcommit != -1)
59 		set_sys_tune("overcommit_memory", overcommit, 0);
60 }
61 
62 static struct tst_test test = {
63 	.needs_root = 1,
64 	.forks_child = 1,
65 	.timeout = -1,
66 	.setup = setup,
67 	.cleanup = cleanup,
68 	.test_all = verify_oom,
69 };
70