summaryrefslogtreecommitdiff
path: root/scripts/contrib/build-perf-test.sh
blob: 0af2cfea0e969fd06afe50cde8cc8a534fb337cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/bin/bash
#
# This script runs a series of tests  (with and without sstate) and reports build time (and tmp/ size)
# 
# Build performance test script
#
# Copyright 2013 Intel Corporation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#
# AUTHORS:
# Stefan Stanacar <stefanx.stanacar@intel.com>


ME=$(basename $0)

#
# usage and setup
#

usage () {
cat << EOT
Usage: $ME [-h]
       $ME [-c <commit>] [-v] [-m <val>] [-j <val>] [-t <val>] [-i <image-name>] [-d <path>]
Options:
        -h
                Display this help and exit.
        -c <commit>
                git checkout <commit> before anything else
        -v
                Show bitbake output, don't redirect it to a log.
        -m <machine>
                Value for MACHINE. Default is qemux86.
        -j <val>
                Value for PARALLEL_MAKE. Default is 8. 
        -t <val>
                Value for BB_NUMBER_THREADS. Default is 8.
        -i <image-name>
                Instead of timing against core-image-sato, use <image-name>
        -d <path>
                Use <path> as DL_DIR
        -p <githash>
                Cherry pick githash onto the commit
                
Note: current working directory must be inside a poky git clone.
 
EOT
}


if clonedir=$(git rev-parse --show-toplevel); then
        cd $clonedir
else
        echo "The current working dir doesn't seem to be a poky git clone. Please cd there before running $ME"
        exit 1
fi

IMAGE="core-image-sato"
verbose=0
dldir=
commit=
pmake=
cherrypicks=
while getopts "hvc:m:j:t:i:d:p:" opt; do
        case $opt in
                h)      usage
                        exit 0
                        ;;
                v)      verbose=1
                        ;;
                c)      commit=$OPTARG
                        ;;
                m)      export MACHINE=$OPTARG
                        ;;
                j)      pmake=$OPTARG
                        ;;
                t)      export BB_NUMBER_THREADS=$OPTARG
                        ;;
                i)      IMAGE=$OPTARG
                        ;;
                d)      dldir=$OPTARG
                        ;;
                p)      cherrypicks="$cherrypicks $OPTARG"
                        ;;
                *)      usage
                        exit 1
                        ;;                      
        esac
done


#drop cached credentials and test for sudo access without a password
sudo -k -n ls > /dev/null 2>&1
reqpass=$?
if [ $reqpass -ne 0 ]; then
    echo "The script requires sudo access to drop caches between builds (echo 3 > /proc/sys/vm/drop_caches)"
    read -s -p "Please enter your sudo password: " pass
    echo
fi

if [ -n "$commit" ]; then
            echo "git checkout $commit"
            git pull > /dev/null 2>&1
            git checkout $commit || exit 1
            git pull > /dev/null 2>&1
fi

if [ -n "$cherrypicks" ]; then
    for c in $cherrypicks; do
        git cherry-pick $c
    done
fi

rev=$(git rev-parse --short HEAD)  || exit 1
OUTDIR="$clonedir/build-perf-test/results-$rev-`date "+%Y%m%d%H%M%S"`"
BUILDDIR="$OUTDIR/build"
resultsfile="$OUTDIR/results.log"
bboutput="$OUTDIR/bitbake.log"
myoutput="$OUTDIR/output.log"
globalres="$clonedir/build-perf-test/globalres.log"

mkdir -p $OUTDIR || exit 1
                                
log () {
    local msg="$1"
    echo "`date`: $msg" | tee -a $myoutput
}


#
# Config stuff
#

rev=$(git rev-parse HEAD) || exit 1
log "Git revision is $rev"

source ./oe-init-build-env $OUTDIR/build >/dev/null || exit 1
cd $OUTDIR/build

[ -n "$MACHINE" ] || export MACHINE="qemux86"
[ -n "$BB_NUMBER_THREADS" ] || export BB_NUMBER_THREADS="8"

if [ -n "$pmake" ]; then
        export PARALLEL_MAKE="-j $pmake"
else
        export PARALLEL_MAKE="-j 8"
fi

if [ -n "$dldir" ]; then
    echo "DL_DIR = \"$dldir\"" >> conf/local.conf
else
    echo "DL_DIR = \"$clonedir/build-perf-test/downloads\"" >> conf/local.conf
fi

# Sometimes I've noticed big differences in timings for the same commit, on the same machine
# Disabling the network sanity check helps a bit (because of my crappy network connection and/or proxy)
echo "CONNECTIVITY_CHECK_URIS =\"\"" >> conf/local.conf

#
# Functions
#

declare -a TIMES
time_count=0

bbtime () {
    log "   Timing: bitbake $1"

    if [ $verbose -eq 0 ]; then 
        /usr/bin/time -v -o $resultsfile bitbake "$1" >> $bboutput
    else
        /usr/bin/time -v -o $resultsfile bitbake "$1" 
    fi
    ret=$?
    if [ $ret -eq 0 ]; then
        t=`grep wall $resultsfile | sed 's/.*m:ss): //'`
        log "   TIME: $t"
        TIMES[(( time_count++ ))]="$t"
    else
        log "ERROR: exit status was non-zero, will report time as 0."
        TIMES[(( time_count++ ))]="0"
    fi
    
    #time by default overwrites the output file and we  want to keep the results
    #it has an append option but I don't want to clobber the results in the same file
    i=`ls $OUTDIR/results.log* |wc -l`
    mv $resultsfile "${resultsfile}.${i}"
    log "More stats can be found in ${resultsfile}.${i}"    
}

#we don't time bitbake here
bbnotime () {
    log "   Running: bitbake $1"
    if [ $verbose -eq 0 ]; then
        bitbake "$1" >> $bboutput
    else
        bitbake "$1" 
    fi
    ret=$?
    if [ $ret -eq 0 ]; then
        log "   Finished bitbake $1"
    else
        log "ERROR: exit status was non-zero. Exit.."
        exit $?
    fi

}

do_rmtmp() {
    log "   Removing tmp"
    rm -rf bitbake.lock pseudodone conf/sanity_info cache tmp
}
do_rmsstate () {
    log "   Removing sstate-cache"
    rm -rf sstate-cache
}
do_sync () {
    log "   Syncing and dropping caches"
    sync; sync
    if [ $reqpass -eq 0 ]; then
        sudo sh -c "echo 3 > /proc/sys/vm/drop_caches"
    else
        echo "$pass" | sudo -S sh -c "echo 3 > /proc/sys/vm/drop_caches"
        echo
    fi
    sleep 3
}

write_results() {
    echo -n "`uname -n`,$rev," >> $globalres
    for i in "${TIMES[@]}"; do
        echo -n "$i," >> $globalres
    done
    echo >> $globalres
    sed -i '$ s/,$//' $globalres
}

####

#
# Test 1
# Measure: Wall clock of "bitbake core-image-sato" and size of tmp/dir (w/o rm_work and w/ rm_work)
# Pre: Downloaded sources, no sstate
# Steps:
#     Part1:
#        - fetchall 
#        - clean build dir
#        - time bitbake core-image-sato
#        - collect data
#     Part2:
#        - bitbake virtual/kernel -c cleansstate
#        - time bitbake virtual/kernel
#     Part3:
#        - add INHERIT to local.conf
#        - clean build dir
#        - build
#        - report size, remove INHERIT

test1_p1 () {
    log "Running Test 1, part 1/3: Measure wall clock of bitbake $IMAGE and size of tmp/ dir"
    bbnotime "$IMAGE -c fetchall"
    do_rmtmp
    do_rmsstate
    do_sync
    bbtime "$IMAGE"
    log "SIZE of tmp dir is: `du -sh tmp | sed 's/tmp//'`"
    log "Buildstats are saved in $OUTDIR/buildstats-test1"
    mv tmp/buildstats $OUTDIR/buildstats-test1
}


test1_p2 () {
    log "Running Test 1, part 2/3: bitbake virtual/kernel -c cleansstate and time bitbake virtual/kernel"
    bbnotime "virtual/kernel -c cleansstate"
    do_sync
    bbtime "virtual/kernel"
}

test1_p3 () {
    log "Running Test 1, part 3/3: Build $IMAGE w/o sstate and report size of tmp/dir with rm_work enabled"
    echo "INHERIT += \"rm_work\"" >> conf/local.conf
    do_rmtmp
    do_rmsstate
    do_sync
    bbtime "$IMAGE"
    sed -i 's/INHERIT += \"rm_work\"//' conf/local.conf
    log "SIZE of tmp dir is: `du -sh tmp | sed 's/tmp//'`"
    log "Buildstats are saved in $OUTDIR/buildstats-test13"
    mv tmp/buildstats $OUTDIR/buildstats-test13
}


#
# Test 2
# Measure: Wall clock of "bitbake core-image-sato" and size of tmp/dir
# Pre: populated sstate cache

test2 () {
    # Assuming test 1 has run
    log "Running Test 2: Measure wall clock of bitbake $IMAGE -c rootfs with sstate"
    do_rmtmp
    do_sync
    bbtime "$IMAGE -c rootfs"
}


# Test 3
# parsing time metrics
#
#  Start with
#   i) "rm -rf tmp/cache; time bitbake -p"
#  ii) "rm -rf tmp/cache/default-eglibc/; time bitbake -p"
# iii) "time bitbake -p"


test3 () {
    log "Running Test 3: Parsing time metrics (bitbake -p)"
    log "   Removing tmp/cache && cache"
    rm -rf tmp/cache cache
    bbtime "-p"
    log "   Removing tmp/cache/default-eglibc/"
    rm -rf tmp/cache/default-eglibc/
    bbtime "-p"
    bbtime "-p"
}



# RUN!

test1_p1
test1_p2
test1_p3
test2
test3

# if we got til here write to global results
write_results

log "All done, cleaning up..."

do_rmtmp
do_rmsstate