summaryrefslogtreecommitdiff
path: root/tools/tracing/rtla/tests/engine.sh
blob: a97d644ead99f60bdb7287019c4061a24a7b247f (plain)
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
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
test_begin() {
	# Count tests to allow the test harness to double-check if all were
	# included correctly.
	ctr=0
	[ -z "$RTLA" ] && RTLA="./rtla"
	[ -n "$TEST_COUNT" ] && echo "1..$TEST_COUNT"
}

reset_osnoise() {
	# Reset osnoise options to default and remove any dangling instances created
	# by improperly exited rtla runs.
	pushd /sys/kernel/tracing >/dev/null || return 1

	# Remove dangling instances created by previous rtla run
	echo 0 > tracing_thresh
	cd instances
	for i in osnoise_top osnoise_hist timerlat_top timerlat_hist
	do
		[ ! -d "$i" ] && continue
		rmdir "$i"
	done

	# Reset options to default
	# Note: those are copied from the default values of osnoise_data
	# in kernel/trace/trace_osnoise.c
	cd ../osnoise
	echo all > cpus
	echo DEFAULTS > options
	echo 1000000 > period_us
	echo 0 > print_stack
	echo 1000000 > runtime_us
	echo 0 > stop_tracing_total_us
	echo 0 > stop_tracing_us
	echo 1000 > timerlat_period_us

	popd >/dev/null
}

check() {
	test_name=$0
	tested_command=$1
	expected_exitcode=${3:-0}
	expected_output=$4
	# Simple check: run rtla with given arguments and test exit code.
	# If TEST_COUNT is set, run the test. Otherwise, just count.
	ctr=$(($ctr + 1))
	if [ -n "$TEST_COUNT" ]
	then
		# Reset osnoise options before running test.
		[ "$NO_RESET_OSNOISE" == 1 ] || reset_osnoise
		# Run rtla; in case of failure, include its output as comment
		# in the test results.
		result=$(eval stdbuf -oL $TIMEOUT "$RTLA" $2 2>&1); exitcode=$?
		# Test if the results matches if requested
		if [ -n "$expected_output" ]
		then
			grep -E "$expected_output" <<< "$result" > /dev/null; grep_result=$?
		else
			grep_result=0
		fi

		if [ $exitcode -eq $expected_exitcode ] && [ $grep_result -eq 0 ]
		then
			echo "ok $ctr - $1"
		else
			echo "not ok $ctr - $1"
			# Add rtla output and exit code as comments in case of failure
			echo "$result" | col -b | while read line; do echo "# $line"; done
			printf "#\n# exit code %s\n" $exitcode
			[ -n "$expected_output" ] && [ $grep_result -ne 0 ] && \
				printf "# Output match failed: \"%s\"\n" "$expected_output"
		fi
	fi
}

check_with_osnoise_options() {
	# Do the same as "check", but with pre-set osnoise options.
	# Note: rtla should reset the osnoise options, this is used to test
	# if it indeed does so.
	# Save original arguments
	arg1=$1
	arg2=$2
	arg3=$3

	# Apply osnoise options (if not dry run)
	if [ -n "$TEST_COUNT" ]
	then
		[ "$NO_RESET_OSNOISE" == 1 ] || reset_osnoise
		shift
		shift
		while shift
		do
			[ "$1" == "" ] && continue
			option=$(echo $1 | cut -d '=' -f 1)
			value=$(echo $1 | cut -d '=' -f 2)
			echo "option: $option, value: $value"
			echo "$value" > "/sys/kernel/tracing/osnoise/$option" || return 1
		done
	fi

	NO_RESET_OSNOISE=1 check "$arg1" "$arg2" "$arg3"
}

set_timeout() {
	TIMEOUT="timeout -v -k 15s $1"
}

unset_timeout() {
	unset TIMEOUT
}

set_no_reset_osnoise() {
	NO_RESET_OSNOISE=1
}

unset_no_reset_osnoise() {
	unset NO_RESET_OSNOISE
}

test_end() {
	# If running without TEST_COUNT, tests are not actually run, just
	# counted. In that case, re-run the test with the correct count.
	[ -z "$TEST_COUNT" ] && TEST_COUNT=$ctr exec bash $0 || true
}

# Avoid any environmental discrepancies
export LC_ALL=C
unset_timeout