2 # kernel lock contention analysis test
3 # SPDX-License-Identifier: GPL-2.0
8 perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
9 result=$(mktemp /tmp/__perf_test.result.XXXXX)
21 trap trap_cleanup EXIT TERM INT
24 if [ `id -u` != 0 ]; then
25 echo "[Skip] No root permission"
30 if ! perf list | grep -q lock:contention_begin; then
31 echo "[Skip] No lock contention tracepoints"
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)"
52 echo "Testing perf lock contention --use-bpf"
54 if ! perf lock con -b true > /dev/null 2>&1 ; then
55 echo "[Skip] No BPF support"
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)"
68 test_record_concurrent()
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)"
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)"
90 if ! perf lock con -b true > /dev/null 2>&1 ; then
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)"
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)"
113 if ! perf lock con -b true > /dev/null 2>&1 ; then
117 # the perf lock contention output goes to the stderr
118 perf lock con -a -b -l -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)"
128 echo "Testing perf lock contention --type-filter (w/ spinlock)"
129 perf lock contention -i ${perfdata} -Y spinlock -q 2> ${result}
130 if [ "$(grep -c -v spinlock "${result}")" != "0" ]; then
131 echo "[Fail] Recorded result should not have non-spinlocks:" "$(cat "${result}")"
136 if ! perf lock con -b true > /dev/null 2>&1 ; then
140 perf lock con -a -b -Y spinlock -q -- perf bench sched messaging > /dev/null 2> ${result}
141 if [ "$(grep -c -v spinlock "${result}")" != "0" ]; then
142 echo "[Fail] BPF result should not have non-spinlocks:" "$(cat "${result}")"
150 echo "Testing perf lock contention --lock-filter (w/ tasklist_lock)"
151 perf lock contention -i ${perfdata} -l -q 2> ${result}
152 if [ "$(grep -c tasklist_lock "${result}")" != "1" ]; then
153 echo "[Skip] Could not find 'tasklist_lock'"
157 perf lock contention -i ${perfdata} -L tasklist_lock -q 2> ${result}
159 # find out the type of tasklist_lock
160 local type=$(head -1 "${result}" | awk '{ print $8 }' | sed -e 's/:.*//')
162 if [ "$(grep -c -v "${type}" "${result}")" != "0" ]; then
163 echo "[Fail] Recorded result should not have non-${type} locks:" "$(cat "${result}")"
168 if ! perf lock con -b true > /dev/null 2>&1 ; then
172 perf lock con -a -b -L tasklist_lock -q -- perf bench sched messaging > /dev/null 2> ${result}
173 if [ "$(grep -c -v "${type}" "${result}")" != "0" ]; then
174 echo "[Fail] BPF result should not have non-${type} locks:" "$(cat "${result}")"
182 echo "Testing perf lock contention --callstack-filter (w/ unix_stream)"
183 perf lock contention -i ${perfdata} -v -q 2> ${result}
184 if [ "$(grep -c unix_stream "${result}")" = "0" ]; then
185 echo "[Skip] Could not find 'unix_stream'"
189 perf lock contention -i ${perfdata} -E 1 -S unix_stream -q 2> ${result}
190 if [ "$(cat "${result}" | wc -l)" != "1" ]; then
191 echo "[Fail] Recorded result should have a lock from unix_stream:" "$(cat "${result}")"
196 if ! perf lock con -b true > /dev/null 2>&1 ; then
200 perf lock con -a -b -S unix_stream -E 1 -q -- perf bench sched messaging > /dev/null 2> ${result}
201 if [ "$(cat "${result}" | wc -l)" != "1" ]; then
202 echo "[Fail] BPF result should have a lock from unix_stream:" "$(cat "${result}")"
208 test_aggr_task_stack_filter()
210 echo "Testing perf lock contention --callstack-filter with task aggregation"
211 perf lock contention -i ${perfdata} -v -q 2> ${result}
212 if [ "$(grep -c unix_stream "${result}")" = "0" ]; then
213 echo "[Skip] Could not find 'unix_stream'"
217 perf lock contention -i ${perfdata} -t -E 1 -S unix_stream -q 2> ${result}
218 if [ "$(cat "${result}" | wc -l)" != "1" ]; then
219 echo "[Fail] Recorded result should have a task from unix_stream:" "$(cat "${result}")"
224 if ! perf lock con -b true > /dev/null 2>&1 ; then
228 perf lock con -a -b -t -S unix_stream -E 1 -q -- perf bench sched messaging > /dev/null 2> ${result}
229 if [ "$(cat "${result}" | wc -l)" != "1" ]; then
230 echo "[Fail] BPF result should have a task from unix_stream:" "$(cat "${result}")"
238 echo "Testing perf lock contention CSV output"
239 perf lock contention -i ${perfdata} -E 1 -x , --output ${result}
240 # count the number of commas in the header
241 # it should have 5: contended, total-wait, max-wait, avg-wait, type, caller
242 header=$(grep "# output:" ${result} | tr -d -c , | wc -c)
243 if [ "${header}" != "5" ]; then
244 echo "[Fail] Recorded result does not have enough output columns: ${header} != 5"
248 # count the number of commas in the output
249 output=$(grep -v "^#" ${result} | tr -d -c , | wc -c)
250 if [ "${header}" != "${output}" ]; then
251 echo "[Fail] Recorded result does not match the number of commas: ${header} != ${output}"
256 if ! perf lock con -b true > /dev/null 2>&1 ; then
257 echo "[Skip] No BPF support"
261 # the perf lock contention output goes to the stderr
262 perf lock con -a -b -E 1 -x , --output ${result} -- perf bench sched messaging > /dev/null 2>&1
263 output=$(grep -v "^#" ${result} | tr -d -c , | wc -c)
264 if [ "${header}" != "${output}" ]; then
265 echo "[Fail] BPF result does not match the number of commas: ${header} != ${output}"
275 test_record_concurrent
281 test_aggr_task_stack_filter