1#!/bin/bash
2# usage ./runmemctl_test.sh test_num
3
4#################################################################################
5#  Copyright (c) International Business Machines  Corp., 2008                   #
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# Name Of File: run_memctl_test.sh                                              #
23#                                                                               #
24# Description: This file runs the setup for testing different memory resource   #
25#              controller features. After setup it runs diff test cases in diff #
26#              setup.                                                           #
27#                                                                               #
28# Test 01:     Tests group memory usage on task migration                       #
29# Test 03:     Tests failcnt increase on memory usage greater than group limit  #
30#                                                                               #
31# Precaution:   Avoid system use by other applications/users to get fair and    #
32#               appropriate results (avoid unnecessary killing of applicatio)   #
33#                                                                               #
34# Author:       Sudhir Kumar   <skumar@linux.vnet.ibm.com>                      #
35#                                                                               #
36# History:                                                                      #
37#                                                                               #
38#  DATE         NAME           EMAIL                         DESC               #
39#                                                                               #
40#  12/03/08  Sudhir Kumar <skumar@linux.vnet.ibm.com>   Created this test       #
41#  11/05/08  Sudhir Kumar <skumar@linux.vnet.ibm.com>   Added third test        #
42#                                                                               #
43#################################################################################
44
45export TCID="memctl_test01-03";
46export TST_TOTAL=3;
47export TST_COUNT=1;
48
49TEST_NUM=$1;
50SCRIPT_PID=$$;
51RC=0;
52PWD=`pwd`;
53
54check_mem_allocated()
55{
56#	MEM_TOTAL=$1;
57	while [ 1 -gt 0 ]
58	do
59		sleep 1;
60		USAGE_FROM_USAGE_IN_BYTES=`cat /dev/memctl/group_1/memory.usage_in_bytes`;
61		if [ $USAGE_FROM_USAGE_IN_BYTES -gt $MEM_TOTAL ]
62		then
63			if [ $USAGE_FROM_USAGE_IN_BYTES -eq $SECOND_READ ]
64			then
65			# seems memory allocation is over now
66				break;
67			fi;
68			sleep 5;
69			SECOND_READ=`cat /dev/memctl/group_1/memory.usage_in_bytes`;
70		fi
71	done
72}
73
74cd $LTPROOT/testcases/bin/
75. myfunctions.sh
76#################################################################################
77#   ****************************** WARNING *********************************    #
78# User can change the parameters in different cases below but before doing      #
79# any change user is supposed to know what he is doing. At any point when       #
80# memory usage of a group becomes more than the group limit OOM Killer will     #
81# be invoked and some of the tasks will be killed. Need to add code to handle   #
82# the OOM Killer issues.                                                        #
83#################################################################################
84
85# First of all check if the system has sufficient memory
86MEM_AVAIL=`cat /proc/meminfo | grep MemTotal | awk '{print $2}'`;
87if [ $MEM_AVAIL -lt 262144 ]	# 256MB(as test requires some ~200MB)
88then
89	echo System does not have sufficient free memory.;
90	echo Skipping execution of memory controller tests.;
91	exit;
92fi
93
94case ${TEST_NUM} in
95
96"1" | "2" )
97	NUM_GROUPS=2;
98	MEMLIMIT_GROUP_1=100M;
99	MEMLIMIT_GROUP_2=132M;
100	CHUNK_SIZE=6291456;				# malloc n chunks of size m(6M)
101	NUM_CHUNKS=10;					# (say)60 MB memory(6*10)
102	TOTAL_TASKS=1;					# num of tasks in a group(1)
103	NUM_MIG_TASKS=$TOTAL_TASKS			# num of tasks to migrate
104	MEM_TASK=`expr $CHUNK_SIZE \* $NUM_CHUNKS`;	# memory allocated by a task
105	MEM_TOTAL=`expr $MEM_TASK \* $TOTAL_TASKS`;	# total memory allocated in a group
106	TEST_NAME=" TASK MIGRATION TEST:";
107	;;
108*  )	usage;
109	exit -1
110		;;
111	esac
112
113echo "TEST $TEST_NUM: MEMORY CONTROLLER TESTING";
114echo "RUNNING SETUP.....";
115setup;
116
117# Trap the signal from any abnormaly terminated task
118# and kill all others and let cleanup be called
119trap 'echo "signal caught from task"; killall memctl_task_*;\
120cleanup; exit -1;' SIGUSR1;#??? may need changes here
121
122echo "TEST STARTED: Please avoid using system while this test executes";
123#Check if  C source  file has been compiled and then run it in different groups
124
125case $TEST_NUM in
126"1" | "2" )
127	setmemlimits;
128	if [ -f memctl_test01 ]
129	then
130		for i in $(seq 1 $TOTAL_TASKS)
131		do
132			MYGROUP=/dev/memctl/group_1;
133			cp memctl_test01 memctl_task_$i # 2>/dev/null;
134			chmod +x memctl_task_$i;
135			TEST_NUM=$TEST_NUM MYGROUP=$MYGROUP SCRIPT_PID=$SCRIPT_PID CHUNK_SIZE=$CHUNK_SIZE \
136				NUM_CHUNKS=$NUM_CHUNKS ./memctl_task_$i &
137			if [ $? -ne 0 ]
138			then
139				echo "Error: Could not run ./memctl_task_$i"
140				cleanup;
141				exit -1;
142			else
143				PID[$i]=$!;
144			fi
145		done;	# tasks are now running in group1
146
147		# Wait untill tasks allocate memory from group1
148		while [ 1 -gt 0 ]
149		do
150			sleep 1;
151			GRP1_MEMUSAGE=`cat /dev/memctl/group_1/memory.usage_in_bytes`;
152			if [ $GRP1_MEMUSAGE -gt $MEM_TOTAL ]
153			then
154				break;
155			fi
156		done
157		GRP2_MEMUSAGE_OLD=`cat /dev/memctl/group_2/memory.usage_in_bytes`;
158		echo Before task migration to group2
159		echo group2 memory usage: $GRP2_MEMUSAGE_OLD Bytes
160
161		# We do not want to migrate charges during migration
162		if [ -f "/dev/memctl/group_2/memory.move_charge_at_immigrate" ]
163		then
164			echo 0 > /dev/memctl/group_2/memory.move_charge_at_immigrate
165		fi
166
167		# Now migrate the tasks to another group
168		for i in $(seq 1 $NUM_MIG_TASKS)
169		do
170			echo ${PID[$i]} >>/dev/memctl/group_2/tasks;
171			if [ $? -ne 0 ]
172			then
173				echo "TFAIL Task migration failed from group_1 to group_2";
174			fi
175		done
176
177		# double check
178		GRP2_TASKS=`cat /dev/memctl/group_2/tasks|wc -l`;
179		if [ $GRP2_TASKS -ne $NUM_MIG_TASKS ]
180		then
181			echo "TFAIL Task Migration failed for some of the tasks";
182		fi;
183
184		# Wait for some time to check if memory usage of group_2 increases
185		# This is not the right approach however working. ??? thoughts???
186		sleep 10;
187
188		# Decision formula: decides PASS or FAIL
189		case $TEST_NUM in
190		"1" )
191			GRP2_MEMUSAGE_NEW=`cat /dev/memctl/group_2/memory.usage_in_bytes`;
192			echo After task migration to group2
193			echo group2 memory usage: $GRP2_MEMUSAGE_NEW Bytes
194
195			if [ $GRP2_MEMUSAGE_NEW -gt $GRP2_MEMUSAGE_OLD ]
196			then
197				echo "TFAIL   Memory resource Controller:  Task Migration test $TEST_NUM FAILED";
198			else
199				echo "TPASS   Memory Resource Controller: Task Migration test $TEST_NUM PASSED";
200			fi
201
202			# Now we can signal the task to finish and do the cleanup
203			for i in $(seq 1 $TOTAL_TASKS)
204			do
205				kill -SIGUSR1 ${PID[$i]};
206			done
207			;;
208		"2" )
209			GRP2_MEMUSAGE_OLD=`cat /dev/memctl/group_2/memory.usage_in_bytes`;
210
211			# signal the migrated tasks to allocate memory
212			for i in $(seq 1 $TOTAL_TASKS)
213			do
214				kill -SIGUSR2 ${PID[$i]};
215			done
216			sleep 10; # Is it fine? Need input/alternates
217			GRP2_MEMUSAGE_NEW=`cat /dev/memctl/group_2/memory.usage_in_bytes`;
218			echo After task migration to group2 and doing malloc
219			echo group2 memory usage: $GRP2_MEMUSAGE_NEW Bytes
220			if [ $GRP2_MEMUSAGE_NEW -le $GRP2_MEMUSAGE_OLD ]
221			then
222				echo "TFAIL   Memory resource Controller:  Task Migration test $TEST_NUM FAILED";
223			else
224				# Now we can signal the task to finish and do the cleanup
225				for i in $(seq 1 $TOTAL_TASKS)
226				do
227					kill -SIGUSR1 ${PID[$i]};
228				done
229				echo "TPASS   Memory Resource Controller: Task Migration test $TEST_NUM PASSED";
230			fi
231
232			;;
233		esac
234
235		else
236		echo "Source file not compiled..Please check Makefile...Exiting test"
237		cleanup;
238		exit -1;
239	fi;
240	;;
241
242	"*" )
243		usage;
244		exit -1;
245		;;
246	esac
247
248	for i in $(seq 1 $TOTAL_TASKS)
249	do
250		wait ${PID[$i]};
251		RC=$?;  # Return status of the task being waited
252		# In abnormal termination of anyone trap will kill all others
253		# and they will return non zero exit status. So Test broke!!
254		if [ $RC -ne 0 ]
255		then
256			echo "Task $i exited abnormaly with return value: $RC";
257			tst_resm TINFO "Test could not execute for the expected duration";
258			cleanup;
259			exit -1;
260		fi
261	done
262
263	echo "Memory Resource Controller test executed successfully.";
264	cleanup;
265	cd $PWD
266	exit 0;         #to let PAN reprt success of test
267