1#!/bin/sh
2# kernel lock contention analysis test
3# SPDX-License-Identifier: GPL-2.0
4
5set -e
6
7err=0
8perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
9result=$(mktemp /tmp/__perf_test.result.XXXXX)
10
11cleanup() {
12	rm -f ${perfdata}
13	rm -f ${result}
14	trap - exit term int
15}
16
17trap_cleanup() {
18	cleanup
19	exit ${err}
20}
21trap trap_cleanup exit term int
22
23check() {
24	if [ `id -u` != 0 ]; then
25		echo "[Skip] No root permission"
26		err=2
27		exit
28	fi
29
30	if ! perf list | grep -q lock:contention_begin; then
31		echo "[Skip] No lock contention tracepoints"
32		err=2
33		exit
34	fi
35}
36
37test_record()
38{
39	echo "Testing perf lock record and perf lock contention"
40	perf lock record -o ${perfdata} -- perf bench sched messaging > /dev/null 2>&1
41	# the output goes to the stderr and we expect only 1 output (-E 1)
42	perf lock contention -i ${perfdata} -E 1 -q 2> ${result}
43	if [ $(cat "${result}" | wc -l) != "1" ]; then
44		echo "[Fail] Recorded result count is not 1:" $(cat "${result}" | wc -l)
45		err=1
46		exit
47	fi
48}
49
50test_bpf()
51{
52	echo "Testing perf lock contention --use-bpf"
53
54	if ! perf lock con -b true > /dev/null 2>&1 ; then
55		echo "[Skip] No BPF support"
56		exit
57	fi
58
59	# the perf lock contention output goes to the stderr
60	perf lock con -a -b -E 1 -q -- perf bench sched messaging > /dev/null 2> ${result}
61	if [ $(cat "${result}" | wc -l) != "1" ]; then
62		echo "[Fail] BPF result count is not 1:" $(cat "${result}" | wc -l)
63		err=1
64		exit
65	fi
66}
67
68check
69
70test_record
71test_bpf
72
73exit ${err}
74