blob: 493c37e3b30a45a4a64bbde696161a02e6c4ad95 [file] [log] [blame]
Zhouping Liu8b7cc632012-03-23 22:12:38 +08001/*
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 Liua5c56172012-08-29 20:42:59 +080013 * b) 2x default value with all overcommit memory policy
14 * c) 5% of MemFree or %2 MemTotal with all overcommit memory policy
Zhouping Liu8b7cc632012-03-23 22:12:38 +080015 *
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 Liu8b7cc632012-03-23 22:12:38 +080051#include "mem.h"
52
53#define MAP_SIZE (1UL<<20)
54
55volatile int end;
56char *TCID = "min_free_kbytes";
57int TST_TOTAL = 1;
58static unsigned long default_tune;
59static unsigned long orig_overcommit;
60static unsigned long total_mem;
61
62static void test_tune(unsigned long overcommit_policy);
63static int eatup_mem(unsigned long overcommit_policy);
64static void check_monitor(void);
65static void sighandler(int signo LTP_ATTRIBUTE_UNUSED);
66
67int main(int argc, char *argv[])
68{
Zhouping Liu8b7cc632012-03-23 22:12:38 +080069 int lc, pid, status;
70 struct sigaction sa;
71
Cyril Hrubisd6d11d02015-03-09 17:35:43 +010072 tst_parse_opts(argc, argv, NULL, NULL);
73
Zhouping Liu8b7cc632012-03-23 22:12:38 +080074 sa.sa_handler = sighandler;
75 if (sigemptyset(&sa.sa_mask) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +080076 tst_brkm(TBROK | TERRNO, cleanup, "sigemptyset");
Zhouping Liu8b7cc632012-03-23 22:12:38 +080077 sa.sa_flags = 0;
78 if (sigaction(SIGUSR1, &sa, NULL) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +080079 tst_brkm(TBROK | TERRNO, cleanup, "sigaction");
Zhouping Liu8b7cc632012-03-23 22:12:38 +080080
Zhouping Liu8b7cc632012-03-23 22:12:38 +080081 setup();
82
83 switch (pid = fork()) {
84 case -1:
Wanlong Gao354ebb42012-12-07 10:10:04 +080085 tst_brkm(TBROK | TERRNO, cleanup, "fork");
Zhouping Liu8b7cc632012-03-23 22:12:38 +080086
87 case 0:
88 /* startup the check monitor */
89 check_monitor();
90 exit(0);
91 }
92
Zhouping Liu8b7cc632012-03-23 22:12:38 +080093 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080094 tst_count = 0;
Zhouping Liu8b7cc632012-03-23 22:12:38 +080095
96 test_tune(2);
97 test_tune(0);
98 test_tune(1);
99 }
100
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800101 if (kill(pid, SIGUSR1) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800102 tst_brkm(TBROK | TERRNO, cleanup, "kill %d", pid);
103 if (waitpid(pid, &status, WUNTRACED | WCONTINUED) == -1)
104 tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800105 if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
106 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800107 "check_monitor child exit with status: %d", status);
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800108
109 cleanup();
110 tst_exit();
111}
112
113static void test_tune(unsigned long overcommit_policy)
114{
115 int status;
Zhouping Liua5c56172012-08-29 20:42:59 +0800116 int pid[3];
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800117 int ret, i;
Zhouping Liua5c56172012-08-29 20:42:59 +0800118 unsigned long tune, memfree, memtotal;
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800119
120 set_sys_tune("overcommit_memory", overcommit_policy, 1);
121
Zhouping Liua5c56172012-08-29 20:42:59 +0800122 for (i = 0; i < 3; i++) {
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800123 /* case1 */
124 if (i == 0)
125 set_sys_tune("min_free_kbytes", default_tune, 1);
126 /* case2 */
Zhouping Liua5c56172012-08-29 20:42:59 +0800127 else if (i == 1) {
128 set_sys_tune("min_free_kbytes", 2 * default_tune, 1);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800129 /* case3 */
Zhouping Liua5c56172012-08-29 20:42:59 +0800130 } else {
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800131 memfree = read_meminfo("MemFree:");
Zhouping Liua5c56172012-08-29 20:42:59 +0800132 memtotal = read_meminfo("MemTotal:");
133 tune = memfree / 20;
134 if (tune > (memtotal / 50))
135 tune = memtotal / 50;
136
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800137 set_sys_tune("min_free_kbytes", tune, 1);
138 }
139
140 fflush(stdout);
141 switch (pid[i] = fork()) {
142 case -1:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800143 tst_brkm(TBROK | TERRNO, cleanup, "fork");
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800144 case 0:
145 ret = eatup_mem(overcommit_policy);
146 exit(ret);
147 }
148
Wanlong Gao354ebb42012-12-07 10:10:04 +0800149 if (waitpid(pid[i], &status, WUNTRACED | WCONTINUED) == -1)
150 tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800151
152 if (overcommit_policy == 2) {
153 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
154 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800155 "child unexpectedly failed: %d",
156 status);
Zhouping Liu4f267b62012-03-30 22:29:58 +0800157 } else if (overcommit_policy == 1) {
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800158 if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGKILL)
Zhouping Liu8c48cfe2012-08-29 20:43:59 +0800159#if __WORDSIZE == 32
160 {
161 if (total_mem < 3145728UL)
162#endif
Wanlong Gao354ebb42012-12-07 10:10:04 +0800163 tst_resm(TFAIL,
164 "child unexpectedly failed: %d",
165 status);
Zhouping Liu8c48cfe2012-08-29 20:43:59 +0800166#if __WORDSIZE == 32
Wanlong Gao354ebb42012-12-07 10:10:04 +0800167 /* in 32-bit system, a process allocate about 3Gb memory at most */
Zhouping Liu8c48cfe2012-08-29 20:43:59 +0800168 else
169 tst_resm(TINFO, "Child can't allocate "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800170 ">3Gb memory in 32bit system");
Zhouping Liu8c48cfe2012-08-29 20:43:59 +0800171 }
172#endif
Zhouping Liu4f267b62012-03-30 22:29:58 +0800173 } else {
174 if (WIFEXITED(status)) {
175 if (WEXITSTATUS(status) != 0) {
176 tst_resm(TFAIL, "child unexpectedly "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800177 "failed: %d", status);
Zhouping Liu4f267b62012-03-30 22:29:58 +0800178 }
179 } else if (!WIFSIGNALED(status) ||
Wanlong Gao354ebb42012-12-07 10:10:04 +0800180 WTERMSIG(status) != SIGKILL) {
Zhouping Liu4f267b62012-03-30 22:29:58 +0800181 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800182 "child unexpectedly failed: %d",
183 status);
Zhouping Liu4f267b62012-03-30 22:29:58 +0800184 }
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800185 }
186 }
187}
188
189static int eatup_mem(unsigned long overcommit_policy)
190{
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800191 int ret = 0;
192 unsigned long memfree;
Zhouping Liu56696ac2012-08-29 20:43:48 +0800193 void *addrs;
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800194
195 memfree = read_meminfo("MemFree:");
196 printf("memfree is %lu kB before eatup mem\n", memfree);
Zhouping Liu56696ac2012-08-29 20:43:48 +0800197 while (1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800198 addrs = mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE,
199 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
Zhouping Liu56696ac2012-08-29 20:43:48 +0800200 if (addrs == MAP_FAILED) {
Zhouping Liu4f267b62012-03-30 22:29:58 +0800201 if (overcommit_policy != 1 && errno != ENOMEM) {
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800202 perror("mmap");
203 ret = -1;
204 }
205 break;
206 }
Zhouping Liu56696ac2012-08-29 20:43:48 +0800207 memset(addrs, 1, MAP_SIZE);
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800208 }
209 memfree = read_meminfo("MemFree:");
210 printf("memfree is %lu kB after eatup mem\n", memfree);
211
212 return ret;
213}
214
215static void check_monitor(void)
216{
217 unsigned long tune;
218 unsigned long memfree;
219
220 while (end) {
221 memfree = read_meminfo("MemFree:");
222 tune = get_sys_tune("min_free_kbytes");
223
224 if (memfree < tune) {
225 tst_resm(TINFO, "MemFree is %lu kB, "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800226 "min_free_kbytes is %lu kB", memfree, tune);
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800227 tst_resm(TFAIL, "MemFree < min_free_kbytes");
228 }
229
230 sleep(2);
231 }
232}
233
234static void sighandler(int signo LTP_ATTRIBUTE_UNUSED)
235{
236 end = 1;
237}
238
239void setup(void)
240{
Cyril Hrubisd1e794d2015-07-30 23:52:51 +0200241 tst_require_root();
Hoang Nguyen1f66e4c2017-03-05 11:00:54 +0700242 if (get_sys_tune("panic_on_oom")) {
243 tst_brkm(TCONF, NULL,
244 "panic_on_oom is set, disable it to run these testcases");
245 }
246
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800247 tst_sig(FORK, DEF_HANDLER, cleanup);
248 TEST_PAUSE;
249
250 total_mem = read_meminfo("MemTotal:") + read_meminfo("SwapTotal:");
251
252 default_tune = get_sys_tune("min_free_kbytes");
253 orig_overcommit = get_sys_tune("overcommit_memory");
254}
255
256void cleanup(void)
257{
258 set_sys_tune("min_free_kbytes", default_tune, 0);
259 set_sys_tune("overcommit_memory", orig_overcommit, 0);
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800260}