blob: 8a3ae7d013d0c82caa84a0845298bbd666513b70 [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{
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020069 const char *msg;
Zhouping Liu8b7cc632012-03-23 22:12:38 +080070 int lc, pid, status;
71 struct sigaction sa;
72
73 sa.sa_handler = sighandler;
74 if (sigemptyset(&sa.sa_mask) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +080075 tst_brkm(TBROK | TERRNO, cleanup, "sigemptyset");
Zhouping Liu8b7cc632012-03-23 22:12:38 +080076 sa.sa_flags = 0;
77 if (sigaction(SIGUSR1, &sa, NULL) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +080078 tst_brkm(TBROK | TERRNO, cleanup, "sigaction");
Zhouping Liu8b7cc632012-03-23 22:12:38 +080079
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 Gao354ebb42012-12-07 10:10:04 +080087 tst_brkm(TBROK | TERRNO, cleanup, "fork");
Zhouping Liu8b7cc632012-03-23 22:12:38 +080088
89 case 0:
90 /* startup the check monitor */
91 check_monitor();
92 exit(0);
93 }
94
Zhouping Liu8b7cc632012-03-23 22:12:38 +080095 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080096 tst_count = 0;
Zhouping Liu8b7cc632012-03-23 22:12:38 +080097
98 test_tune(2);
99 test_tune(0);
100 test_tune(1);
101 }
102
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800103 if (kill(pid, SIGUSR1) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800104 tst_brkm(TBROK | TERRNO, cleanup, "kill %d", pid);
105 if (waitpid(pid, &status, WUNTRACED | WCONTINUED) == -1)
106 tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800107 if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
108 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800109 "check_monitor child exit with status: %d", status);
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800110
111 cleanup();
112 tst_exit();
113}
114
115static void test_tune(unsigned long overcommit_policy)
116{
117 int status;
Zhouping Liua5c56172012-08-29 20:42:59 +0800118 int pid[3];
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800119 int ret, i;
Zhouping Liua5c56172012-08-29 20:42:59 +0800120 unsigned long tune, memfree, memtotal;
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800121
122 set_sys_tune("overcommit_memory", overcommit_policy, 1);
123
Zhouping Liua5c56172012-08-29 20:42:59 +0800124 for (i = 0; i < 3; i++) {
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800125 /* case1 */
126 if (i == 0)
127 set_sys_tune("min_free_kbytes", default_tune, 1);
128 /* case2 */
Zhouping Liua5c56172012-08-29 20:42:59 +0800129 else if (i == 1) {
130 set_sys_tune("min_free_kbytes", 2 * default_tune, 1);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800131 /* case3 */
Zhouping Liua5c56172012-08-29 20:42:59 +0800132 } else {
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800133 memfree = read_meminfo("MemFree:");
Zhouping Liua5c56172012-08-29 20:42:59 +0800134 memtotal = read_meminfo("MemTotal:");
135 tune = memfree / 20;
136 if (tune > (memtotal / 50))
137 tune = memtotal / 50;
138
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800139 set_sys_tune("min_free_kbytes", tune, 1);
140 }
141
142 fflush(stdout);
143 switch (pid[i] = fork()) {
144 case -1:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800145 tst_brkm(TBROK | TERRNO, cleanup, "fork");
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800146 case 0:
147 ret = eatup_mem(overcommit_policy);
148 exit(ret);
149 }
150
Wanlong Gao354ebb42012-12-07 10:10:04 +0800151 if (waitpid(pid[i], &status, WUNTRACED | WCONTINUED) == -1)
152 tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800153
154 if (overcommit_policy == 2) {
155 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
156 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800157 "child unexpectedly failed: %d",
158 status);
Zhouping Liu4f267b62012-03-30 22:29:58 +0800159 } else if (overcommit_policy == 1) {
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800160 if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGKILL)
Zhouping Liu8c48cfe2012-08-29 20:43:59 +0800161#if __WORDSIZE == 32
162 {
163 if (total_mem < 3145728UL)
164#endif
Wanlong Gao354ebb42012-12-07 10:10:04 +0800165 tst_resm(TFAIL,
166 "child unexpectedly failed: %d",
167 status);
Zhouping Liu8c48cfe2012-08-29 20:43:59 +0800168#if __WORDSIZE == 32
Wanlong Gao354ebb42012-12-07 10:10:04 +0800169 /* in 32-bit system, a process allocate about 3Gb memory at most */
Zhouping Liu8c48cfe2012-08-29 20:43:59 +0800170 else
171 tst_resm(TINFO, "Child can't allocate "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800172 ">3Gb memory in 32bit system");
Zhouping Liu8c48cfe2012-08-29 20:43:59 +0800173 }
174#endif
Zhouping Liu4f267b62012-03-30 22:29:58 +0800175 } else {
176 if (WIFEXITED(status)) {
177 if (WEXITSTATUS(status) != 0) {
178 tst_resm(TFAIL, "child unexpectedly "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800179 "failed: %d", status);
Zhouping Liu4f267b62012-03-30 22:29:58 +0800180 }
181 } else if (!WIFSIGNALED(status) ||
Wanlong Gao354ebb42012-12-07 10:10:04 +0800182 WTERMSIG(status) != SIGKILL) {
Zhouping Liu4f267b62012-03-30 22:29:58 +0800183 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800184 "child unexpectedly failed: %d",
185 status);
Zhouping Liu4f267b62012-03-30 22:29:58 +0800186 }
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800187 }
188 }
189}
190
191static int eatup_mem(unsigned long overcommit_policy)
192{
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800193 int ret = 0;
194 unsigned long memfree;
Zhouping Liu56696ac2012-08-29 20:43:48 +0800195 void *addrs;
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800196
197 memfree = read_meminfo("MemFree:");
198 printf("memfree is %lu kB before eatup mem\n", memfree);
Zhouping Liu56696ac2012-08-29 20:43:48 +0800199 while (1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800200 addrs = mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE,
201 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
Zhouping Liu56696ac2012-08-29 20:43:48 +0800202 if (addrs == MAP_FAILED) {
Zhouping Liu4f267b62012-03-30 22:29:58 +0800203 if (overcommit_policy != 1 && errno != ENOMEM) {
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800204 perror("mmap");
205 ret = -1;
206 }
207 break;
208 }
Zhouping Liu56696ac2012-08-29 20:43:48 +0800209 memset(addrs, 1, MAP_SIZE);
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800210 }
211 memfree = read_meminfo("MemFree:");
212 printf("memfree is %lu kB after eatup mem\n", memfree);
213
214 return ret;
215}
216
217static 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 Gao354ebb42012-12-07 10:10:04 +0800228 "min_free_kbytes is %lu kB", memfree, tune);
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800229 tst_resm(TFAIL, "MemFree < min_free_kbytes");
230 }
231
232 sleep(2);
233 }
234}
235
236static void sighandler(int signo LTP_ATTRIBUTE_UNUSED)
237{
238 end = 1;
239}
240
241void 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
253void cleanup(void)
254{
255 set_sys_tune("min_free_kbytes", default_tune, 0);
256 set_sys_tune("overcommit_memory", orig_overcommit, 0);
Zhouping Liu8b7cc632012-03-23 22:12:38 +0800257}