Linux Audio

Check our new training course

Loading...
v6.8
  1#!/bin/sh
  2# Miscellaneous Intel PT testing
  3# SPDX-License-Identifier: GPL-2.0
  4
  5set -e
  6
  7# Skip if no Intel PT
  8perf list | grep -q 'intel_pt//' || exit 2
  9
 10shelldir=$(dirname "$0")
 11# shellcheck source=lib/waiting.sh
 12. "${shelldir}"/lib/waiting.sh
 13
 14skip_cnt=0
 15ok_cnt=0
 16err_cnt=0
 17
 18temp_dir=$(mktemp -d /tmp/perf-test-intel-pt-sh.XXXXXXXXXX)
 19
 20tmpfile="${temp_dir}/tmp-perf.data"
 21perfdatafile="${temp_dir}/test-perf.data"
 22outfile="${temp_dir}/test-out.txt"
 23errfile="${temp_dir}/test-err.txt"
 24workload="${temp_dir}/workload"
 25awkscript="${temp_dir}/awkscript"
 26jitdump_workload="${temp_dir}/jitdump_workload"
 27maxbrstack="${temp_dir}/maxbrstack.py"
 28
 29cleanup()
 30{
 31	trap - EXIT TERM INT
 32	sane=$(echo "${temp_dir}" | cut -b 1-26)
 33	if [ "${sane}" = "/tmp/perf-test-intel-pt-sh" ] ; then
 34		echo "--- Cleaning up ---"
 35		rm -f "${temp_dir}/"*
 36		rmdir "${temp_dir}"
 37	fi
 38}
 39
 40trap_cleanup()
 41{
 42	cleanup
 43	exit 1
 44}
 45
 46trap trap_cleanup EXIT TERM INT
 47
 48# perf record for testing without decoding
 49perf_record_no_decode()
 50{
 51	# Options to speed up recording: no post-processing, no build-id cache update,
 52	# and no BPF events.
 53	perf record -B -N --no-bpf-event "$@"
 54}
 55
 56# perf record for testing should not need BPF events
 57perf_record_no_bpf()
 58{
 59	# Options for no BPF events
 60	perf record --no-bpf-event "$@"
 61}
 62
 63have_workload=false
 64cat << _end_of_file_ | /usr/bin/cc -o "${workload}" -xc - -pthread && have_workload=true
 65#include <time.h>
 66#include <pthread.h>
 67
 68void work(void) {
 69	struct timespec tm = {
 70		.tv_nsec = 1000000,
 71	};
 72	int i;
 73
 74	/* Run for about 30 seconds */
 75	for (i = 0; i < 30000; i++)
 76		nanosleep(&tm, NULL);
 77}
 78
 79void *threadfunc(void *arg) {
 80	work();
 81	return NULL;
 82}
 83
 84int main(void) {
 85	pthread_t th;
 86
 87	pthread_create(&th, NULL, threadfunc, NULL);
 88	work();
 89	pthread_join(th, NULL);
 90	return 0;
 91}
 92_end_of_file_
 93
 94can_cpu_wide()
 95{
 96	echo "Checking for CPU-wide recording on CPU $1"
 97	if ! perf_record_no_decode -o "${tmpfile}" -e dummy:u -C "$1" true >/dev/null 2>&1 ; then
 98		echo "No so skipping"
 99		return 2
100	fi
101	echo OK
102	return 0
103}
104
105test_system_wide_side_band()
106{
107	echo "--- Test system-wide sideband ---"
108
109	# Need CPU 0 and CPU 1
110	can_cpu_wide 0 || return $?
111	can_cpu_wide 1 || return $?
112
113	# Record on CPU 0 a task running on CPU 1
114	perf_record_no_decode -o "${perfdatafile}" -e intel_pt//u -C 0 -- taskset --cpu-list 1 uname
115
116	# Should get MMAP events from CPU 1 because they can be needed to decode
117	mmap_cnt=$(perf script -i "${perfdatafile}" --no-itrace --show-mmap-events -C 1 2>/dev/null | grep -c MMAP)
118
119	if [ "${mmap_cnt}" -gt 0 ] ; then
120		echo OK
121		return 0
122	fi
123
124	echo "Failed to record MMAP events on CPU 1 when tracing CPU 0"
125	return 1
126}
127
128can_kernel()
129{
130	if [ -z "${can_kernel_trace}" ] ; then
131		can_kernel_trace=0
132		perf_record_no_decode -o "${tmpfile}" -e dummy:k true >/dev/null 2>&1 && can_kernel_trace=1
133	fi
134	if [ ${can_kernel_trace} -eq 0 ] ; then
135		echo "SKIP: no kernel tracing"
136		return 2
137	fi
138	return 0
139}
140
141test_per_thread()
142{
143	k="$1"
144	desc="$2"
145
146	echo "--- Test per-thread ${desc}recording ---"
147
148	if ! $have_workload ; then
149		echo "No workload, so skipping"
150		return 2
151	fi
152
153	if [ "${k}" = "k" ] ; then
154		can_kernel || return 2
155	fi
156
157	cat <<- "_end_of_file_" > "${awkscript}"
158	BEGIN {
159		s = "[ ]*"
160		u = s"[0-9]+"s
161		d = s"[0-9-]+"s
162		x = s"[0-9a-fA-FxX]+"s
163		mmapping = "idx"u": mmapping fd"u
164		set_output = "idx"u": set output fd"u"->"u
165		perf_event_open = "sys_perf_event_open: pid"d"cpu"d"group_fd"d"flags"x"="u
166	}
167
168	/perf record opening and mmapping events/ {
169		if (!done)
170			active = 1
171	}
172
173	/perf record done opening and mmapping events/ {
174		active = 0
175		done = 1
176	}
177
178	$0 ~ perf_event_open && active {
179		match($0, perf_event_open)
180		$0 = substr($0, RSTART, RLENGTH)
181		pid = $3
182		cpu = $5
183		fd = $11
184		print "pid " pid " cpu " cpu " fd " fd " : " $0
185		fd_array[fd] = fd
186		pid_array[fd] = pid
187		cpu_array[fd] = cpu
188	}
189
190	$0 ~ mmapping && active  {
191		match($0, mmapping)
192		$0 = substr($0, RSTART, RLENGTH)
193		fd = $5
194		print "fd " fd " : " $0
195		if (fd in fd_array) {
196			mmap_array[fd] = 1
197		} else {
198			print "Unknown fd " fd
199			exit 1
200		}
201	}
202
203	$0 ~ set_output && active {
204		match($0, set_output)
205		$0 = substr($0, RSTART, RLENGTH)
206		fd = $6
207		fd_to = $8
208		print "fd " fd " fd_to " fd_to " : " $0
209		if (fd in fd_array) {
210			if (fd_to in fd_array) {
211				set_output_array[fd] = fd_to
212			} else {
213				print "Unknown fd " fd_to
214				exit 1
215			}
216		} else {
217			print "Unknown fd " fd
218			exit 1
219		}
220	}
221
222	END {
223		print "Checking " length(fd_array) " fds"
224		for (fd in fd_array) {
225			if (fd in mmap_array) {
226				pid = pid_array[fd]
227				if (pid != -1) {
228					if (pid in pids) {
229						print "More than 1 mmap for PID " pid
230						exit 1
231					}
232					pids[pid] = 1
233				}
234				cpu = cpu_array[fd]
235				if (cpu != -1) {
236					if (cpu in cpus) {
237						print "More than 1 mmap for CPU " cpu
238						exit 1
239					}
240					cpus[cpu] = 1
241				}
242			} else if (!(fd in set_output_array)) {
243				print "No mmap for fd " fd
244				exit 1
245			}
246		}
247		n = length(pids)
248		if (n != thread_cnt) {
249			print "Expected " thread_cnt " per-thread mmaps - found " n
250			exit 1
251		}
252	}
253	_end_of_file_
254
255	$workload &
256	w1=$!
257	$workload &
258	w2=$!
259	echo "Workload PIDs are $w1 and $w2"
260	wait_for_threads ${w1} 2
261	wait_for_threads ${w2} 2
262
263	perf_record_no_decode -o "${perfdatafile}" -e intel_pt//u"${k}" -vvv --per-thread -p "${w1},${w2}" 2>"${errfile}" >"${outfile}" &
264	ppid=$!
265	echo "perf PID is $ppid"
266	wait_for_perf_to_start ${ppid} "${errfile}" || return 1
267
268	kill ${w1}
269	wait_for_process_to_exit ${w1} || return 1
270	is_running ${ppid} || return 1
271
272	kill ${w2}
273	wait_for_process_to_exit ${w2} || return 1
274	wait_for_process_to_exit ${ppid} || return 1
275
276	awk -v thread_cnt=4 -f "${awkscript}" "${errfile}" || return 1
277
278	echo OK
279	return 0
280}
281
282test_jitdump()
283{
284	echo "--- Test tracing self-modifying code that uses jitdump ---"
285
286	script_path=$(realpath "$0")
287	script_dir=$(dirname "$script_path")
288	jitdump_incl_dir="${script_dir}/../../util"
289	jitdump_h="${jitdump_incl_dir}/jitdump.h"
290
291	if [ ! -e "${jitdump_h}" ] ; then
292		echo "SKIP: Include file jitdump.h not found"
293		return 2
294	fi
295
296	if [ -z "${have_jitdump_workload}" ] ; then
297		have_jitdump_workload=false
298		# Create a workload that uses self-modifying code and generates its own jitdump file
299		cat <<- "_end_of_file_" | /usr/bin/cc -o "${jitdump_workload}" -I "${jitdump_incl_dir}" -xc - -pthread && have_jitdump_workload=true
300		#define _GNU_SOURCE
301		#include <sys/mman.h>
302		#include <sys/types.h>
303		#include <stddef.h>
304		#include <stdio.h>
305		#include <stdint.h>
306		#include <unistd.h>
307		#include <string.h>
308
309		#include "jitdump.h"
310
311		#define CHK_BYTE 0x5a
312
313		static inline uint64_t rdtsc(void)
314		{
315			unsigned int low, high;
316
317			asm volatile("rdtsc" : "=a" (low), "=d" (high));
318
319			return low | ((uint64_t)high) << 32;
320		}
321
322		static FILE *open_jitdump(void)
323		{
324			struct jitheader header = {
325				.magic      = JITHEADER_MAGIC,
326				.version    = JITHEADER_VERSION,
327				.total_size = sizeof(header),
328				.pid        = getpid(),
329				.timestamp  = rdtsc(),
330				.flags      = JITDUMP_FLAGS_ARCH_TIMESTAMP,
331			};
332			char filename[256];
333			FILE *f;
334			void *m;
335
336			snprintf(filename, sizeof(filename), "jit-%d.dump", getpid());
337			f = fopen(filename, "w+");
338			if (!f)
339				goto err;
340			/* Create an MMAP event for the jitdump file. That is how perf tool finds it. */
341			m = mmap(0, 4096, PROT_READ | PROT_EXEC, MAP_PRIVATE, fileno(f), 0);
342			if (m == MAP_FAILED)
343				goto err_close;
344			munmap(m, 4096);
345			if (fwrite(&header,sizeof(header),1,f) != 1)
346				goto err_close;
347			return f;
348
349		err_close:
350			fclose(f);
351		err:
352			return NULL;
353		}
354
355		static int write_jitdump(FILE *f, void *addr, const uint8_t *dat, size_t sz, uint64_t *idx)
356		{
357			struct jr_code_load rec = {
358				.p.id          = JIT_CODE_LOAD,
359				.p.total_size  = sizeof(rec) + sz,
360				.p.timestamp   = rdtsc(),
361				.pid	       = getpid(),
362				.tid	       = gettid(),
363				.vma           = (unsigned long)addr,
364				.code_addr     = (unsigned long)addr,
365				.code_size     = sz,
366				.code_index    = ++*idx,
367			};
368
369			if (fwrite(&rec,sizeof(rec),1,f) != 1 ||
370			fwrite(dat, sz, 1, f) != 1)
371				return -1;
372			return 0;
373		}
374
375		static void close_jitdump(FILE *f)
376		{
377			fclose(f);
378		}
379
380		int main()
381		{
382			/* Get a memory page to store executable code */
383			void *addr = mmap(0, 4096, PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
384			/* Code to execute: mov CHK_BYTE, %eax ; ret */
385			uint8_t dat[] = {0xb8, CHK_BYTE, 0x00, 0x00, 0x00, 0xc3};
386			FILE *f = open_jitdump();
387			uint64_t idx = 0;
388			int ret = 1;
389
390			if (!f)
391				return 1;
392			/* Copy executable code to executable memory page */
393			memcpy(addr, dat, sizeof(dat));
394			/* Record it in the jitdump file */
395			if (write_jitdump(f, addr, dat, sizeof(dat), &idx))
396				goto out_close;
397			/* Call it */
398			ret = ((int (*)(void))addr)() - CHK_BYTE;
399		out_close:
400			close_jitdump(f);
401			return ret;
402		}
403		_end_of_file_
404	fi
405
406	if ! $have_jitdump_workload ; then
407		echo "SKIP: No jitdump workload"
408		return 2
409	fi
410
411	# Change to temp_dir so jitdump collateral files go there
412	cd "${temp_dir}"
413	perf_record_no_bpf -o "${tmpfile}" -e intel_pt//u "${jitdump_workload}"
414	perf inject -i "${tmpfile}" -o "${perfdatafile}" --jit
415	decode_br_cnt=$(perf script -i "${perfdatafile}" --itrace=b | wc -l)
416	# Note that overflow and lost errors are suppressed for the error count
417	decode_err_cnt=$(perf script -i "${perfdatafile}" --itrace=e-o-l | grep -ci error)
418	cd -
419	# Should be thousands of branches
420	if [ "${decode_br_cnt}" -lt 1000 ] ; then
421		echo "Decode failed, only ${decode_br_cnt} branches"
422		return 1
423	fi
424	# Should be no errors
425	if [ "${decode_err_cnt}" -ne 0 ] ; then
426		echo "Decode failed, ${decode_err_cnt} errors"
427		perf script -i "${perfdatafile}" --itrace=e-o-l --show-mmap-events | cat
428		return 1
429	fi
430
431	echo OK
432	return 0
433}
434
435test_packet_filter()
436{
437	echo "--- Test with MTC and TSC disabled ---"
438	# Disable MTC and TSC
439	perf_record_no_decode -o "${perfdatafile}" -e intel_pt/mtc=0,tsc=0/u uname
440	# Should not get MTC packet
441	mtc_cnt=$(perf script -i "${perfdatafile}" -D 2>/dev/null | grep -c "MTC 0x")
442	if [ "${mtc_cnt}" -ne 0 ] ; then
443		echo "Failed to filter with mtc=0"
444		return 1
445	fi
446	# Should not get TSC package
447	tsc_cnt=$(perf script -i "${perfdatafile}" -D 2>/dev/null | grep -c "TSC 0x")
448	if [ "${tsc_cnt}" -ne 0 ] ; then
449		echo "Failed to filter with tsc=0"
450		return 1
451	fi
452	echo OK
453	return 0
454}
455
456test_disable_branch()
457{
458	echo "--- Test with branches disabled ---"
459	# Disable branch
460	perf_record_no_decode -o "${perfdatafile}" -e intel_pt/branch=0/u uname
461	# Should not get branch related packets
462	tnt_cnt=$(perf script -i "${perfdatafile}" -D 2>/dev/null | grep -c "TNT 0x")
463	tip_cnt=$(perf script -i "${perfdatafile}" -D 2>/dev/null | grep -c "TIP 0x")
464	fup_cnt=$(perf script -i "${perfdatafile}" -D 2>/dev/null | grep -c "FUP 0x")
465	if [ "${tnt_cnt}" -ne 0 ] || [ "${tip_cnt}" -ne 0 ] || [ "${fup_cnt}" -ne 0 ] ; then
466		echo "Failed to disable branches"
467		return 1
468	fi
469	echo OK
470	return 0
471}
472
473test_time_cyc()
474{
475	echo "--- Test with/without CYC ---"
476	# Check if CYC is supported
477	cyc=$(cat /sys/bus/event_source/devices/intel_pt/caps/psb_cyc)
478	if [ "${cyc}" != "1" ] ; then
479		echo "SKIP: CYC is not supported"
480		return 2
481	fi
482	# Enable CYC
483	perf_record_no_decode -o "${perfdatafile}" -e intel_pt/cyc/u uname
484	# should get CYC packets
485	cyc_cnt=$(perf script -i "${perfdatafile}" -D 2>/dev/null | grep -c "CYC 0x")
486	if [ "${cyc_cnt}" = "0" ] ; then
487		echo "Failed to get CYC packet"
488		return 1
489	fi
490	# Without CYC
491	perf_record_no_decode -o "${perfdatafile}" -e intel_pt//u uname
492	# Should not get CYC packets
493	cyc_cnt=$(perf script -i "${perfdatafile}" -D 2>/dev/null | grep -c "CYC 0x")
494	if [ "${cyc_cnt}" -gt 0 ] ; then
495		echo "Still get CYC packet without cyc"
496		return 1
497	fi
498	echo OK
499	return 0
500}
501
502test_sample()
503{
504	echo "--- Test recording with sample mode ---"
505	# Check if recording with sample mode is working
506	if ! perf_record_no_decode -o "${perfdatafile}" --aux-sample=8192 -e '{intel_pt//u,branch-misses:u}' uname ; then
507		echo "perf record failed with --aux-sample"
508		return 1
509	fi
510	# Check with event with PMU name
511	if perf_record_no_decode -o "${perfdatafile}" -e br_misp_retired.all_branches:u uname ; then
512		if ! perf_record_no_decode -o "${perfdatafile}" -e '{intel_pt//,br_misp_retired.all_branches/aux-sample-size=8192/}:u' uname ; then
513			echo "perf record failed with --aux-sample-size"
514			return 1
515		fi
516	fi
517	echo OK
518	return 0
519}
520
521test_kernel_trace()
522{
523	echo "--- Test with kernel trace ---"
524	# Check if recording with kernel trace is working
525	can_kernel || return 2
526	if ! perf_record_no_decode -o "${perfdatafile}" -e intel_pt//k -m1,128 uname ; then
527		echo "perf record failed with intel_pt//k"
528		return 1
529	fi
530	echo OK
531	return 0
532}
533
534test_virtual_lbr()
535{
536	echo "--- Test virtual LBR ---"
537	# Check if python script is supported
538	libpython=$(perf version --build-options | grep python | grep -cv OFF)
539	if [ "${libpython}" != "1" ] ; then
540		echo "SKIP: python scripting is not supported"
541		return 2
542	fi
543
544	# Python script to determine the maximum size of branch stacks
545	cat << "_end_of_file_" > "${maxbrstack}"
546from __future__ import print_function
547
548bmax = 0
549
550def process_event(param_dict):
551	if "brstack" in param_dict:
552		brstack = param_dict["brstack"]
553		n = len(brstack)
554		global bmax
555		if n > bmax:
556			bmax = n
557
558def trace_end():
559	print("max brstack", bmax)
560_end_of_file_
561
562	# Check if virtual lbr is working
563	perf_record_no_bpf -o "${perfdatafile}" --aux-sample -e '{intel_pt//,cycles}:u' uname
564	times_val=$(perf script -i "${perfdatafile}" --itrace=L -s "${maxbrstack}" 2>/dev/null | grep "max brstack " | cut -d " " -f 3)
565	case "${times_val}" in
566		[0-9]*)	;;
567		*)	times_val=0;;
568	esac
569	if [ "${times_val}" -lt 2 ] ; then
570		echo "Failed with virtual lbr"
571		return 1
572	fi
573	echo OK
574	return 0
575}
576
577test_power_event()
578{
579	echo "--- Test power events ---"
580	# Check if power events are supported
581	power_event=$(cat /sys/bus/event_source/devices/intel_pt/caps/power_event_trace)
582	if [ "${power_event}" != "1" ] ; then
583		echo "SKIP: power_event_trace is not supported"
584		return 2
585	fi
586	if ! perf_record_no_decode -o "${perfdatafile}" -a -e intel_pt/pwr_evt/u uname ; then
587		echo "perf record failed with pwr_evt"
588		return 1
589	fi
590	echo OK
591	return 0
592}
593
594test_no_tnt()
595{
596	echo "--- Test with TNT packets disabled  ---"
597	# Check if TNT disable is supported
598	notnt=$(cat /sys/bus/event_source/devices/intel_pt/caps/tnt_disable)
599	if [ "${notnt}" != "1" ] ; then
600		echo "SKIP: tnt_disable is not supported"
601		return 2
602	fi
603	perf_record_no_decode -o "${perfdatafile}" -e intel_pt/notnt/u uname
604	# Should be no TNT packets
605	tnt_cnt=$(perf script -i "${perfdatafile}" -D | grep -c TNT)
606	if [ "${tnt_cnt}" -ne 0 ] ; then
607		echo "TNT packets still there after notnt"
608		return 1
609	fi
610	echo OK
611	return 0
612}
613
614test_event_trace()
615{
616	echo "--- Test with event_trace ---"
617	# Check if event_trace is supported
618	event_trace=$(cat /sys/bus/event_source/devices/intel_pt/caps/event_trace)
619	if [ "${event_trace}" != 1 ] ; then
620		echo "SKIP: event_trace is not supported"
621		return 2
622	fi
623	if ! perf_record_no_decode -o "${perfdatafile}" -e intel_pt/event/u uname ; then
624		echo "perf record failed with event trace"
625		return 1
626	fi
627	echo OK
628	return 0
629}
630
631test_pipe()
632{
633	echo "--- Test with pipe mode ---"
634	# Check if it works with pipe
635	if ! perf_record_no_bpf -o- -e intel_pt//u uname | perf report -q -i- --itrace=i10000 ; then
636		echo "perf record + report failed with pipe mode"
637		return 1
638	fi
639	if ! perf_record_no_bpf -o- -e intel_pt//u uname | perf inject -b > /dev/null ; then
640		echo "perf record + inject failed with pipe mode"
641		return 1
642	fi
643	echo OK
644	return 0
645}
646
647count_result()
648{
649	if [ "$1" -eq 2 ] ; then
650		skip_cnt=$((skip_cnt + 1))
651		return
652	fi
653	if [ "$1" -eq 0 ] ; then
654		ok_cnt=$((ok_cnt + 1))
655		return
656	fi
657	err_cnt=$((err_cnt + 1))
658}
659
660ret=0
661test_system_wide_side_band		|| ret=$? ; count_result $ret ; ret=0
662test_per_thread "" ""			|| ret=$? ; count_result $ret ; ret=0
663test_per_thread "k" "(incl. kernel) "	|| ret=$? ; count_result $ret ; ret=0
664test_jitdump				|| ret=$? ; count_result $ret ; ret=0
665test_packet_filter			|| ret=$? ; count_result $ret ; ret=0
666test_disable_branch			|| ret=$? ; count_result $ret ; ret=0
667test_time_cyc				|| ret=$? ; count_result $ret ; ret=0
668test_sample				|| ret=$? ; count_result $ret ; ret=0
669test_kernel_trace			|| ret=$? ; count_result $ret ; ret=0
670test_virtual_lbr			|| ret=$? ; count_result $ret ; ret=0
671test_power_event			|| ret=$? ; count_result $ret ; ret=0
672test_no_tnt				|| ret=$? ; count_result $ret ; ret=0
673test_event_trace			|| ret=$? ; count_result $ret ; ret=0
674test_pipe				|| ret=$? ; count_result $ret ; ret=0
675
676cleanup
677
678echo "--- Done ---"
679
680if [ ${err_cnt} -gt 0 ] ; then
681	exit 1
682fi
683
684if [ ${ok_cnt} -gt 0 ] ; then
685	exit 0
686fi
687
688exit 2
v6.2
  1#!/bin/sh
  2# Miscellaneous Intel PT testing
  3# SPDX-License-Identifier: GPL-2.0
  4
  5set -e
  6
  7# Skip if no Intel PT
  8perf list | grep -q 'intel_pt//' || exit 2
  9
 10shelldir=$(dirname "$0")
 
 11. "${shelldir}"/lib/waiting.sh
 12
 13skip_cnt=0
 14ok_cnt=0
 15err_cnt=0
 16
 17temp_dir=$(mktemp -d /tmp/perf-test-intel-pt-sh.XXXXXXXXXX)
 18
 19tmpfile="${temp_dir}/tmp-perf.data"
 20perfdatafile="${temp_dir}/test-perf.data"
 21outfile="${temp_dir}/test-out.txt"
 22errfile="${temp_dir}/test-err.txt"
 23workload="${temp_dir}/workload"
 24awkscript="${temp_dir}/awkscript"
 25jitdump_workload="${temp_dir}/jitdump_workload"
 26maxbrstack="${temp_dir}/maxbrstack.py"
 27
 28cleanup()
 29{
 30	trap - EXIT TERM INT
 31	sane=$(echo "${temp_dir}" | cut -b 1-26)
 32	if [ "${sane}" = "/tmp/perf-test-intel-pt-sh" ] ; then
 33		echo "--- Cleaning up ---"
 34		rm -f "${temp_dir}/"*
 35		rmdir "${temp_dir}"
 36	fi
 37}
 38
 39trap_cleanup()
 40{
 41	cleanup
 42	exit 1
 43}
 44
 45trap trap_cleanup EXIT TERM INT
 46
 47# perf record for testing without decoding
 48perf_record_no_decode()
 49{
 50	# Options to speed up recording: no post-processing, no build-id cache update,
 51	# and no BPF events.
 52	perf record -B -N --no-bpf-event "$@"
 53}
 54
 55# perf record for testing should not need BPF events
 56perf_record_no_bpf()
 57{
 58	# Options for no BPF events
 59	perf record --no-bpf-event "$@"
 60}
 61
 62have_workload=false
 63cat << _end_of_file_ | /usr/bin/cc -o "${workload}" -xc - -pthread && have_workload=true
 64#include <time.h>
 65#include <pthread.h>
 66
 67void work(void) {
 68	struct timespec tm = {
 69		.tv_nsec = 1000000,
 70	};
 71	int i;
 72
 73	/* Run for about 30 seconds */
 74	for (i = 0; i < 30000; i++)
 75		nanosleep(&tm, NULL);
 76}
 77
 78void *threadfunc(void *arg) {
 79	work();
 80	return NULL;
 81}
 82
 83int main(void) {
 84	pthread_t th;
 85
 86	pthread_create(&th, NULL, threadfunc, NULL);
 87	work();
 88	pthread_join(th, NULL);
 89	return 0;
 90}
 91_end_of_file_
 92
 93can_cpu_wide()
 94{
 95	echo "Checking for CPU-wide recording on CPU $1"
 96	if ! perf_record_no_decode -o "${tmpfile}" -e dummy:u -C "$1" true >/dev/null 2>&1 ; then
 97		echo "No so skipping"
 98		return 2
 99	fi
100	echo OK
101	return 0
102}
103
104test_system_wide_side_band()
105{
106	echo "--- Test system-wide sideband ---"
107
108	# Need CPU 0 and CPU 1
109	can_cpu_wide 0 || return $?
110	can_cpu_wide 1 || return $?
111
112	# Record on CPU 0 a task running on CPU 1
113	perf_record_no_decode -o "${perfdatafile}" -e intel_pt//u -C 0 -- taskset --cpu-list 1 uname
114
115	# Should get MMAP events from CPU 1 because they can be needed to decode
116	mmap_cnt=$(perf script -i "${perfdatafile}" --no-itrace --show-mmap-events -C 1 2>/dev/null | grep -c MMAP)
117
118	if [ "${mmap_cnt}" -gt 0 ] ; then
119		echo OK
120		return 0
121	fi
122
123	echo "Failed to record MMAP events on CPU 1 when tracing CPU 0"
124	return 1
125}
126
127can_kernel()
128{
129	if [ -z "${can_kernel_trace}" ] ; then
130		can_kernel_trace=0
131		perf_record_no_decode -o "${tmpfile}" -e dummy:k true >/dev/null 2>&1 && can_kernel_trace=1
132	fi
133	if [ ${can_kernel_trace} -eq 0 ] ; then
134		echo "SKIP: no kernel tracing"
135		return 2
136	fi
137	return 0
138}
139
140test_per_thread()
141{
142	k="$1"
143	desc="$2"
144
145	echo "--- Test per-thread ${desc}recording ---"
146
147	if ! $have_workload ; then
148		echo "No workload, so skipping"
149		return 2
150	fi
151
152	if [ "${k}" = "k" ] ; then
153		can_kernel || return 2
154	fi
155
156	cat <<- "_end_of_file_" > "${awkscript}"
157	BEGIN {
158		s = "[ ]*"
159		u = s"[0-9]+"s
160		d = s"[0-9-]+"s
161		x = s"[0-9a-fA-FxX]+"s
162		mmapping = "idx"u": mmapping fd"u
163		set_output = "idx"u": set output fd"u"->"u
164		perf_event_open = "sys_perf_event_open: pid"d"cpu"d"group_fd"d"flags"x"="u
165	}
166
167	/perf record opening and mmapping events/ {
168		if (!done)
169			active = 1
170	}
171
172	/perf record done opening and mmapping events/ {
173		active = 0
174		done = 1
175	}
176
177	$0 ~ perf_event_open && active {
178		match($0, perf_event_open)
179		$0 = substr($0, RSTART, RLENGTH)
180		pid = $3
181		cpu = $5
182		fd = $11
183		print "pid " pid " cpu " cpu " fd " fd " : " $0
184		fd_array[fd] = fd
185		pid_array[fd] = pid
186		cpu_array[fd] = cpu
187	}
188
189	$0 ~ mmapping && active  {
190		match($0, mmapping)
191		$0 = substr($0, RSTART, RLENGTH)
192		fd = $5
193		print "fd " fd " : " $0
194		if (fd in fd_array) {
195			mmap_array[fd] = 1
196		} else {
197			print "Unknown fd " fd
198			exit 1
199		}
200	}
201
202	$0 ~ set_output && active {
203		match($0, set_output)
204		$0 = substr($0, RSTART, RLENGTH)
205		fd = $6
206		fd_to = $8
207		print "fd " fd " fd_to " fd_to " : " $0
208		if (fd in fd_array) {
209			if (fd_to in fd_array) {
210				set_output_array[fd] = fd_to
211			} else {
212				print "Unknown fd " fd_to
213				exit 1
214			}
215		} else {
216			print "Unknown fd " fd
217			exit 1
218		}
219	}
220
221	END {
222		print "Checking " length(fd_array) " fds"
223		for (fd in fd_array) {
224			if (fd in mmap_array) {
225				pid = pid_array[fd]
226				if (pid != -1) {
227					if (pid in pids) {
228						print "More than 1 mmap for PID " pid
229						exit 1
230					}
231					pids[pid] = 1
232				}
233				cpu = cpu_array[fd]
234				if (cpu != -1) {
235					if (cpu in cpus) {
236						print "More than 1 mmap for CPU " cpu
237						exit 1
238					}
239					cpus[cpu] = 1
240				}
241			} else if (!(fd in set_output_array)) {
242				print "No mmap for fd " fd
243				exit 1
244			}
245		}
246		n = length(pids)
247		if (n != thread_cnt) {
248			print "Expected " thread_cnt " per-thread mmaps - found " n
249			exit 1
250		}
251	}
252	_end_of_file_
253
254	$workload &
255	w1=$!
256	$workload &
257	w2=$!
258	echo "Workload PIDs are $w1 and $w2"
259	wait_for_threads ${w1} 2
260	wait_for_threads ${w2} 2
261
262	perf_record_no_decode -o "${perfdatafile}" -e intel_pt//u"${k}" -vvv --per-thread -p "${w1},${w2}" 2>"${errfile}" >"${outfile}" &
263	ppid=$!
264	echo "perf PID is $ppid"
265	wait_for_perf_to_start ${ppid} "${errfile}" || return 1
266
267	kill ${w1}
268	wait_for_process_to_exit ${w1} || return 1
269	is_running ${ppid} || return 1
270
271	kill ${w2}
272	wait_for_process_to_exit ${w2} || return 1
273	wait_for_process_to_exit ${ppid} || return 1
274
275	awk -v thread_cnt=4 -f "${awkscript}" "${errfile}" || return 1
276
277	echo OK
278	return 0
279}
280
281test_jitdump()
282{
283	echo "--- Test tracing self-modifying code that uses jitdump ---"
284
285	script_path=$(realpath "$0")
286	script_dir=$(dirname "$script_path")
287	jitdump_incl_dir="${script_dir}/../../util"
288	jitdump_h="${jitdump_incl_dir}/jitdump.h"
289
290	if [ ! -e "${jitdump_h}" ] ; then
291		echo "SKIP: Include file jitdump.h not found"
292		return 2
293	fi
294
295	if [ -z "${have_jitdump_workload}" ] ; then
296		have_jitdump_workload=false
297		# Create a workload that uses self-modifying code and generates its own jitdump file
298		cat <<- "_end_of_file_" | /usr/bin/cc -o "${jitdump_workload}" -I "${jitdump_incl_dir}" -xc - -pthread && have_jitdump_workload=true
299		#define _GNU_SOURCE
300		#include <sys/mman.h>
301		#include <sys/types.h>
302		#include <stddef.h>
303		#include <stdio.h>
304		#include <stdint.h>
305		#include <unistd.h>
306		#include <string.h>
307
308		#include "jitdump.h"
309
310		#define CHK_BYTE 0x5a
311
312		static inline uint64_t rdtsc(void)
313		{
314			unsigned int low, high;
315
316			asm volatile("rdtsc" : "=a" (low), "=d" (high));
317
318			return low | ((uint64_t)high) << 32;
319		}
320
321		static FILE *open_jitdump(void)
322		{
323			struct jitheader header = {
324				.magic      = JITHEADER_MAGIC,
325				.version    = JITHEADER_VERSION,
326				.total_size = sizeof(header),
327				.pid        = getpid(),
328				.timestamp  = rdtsc(),
329				.flags      = JITDUMP_FLAGS_ARCH_TIMESTAMP,
330			};
331			char filename[256];
332			FILE *f;
333			void *m;
334
335			snprintf(filename, sizeof(filename), "jit-%d.dump", getpid());
336			f = fopen(filename, "w+");
337			if (!f)
338				goto err;
339			/* Create an MMAP event for the jitdump file. That is how perf tool finds it. */
340			m = mmap(0, 4096, PROT_READ | PROT_EXEC, MAP_PRIVATE, fileno(f), 0);
341			if (m == MAP_FAILED)
342				goto err_close;
343			munmap(m, 4096);
344			if (fwrite(&header,sizeof(header),1,f) != 1)
345				goto err_close;
346			return f;
347
348		err_close:
349			fclose(f);
350		err:
351			return NULL;
352		}
353
354		static int write_jitdump(FILE *f, void *addr, const uint8_t *dat, size_t sz, uint64_t *idx)
355		{
356			struct jr_code_load rec = {
357				.p.id          = JIT_CODE_LOAD,
358				.p.total_size  = sizeof(rec) + sz,
359				.p.timestamp   = rdtsc(),
360				.pid	       = getpid(),
361				.tid	       = gettid(),
362				.vma           = (unsigned long)addr,
363				.code_addr     = (unsigned long)addr,
364				.code_size     = sz,
365				.code_index    = ++*idx,
366			};
367
368			if (fwrite(&rec,sizeof(rec),1,f) != 1 ||
369			fwrite(dat, sz, 1, f) != 1)
370				return -1;
371			return 0;
372		}
373
374		static void close_jitdump(FILE *f)
375		{
376			fclose(f);
377		}
378
379		int main()
380		{
381			/* Get a memory page to store executable code */
382			void *addr = mmap(0, 4096, PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
383			/* Code to execute: mov CHK_BYTE, %eax ; ret */
384			uint8_t dat[] = {0xb8, CHK_BYTE, 0x00, 0x00, 0x00, 0xc3};
385			FILE *f = open_jitdump();
386			uint64_t idx = 0;
387			int ret = 1;
388
389			if (!f)
390				return 1;
391			/* Copy executable code to executable memory page */
392			memcpy(addr, dat, sizeof(dat));
393			/* Record it in the jitdump file */
394			if (write_jitdump(f, addr, dat, sizeof(dat), &idx))
395				goto out_close;
396			/* Call it */
397			ret = ((int (*)(void))addr)() - CHK_BYTE;
398		out_close:
399			close_jitdump(f);
400			return ret;
401		}
402		_end_of_file_
403	fi
404
405	if ! $have_jitdump_workload ; then
406		echo "SKIP: No jitdump workload"
407		return 2
408	fi
409
410	# Change to temp_dir so jitdump collateral files go there
411	cd "${temp_dir}"
412	perf_record_no_bpf -o "${tmpfile}" -e intel_pt//u "${jitdump_workload}"
413	perf inject -i "${tmpfile}" -o "${perfdatafile}" --jit
414	decode_br_cnt=$(perf script -i "${perfdatafile}" --itrace=b | wc -l)
415	# Note that overflow and lost errors are suppressed for the error count
416	decode_err_cnt=$(perf script -i "${perfdatafile}" --itrace=e-o-l | grep -ci error)
417	cd -
418	# Should be thousands of branches
419	if [ "${decode_br_cnt}" -lt 1000 ] ; then
420		echo "Decode failed, only ${decode_br_cnt} branches"
421		return 1
422	fi
423	# Should be no errors
424	if [ "${decode_err_cnt}" -ne 0 ] ; then
425		echo "Decode failed, ${decode_err_cnt} errors"
426		perf script -i "${perfdatafile}" --itrace=e-o-l --show-mmap-events | cat
427		return 1
428	fi
429
430	echo OK
431	return 0
432}
433
434test_packet_filter()
435{
436	echo "--- Test with MTC and TSC disabled ---"
437	# Disable MTC and TSC
438	perf_record_no_decode -o "${perfdatafile}" -e intel_pt/mtc=0,tsc=0/u uname
439	# Should not get MTC packet
440	mtc_cnt=$(perf script -i "${perfdatafile}" -D 2>/dev/null | grep -c "MTC 0x")
441	if [ "${mtc_cnt}" -ne 0 ] ; then
442		echo "Failed to filter with mtc=0"
443		return 1
444	fi
445	# Should not get TSC package
446	tsc_cnt=$(perf script -i "${perfdatafile}" -D 2>/dev/null | grep -c "TSC 0x")
447	if [ "${tsc_cnt}" -ne 0 ] ; then
448		echo "Failed to filter with tsc=0"
449		return 1
450	fi
451	echo OK
452	return 0
453}
454
455test_disable_branch()
456{
457	echo "--- Test with branches disabled ---"
458	# Disable branch
459	perf_record_no_decode -o "${perfdatafile}" -e intel_pt/branch=0/u uname
460	# Should not get branch related packets
461	tnt_cnt=$(perf script -i "${perfdatafile}" -D 2>/dev/null | grep -c "TNT 0x")
462	tip_cnt=$(perf script -i "${perfdatafile}" -D 2>/dev/null | grep -c "TIP 0x")
463	fup_cnt=$(perf script -i "${perfdatafile}" -D 2>/dev/null | grep -c "FUP 0x")
464	if [ "${tnt_cnt}" -ne 0 ] || [ "${tip_cnt}" -ne 0 ] || [ "${fup_cnt}" -ne 0 ] ; then
465		echo "Failed to disable branches"
466		return 1
467	fi
468	echo OK
469	return 0
470}
471
472test_time_cyc()
473{
474	echo "--- Test with/without CYC ---"
475	# Check if CYC is supported
476	cyc=$(cat /sys/bus/event_source/devices/intel_pt/caps/psb_cyc)
477	if [ "${cyc}" != "1" ] ; then
478		echo "SKIP: CYC is not supported"
479		return 2
480	fi
481	# Enable CYC
482	perf_record_no_decode -o "${perfdatafile}" -e intel_pt/cyc/u uname
483	# should get CYC packets
484	cyc_cnt=$(perf script -i "${perfdatafile}" -D 2>/dev/null | grep -c "CYC 0x")
485	if [ "${cyc_cnt}" = "0" ] ; then
486		echo "Failed to get CYC packet"
487		return 1
488	fi
489	# Without CYC
490	perf_record_no_decode -o "${perfdatafile}" -e intel_pt//u uname
491	# Should not get CYC packets
492	cyc_cnt=$(perf script -i "${perfdatafile}" -D 2>/dev/null | grep -c "CYC 0x")
493	if [ "${cyc_cnt}" -gt 0 ] ; then
494		echo "Still get CYC packet without cyc"
495		return 1
496	fi
497	echo OK
498	return 0
499}
500
501test_sample()
502{
503	echo "--- Test recording with sample mode ---"
504	# Check if recording with sample mode is working
505	if ! perf_record_no_decode -o "${perfdatafile}" --aux-sample=8192 -e '{intel_pt//u,branch-misses:u}' uname ; then
506		echo "perf record failed with --aux-sample"
507		return 1
508	fi
 
 
 
 
 
 
 
509	echo OK
510	return 0
511}
512
513test_kernel_trace()
514{
515	echo "--- Test with kernel trace ---"
516	# Check if recording with kernel trace is working
517	can_kernel || return 2
518	if ! perf_record_no_decode -o "${perfdatafile}" -e intel_pt//k -m1,128 uname ; then
519		echo "perf record failed with intel_pt//k"
520		return 1
521	fi
522	echo OK
523	return 0
524}
525
526test_virtual_lbr()
527{
528	echo "--- Test virtual LBR ---"
529	# Check if python script is supported
530	libpython=$(perf version --build-options | grep python | grep -cv OFF)
531	if [ "${libpython}" != "1" ] ; then
532		echo "SKIP: python scripting is not supported"
533		return 2
534	fi
535
536	# Python script to determine the maximum size of branch stacks
537	cat << "_end_of_file_" > "${maxbrstack}"
538from __future__ import print_function
539
540bmax = 0
541
542def process_event(param_dict):
543	if "brstack" in param_dict:
544		brstack = param_dict["brstack"]
545		n = len(brstack)
546		global bmax
547		if n > bmax:
548			bmax = n
549
550def trace_end():
551	print("max brstack", bmax)
552_end_of_file_
553
554	# Check if virtual lbr is working
555	perf_record_no_bpf -o "${perfdatafile}" --aux-sample -e '{intel_pt//,cycles}:u' uname
556	times_val=$(perf script -i "${perfdatafile}" --itrace=L -s "${maxbrstack}" 2>/dev/null | grep "max brstack " | cut -d " " -f 3)
557	case "${times_val}" in
558		[0-9]*)	;;
559		*)	times_val=0;;
560	esac
561	if [ "${times_val}" -lt 2 ] ; then
562		echo "Failed with virtual lbr"
563		return 1
564	fi
565	echo OK
566	return 0
567}
568
569test_power_event()
570{
571	echo "--- Test power events ---"
572	# Check if power events are supported
573	power_event=$(cat /sys/bus/event_source/devices/intel_pt/caps/power_event_trace)
574	if [ "${power_event}" != "1" ] ; then
575		echo "SKIP: power_event_trace is not supported"
576		return 2
577	fi
578	if ! perf_record_no_decode -o "${perfdatafile}" -a -e intel_pt/pwr_evt/u uname ; then
579		echo "perf record failed with pwr_evt"
580		return 1
581	fi
582	echo OK
583	return 0
584}
585
586test_no_tnt()
587{
588	echo "--- Test with TNT packets disabled  ---"
589	# Check if TNT disable is supported
590	notnt=$(cat /sys/bus/event_source/devices/intel_pt/caps/tnt_disable)
591	if [ "${notnt}" != "1" ] ; then
592		echo "SKIP: tnt_disable is not supported"
593		return 2
594	fi
595	perf_record_no_decode -o "${perfdatafile}" -e intel_pt/notnt/u uname
596	# Should be no TNT packets
597	tnt_cnt=$(perf script -i "${perfdatafile}" -D | grep -c TNT)
598	if [ "${tnt_cnt}" -ne 0 ] ; then
599		echo "TNT packets still there after notnt"
600		return 1
601	fi
602	echo OK
603	return 0
604}
605
606test_event_trace()
607{
608	echo "--- Test with event_trace ---"
609	# Check if event_trace is supported
610	event_trace=$(cat /sys/bus/event_source/devices/intel_pt/caps/event_trace)
611	if [ "${event_trace}" != 1 ] ; then
612		echo "SKIP: event_trace is not supported"
613		return 2
614	fi
615	if ! perf_record_no_decode -o "${perfdatafile}" -e intel_pt/event/u uname ; then
616		echo "perf record failed with event trace"
617		return 1
618	fi
619	echo OK
620	return 0
621}
622
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
623count_result()
624{
625	if [ "$1" -eq 2 ] ; then
626		skip_cnt=$((skip_cnt + 1))
627		return
628	fi
629	if [ "$1" -eq 0 ] ; then
630		ok_cnt=$((ok_cnt + 1))
631		return
632	fi
633	err_cnt=$((err_cnt + 1))
634}
635
636ret=0
637test_system_wide_side_band		|| ret=$? ; count_result $ret ; ret=0
638test_per_thread "" ""			|| ret=$? ; count_result $ret ; ret=0
639test_per_thread "k" "(incl. kernel) "	|| ret=$? ; count_result $ret ; ret=0
640test_jitdump				|| ret=$? ; count_result $ret ; ret=0
641test_packet_filter			|| ret=$? ; count_result $ret ; ret=0
642test_disable_branch			|| ret=$? ; count_result $ret ; ret=0
643test_time_cyc				|| ret=$? ; count_result $ret ; ret=0
644test_sample				|| ret=$? ; count_result $ret ; ret=0
645test_kernel_trace			|| ret=$? ; count_result $ret ; ret=0
646test_virtual_lbr			|| ret=$? ; count_result $ret ; ret=0
647test_power_event			|| ret=$? ; count_result $ret ; ret=0
648test_no_tnt				|| ret=$? ; count_result $ret ; ret=0
649test_event_trace			|| ret=$? ; count_result $ret ; ret=0
 
650
651cleanup
652
653echo "--- Done ---"
654
655if [ ${err_cnt} -gt 0 ] ; then
656	exit 1
657fi
658
659if [ ${ok_cnt} -gt 0 ] ; then
660	exit 0
661fi
662
663exit 2