From e421f2da900dbb83be097969c30894cb6cc8ba33 Mon Sep 17 00:00:00 2001 From: alevymyers Date: Fri, 22 May 2026 19:28:41 -0500 Subject: [PATCH] Re-register custom events on every JFR chunk (#1740) Co-authored-by: Andrei Pangin <1749416+apangin@users.noreply.github.com> --- src/converter/one/jfr/JfrReader.java | 31 +++++++++++++++++++++------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/converter/one/jfr/JfrReader.java b/src/converter/one/jfr/JfrReader.java index 1f8c955e..b9503f9f 100644 --- a/src/converter/one/jfr/JfrReader.java +++ b/src/converter/one/jfr/JfrReader.java @@ -64,6 +64,7 @@ public class JfrReader implements Closeable { public final Map> enums = new HashMap<>(); private final Dictionary> customEvents = new Dictionary<>(); + private final Map> customEventsByName = new HashMap<>(); private int executionSample; private int nativeMethodSample; @@ -129,6 +130,11 @@ public class JfrReader implements Closeable { } public void registerEvent(String name, Class eventClass) { + customEventsByName.put(name, eventClass); + registerCustomEvent(name, eventClass); + } + + private void registerCustomEvent(String name, Class eventClass) { JfrClass type = typesByName.get(name); if (type != null) { try { @@ -139,6 +145,22 @@ public class JfrReader implements Closeable { } } + private void registerCustomEvents() { + customEvents.clear(); + + // The following classes are instantiated reflectively. + // Make sure to list them in reachability-metadata.json. + registerCustomEvent("jdk.CPULoad", CPULoad.class); + registerCustomEvent("jdk.GCHeapSummary", GCHeapSummary.class); + registerCustomEvent("jdk.ObjectCount", ObjectCount.class); + registerCustomEvent("jdk.ObjectCountAfterGC", ObjectCount.class); + registerCustomEvent("profiler.ProcessSample", ProcessSample.class); + + for (Map.Entry> entry : customEventsByName.entrySet()) { + registerCustomEvent(entry.getKey(), entry.getValue()); + } + } + // Similar to eof(), but parses the next chunk header public boolean hasMoreChunks() throws IOException { return state == STATE_NEW_CHUNK ? readChunk(buf.position()) : state == STATE_READING; @@ -361,6 +383,7 @@ public class JfrReader implements Closeable { readMeta(chunkStart + metaOffset); readConstantPool(chunkStart + cpOffset); cacheEventTypes(); + registerCustomEvents(); seek(chunkStart + CHUNK_HEADER_SIZE); state = STATE_READING; @@ -616,14 +639,6 @@ public class JfrReader implements Closeable { cpuTimeSample = getTypeId("jdk.CPUTimeSample"); nativeLock = getTypeId("profiler.NativeLock"); - // The following classes are instantiated reflectively. - // Make sure to list them in reachability-metadata.json. - registerEvent("jdk.CPULoad", CPULoad.class); - registerEvent("jdk.GCHeapSummary", GCHeapSummary.class); - registerEvent("jdk.ObjectCount", ObjectCount.class); - registerEvent("jdk.ObjectCountAfterGC", ObjectCount.class); - registerEvent("profiler.ProcessSample", ProcessSample.class); - JfrClass wallClass = typesByName.get("profiler.WallClockSample"); hasWallTimeSpan = wallClass != null && wallClass.field("timeSpan") != null; }