File: parallel.bash

package info (click to toggle)
bladerf 0.2024.05-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 245,984 kB
  • sloc: ansic: 361,923; vhdl: 28,167; tcl: 14,424; python: 3,668; sh: 1,811; makefile: 1,255; xml: 1,020; cpp: 473; asm: 158; csh: 18
file content (213 lines) | stat: -rw-r--r-- 5,357 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env bash
#
# CI build orchestration for libbladeRF on Linux
#
# Copyright (c) 2018 Nuand LLC
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

# Usage:
# parallel.bash [--no-base] [target [target ...]]

# Enable job control
set -m

# Initial assumptions...
max_builds=4
max_load=$(nproc)
repository="nuand/bladerf-buildenv"

if [ -z "${max_load}" ]; then
    echo "nproc not found, assuming max load of 8"
    max_load=8
fi

# assuming the script is 3 levels down (host/misc/docker)
dfdir=$(dirname "${0}")
basedir=${dfdir}/../../..
buildscript=${dfdir}/build.bash

# globals
success=""
failure=""
rels=""

# command line arg
if [ "${1}" == "--no-base" ]; then
    echo "*** Won't rebuild the base image"
    _skip_base=1
    shift
fi

loadavg () {
    # usage: loadavg
    # outputs the current load average as an integer
    if [ -f "/proc/loadavg" ]; then
        awk '{print int($1+0)}' /proc/loadavg
    else
        echo 1
    fi
}

do_base () {
    # usage: do_base
    # builds the base image
    docker build -f host/misc/docker/_base.Dockerfile \
                 -t ${repository}:base \
                 .
    __status=$?
}

wait_for_jobs () {
    # usage: wait_for_jobs n
    # spins until there are fewer than n jobs active
    [ -z "${1}" ] && exit 1

    while [ "$(jobs | wc -l)" -ge "${1}" ]; do
        if [ -z "${waiting}" ]; then
            echo -n "*** Pausing: job count above threshold ($(jobs | wc -l) >= ${1})"
            waiting=1
        else
            echo -n "."
        fi
        sleep 5
    done

    if [ -n "${waiting}" ]; then
        echo ""
        unset waiting
    fi
}

wait_for_load () {
    # usage: wait_for_load
    # spins until the system load is less than n
    [ -z "${1}" ] && exit 1

    while [ "$(loadavg)" -ge "${1}" ]; do
        if [ -z "${waiting}" ]; then
            echo -n "*** Pausing: load average above threshold ($(loadavg) >= ${1})"
            waiting=1
        else
            echo -n "."
        fi
        sleep 5
    done

    if [ -n "${waiting}" ]; then
        echo ""
        unset waiting
    fi
}

do_spawn () {
    # usage: do_spawn dockerfile...
    # spawns a subprocess to execute a build, being mindful of limits
    [ -z "${1}" ] && exit 1

    dff=${1}
    rel=$(basename -s ".Dockerfile" ${dff})

    if [ ! -f "${dff}" ]; then
        echo "*** File doesn't exist: ${1}"
        exit 1
    fi

    wait_for_jobs ${max_builds}
    wait_for_load ${max_load}

    bash ${buildscript} ${rel} 2>&1 > ${LOGDIR}/${rel}.txt &
    __pid=$!
    echo "*** Spawned ${rel} pid ${__pid}. Jobs: $(jobs | wc -l), loadavg: $(loadavg)"
}

crawl_dir () {
    # usage: crawl_dir path...
    # outputs a space-seperated list of Dockerfiles in that path
    # e.g. crawl_dir host/misc/docker -> archlinux centos-7 clang-scan etc...
    [ -z "${1}" ] && exit 1

    _out=""

    for f in $(ls ${1}/*.Dockerfile); do
        _out="${_out} $(basename -s ".Dockerfile" ${f})"
    done

    echo ${_out}
}

LOGDIR=$(mktemp -d /tmp/bladerf-docker-logs.XXXXXXXXXX)
[ -z "${LOGDIR}" ] && echo "Couldn't create a tempdir for logs" && exit 1

echo "*** log dir: ${LOGDIR}"

if [ -z "${*}" ]; then
    # assume all files
    candidates=$(crawl_dir ${dfdir})
else
    # user selecting a few
    for candidate in ${*}; do
        candidates="${candidates} ${candidate}"
    done
fi

echo "*** Will build: ${candidates}"

cd ${basedir}

if [ -z "${_skip_base}" ]; then
    echo "*** Running initial build job: _base"
    do_base 2>&1 > ${LOGDIR}/_base.txt

    if [ "${__status}" -ne 0 ]; then
        echo "*** _base build failed, can't continue"
        cat ${LOGDIR}/_base.txt
        exit 1
    fi
fi

for dff in $candidates; do
    if [ "${dff}" == "_base" ]; then
        continue
    fi

    do_spawn ${dfdir}/${dff}.Dockerfile
    WAITPID="${WAITPID} ${__pid}"

    # sleep for a bit to provide settling time
    sleep 5
done

echo "*** Waiting for jobs to complete"
wait ${WAITPID}

echo "*** Build logs:"
for f in ${LOGDIR}/*.txt; do
    mylog="$(basename -s .txt ${f})"
    echo "*** BEGIN LOGS for ${mylog}"
    sed "s/.*/[${mylog}] &/" ${f}
done

echo -n "*** Successful: "
grep -h "^RESULT:SUCCESS:" ${LOGDIR}/*.txt | sed 's/RESULT:SUCCESS://' | xargs

echo -n "*** Failed: "
grep -h "^RESULT:FAILED:" ${LOGDIR}/*.txt | sed 's/RESULT:FAILED://' | xargs

rm -r ${LOGDIR}