|
| 1 | +///usr/bin/env jbang "$0" "$@" ; exit $? |
| 2 | +//DEPS info.picocli:picocli:4.6.3 |
| 3 | + |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.PrintWriter; |
| 8 | +import java.nio.file.Path; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.LinkedHashMap; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Optional; |
| 13 | +import java.util.concurrent.Callable; |
| 14 | +import java.util.stream.Collectors; |
| 15 | + |
| 16 | +import jdk.jfr.consumer.RecordedEvent; |
| 17 | +import jdk.jfr.consumer.RecordedFrame; |
| 18 | +import jdk.jfr.consumer.RecordedMethod; |
| 19 | +import jdk.jfr.consumer.RecordedStackTrace; |
| 20 | +import jdk.jfr.consumer.RecordingFile; |
| 21 | +import picocli.CommandLine; |
| 22 | +import picocli.CommandLine.Command; |
| 23 | +import picocli.CommandLine.Parameters; |
| 24 | + |
| 25 | +/** |
| 26 | + * A helper class to parse Java Flight Recorder (JFR) files |
| 27 | + * and extract insights from custom Infinispan ProtoStream events. |
| 28 | + * |
| 29 | + * It specifically looks for 'org.infinispan.protostream.ResizeEvent' |
| 30 | + * and 'org.infinispan.protostream.AllocateEvent' to generate a |
| 31 | + * summary about buffer allocation and resizing patterns. |
| 32 | + */ |
| 33 | +@Command(name = "parser", mixinStandardHelpOptions = true, version = "parser 0.1", |
| 34 | + description = "parser made with jbang") |
| 35 | +class parser implements Callable<Integer> { |
| 36 | + |
| 37 | + @Parameters(index = "0", description = "Path to JFR file") |
| 38 | + private File file; |
| 39 | + |
| 40 | + public static void main(String... args) { |
| 41 | + int exitCode = new CommandLine(new parser()).execute(args); |
| 42 | + System.exit(exitCode); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public Integer call() throws Exception { |
| 47 | + Path p = file.toPath(); |
| 48 | + try (PrintWriter writer = new PrintWriter(System.out)) { |
| 49 | + parseAndSummarize(p, writer); |
| 50 | + } |
| 51 | + return 0; |
| 52 | + } |
| 53 | + |
| 54 | + private static final String RESIZE_EVENT_NAME = "org.infinispan.protostream.ResizeEvent"; |
| 55 | + private static final String ALLOCATE_EVENT_NAME = "org.infinispan.protostream.AllocateEvent"; |
| 56 | + |
| 57 | + /** |
| 58 | + * Parses a JFR file to analyze ProtoStream buffer events and prints a summary. |
| 59 | + * |
| 60 | + * @param jfrFilePath The path to the JFR file. |
| 61 | + * @param writer The PrintWriter to which the summary report will be written. |
| 62 | + * @throws IOException If an error occurs while reading the JFR file. |
| 63 | + */ |
| 64 | + public void parseAndSummarize(Path jfrFilePath, PrintWriter writer) throws IOException { |
| 65 | + if (jfrFilePath == null || writer == null) { |
| 66 | + throw new IllegalArgumentException("JFR file path and PrintWriter cannot be null."); |
| 67 | + } |
| 68 | + |
| 69 | + // --- Data Collectors --- |
| 70 | + // Metrics for Resize Events |
| 71 | + long resizeEventCount = 0; |
| 72 | + long totalBytesResized = 0; |
| 73 | + int maxResizeFrom = 0; |
| 74 | + int maxResizeTo = 0; |
| 75 | + Map<String, HotSpot> resizeHotspots = new HashMap<>(); |
| 76 | + |
| 77 | + // Metrics for Allocate Events |
| 78 | + long allocateEventCount = 0; |
| 79 | + long totalBytesAllocated = 0; |
| 80 | + int maxAllocationSize = 0; |
| 81 | + Map<String, HotSpot> allocationHotspots = new HashMap<>(); |
| 82 | + |
| 83 | + try (RecordingFile recordingFile = new RecordingFile(jfrFilePath)) { |
| 84 | + while (recordingFile.hasMoreEvents()) { |
| 85 | + RecordedEvent event = recordingFile.readEvent(); |
| 86 | + |
| 87 | + switch (event.getEventType().getName()) { |
| 88 | + case RESIZE_EVENT_NAME: |
| 89 | + resizeEventCount++; |
| 90 | + int fromSize = event.getValue("before"); |
| 91 | + int toSize = event.getValue("after"); |
| 92 | + totalBytesResized += (long) toSize - fromSize; |
| 93 | + |
| 94 | + if (toSize > maxResizeTo) { |
| 95 | + maxResizeTo = toSize; |
| 96 | + maxResizeFrom = fromSize; |
| 97 | + } |
| 98 | + |
| 99 | + // Use the first frame of the stack trace as the hotspot identifier |
| 100 | + Optional.ofNullable(event.getStackTrace()).ifPresent(stackTrace -> { |
| 101 | + if (!stackTrace.getFrames().isEmpty()) { |
| 102 | + String topFrame = stackTrace.getFrames().get(0).toString(); |
| 103 | + resizeHotspots.computeIfAbsent(topFrame, k -> new HotSpot(stackTrace)).inc(); |
| 104 | + } |
| 105 | + }); |
| 106 | + break; |
| 107 | + |
| 108 | + case ALLOCATE_EVENT_NAME: |
| 109 | + allocateEventCount++; |
| 110 | + int newSize = event.getValue("size"); |
| 111 | + totalBytesAllocated += newSize; |
| 112 | + |
| 113 | + if (newSize > maxAllocationSize) { |
| 114 | + maxAllocationSize = newSize; |
| 115 | + } |
| 116 | + |
| 117 | + Optional.ofNullable(event.getStackTrace()).ifPresent(stackTrace -> { |
| 118 | + if (!stackTrace.getFrames().isEmpty()) { |
| 119 | + String topFrame = stackTrace.getFrames().get(0).toString(); |
| 120 | + allocationHotspots.computeIfAbsent(topFrame, k -> new HotSpot(stackTrace)).inc(); |
| 121 | + } |
| 122 | + }); |
| 123 | + break; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // --- Generate Summary Report --- |
| 129 | + generateReport( |
| 130 | + writer, |
| 131 | + resizeEventCount, totalBytesResized, maxResizeFrom, maxResizeTo, resizeHotspots, |
| 132 | + allocateEventCount, totalBytesAllocated, maxAllocationSize, allocationHotspots |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + private void generateReport(PrintWriter writer, |
| 137 | + long resizeEventCount, long totalBytesResized, int maxResizeFrom, int maxResizeTo, Map<String, HotSpot> resizeHotspots, |
| 138 | + long allocateEventCount, long totalBytesAllocated, int maxAllocationSize, Map<String, HotSpot> allocationHotspots) { |
| 139 | + |
| 140 | + writer.println("========================================================="); |
| 141 | + writer.println(" Infinispan ProtoStream Buffer Events JFR Summary "); |
| 142 | + writer.println("========================================================="); |
| 143 | + writer.println(); |
| 144 | + |
| 145 | + // --- Resize Events Section --- |
| 146 | + writer.println("--- Buffer Resize Events (" + RESIZE_EVENT_NAME + ") ---"); |
| 147 | + if (resizeEventCount > 0) { |
| 148 | + writer.printf("Total Resize Events: %,d%n", resizeEventCount); |
| 149 | + writer.printf("Total Bytes Added by Resizing: %,d bytes%n", totalBytesResized); |
| 150 | + writer.printf("Average Resize Increase: %,.2f bytes%n", (double) totalBytesResized / resizeEventCount); |
| 151 | + writer.printf("Largest Single Resize: from %,d to %,d bytes (an increase of %,d bytes)%n", maxResizeFrom, maxResizeTo, maxResizeTo - maxResizeFrom); |
| 152 | + writer.println(); |
| 153 | + writer.println("Top 5 Most Common Resize Locations (Stack Trace):"); |
| 154 | + printTopHotspots(writer, resizeHotspots, 5); |
| 155 | + } else { |
| 156 | + writer.println("No resize events found in this recording."); |
| 157 | + } |
| 158 | + writer.println(); |
| 159 | + |
| 160 | + // --- Allocate Events Section --- |
| 161 | + writer.println("--- Buffer Allocate Events (" + ALLOCATE_EVENT_NAME + ") ---"); |
| 162 | + if (allocateEventCount > 0) { |
| 163 | + writer.printf("Total Allocation Events: %,d%n", allocateEventCount); |
| 164 | + writer.printf("Total Bytes Allocated: %,d bytes%n", totalBytesAllocated); |
| 165 | + writer.printf("Average Allocation Size: %,.2f bytes%n", (double) totalBytesAllocated / allocateEventCount); |
| 166 | + writer.printf("Largest Single Allocation: %,d bytes%n", maxAllocationSize); |
| 167 | + writer.println(); |
| 168 | + writer.println("Top 5 Most Common Allocation Locations (Stack Trace):"); |
| 169 | + printTopHotspots(writer, allocationHotspots, 5); |
| 170 | + } else { |
| 171 | + writer.println("No allocation events found in this recording."); |
| 172 | + } |
| 173 | + writer.println(); |
| 174 | + writer.println("========================================================="); |
| 175 | + writer.flush(); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Helper to sort and print the most frequent call sites. |
| 180 | + */ |
| 181 | + private void printTopHotspots(PrintWriter writer, Map<String, HotSpot> hotspots, int limit) { |
| 182 | + if (hotspots.isEmpty()) { |
| 183 | + writer.println(" (No stack trace information available)"); |
| 184 | + return; |
| 185 | + } |
| 186 | + |
| 187 | + // Sort the map by value (count) in descending order |
| 188 | + LinkedHashMap<String, HotSpot> sortedHotspots = hotspots.entrySet() |
| 189 | + .stream() |
| 190 | + .sorted(Map.Entry.comparingByValue((a, b) -> Integer.compare(b.times(), a.times()))) |
| 191 | + .collect(Collectors.toMap( |
| 192 | + Map.Entry::getKey, |
| 193 | + Map.Entry::getValue, |
| 194 | + (e1, e2) -> e1, |
| 195 | + LinkedHashMap::new |
| 196 | + )); |
| 197 | + |
| 198 | + int count = 0; |
| 199 | + for (Map.Entry<String, HotSpot> entry : sortedHotspots.entrySet()) { |
| 200 | + if (count++ >= limit) { |
| 201 | + break; |
| 202 | + } |
| 203 | + HotSpot hs = entry.getValue(); |
| 204 | + StringBuilder sb = new StringBuilder(); |
| 205 | + boolean first = true; |
| 206 | + for (RecordedFrame frame : hs.stackTrace.getFrames()) { |
| 207 | + if (first) { |
| 208 | + sb.append(frameToString(frame)); |
| 209 | + first = false; |
| 210 | + } else { |
| 211 | + sb.append('\t').append("at ").append(frameToString(frame)); |
| 212 | + } |
| 213 | + sb.append(System.lineSeparator()); |
| 214 | + } |
| 215 | + writer.printf(" - [%,d times]:%n%s%n", hs.times(), sb); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + private String frameToString(RecordedFrame frame) { |
| 220 | + RecordedMethod method = frame.getMethod(); |
| 221 | + return String.format("%s.%s:%d [%s]", method.getType().getName(), method.getName(), frame.getLineNumber(), frame.getType()); |
| 222 | + } |
| 223 | + |
| 224 | + private static class HotSpot { |
| 225 | + private final RecordedStackTrace stackTrace; |
| 226 | + private int times; |
| 227 | + |
| 228 | + private HotSpot(RecordedStackTrace stackTrace) { |
| 229 | + this.stackTrace = stackTrace; |
| 230 | + } |
| 231 | + |
| 232 | + public void inc() { |
| 233 | + times++; |
| 234 | + } |
| 235 | + |
| 236 | + public int times() { |
| 237 | + return times; |
| 238 | + } |
| 239 | + } |
| 240 | +} |
0 commit comments