aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tools/perf/tests/shell/lock_contention.sh
blob: c1ec5762215ba43081fc848243ddf32fe1a82ac0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/bin/sh
# kernel lock contention analysis test
# SPDX-License-Identifier: GPL-2.0

set -e

err=0
perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
result=$(mktemp /tmp/__perf_test.result.XXXXX)

cleanup() {
	rm -f ${perfdata}
	rm -f ${result}
	trap - EXIT TERM INT
}

trap_cleanup() {
	cleanup
	exit ${err}
}
trap trap_cleanup EXIT TERM INT

check() {
	if [ "$(id -u)" != 0 ]; then
		echo "[Skip] No root permission"
		err=2
		exit
	fi

	if ! perf list | grep -q lock:contention_begin; then
		echo "[Skip] No lock contention tracepoints"
		err=2
		exit
	fi

	# shellcheck disable=SC2046
	if [ `nproc` -lt 4 ]; then
		echo "[Skip] Low number of CPUs (`nproc`), lock event cannot be triggered certainly"
		err=2
		exit
	fi
}

test_record()
{
	echo "Testing perf lock record and perf lock contention"
	perf lock record -o ${perfdata} -- perf bench sched messaging > /dev/null 2>&1
	# the output goes to the stderr and we expect only 1 output (-E 1)
	perf lock contention -i ${perfdata} -E 1 -q 2> ${result}
	if [ "$(cat "${result}" | wc -l)" != "1" ]; then
		echo "[Fail] Recorded result count is not 1:" "$(cat "${result}" | wc -l)"
		err=1
		exit
	fi
}

test_bpf()
{
	echo "Testing perf lock contention --use-bpf"

	if ! perf lock con -b true > /dev/null 2>&1 ; then
		echo "[Skip] No BPF support"
		return
	fi

	# the perf lock contention output goes to the stderr
	perf lock con -a -b -E 1 -q -- perf bench sched messaging > /dev/null 2> ${result}
	if [ "$(cat "${result}" | wc -l)" != "1" ]; then
		echo "[Fail] BPF result count is not 1:" "$(cat "${result}" | wc -l)"
		err=1
		exit
	fi
}

test_record_concurrent()
{
	echo "Testing perf lock record and perf lock contention at the same time"
	perf lock record -o- -- perf bench sched messaging 2> /dev/null | \
	perf lock contention -i- -E 1 -q 2> ${result}
	if [ "$(cat "${result}" | wc -l)" != "1" ]; then
		echo "[Fail] Recorded result count is not 1:" "$(cat "${result}" | wc -l)"
		err=1
		exit
	fi
}

test_aggr_task()
{
	echo "Testing perf lock contention --threads"
	perf lock contention -i ${perfdata} -t -E 1 -q 2> ${result}
	if [ "$(cat "${result}" | wc -l)" != "1" ]; then
		echo "[Fail] Recorded result count is not 1:" "$(cat "${result}" | wc -l)"
		err=1
		exit
	fi

	if ! perf lock con -b true > /dev/null 2>&1 ; then
		return
	fi

	# the perf lock contention output goes to the stderr
	perf lock con -a -b -t -E 1 -q -- perf bench sched messaging > /dev/null 2> ${result}
	if [ "$(cat "${result}" | wc -l)" != "1" ]; then
		echo "[Fail] BPF result count is not 1:" "$(cat "${result}" | wc -l)"
		err=1
		exit
	fi
}

test_aggr_addr()
{
	echo "Testing perf lock contention --lock-addr"
	perf lock contention -i ${perfdata} -l -E 1 -q 2> ${result}
	if [ "$(cat "${result}" | wc -l)" != "1" ]; then
		echo "[Fail] Recorded result count is not 1:" "$(cat "${result}" | wc -l)"
		err=1
		exit
	fi

	if ! perf lock con -b true > /dev/null 2>&1 ; then
		return
	fi

	# the perf lock contention output goes to the stderr
	perf lock con -a -b -l -E 1 -q -- perf bench sched messaging > /dev/null 2> ${result}
	if [ "$(cat "${result}" | wc -l)" != "1" ]; then
		echo "[Fail] BPF result count is not 1:" "$(cat "${result}" | wc -l)"
		err=1
		exit
	fi
}

test_aggr_cgroup()
{
	echo "Testing perf lock contention --lock-cgroup"

	if ! perf lock con -b true > /dev/null 2>&1 ; then
		echo "[Skip] No BPF support"
		return
	fi

	# the perf lock contention output goes to the stderr
	perf lock con -a -b -g -E 1 -q -- perf bench sched messaging > /dev/null 2> ${result}
	if [ "$(cat "${result}" | wc -l)" != "1" ]; then
		echo "[Fail] BPF result count is not 1:" "$(cat "${result}" | wc -l)"
		err=1
		exit
	fi
}

test_type_filter()
{
	echo "Testing perf lock contention --type-filter (w/ spinlock)"
	perf lock contention -i ${perfdata} -Y spinlock -q 2> ${result}
	if [ "$(grep -c -v spinlock "${result}")" != "0" ]; then
		echo "[Fail] Recorded result should not have non-spinlocks:" "$(cat "${result}")"
		err=1
		exit
	fi

	if ! perf lock con -b true > /dev/null 2>&1 ; then
		return
	fi

	perf lock con -a -b -Y spinlock -q -- perf bench sched messaging > /dev/null 2> ${result}
	if [ "$(grep -c -v spinlock "${result}")" != "0" ]; then
		echo "[Fail] BPF result should not have non-spinlocks:" "$(cat "${result}")"
		err=1
		exit
	fi
}

test_lock_filter()
{
	echo "Testing perf lock contention --lock-filter (w/ tasklist_lock)"
	perf lock contention -i ${perfdata} -l -q 2> ${result}
	if [ "$(grep -c tasklist_lock "${result}")" != "1" ]; then
		echo "[Skip] Could not find 'tasklist_lock'"
		return
	fi

	perf lock contention -i ${perfdata} -L tasklist_lock -q 2> ${result}

	# find out the type of tasklist_lock
	test_lock_filter_type=$(head -1 "${result}" | awk '{ print $8 }' | sed -e 's/:.*//')

	if [ "$(grep -c -v "${test_lock_filter_type}" "${result}")" != "0" ]; then
		echo "[Fail] Recorded result should not have non-${test_lock_filter_type} locks:" "$(cat "${result}")"
		err=1
		exit
	fi

	if ! perf lock con -b true > /dev/null 2>&1 ; then
		return
	fi

	perf lock con -a -b -L tasklist_lock -q -- perf bench sched messaging > /dev/null 2> ${result}
	if [ "$(grep -c -v "${test_lock_filter_type}" "${result}")" != "0" ]; then
		echo "[Fail] BPF result should not have non-${test_lock_filter_type} locks:" "$(cat "${result}")"
		err=1
		exit
	fi
}

test_stack_filter()
{
	echo "Testing perf lock contention --callstack-filter (w/ unix_stream)"
	perf lock contention -i ${perfdata} -v -q 2> ${result}
	if [ "$(grep -c unix_stream "${result}")" = "0" ]; then
		echo "[Skip] Could not find 'unix_stream'"
		return
	fi

	perf lock contention -i ${perfdata} -E 1 -S unix_stream -q 2> ${result}
	if [ "$(cat "${result}" | wc -l)" != "1" ]; then
		echo "[Fail] Recorded result should have a lock from unix_stream:" "$(cat "${result}")"
		err=1
		exit
	fi

	if ! perf lock con -b true > /dev/null 2>&1 ; then
		return
	fi

	perf lock con -a -b -S unix_stream -E 1 -q -- perf bench sched messaging > /dev/null 2> ${result}
	if [ "$(cat "${result}" | wc -l)" != "1" ]; then
		echo "[Fail] BPF result should have a lock from unix_stream:" "$(cat "${result}")"
		err=1
		exit
	fi
}

test_aggr_task_stack_filter()
{
	echo "Testing perf lock contention --callstack-filter with task aggregation"
	perf lock contention -i ${perfdata} -v -q 2> ${result}
	if [ "$(grep -c unix_stream "${result}")" = "0" ]; then
		echo "[Skip] Could not find 'unix_stream'"
		return
	fi

	perf lock contention -i ${perfdata} -t -E 1 -S unix_stream -q 2> ${result}
	if [ "$(cat "${result}" | wc -l)" != "1" ]; then
		echo "[Fail] Recorded result should have a task from unix_stream:" "$(cat "${result}")"
		err=1
		exit
	fi

	if ! perf lock con -b true > /dev/null 2>&1 ; then
		return
	fi

	perf lock con -a -b -t -S unix_stream -E 1 -q -- perf bench sched messaging > /dev/null 2> ${result}
	if [ "$(cat "${result}" | wc -l)" != "1" ]; then
		echo "[Fail] BPF result should have a task from unix_stream:" "$(cat "${result}")"
		err=1
		exit
	fi
}
test_cgroup_filter()
{
	echo "Testing perf lock contention --cgroup-filter"

	if ! perf lock con -b true > /dev/null 2>&1 ; then
		echo "[Skip] No BPF support"
		return
	fi

	perf lock con -a -b -g -E 1 -F wait_total -q -- perf bench sched messaging > /dev/null 2> ${result}
	if [ "$(cat "${result}" | wc -l)" != "1" ]; then
		echo "[Fail] BPF result should have a cgroup result:" "$(cat "${result}")"
		err=1
		exit
	fi

	cgroup=$(cat "${result}" | awk '{ print $3 }')
	perf lock con -a -b -g -E 1 -G "${cgroup}" -q -- perf bench sched messaging > /dev/null 2> ${result}
	if [ "$(cat "${result}" | wc -l)" != "1" ]; then
		echo "[Fail] BPF result should have a result with cgroup filter:" "$(cat "${cgroup}")"
		err=1
		exit
	fi
}


test_csv_output()
{
	echo "Testing perf lock contention CSV output"
	perf lock contention -i ${perfdata} -E 1 -x , --output ${result}
	# count the number of commas in the header
	# it should have 5: contended, total-wait, max-wait, avg-wait, type, caller
	header=$(grep "# output:" ${result} | tr -d -c , | wc -c)
	if [ "${header}" != "5" ]; then
		echo "[Fail] Recorded result does not have enough output columns: ${header} != 5"
		err=1
		exit
	fi
	# count the number of commas in the output
	output=$(grep -v "^#" ${result} | tr -d -c , | wc -c)
	if [ "${header}" != "${output}" ]; then
		echo "[Fail] Recorded result does not match the number of commas: ${header} != ${output}"
		err=1
		exit
	fi

	if ! perf lock con -b true > /dev/null 2>&1 ; then
		echo "[Skip] No BPF support"
		return
	fi

	# the perf lock contention output goes to the stderr
	perf lock con -a -b -E 1 -x , --output ${result} -- perf bench sched messaging > /dev/null 2>&1
	output=$(grep -v "^#" ${result} | tr -d -c , | wc -c)
	if [ "${header}" != "${output}" ]; then
		echo "[Fail] BPF result does not match the number of commas: ${header} != ${output}"
		err=1
		exit
	fi
}

check

test_record
test_bpf
test_record_concurrent
test_aggr_task
test_aggr_addr
test_aggr_cgroup
test_type_filter
test_lock_filter
test_stack_filter
test_aggr_task_stack_filter
test_cgroup_filter
test_csv_output

exit ${err}