Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 1 | /* |
| 2 | * The case is designed to test min_free_kbytes tunable. |
| 3 | * |
| 4 | * The tune is used to control free memory, and system always |
| 5 | * reserve min_free_kbytes memory at least. |
| 6 | * |
| 7 | * Since the tune is not too large or too little, which will |
| 8 | * lead to the system hang, so I choose two cases, and test them |
| 9 | * on all overcommit_memory policy, at the same time, compare |
| 10 | * the current free memory with the tunable value repeatedly. |
| 11 | * |
| 12 | * a) default min_free_kbytes with all overcommit memory policy |
Zhouping Liu | a5c5617 | 2012-08-29 20:42:59 +0800 | [diff] [blame] | 13 | * b) 2x default value with all overcommit memory policy |
| 14 | * c) 5% of MemFree or %2 MemTotal with all overcommit memory policy |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 15 | * |
| 16 | ******************************************************************** |
| 17 | * Copyright (C) 2012 Red Hat, Inc. |
| 18 | * |
| 19 | * This program is free software; you can redistribute it and/or |
| 20 | * modify it under the terms of version 2 of the GNU General Public |
| 21 | * License as published by the Free Software Foundation. |
| 22 | * |
| 23 | * This program is distributed in the hope that it would be useful, |
| 24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 26 | * |
| 27 | * Further, this software is distributed without any warranty that it |
| 28 | * is free of the rightful claim of any third person regarding |
| 29 | * infringement or the like. Any license provided herein, whether |
| 30 | * implied or otherwise, applies only to this software file. Patent |
| 31 | * licenses, if any, provided herein do not apply to combinations of |
| 32 | * this program with other software, or any other product whatsoever. |
| 33 | * |
| 34 | * You should have received a copy of the GNU General Public License |
| 35 | * along with this program; if not, write the Free Software |
| 36 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 37 | * 02110-1301, USA. |
| 38 | * |
| 39 | * ******************************************************************** |
| 40 | */ |
| 41 | |
| 42 | #include <sys/types.h> |
| 43 | #include <sys/mman.h> |
| 44 | #include <sys/wait.h> |
| 45 | #include <errno.h> |
| 46 | #include <fcntl.h> |
| 47 | #include <signal.h> |
| 48 | #include <stdio.h> |
| 49 | #include <stdlib.h> |
| 50 | #include "test.h" |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 51 | #include "mem.h" |
| 52 | |
| 53 | #define MAP_SIZE (1UL<<20) |
| 54 | |
| 55 | volatile int end; |
| 56 | char *TCID = "min_free_kbytes"; |
| 57 | int TST_TOTAL = 1; |
| 58 | static unsigned long default_tune; |
| 59 | static unsigned long orig_overcommit; |
| 60 | static unsigned long total_mem; |
| 61 | |
| 62 | static void test_tune(unsigned long overcommit_policy); |
| 63 | static int eatup_mem(unsigned long overcommit_policy); |
| 64 | static void check_monitor(void); |
| 65 | static void sighandler(int signo LTP_ATTRIBUTE_UNUSED); |
| 66 | |
| 67 | int main(int argc, char *argv[]) |
| 68 | { |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 69 | const char *msg; |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 70 | int lc, pid, status; |
| 71 | struct sigaction sa; |
| 72 | |
| 73 | sa.sa_handler = sighandler; |
| 74 | if (sigemptyset(&sa.sa_mask) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 75 | tst_brkm(TBROK | TERRNO, cleanup, "sigemptyset"); |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 76 | sa.sa_flags = 0; |
| 77 | if (sigaction(SIGUSR1, &sa, NULL) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 78 | tst_brkm(TBROK | TERRNO, cleanup, "sigaction"); |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 79 | |
| 80 | msg = parse_opts(argc, argv, NULL, NULL); |
| 81 | if (msg != NULL) |
| 82 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR -s %s", msg); |
| 83 | setup(); |
| 84 | |
| 85 | switch (pid = fork()) { |
| 86 | case -1: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 87 | tst_brkm(TBROK | TERRNO, cleanup, "fork"); |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 88 | |
| 89 | case 0: |
| 90 | /* startup the check monitor */ |
| 91 | check_monitor(); |
| 92 | exit(0); |
| 93 | } |
| 94 | |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 95 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 96 | tst_count = 0; |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 97 | |
| 98 | test_tune(2); |
| 99 | test_tune(0); |
| 100 | test_tune(1); |
| 101 | } |
| 102 | |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 103 | if (kill(pid, SIGUSR1) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 104 | tst_brkm(TBROK | TERRNO, cleanup, "kill %d", pid); |
| 105 | if (waitpid(pid, &status, WUNTRACED | WCONTINUED) == -1) |
| 106 | tst_brkm(TBROK | TERRNO, cleanup, "waitpid"); |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 107 | if (WIFEXITED(status) && WEXITSTATUS(status) != 0) |
| 108 | tst_resm(TFAIL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 109 | "check_monitor child exit with status: %d", status); |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 110 | |
| 111 | cleanup(); |
| 112 | tst_exit(); |
| 113 | } |
| 114 | |
| 115 | static void test_tune(unsigned long overcommit_policy) |
| 116 | { |
| 117 | int status; |
Zhouping Liu | a5c5617 | 2012-08-29 20:42:59 +0800 | [diff] [blame] | 118 | int pid[3]; |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 119 | int ret, i; |
Zhouping Liu | a5c5617 | 2012-08-29 20:42:59 +0800 | [diff] [blame] | 120 | unsigned long tune, memfree, memtotal; |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 121 | |
| 122 | set_sys_tune("overcommit_memory", overcommit_policy, 1); |
| 123 | |
Zhouping Liu | a5c5617 | 2012-08-29 20:42:59 +0800 | [diff] [blame] | 124 | for (i = 0; i < 3; i++) { |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 125 | /* case1 */ |
| 126 | if (i == 0) |
| 127 | set_sys_tune("min_free_kbytes", default_tune, 1); |
| 128 | /* case2 */ |
Zhouping Liu | a5c5617 | 2012-08-29 20:42:59 +0800 | [diff] [blame] | 129 | else if (i == 1) { |
| 130 | set_sys_tune("min_free_kbytes", 2 * default_tune, 1); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 131 | /* case3 */ |
Zhouping Liu | a5c5617 | 2012-08-29 20:42:59 +0800 | [diff] [blame] | 132 | } else { |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 133 | memfree = read_meminfo("MemFree:"); |
Zhouping Liu | a5c5617 | 2012-08-29 20:42:59 +0800 | [diff] [blame] | 134 | memtotal = read_meminfo("MemTotal:"); |
| 135 | tune = memfree / 20; |
| 136 | if (tune > (memtotal / 50)) |
| 137 | tune = memtotal / 50; |
| 138 | |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 139 | set_sys_tune("min_free_kbytes", tune, 1); |
| 140 | } |
| 141 | |
| 142 | fflush(stdout); |
| 143 | switch (pid[i] = fork()) { |
| 144 | case -1: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 145 | tst_brkm(TBROK | TERRNO, cleanup, "fork"); |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 146 | case 0: |
| 147 | ret = eatup_mem(overcommit_policy); |
| 148 | exit(ret); |
| 149 | } |
| 150 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 151 | if (waitpid(pid[i], &status, WUNTRACED | WCONTINUED) == -1) |
| 152 | tst_brkm(TBROK | TERRNO, cleanup, "waitpid"); |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 153 | |
| 154 | if (overcommit_policy == 2) { |
| 155 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) |
| 156 | tst_resm(TFAIL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 157 | "child unexpectedly failed: %d", |
| 158 | status); |
Zhouping Liu | 4f267b6 | 2012-03-30 22:29:58 +0800 | [diff] [blame] | 159 | } else if (overcommit_policy == 1) { |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 160 | if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGKILL) |
Zhouping Liu | 8c48cfe | 2012-08-29 20:43:59 +0800 | [diff] [blame] | 161 | #if __WORDSIZE == 32 |
| 162 | { |
| 163 | if (total_mem < 3145728UL) |
| 164 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 165 | tst_resm(TFAIL, |
| 166 | "child unexpectedly failed: %d", |
| 167 | status); |
Zhouping Liu | 8c48cfe | 2012-08-29 20:43:59 +0800 | [diff] [blame] | 168 | #if __WORDSIZE == 32 |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 169 | /* in 32-bit system, a process allocate about 3Gb memory at most */ |
Zhouping Liu | 8c48cfe | 2012-08-29 20:43:59 +0800 | [diff] [blame] | 170 | else |
| 171 | tst_resm(TINFO, "Child can't allocate " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 172 | ">3Gb memory in 32bit system"); |
Zhouping Liu | 8c48cfe | 2012-08-29 20:43:59 +0800 | [diff] [blame] | 173 | } |
| 174 | #endif |
Zhouping Liu | 4f267b6 | 2012-03-30 22:29:58 +0800 | [diff] [blame] | 175 | } else { |
| 176 | if (WIFEXITED(status)) { |
| 177 | if (WEXITSTATUS(status) != 0) { |
| 178 | tst_resm(TFAIL, "child unexpectedly " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 179 | "failed: %d", status); |
Zhouping Liu | 4f267b6 | 2012-03-30 22:29:58 +0800 | [diff] [blame] | 180 | } |
| 181 | } else if (!WIFSIGNALED(status) || |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 182 | WTERMSIG(status) != SIGKILL) { |
Zhouping Liu | 4f267b6 | 2012-03-30 22:29:58 +0800 | [diff] [blame] | 183 | tst_resm(TFAIL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 184 | "child unexpectedly failed: %d", |
| 185 | status); |
Zhouping Liu | 4f267b6 | 2012-03-30 22:29:58 +0800 | [diff] [blame] | 186 | } |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | static int eatup_mem(unsigned long overcommit_policy) |
| 192 | { |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 193 | int ret = 0; |
| 194 | unsigned long memfree; |
Zhouping Liu | 56696ac | 2012-08-29 20:43:48 +0800 | [diff] [blame] | 195 | void *addrs; |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 196 | |
| 197 | memfree = read_meminfo("MemFree:"); |
| 198 | printf("memfree is %lu kB before eatup mem\n", memfree); |
Zhouping Liu | 56696ac | 2012-08-29 20:43:48 +0800 | [diff] [blame] | 199 | while (1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 200 | addrs = mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE, |
| 201 | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); |
Zhouping Liu | 56696ac | 2012-08-29 20:43:48 +0800 | [diff] [blame] | 202 | if (addrs == MAP_FAILED) { |
Zhouping Liu | 4f267b6 | 2012-03-30 22:29:58 +0800 | [diff] [blame] | 203 | if (overcommit_policy != 1 && errno != ENOMEM) { |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 204 | perror("mmap"); |
| 205 | ret = -1; |
| 206 | } |
| 207 | break; |
| 208 | } |
Zhouping Liu | 56696ac | 2012-08-29 20:43:48 +0800 | [diff] [blame] | 209 | memset(addrs, 1, MAP_SIZE); |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 210 | } |
| 211 | memfree = read_meminfo("MemFree:"); |
| 212 | printf("memfree is %lu kB after eatup mem\n", memfree); |
| 213 | |
| 214 | return ret; |
| 215 | } |
| 216 | |
| 217 | static void check_monitor(void) |
| 218 | { |
| 219 | unsigned long tune; |
| 220 | unsigned long memfree; |
| 221 | |
| 222 | while (end) { |
| 223 | memfree = read_meminfo("MemFree:"); |
| 224 | tune = get_sys_tune("min_free_kbytes"); |
| 225 | |
| 226 | if (memfree < tune) { |
| 227 | tst_resm(TINFO, "MemFree is %lu kB, " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 228 | "min_free_kbytes is %lu kB", memfree, tune); |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 229 | tst_resm(TFAIL, "MemFree < min_free_kbytes"); |
| 230 | } |
| 231 | |
| 232 | sleep(2); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | static void sighandler(int signo LTP_ATTRIBUTE_UNUSED) |
| 237 | { |
| 238 | end = 1; |
| 239 | } |
| 240 | |
| 241 | void setup(void) |
| 242 | { |
| 243 | tst_require_root(NULL); |
| 244 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 245 | TEST_PAUSE; |
| 246 | |
| 247 | total_mem = read_meminfo("MemTotal:") + read_meminfo("SwapTotal:"); |
| 248 | |
| 249 | default_tune = get_sys_tune("min_free_kbytes"); |
| 250 | orig_overcommit = get_sys_tune("overcommit_memory"); |
| 251 | } |
| 252 | |
| 253 | void cleanup(void) |
| 254 | { |
| 255 | set_sys_tune("min_free_kbytes", default_tune, 0); |
| 256 | set_sys_tune("overcommit_memory", orig_overcommit, 0); |
Zhouping Liu | 8b7cc63 | 2012-03-23 22:12:38 +0800 | [diff] [blame] | 257 | } |