You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by lh...@apache.org on 2026/09/20 20:04:50 UTC

(pulsar) 05/09: [improve][test] Report JFR recording time from chunk headers

This is an automated email from the ASF dual-hosted git repository.

lhotari pushed a commit to branch lh-improve-perf-measurement-jfr
in repository https://gitbox.apache.org/repos/asf/pulsar.git

commit 1adf7d2f25fc64bd05bb89a109551d913db167ae
Author: Lari Hotari <lh...@apache.org>
AuthorDate: Sun Sep 20 19:57:37 2026 +0300

    [improve][test] Report JFR recording time from chunk headers
---
 tests/performance/README.md                        |   6 +-
 .../pulsar/tests/performance/launcher/JfrCut.java  | 109 +++++++++++++++++----
 .../tests/performance/launcher/JfrCutTest.java     |   6 +-
 3 files changed, 98 insertions(+), 23 deletions(-)

diff --git a/tests/performance/README.md b/tests/performance/README.md
index fe6700c66446..40888f4fde64 100644
--- a/tests/performance/README.md
+++ b/tests/performance/README.md
@@ -118,9 +118,11 @@ recording after a successful cut, or `profiling.createMeasurementRecording: fals
 recording. Both options default to `true` and apply to broker, producer and consumer recordings.
 
 Use the same cutter independently to select a different interval from an existing recording. `--from` and `--to`
-accept ISO-8601 instants, epoch milliseconds, or offsets from the first event such as `500ms`, `5s`, `2m`, `1h`,
+accept ISO-8601 instants, epoch milliseconds, or offsets from the recording start such as `500ms`, `5s`, `2m`, `1h`,
 or `PT5S`. Omit `--from` to select from the beginning, or omit `--to` to select through the end. Use `--info`
-without either boundary to display the first and last event times and duration; it can also accompany a cut. The
+without either boundary to display the actual recording start, end and total duration from the JFR chunk headers;
+it can also accompany a cut. JFR cutting preserves those source chunk timestamps, so the original recording period
+remains available in the cut file and in JDK Mission Control. The
 task requires JDK 19 or newer because it uses the public JFR recording writer added in that release:
 
 ```bash
diff --git a/tests/performance/launcher/src/main/java/org/apache/pulsar/tests/performance/launcher/JfrCut.java b/tests/performance/launcher/src/main/java/org/apache/pulsar/tests/performance/launcher/JfrCut.java
index d179617fde75..e6050e5d7644 100644
--- a/tests/performance/launcher/src/main/java/org/apache/pulsar/tests/performance/launcher/JfrCut.java
+++ b/tests/performance/launcher/src/main/java/org/apache/pulsar/tests/performance/launcher/JfrCut.java
@@ -21,9 +21,12 @@ package org.apache.pulsar.tests.performance.launcher;
 import java.io.IOException;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.StandardCopyOption;
+import java.nio.file.StandardOpenOption;
 import java.time.Duration;
 import java.time.Instant;
 import java.util.Set;
@@ -36,14 +39,35 @@ import picocli.CommandLine.Command;
 import picocli.CommandLine.Option;
 
 /**
- * Writes the events overlapping a time interval to a new JFR recording.
+ * Creates a smaller JFR recording for a selected interval of a longer recording.
  *
- * <p>Call {@link #cut(Path, Instant, Instant, Path)} directly when embedding the cutter, or invoke this class as a
- * command-line application through the {@code runJfrCut} Gradle task.
+ * <p>Performance recordings commonly start before clients, connections and application code have finished warming
+ * up. Startup, class loading, JIT compilation and warmup traffic can then dominate a short profile even when the
+ * experiment reports throughput for a later steady measurement interval. Cutting the JFR to the same interval makes
+ * CPU, allocation and lock profiles correspond to the reported measurement and makes profiles from separate runs
+ * easier to compare.
+ *
+ * <p>The cutter keeps the source JFR chunk headers, including the actual recording start, end and duration. It also
+ * retains one-time JVM and host configuration events that normally occur before the selected interval. The result can
+ * therefore still be interpreted in JDK Mission Control with the JVM flags, runtime configuration and machine details
+ * that produced the measured events. Keeping the complete source recording alongside the cut recording remains useful
+ * when investigating startup or shutdown behavior.
+ *
+ * <p>Boundaries can be supplied as absolute {@link Instant} values through {@link #cut(Path, Instant, Instant, Path)}.
+ * {@link #cutUsingTimeExpressions(Path, String, String, Path)} additionally accepts ISO-8601 timestamps, epoch
+ * milliseconds and offsets from the recording start such as {@code 5s}. A missing boundary selects the corresponding
+ * beginning or end of the recording. {@link #recordingInfo(Path)} reads the source timestamps directly from its JFR
+ * chunk headers.
+ *
+ * <p>Applications can call these methods directly. For one-off use, invoke this class through the {@code runJfrCut}
+ * Gradle task.
  */
 @Command(name = "jfr-cut", mixinStandardHelpOptions = true,
         description = "Write events overlapping a time interval to a new JFR recording")
 public final class JfrCut implements Callable<Integer> {
+    private static final int JFR_CHUNK_HEADER_SIZE = 68;
+    private static final byte[] JFR_MAGIC = {'F', 'L', 'R', 0};
+    private static final long NANOS_PER_SECOND = 1_000_000_000L;
     private static final Set<String> JVM_CONTEXT_EVENTS = Set.of(
             "jdk.ActiveRecording",
             "jdk.ActiveSetting",
@@ -90,7 +114,7 @@ public final class JfrCut implements Callable<Integer> {
             description = "Exclusive ISO-8601 instant, epoch milliseconds, or offset such as 5s")
     private String to;
 
-    @Option(names = "--info", description = "Show the recording event range and duration")
+    @Option(names = "--info", description = "Show the recording start, end and total duration")
     private boolean showInfo;
 
     private JfrCut() {
@@ -122,7 +146,7 @@ public final class JfrCut implements Callable<Integer> {
 
     /**
      * Resolves command-line time expressions and cuts a recording without invoking {@link #main(String[])}.
-     * Relative values such as {@code 5s} are measured from the first event in the recording. A {@code null} start
+     * Relative values such as {@code 5s} are measured from the recording start. A {@code null} start
      * or end selects the corresponding recording boundary.
      */
     public static void cutUsingTimeExpressions(Path input, String from, String to, Path output) throws IOException {
@@ -213,28 +237,75 @@ public final class JfrCut implements Callable<Integer> {
         return input.resolveSibling(basename + ".cut.jfr");
     }
 
-    /** Returns the first and last event times and their duration. */
+    /** Returns the start, end and total duration recorded in the JFR chunk headers. */
     public static RecordingInfo recordingInfo(Path input) throws IOException {
-        Instant start = null;
-        Instant end = null;
-        try (RecordingFile recording = new RecordingFile(input.toAbsolutePath().normalize())) {
-            while (recording.hasMoreEvents()) {
-                RecordedEvent event = recording.readEvent();
-                if (start == null || event.getStartTime().isBefore(start)) {
-                    start = event.getStartTime();
+        long firstStartNanos = Long.MAX_VALUE;
+        long lastEndNanos = Long.MIN_VALUE;
+        Path normalizedInput = input.toAbsolutePath().normalize();
+        try (FileChannel channel = FileChannel.open(normalizedInput, StandardOpenOption.READ)) {
+            long fileSize = channel.size();
+            long chunkOffset = 0;
+            while (chunkOffset < fileSize) {
+                ByteBuffer header = ByteBuffer.allocate(JFR_CHUNK_HEADER_SIZE);
+                readFully(channel, header, chunkOffset);
+                header.flip();
+                for (byte expected : JFR_MAGIC) {
+                    if (header.get() != expected) {
+                        throw new IOException("Invalid JFR chunk at offset " + chunkOffset + " in " + input);
+                    }
                 }
-                if (end == null || event.getEndTime().isAfter(end)) {
-                    end = event.getEndTime();
+                header.getShort();
+                header.getShort();
+                long chunkSize = header.getLong();
+                header.getLong();
+                header.getLong();
+                long startNanos = header.getLong();
+                long durationNanos = header.getLong();
+                if (chunkSize < JFR_CHUNK_HEADER_SIZE || chunkSize > fileSize - chunkOffset
+                        || durationNanos < 0) {
+                    throw new IOException("Invalid JFR chunk header at offset " + chunkOffset + " in " + input);
                 }
+                final long endNanos;
+                try {
+                    endNanos = Math.addExact(startNanos, durationNanos);
+                } catch (ArithmeticException overflow) {
+                    throw new IOException("JFR chunk timestamp overflow at offset " + chunkOffset + " in " + input,
+                            overflow);
+                }
+                firstStartNanos = Math.min(firstStartNanos, startNanos);
+                lastEndNanos = Math.max(lastEndNanos, endNanos);
+                chunkOffset += chunkSize;
             }
         }
-        if (start == null) {
-            throw new IOException("Cannot cut a JFR recording with no events: " + input);
+        if (firstStartNanos == Long.MAX_VALUE) {
+            throw new IOException("Cannot inspect an empty JFR recording: " + input);
+        }
+        final long totalDurationNanos;
+        try {
+            totalDurationNanos = Math.subtractExact(lastEndNanos, firstStartNanos);
+        } catch (ArithmeticException overflow) {
+            throw new IOException("JFR recording duration overflow in " + input, overflow);
         }
-        return new RecordingInfo(start, end, Duration.between(start, end));
+        Instant start = epochNanosToInstant(firstStartNanos);
+        Instant end = epochNanosToInstant(lastEndNanos);
+        return new RecordingInfo(start, end, Duration.ofNanos(totalDurationNanos));
+    }
+
+    private static void readFully(FileChannel channel, ByteBuffer target, long position) throws IOException {
+        while (target.hasRemaining()) {
+            int read = channel.read(target, position + target.position());
+            if (read < 0) {
+                throw new IOException("Truncated JFR chunk header at offset " + position);
+            }
+        }
+    }
+
+    private static Instant epochNanosToInstant(long epochNanos) {
+        return Instant.ofEpochSecond(Math.floorDiv(epochNanos, NANOS_PER_SECOND),
+                Math.floorMod(epochNanos, NANOS_PER_SECOND));
     }
 
-    /** Event time range found in a JFR recording. */
+    /** Time range stored in a JFR recording's chunk headers. */
     public record RecordingInfo(Instant start, Instant end, Duration duration) {
         Instant exclusiveEnd() {
             return end.equals(Instant.MAX) ? end : end.plusNanos(1);
diff --git a/tests/performance/launcher/src/test/java/org/apache/pulsar/tests/performance/launcher/JfrCutTest.java b/tests/performance/launcher/src/test/java/org/apache/pulsar/tests/performance/launcher/JfrCutTest.java
index 68c798f137ae..bcca2c73fc00 100644
--- a/tests/performance/launcher/src/test/java/org/apache/pulsar/tests/performance/launcher/JfrCutTest.java
+++ b/tests/performance/launcher/src/test/java/org/apache/pulsar/tests/performance/launcher/JfrCutTest.java
@@ -57,6 +57,7 @@ public class JfrCutTest {
             assertEquals(markers(output), List.of("measurement"));
             assertEquals(markers(input), List.of("before", "measurement", "after"));
             assertTrue(eventNames(output).contains("jdk.JVMInformation"));
+            assertEquals(JfrCut.recordingInfo(output), JfrCut.recordingInfo(input));
         } finally {
             deleteDirectory(directory);
         }
@@ -140,7 +141,7 @@ public class JfrCutTest {
         Path directory = Files.createTempDirectory("jfr-relative-cut-test");
         try {
             Path input = directory.resolve("input.jfr");
-            createRecording(input);
+            Instant[] measurementInterval = createRecording(input);
             Path fromBeginning = directory.resolve("from-beginning.jfr");
             Path throughEnd = directory.resolve("through-end.jfr");
 
@@ -150,7 +151,8 @@ public class JfrCutTest {
             assertEquals(markers(fromBeginning), List.of("before", "measurement", "after"));
             assertEquals(markers(throughEnd), List.of("before", "measurement", "after"));
             JfrCut.RecordingInfo info = JfrCut.recordingInfo(input);
-            assertTrue(!info.end().isBefore(info.start()));
+            assertTrue(!info.start().isAfter(measurementInterval[0]));
+            assertTrue(!info.end().isBefore(measurementInterval[1]));
             assertEquals(info.duration(), Duration.between(info.start(), info.end()));
         } finally {
             deleteDirectory(directory);