cgroup_fj_stress.sh revision 4548c6cf9bcdd96d8303caa4130ab638b61f8a30
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: Shi Weihua <shiwh@cn.fujitsu.com>                                  ##
22##                                                                            ##
23################################################################################
24
25subsystem=$1			# 1: debug
26				# 2: cpuset
27				# 3: ns
28				# 4: cpu
29				# 5: cpuacct
30				# 6: memory
31				# 7: all
32mount_times=$2			#1: execute once
33				#2: execute 100 times
34subgroup_num=$3			#subgroup number in the same hierarchy
35				#1: 1
36				#2: 100
37subgroup_hiers=$4		#number of subgroup's hierarchy
38				#1: 1
39				#2: 100
40attach_operation=$5		# 1: attach one process to every subcgroup
41				# 2: attach all processes in root group to one subcgroup
42				# 3: attach all processes in root group to every subcgroup
43
44usage()
45{
46	echo "usage of cgroup_fj_stress.sh: "
47	echo "  ./cgroup_fj_stress.sh -subsystem -mount_times -subgroup_num -subgroup_hiers -attach_operation"
48	echo "    subsystem's usable number"
49	echo "      1: debug"
50	echo "      2: cpuset"
51	echo "      3: ns"
52	echo "      4: cpu"
53	echo "      5: cpuacct"
54	echo "      6: memory"
55	echo "      7: all"
56	echo "    mount_times's usable number"
57	echo "      1: execute once"
58	echo "      2: execute 100 times"
59	echo "    subgroup_num's usable number"
60	echo "      (subgroup number in the same hierarchy)"
61	echo "      1: 1"
62	echo "      2: 100"
63        echo "    subgroup_hiers's usable number"
64	echo "      (number of subgroup's hierarchy)"
65	echo "      1: 1"
66	echo "      2: 100"
67	echo "    attach_operation's usable number"
68	echo "      1: attach one process to every subcgroup"
69	echo "      2: attach all processes in root group to one subcgroup"
70	echo "      3: attach all processes in root group to every subcgroup"
71        echo "example: ./cgroup_fj_stress.sh 1 1 1 1 1"
72        echo "  will use "debug" to test, will mount once, will create one subgroup in same hierarchy,"
73	echo "  will create one hierarchy, will attach one process to every subcgroup"
74}
75
76
77exit_parameter()
78{
79	echo "ERROR: Wrong inputed parameter..Exiting test" >> $LOGFILE
80	exit -1;
81}
82
83export TESTROOT=`pwd`
84if [ "$LOGFILE" = "" ]; then
85	LOGFILE="/dev/stdout"
86fi
87export TMPFILE=$TESTROOT/tmp_tasks
88
89. $TESTROOT/cgroup_fj_utility.sh
90
91pid=0;
92release_agent_para=1;
93release_agent_echo=1;
94subsystem_str="debug";
95get_subsystem;
96if [ "$?" -ne "0" ] || [ "$#" -ne "5" ]; then
97	usage;
98	exit_parameter;
99fi
100remount_use_str="";
101noprefix_use_str="";
102release_agent_para_str="";
103ulimit_u=`ulimit -u`
104no_debug=1
105cur_subgroup_path1=""
106cur_subgroup_path2=""
107
108get_subgroup_path1()
109{
110	cur_subgroup_path1=""
111	if [ "$#" -ne 1 ] || [ "$1" -lt 1 ] || [ "$1" -gt $ulimit_u ]; then
112		return;
113	fi
114
115	cur_subgroup_path1="/dev/cgroup/subgroup_$1/"
116}
117
118
119get_subgroup_path2()
120{
121	cur_subgroup_path2=""
122	if [ "$#" -ne 1 ] || [ "$1" -lt 2 ] || [ "$1" -gt $ulimit_u ]; then
123		return;
124	fi
125
126	for i in `seq 2 $1`
127	do
128		cur_subgroup_path2="$cur_subgroup_path2""s/"
129	done
130}
131
132case $mount_times in
133"1" )
134	mount_times=1
135	;;
136"2" )
137	mount_times=100
138	;;
139*  )
140	usage;
141	exit_parameter;
142	;;
143esac
144
145case $subgroup_num in
146"1" )
147	subgroup_num=1
148	;;
149"2" )
150	subgroup_num=100
151	;;
152*  )
153	usage;
154	exit_parameter;
155	;;
156esac
157
158case $subgroup_hiers in
159"1" )
160	subgroup_hiers=1
161	;;
162"2" )
163	subgroup_hiers=100
164	;;
165*  )
166	usage;
167	exit_parameter;
168	;;
169esac
170
171##########################  main   #######################
172echo "-------------------------------------------------------------------------" >> $LOGFILE
173echo "case no : $CASENO2" >> $LOGFILE
174echo `date` >> $LOGFILE
175
176setup;
177
178echo "INFO: now we begin to stress test no $CASENO2 ..." >> $LOGFILE
179
180mount_cgroup;
181
182$TESTROOT/cgroup_fj_proc &
183pid=$!
184
185cpus=0
186mems=0
187exist_cpuset=0
188exist_cpuset=`grep -w cpuset /proc/cgroups | cut -f1`;
189if [ $subsystem -eq 2 ] || [ $subsystem -eq 7 ] ; then
190	if [ "$exist_cpuset" != "" ]; then
191		cpus=`cat /dev/cgroup/cpuset.cpus`
192		mems=`cat /dev/cgroup/cpuset.mems`
193	fi
194fi
195
196mkdir_subgroup;
197
198# cpuset.cpus and cpuset.mems should be specified with suitable value
199# before attachint operation if subsystem is cpuset
200if [ $subsystem -eq 2 ] || [ $subsystem -eq 7 ] ; then
201	if [ "$exist_cpuset" != "" ]; then
202		do_echo 1 1 "$cpus" /dev/cgroup/subgroup_1/cpuset.cpus;
203		do_echo 1 1 "$mems" /dev/cgroup/subgroup_1/cpuset.mems;
204	fi
205fi
206
207if [ $mount_times -ne 1 ]; then
208	count=0
209	for i in `seq 1 $mount_times`
210	do
211		do_echo 1 1 $pid /dev/cgroup/subgroup_1/tasks
212		if [ $subsystem -eq 3 ] || [ $subsystem -eq 7 ] ; then
213			do_kill 1 1 9 $pid
214			$TESTROOT/cgroup_fj_proc &
215			pid=$!
216		else
217			do_echo 1 1 $pid /dev/cgroup/tasks
218		fi
219		setup;
220		$TESTROOT/cgroup_fj_proc &
221		pid=$!
222		mount_cgroup;
223		mkdir_subgroup;
224		if [ $subsystem -eq 2 ] || [ $subsystem -eq 7 ] ; then
225			if [ "$exist_cpuset" != "" ]; then
226				do_echo 1 1 "$cpus" /dev/cgroup/subgroup_1/cpuset.cpus;
227				do_echo 1 1 "$mems" /dev/cgroup/subgroup_1/cpuset.mems;
228			fi
229		fi
230		let "count = $count + 1"
231		echo "$count .. OK" >> $LOGFILE
232	done
233	echo "...executed $count times" >> $LOGFILE
234else
235	get_subgroup_path2 $subgroup_hiers
236	count=0
237	pathes[1]=""
238	for i in `seq 1 $subgroup_num`
239	do
240		get_subgroup_path1 $i
241		do_mkdir 1 1 $cur_subgroup_path1
242		if [ $subsystem -eq 2 ] || [ $subsystem -eq 7 ] ; then
243			if [ "$exist_cpuset" != "" ]; then
244				do_echo 1 1 "$cpus" "$cur_subgroup_path1""cpuset.cpus";
245				do_echo 1 1 "$mems" "$cur_subgroup_path1""cpuset.mems";
246			fi
247		fi
248		let "count = $count + 1"
249		pathes[$count]="$cur_subgroup_path1"
250		for j in `seq 2 $subgroup_hiers`
251		do
252			get_subgroup_path2 $j
253			do_mkdir 1 1 "$cur_subgroup_path1""$cur_subgroup_path2" 1
254			if [ $subsystem -eq 2 ] || [ $subsystem -eq 7 ] ; then
255				if [ "$exist_cpuset" != "" ]; then
256					do_echo 1 1 "$cpus" "$cur_subgroup_path1""$cur_subgroup_path2""cpuset.cpus";
257					do_echo 1 1 "$mems" "$cur_subgroup_path1""$cur_subgroup_path2""cpuset.mems";
258				fi
259			fi
260			let "count = $count + 1"
261			pathes[$count]="$cur_subgroup_path1""$cur_subgroup_path2"
262		done
263	done
264	echo "...mkdired $count times" >> $LOGFILE
265
266	sleep 1
267
268	case $attach_operation in
269	"1" )
270		for i in `seq 1 $count`
271		do
272			do_echo 1 1 $pid "${pathes[$i]}""tasks"
273		done
274		do_echo 1 1 $pid /dev/cgroup/tasks
275		;;
276	"2" )
277		pathes2[0]="/dev/cgroup/"
278		pathes2[1]="${pathes[$count]}"
279		pathes2[3]="/dev/cgroup/"
280		for i in `seq 1 $nlines`
281		do
282			j=$i
283			let "j = $j + 1"
284			cat "${pathes2[$i]}tasks" > $TMPFILE
285			nlines=`cat "$TMPFILE" | wc -l`
286			if [ $no_debug -ne 1 ]; then
287				echo "DEBUG: move $nlines processes from "$i"th path to "$j"th"
288			fi
289			for k in `seq 1 $nlines`
290			do
291				cur_pid=`sed -n "$k""p" $TMPFILE`
292				if [ -e /proc/$cur_pid/ ];then
293					do_echo 0 1 "$cur_pid" "${pathes[$j]}tasks"
294				fi
295			done
296		done
297		;;
298	"3" )
299		count2=$count
300		let "count2 = $count2 + 1"
301		pathes[0]="/dev/cgroup/"
302		pathes[$count2]="/dev/cgroup/"
303		for i in `seq 0 $count`
304		do
305			j=$i
306			let "j = $j + 1"
307			cat "${pathes[$i]}tasks" > $TMPFILE
308			nlines=`cat "$TMPFILE" | wc -l`
309			if [ $no_debug -ne 1 ]; then
310				echo "DEBUG: move $nlines processes from "$i"th path to "$j"th"
311			fi
312			for k in `seq 1 $nlines`
313			do
314				cur_pid=`sed -n "$k""p" $TMPFILE`
315				if [ -e /proc/$cur_pid/ ];then
316					do_echo 0 1 "$cur_pid" "${pathes[$j]}tasks"
317				fi
318			done
319		done
320		;;
321	*  )
322		;;
323	esac
324	reclaim_foundling;
325	for i in `seq 1 $count`
326	do
327		j=i
328		let "j = $count - $j + 1"
329		do_rmdir 1 1 ${pathes[$j]}
330	done
331fi
332
333do_rmdir 0 1 /dev/cgroup/subgroup_*
334
335sleep 1
336
337cleanup;
338do_kill 1 1 9 $pid
339sleep 1
340exit 0;
341