Mods for Linux 3.0 and fix a typo
[yaffs2.git] / linux-tests / stress_test.sh
1 #!/bin/bash
2 # Do some random operations on a yaffs2 file system
3
4 MTD="/dev/mtdblock0"
5 MOUNT_PT="/mnt"
6 TEST_DIR="$MOUNT_PT/stress_dir"
7 REF_FILE="$MOUNT_PT/ref.file"
8 SUM_FILE="$MOUNT_PT/sum.file"
9 MIN_LOW_WATER=5
10 MAX_LOW_WATER=85
11 MIN_HIGH_WATER=15
12 MAX_HIGH_WATER=95
13
14 low_water=25
15 high_water=90
16 df_percent=50
17
18
19 random_number(){
20  local val
21  val=$RANDOM
22  echo $val
23 }
24
25
26 update_df_percent(){
27         df_percent=$(df | grep $MTD | sed "s_[ %] *_ _g" | cut -d" " -f 5)
28 }
29
30 update_df_percent
31
32 calc_low_water() {
33    low_water=0
34    while [ $low_water -lt $MIN_LOW_WATER ] ;
35    do
36         low_water=$(($(random_number) % 100))
37         while [[ $low_water -ge $df_percent  || $low_water -gt $MAX_LOW_WATER ]] ;
38         do
39                 low_water=$(($(random_number) % 100))
40         done
41    done
42 }
43
44 calc_high_water() {
45    high_water=100
46    while [ $high_water -gt $MAX_HIGH_WATER ] ;
47    do
48         high_water=$(($(random_number) % 100))
49         while [[ $high_water -le $df_percent || $high_water -lt $MIN_HIGH_WATER ]] ;
50         do
51                 high_water=$(($(random_number) % 100))
52         done
53    done
54 }
55
56
57
58 maybe_sleep(){
59   local val
60   val=$(($(random_number) % 10000))
61   if [ $val -le 1 ] ; then
62         val=$(($(random_number) % 100))
63         echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
64         echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
65         echo "!!!!!!!!!                                                              !!!!!!!!!!!!!!!!!!!"
66         echo "!!!!!!!!! S L E E P  $val"
67         echo "!!!!!!!!!                                                              !!!!!!!!!!!!!!!!!!!"
68         echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
69         echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
70         sleep $val
71         echo " Sleep completed "
72   fi
73 }
74
75 remount_count=0
76 maybe_remount_count=0
77
78 maybe_remount(){
79   local val
80   val=$(($(random_number) % 10000))
81   maybe_remount_count=$(($maybe_remount_count+1))
82   
83   if [ $val -le 1 ] ; then
84         remount_count=$(($remount_count+1))
85         echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
86         echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
87         echo "!!!!!!!!!                                                              !!!!!!!!!!!!!!!!!!!"
88         echo "!!!!!!!!! R E M O U N T $remount_count from $maybe_remount_count"
89         echo "!!!!!!!!!                                                              !!!!!!!!!!!!!!!!!!!"
90         echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
91         echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
92   
93         umount $MOUNT_PT
94         mount -t yaffs2 $MTD $MOUNT_PT
95   fi
96
97 }
98
99
100
101 make_space_slow(){
102    
103    update_df_percent
104    
105
106    calc_low_water       
107
108    echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
109    echo "!!!!! Slowly reduce usage from $df_percent percent to $low_water percent"
110    echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
111    echo "!!!!!!"
112    while [ $df_percent -ge $low_water ] ; do
113
114       DELETE_FILE=`ls $TEST_DIR | head -n 1`
115       rm -rf "$TEST_DIR/$DELETE_FILE"
116
117       update_df_percent
118       maybe_sleep
119       maybe_remount
120    done
121 }
122
123 make_space_fast(){
124    
125    calc_low_water
126    update_df_percent
127
128    echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
129    echo "!!!!! Quickly reduce usage from $df_percent percent to $low_water percent"
130    echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
131    echo "!!!!!!"
132    while [ $df_percent -ge $low_water ] ; do
133
134       file_prefix=$(($(random_number) % 100))
135       rm -rf $TEST_DIR/dummy $TEST_DIR/$file_prefix*
136
137       update_df_percent
138       maybe_sleep
139       maybe_remount
140    done
141 }
142
143 make_space(){
144    local xx
145    xx=$(($(random_number) % 100))
146    update_df_percent
147
148    if [ $df_percent -gt $MIN_LOW_WATER ] ; then
149
150            if [ $xx -lt 50 ] ; then
151                 make_space_slow
152            else
153                 make_space_fast
154            fi
155   fi
156 }
157
158 periodic_action(){
159
160   maybe_sleep
161   maybe_remount
162 }
163
164
165
166 drop_caches(){
167         echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
168         echo "!!!!!!!!!!! D R O P   C A C H E S"
169         echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
170         sync
171         echo 3 > /proc/sys/vm/drop_caches
172 }
173
174 maybe_drop_caches(){
175         local x
176         x=$(($(random_number) % 10000))
177         if [ $x -eq 99 ] ; then
178                 drop_caches
179         fi
180 }
181
182 create_files(){
183    
184    update_df_percent
185    
186    if [ $df_percent -lt $MAX_HIGH_WATER ] ; then
187    
188            calc_high_water
189            
190            rm -f $SUM_FILE
191
192            for i in $(find $TEST_DIR) ; do
193                 [ -f $i ] && md5sum $i >> $SUM_FILE
194            done
195
196            echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
197            echo "!!!!! Fill from $df_percent percent to $high_water percent"
198            echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
199            chunks=$(($(random_number) % 100))
200
201         
202            update_df_percent
203            
204            skip_file_create=0
205            skip_dir_create=0
206
207            while [ $df_percent -le $high_water ] ; do
208            
209                 if [ $skip_file_create -lt 1 ] ; then
210                         fsize=$(($(random_number)*$chunks+1))
211                         echo "!!!!!! File size is $fsize"
212                         dd if=/dev/urandom of=$REF_FILE bs=$fsize count=1
213                         REF_SUM=$(md5sum $REF_FILE | cut -f1 -d" " )
214                         skip_file_create=$(($(random_number) % 50))
215                 else
216                         skip_file_create=$(($skip_file_create-1))
217                 fi
218
219                 if [ $skip_dir_create -lt 1 ] ; then
220                         dir_name=$TEST_DIR/$(random_number)-dir
221                         mkdir -p $dir_name
222                         echo "Creating in directory $dir_name"
223                         skip_dir_create=$(($(random_number) % 100))
224                 else
225                         skip_dir_create=$(($skip_dir_create-1))
226                 fi
227            
228                 FNAME=$dir_name/$(random_number)-$(random_number)-$(random_number)
229                 cp $REF_FILE $FNAME
230                 FILE_SUM=$(md5sum $FNAME | cut -f1 -d" ")
231                 md5sum $FNAME >> $SUM_FILE
232
233                 if [ "$FILE_SUM" != "$REF_SUM" ] ; then
234                         echo "File comparison between $REF_FILE and $FNAME failed $REF_SUM - $FILE_SUM"
235                         exit 1
236                 fi
237
238                 update_df_percent
239                 maybe_sleep
240                 maybe_remount
241                 maybe_drop_caches
242            done
243
244            if ! md5sum -c $SUM_FILE > /dev/null ; then
245                 echo "File verification failed"
246                 exit 1
247            fi
248    fi
249         
250 }
251
252 fill_disk(){
253         echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
254         echo "!!!!!!!!!!! F I L L   D I S K"
255         echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
256         full=0
257         while [ $full -eq 0 ] ; do
258                 dd if=/dev/urandom of=$TEST_DIR/$(random_number)-$(random_number)-fill bs=1000 count=50 2> /dev/null || full=1 || true
259         done
260                 
261 }
262
263 rm -rf $TEST_DIR/*
264 mkdir -p $TEST_DIR
265 touch $TEST_DIR/dont-want-empty-dir
266 create_files
267
268 while true ; do
269
270         x=$(($(random_number) % 100))
271         if [ $x -lt 50 ] ; then
272                 create_files
273         fi
274
275         x=$(($(random_number) % 100))
276         if [ $x -lt 50 ] ; then
277                 make_space
278         fi
279
280         x=$(($(random_number) % 100))
281         if [ $x -lt 5 ] ; then 
282                 fill_disk
283         fi
284
285         x=$(($(random_number) % 100))
286         if [ $x -lt 1 ] ; then
287                 drop_caches
288         fi
289 done