2 # Miscellaneous Intel PT testing
3 # SPDX-License-Identifier: GPL-2.0
8 perf list | grep -q 'intel_pt//' || exit 2
10 shelldir=$(dirname "$0")
11 . "${shelldir}"/lib/waiting.sh
17 temp_dir=$(mktemp -d /tmp/perf-test-intel-pt-sh.XXXXXXXXXX)
19 tmpfile="${temp_dir}/tmp-perf.data"
20 perfdatafile="${temp_dir}/test-perf.data"
21 outfile="${temp_dir}/test-out.txt"
22 errfile="${temp_dir}/test-err.txt"
23 workload="${temp_dir}/workload"
24 awkscript="${temp_dir}/awkscript"
29 sane=$(echo "${temp_dir}" | cut -b 1-26)
30 if [ "${sane}" = "/tmp/perf-test-intel-pt-sh" ] ; then
31 echo "--- Cleaning up ---"
43 trap trap_cleanup EXIT TERM INT
45 # perf record for testing without decoding
46 perf_record_no_decode()
48 # Options to speed up recording: no post-processing, no build-id cache update,
50 perf record -B -N --no-bpf-event "$@"
54 cat << _end_of_file_ | /usr/bin/cc -o "${workload}" -xc - -pthread && have_workload=true
59 struct timespec tm = {
64 /* Run for about 30 seconds */
65 for (i = 0; i < 30000; i++)
69 void *threadfunc(void *arg) {
77 pthread_create(&th, NULL, threadfunc, NULL);
79 pthread_join(th, NULL);
86 echo "Checking for CPU-wide recording on CPU $1"
87 if ! perf_record_no_decode -o "${tmpfile}" -e dummy:u -C "$1" true >/dev/null 2>&1 ; then
95 test_system_wide_side_band()
97 echo "--- Test system-wide sideband ---"
99 # Need CPU 0 and CPU 1
100 can_cpu_wide 0 || return $?
101 can_cpu_wide 1 || return $?
103 # Record on CPU 0 a task running on CPU 1
104 perf_record_no_decode -o "${perfdatafile}" -e intel_pt//u -C 0 -- taskset --cpu-list 1 uname
106 # Should get MMAP events from CPU 1 because they can be needed to decode
107 mmap_cnt=$(perf script -i "${perfdatafile}" --no-itrace --show-mmap-events -C 1 2>/dev/null | grep -c MMAP)
109 if [ "${mmap_cnt}" -gt 0 ] ; then
114 echo "Failed to record MMAP events on CPU 1 when tracing CPU 0"
120 perf_record_no_decode -o "${tmpfile}" -e dummy:k true >/dev/null 2>&1 || return 2
129 echo "--- Test per-thread ${desc}recording ---"
131 if ! $have_workload ; then
132 echo "No workload, so skipping"
136 if [ "${k}" = "k" ] ; then
137 can_kernel || return 2
140 cat <<- "_end_of_file_" > "${awkscript}"
145 x = s"[0-9a-fA-FxX]+"s
146 mmapping = "idx"u": mmapping fd"u
147 set_output = "idx"u": set output fd"u"->"u
148 perf_event_open = "sys_perf_event_open: pid"d"cpu"d"group_fd"d"flags"x"="u
151 /perf record opening and mmapping events/ {
156 /perf record done opening and mmapping events/ {
161 $0 ~ perf_event_open && active {
162 match($0, perf_event_open)
163 $0 = substr($0, RSTART, RLENGTH)
167 print "pid " pid " cpu " cpu " fd " fd " : " $0
173 $0 ~ mmapping && active {
175 $0 = substr($0, RSTART, RLENGTH)
177 print "fd " fd " : " $0
178 if (fd in fd_array) {
181 print "Unknown fd " fd
186 $0 ~ set_output && active {
187 match($0, set_output)
188 $0 = substr($0, RSTART, RLENGTH)
191 print "fd " fd " fd_to " fd_to " : " $0
192 if (fd in fd_array) {
193 if (fd_to in fd_array) {
194 set_output_array[fd] = fd_to
196 print "Unknown fd " fd_to
200 print "Unknown fd " fd
206 print "Checking " length(fd_array) " fds"
207 for (fd in fd_array) {
208 if (fd in mmap_array) {
212 print "More than 1 mmap for PID " pid
220 print "More than 1 mmap for CPU " cpu
225 } else if (!(fd in set_output_array)) {
226 print "No mmap for fd " fd
231 if (n != thread_cnt) {
232 print "Expected " thread_cnt " per-thread mmaps - found " n
242 echo "Workload PIDs are $w1 and $w2"
243 wait_for_threads ${w1} 2
244 wait_for_threads ${w2} 2
246 perf_record_no_decode -o "${perfdatafile}" -e intel_pt//u"${k}" -vvv --per-thread -p "${w1},${w2}" 2>"${errfile}" >"${outfile}" &
248 echo "perf PID is $ppid"
249 wait_for_perf_to_start ${ppid} "${errfile}" || return 1
252 wait_for_process_to_exit ${w1} || return 1
253 is_running ${ppid} || return 1
256 wait_for_process_to_exit ${w2} || return 1
257 wait_for_process_to_exit ${ppid} || return 1
259 awk -v thread_cnt=4 -f "${awkscript}" "${errfile}" || return 1
267 if [ "$1" -eq 2 ] ; then
268 skip_cnt=$((skip_cnt + 1))
271 if [ "$1" -eq 0 ] ; then
272 ok_cnt=$((ok_cnt + 1))
275 err_cnt=$((err_cnt + 1))
279 test_system_wide_side_band || ret=$? ; count_result $ret ; ret=0
280 test_per_thread "" "" || ret=$? ; count_result $ret ; ret=0
281 test_per_thread "k" "(incl. kernel) " || ret=$? ; count_result $ret ; ret=0
287 if [ ${err_cnt} -gt 0 ] ; then
291 if [ ${ok_cnt} -gt 0 ] ; then