1#! /bin/sh
2
3################################################################################
4##                                                                            ##
5## Copyright (c) 2012 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################################################################################
22#
23# File :        memcg_stat_test.sh
24# Description:  Tests memroy.stat.
25# Author:       Peng Haitao <penght@cn.fujitsu.com>
26# History:      2012/01/16 - Created.
27#
28
29export TCID="memcg_stat_test"
30export TST_TOTAL=8
31export TST_COUNT=0
32
33. memcg_lib.sh || exit 1
34
35MEM_SWAP_FLAG=0
36
37# Test cache
38testcase_1()
39{
40	test_mem_stat "--shm -k 3" $PAGESIZE "cache" $PAGESIZE 0
41}
42
43# Test mapped_file
44testcase_2()
45{
46	test_mem_stat "--mmap-file" $PAGESIZE "mapped_file" $PAGESIZE 0
47}
48
49# Test unevictable with MAP_LOCKED
50testcase_3()
51{
52	test_mem_stat "--mmap-lock1" $PAGESIZE "unevictable" $PAGESIZE 0
53}
54
55# Test unevictable with mlock
56testcase_4()
57{
58	test_mem_stat "--mmap-lock2" $PAGESIZE "unevictable" $PAGESIZE 0
59}
60
61# Test hierarchical_memory_limit with enabling hierarchical accounting
62testcase_5()
63{
64	echo 1 > memory.use_hierarchy
65
66	mkdir subgroup
67	echo $PAGESIZE > memory.limit_in_bytes
68	echo $((PAGESIZE*2)) > subgroup/memory.limit_in_bytes
69
70	cd subgroup
71	check_mem_stat "hierarchical_memory_limit" $PAGESIZE
72
73	cd ..
74	rmdir subgroup
75}
76
77# Test hierarchical_memory_limit with disabling hierarchical accounting
78testcase_6()
79{
80	echo 0 > memory.use_hierarchy
81
82	mkdir subgroup
83	echo $PAGESIZE > memory.limit_in_bytes
84	echo $((PAGESIZE*2)) > subgroup/memory.limit_in_bytes
85
86	cd subgroup
87	check_mem_stat "hierarchical_memory_limit" $((PAGESIZE*2))
88
89	cd ..
90	rmdir subgroup
91}
92
93# Test hierarchical_memsw_limit with enabling hierarchical accounting
94testcase_7()
95{
96	if [ $MEM_SWAP_FLAG -eq 0 ]; then
97		tst_resm TCONF "mem+swap is not enabled"
98		return
99	fi
100
101	echo 1 > memory.use_hierarchy
102
103	mkdir subgroup
104	echo $PAGESIZE > memory.limit_in_bytes
105	echo $PAGESIZE > memory.memsw.limit_in_bytes
106	echo $((PAGESIZE*2)) > subgroup/memory.limit_in_bytes
107	echo $((PAGESIZE*2)) > subgroup/memory.memsw.limit_in_bytes
108
109	cd subgroup
110	check_mem_stat "hierarchical_memsw_limit" $PAGESIZE
111
112	cd ..
113	rmdir subgroup
114}
115
116# Test hierarchical_memsw_limit with disabling hierarchical accounting
117testcase_8()
118{
119	if [ $MEM_SWAP_FLAG -eq 0 ]; then
120		tst_resm TCONF "mem+swap is not enabled"
121		return
122	fi
123
124	echo 0 > memory.use_hierarchy
125
126	mkdir subgroup
127	echo $PAGESIZE > memory.limit_in_bytes
128	echo $PAGESIZE > memory.memsw.limit_in_bytes
129	echo $((PAGESIZE*2)) > subgroup/memory.limit_in_bytes
130	echo $((PAGESIZE*2)) > subgroup/memory.memsw.limit_in_bytes
131
132	cd subgroup
133	check_mem_stat "hierarchical_memsw_limit" $((PAGESIZE*2))
134
135	cd ..
136	rmdir subgroup
137}
138
139# Run all the test cases
140for i in $(seq 1 $TST_TOTAL)
141do
142	export TST_COUNT=$(( $TST_COUNT + 1 ))
143	cur_id=$i
144
145	do_mount
146	if [ $? -ne 0 ]; then
147		echo "Cannot create memcg"
148		exit 1
149	fi
150
151	# prepare
152	mkdir /dev/memcg/$i 2> /dev/null
153	cd /dev/memcg/$i
154
155	if [ -e memory.memsw.limit_in_bytes ]; then
156		MEM_SWAP_FLAG=1
157	fi
158
159	# run the case
160	testcase_$i
161
162	# clean up
163	sleep 1
164	cd $TEST_PATH
165	rmdir /dev/memcg/$i
166
167	cleanup
168done
169
170if [ $failed -ne 0 ]; then
171	exit $failed
172else
173	exit 0
174fi
175