Record a deterministic Java workload using the JVMTI agent and check that the decoded source lines match the expected sequence.
Alternate fresh class loaders and request garbage collection to encourage address re-use so that we can test if trace timestamps match up to jitdump events.
Signed-off-by: James Clark james.clark@linaro.org --- tools/perf/Makefile.perf | 4 +- tools/perf/tests/shell/coresight/java-jit.sh | 65 ++++++++++++++++++ tools/perf/tests/shell/coresight/lib/PerfJit.java | 82 +++++++++++++++++++++++ 3 files changed, 150 insertions(+), 1 deletion(-)
diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf index b74b534e1153..53c10e8e4556 100644 --- a/tools/perf/Makefile.perf +++ b/tools/perf/Makefile.perf @@ -992,7 +992,9 @@ install-tests: all install-gtk $(INSTALL) tests/shell/base_report/*.sh '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests/shell/base_report'; \ $(INSTALL) tests/shell/base_report/*.txt '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests/shell/base_report'; \ $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests/shell/coresight' ; \ - $(INSTALL) tests/shell/coresight/*.sh '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests/shell/coresight' + $(INSTALL) tests/shell/coresight/*.sh '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests/shell/coresight'; \ + $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests/shell/coresight/lib'; \ + $(INSTALL) tests/shell/coresight/lib/*.java -m 644 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests/shell/coresight/lib'
install-bin: install-tools install-tests
diff --git a/tools/perf/tests/shell/coresight/java-jit.sh b/tools/perf/tests/shell/coresight/java-jit.sh new file mode 100755 index 000000000000..cb6099efa899 --- /dev/null +++ b/tools/perf/tests/shell/coresight/java-jit.sh @@ -0,0 +1,65 @@ +#!/bin/bash +# CoreSight Java JIT source lines and code address reuse (exclusive) +# SPDX-License-Identifier: GPL-2.0 + +set -e +set -o pipefail + +script_dir=$(dirname "$0") + +# Requires a HotSpot JDK (11+) and libperf-jvmti with line number support. +perf list pmu | grep cs_etm > /dev/null || exit 2 +# Require TRBE so that the trace always has virtual timestamps. +if ! ls /sys/bus/coresight/devices/trbe* > /dev/null 2>&1; then + echo "No TRBE sinks, skipping" + exit 2 +fi +command -v java > /dev/null || exit 2 +command -v javac > /dev/null || exit 2 +command -v timeout > /dev/null || exit 2 +for lib in "${LIBJVMTI:-}" \ + "$(dirname "$(readlink -f "$(type -P perf)")")/libperf-jvmti.so" \ + "${PREFIX:-/usr}/lib64/libperf-jvmti.so" "${PREFIX:-/usr}/lib/libperf-jvmti.so"; do + if [ -f "$lib" ]; then + LIBJVMTI=$(readlink -f "$lib") + break + fi +done +[ -f "${LIBJVMTI:-}" ] || { echo "SKIP: missing libperf-jvmti.so"; exit 2; } + +tmpdir=$(mktemp -d /tmp/__perf_test.coresight_java_jit.XXXXX) +trap '[ "$?" -eq 0 ] && rm -rf "$tmpdir" || echo "Java JIT test files retained in $tmpdir"' EXIT +trap 'exit 1' TERM INT + +javac -g --release 11 -d "$tmpdir" "$script_dir/lib/PerfJit.java" +mkfifo "$tmpdir/ctl" "$tmpdir/ack" +export JITDUMPDIR="$tmpdir" JITDUMP_USE_ARCH_TIMESTAMP=1 + +# Compile only run(), and collect trace only while it executes. +# A small code cache and class unloading encourage address reuse. +timeout 60s perf record \ + -o "$tmpdir/data" -e cs_etm//u -D -1 -m ,8M \ + --control "fifo:$tmpdir/ctl,$tmpdir/ack" -- \ + java -Xbatch -XX:-TieredCompilation -XX:CompileThreshold=100 \ + -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints \ + '-XX:CompileCommand=compileonly,PerfJit$*::run' \ + -XX:ReservedCodeCacheSize=4m -XX:+UseSerialGC \ + -agentpath:"$LIBJVMTI" -cp "$tmpdir" PerfJit "$tmpdir/ctl" "$tmpdir/ack" + +perf inject --jit -b \ + -i "$tmpdir/data" -o "$tmpdir/injected.data" + +perf script \ + -i "$tmpdir/injected.data" --itrace=i1i -F ip,srcline \ + --symbols 'PerfJit$First.run(),PerfJit$Second.run()' > "$tmpdir/script" + +# The workload runs eight pairs of First.run() and Second.run(). +awk '/// TRACE$/ { lines = lines "PerfJit.java:" NR "\n" } + END { for (i = 0; i < 8; i++) printf "%s", lines }' \ + "$script_dir/lib/PerfJit.java" > "$tmpdir/expected" + +# Ignore entry/return lines and retain the order of the marked Java lines. +sed -n 's/^.*PerfJit.java:/PerfJit.java:/p' "$tmpdir/script" | uniq > "$tmpdir/lines" +grep -Fxf "$tmpdir/expected" "$tmpdir/lines" > "$tmpdir/actual" || true +diff -u "$tmpdir/expected" "$tmpdir/actual" +echo "Java JIT source lines match the expected sequence" diff --git a/tools/perf/tests/shell/coresight/lib/PerfJit.java b/tools/perf/tests/shell/coresight/lib/PerfJit.java new file mode 100644 index 000000000000..30b22e952bd4 --- /dev/null +++ b/tools/perf/tests/shell/coresight/lib/PerfJit.java @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-2.0 +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.InputStream; +import java.util.Arrays; + +public class PerfJit { + public static class First implements Runnable { + public static volatile int value; + public void run() { + value += 1; // TRACE + value += 2; // TRACE + value += 3; // TRACE + value += 0; // Give the last checked line a DWARF end address. + } + } + + public static class Second implements Runnable { + public static volatile int value; + public void run() { + value += 4; // TRACE + value += 5; // TRACE + value += 6; // TRACE + value += 0; // Give the last checked line a DWARF end address. + } + } + + static FileWriter control; + static FileInputStream ack; + + static void command(String command) throws Exception { + if (control != null) { + control.write(command + "\n"); + control.flush(); + // perf writes sizeof("ack\n"), including the trailing NUL. + byte[] reply = new byte[5]; + if (ack.readNBytes(reply, 0, reply.length) != reply.length || + !Arrays.equals(reply, new byte[] {'a', 'c', 'k', '\n', 0})) + throw new Exception("Missing perf control ack"); + } + } + + static void phase(String name, int repeats) throws Exception { + byte[] code; + try (InputStream in = PerfJit.class.getResourceAsStream("PerfJit$" + name + ".class")) { + code = in.readAllBytes(); + } + // A fresh loader allows this class and its compiled code to be reclaimed. + Class<?> block = new ClassLoader(null) { + Class<?> load() { return defineClass(null, code, 0, code.length); } + }.load(); + Runnable work = (Runnable) block.getConstructor().newInstance(); + for (int i = 0; i < 20000; i++) + work.run(); + command("enable"); + for (int i = 0; i < repeats; i++) + work.run(); + command("disable"); + } + + public static void main(String[] args) throws Exception { + if (args.length >= 2) { + control = new FileWriter(args[0]); + ack = new FileInputStream(args[1]); + } + int rounds = args.length >= 3 ? Integer.parseInt(args[2]) : 8; + int repeats = args.length >= 4 ? Integer.parseInt(args[3]) : 1; + // Warm up the FIFO path before enabling trace for the first method. + command("disable"); + for (int i = 0; i < rounds * 2; i++) { + phase(i % 2 == 0 ? "First" : "Second", repeats); + for (int gc = 0; gc < 3; gc++) { + System.gc(); + Thread.sleep(20); + } + } + if (control != null) { + control.close(); + ack.close(); + } + } +}