Sync scripts with Arm internal CI

This patch syncs utility scripts and scripts
in the script directory with the internal CI.

Where a path update is required,
the changes have been commented out.

Signed-off-by: Zelalem <zelalem.aweke@arm.com>
Change-Id: Ifa4bd805e345184d1378e8423e5f878a2fbfbcd4
diff --git a/script/run_package.sh b/script/run_package.sh
index 382c336..a9d91d2 100755
--- a/script/run_package.sh
+++ b/script/run_package.sh
@@ -1,6 +1,6 @@
 #!/bin/bash
 #
-# Copyright (c) 2019, Arm Limited. All rights reserved.
+# Copyright (c) 2019-2020, Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -24,14 +24,22 @@
 
 kill_and_reap() {
 	local gid
-
 	# Kill an active process. Ignore errors
 	[ "$1" ] || return 0
 	kill -0 "$1" &>/dev/null || return 0
 
+	# Kill the children
+	kill -- "-$1"  &>/dev/null || true
 	# Kill the group
-	gid="$(awk '{print $5}' < /proc/$1/stat)"
-	kill -SIGKILL -- "-$gid" &>/dev/null || true
+	{ gid="$(awk '{print $5}' < /proc/$1/stat)";} 2>/dev/null || return
+	# For Code Coverage plugin it is needed to propagate
+	# the kill signal to the plugin in order to save
+	# the trace statistics.
+	if [ "${COVERAGE_ON}" == "1" ] || [ -n "$cc_enable" ]; then
+		kill -SIGTERM -- "-$gid" &>/dev/null || true
+	else
+		kill -SIGKILL -- "-$gid" &>/dev/null || true
+	fi
 	wait "$gid" &>/dev/null || true
 }
 
@@ -44,7 +52,19 @@
 	set +e
 	while read pid; do
 		pid="$(cat $pid)"
-		kill_and_reap "$pid"
+		# Forcefully killing model process does not show statistical
+		# data (Host CPU time spent running in User and System). Safely
+		# kill the model by using SIGINT(^C) that helps in printing
+		# statistical data.
+		if [ "$pid" == "$model_pid" ]; then
+			model_cid=$(pgrep -P "$model_pid" | xargs)
+			# ignore errors
+			kill -SIGINT "$model_cid" &>/dev/null || true
+		# Allow some time to print data
+			sleep 2
+		else
+			kill_and_reap "$pid"
+		fi
 	done < <(find -name '*.pid')
 	popd
 }
@@ -114,11 +134,54 @@
 model_out="$run_root/model_log.txt"
 run_sh="$run_root/run.sh"
 
+
 # Generate run.sh
 echo "$model_path \\" > "$run_sh"
 sed '/^\s*$/d' < model_params | sort | sed 's/^/\t/;s/$/ \\/' >> "$run_sh"
+
+if [ "${COVERAGE_ON}" == "1" ]; then
+	# Adding code coverage plugin
+	echo -e "\t-C TRACE.CoverageTrace.trace-file-prefix=$trace_file_prefix \\" >> "$run_sh"
+	echo -e "\t--plugin $coverage_trace_plugin \\" >> "$run_sh"
+fi
 echo -e "\t\"\$@\"" >> "$run_sh"
 
+# Running Reboot/Shutdown tests requires storing the state in non-volatile
+# memory(NVM) across reboot. On FVP, NVM is not persistent across reboot, hence
+# NVM was saved to a file($NVM_file) when running the model using the run.sh
+# shell script.
+# If TFTF Reboot/Shutdown tests are enabled, run the fvp model 10 times by
+# feeding the file containing NVM state generated from the previous run. Note
+# that this file also includes FIP image.
+
+if upon "$run_tftf_reboot_tests" = "1"; then
+	tftf_reboot_tests="$run_root/tftf_reboot_tests.sh"
+
+	# Generate tftf_reboot_tests command. It is similar to run_sh.
+	# The model would run the reboot and shutdown tests 10 times
+	# The uart log file generated by FVP model gets overwritten
+	# across reboots. Copy its contents at the end of the test
+	echo "cat $uart0_file >> UART0.log" >>"$tftf_reboot_tests"
+	echo "cat $uart1_file >> UART1.log" >>"$tftf_reboot_tests"
+	cat <<EOF >>"$tftf_reboot_tests"
+
+for i in {1..10}
+do
+EOF
+	cat "$run_sh" >> "$tftf_reboot_tests"
+	echo "cat $uart0_file >> UART0.log" >>"$tftf_reboot_tests"
+	echo "cat $uart1_file >> UART1.log" >>"$tftf_reboot_tests"
+        cat <<EOF >>"$tftf_reboot_tests"
+done
+EOF
+	#Replace fip.bin with file $NVM_file
+	sed -i 's/fip.bin/'"$NVM_file"'/' "$tftf_reboot_tests"
+
+	echo "TFTF Reboot/Shutdown Tests Enabled"
+	cat "$tftf_reboot_tests" >> "$run_sh"
+	rm "$tftf_reboot_tests"
+fi
+
 echo "Model command line:"
 echo
 cat "$run_sh"
@@ -151,8 +214,8 @@
 		die "Failed to launch model!"
 	fi
 done
-model_pid="$(cat "$pid_dir/model.pid")"
 
+model_pid="$(cat $pid_dir/model.pid)"
 ports_output="$(mktempfile)"
 if not_upon "$ports_script"; then
 	# Default AWK script to parse model ports
@@ -217,6 +280,11 @@
 	exit 1
 fi
 
+if ! [ -x "$(command -v expect)" ]; then
+	echo "Error: Expect is not installed."
+	exit 1
+fi
+
 # The wait loop above exited after model port numbers have been parsed. The
 # script's output is ready to be sourced now.
 declare -a ports
@@ -230,7 +298,7 @@
 
 # Launch expect scripts for all UARTs
 uarts=0
-for u in $(seq 0 $num_uarts | tac); do
+for u in $(seq 0 $(( $num_uarts - 1 )) | tac); do
 	script="run/uart$u/expect"
 	if [ -f "$script" ]; then
 		script="$(cat "$script")"
@@ -243,6 +311,7 @@
 		if [ "$u" = "$primary_uart" ]; then
 			die "No primary UART script!"
 		else
+			echo "Ignoring UART$u (no expect script provided)."
 			continue
 		fi
 	fi
@@ -289,7 +358,6 @@
 	let "uarts += 1"
 	echo "Tracking UART$u$star with $script; timeout $timeout."
 done
-
 # Wait here long 'enough' for expect scripts to connect to ports; then
 # let the model proceed
 sleep 2
@@ -331,19 +399,9 @@
 fi
 
 if upon "$jenkins_run" && upon "$artefacts_receiver" && [ -d "$workspace/run" ]; then
-	pushd "$workspace"
-	run_archive="run.tar.xz"
-	tar -cJf "$run_archive" "run"
-	where="$artefacts_receiver/${TEST_GROUP:?}/${TEST_CONFIG:?}/$run_archive"
-	where+="?j=$JOB_NAME&b=$BUILD_NUMBER"
-	if wget -q --method=PUT --body-file="$run_archive" "$where"; then
-		echo "Run logs submitted to $where."
-	else
-		echo "Error submitting run logs to $where."
-	fi
-	popd
+	source "$CI_ROOT/script/send_artefacts.sh" "run"
 fi
 
-exit "$result"
 
+exit "$result"
 # vim: set tw=80 sw=8 noet: