mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: James Clark <james.clark@linaro.org>
To: Leo Yan <leo.yan@arm.com>
Cc: Ingo Molnar <mingo@kernel.org>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org,
	linux-doc@vger.kernel.org, Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Suzuki K Poulose <suzuki.poulose@arm.com>,
	Mike Leach <mike.leach@arm.com>,
	Anshuman Khandual <anshuman.khandual@arm.com>,
	Yeoreum Yun <yeoreum.yun@arm.com>,
	Jonathan Corbet <corbet@lwn.net>, Suyash Mahar <smahar@meta.com>,
	Amir Ayupov <aaupov@fb.com>
Subject: Re: [PATCH 24/25] perf test: Add CoreSight AUX sample decoding test
Date: Tue, 20 Oct 2026 15:35:41 +0100	[thread overview]
Message-ID: <0792487e-b6fc-4b9d-8120-83281a15217c@linaro.org> (raw)
In-Reply-To: <20260915-arm_cs_support_aux_sample-v1-24-35cf6f297d20@arm.com>



On 15/09/2026 16:49, Leo Yan wrote:
> Add a shell test that records the brstack workload with 8 KiB AUX
> samples attached to cycle events. Pin the workload to a CPU with a TRBE
> sink and use per-thread recording with trace timestamps and context IDs
> disabled, exercising decoding with the owning sample's context.
> 
> Check branch stacks and callchains with --itrace=L4, L64, G3 and G3L64.
> Require at least one sample to reach both requested depths, with all
> callchain frames belonging to the workload, and reject samples exceeding
> either limit. Use a callchain depth of three to match the nested calls
> in brstack.
> 
> Compare with AUX decoding enabled and disabled to verify that adding
> history preserves the original PMU samples.
> 
> Assisted-by: Codex:gpt-6
> Signed-off-by: Leo Yan <leo.yan@arm.com>
> ---
>   tools/perf/tests/shell/coresight/aux_sample.sh | 189 +++++++++++++++++++++++++
>   1 file changed, 189 insertions(+)
> 
> diff --git a/tools/perf/tests/shell/coresight/aux_sample.sh b/tools/perf/tests/shell/coresight/aux_sample.sh
> new file mode 100755
> index 0000000000000000000000000000000000000000..887ad6a085eacdfd192db3863fbb61dae1aa19b5
> --- /dev/null
> +++ b/tools/perf/tests/shell/coresight/aux_sample.sh
> @@ -0,0 +1,189 @@
> +#!/bin/bash -e
> +# SPDX-License-Identifier: GPL-2.0
> +# CoreSight AUX samples with callchains and branch stacks (exclusive)
> +
> +export LC_ALL=C
> +
> +skip()
> +{
> +	echo "[Skip] $1"
> +	exit 2
> +}
> +
> +perf list pmu | grep -q 'cs_etm//' || skip "cs_etm is not available"
> +perf check feature -q libopencsd || skip "perf was built without OpenCSD"
> +[ "$(id -u)" = 0 ] || skip "No root permission"
> +command -v taskset >/dev/null || skip "taskset is not available"
> +
> +# AUX sample now supports per CPU sink (e.g., TRBE). Select a usable CPU
> +# and name its sink explicitly to avoid a shared sink.
> +sink=
> +for dev in /sys/bus/coresight/devices/trbe[0-9]*; do
> +	[ -d "$dev" ] || continue
> +	name=${dev##*/}
> +	cpu=${name#trbe}
> +	[ -e "/sys/bus/event_source/devices/cs_etm/cpu$cpu" ] || continue
> +	if taskset -c "$cpu" true 2>/dev/null; then
> +		sink=$name
> +		break
> +	fi
> +done
> +[ -n "$sink" ] || skip "No usable TRBE CPU is available"
> +
> +tmpdir=$(mktemp -d /tmp/perf-cs-aux-sample.XXXXXX)
> +
> +cleanup()
> +{
> +	rm -rf "$tmpdir"
> +}
> +
> +trap cleanup EXIT
> +trap 'exit 1' TERM INT
> +
> +fail()
> +{
> +	echo "$1" >&2
> +	cat "$tmpdir/stderr" >&2
> +	exit 1
> +}
> +
> +cf="$tmpdir/ctl"
> +af="$tmpdir/ack"
> +mkfifo "$cf" "$af"
> +
> +# Disable trace timestamps and context IDs to use the owning sample's context.
> +# Use a per-thread mmap so the explicit sink is only used on the pinned CPU.
> +if ! taskset -c "$cpu" perf record -o "$tmpdir/data" --aux-sample=8192 \
> +	-e "{cs_etm/@$sink,timestamp=0,contextid=0/u,cycles/period=100003/u}" \
> +	--per-thread -D -1 --control fifo:"$cf","$af" -- \

Minor nit, but on "[PATCH 00/14] perf cs-etm: Per-thread mode fixes and 
snapshot wrap support" I just did:

+if ! ls /sys/bus/coresight/devices/trbe* > /dev/null 2>&1; then
+	echo "No TRBE sinks, skipping output validation"
+	exit 0
+fi

I know looking up the actual sink, passing it in and also using 
--per-thread mode is "technically correct", but I think we can assume 
that any normal record command will work on any system with TRBE.

Otherwise, we should re-write all of the tests to only target CPUs with 
known loaded ETM instances instead of hard coding CPU 0 etc.

Other than that LGTM:

Reviewed-by: James Clark <james.clark@linaro.org>

> +	perf test --record-ctl fifo:"$cf","$af" -w brstack 1000000 \
> +	>/dev/null 2>"$tmpdir/stderr"; then
> +	# Kernels without snapshot_aux reject the sampling event at open time.
> +	if grep -Eq 'sys_perf_event_open.*event \(.*cycles.*\): Invalid argument' \
> +		"$tmpdir/stderr"; then
> +		skip "Kernel does not accept CoreSight AUX sampling"
> +	fi
> +	fail "Failed to record CoreSight AUX samples"
> +fi
> +
> +perf evlist -v -i "$tmpdir/data" >"$tmpdir/evlist" 2>"$tmpdir/stderr" ||
> +	fail "Failed to read AUX sample attributes"
> +grep -Eq 'sample_type:.*\|AUX(,|\|).*aux_sample_size: 8192(,|$)' \
> +	"$tmpdir/evlist" || fail "Missing AUX sample attributes"
> +
> +check_history()
> +{
> +	local options=$1
> +	local max_branches=$2
> +	local max_callchains=$3
> +	local output="$tmpdir/script-$options"
> +
> +	perf script -i "$tmpdir/data" --itrace="$options" \
> +		-F comm,pid,tid,cpu,event,ip,sym,brstack >"$output" \
> +		2>"$tmpdir/stderr" || fail "Failed to decode AUX samples with $options"
> +
> +	# Some windows may contain incomplete trace. Require at least one cycle
> +	# sample to reach both requested depths, and reject any that exceed them.
> +	if ! awk -v max_branches="$max_branches" \
> +		-v max_callchains="$max_callchains" '
> +		function check_sample() {
> +			if (in_sample == 0) {
> +				return
> +			}
> +
> +			if (branch_entries > max_branches || callchain_frames > max_callchains) {
> +				depth_exceeded = 1
> +			}
> +
> +			# Both depths must be reached in the same sample, with all
> +			# callchain frames belonging to the brstack workload.
> +			if (branch_entries == max_branches &&
> +			    callchain_frames == max_callchains &&
> +			    workload_frames == max_callchains) {
> +				found_sample = 1
> +			}
> +
> +			branch_entries = 0
> +			callchain_frames = 0
> +			workload_frames = 0
> +			in_sample = 0
> +		}
> +		{
> +			# A blank line marks the end of a sample.
> +			if (NF == 0) {
> +				check_sample()
> +				next
> +			}
> +
> +			# Sample header: comm pid/tid [cpu] event ...
> +			# The event may include a PMU prefix and configuration terms.
> +			if ($4 ~ /(^|\/)cycles([,\/:]|$)/) {
> +				check_sample()
> +				in_sample = 1
> +			}
> +			if (in_sample == 0) {
> +				next
> +			}
> +
> +			# Each branch entry is a field in 0xFROM/0xTO/... form.
> +			for (i = 1; i <= NF; i++) {
> +				if ($i ~ /^0x[[:xdigit:]]+\/0x[[:xdigit:]]+\//) {
> +					branch_entries++
> +				}
> +			}
> +
> +			# Callchain rows start with a hexadecimal IP, then a symbol.
> +			if ($1 ~ /^[[:xdigit:]]+$/) {
> +				callchain_frames++
> +				if ($2 ~ /^brstack(_|$)/) {
> +					workload_frames++
> +				}
> +			}
> +		}
> +		END {
> +			# Account for the last sample even without a trailing blank line.
> +			check_sample()
> +
> +			if (depth_exceeded != 0) {
> +				print "AUX sample history exceeds the requested depth" > "/dev/stderr"
> +				exit 1
> +			}
> +			if (found_sample == 0) {
> +				printf "No sample has %d branches and %d workload callchain frames\n", \
> +					max_branches, max_callchains > "/dev/stderr"
> +				exit 1
> +			}
> +			exit 0
> +		}
> +	' "$output"; then
> +		head -n 80 "$output" >&2
> +		fail "Missing AUX sample history or incorrect depth with $options"
> +	fi
> +}
> +
> +check_sample_identity()
> +{
> +	local fields=comm,pid,tid,cpu,event,ip
> +
> +	# Hiding history must leave exactly the original PMU samples, including
> +	# their PID/TID, CPU and sampled IP, with no synthesized events. Per-thread
> +	# recording does not require sample timestamps.
> +	perf script -i "$tmpdir/data" --no-itrace -F "$fields" \
> +		>"$tmpdir/original" 2>"$tmpdir/stderr" || fail "Failed to read cycle samples"
> +	perf script -i "$tmpdir/data" --itrace=L4 -F "$fields" \
> +		>"$tmpdir/decoded" 2>"$tmpdir/stderr" || fail "Failed to decode cycle samples"
> +	diff -u "$tmpdir/original" "$tmpdir/decoded" ||
> +		fail "AUX decoding changed the original cycle samples"
> +}
> +
> +check_history L4 4 0
> +check_history L64 64 0
> +
> +# brstack -> brstack_bench -> brstack_foo -> brstack_bar provides three
> +# nested calls whose caller frames can be reconstructed from an AUX window.
> +check_history G3 0 3
> +check_history G3L64 64 3
> +
> +check_sample_identity
> +
> +echo "CoreSight AUX sample decoding: PASS"
> 


  reply	other threads:[~2026-10-20 14:35 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-15 15:48 [PATCH 00/25] CoreSight / perf: Support AUX sampling Leo Yan
2026-09-15 15:48 ` [PATCH 01/25] perf/core: Reject non-positive AUX snapshot sizes Leo Yan
2026-10-16 13:14   ` James Clark
2026-09-15 15:48 ` [PATCH 02/25] perf/core: Prevent AUX sampling from racing with pause/resume Leo Yan
2026-10-16 13:29   ` James Clark
2026-09-15 15:48 ` [PATCH 03/25] perf/core: Export output copy helpers for CoreSight Leo Yan
2026-10-16 13:30   ` James Clark
2026-09-15 15:48 ` [PATCH 04/25] coresight: perf: End AUX output when an event is throttled Leo Yan
2026-10-16 13:46   ` James Clark
2026-09-15 15:48 ` [PATCH 05/25] coresight: perf: Extract AUX buffer update helper Leo Yan
2026-10-16 13:53   ` James Clark
2026-09-15 15:48 ` [PATCH 06/25] coresight: perf: Simplify flow for CPUs without a path Leo Yan
2026-10-16 13:59   ` James Clark
2026-09-15 15:48 ` [PATCH 07/25] coresight: perf: Validate the live context through its path Leo Yan
2026-10-19  9:25   ` James Clark
2026-09-15 15:48 ` [PATCH 08/25] coresight: perf: Validate context before resuming trace Leo Yan
2026-10-19  9:27   ` James Clark
2026-09-15 15:48 ` [PATCH 09/25] coresight: perf: Improve hardware state transitions Leo Yan
2026-10-19  9:33   ` James Clark
2026-10-19  9:35   ` James Clark
2026-09-15 15:48 ` [PATCH 10/25] coresight: trbe: Pad snapshot buffers Leo Yan
2026-10-19  9:45   ` James Clark
2026-09-15 15:48 ` [PATCH 11/25] coresight: trbe: Use circular buffer mode for snapshots Leo Yan
2026-10-19 10:13   ` James Clark
2026-09-15 15:48 ` [PATCH 12/25] coresight: trbe: Track per-CPU sink interrupt handling Leo Yan
2026-10-19 11:54   ` James Clark
2026-09-15 15:48 ` [PATCH 13/25] coresight: trbe: Handle an already disabled sink Leo Yan
2026-10-19 12:44   ` James Clark
2026-09-15 15:49 ` [PATCH 14/25] coresight: perf: Look up the trace path in etm_event_pause() Leo Yan
2026-10-19 12:46   ` James Clark
2026-09-15 15:49 ` [PATCH 15/25] coresight: perf: Serialize AUX pause and resume with event stops Leo Yan
2026-10-19 13:02   ` James Clark
2026-09-15 15:49 ` [PATCH 16/25] coresight: perf: Support AUX sampling with per-CPU sinks Leo Yan
2026-10-20 13:21   ` James Clark
2026-09-15 15:49 ` [PATCH 17/25] perf cs-etm: Allow history collection for samples with zero IP Leo Yan
2026-10-20 13:23   ` James Clark
2026-09-15 15:49 ` [PATCH 18/25] perf cs-etm: Unify timeless buffer decoding Leo Yan
2026-10-20 14:15   ` James Clark
2026-09-15 15:49 ` [PATCH 19/25] perf cs-etm: Propagate errors from trace queue flushing Leo Yan
2026-09-15 15:49 ` [PATCH 20/25] perf cs-etm: Drain pending packets before finishing trace blocks Leo Yan
2026-09-15 15:49 ` [PATCH 21/25] perf cs-etm: Complete packet draining with end of trace Leo Yan
2026-09-15 15:49 ` [PATCH 22/25] perf cs-etm: Centralize sample history collection Leo Yan
2026-09-15 15:49 ` [PATCH 23/25] perf cs-etm: Decode AUX samples into callchains and branch stacks Leo Yan
2026-10-20 14:13   ` James Clark
2026-09-15 15:49 ` [PATCH 24/25] perf test: Add CoreSight AUX sample decoding test Leo Yan
2026-10-20 14:35   ` James Clark [this message]
2026-09-15 15:49 ` [PATCH 25/25] Documentation: coresight: Document AUX sample decoding Leo Yan
2026-10-20 14:26   ` James Clark

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0792487e-b6fc-4b9d-8120-83281a15217c@linaro.org \
    --to=james.clark@linaro.org \
    --cc=aaupov@fb.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=anshuman.khandual@arm.com \
    --cc=corbet@lwn.net \
    --cc=coresight@lists.linaro.org \
    --cc=irogers@google.com \
    --cc=leo.yan@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mike.leach@arm.com \
    --cc=mingo@kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=smahar@meta.com \
    --cc=suzuki.poulose@arm.com \
    --cc=yeoreum.yun@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®