xref: /linux/tools/tracing/rtla/tests/engine.sh (revision e78f70bad29c5ae1e1076698b690b15794e9b81e)
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 || 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
39}
40
41check() {
42	expected_exitcode=${3:-0}
43	# Simple check: run rtla with given arguments and test exit code.
44	# If TEST_COUNT is set, run the test. Otherwise, just count.
45	ctr=$(($ctr + 1))
46	if [ -n "$TEST_COUNT" ]
47	then
48		# Reset osnoise options before running test.
49		[ "$NO_RESET_OSNOISE" == 1 ] || reset_osnoise
50		# Run rtla; in case of failure, include its output as comment
51		# in the test results.
52		result=$(stdbuf -oL $TIMEOUT "$RTLA" $2 2>&1); exitcode=$?
53		if [ $exitcode -eq $expected_exitcode ]
54		then
55			echo "ok $ctr - $1"
56		else
57			echo "not ok $ctr - $1"
58			# Add rtla output and exit code as comments in case of failure
59			echo "$result" | col -b | while read line; do echo "# $line"; done
60			printf "#\n# exit code %s\n" $exitcode
61		fi
62	fi
63}
64
65check_with_osnoise_options() {
66	# Do the same as "check", but with pre-set osnoise options.
67	# Note: rtla should reset the osnoise options, this is used to test
68	# if it indeed does so.
69	# Save original arguments
70	arg1=$1
71	arg2=$2
72	arg3=$3
73
74	# Apply osnoise options (if not dry run)
75	if [ -n "$TEST_COUNT" ]
76	then
77		[ "$NO_RESET_OSNOISE" == 1 ] || reset_osnoise
78		shift
79		shift
80		while shift
81		do
82			[ "$1" == "" ] && continue
83			option=$(echo $1 | cut -d '=' -f 1)
84			value=$(echo $1 | cut -d '=' -f 2)
85			echo "option: $option, value: $value"
86			echo "$value" > "/sys/kernel/tracing/osnoise/$option" || return 1
87		done
88	fi
89
90	NO_RESET_OSNOISE=1 check "$arg1" "$arg2" "$arg3"
91}
92
93set_timeout() {
94	TIMEOUT="timeout -v -k 15s $1"
95}
96
97unset_timeout() {
98	unset TIMEOUT
99}
100
101set_no_reset_osnoise() {
102	NO_RESET_OSNOISE=1
103}
104
105unset_no_reset_osnoise() {
106	unset NO_RESET_OSNOISE
107}
108
109test_end() {
110	# If running without TEST_COUNT, tests are not actually run, just
111	# counted. In that case, re-run the test with the correct count.
112	[ -z "$TEST_COUNT" ] && TEST_COUNT=$ctr exec bash $0 || true
113}
114
115# Avoid any environmental discrepancies
116export LC_ALL=C
117unset_timeout
118