1#!/bin/sh
2
3################################################################################
4##                                                                            ##
5##   Copyright (c) 2010 Mohamed Naufal Basheer                                ##
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,          ##
13##   but WITHOUT ANY WARRANTY;  without even the implied warranty of          ##
14##   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See                ##
15##   the GNU General Public License 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##   File:    memcg_control_test.sh                                           ##
23##                                                                            ##
24##   Purpose: Implement various memory controller tests                       ##
25##                                                                            ##
26##   Author:  Mohamed Naufal Basheer <naufal11@gmail.com>                     ##
27##                                                                            ##
28################################################################################
29
30if [ "x$(grep -w memory /proc/cgroups | cut -f4)" != "x1" ]; then
31	echo "WARNING:"
32	echo "Either kernel does not support memory resource controller or feature not enabled"
33	echo "Skipping all memcg_control testcases...."
34	exit 0
35fi
36
37export TCID="memcg_control"
38export TST_TOTAL=1
39export TST_COUNT=0
40
41export TMP=${TMP:-/tmp}
42cd $TMP
43
44TOT_MEM_LIMIT=$1
45ACTIVE_MEM_LIMIT=$2
46PROC_MEM=$3
47
48TST_PATH=$PWD
49STATUS_PIPE="$TMP/status_pipe"
50
51PASS=0
52FAIL=1
53
54# Check if the test process is killed on crossing boundary
55test_proc_kill()
56{
57	cd $TMP
58	mem_process -m $PROC_MEM &
59	cd $OLDPWD
60	sleep 1
61	echo $! > tasks
62
63	#Instruct the test process to start acquiring memory
64	echo m > $STATUS_PIPE
65	sleep 5
66
67	#Check if killed
68	ps -p $! > /dev/null 2> /dev/null
69	if [ $? -eq 0 ]; then
70		echo m > $STATUS_PIPE
71		echo x > $STATUS_PIPE
72	else
73		: $((KILLED_CNT += 1))
74	fi
75}
76
77# Validate the memory usage limit imposed by the hierarchically topmost group
78testcase_1()
79{
80	TST_COUNT=1
81	tst_resm TINFO "Test #1: Checking if the memory usage limit imposed by the topmost group is enforced"
82
83	echo "$ACTIVE_MEM_LIMIT" > $TST_PATH/mnt/$TST_NUM/memory.limit_in_bytes
84	echo "$TOT_MEM_LIMIT" > $TST_PATH/mnt/$TST_NUM/memory.memsw.limit_in_bytes
85
86	mkdir sub
87	(cd sub
88	KILLED_CNT=0
89	test_proc_kill
90
91	if [ $PROC_MEM -gt $TOT_MEM_LIMIT ] && [ $KILLED_CNT -eq 0 ]; then
92		result $FAIL "Test #1: failed"
93	else
94		result $PASS "Test #1: passed"
95	fi)
96	rmdir sub
97}
98
99# Record the test results
100#
101# $1: Result of the test case, $PASS or $FAIL
102# $2: Output information
103result()
104{
105	RES=$1
106	INFO=$2
107
108	if [ $RES -eq $PASS ]; then
109		tst_resm TPASS "$INFO"
110	else
111		: $((FAILED_CNT += 1))
112		tst_resm TFAIL "$INFO"
113	fi
114}
115
116cleanup()
117{
118	if [ -e $TST_PATH/mnt ]; then
119		umount $TST_PATH/mnt 2> /dev/null
120		rm -rf $TST_PATH/mnt
121	fi
122}
123
124do_mount()
125{
126	cleanup
127
128	mkdir $TST_PATH/mnt
129	mount -t cgroup -o memory cgroup $TST_PATH/mnt 2> /dev/null
130	if [ $? -ne 0 ]; then
131		tst_brkm TBROK NULL "Mounting cgroup to temp dir failed"
132		rmdir $TST_PATH/mnt
133		exit 1
134	fi
135}
136
137do_mount
138
139echo 1 > mnt/memory.use_hierarchy 2> /dev/null
140
141FAILED_CNT=0
142
143TST_NUM=1
144while [ $TST_NUM -le $TST_TOTAL ]; do
145	mkdir $TST_PATH/mnt/$TST_NUM
146	(cd $TST_PATH/mnt/$TST_NUM && testcase_$TST_NUM)
147	rmdir $TST_PATH/mnt/$TST_NUM
148	: $((TST_NUM += 1))
149done
150
151echo 0 > mnt/memory.use_hierarchy 2> /dev/null
152
153cleanup
154
155if [ "$FAILED_CNT" -ne 0 ]; then
156	tst_resm TFAIL "memcg_control: failed"
157	exit 1
158else
159	tst_resm TPASS "memcg_control: passed"
160	exit 0
161fi
162