xref: /linux/tools/perf/tests/shell/coresight/context_switch_thread.sh (revision efe86f088f48f18c27b648e5724048947f3b7fb4)
1#!/bin/bash -e
2# CoreSight context switch thread attribution (exclusive)
3
4# SPDX-License-Identifier: GPL-2.0
5
6# If CoreSight is not available, skip the test
7perf list pmu | grep -q cs_etm || exit 2
8
9if [ "$(id -u)" != 0 ]; then
10	# Requires root for "-C 0" in record command
11	echo "[Skip] No root permission"
12	exit 2
13fi
14
15tmpdir=$(mktemp -d /tmp/__perf_test.coresight_context_switch.XXXXX)
16
17cleanup() {
18	rm -rf "${tmpdir}"
19	trap - EXIT TERM INT
20}
21
22trap_cleanup() {
23	cleanup
24	exit 1
25}
26trap trap_cleanup EXIT TERM INT
27
28check_samples() {
29	owner_samples=$(grep -c "proc1.*context_switch_loop_proc1" "$tmpdir/script" || true)
30	next_samples=$(grep -c "proc2.*context_switch_loop_proc2" "$tmpdir/script" || true)
31
32	if [ "$owner_samples" -eq 0 ] || [ "$next_samples" -eq 0 ]; then
33		echo "No samples found"
34		cleanup
35		exit 1
36	fi
37
38	if grep "proc2.*context_switch_loop_proc1" "$tmpdir/script"; then
39		echo "Thread1 symbol was attributed to proc2"
40		cleanup
41		exit 1
42	fi
43
44	if grep "proc1.*context_switch_loop_proc2" "$tmpdir/script"; then
45		echo "Thread2 symbol was attributed to proc1"
46		cleanup
47		exit 1
48	fi
49}
50
51cf="$tmpdir/ctl"
52af="$tmpdir/ack"
53mkfifo "$cf" "$af"
54
55# Pin to one CPU so the two threads alternate running but record into the same
56# trace buffer. Start disabled and use the control FIFO to only record the
57# workload and not startup.
58perf record -o "$tmpdir/data" -e cs_etm/timestamp=0/u -C 0 -D -1 --control fifo:"$cf","$af" -- \
59	taskset --cpu-list 0 perf test --record-ctl fifo:"$cf","$af" \
60	-w context_switch_loop > /dev/null 2>&1
61
62# Test both instruction and branch sample generation modes.
63perf script -i "$tmpdir/data" --itrace=i4 -F comm,pid,tid,ip,sym > "$tmpdir/script" 2>/dev/null
64check_samples
65perf script -i "$tmpdir/data" --itrace=b -F comm,pid,tid,ip,sym > "$tmpdir/script" 2>/dev/null
66check_samples
67
68cleanup
69exit 0
70