1#!/bin/bash
2# Test data symbol (exclusive)
3
4# SPDX-License-Identifier: GPL-2.0
5# Leo Yan <[email protected]>, 2022
6
7shelldir=$(dirname "$0")
8# shellcheck source=lib/waiting.sh
9. "${shelldir}"/lib/waiting.sh
10
11# shellcheck source=lib/perf_has_symbol.sh
12. "${shelldir}"/lib/perf_has_symbol.sh
13
14skip_if_no_mem_event() {
15	perf mem record -e list 2>&1 | grep -E -q 'available' && return 0
16	return 2
17}
18
19skip_if_no_mem_event || exit 2
20
21skip_test_missing_symbol workload_datasym_buf1
22
23TEST_PROGRAM="perf test -w datasym"
24PERF_DATA=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
25ERR_FILE=$(mktemp /tmp/__perf_test.stderr.XXXXX)
26
27check_result() {
28	# The memory report format is as below:
29	#    99.92%  ...  [.] workload_datasym_buf1+0x38
30	result=$(perf mem report -i ${PERF_DATA} -s symbol_daddr -q 2>&1 |
31		 awk '/workload_datasym_buf1/ { print $4 }')
32
33	# Testing is failed if has no any sample for "workload_datasym_buf1"
34	[ -z "$result" ] && return 1
35
36	while IFS= read -r line; do
37		# The "data1" and "data2" fields in structure
38		# "workload_datasym_buf1" have offset "0x0" and "0x38", returns
39		# failure if detect any other offset value.
40		if [ "$line" != "workload_datasym_buf1+0x0" ] && \
41		   [ "$line" != "workload_datasym_buf1+0x38" ]; then
42			return 1
43		fi
44	done <<< "$result"
45
46	return 0
47}
48
49cleanup_files()
50{
51	echo "Cleaning up files..."
52	rm -f ${PERF_DATA}
53}
54
55trap cleanup_files exit term int
56
57echo "Recording workload..."
58
59# perf mem/c2c internally uses IBS PMU on AMD CPU which doesn't support
60# user/kernel filtering and per-process monitoring, spin program on
61# specific CPU and test in per-CPU mode.
62is_amd=$(grep -E -c 'vendor_id.*AuthenticAMD' /proc/cpuinfo)
63if (($is_amd >= 1)); then
64	perf mem record -vvv -o ${PERF_DATA} -C 0 -- taskset -c 0 $TEST_PROGRAM 2>"${ERR_FILE}" &
65else
66	perf mem record -vvv --all-user -o ${PERF_DATA} -- $TEST_PROGRAM 2>"${ERR_FILE}" &
67fi
68
69PERFPID=$!
70
71wait_for_perf_to_start ${PERFPID} "${ERR_FILE}"
72
73sleep 1
74
75kill $PERFPID
76wait $PERFPID
77
78check_result
79exit $?
80