Garrett Cooper | 6a4f572 | 2011-01-18 01:55:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Out Of Memory (OOM) for Memory Resource Controller |
| 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 Zhang | 79667fa | 2012-03-12 14:41:46 +0800 | [diff] [blame] | 33 | #include <errno.h> |
Garrett Cooper | 6a4f572 | 2011-01-18 01:55:30 -0800 | [diff] [blame] | 34 | #include <fcntl.h> |
| 35 | #include <stdio.h> |
Zhouping Liu | 4849bb3 | 2013-03-19 11:40:52 +0800 | [diff] [blame] | 36 | #include "numa_helper.h" |
Garrett Cooper | 6a4f572 | 2011-01-18 01:55:30 -0800 | [diff] [blame] | 37 | #include "test.h" |
Caspar Zhang | 79667fa | 2012-03-12 14:41:46 +0800 | [diff] [blame] | 38 | #include "mem.h" |
Garrett Cooper | 6a4f572 | 2011-01-18 01:55:30 -0800 | [diff] [blame] | 39 | |
| 40 | char *TCID = "oom03"; |
| 41 | int TST_TOTAL = 1; |
Garrett Cooper | 6a4f572 | 2011-01-18 01:55:30 -0800 | [diff] [blame] | 42 | |
Zhouping Liu | 4849bb3 | 2013-03-19 11:40:52 +0800 | [diff] [blame] | 43 | #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \ |
| 44 | && HAVE_MPOL_CONSTANTS |
| 45 | |
Garrett Cooper | 6a4f572 | 2011-01-18 01:55:30 -0800 | [diff] [blame] | 46 | int main(int argc, char *argv[]) |
| 47 | { |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 48 | const char *msg; |
Caspar Zhang | a52957f | 2012-02-27 17:28:41 +0800 | [diff] [blame] | 49 | int lc; |
Garrett Cooper | 6a4f572 | 2011-01-18 01:55:30 -0800 | [diff] [blame] | 50 | |
Caspar Zhang | 05697c4 | 2012-02-20 19:20:57 +0800 | [diff] [blame] | 51 | msg = parse_opts(argc, argv, NULL, NULL); |
| 52 | if (msg != NULL) |
Garrett Cooper | 4bab335 | 2011-01-18 01:59:16 -0800 | [diff] [blame] | 53 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 6a4f572 | 2011-01-18 01:55:30 -0800 | [diff] [blame] | 54 | |
Garrett Cooper | 10e5d88 | 2011-01-20 01:29:53 -0800 | [diff] [blame] | 55 | #if __WORDSIZE == 32 |
Garrett Cooper | 4bab335 | 2011-01-18 01:59:16 -0800 | [diff] [blame] | 56 | tst_brkm(TCONF, NULL, "test is not designed for 32-bit system."); |
| 57 | #endif |
Garrett Cooper | 6a4f572 | 2011-01-18 01:55:30 -0800 | [diff] [blame] | 58 | |
| 59 | setup(); |
| 60 | |
| 61 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 62 | tst_count = 0; |
Garrett Cooper | 6a4f572 | 2011-01-18 01:55:30 -0800 | [diff] [blame] | 63 | |
Zhouping Liu | e096339 | 2013-04-23 15:12:55 +0800 | [diff] [blame] | 64 | SAFE_FILE_PRINTF(cleanup, MEMCG_PATH_NEW "/tasks", |
| 65 | "%d", getpid()); |
| 66 | SAFE_FILE_PRINTF(cleanup, MEMCG_LIMIT, "%ld", TESTMEM); |
Caspar Zhang | a52957f | 2012-02-27 17:28:41 +0800 | [diff] [blame] | 67 | |
Jan Stancek | 30ac84c | 2014-07-25 09:58:47 +0200 | [diff] [blame] | 68 | testoom(0, 0, ENOMEM, 1); |
Garrett Cooper | 6a4f572 | 2011-01-18 01:55:30 -0800 | [diff] [blame] | 69 | |
Zhouping Liu | 3f8c002 | 2012-03-19 18:28:15 +0800 | [diff] [blame] | 70 | if (access(MEMCG_SW_LIMIT, F_OK) == -1) { |
| 71 | if (errno == ENOENT) |
| 72 | tst_resm(TCONF, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 73 | "memcg swap accounting is disabled"); |
Zhouping Liu | 3f8c002 | 2012-03-19 18:28:15 +0800 | [diff] [blame] | 74 | else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 75 | tst_brkm(TBROK | TERRNO, cleanup, "access"); |
Zhouping Liu | 3f8c002 | 2012-03-19 18:28:15 +0800 | [diff] [blame] | 76 | } else { |
Zhouping Liu | e096339 | 2013-04-23 15:12:55 +0800 | [diff] [blame] | 77 | SAFE_FILE_PRINTF(cleanup, MEMCG_SW_LIMIT, |
| 78 | "%ld", TESTMEM); |
Jan Stancek | 30ac84c | 2014-07-25 09:58:47 +0200 | [diff] [blame] | 79 | testoom(0, 1, ENOMEM, 1); |
Zhouping Liu | 3f8c002 | 2012-03-19 18:28:15 +0800 | [diff] [blame] | 80 | } |
Zhouping Liu | 4849bb3 | 2013-03-19 11:40:52 +0800 | [diff] [blame] | 81 | |
| 82 | /* OOM for MEMCG with mempolicy */ |
| 83 | if (is_numa(cleanup)) { |
| 84 | tst_resm(TINFO, "OOM on MEMCG & mempolicy..."); |
Jan Stancek | 30ac84c | 2014-07-25 09:58:47 +0200 | [diff] [blame] | 85 | testoom(MPOL_BIND, 0, ENOMEM, 1); |
| 86 | testoom(MPOL_INTERLEAVE, 0, ENOMEM, 1); |
| 87 | testoom(MPOL_PREFERRED, 0, ENOMEM, 1); |
Zhouping Liu | 4849bb3 | 2013-03-19 11:40:52 +0800 | [diff] [blame] | 88 | } |
Garrett Cooper | 6a4f572 | 2011-01-18 01:55:30 -0800 | [diff] [blame] | 89 | } |
| 90 | cleanup(); |
Garrett Cooper | a11ad22 | 2011-01-18 02:02:55 -0800 | [diff] [blame] | 91 | tst_exit(); |
Garrett Cooper | 6a4f572 | 2011-01-18 01:55:30 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void setup(void) |
| 95 | { |
Garrett Cooper | a11ad22 | 2011-01-18 02:02:55 -0800 | [diff] [blame] | 96 | tst_require_root(NULL); |
Garrett Cooper | 6a4f572 | 2011-01-18 01:55:30 -0800 | [diff] [blame] | 97 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 98 | TEST_PAUSE; |
| 99 | |
Caspar Zhang | ff74ba5 | 2012-02-27 16:13:18 +0800 | [diff] [blame] | 100 | overcommit = get_sys_tune("overcommit_memory"); |
| 101 | set_sys_tune("overcommit_memory", 1, 1); |
Garrett Cooper | 6a4f572 | 2011-01-18 01:55:30 -0800 | [diff] [blame] | 102 | mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW); |
| 103 | } |
| 104 | |
| 105 | void cleanup(void) |
| 106 | { |
Caspar Zhang | ff74ba5 | 2012-02-27 16:13:18 +0800 | [diff] [blame] | 107 | set_sys_tune("overcommit_memory", overcommit, 0); |
Garrett Cooper | 6a4f572 | 2011-01-18 01:55:30 -0800 | [diff] [blame] | 108 | umount_mem(MEMCG_PATH, MEMCG_PATH_NEW); |
Garrett Cooper | 6a4f572 | 2011-01-18 01:55:30 -0800 | [diff] [blame] | 109 | } |
Zhouping Liu | 4849bb3 | 2013-03-19 11:40:52 +0800 | [diff] [blame] | 110 | |
| 111 | #else |
| 112 | int main(void) |
| 113 | { |
| 114 | tst_brkm(TCONF, NULL, "no NUMA development packages installed."); |
| 115 | } |
| 116 | #endif |