1#! /bin/sh
2
3################################################################################
4##                                                                            ##
5## Copyright (c) 2009 FUJITSU LIMITED                                         ##
6##                                                                            ##
7## This program is free software;  you can redistribute it and#or modify      ##
8## it under the terms of the GNU General Public License as published by       ##
9## the Free Software Foundation; either version 2 of the License, or          ##
10## (at your option) any later version.                                        ##
11##                                                                            ##
12## This program is distributed in the hope that it will be useful, but        ##
13## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
14## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
15## for more details.                                                          ##
16##                                                                            ##
17## You should have received a copy of the GNU General Public License          ##
18## along with this program;  if not, write to the Free Software               ##
19## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    ##
20##                                                                            ##
21## Author: Li Zefan <lizf@cn.fujitsu.com>                                     ##
22## Restructure for LTP: Shi Weihua <shiwh@cn.fujitsu.com>                     ##
23## Added memcg enable/disable functinality: Rishikesh K Rajak		      ##
24##						<risrajak@linux.vnet.ibm.com  ##
25##                                                                            ##
26################################################################################
27
28export TCID="memcg_function_test"
29export TST_TOTAL=38
30export TST_COUNT=0
31
32. memcg_lib.sh || exit 1
33
34# Case 1 - 10: Test the management and counting of memory
35testcase_1()
36{
37	test_mem_stat "--mmap-anon" $PAGESIZE "rss" $PAGESIZE 0
38}
39
40testcase_2()
41{
42	test_mem_stat "--mmap-file" $PAGESIZE "rss" 0 0
43}
44
45testcase_3()
46{
47	test_mem_stat "--shm -k 3" $PAGESIZE "rss" 0 0
48}
49
50testcase_4()
51{
52	test_mem_stat "--mmap-anon --mmap-file --shm" $PAGESIZE "rss" \
53		$PAGESIZE 0
54}
55
56testcase_5()
57{
58	test_mem_stat "--mmap-lock1" $PAGESIZE "rss" $PAGESIZE 0
59}
60
61testcase_6()
62{
63	test_mem_stat "--mmap-anon" $PAGESIZE "rss" $PAGESIZE 1
64}
65
66testcase_7()
67{
68	test_mem_stat "--mmap-file" $PAGESIZE "rss" 0 1
69}
70
71testcase_8()
72{
73	test_mem_stat "--shm -k 8" $PAGESIZE "rss" 0 1
74}
75
76testcase_9()
77{
78	test_mem_stat "--mmap-anon --mmap-file --shm" $PAGESIZE "rss" \
79		$PAGESIZE 1
80}
81
82testcase_10()
83{
84	test_mem_stat "--mmap-lock1" $PAGESIZE "rss" $PAGESIZE 1
85}
86
87# Case 11 - 13: Test memory.failcnt
88testcase_11()
89{
90	echo $PAGESIZE > memory.limit_in_bytes
91	malloc_free_memory "--mmap-anon" $(($PAGESIZE*2))
92	test_failcnt "memory.failcnt"
93}
94
95testcase_12()
96{
97	echo $PAGESIZE > memory.limit_in_bytes
98	malloc_free_memory "--mmap-file" $(($PAGESIZE*2))
99	test_failcnt "memory.failcnt"
100}
101
102testcase_13()
103{
104	echo $PAGESIZE > memory.limit_in_bytes
105	malloc_free_memory "--shm" $(($PAGESIZE*2))
106	test_failcnt "memory.failcnt"
107}
108
109# Case 14 - 15: Test mmap(locked) + alloc_mem > limit_in_bytes
110testcase_14()
111{
112	test_proc_kill $PAGESIZE "--mmap-lock1" $((PAGESIZE*2)) 0
113}
114
115testcase_15()
116{
117	test_proc_kill $PAGESIZE "--mmap-lock2" $((PAGESIZE*2)) 0
118}
119
120# Case 16 - 18: Test swapoff + alloc_mem > limi_in_bytes
121testcase_16()
122{
123	swapoff -a
124	test_proc_kill $PAGESIZE "--mmap-anon" $((PAGESIZE*2)) 0
125	swapon -a
126}
127
128testcase_17()
129{
130	swapoff -a
131	test_proc_kill $PAGESIZE "--mmap-file" $((PAGESIZE*2)) 0
132	swapon -a
133}
134
135testcase_18()
136{
137	swapoff -a
138	test_proc_kill $PAGESIZE "--shm -k 18" $((PAGESIZE*2)) 0
139	swapon -a
140}
141
142# Case 19 - 21: Test limit_in_bytes == 0
143testcase_19()
144{
145	test_proc_kill 0 "--mmap-anon" $PAGESIZE 0
146}
147
148testcase_20()
149{
150	test_proc_kill 0 "--mmap-file" $PAGESIZE 0
151}
152
153testcase_21()
154{
155	test_proc_kill 0 "--shm -k 21" $PAGESIZE 0
156}
157
158# Case 22 - 24: Test limit_in_bytes will be aligned to PAGESIZE
159testcase_22()
160{
161	test_limit_in_bytes $((PAGESIZE-1)) 0
162}
163
164testcase_23()
165{
166	test_limit_in_bytes $((PAGESIZE+1)) 0
167}
168
169testcase_24()
170{
171	test_limit_in_bytes 1 0
172}
173
174# Case 25 - 28: Test invaild memory.limit_in_bytes
175testcase_25()
176{
177	echo -1 > memory.limit_in_bytes 2> /dev/null
178	ret=$?
179	tst_kvercmp 2 6 31
180	if [ $? -eq 0 ]; then
181		result $(( !($ret != 0) ))  "return value is $ret"
182	else
183		result $(( !($ret == 0) ))  "return value is $ret"
184	fi
185}
186
187testcase_26()
188{
189	echo 1.0 > memory.limit_in_bytes 2> /dev/null
190	result $(( !($? != 0) )) "return value is $?"
191}
192
193testcase_27()
194{
195	echo 1xx > memory.limit_in_bytes 2> /dev/null
196	result $(( !($? != 0) )) "return value is $?"
197}
198
199testcase_28()
200{
201	echo xx > memory.limit_in_bytes 2> /dev/null
202	result $(( !($? != 0) )) "return value is $?"
203}
204
205# Case 29 - 35: Test memory.force_empty
206testcase_29()
207{
208	$TEST_PATH/memcg_process --mmap-anon -s $PAGESIZE &
209	pid=$!
210	sleep 1
211	echo $pid > tasks
212	kill -s USR1 $pid 2> /dev/null
213	sleep 1
214	echo $pid > ../tasks
215
216	# This expects that there is swap configured
217	echo 1 > memory.force_empty
218	if [ $? -eq 0 ]; then
219		result $PASS "force memory succeeded"
220	else
221		result $FAIL "force memory failed"
222	fi
223
224	kill -s INT $pid 2> /dev/null
225}
226
227testcase_30()
228{
229	$TEST_PATH/memcg_process --mmap-lock2 -s $PAGESIZE &
230	pid=$!
231	sleep 1
232	echo $pid > tasks
233	kill -s USR1 $pid 2> /dev/null
234	sleep 1
235
236	echo 1 > memory.force_empty 2> /dev/null
237	if [ $? -ne 0 ]; then
238		result $PASS "force memory failed as expected"
239	else
240		result $FAIL "force memory should fail"
241	fi
242
243	kill -s INT $pid 2> /dev/null
244}
245
246testcase_31()
247{
248	echo 0 > memory.force_empty 2> /dev/null
249	result $? "return value is $?"
250}
251
252testcase_32()
253{
254	echo 1.0 > memory.force_empty 2> /dev/null
255	result $? "return value is $?"
256}
257
258testcase_33()
259{
260	echo 1xx > memory.force_empty 2> /dev/null
261	result $? "return value is $?"
262}
263
264testcase_34()
265{
266	echo xx > memory.force_empty 2> /dev/null
267	result $? "return value is $?"
268}
269
270testcase_35()
271{
272	# writing to non-empty top mem cgroup's force_empty
273	# should return failure
274	echo 1 > /dev/memcg/memory.force_empty 2> /dev/null
275	result $(( !$? )) "return value is $?"
276}
277
278# Case 36 - 38: Test that group and subgroup have no relationship
279testcase_36()
280{
281	test_subgroup $PAGESIZE $((2*PAGESIZE))
282}
283
284testcase_37()
285{
286	test_subgroup $PAGESIZE $PAGESIZE
287}
288
289testcase_38()
290{
291	test_subgroup $PAGESIZE 0
292}
293
294shmmax=`cat /proc/sys/kernel/shmmax`
295if [ $shmmax -lt $HUGEPAGESIZE ]; then
296	echo $(($HUGEPAGESIZE)) > /proc/sys/kernel/shmmax
297fi
298
299# Run all the test cases
300for i in $(seq 1 $TST_TOTAL)
301do
302	export TST_COUNT=$(( $TST_COUNT + 1 ))
303	cur_id=$i
304
305	do_mount
306	if [ $? -ne 0 ]; then
307		echo "Cannot create memcg"
308		exit 1
309	fi
310
311	# prepare
312	mkdir /dev/memcg/$i 2> /dev/null
313	cd /dev/memcg/$i
314
315	# run the case
316	testcase_$i
317
318	# clean up
319	sleep 1
320	cd $TEST_PATH
321	rmdir /dev/memcg/$i
322
323	cleanup
324done
325
326echo $shmmax > /proc/sys/kernel/shmmax
327
328if [ $failed -ne 0 ]; then
329	exit $failed
330else
331	exit 0
332fi
333