blob: 55e0870d92d1b8e90e417578b24ad539ed7f6fb1 [file] [log] [blame]
Leonardo Sandoval9dfdd1b2020-08-06 17:08:11 -05001#!/usr/bin/env bash
Fathi Boudra422bf772019-12-02 11:10:16 +02002#
Zelalem219df412020-05-17 19:21:20 -05003# Copyright (c) 2019-2020, Arm Limited. All rights reserved.
Fathi Boudra422bf772019-12-02 11:10:16 +02004#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
8set -e
9
10# Enable job control to have background processes run in their own process
11# group. That way, we can kill a background process group in one go.
12set -m
13
14ci_root="$(readlink -f "$(dirname "$0")/..")"
15source "$ci_root/utils.sh"
16
17artefacts="${artefacts-$workspace/artefacts}"
18
19run_root="$workspace/run"
20pid_dir="$workspace/pids"
21
Manish V Badarkhe359b3ff2020-12-25 18:54:03 +000022# This variable avoids graceful termination of the model when
23# launched with the parameter 'bp.pl011_uart0.shutdown_on_eot=1'
24exit_on_model_param=0
25
26# Model exit parameter string
27model_exit_param_string="bp.pl011_uart0.shutdown_on_eot=1"
28
Fathi Boudra422bf772019-12-02 11:10:16 +020029mkdir -p "$pid_dir"
30mkdir -p "$run_root"
31
32kill_and_reap() {
33 local gid
Fathi Boudra422bf772019-12-02 11:10:16 +020034 # Kill an active process. Ignore errors
35 [ "$1" ] || return 0
36 kill -0 "$1" &>/dev/null || return 0
37
Zelalem219df412020-05-17 19:21:20 -050038 # Kill the children
39 kill -- "-$1" &>/dev/null || true
Fathi Boudra422bf772019-12-02 11:10:16 +020040 # Kill the group
Zelalem219df412020-05-17 19:21:20 -050041 { gid="$(awk '{print $5}' < /proc/$1/stat)";} 2>/dev/null || return
42 # For Code Coverage plugin it is needed to propagate
43 # the kill signal to the plugin in order to save
44 # the trace statistics.
45 if [ "${COVERAGE_ON}" == "1" ] || [ -n "$cc_enable" ]; then
46 kill -SIGTERM -- "-$gid" &>/dev/null || true
47 else
48 kill -SIGKILL -- "-$gid" &>/dev/null || true
49 fi
Fathi Boudra422bf772019-12-02 11:10:16 +020050 wait "$gid" &>/dev/null || true
51}
52
53# Perform clean up and ignore errors
54cleanup() {
55 local pid
Manish V Badarkhe359b3ff2020-12-25 18:54:03 +000056 local sig
Fathi Boudra422bf772019-12-02 11:10:16 +020057
Fathi Boudra422bf772019-12-02 11:10:16 +020058 pushd "$pid_dir"
59 set +e
Manish V Badarkhe359b3ff2020-12-25 18:54:03 +000060
61 sig=${1:-SIGINT}
62 echo "signal received: $sig"
63
64 # Avoid the model termination gracefully when the parameter 'exit_on_model_param'
65 # is set and test if exited successfully.
66 if [ "$exit_on_model_param" -eq 0 ] || [ "$sig" != "EXIT" ]; then
67 # Kill all background processes so far and wait for them
68 while read pid; do
69 pid="$(cat $pid)"
70 echo $pid
71 # Forcefully killing model process does not show statistical
72 # data (Host CPU time spent running in User and System). Safely
73 # kill the model by using SIGINT(^C) that helps in printing
74 # statistical data.
75 if [ "$pid" == "$model_pid" ] && [ "${COVERAGE_ON}" != "1" ]; then
76 model_cid=$(pgrep -P "$model_pid" | xargs)
77 # ignore errors
78 kill -SIGINT "$model_cid" &>/dev/null || true
79 # Allow some time to print data
80 sleep 2
81 else
82 kill_and_reap "$pid"
83 fi
84 done < <(find -name '*.pid')
85 fi
86
Fathi Boudra422bf772019-12-02 11:10:16 +020087 popd
88}
89
90# Launch a program. Have its PID saved in a file with given name with .pid
91# suffix. When the program exits, create a file with .success suffix, or one
92# with .fail if it fails. This function blocks, so the caller must '&' this if
93# they want to continue. Call must wait for $pid_dir/$name.pid to be created
94# should it want to read it.
95launch() {
96 local pid
97
98 "$@" &
99 pid="$!"
100 echo "$pid" > "$pid_dir/${name:?}.pid"
101 if wait "$pid"; then
102 touch "$pid_dir/$name.success"
103 else
104 touch "$pid_dir/$name.fail"
105 fi
106}
107
Manish V Badarkhe359b3ff2020-12-25 18:54:03 +0000108# Provide signal as an argument to the trap function.
109trap_with_sig() {
110 local func
111
112 func="$1" ; shift
113 for sig ; do
114 trap "$func $sig" "$sig"
115 done
116}
117
Fathi Boudra422bf772019-12-02 11:10:16 +0200118# Cleanup actions
Manish V Badarkhe359b3ff2020-12-25 18:54:03 +0000119trap_with_sig cleanup SIGINT SIGHUP SIGTERM EXIT
Fathi Boudra422bf772019-12-02 11:10:16 +0200120
121# Prevent xterm windows from untracked terminals from popping up, especially
122# when running locally
123not_upon "$test_run" && export DISPLAY=
124
125# Source variables required for run
126source "$artefacts/env"
127
128echo
129echo "RUNNING: $TEST_CONFIG"
130echo
131
132# Accept BIN_MODE from environment, or default to release. If bin_mode is set
133# and non-empty (intended to be set from command line), that takes precedence.
134pkg_bin_mode="${BIN_MODE:-release}"
135bin_mode="${bin_mode:-$pkg_bin_mode}"
136
137# Assume 0 is the primary UART to track
138primary_uart=0
139
140# Assume 4 UARTs by default
141num_uarts="${num_uarts:-4}"
142
143# Whether to display primary UART progress live on the console
144primary_live="${primary_live-$PRIMARY_LIVE}"
145
146# Change directory so that all binaries can be accessed realtive to where they
147# lie
148run_cwd="$artefacts/$bin_mode"
149cd "$run_cwd"
150
151# Source environment for run
152if [ -f "run/env" ]; then
153 source "run/env"
154fi
155
Zelalem1af7a7b2020-08-04 17:34:32 -0500156# Source model environment for run
157if [ -f "run/model_env" ]; then
158 source "run/model_env"
159fi
Fathi Boudra422bf772019-12-02 11:10:16 +0200160# Fail if there was no model path set
161if [ -z "$model_path" ]; then
162 die "No model path set by package!"
163fi
164
165# Launch model with parameters
166model_out="$run_root/model_log.txt"
167run_sh="$run_root/run.sh"
168
Zelalem219df412020-05-17 19:21:20 -0500169
Fathi Boudra422bf772019-12-02 11:10:16 +0200170# Generate run.sh
171echo "$model_path \\" > "$run_sh"
172sed '/^\s*$/d' < model_params | sort | sed 's/^/\t/;s/$/ \\/' >> "$run_sh"
Zelalem219df412020-05-17 19:21:20 -0500173
174if [ "${COVERAGE_ON}" == "1" ]; then
175 # Adding code coverage plugin
176 echo -e "\t-C TRACE.CoverageTrace.trace-file-prefix=$trace_file_prefix \\" >> "$run_sh"
177 echo -e "\t--plugin $coverage_trace_plugin \\" >> "$run_sh"
178fi
Fathi Boudra422bf772019-12-02 11:10:16 +0200179echo -e "\t\"\$@\"" >> "$run_sh"
180
Zelalem219df412020-05-17 19:21:20 -0500181# Running Reboot/Shutdown tests requires storing the state in non-volatile
182# memory(NVM) across reboot. On FVP, NVM is not persistent across reboot, hence
183# NVM was saved to a file($NVM_file) when running the model using the run.sh
184# shell script.
185# If TFTF Reboot/Shutdown tests are enabled, run the fvp model 10 times by
186# feeding the file containing NVM state generated from the previous run. Note
187# that this file also includes FIP image.
188
189if upon "$run_tftf_reboot_tests" = "1"; then
190 tftf_reboot_tests="$run_root/tftf_reboot_tests.sh"
191
192 # Generate tftf_reboot_tests command. It is similar to run_sh.
193 # The model would run the reboot and shutdown tests 10 times
194 # The uart log file generated by FVP model gets overwritten
195 # across reboots. Copy its contents at the end of the test
196 echo "cat $uart0_file >> UART0.log" >>"$tftf_reboot_tests"
197 echo "cat $uart1_file >> UART1.log" >>"$tftf_reboot_tests"
198 cat <<EOF >>"$tftf_reboot_tests"
199
200for i in {1..10}
201do
202EOF
203 cat "$run_sh" >> "$tftf_reboot_tests"
204 echo "cat $uart0_file >> UART0.log" >>"$tftf_reboot_tests"
205 echo "cat $uart1_file >> UART1.log" >>"$tftf_reboot_tests"
206 cat <<EOF >>"$tftf_reboot_tests"
207done
208EOF
209 #Replace fip.bin with file $NVM_file
210 sed -i 's/fip.bin/'"$NVM_file"'/' "$tftf_reboot_tests"
211
212 echo "TFTF Reboot/Shutdown Tests Enabled"
213 cat "$tftf_reboot_tests" >> "$run_sh"
214 rm "$tftf_reboot_tests"
215fi
216
Fathi Boudra422bf772019-12-02 11:10:16 +0200217echo "Model command line:"
218echo
219cat "$run_sh"
220chmod +x "$run_sh"
221echo
222
223# If it's a test run, skip all the hoops and launch model directly.
224if upon "$test_run"; then
225 "$run_sh" "$@"
226 exit 0
227fi
228
229# For an automated run, export a known variable so that we can identify stale
230# processes spawned by Trusted Firmware CI by inspecting its environment.
231export TRUSTED_FIRMWARE_CI="1"
232
233# Change directory to workspace, as all artifacts paths are relative to
234# that, and launch the model. Have model use no buffering on stdout
235: >"$model_out"
236name="model" launch stdbuf -o0 -e0 "$run_sh" &>"$model_out" &
237wait_count=0
238while :; do
239 if [ -f "$pid_dir/model.pid" ]; then
240 break
241 fi
242 sleep 0.1
243
244 let "wait_count += 1"
245 if [ "$wait_count" -gt 100 ]; then
246 die "Failed to launch model!"
247 fi
248done
Fathi Boudra422bf772019-12-02 11:10:16 +0200249
Zelalem219df412020-05-17 19:21:20 -0500250model_pid="$(cat $pid_dir/model.pid)"
Fathi Boudra422bf772019-12-02 11:10:16 +0200251ports_output="$(mktempfile)"
252if not_upon "$ports_script"; then
253 # Default AWK script to parse model ports
254 ports_script="$(mktempfile)"
255 cat <<'EOF' >"$ports_script"
256/terminal_0/ { ports[0] = $NF }
257/terminal_1/ { ports[1] = $NF }
258/terminal_2/ { ports[2] = $NF }
259/terminal_3/ { ports[3] = $NF }
260END {
261 for (i = 0; i < num_uarts; i++) {
262 if (ports[i] != "")
263 print "ports[" i "]=" ports[i]
264 }
265}
266EOF
267fi
268
269# Start a watchdog to kill ourselves if we wait too long for the model
270# response. Note that this is not the timeout for the whole test, but only for
271# the Model to output port numbers.
272(
273if upon "$jenkins_run"; then
274 # Increase this timeout for a cluster run, as it could take longer if
275 # the load on the Jenkins server is high.
276 model_wait_timeout=120
277else
278 model_wait_timeout=30
279fi
280sleep $model_wait_timeout
281echo "Model wait timeout!"
282kill "$$"
283) &
284watchdog="$!"
285
286# Parse UARTs ports from early model output. Send a SIGSTOP to the model
287# as soon as it outputs all UART ports. This is to prevent the model
288# executing before the expect scripts get a chance to connect to the
289# UART thereby losing messages.
290model_fail=1
291while :; do
292 awk -v "num_uarts=$num_uarts" -f "$ports_script" "$model_out" \
293 > "$ports_output"
294 if [ $(wc -l < "$ports_output") -eq "$num_uarts" ]; then
295 kill -SIGSTOP "$model_pid"
296 model_fail=0
297 break
298 fi
299
300 # Bail out if model exited meanwhile
301 if ! kill -0 "$model_pid" &>/dev/null; then
302 echo "Model terminated unexpectedly!"
303 break
304 fi
305done
306
307# Kill the watch dog
308kill_and_reap "$watchdog" || true
309
310# Check the model had failed meanwhile, for some reason
311if [ "$model_fail" -ne 0 ]; then
312 exit 1
313fi
314
Zelalem219df412020-05-17 19:21:20 -0500315if ! [ -x "$(command -v expect)" ]; then
316 echo "Error: Expect is not installed."
317 exit 1
318fi
319
Fathi Boudra422bf772019-12-02 11:10:16 +0200320# The wait loop above exited after model port numbers have been parsed. The
321# script's output is ready to be sourced now.
322declare -a ports
323source "$ports_output"
324rm -f "$ports_output"
325if [ "${#ports[@]}" -ne "$num_uarts" ]; then
326 echo "Failed to get UART port numbers"
327 kill_and_reap "$model_pid"
328 unset model_pid
329fi
330
331# Launch expect scripts for all UARTs
332uarts=0
Zelalem219df412020-05-17 19:21:20 -0500333for u in $(seq 0 $(( $num_uarts - 1 )) | tac); do
Fathi Boudra422bf772019-12-02 11:10:16 +0200334 script="run/uart$u/expect"
335 if [ -f "$script" ]; then
336 script="$(cat "$script")"
337 else
338 script=
339 fi
340
341 # Primary UART must have a script
342 if [ -z "$script" ]; then
343 if [ "$u" = "$primary_uart" ]; then
344 die "No primary UART script!"
345 else
Zelalem219df412020-05-17 19:21:20 -0500346 echo "Ignoring UART$u (no expect script provided)."
Fathi Boudra422bf772019-12-02 11:10:16 +0200347 continue
348 fi
349 fi
350
351 timeout="run/uart$u/timeout"
352 if [ -f "$timeout" ]; then
353 timeout="$(cat "$timeout")"
354 else
355 timeout=
356 fi
Saul Romerod54b6eb2020-10-20 09:03:08 +0100357 timeout="${timeout-1200}"
Fathi Boudra422bf772019-12-02 11:10:16 +0200358
359 full_log="$run_root/uart${u}_full.txt"
360
361 if [ "$u" = "$primary_uart" ]; then
362 star="*"
363 uart_name="primary_uart"
364 else
365 star=" "
366 uart_name="uart$u"
367 fi
368
369 # Launch expect after exporting required variables
370 (
371 if [ -f "run/uart$u/env" ]; then
372 set -a
373 source "run/uart$u/env"
374 set +a
375 fi
376
377 if [ "$u" = "$primary_uart" ] && upon "$primary_live"; then
378 uart_port="${ports[$u]}" timeout="$timeout" \
379 name="$uart_name" launch expect -f "$ci_root/expect/$script" | \
380 tee "$full_log"
381 echo
382 else
383 uart_port="${ports[$u]}" timeout="$timeout" \
384 name="$uart_name" launch expect -f "$ci_root/expect/$script" \
385 &>"$full_log"
386 fi
387
388 ) &
389
390 let "uarts += 1"
391 echo "Tracking UART$u$star with $script; timeout $timeout."
392done
Fathi Boudra422bf772019-12-02 11:10:16 +0200393# Wait here long 'enough' for expect scripts to connect to ports; then
394# let the model proceed
395sleep 2
396kill -SIGCONT "$model_pid"
397
398# Wait for all children. Note that the wait below is *not* a timed wait.
399result=0
400
401set +e
402pushd "$pid_dir"
403while :; do
404 wait -n
405
406 # Exit failure if we've any failures
407 if [ "$(wc -l < <(find -name '*.fail'))" -ne 0 ]; then
408 result=1
409 break
410 fi
411
412 # We're done if the primary UART exits success
413 if [ -f "$pid_dir/primary_uart.success" ]; then
414 break
415 fi
416done
417popd
418
Manish V Badarkhe359b3ff2020-12-25 18:54:03 +0000419# Capture whether the model is running with the 'exit model parameter' or not.
420exit_on_model_param=$(grep -wc "$model_exit_param_string" "$run_cwd/model_params")
Fathi Boudra422bf772019-12-02 11:10:16 +0200421
422if [ "$result" -eq 0 ]; then
423 echo "Test success!"
424else
425 echo "Test failed!"
426fi
427
428if upon "$jenkins_run"; then
429 echo
430 echo "Artefacts location: $BUILD_URL."
431 echo
432fi
433
434if upon "$jenkins_run" && upon "$artefacts_receiver" && [ -d "$workspace/run" ]; then
Zelalem219df412020-05-17 19:21:20 -0500435 source "$CI_ROOT/script/send_artefacts.sh" "run"
Fathi Boudra422bf772019-12-02 11:10:16 +0200436fi
437
Fathi Boudra422bf772019-12-02 11:10:16 +0200438
Zelalem219df412020-05-17 19:21:20 -0500439exit "$result"
Fathi Boudra422bf772019-12-02 11:10:16 +0200440# vim: set tw=80 sw=8 noet: