xref: /linux/tools/tracing/rtla/tests/engine.sh (revision d9f24f8e60798c066ead61f77e67ee6a5a204514)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3test_begin() {
4	# Count tests to allow the test harness to double-check if all were
5	# included correctly.
6	ctr=0
7	[ -z "$RTLA" ] && RTLA="./rtla"
8	[ -n "$TEST_COUNT" ] && echo "1..$TEST_COUNT"
9}
10
11reset_osnoise() {
12	# Reset osnoise options to default and remove any dangling instances created
13	# by improperly exited rtla runs.
14	pushd /sys/kernel/tracing >/dev/null || return 1
15
16	# Remove dangling instances created by previous rtla run
17	echo 0 > tracing_thresh
18	cd instances
19	for i in osnoise_top osnoise_hist timerlat_top timerlat_hist
20	do
21		[ ! -d "$i" ] && continue
22		rmdir "$i"
23	done
24
25	# Reset options to default
26	# Note: those are copied from the default values of osnoise_data
27	# in kernel/trace/trace_osnoise.c
28	cd ../osnoise
29	echo all > cpus
30	echo DEFAULTS > options
31	echo 1000000 > period_us
32	echo 0 > print_stack
33	echo 1000000 > runtime_us
34	echo 0 > stop_tracing_total_us
35	echo 0 > stop_tracing_us
36	echo 1000 > timerlat_period_us
37
38	popd >/dev/null
39}
40
41check() {
42	test_name=$0
43	tested_command=$1
44	expected_exitcode=${3:-0}
45	expected_output=$4
46	unexpected_output=$5
47	# Simple check: run rtla with given arguments and test exit code.
48	# If TEST_COUNT is set, run the test. Otherwise, just count.
49	ctr=$(($ctr + 1))
50	if [ -n "$TEST_COUNT" ]
51	then
52		# Reset osnoise options before running test.
53		[ "$NO_RESET_OSNOISE" == 1 ] || reset_osnoise
54		# Run rtla; in case of failure, include its output as comment
55		# in the test results.
56		result=$(eval stdbuf -oL $TIMEOUT "$RTLA" $2 2>&1); exitcode=$?
57		failbuf=''
58		fail=0
59
60		# Test if the results matches if requested
61		if [ -n "$expected_output" ] && ! grep -qE "$expected_output" <<< "$result"
62		then
63			fail=1
64			failbuf+=$(printf "# Output match failed: \"%s\"" "$expected_output")
65			failbuf+=$'\n'
66		fi
67
68		if [ -n "$unexpected_output" ] && grep -qE "$unexpected_output" <<< "$result"
69		then
70			fail=1
71			failbuf+=$(printf "# Output non-match failed: \"%s\"" "$unexpected_output")
72			failbuf+=$'\n'
73		fi
74
75		if [ $exitcode -eq $expected_exitcode ] && [ $fail -eq 0 ]
76		then
77			echo "ok $ctr - $1"
78		else
79			# Add rtla output and exit code as comments in case of failure
80			echo "not ok $ctr - $1"
81			echo -n "$failbuf"
82			echo "$result" | col -b | while read line; do echo "# $line"; done
83			printf "#\n# exit code %s\n" $exitcode
84		fi
85	fi
86}
87
88check_with_osnoise_options() {
89	# Do the same as "check", but with pre-set osnoise options.
90	# Note: rtla should reset the osnoise options, this is used to test
91	# if it indeed does so.
92	# Save original arguments
93	arg1=$1
94	arg2=$2
95	arg3=$3
96
97	# Apply osnoise options (if not dry run)
98	if [ -n "$TEST_COUNT" ]
99	then
100		[ "$NO_RESET_OSNOISE" == 1 ] || reset_osnoise
101		shift
102		shift
103		while shift
104		do
105			[ "$1" == "" ] && continue
106			option=$(echo $1 | cut -d '=' -f 1)
107			value=$(echo $1 | cut -d '=' -f 2)
108			echo "option: $option, value: $value"
109			echo "$value" > "/sys/kernel/tracing/osnoise/$option" || return 1
110		done
111	fi
112
113	NO_RESET_OSNOISE=1 check "$arg1" "$arg2" "$arg3"
114}
115
116set_timeout() {
117	TIMEOUT="timeout -v -k 15s $1"
118}
119
120unset_timeout() {
121	unset TIMEOUT
122}
123
124set_no_reset_osnoise() {
125	NO_RESET_OSNOISE=1
126}
127
128unset_no_reset_osnoise() {
129	unset NO_RESET_OSNOISE
130}
131
132test_end() {
133	# If running without TEST_COUNT, tests are not actually run, just
134	# counted. In that case, re-run the test with the correct count.
135	[ -z "$TEST_COUNT" ] && TEST_COUNT=$ctr exec bash $0 || true
136}
137
138# Avoid any environmental discrepancies
139export LC_ALL=C
140unset_timeout
141