summaryrefslogtreecommitdiff
path: root/tools/perf/tests/shell/script_dlfilter.sh
blob: 45c97d4a7d5f90e84bd7517f39c6c8fa97d89b7e (plain)
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
#!/bin/bash
# perf script --dlfilter tests
# SPDX-License-Identifier: GPL-2.0

set -e

shelldir=$(dirname "$0")
# shellcheck source=lib/setup_python.sh
. "${shelldir}"/lib/setup_python.sh

# skip if there's no compiler
if ! [ -x "$(command -v cc)" ]; then
	echo "failed: no compiler, install gcc"
	exit 2
fi

err=0
perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
dlfilter_c=$(mktemp /tmp/__perf_test.dlfilter.test.c.XXXXX)
dlfilter_so=$(mktemp /tmp/__perf_test.dlfilter.so.XXXXX)

cleanup() {
	rm -f "${perfdata}"
	rm -f "${dlfilter_c}"
	rm -f "${dlfilter_so}"
	rm -f "${dlfilter_so}.o"

	trap - EXIT TERM INT
}

trap_cleanup() {
	echo "Unexpected signal in ${FUNCNAME[1]}"
	cleanup
	exit 1
}
trap trap_cleanup EXIT TERM INT

cat <<EOF > "${dlfilter_c}"
#include <perf/perf_dlfilter.h>
#include <string.h>
#include <stdio.h>

struct perf_dlfilter_fns perf_dlfilter_fns;

int filter_event(void *data, const struct perf_dlfilter_sample *sample, void *ctx)
{
	const struct perf_dlfilter_al *al;

	if (!sample->ip)
		return 0;

	al = perf_dlfilter_fns.resolve_ip(ctx);
	if (!al || !al->sym || strcmp(al->sym, "test_loop"))
		return 1;

	return 0;
}
EOF

test_dlfilter() {
	echo "Basic --dlfilter test"
	# Generate perf.data file
	if ! perf record -o "${perfdata}" perf test -w thloop 1 2> /dev/null
	then
		echo "Basic --dlfilter test [Failed record]"
		err=1
		return
	fi

	# Build the dlfilter
	if ! cc -c -I tools/perf/include -fpic -x c "${dlfilter_c}" -o "${dlfilter_so}.o"
	then
		echo "Basic --dlfilter test [Failed to build dlfilter object]"
		err=1
		return
	fi

	if ! cc -shared -o "${dlfilter_so}" "${dlfilter_so}.o"
	then
		echo "Basic --dlfilter test [Failed to link dlfilter shared object]"
		err=1
		return
	fi

	# Check that the output contains "test_loop" and nothing else
	if ! perf script -i "${perfdata}" --dlfilter "${dlfilter_so}" | grep -q "test_loop"
	then
		echo "Basic --dlfilter test [Failed missing output]"
		err=1
		return
	fi

	# The filter should filter out everything except test_loop, so ensure no other symbols are present
	# This is a simple check; we could be more rigorous
	if perf script -i "${perfdata}" --dlfilter "${dlfilter_so}" | grep -v "test_loop" | grep -q "perf"
	then
		echo "Basic --dlfilter test [Failed filtering]"
		err=1
		return
	fi

	echo "Basic --dlfilter test [Success]"
}

test_dlfilter
cleanup
exit $err