Whamcloud - gitweb
LU-1866 lfsck: enhance otable-based iteration
[fs/lustre-release.git] / lustre / tests / kbuild
1 #! /bin/sh
2
3 #
4 #  lustre/lustre/tests/kbuild
5 #
6 #  Copyright (C) 2005 Cluster File Systems, Inc.
7 #
8 #  Author: Nikita Danilov <nikita@clusterfs.com>
9 #
10 #  This file is part of Lustre, http://www.lustre.org.
11 #
12 #         Lustre is free  software; you can  redistribute it and/or  modify it
13 #         under the terms of  version 2 of  the GNU General Public License  as
14 #         published by the Free Software Foundation.
15 #
16 #         Lustre  is distributed  in the  hope  that it  will  be useful,  but
17 #         WITHOUT  ANY   WARRANTY; without  even    the  implied  warranty  of
18 #         MERCHANTABILITY or FITNESS FOR   A PARTICULAR PURPOSE.  See the  GNU
19 #         General Public License for more details.
20 #
21 #         You  should have received a copy  of the  GNU General Public License
22 #         along with  Lustre; if not, write to  the Free  Software Foundation,
23 #         Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #
25 #
26 #  kbuild is a swiss-army linux kernel build script. Its purpose is to run
27 #  automated kernel builds on given target file system (presumably Lustre) to
28 #  measure file system performance and, occasionally, correctness.
29 #
30 #  Usual kernel build doesn't not stress file system, because the bottleneck
31 #  is CPU consumption by the user level (compiler). To work around this,
32 #  kbuild uses ccache(1) that eliminates most of CPU load by the compiler,
33 #  once the cache is primed.
34 #
35 #  Options:
36
37 function usage()
38 {
39         cat <<EOF
40        $pname --- builds a kernel.
41
42 Usage: $pname [-s <source>]         \\
43               [-t <target>]         \\
44               [-m <make-options>]   \\
45               [-i <iterations>]     \\
46               [-v <verbosity>]      \\
47               [-c <config-target>]  \\
48               [-S]                  \\
49               [-C <config-file>]
50
51    -s <source>        source of kernel to build. This can be:
52
53                         . path to directory;
54
55                         . tar.gz, .tgz, or .tar.bz2 archive;
56
57                         . ftp or http URL to the source archive;
58
59                       defaults to "$src".
60
61    -t <target>        target directory, where build process takes place.
62                       Defaults to "$tgt".
63
64    -m <make-options>  additional options supplied to each make invocation.
65                       Defaults to "$mopt"
66
67    -c <config-target> kernel makefile target to invoke to configure kernel
68                       (defconfig, allyesconfig, allmodconfig, etc.). This
69                       option conflicts with -C <config-file>. Defaults to
70                       "$mconfig".
71
72    -C <config-file>   use given .config file as kernel configuration. Not
73                       used by default.
74
75    -S                 skip kernel copying: kernel source is already unpacked
76                       in $target. Defaults to false.
77
78    -v                 increase verbosity level.
79
80 Examples:
81
82   $pname -s /usr/src/linux-2.6.10-base.tar.gz -t /mnt/lustre2 \\
83          -m -j4 -C /usr/src/.config.fc3
84
85   $pname -s ftp://ftp.clusterfs.com/pub/kernels/fc3-2.6/linux-2.6.10-base.tgz \\
86          -m -j4 -c defconfig -vvv
87
88 EOF
89         exit 1
90 }
91
92 #
93 #  Results:
94 #
95 #  The output of kbuild are times as reported by time. First line is for build
96 #  that fills the ccache cache (that is also located on the target file
97 #  system). Consecutive times are repeated builds that reuse ccache
98 #  cache. Number of iteration is set through -i option. Example output:
99 #  
100 #  R 783.757 S 319.615 U 281.720
101 #  R 540.823 S 277.387 U 54.168
102 #  R 557.762 S 263.566 U 53.222
103 #  R 543.877 S 278.569 U 54.412
104 #  R 544.455 S 279.096 U 53.697
105 #  R 545.445 S 280.546 U 53.943
106 #
107 #  Notes:
108 #
109 #  Kernel builds can be quite slow as example output above shows. Create your
110 #  own .config file to build smaller kernel.
111 #
112 #
113
114 OPTVAL=`getopt -o s:m:i:t:vc:SC:h -n 'kbuild' -- "$@"` || usage
115
116 # Note the quotes around `$OPTVAL': they are essential!
117 eval set -- "$OPTVAL"
118
119 LOG_CRIT=0
120 LOG_ERROR=1
121 LOG_WARN=2
122 LOG_INFO=3
123 LOG_PROGRESS=4
124 LOG_TRACE=5
125 LOG_ALL=6
126 LOG_DEBUG=7
127
128 src=/usr/src/linux
129 tgt=/mnt/lustre
130 verbose=$LOG_CRIT
131
132 pname=$(basename $0)
133
134 mopt=""
135 mconfig=allyesconfig
136 it=3
137 lfile=/tmp/$pname-tmp-log.$$
138 skip_copy=0
139 conf_file=""
140
141 while : ;do
142         case "$1" in
143                 -s)
144                         src="$2"
145                         shift 2
146                 ;;
147                 -t)
148                         tgt="$2"
149                         shift 2
150                 ;;
151                 -m)
152                         mopt="$2"
153                         shift 2
154                 ;;
155                 -C)
156                         conf_file="$2"
157                         shift 2
158                 ;;
159                 -i)
160                         it="$2"
161                         shift 2
162                 ;;
163                 -c)
164                         mconfig="$2"
165                         shift 2
166                 ;;
167                 -S)
168                         skip_copy=1
169                         shift
170                 ;;
171                 -v)
172                         verbose=$(($verbose + 1))
173                         shift
174                 ;;
175                 -h)
176                         usage
177                 ;;
178                 --) 
179                         shift 
180                         break 
181                 ;;
182                 *) 
183                         echo "Internal error!" 
184                         usage
185                 ;;
186         esac
187 done
188
189 [ $verbose -ge $LOG_ALL ] && set -x
190
191
192 function warning()
193 {
194         echo WARNING $pname: $*
195 }
196
197 function fail()
198 {
199         local rc
200
201         rc=$1
202         shift
203         warning $* ... failing.
204         exit $rc
205 }
206
207 function log()
208 {
209         local level
210
211         level=$1
212         shift
213         if [ $verbose -ge $level ] ;then
214                echo $*
215         fi
216 }
217
218 function doquiet()
219 {
220         local cmd
221
222         cmd="$*"
223         echo >> $lfile
224         echo ---- start: $(date +"%Y-%m-%d %H:%M:%S") ---- >> $lfile
225         for i in $cmd ;do
226                 echo "ARG: $i" >> $lfile
227         done
228         log $LOG_PROGRESS "Running '$cmd'..."
229         $cmd >>$lfile 2>&1 || \
230                 fail 1 "Errors while running '$cmd'. See $lfile for transcript"
231         log $LOG_PROGRESS "Finished '$cmd'."
232         echo ---- done: $(date +"%Y-%m-%d %H:%M:%S") ---- >> $lfile
233 }
234
235 function dotime()
236 {
237         local cmd
238
239         cmd="$*"
240         export TIMEFORMAT="R %3R S %3S U %3U"
241         time $cmd
242 }
243
244 ccache_dir=$tgt/ccache_dir
245 cc_script=$tgt/cc_script
246
247 which ccache >/dev/null || fail 2 "No ccache found"
248 mkdir -p $ccache_dir || fail 3 "Cannot create $ccache_dir"
249
250 export CCACHE_DIR=$ccache_dir
251
252 # start the stuff
253
254 cd $tgt || fail 4 "Cannot cd into $tgt"
255
256 echo '#! /bin/sh'   >  $cc_script || fail 5 "Cannot write into $cc_script"
257 echo 'ccache cc $*' >> $cc_script || fail 6 "Cannot append to $cc_script"
258 chmod u+rx $cc_script || fail 7 "Cannot chmod u+rx $cc_script"
259
260 cc_opt="CC=$cc_script"
261
262 [ $verbose -ge $LOG_TRACE ] && vopt=-v
263
264 if [ $skip_copy -eq 0 ] ;then
265         case "$src" in
266         ftp://*|http://*)
267                 wget -c $src
268                 src=$(basename $src)
269                 ;;
270         esac
271
272         case "$src" in
273         */)
274                 log $LOG_PROGRESS "Copying directory $src into $tgt"
275                 cp -a$vopt "$src" .
276                 ;;
277         *.tar.gz|*.tgz)
278                 tar xzf "$src" $vopt
279                 ;;
280         *.tar.bz2)
281                 tar xjf "$src" $vopt
282                 ;;
283         *)
284                 fail 10 "No $src"
285                 ;;
286         esac
287 fi
288
289 cd linux-* || fail 20 "Cannot change to linux-* from $PWD"
290
291 function dokernel()
292 {
293         doquiet make $mopt mrproper
294         if [ x$conf_file = x ] ;then
295                 doquiet make $mopt $mconfig
296         else
297                 cp $conf_file .config   || fail 8 "Cannot copy $conf_file"
298                 ls -l .config
299                 doquiet make $mopt oldconfig
300         fi
301
302         dotime doquiet make $mopt $cc_opt bzImage modules
303 }
304
305 log $LOG_PROGRESS Fill the cache...
306
307 dokernel
308
309 for i in $(seq 1 $it) ;do
310         log $LOG_PROGRESS Iteration $i...
311         dokernel
312 done