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