xref: /linux/tools/perf/tests/shell/lock_contention.sh (revision e96b95c2b7a63a454b6498e2df67aac14d046d13)
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		return
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
68test_record_concurrent()
69{
70	echo "Testing perf lock record and perf lock contention at the same time"
71	perf lock record -o- -- perf bench sched messaging 2> /dev/null | \
72	perf lock contention -i- -E 1 -q 2> ${result}
73	if [ $(cat "${result}" | wc -l) != "1" ]; then
74		echo "[Fail] Recorded result count is not 1:" $(cat "${result}" | wc -l)
75		err=1
76		exit
77	fi
78}
79
80test_aggr_task()
81{
82	echo "Testing perf lock contention --threads"
83	perf lock contention -i ${perfdata} -t -E 1 -q 2> ${result}
84	if [ $(cat "${result}" | wc -l) != "1" ]; then
85		echo "[Fail] Recorded result count is not 1:" $(cat "${result}" | wc -l)
86		err=1
87		exit
88	fi
89
90	if ! perf lock con -b true > /dev/null 2>&1 ; then
91		return
92	fi
93
94	# the perf lock contention output goes to the stderr
95	perf lock con -a -b -t -E 1 -q -- perf bench sched messaging > /dev/null 2> ${result}
96	if [ $(cat "${result}" | wc -l) != "1" ]; then
97		echo "[Fail] BPF result count is not 1:" $(cat "${result}" | wc -l)
98		err=1
99		exit
100	fi
101}
102
103test_aggr_addr()
104{
105	echo "Testing perf lock contention --lock-addr"
106	perf lock contention -i ${perfdata} -l -E 1 -q 2> ${result}
107	if [ $(cat "${result}" | wc -l) != "1" ]; then
108		echo "[Fail] Recorded result count is not 1:" $(cat "${result}" | wc -l)
109		err=1
110		exit
111	fi
112
113	if ! perf lock con -b true > /dev/null 2>&1 ; then
114		return
115	fi
116
117	# the perf lock contention output goes to the stderr
118	perf lock con -a -b -t -E 1 -q -- perf bench sched messaging > /dev/null 2> ${result}
119	if [ $(cat "${result}" | wc -l) != "1" ]; then
120		echo "[Fail] BPF result count is not 1:" $(cat "${result}" | wc -l)
121		err=1
122		exit
123	fi
124}
125
126check
127
128test_record
129test_bpf
130test_record_concurrent
131test_aggr_task
132test_aggr_addr
133
134exit ${err}
135