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
46 cat << _end_of_file_ | /usr/bin/cc -o "${workload}" -xc - -pthread && have_workload=true
51 struct timespec tm = {
56 /* Run for about 30 seconds */
57 for (i = 0; i < 30000; i++)
61 void *threadfunc(void *arg) {
69 pthread_create(&th, NULL, threadfunc, NULL);
71 pthread_join(th, NULL);
78 echo "Checking for CPU-wide recording on CPU $1"
79 if ! perf record -o "${tmpfile}" -B -N --no-bpf-event -e dummy:u -C "$1" true >/dev/null 2>&1 ; then
87 test_system_wide_side_band()
89 echo "--- Test system-wide sideband ---"
91 # Need CPU 0 and CPU 1
92 can_cpu_wide 0 || return $?
93 can_cpu_wide 1 || return $?
95 # Record on CPU 0 a task running on CPU 1
96 perf record -B -N --no-bpf-event -o "${perfdatafile}" -e intel_pt//u -C 0 -- taskset --cpu-list 1 uname
98 # Should get MMAP events from CPU 1 because they can be needed to decode
99 mmap_cnt=$(perf script -i "${perfdatafile}" --no-itrace --show-mmap-events -C 1 2>/dev/null | grep -c MMAP)
101 if [ "${mmap_cnt}" -gt 0 ] ; then
106 echo "Failed to record MMAP events on CPU 1 when tracing CPU 0"
112 perf record -o "${tmpfile}" -B -N --no-bpf-event -e dummy:k true >/dev/null 2>&1 || return 2
121 echo "--- Test per-thread ${desc}recording ---"
123 if ! $have_workload ; then
124 echo "No workload, so skipping"
128 if [ "${k}" = "k" ] ; then
129 can_kernel || return 2
132 cat <<- "_end_of_file_" > "${awkscript}"
137 x = s"[0-9a-fA-FxX]+"s
138 mmapping = "idx"u": mmapping fd"u
139 set_output = "idx"u": set output fd"u"->"u
140 perf_event_open = "sys_perf_event_open: pid"d"cpu"d"group_fd"d"flags"x"="u
143 /perf record opening and mmapping events/ {
148 /perf record done opening and mmapping events/ {
153 $0 ~ perf_event_open && active {
154 match($0, perf_event_open)
155 $0 = substr($0, RSTART, RLENGTH)
159 print "pid " pid " cpu " cpu " fd " fd " : " $0
165 $0 ~ mmapping && active {
167 $0 = substr($0, RSTART, RLENGTH)
169 print "fd " fd " : " $0
170 if (fd in fd_array) {
173 print "Unknown fd " fd
178 $0 ~ set_output && active {
179 match($0, set_output)
180 $0 = substr($0, RSTART, RLENGTH)
183 print "fd " fd " fd_to " fd_to " : " $0
184 if (fd in fd_array) {
185 if (fd_to in fd_array) {
186 set_output_array[fd] = fd_to
188 print "Unknown fd " fd_to
192 print "Unknown fd " fd
198 print "Checking " length(fd_array) " fds"
199 for (fd in fd_array) {
200 if (fd in mmap_array) {
204 print "More than 1 mmap for PID " pid
212 print "More than 1 mmap for CPU " cpu
217 } else if (!(fd in set_output_array)) {
218 print "No mmap for fd " fd
223 if (n != thread_cnt) {
224 print "Expected " thread_cnt " per-thread mmaps - found " n
234 echo "Workload PIDs are $w1 and $w2"
235 wait_for_threads ${w1} 2
236 wait_for_threads ${w2} 2
238 perf record -B -N --no-bpf-event -o "${perfdatafile}" -e intel_pt//u"${k}" -vvv --per-thread -p "${w1},${w2}" 2>"${errfile}" >"${outfile}" &
240 echo "perf PID is $ppid"
241 wait_for_perf_to_start ${ppid} "${errfile}" || return 1
244 wait_for_process_to_exit ${w1} || return 1
245 is_running ${ppid} || return 1
248 wait_for_process_to_exit ${w2} || return 1
249 wait_for_process_to_exit ${ppid} || return 1
251 awk -v thread_cnt=4 -f "${awkscript}" "${errfile}" || return 1
259 if [ "$1" -eq 2 ] ; then
260 skip_cnt=$((skip_cnt + 1))
263 if [ "$1" -eq 0 ] ; then
264 ok_cnt=$((ok_cnt + 1))
267 err_cnt=$((err_cnt + 1))
271 test_system_wide_side_band || ret=$? ; count_result $ret ; ret=0
272 test_per_thread "" "" || ret=$? ; count_result $ret ; ret=0
273 test_per_thread "k" "(incl. kernel) " || ret=$? ; count_result $ret ; ret=0
279 if [ ${err_cnt} -gt 0 ] ; then
283 if [ ${ok_cnt} -gt 0 ] ; then