xref: /OK3568_Linux_fs/yocto/poky/scripts/contrib/build-perf-test-wrapper.sh (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#!/bin/bash
2*4882a593Smuzhiyun#
3*4882a593Smuzhiyun# Build performance test script wrapper
4*4882a593Smuzhiyun#
5*4882a593Smuzhiyun# Copyright (c) 2016, Intel Corporation.
6*4882a593Smuzhiyun#
7*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0-only
8*4882a593Smuzhiyun#
9*4882a593Smuzhiyun# This script is a simple wrapper around the actual build performance tester
10*4882a593Smuzhiyun# script. This script initializes the build environment, runs
11*4882a593Smuzhiyun# oe-build-perf-test and archives the results.
12*4882a593Smuzhiyun
13*4882a593Smuzhiyunscript=`basename $0`
14*4882a593Smuzhiyunscript_dir=$(realpath $(dirname $0))
15*4882a593Smuzhiyunarchive_dir=~/perf-results/archives
16*4882a593Smuzhiyun
17*4882a593Smuzhiyunusage () {
18*4882a593Smuzhiyuncat << EOF
19*4882a593SmuzhiyunUsage: $script [-h] [-c COMMITISH] [-C GIT_REPO]
20*4882a593Smuzhiyun
21*4882a593SmuzhiyunOptional arguments:
22*4882a593Smuzhiyun  -h                show this help and exit.
23*4882a593Smuzhiyun  -a ARCHIVE_DIR    archive results tarball here, give an empty string to
24*4882a593Smuzhiyun                    disable tarball archiving (default: $archive_dir)
25*4882a593Smuzhiyun  -c COMMITISH      test (checkout) this commit, <branch>:<commit> can be
26*4882a593Smuzhiyun                    specified to test specific commit of certain branch
27*4882a593Smuzhiyun  -C GIT_REPO       commit results into Git
28*4882a593Smuzhiyun  -d DOWNLOAD_DIR   directory to store downloaded sources in
29*4882a593Smuzhiyun  -E EMAIL_ADDR     send email report
30*4882a593Smuzhiyun  -g GLOBALRES_DIR  where to place the globalres file
31*4882a593Smuzhiyun  -P GIT_REMOTE     push results to a remote Git repository
32*4882a593Smuzhiyun  -R DEST           rsync reports to a remote destination
33*4882a593Smuzhiyun  -w WORK_DIR       work dir for this script
34*4882a593Smuzhiyun                    (default: GIT_TOP_DIR/build-perf-test)
35*4882a593Smuzhiyun  -x                create xml report (instead of json)
36*4882a593SmuzhiyunEOF
37*4882a593Smuzhiyun}
38*4882a593Smuzhiyun
39*4882a593Smuzhiyunget_os_release_var () {
40*4882a593Smuzhiyun    ( source /etc/os-release; eval echo '$'$1 )
41*4882a593Smuzhiyun}
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun# Parse command line arguments
45*4882a593Smuzhiyuncommitish=""
46*4882a593Smuzhiyunoe_build_perf_test_extra_opts=()
47*4882a593Smuzhiyunoe_git_archive_extra_opts=()
48*4882a593Smuzhiyunwhile getopts "ha:c:C:d:E:g:P:R:w:x" opt; do
49*4882a593Smuzhiyun    case $opt in
50*4882a593Smuzhiyun        h)  usage
51*4882a593Smuzhiyun            exit 0
52*4882a593Smuzhiyun            ;;
53*4882a593Smuzhiyun        a)  mkdir -p "$OPTARG"
54*4882a593Smuzhiyun            archive_dir=`realpath -s "$OPTARG"`
55*4882a593Smuzhiyun            ;;
56*4882a593Smuzhiyun        c)  commitish=$OPTARG
57*4882a593Smuzhiyun            ;;
58*4882a593Smuzhiyun        C)  mkdir -p "$OPTARG"
59*4882a593Smuzhiyun            results_repo=`realpath -s "$OPTARG"`
60*4882a593Smuzhiyun            ;;
61*4882a593Smuzhiyun        d)  download_dir=`realpath -s "$OPTARG"`
62*4882a593Smuzhiyun            ;;
63*4882a593Smuzhiyun        E)  email_to="$OPTARG"
64*4882a593Smuzhiyun            ;;
65*4882a593Smuzhiyun        g)  mkdir -p "$OPTARG"
66*4882a593Smuzhiyun            globalres_dir=`realpath -s "$OPTARG"`
67*4882a593Smuzhiyun            ;;
68*4882a593Smuzhiyun        P)  oe_git_archive_extra_opts+=("--push" "$OPTARG")
69*4882a593Smuzhiyun            ;;
70*4882a593Smuzhiyun        R)  rsync_dst="$OPTARG"
71*4882a593Smuzhiyun            ;;
72*4882a593Smuzhiyun        w)  base_dir=`realpath -s "$OPTARG"`
73*4882a593Smuzhiyun            ;;
74*4882a593Smuzhiyun        x)  oe_build_perf_test_extra_opts+=("--xml")
75*4882a593Smuzhiyun            ;;
76*4882a593Smuzhiyun        *)  usage
77*4882a593Smuzhiyun            exit 1
78*4882a593Smuzhiyun            ;;
79*4882a593Smuzhiyun    esac
80*4882a593Smuzhiyundone
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun# Check positional args
83*4882a593Smuzhiyunshift "$((OPTIND - 1))"
84*4882a593Smuzhiyunif [ $# -ne 0 ]; then
85*4882a593Smuzhiyun    echo "ERROR: No positional args are accepted."
86*4882a593Smuzhiyun    usage
87*4882a593Smuzhiyun    exit 1
88*4882a593Smuzhiyunfi
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun# Open a file descriptor for flock and acquire lock
91*4882a593SmuzhiyunLOCK_FILE="/tmp/oe-build-perf-test-wrapper.lock"
92*4882a593Smuzhiyunif ! exec 3> "$LOCK_FILE"; then
93*4882a593Smuzhiyun    echo "ERROR: Unable to open loemack file"
94*4882a593Smuzhiyun    exit 1
95*4882a593Smuzhiyunfi
96*4882a593Smuzhiyunif ! flock -n 3; then
97*4882a593Smuzhiyun    echo "ERROR: Another instance of this script is running"
98*4882a593Smuzhiyun    exit 1
99*4882a593Smuzhiyunfi
100*4882a593Smuzhiyun
101*4882a593Smuzhiyunecho "Running on `uname -n`"
102*4882a593Smuzhiyunif ! git_topdir=$(git rev-parse --show-toplevel); then
103*4882a593Smuzhiyun        echo "The current working dir doesn't seem to be a git clone. Please cd there before running `basename $0`"
104*4882a593Smuzhiyun        exit 1
105*4882a593Smuzhiyunfi
106*4882a593Smuzhiyun
107*4882a593Smuzhiyuncd "$git_topdir"
108*4882a593Smuzhiyun
109*4882a593Smuzhiyunif [ -n "$commitish" ]; then
110*4882a593Smuzhiyun    echo "Running git fetch"
111*4882a593Smuzhiyun    git fetch &> /dev/null
112*4882a593Smuzhiyun    git checkout HEAD^0 &> /dev/null
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun    # Handle <branch>:<commit> format
115*4882a593Smuzhiyun    if echo "$commitish" | grep -q ":"; then
116*4882a593Smuzhiyun        commit=`echo "$commitish" | cut -d":" -f2`
117*4882a593Smuzhiyun        branch=`echo "$commitish" | cut -d":" -f1`
118*4882a593Smuzhiyun    else
119*4882a593Smuzhiyun        commit="$commitish"
120*4882a593Smuzhiyun        branch="$commitish"
121*4882a593Smuzhiyun    fi
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun    echo "Checking out $commitish"
124*4882a593Smuzhiyun    git branch -D $branch &> /dev/null
125*4882a593Smuzhiyun    if ! git checkout -f $branch &> /dev/null; then
126*4882a593Smuzhiyun        echo "ERROR: Git checkout failed"
127*4882a593Smuzhiyun        exit 1
128*4882a593Smuzhiyun    fi
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun    # Check that the specified branch really contains the commit
131*4882a593Smuzhiyun    commit_hash=`git rev-parse --revs-only $commit --`
132*4882a593Smuzhiyun    if [ -z "$commit_hash" -o "`git merge-base $branch $commit`" != "$commit_hash" ]; then
133*4882a593Smuzhiyun        echo "ERROR: branch $branch does not contain commit $commit"
134*4882a593Smuzhiyun        exit 1
135*4882a593Smuzhiyun    fi
136*4882a593Smuzhiyun    git reset --hard $commit > /dev/null
137*4882a593Smuzhiyunfi
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun# Determine name of the current branch
140*4882a593Smuzhiyunbranch=`git symbolic-ref HEAD 2> /dev/null`
141*4882a593Smuzhiyun# Strip refs/heads/
142*4882a593Smuzhiyunbranch=${branch:11}
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun# Setup build environment
145*4882a593Smuzhiyunif [ -z "$base_dir" ]; then
146*4882a593Smuzhiyun    base_dir="$git_topdir/build-perf-test"
147*4882a593Smuzhiyunfi
148*4882a593Smuzhiyunecho "Using working dir $base_dir"
149*4882a593Smuzhiyun
150*4882a593Smuzhiyunif [ -z "$download_dir" ]; then
151*4882a593Smuzhiyun    download_dir="$base_dir/downloads"
152*4882a593Smuzhiyunfi
153*4882a593Smuzhiyunif [ -z "$globalres_dir" ]; then
154*4882a593Smuzhiyun    globalres_dir="$base_dir"
155*4882a593Smuzhiyunfi
156*4882a593Smuzhiyun
157*4882a593Smuzhiyuntimestamp=`date "+%Y%m%d%H%M%S"`
158*4882a593Smuzhiyungit_rev=$(git rev-parse --short HEAD)  || exit 1
159*4882a593Smuzhiyunbuild_dir="$base_dir/build-$git_rev-$timestamp"
160*4882a593Smuzhiyunresults_dir="$base_dir/results-$git_rev-$timestamp"
161*4882a593Smuzhiyunglobalres_log="$globalres_dir/globalres.log"
162*4882a593Smuzhiyunmachine="qemux86"
163*4882a593Smuzhiyun
164*4882a593Smuzhiyunmkdir -p "$base_dir"
165*4882a593Smuzhiyunsource ./oe-init-build-env $build_dir >/dev/null || exit 1
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun# Additional config
168*4882a593Smuzhiyunauto_conf="$build_dir/conf/auto.conf"
169*4882a593Smuzhiyunecho "MACHINE = \"$machine\"" > "$auto_conf"
170*4882a593Smuzhiyunecho 'BB_NUMBER_THREADS = "8"' >> "$auto_conf"
171*4882a593Smuzhiyunecho 'PARALLEL_MAKE = "-j 8"' >> "$auto_conf"
172*4882a593Smuzhiyunecho "DL_DIR = \"$download_dir\"" >> "$auto_conf"
173*4882a593Smuzhiyun# Disabling network sanity check slightly reduces the variance of timing results
174*4882a593Smuzhiyunecho 'CONNECTIVITY_CHECK_URIS = ""' >> "$auto_conf"
175*4882a593Smuzhiyun# Possibility to define extra settings
176*4882a593Smuzhiyunif [ -f "$base_dir/auto.conf.extra" ]; then
177*4882a593Smuzhiyun    cat "$base_dir/auto.conf.extra" >> "$auto_conf"
178*4882a593Smuzhiyunfi
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun# Run actual test script
181*4882a593Smuzhiyunoe-build-perf-test --out-dir "$results_dir" \
182*4882a593Smuzhiyun                   --globalres-file "$globalres_log" \
183*4882a593Smuzhiyun                   "${oe_build_perf_test_extra_opts[@]}" \
184*4882a593Smuzhiyun                   --lock-file "$base_dir/oe-build-perf.lock"
185*4882a593Smuzhiyun
186*4882a593Smuzhiyuncase $? in
187*4882a593Smuzhiyun    1)  echo "ERROR: oe-build-perf-test script failed!"
188*4882a593Smuzhiyun        exit 1
189*4882a593Smuzhiyun        ;;
190*4882a593Smuzhiyun    2)  echo "NOTE: some tests failed!"
191*4882a593Smuzhiyun        ;;
192*4882a593Smuzhiyunesac
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun# Commit results to git
195*4882a593Smuzhiyunif [ -n "$results_repo" ]; then
196*4882a593Smuzhiyun    echo -e "\nArchiving results in $results_repo"
197*4882a593Smuzhiyun    oe-git-archive \
198*4882a593Smuzhiyun        --git-dir "$results_repo" \
199*4882a593Smuzhiyun        --branch-name "{hostname}/{branch}/{machine}" \
200*4882a593Smuzhiyun        --tag-name "{hostname}/{branch}/{machine}/{commit_count}-g{commit}/{tag_number}" \
201*4882a593Smuzhiyun        --exclude "buildstats.json" \
202*4882a593Smuzhiyun        --notes "buildstats/{branch_name}" "$results_dir/buildstats.json" \
203*4882a593Smuzhiyun        "${oe_git_archive_extra_opts[@]}" \
204*4882a593Smuzhiyun        "$results_dir"
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun    # Generate test reports
207*4882a593Smuzhiyun    sanitized_branch=`echo $branch | tr / _`
208*4882a593Smuzhiyun    report_txt=`hostname`_${sanitized_branch}_${machine}.txt
209*4882a593Smuzhiyun    report_html=`hostname`_${sanitized_branch}_${machine}.html
210*4882a593Smuzhiyun    echo -e "\nGenerating test report"
211*4882a593Smuzhiyun    oe-build-perf-report -r "$results_repo" > $report_txt
212*4882a593Smuzhiyun    oe-build-perf-report -r "$results_repo" --html > $report_html
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun    # Send email report
215*4882a593Smuzhiyun    if [ -n "$email_to" ]; then
216*4882a593Smuzhiyun        echo "Emailing test report"
217*4882a593Smuzhiyun        os_name=`get_os_release_var PRETTY_NAME`
218*4882a593Smuzhiyun        "$script_dir"/oe-build-perf-report-email.py --to "$email_to" --subject "Build Perf Test Report for $os_name" --text $report_txt "${OE_BUILD_PERF_REPORT_EMAIL_EXTRA_ARGS[@]}"
219*4882a593Smuzhiyun    fi
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun    # Upload report files, unless we're on detached head
222*4882a593Smuzhiyun    if [ -n "$rsync_dst" -a -n "$branch" ]; then
223*4882a593Smuzhiyun        echo "Uploading test report"
224*4882a593Smuzhiyun        rsync $report_txt $report_html $rsync_dst
225*4882a593Smuzhiyun    fi
226*4882a593Smuzhiyunfi
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun
229*4882a593Smuzhiyunecho -ne "\n\n-----------------\n"
230*4882a593Smuzhiyunecho "Global results file:"
231*4882a593Smuzhiyunecho -ne "\n"
232*4882a593Smuzhiyun
233*4882a593Smuzhiyuncat "$globalres_log"
234*4882a593Smuzhiyun
235*4882a593Smuzhiyunif [ -n "$archive_dir" ]; then
236*4882a593Smuzhiyun    echo -ne "\n\n-----------------\n"
237*4882a593Smuzhiyun    echo "Archiving results in $archive_dir"
238*4882a593Smuzhiyun    mkdir -p "$archive_dir"
239*4882a593Smuzhiyun    results_basename=`basename "$results_dir"`
240*4882a593Smuzhiyun    results_dirname=`dirname "$results_dir"`
241*4882a593Smuzhiyun    tar -czf "$archive_dir/`uname -n`-${results_basename}.tar.gz" -C "$results_dirname" "$results_basename"
242*4882a593Smuzhiyunfi
243*4882a593Smuzhiyun
244*4882a593Smuzhiyunrm -rf "$build_dir"
245*4882a593Smuzhiyunrm -rf "$results_dir"
246*4882a593Smuzhiyun
247*4882a593Smuzhiyunecho "DONE"
248