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