blob: b89c4a682680821cd6f8ff6921172ec5294bc4c3 [file] [log] [blame]
Leonardo Sandoval9dfdd1b2020-08-06 17:08:11 -05001#!/usr/bin/env bash
Fathi Boudra422bf772019-12-02 11:10:16 +02002#
Boyan Karatotev55a18e92025-07-28 09:17:42 +01003# Copyright (c) 2019-2025, 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
Fathi Boudra422bf772019-12-02 11:10:16 +020013ci_root="$(readlink -f "$(dirname "$0")/..")"
Juan Pablo Conde64f59be2024-01-12 16:56:58 -060014source "$ci_root/script/run_common.sh"
Fathi Boudra422bf772019-12-02 11:10:16 +020015
16artefacts="${artefacts-$workspace/artefacts}"
17
18run_root="$workspace/run"
19pid_dir="$workspace/pids"
20
Manish V Badarkhe359b3ff2020-12-25 18:54:03 +000021# Model exit parameter string
22model_exit_param_string="bp.pl011_uart0.shutdown_on_eot=1"
23
Fathi Boudra422bf772019-12-02 11:10:16 +020024mkdir -p "$pid_dir"
25mkdir -p "$run_root"
26
27kill_and_reap() {
28 local gid
Fathi Boudra422bf772019-12-02 11:10:16 +020029 # Kill an active process. Ignore errors
30 [ "$1" ] || return 0
31 kill -0 "$1" &>/dev/null || return 0
32
Zelalem219df412020-05-17 19:21:20 -050033 # Kill the children
34 kill -- "-$1" &>/dev/null || true
Fathi Boudra422bf772019-12-02 11:10:16 +020035 # Kill the group
Zelalem219df412020-05-17 19:21:20 -050036 { gid="$(awk '{print $5}' < /proc/$1/stat)";} 2>/dev/null || return
37 # For Code Coverage plugin it is needed to propagate
38 # the kill signal to the plugin in order to save
39 # the trace statistics.
40 if [ "${COVERAGE_ON}" == "1" ] || [ -n "$cc_enable" ]; then
41 kill -SIGTERM -- "-$gid" &>/dev/null || true
42 else
43 kill -SIGKILL -- "-$gid" &>/dev/null || true
44 fi
Fathi Boudra422bf772019-12-02 11:10:16 +020045 wait "$gid" &>/dev/null || true
46}
47
48# Perform clean up and ignore errors
49cleanup() {
50 local pid
Manish V Badarkhe359b3ff2020-12-25 18:54:03 +000051 local sig
Fathi Boudra422bf772019-12-02 11:10:16 +020052
Fathi Boudra422bf772019-12-02 11:10:16 +020053 pushd "$pid_dir"
54 set +e
Manish V Badarkhe359b3ff2020-12-25 18:54:03 +000055
56 sig=${1:-SIGINT}
57 echo "signal received: $sig"
58
Boyan Karatotev40fa01a2025-07-29 14:18:02 +010059 while read pid; do
60 pid="$(cat $pid)"
61 echo $pid
62 # Forcefully killing model process does not show statistical
63 # data (Host CPU time spent running in User and System). Safely
64 # kill the model by using SIGINT(^C) that helps in printing
65 # statistical data.
66 if [ "$pid" == "$model_pid" ] && [ "${COVERAGE_ON}" != "1" ]; then
67 model_cid=$(pgrep -P "$model_pid" | xargs || true)
68 if [ -z "$model_cid" ]; then
69 echo "Model quit by itself. Not killing!"
70 continue
Manish V Badarkhe359b3ff2020-12-25 18:54:03 +000071 fi
Harrison Mutai5d66de32022-10-19 11:36:30 +010072
Boyan Karatotev40fa01a2025-07-29 14:18:02 +010073 # ignore errors
74 kill -SIGINT "$model_cid" &>/dev/null || true
75 # Allow some time to print data, we can't use wait since the process is
76 # a child of the daemonized launch process.
77 sleep 5
78 fi
Harrison Mutai5d66de32022-10-19 11:36:30 +010079
Boyan Karatotev40fa01a2025-07-29 14:18:02 +010080 kill_and_reap "$pid"
81
82 done < <(find -name '*.pid')
Manish V Badarkhe359b3ff2020-12-25 18:54:03 +000083
Fathi Boudra422bf772019-12-02 11:10:16 +020084 popd
85}
86
Fathi Boudra422bf772019-12-02 11:10:16 +020087# Cleanup actions
Manish V Badarkhe359b3ff2020-12-25 18:54:03 +000088trap_with_sig cleanup SIGINT SIGHUP SIGTERM EXIT
Fathi Boudra422bf772019-12-02 11:10:16 +020089
90# Prevent xterm windows from untracked terminals from popping up, especially
91# when running locally
92not_upon "$test_run" && export DISPLAY=
93
94# Source variables required for run
95source "$artefacts/env"
96
97echo
98echo "RUNNING: $TEST_CONFIG"
99echo
100
101# Accept BIN_MODE from environment, or default to release. If bin_mode is set
102# and non-empty (intended to be set from command line), that takes precedence.
103pkg_bin_mode="${BIN_MODE:-release}"
104bin_mode="${bin_mode:-$pkg_bin_mode}"
105
Fathi Boudra422bf772019-12-02 11:10:16 +0200106# Whether to display primary UART progress live on the console
107primary_live="${primary_live-$PRIMARY_LIVE}"
108
109# Change directory so that all binaries can be accessed realtive to where they
110# lie
111run_cwd="$artefacts/$bin_mode"
112cd "$run_cwd"
113
114# Source environment for run
115if [ -f "run/env" ]; then
116 source "run/env"
117fi
118
Slava Andrianov192ee172025-06-11 15:40:43 -0500119if [ -v verify_hashes ]; then
120 export verify_hashes=1
121 export artefacts_dir=$run_cwd
122fi
123
Zelalem1af7a7b2020-08-04 17:34:32 -0500124# Source model environment for run
125if [ -f "run/model_env" ]; then
126 source "run/model_env"
127fi
Fathi Boudra422bf772019-12-02 11:10:16 +0200128# Fail if there was no model path set
129if [ -z "$model_path" ]; then
130 die "No model path set by package!"
131fi
132
133# Launch model with parameters
134model_out="$run_root/model_log.txt"
135run_sh="$run_root/run.sh"
136
Zelalem219df412020-05-17 19:21:20 -0500137
Fathi Boudra422bf772019-12-02 11:10:16 +0200138# Generate run.sh
139echo "$model_path \\" > "$run_sh"
140sed '/^\s*$/d' < model_params | sort | sed 's/^/\t/;s/$/ \\/' >> "$run_sh"
Zelalem219df412020-05-17 19:21:20 -0500141
142if [ "${COVERAGE_ON}" == "1" ]; then
143 # Adding code coverage plugin
Zelalem219df412020-05-17 19:21:20 -0500144 echo -e "\t--plugin $coverage_trace_plugin \\" >> "$run_sh"
145fi
Fathi Boudra422bf772019-12-02 11:10:16 +0200146echo -e "\t\"\$@\"" >> "$run_sh"
147
Zelalem219df412020-05-17 19:21:20 -0500148# Running Reboot/Shutdown tests requires storing the state in non-volatile
149# memory(NVM) across reboot. On FVP, NVM is not persistent across reboot, hence
150# NVM was saved to a file($NVM_file) when running the model using the run.sh
151# shell script.
152# If TFTF Reboot/Shutdown tests are enabled, run the fvp model 10 times by
153# feeding the file containing NVM state generated from the previous run. Note
154# that this file also includes FIP image.
155
156if upon "$run_tftf_reboot_tests" = "1"; then
157 tftf_reboot_tests="$run_root/tftf_reboot_tests.sh"
158
159 # Generate tftf_reboot_tests command. It is similar to run_sh.
160 # The model would run the reboot and shutdown tests 10 times
161 # The uart log file generated by FVP model gets overwritten
162 # across reboots. Copy its contents at the end of the test
163 echo "cat $uart0_file >> UART0.log" >>"$tftf_reboot_tests"
164 echo "cat $uart1_file >> UART1.log" >>"$tftf_reboot_tests"
165 cat <<EOF >>"$tftf_reboot_tests"
166
167for i in {1..10}
168do
169EOF
170 cat "$run_sh" >> "$tftf_reboot_tests"
171 echo "cat $uart0_file >> UART0.log" >>"$tftf_reboot_tests"
172 echo "cat $uart1_file >> UART1.log" >>"$tftf_reboot_tests"
173 cat <<EOF >>"$tftf_reboot_tests"
174done
175EOF
176 #Replace fip.bin with file $NVM_file
177 sed -i 's/fip.bin/'"$NVM_file"'/' "$tftf_reboot_tests"
178
179 echo "TFTF Reboot/Shutdown Tests Enabled"
180 cat "$tftf_reboot_tests" >> "$run_sh"
181 rm "$tftf_reboot_tests"
182fi
183
Fathi Boudra422bf772019-12-02 11:10:16 +0200184echo "Model command line:"
185echo
186cat "$run_sh"
187chmod +x "$run_sh"
188echo
189
190# If it's a test run, skip all the hoops and launch model directly.
191if upon "$test_run"; then
192 "$run_sh" "$@"
193 exit 0
194fi
195
196# For an automated run, export a known variable so that we can identify stale
197# processes spawned by Trusted Firmware CI by inspecting its environment.
198export TRUSTED_FIRMWARE_CI="1"
199
200# Change directory to workspace, as all artifacts paths are relative to
201# that, and launch the model. Have model use no buffering on stdout
202: >"$model_out"
203name="model" launch stdbuf -o0 -e0 "$run_sh" &>"$model_out" &
204wait_count=0
205while :; do
206 if [ -f "$pid_dir/model.pid" ]; then
207 break
208 fi
209 sleep 0.1
210
211 let "wait_count += 1"
212 if [ "$wait_count" -gt 100 ]; then
213 die "Failed to launch model!"
214 fi
215done
Fathi Boudra422bf772019-12-02 11:10:16 +0200216
Zelalem219df412020-05-17 19:21:20 -0500217model_pid="$(cat $pid_dir/model.pid)"
Fathi Boudra422bf772019-12-02 11:10:16 +0200218
219# Start a watchdog to kill ourselves if we wait too long for the model
220# response. Note that this is not the timeout for the whole test, but only for
221# the Model to output port numbers.
222(
223if upon "$jenkins_run"; then
224 # Increase this timeout for a cluster run, as it could take longer if
225 # the load on the Jenkins server is high.
226 model_wait_timeout=120
227else
228 model_wait_timeout=30
229fi
230sleep $model_wait_timeout
231echo "Model wait timeout!"
232kill "$$"
233) &
234watchdog="$!"
235
Chris Kay03ffbe72022-11-17 18:53:50 +0000236ports_output="$(mktempfile)"
237
Fathi Boudra422bf772019-12-02 11:10:16 +0200238# Parse UARTs ports from early model output. Send a SIGSTOP to the model
239# as soon as it outputs all UART ports. This is to prevent the model
240# executing before the expect scripts get a chance to connect to the
241# UART thereby losing messages.
242model_fail=1
243while :; do
Chris Kay03ffbe72022-11-17 18:53:50 +0000244 awk -v "num_uarts=$(get_num_uarts "${run_cwd}")" \
245 -f "$(get_ports_script "${run_cwd}")" "$model_out" > "$ports_output"
246 if [ $(wc -l < "$ports_output") -eq "$(get_num_uarts "${run_cwd}")" ]; then
Fathi Boudra422bf772019-12-02 11:10:16 +0200247 kill -SIGSTOP "$model_pid"
248 model_fail=0
249 break
250 fi
251
252 # Bail out if model exited meanwhile
253 if ! kill -0 "$model_pid" &>/dev/null; then
254 echo "Model terminated unexpectedly!"
255 break
256 fi
257done
258
259# Kill the watch dog
260kill_and_reap "$watchdog" || true
261
262# Check the model had failed meanwhile, for some reason
263if [ "$model_fail" -ne 0 ]; then
264 exit 1
265fi
266
Zelalem219df412020-05-17 19:21:20 -0500267if ! [ -x "$(command -v expect)" ]; then
268 echo "Error: Expect is not installed."
269 exit 1
270fi
271
Fathi Boudra422bf772019-12-02 11:10:16 +0200272# The wait loop above exited after model port numbers have been parsed. The
273# script's output is ready to be sourced now.
274declare -a ports
275source "$ports_output"
276rm -f "$ports_output"
Chris Kay03ffbe72022-11-17 18:53:50 +0000277if [ "${#ports[@]}" -ne "$(get_num_uarts "${run_cwd}")" ]; then
Fathi Boudra422bf772019-12-02 11:10:16 +0200278 echo "Failed to get UART port numbers"
279 kill_and_reap "$model_pid"
280 unset model_pid
281fi
282
283# Launch expect scripts for all UARTs
284uarts=0
Chris Kay03ffbe72022-11-17 18:53:50 +0000285for u in $(seq 0 $(( "$(get_num_uarts "${run_cwd}")" - 1 )) | tac); do
Fathi Boudra422bf772019-12-02 11:10:16 +0200286 script="run/uart$u/expect"
287 if [ -f "$script" ]; then
288 script="$(cat "$script")"
289 else
290 script=
291 fi
292
293 # Primary UART must have a script
294 if [ -z "$script" ]; then
Chris Kay03ffbe72022-11-17 18:53:50 +0000295 if [ "$u" = "$(get_primary_uart "${run_cwd}")" ]; then
Fathi Boudra422bf772019-12-02 11:10:16 +0200296 die "No primary UART script!"
297 else
Zelalem219df412020-05-17 19:21:20 -0500298 echo "Ignoring UART$u (no expect script provided)."
Fathi Boudra422bf772019-12-02 11:10:16 +0200299 continue
300 fi
301 fi
302
303 timeout="run/uart$u/timeout"
304 if [ -f "$timeout" ]; then
305 timeout="$(cat "$timeout")"
306 else
307 timeout=
308 fi
Saul Romerod54b6eb2020-10-20 09:03:08 +0100309 timeout="${timeout-1200}"
Fathi Boudra422bf772019-12-02 11:10:16 +0200310
311 full_log="$run_root/uart${u}_full.txt"
312
Chris Kay03ffbe72022-11-17 18:53:50 +0000313 if [ "$u" = "$(get_primary_uart "${run_cwd}")" ]; then
Fathi Boudra422bf772019-12-02 11:10:16 +0200314 star="*"
Fathi Boudra422bf772019-12-02 11:10:16 +0200315 else
316 star=" "
Fathi Boudra422bf772019-12-02 11:10:16 +0200317 fi
318
Madhukar Pappireddy1e953722021-11-08 15:23:02 -0600319 uart_name="uart$u"
320
Fathi Boudra422bf772019-12-02 11:10:16 +0200321 # Launch expect after exporting required variables
322 (
323 if [ -f "run/uart$u/env" ]; then
324 set -a
325 source "run/uart$u/env"
326 set +a
327 fi
328
Chris Kay03ffbe72022-11-17 18:53:50 +0000329 if [ "$u" = "$(get_primary_uart "${run_cwd}")" ] && upon "$primary_live"; then
Fathi Boudra422bf772019-12-02 11:10:16 +0200330 uart_port="${ports[$u]}" timeout="$timeout" \
331 name="$uart_name" launch expect -f "$ci_root/expect/$script" | \
332 tee "$full_log"
333 echo
334 else
335 uart_port="${ports[$u]}" timeout="$timeout" \
336 name="$uart_name" launch expect -f "$ci_root/expect/$script" \
337 &>"$full_log"
338 fi
339
340 ) &
341
342 let "uarts += 1"
343 echo "Tracking UART$u$star with $script; timeout $timeout."
344done
Fathi Boudra422bf772019-12-02 11:10:16 +0200345# Wait here long 'enough' for expect scripts to connect to ports; then
346# let the model proceed
347sleep 2
348kill -SIGCONT "$model_pid"
349
350# Wait for all children. Note that the wait below is *not* a timed wait.
351result=0
352
353set +e
354pushd "$pid_dir"
Chris Kay23f58692022-11-03 14:01:43 +0000355
356timeout=3600
357
358echo
359
Fathi Boudra422bf772019-12-02 11:10:16 +0200360while :; do
Chris Kay23f58692022-11-03 14:01:43 +0000361 readarray -d '' all < <(find "${pid_dir}" -name 'uart*.pid' -print0)
362 readarray -d '' succeeded < <(find "${pid_dir}" -name 'uart*.success' -print0)
363 readarray -d '' failed < <(find "${pid_dir}" -name 'uart*.fail' -print0)
Fathi Boudra422bf772019-12-02 11:10:16 +0200364
Chris Kay23f58692022-11-03 14:01:43 +0000365 all=("${all[@]##${pid_dir}/uart}")
366 all=("${all[@]%%.pid}")
367
368 succeeded=("${succeeded[@]##${pid_dir}/uart}")
369 succeeded=("${succeeded[@]%%.success}")
370
371 failed=("${failed[@]##${pid_dir}/uart}")
372 failed=("${failed[@]%%.fail}")
373
374 completed=("${succeeded[@]}" "${failed[@]}")
375
376 readarray -t remaining < <( \
377 comm -23 \
378 <(printf '%s\n' "${all[@]}" | sort) \
379 <(printf '%s\n' "${completed[@]}" | sort) \
380 )
381
382 if [ ${#remaining[@]} = 0 ]; then
Fathi Boudra422bf772019-12-02 11:10:16 +0200383 break
384 fi
385
Chris Kay23f58692022-11-03 14:01:43 +0000386 echo "Waiting ${timeout}s for ${#remaining[@]} UART(s): ${remaining[@]}"
387
Chris Kay03ffbe72022-11-17 18:53:50 +0000388 if [[ " ${completed[@]} " =~ " $(get_payload_uart "${run_cwd}") " ]]; then
389 echo "- Payload (UART $(get_payload_uart "${run_cwd}")) completed!"
Chris Kay23f58692022-11-03 14:01:43 +0000390
391 for uart in "${remaining[@]}"; do
392 pid=$(cat "${pid_dir}/uart${uart}.pid")
393
394 echo "- Terminating UART ${uart} script (PID ${pid})..."
395
396 kill -SIGINT ${pid} || true # Send Ctrl+C - don't force-kill it!
397 done
Fathi Boudra422bf772019-12-02 11:10:16 +0200398 fi
Chris Kay23f58692022-11-03 14:01:43 +0000399
400 if [ ${timeout} = 0 ]; then
401 echo "- Timeout exceeded! Killing model (PID ${model_pid})..."
402
403 kill_and_reap "${model_pid}"
404 fi
405
406 timeout=$((${timeout} - 5)) && sleep 5
Fathi Boudra422bf772019-12-02 11:10:16 +0200407done
Chris Kay23f58692022-11-03 14:01:43 +0000408
409echo
410
411if [ ${#failed[@]} != 0 ]; then
412 echo "${#failed[@]} UART(s) did not match expectations:"
413 echo
414
415 for uart in "${failed[@]}"; do
416 echo " - UART ${uart}: uart${uart}_full.txt"
417 done
418
419 echo
420
421 result=1
422fi
423
Fathi Boudra422bf772019-12-02 11:10:16 +0200424popd
425
Fathi Boudra422bf772019-12-02 11:10:16 +0200426if [ "$result" -eq 0 ]; then
427 echo "Test success!"
428else
429 echo "Test failed!"
430fi
431
432if upon "$jenkins_run"; then
433 echo
434 echo "Artefacts location: $BUILD_URL."
435 echo
436fi
437
438if upon "$jenkins_run" && upon "$artefacts_receiver" && [ -d "$workspace/run" ]; then
Zelalem219df412020-05-17 19:21:20 -0500439 source "$CI_ROOT/script/send_artefacts.sh" "run"
Fathi Boudra422bf772019-12-02 11:10:16 +0200440fi
441
Fathi Boudra422bf772019-12-02 11:10:16 +0200442
Zelalem219df412020-05-17 19:21:20 -0500443exit "$result"
Fathi Boudra422bf772019-12-02 11:10:16 +0200444# vim: set tw=80 sw=8 noet: