blob: 340b3a5a8458f9bdf8641ca58c4ccb5042a522fe [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")/..")"
14source "$ci_root/utils.sh"
15
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
87# Launch a program. Have its PID saved in a file with given name with .pid
88# suffix. When the program exits, create a file with .success suffix, or one
89# with .fail if it fails. This function blocks, so the caller must '&' this if
90# they want to continue. Call must wait for $pid_dir/$name.pid to be created
91# should it want to read it.
92launch() {
93 local pid
94
95 "$@" &
96 pid="$!"
97 echo "$pid" > "$pid_dir/${name:?}.pid"
98 if wait "$pid"; then
99 touch "$pid_dir/$name.success"
100 else
101 touch "$pid_dir/$name.fail"
102 fi
103}
104
Manish V Badarkhe359b3ff2020-12-25 18:54:03 +0000105# Provide signal as an argument to the trap function.
106trap_with_sig() {
107 local func
108
109 func="$1" ; shift
110 for sig ; do
111 trap "$func $sig" "$sig"
112 done
113}
114
Fathi Boudra422bf772019-12-02 11:10:16 +0200115# Cleanup actions
Manish V Badarkhe359b3ff2020-12-25 18:54:03 +0000116trap_with_sig cleanup SIGINT SIGHUP SIGTERM EXIT
Fathi Boudra422bf772019-12-02 11:10:16 +0200117
118# Prevent xterm windows from untracked terminals from popping up, especially
119# when running locally
120not_upon "$test_run" && export DISPLAY=
121
122# Source variables required for run
123source "$artefacts/env"
124
125echo
126echo "RUNNING: $TEST_CONFIG"
127echo
128
129# Accept BIN_MODE from environment, or default to release. If bin_mode is set
130# and non-empty (intended to be set from command line), that takes precedence.
131pkg_bin_mode="${BIN_MODE:-release}"
132bin_mode="${bin_mode:-$pkg_bin_mode}"
133
Fathi Boudra422bf772019-12-02 11:10:16 +0200134# Whether to display primary UART progress live on the console
135primary_live="${primary_live-$PRIMARY_LIVE}"
136
137# Change directory so that all binaries can be accessed realtive to where they
138# lie
139run_cwd="$artefacts/$bin_mode"
140cd "$run_cwd"
141
142# Source environment for run
143if [ -f "run/env" ]; then
144 source "run/env"
145fi
146
Zelalem1af7a7b2020-08-04 17:34:32 -0500147# Source model environment for run
148if [ -f "run/model_env" ]; then
149 source "run/model_env"
150fi
Fathi Boudra422bf772019-12-02 11:10:16 +0200151# Fail if there was no model path set
152if [ -z "$model_path" ]; then
153 die "No model path set by package!"
154fi
155
156# Launch model with parameters
157model_out="$run_root/model_log.txt"
158run_sh="$run_root/run.sh"
159
Zelalem219df412020-05-17 19:21:20 -0500160
Fathi Boudra422bf772019-12-02 11:10:16 +0200161# Generate run.sh
162echo "$model_path \\" > "$run_sh"
163sed '/^\s*$/d' < model_params | sort | sed 's/^/\t/;s/$/ \\/' >> "$run_sh"
Zelalem219df412020-05-17 19:21:20 -0500164
165if [ "${COVERAGE_ON}" == "1" ]; then
166 # Adding code coverage plugin
Zelalem219df412020-05-17 19:21:20 -0500167 echo -e "\t--plugin $coverage_trace_plugin \\" >> "$run_sh"
168fi
Fathi Boudra422bf772019-12-02 11:10:16 +0200169echo -e "\t\"\$@\"" >> "$run_sh"
170
Zelalem219df412020-05-17 19:21:20 -0500171# Running Reboot/Shutdown tests requires storing the state in non-volatile
172# memory(NVM) across reboot. On FVP, NVM is not persistent across reboot, hence
173# NVM was saved to a file($NVM_file) when running the model using the run.sh
174# shell script.
175# If TFTF Reboot/Shutdown tests are enabled, run the fvp model 10 times by
176# feeding the file containing NVM state generated from the previous run. Note
177# that this file also includes FIP image.
178
179if upon "$run_tftf_reboot_tests" = "1"; then
180 tftf_reboot_tests="$run_root/tftf_reboot_tests.sh"
181
182 # Generate tftf_reboot_tests command. It is similar to run_sh.
183 # The model would run the reboot and shutdown tests 10 times
184 # The uart log file generated by FVP model gets overwritten
185 # across reboots. Copy its contents at the end of the test
186 echo "cat $uart0_file >> UART0.log" >>"$tftf_reboot_tests"
187 echo "cat $uart1_file >> UART1.log" >>"$tftf_reboot_tests"
188 cat <<EOF >>"$tftf_reboot_tests"
189
190for i in {1..10}
191do
192EOF
193 cat "$run_sh" >> "$tftf_reboot_tests"
194 echo "cat $uart0_file >> UART0.log" >>"$tftf_reboot_tests"
195 echo "cat $uart1_file >> UART1.log" >>"$tftf_reboot_tests"
196 cat <<EOF >>"$tftf_reboot_tests"
197done
198EOF
199 #Replace fip.bin with file $NVM_file
200 sed -i 's/fip.bin/'"$NVM_file"'/' "$tftf_reboot_tests"
201
202 echo "TFTF Reboot/Shutdown Tests Enabled"
203 cat "$tftf_reboot_tests" >> "$run_sh"
204 rm "$tftf_reboot_tests"
205fi
206
Fathi Boudra422bf772019-12-02 11:10:16 +0200207echo "Model command line:"
208echo
209cat "$run_sh"
210chmod +x "$run_sh"
211echo
212
213# If it's a test run, skip all the hoops and launch model directly.
214if upon "$test_run"; then
215 "$run_sh" "$@"
216 exit 0
217fi
218
219# For an automated run, export a known variable so that we can identify stale
220# processes spawned by Trusted Firmware CI by inspecting its environment.
221export TRUSTED_FIRMWARE_CI="1"
222
223# Change directory to workspace, as all artifacts paths are relative to
224# that, and launch the model. Have model use no buffering on stdout
225: >"$model_out"
226name="model" launch stdbuf -o0 -e0 "$run_sh" &>"$model_out" &
227wait_count=0
228while :; do
229 if [ -f "$pid_dir/model.pid" ]; then
230 break
231 fi
232 sleep 0.1
233
234 let "wait_count += 1"
235 if [ "$wait_count" -gt 100 ]; then
236 die "Failed to launch model!"
237 fi
238done
Fathi Boudra422bf772019-12-02 11:10:16 +0200239
Zelalem219df412020-05-17 19:21:20 -0500240model_pid="$(cat $pid_dir/model.pid)"
Fathi Boudra422bf772019-12-02 11:10:16 +0200241
242# Start a watchdog to kill ourselves if we wait too long for the model
243# response. Note that this is not the timeout for the whole test, but only for
244# the Model to output port numbers.
245(
246if upon "$jenkins_run"; then
247 # Increase this timeout for a cluster run, as it could take longer if
248 # the load on the Jenkins server is high.
249 model_wait_timeout=120
250else
251 model_wait_timeout=30
252fi
253sleep $model_wait_timeout
254echo "Model wait timeout!"
255kill "$$"
256) &
257watchdog="$!"
258
Chris Kay03ffbe72022-11-17 18:53:50 +0000259ports_output="$(mktempfile)"
260
Fathi Boudra422bf772019-12-02 11:10:16 +0200261# Parse UARTs ports from early model output. Send a SIGSTOP to the model
262# as soon as it outputs all UART ports. This is to prevent the model
263# executing before the expect scripts get a chance to connect to the
264# UART thereby losing messages.
265model_fail=1
266while :; do
Chris Kay03ffbe72022-11-17 18:53:50 +0000267 awk -v "num_uarts=$(get_num_uarts "${run_cwd}")" \
268 -f "$(get_ports_script "${run_cwd}")" "$model_out" > "$ports_output"
269 if [ $(wc -l < "$ports_output") -eq "$(get_num_uarts "${run_cwd}")" ]; then
Fathi Boudra422bf772019-12-02 11:10:16 +0200270 kill -SIGSTOP "$model_pid"
271 model_fail=0
272 break
273 fi
274
275 # Bail out if model exited meanwhile
276 if ! kill -0 "$model_pid" &>/dev/null; then
277 echo "Model terminated unexpectedly!"
278 break
279 fi
280done
281
282# Kill the watch dog
283kill_and_reap "$watchdog" || true
284
285# Check the model had failed meanwhile, for some reason
286if [ "$model_fail" -ne 0 ]; then
287 exit 1
288fi
289
Zelalem219df412020-05-17 19:21:20 -0500290if ! [ -x "$(command -v expect)" ]; then
291 echo "Error: Expect is not installed."
292 exit 1
293fi
294
Fathi Boudra422bf772019-12-02 11:10:16 +0200295# The wait loop above exited after model port numbers have been parsed. The
296# script's output is ready to be sourced now.
297declare -a ports
298source "$ports_output"
299rm -f "$ports_output"
Chris Kay03ffbe72022-11-17 18:53:50 +0000300if [ "${#ports[@]}" -ne "$(get_num_uarts "${run_cwd}")" ]; then
Fathi Boudra422bf772019-12-02 11:10:16 +0200301 echo "Failed to get UART port numbers"
302 kill_and_reap "$model_pid"
303 unset model_pid
304fi
305
306# Launch expect scripts for all UARTs
307uarts=0
Chris Kay03ffbe72022-11-17 18:53:50 +0000308for u in $(seq 0 $(( "$(get_num_uarts "${run_cwd}")" - 1 )) | tac); do
Fathi Boudra422bf772019-12-02 11:10:16 +0200309 script="run/uart$u/expect"
310 if [ -f "$script" ]; then
311 script="$(cat "$script")"
312 else
313 script=
314 fi
315
316 # Primary UART must have a script
317 if [ -z "$script" ]; then
Chris Kay03ffbe72022-11-17 18:53:50 +0000318 if [ "$u" = "$(get_primary_uart "${run_cwd}")" ]; then
Fathi Boudra422bf772019-12-02 11:10:16 +0200319 die "No primary UART script!"
320 else
Zelalem219df412020-05-17 19:21:20 -0500321 echo "Ignoring UART$u (no expect script provided)."
Fathi Boudra422bf772019-12-02 11:10:16 +0200322 continue
323 fi
324 fi
325
326 timeout="run/uart$u/timeout"
327 if [ -f "$timeout" ]; then
328 timeout="$(cat "$timeout")"
329 else
330 timeout=
331 fi
Saul Romerod54b6eb2020-10-20 09:03:08 +0100332 timeout="${timeout-1200}"
Fathi Boudra422bf772019-12-02 11:10:16 +0200333
334 full_log="$run_root/uart${u}_full.txt"
335
Chris Kay03ffbe72022-11-17 18:53:50 +0000336 if [ "$u" = "$(get_primary_uart "${run_cwd}")" ]; then
Fathi Boudra422bf772019-12-02 11:10:16 +0200337 star="*"
Fathi Boudra422bf772019-12-02 11:10:16 +0200338 else
339 star=" "
Fathi Boudra422bf772019-12-02 11:10:16 +0200340 fi
341
Madhukar Pappireddy1e953722021-11-08 15:23:02 -0600342 uart_name="uart$u"
343
Fathi Boudra422bf772019-12-02 11:10:16 +0200344 # Launch expect after exporting required variables
345 (
346 if [ -f "run/uart$u/env" ]; then
347 set -a
348 source "run/uart$u/env"
349 set +a
350 fi
351
Chris Kay03ffbe72022-11-17 18:53:50 +0000352 if [ "$u" = "$(get_primary_uart "${run_cwd}")" ] && upon "$primary_live"; then
Fathi Boudra422bf772019-12-02 11:10:16 +0200353 uart_port="${ports[$u]}" timeout="$timeout" \
354 name="$uart_name" launch expect -f "$ci_root/expect/$script" | \
355 tee "$full_log"
356 echo
357 else
358 uart_port="${ports[$u]}" timeout="$timeout" \
359 name="$uart_name" launch expect -f "$ci_root/expect/$script" \
360 &>"$full_log"
361 fi
362
363 ) &
364
365 let "uarts += 1"
366 echo "Tracking UART$u$star with $script; timeout $timeout."
367done
Fathi Boudra422bf772019-12-02 11:10:16 +0200368# Wait here long 'enough' for expect scripts to connect to ports; then
369# let the model proceed
370sleep 2
371kill -SIGCONT "$model_pid"
372
373# Wait for all children. Note that the wait below is *not* a timed wait.
374result=0
375
376set +e
377pushd "$pid_dir"
Chris Kay23f58692022-11-03 14:01:43 +0000378
379timeout=3600
380
381echo
382
Fathi Boudra422bf772019-12-02 11:10:16 +0200383while :; do
Chris Kay23f58692022-11-03 14:01:43 +0000384 readarray -d '' all < <(find "${pid_dir}" -name 'uart*.pid' -print0)
385 readarray -d '' succeeded < <(find "${pid_dir}" -name 'uart*.success' -print0)
386 readarray -d '' failed < <(find "${pid_dir}" -name 'uart*.fail' -print0)
Fathi Boudra422bf772019-12-02 11:10:16 +0200387
Chris Kay23f58692022-11-03 14:01:43 +0000388 all=("${all[@]##${pid_dir}/uart}")
389 all=("${all[@]%%.pid}")
390
391 succeeded=("${succeeded[@]##${pid_dir}/uart}")
392 succeeded=("${succeeded[@]%%.success}")
393
394 failed=("${failed[@]##${pid_dir}/uart}")
395 failed=("${failed[@]%%.fail}")
396
397 completed=("${succeeded[@]}" "${failed[@]}")
398
399 readarray -t remaining < <( \
400 comm -23 \
401 <(printf '%s\n' "${all[@]}" | sort) \
402 <(printf '%s\n' "${completed[@]}" | sort) \
403 )
404
405 if [ ${#remaining[@]} = 0 ]; then
Fathi Boudra422bf772019-12-02 11:10:16 +0200406 break
407 fi
408
Chris Kay23f58692022-11-03 14:01:43 +0000409 echo "Waiting ${timeout}s for ${#remaining[@]} UART(s): ${remaining[@]}"
410
Chris Kay03ffbe72022-11-17 18:53:50 +0000411 if [[ " ${completed[@]} " =~ " $(get_payload_uart "${run_cwd}") " ]]; then
412 echo "- Payload (UART $(get_payload_uart "${run_cwd}")) completed!"
Chris Kay23f58692022-11-03 14:01:43 +0000413
414 for uart in "${remaining[@]}"; do
415 pid=$(cat "${pid_dir}/uart${uart}.pid")
416
417 echo "- Terminating UART ${uart} script (PID ${pid})..."
418
419 kill -SIGINT ${pid} || true # Send Ctrl+C - don't force-kill it!
420 done
Fathi Boudra422bf772019-12-02 11:10:16 +0200421 fi
Chris Kay23f58692022-11-03 14:01:43 +0000422
423 if [ ${timeout} = 0 ]; then
424 echo "- Timeout exceeded! Killing model (PID ${model_pid})..."
425
426 kill_and_reap "${model_pid}"
427 fi
428
429 timeout=$((${timeout} - 5)) && sleep 5
Fathi Boudra422bf772019-12-02 11:10:16 +0200430done
Chris Kay23f58692022-11-03 14:01:43 +0000431
432echo
433
434if [ ${#failed[@]} != 0 ]; then
435 echo "${#failed[@]} UART(s) did not match expectations:"
436 echo
437
438 for uart in "${failed[@]}"; do
439 echo " - UART ${uart}: uart${uart}_full.txt"
440 done
441
442 echo
443
444 result=1
445fi
446
Fathi Boudra422bf772019-12-02 11:10:16 +0200447popd
448
Fathi Boudra422bf772019-12-02 11:10:16 +0200449if [ "$result" -eq 0 ]; then
450 echo "Test success!"
451else
452 echo "Test failed!"
453fi
454
455if upon "$jenkins_run"; then
456 echo
457 echo "Artefacts location: $BUILD_URL."
458 echo
459fi
460
461if upon "$jenkins_run" && upon "$artefacts_receiver" && [ -d "$workspace/run" ]; then
Zelalem219df412020-05-17 19:21:20 -0500462 source "$CI_ROOT/script/send_artefacts.sh" "run"
Fathi Boudra422bf772019-12-02 11:10:16 +0200463fi
464
Fathi Boudra422bf772019-12-02 11:10:16 +0200465
Zelalem219df412020-05-17 19:21:20 -0500466exit "$result"
Fathi Boudra422bf772019-12-02 11:10:16 +0200467# vim: set tw=80 sw=8 noet: