| 1 | package net.minecraft.util.profiling.jfr.parse;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Pair;
|
| 4 | import java.io.IOException;
|
| 5 | import java.io.UncheckedIOException;
|
| 6 | import java.nio.file.Path;
|
| 7 | import java.time.Duration;
|
| 8 | import java.time.Instant;
|
| 9 | import java.util.ArrayList;
|
| 10 | import java.util.HashMap;
|
| 11 | import java.util.Iterator;
|
| 12 | import java.util.List;
|
| 13 | import java.util.Map;
|
| 14 | import java.util.NoSuchElementException;
|
| 15 | import java.util.Spliterators;
|
| 16 | import java.util.stream.Stream;
|
| 17 | import java.util.stream.StreamSupport;
|
| 18 | import jdk.jfr.consumer.RecordedEvent;
|
| 19 | import jdk.jfr.consumer.RecordingFile;
|
| 20 | import net.minecraft.util.profiling.jfr.stats.ChunkGenStat;
|
| 21 | import net.minecraft.util.profiling.jfr.stats.ChunkIdentification;
|
| 22 | import net.minecraft.util.profiling.jfr.stats.CpuLoadStat;
|
| 23 | import net.minecraft.util.profiling.jfr.stats.FileIOStat;
|
| 24 | import net.minecraft.util.profiling.jfr.stats.FpsStat;
|
| 25 | import net.minecraft.util.profiling.jfr.stats.GcHeapStat;
|
| 26 | import net.minecraft.util.profiling.jfr.stats.IoSummary;
|
| 27 | import net.minecraft.util.profiling.jfr.stats.PacketIdentification;
|
| 28 | import net.minecraft.util.profiling.jfr.stats.StructureGenStat;
|
| 29 | import net.minecraft.util.profiling.jfr.stats.ThreadAllocationStat;
|
| 30 | import net.minecraft.util.profiling.jfr.stats.TickTimeStat;
|
| 31 | import org.jspecify.annotations.Nullable;
|
| 32 |
|
| 33 | public class JfrStatsParser {
|
| 34 | private Instant recordingStarted = Instant.EPOCH;
|
| 35 | private Instant recordingEnded = Instant.EPOCH;
|
| 36 | private final List<ChunkGenStat> chunkGenStats = new ArrayList<>();
|
| 37 | private final List<StructureGenStat> structureGenStats = new ArrayList<>();
|
| 38 | private final List<CpuLoadStat> cpuLoadStat = new ArrayList<>();
|
| 39 | private final Map<PacketIdentification, JfrStatsParser.MutableCountAndSize> receivedPackets = new HashMap<>();
|
| 40 | private final Map<PacketIdentification, JfrStatsParser.MutableCountAndSize> sentPackets = new HashMap<>();
|
| 41 | private final Map<ChunkIdentification, JfrStatsParser.MutableCountAndSize> readChunks = new HashMap<>();
|
| 42 | private final Map<ChunkIdentification, JfrStatsParser.MutableCountAndSize> writtenChunks = new HashMap<>();
|
| 43 | private final List<FileIOStat> fileWrites = new ArrayList<>();
|
| 44 | private final List<FileIOStat> fileReads = new ArrayList<>();
|
| 45 | private int garbageCollections;
|
| 46 | private Duration gcTotalDuration = Duration.ZERO;
|
| 47 | private final List<GcHeapStat> gcHeapStats = new ArrayList<>();
|
| 48 | private final List<ThreadAllocationStat> threadAllocationStats = new ArrayList<>();
|
| 49 | private final List<FpsStat> fps = new ArrayList<>();
|
| 50 | private final List<TickTimeStat> serverTickTimes = new ArrayList<>();
|
| 51 | private @Nullable Duration worldCreationDuration = null;
|
| 52 |
|
| 53 | private JfrStatsParser(final Stream<RecordedEvent> events) {
|
| 54 | this.capture(events);
|
| 55 | }
|
| 56 |
|
| 57 | public static JfrStatsResult parse(final Path path) {
|
| 58 | try (final RecordingFile recordingFile = new RecordingFile(path)) {
|
| 59 | Iterator<RecordedEvent> iterator = new Iterator<RecordedEvent>() {
|
| 60 | @Override
|
| 61 | public boolean hasNext() {
|
| 62 | return recordingFile.hasMoreEvents();
|
| 63 | }
|
| 64 |
|
| 65 | public RecordedEvent next() {
|
| 66 | if (!this.hasNext()) {
|
| 67 | throw new NoSuchElementException();
|
| 68 | }
|
| 69 |
|
| 70 | try {
|
| 71 | return recordingFile.readEvent();
|
| 72 | } catch (IOException e) {
|
| 73 | throw new UncheckedIOException(e);
|
| 74 | }
|
| 75 | }
|
| 76 | };
|
| 77 | Stream<RecordedEvent> events = StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 1297), false);
|
| 78 | return new JfrStatsParser(events).results();
|
| 79 | } catch (IOException e) {
|
| 80 | throw new UncheckedIOException(e);
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | private JfrStatsResult results() {
|
| 85 | Duration recordingDuration = Duration.between(this.recordingStarted, this.recordingEnded);
|
| 86 | return new JfrStatsResult(
|
| 87 | this.recordingStarted,
|
| 88 | this.recordingEnded,
|
| 89 | recordingDuration,
|
| 90 | this.worldCreationDuration,
|
| 91 | this.fps,
|
| 92 | this.serverTickTimes,
|
| 93 | this.cpuLoadStat,
|
| 94 | GcHeapStat.summary(recordingDuration, this.gcHeapStats, this.gcTotalDuration, this.garbageCollections),
|
| 95 | ThreadAllocationStat.summary(this.threadAllocationStats),
|
| 96 | collectIoStats(recordingDuration, this.receivedPackets),
|
| 97 | collectIoStats(recordingDuration, this.sentPackets),
|
| 98 | collectIoStats(recordingDuration, this.writtenChunks),
|
| 99 | collectIoStats(recordingDuration, this.readChunks),
|
| 100 | FileIOStat.summary(recordingDuration, this.fileWrites),
|
| 101 | FileIOStat.summary(recordingDuration, this.fileReads),
|
| 102 | this.chunkGenStats,
|
| 103 | this.structureGenStats
|
| 104 | );
|
| 105 | }
|
| 106 |
|
| 107 | private void capture(final Stream<RecordedEvent> events) {
|
| 108 | events.forEach(event -> {
|
| 109 | if (event.getEndTime().isAfter(this.recordingEnded) || this.recordingEnded.equals(Instant.EPOCH)) {
|
| 110 | this.recordingEnded = event.getEndTime();
|
| 111 | }
|
| 112 |
|
| 113 | if (event.getStartTime().isBefore(this.recordingStarted) || this.recordingStarted.equals(Instant.EPOCH)) {
|
| 114 | this.recordingStarted = event.getStartTime();
|
| 115 | }
|
| 116 |
|
| 117 | switch (event.getEventType().getName()) {
|
| 118 | case "minecraft.ChunkGeneration":
|
| 119 | this.chunkGenStats.add(ChunkGenStat.from(event));
|
| 120 | break;
|
| 121 | case "minecraft.StructureGeneration":
|
| 122 | this.structureGenStats.add(StructureGenStat.from(event));
|
| 123 | break;
|
| 124 | case "minecraft.LoadWorld":
|
| 125 | this.worldCreationDuration = event.getDuration();
|
| 126 | break;
|
| 127 | case "minecraft.ClientFps":
|
| 128 | this.fps.add(FpsStat.from(event, "fps"));
|
| 129 | break;
|
| 130 | case "minecraft.ServerTickTime":
|
| 131 | this.serverTickTimes.add(TickTimeStat.from(event));
|
| 132 | break;
|
| 133 | case "minecraft.PacketReceived":
|
| 134 | this.incrementPacket(event, event.getInt("bytes"), this.receivedPackets);
|
| 135 | break;
|
| 136 | case "minecraft.PacketSent":
|
| 137 | this.incrementPacket(event, event.getInt("bytes"), this.sentPackets);
|
| 138 | break;
|
| 139 | case "minecraft.ChunkRegionRead":
|
| 140 | this.incrementChunk(event, event.getInt("bytes"), this.readChunks);
|
| 141 | break;
|
| 142 | case "minecraft.ChunkRegionWrite":
|
| 143 | this.incrementChunk(event, event.getInt("bytes"), this.writtenChunks);
|
| 144 | break;
|
| 145 | case "jdk.ThreadAllocationStatistics":
|
| 146 | this.threadAllocationStats.add(ThreadAllocationStat.from(event));
|
| 147 | break;
|
| 148 | case "jdk.GCHeapSummary":
|
| 149 | this.gcHeapStats.add(GcHeapStat.from(event));
|
| 150 | break;
|
| 151 | case "jdk.CPULoad":
|
| 152 | this.cpuLoadStat.add(CpuLoadStat.from(event));
|
| 153 | break;
|
| 154 | case "jdk.FileWrite":
|
| 155 | this.appendFileIO(event, this.fileWrites, "bytesWritten");
|
| 156 | break;
|
| 157 | case "jdk.FileRead":
|
| 158 | this.appendFileIO(event, this.fileReads, "bytesRead");
|
| 159 | break;
|
| 160 | case "jdk.GarbageCollection":
|
| 161 | this.garbageCollections++;
|
| 162 | this.gcTotalDuration = this.gcTotalDuration.plus(event.getDuration());
|
| 163 | }
|
| 164 | });
|
| 165 | }
|
| 166 |
|
| 167 | private void incrementPacket(final RecordedEvent event, final int packetSize, final Map<PacketIdentification, JfrStatsParser.MutableCountAndSize> packets) {
|
| 168 | packets.computeIfAbsent(PacketIdentification.from(event), ignored -> new JfrStatsParser.MutableCountAndSize()).increment(packetSize);
|
| 169 | }
|
| 170 |
|
| 171 | private void incrementChunk(final RecordedEvent event, final int chunkSize, final Map<ChunkIdentification, JfrStatsParser.MutableCountAndSize> packets) {
|
| 172 | packets.computeIfAbsent(ChunkIdentification.from(event), ignored -> new JfrStatsParser.MutableCountAndSize()).increment(chunkSize);
|
| 173 | }
|
| 174 |
|
| 175 | private void appendFileIO(final RecordedEvent event, final List<FileIOStat> stats, final String sizeField) {
|
| 176 | stats.add(new FileIOStat(event.getDuration(), event.getString("path"), event.getLong(sizeField)));
|
| 177 | }
|
| 178 |
|
| 179 | private static <T> IoSummary<T> collectIoStats(final Duration recordingDuration, final Map<T, JfrStatsParser.MutableCountAndSize> packetStats) {
|
| 180 | List<Pair<T, IoSummary.CountAndSize>> summaryStats = packetStats.entrySet().stream().map(e -> Pair.of(e.getKey(), e.getValue().toCountAndSize())).toList();
|
| 181 | return new IoSummary<>(recordingDuration, summaryStats);
|
| 182 | }
|
| 183 |
|
| 184 | public static final class MutableCountAndSize {
|
| 185 | private long count;
|
| 186 | private long totalSize;
|
| 187 |
|
| 188 | public void increment(final int bytes) {
|
| 189 | this.totalSize += bytes;
|
| 190 | this.count++;
|
| 191 | }
|
| 192 |
|
| 193 | public IoSummary.CountAndSize toCountAndSize() {
|
| 194 | return new IoSummary.CountAndSize(this.count, this.totalSize);
|
| 195 | }
|
| 196 | }
|
| 197 | }
|
| 198 |
|