JfrResultJsonSerializer .java

net.minecraft.util.profiling.jfr.serialize.JfrResultJsonSerializer (class): members: DURATION_NANOS_TOTAL, BYTES_PER_SECOND, COUNT_PER_SECOND, TOTAL_BYTES.

net.minecraft.util.profiling.jfr.serializeclass279 linescommon13.7 KB

API Surface (1)

String format(final JfrStatsResult jfrStats)

Fields & Constants (6)

BYTES_PER_SECOND
COUNT
DURATION_NANOS_TOTAL
TOTAL_BYTES
COUNT_PER_SECOND
gson

String literals (59)

"bytesPerSecond"
"count"
"durationNanosTotal"
"totalBytes"
"countPerSecond"
"protocolId"
"packetId"
"level"
"dimension"
"startedEpoch"
"endedEpoch"
"durationMs"
"worldGenDurationMs"
"heap"
"cpuPercent"
"network"
"fileIO"
"serverTick"
"threadAllocation"
"chunkGen"
"structureGen"
"allocationRateBytesPerSecond"
"gcCount"
"gcOverHeadPercent"

Same Package (1)

Full Source

JfrResultJsonSerializer.java279 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.profiling.jfr.serialize;
2
3
import com.google.gson.Gson;
4
import com.google.gson.GsonBuilder;
5
import com.google.gson.JsonArray;
6
import com.google.gson.JsonElement;
7
import com.google.gson.JsonNull;
8
import com.google.gson.JsonObject;
9
import com.google.gson.LongSerializationPolicy;
10
import com.mojang.datafixers.util.Pair;
11
import java.time.Duration;
12
import java.util.DoubleSummaryStatistics;
13
import java.util.IntSummaryStatistics;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.Optional;
17
import java.util.function.BiConsumer;
18
import java.util.function.BiFunction;
19
import java.util.function.Function;
20
import java.util.function.ToDoubleFunction;
21
import java.util.stream.Collectors;
22
import java.util.stream.DoubleStream;
23
import java.util.stream.IntStream;
24
import net.minecraft.util.Util;
25
import net.minecraft.util.profiling.jfr.Percentiles;
26
import net.minecraft.util.profiling.jfr.parse.JfrStatsResult;
27
import net.minecraft.util.profiling.jfr.stats.ChunkGenStat;
28
import net.minecraft.util.profiling.jfr.stats.ChunkIdentification;
29
import net.minecraft.util.profiling.jfr.stats.CpuLoadStat;
30
import net.minecraft.util.profiling.jfr.stats.FileIOStat;
31
import net.minecraft.util.profiling.jfr.stats.FpsStat;
32
import net.minecraft.util.profiling.jfr.stats.GcHeapStat;
33
import net.minecraft.util.profiling.jfr.stats.IoSummary;
34
import net.minecraft.util.profiling.jfr.stats.PacketIdentification;
35
import net.minecraft.util.profiling.jfr.stats.StructureGenStat;
36
import net.minecraft.util.profiling.jfr.stats.ThreadAllocationStat;
37
import net.minecraft.util.profiling.jfr.stats.TickTimeStat;
38
import net.minecraft.util.profiling.jfr.stats.TimedStatSummary;
39
import net.minecraft.world.level.chunk.status.ChunkStatus;
40
41
public class JfrResultJsonSerializer {
42
	private static final String BYTES_PER_SECOND = "bytesPerSecond";
43
	private static final String COUNT = "count";
44
	private static final String DURATION_NANOS_TOTAL = "durationNanosTotal";
45
	private static final String TOTAL_BYTES = "totalBytes";
46
	private static final String COUNT_PER_SECOND = "countPerSecond";
47
	private final Gson gson = new GsonBuilder().setPrettyPrinting().setLongSerializationPolicy(LongSerializationPolicy.DEFAULT).create();
48
49
	private static void serializePacketId(final PacketIdentification identifier, final JsonObject output) {
50
		output.addProperty("protocolId", identifier.protocolId());
51
		output.addProperty("packetId", identifier.packetId());
52
	}
53
54
	private static void serializeChunkId(final ChunkIdentification identifier, final JsonObject output) {
55
		output.addProperty("level", identifier.level());
56
		output.addProperty("dimension", identifier.dimension());
57
		output.addProperty("x", identifier.x());
58
		output.addProperty("z", identifier.z());
59
	}
60
61
	public String format(final JfrStatsResult jfrStats) {
62
		JsonObject root = new JsonObject();
63
		root.addProperty("startedEpoch", jfrStats.recordingStarted().toEpochMilli());
64
		root.addProperty("endedEpoch", jfrStats.recordingEnded().toEpochMilli());
65
		root.addProperty("durationMs", jfrStats.recordingDuration().toMillis());
66
		Duration worldCreationDuration = jfrStats.worldCreationDuration();
67
		if (worldCreationDuration != null) {
68
			root.addProperty("worldGenDurationMs", worldCreationDuration.toMillis());
69
		}
70
71
		root.add("heap", this.heap(jfrStats.heapSummary()));
72
		root.add("cpuPercent", this.cpu(jfrStats.cpuLoadStats()));
73
		root.add("network", this.network(jfrStats));
74
		root.add("fileIO", this.fileIO(jfrStats));
75
		root.add("fps", this.fps(jfrStats.fps()));
76
		root.add("serverTick", this.serverTicks(jfrStats.serverTickTimes()));
77
		root.add("threadAllocation", this.threadAllocations(jfrStats.threadAllocationSummary()));
78
		root.add("chunkGen", this.chunkGen(jfrStats.chunkGenSummary()));
79
		root.add("structureGen", this.structureGen(jfrStats.structureGenStats()));
80
		return this.gson.toJson(root);
81
	}
82
83
	private JsonElement heap(final GcHeapStat.Summary heapSummary) {
84
		JsonObject json = new JsonObject();
85
		json.addProperty("allocationRateBytesPerSecond", heapSummary.allocationRateBytesPerSecond());
86
		json.addProperty("gcCount", heapSummary.totalGCs());
87
		json.addProperty("gcOverHeadPercent", heapSummary.gcOverHead());
88
		json.addProperty("gcTotalDurationMs", heapSummary.gcTotalDuration().toMillis());
89
		return json;
90
	}
91
92
	private JsonElement structureGen(final List<StructureGenStat> structureGenStats) {
93
		JsonObject root = new JsonObject();
94
		Optional<TimedStatSummary<StructureGenStat>> optionalSummary = TimedStatSummary.summary(structureGenStats);
95
		if (optionalSummary.isEmpty()) {
96
			return root;
97
		}
98
99
		TimedStatSummary<StructureGenStat> summary = optionalSummary.get();
100
		JsonArray structureJsonArray = new JsonArray();
101
		root.add("structure", structureJsonArray);
102
		structureGenStats.stream().collect(Collectors.groupingBy(StructureGenStat::structureName)).forEach((structureName, timedStat) -> {
103
			Optional<TimedStatSummary<StructureGenStat>> optionalStatSummary = TimedStatSummary.summary((List<StructureGenStat>)timedStat);
104
			if (!optionalStatSummary.isEmpty()) {
105
				TimedStatSummary<StructureGenStat> statSummary = optionalStatSummary.get();
106
				JsonObject structureJson = new JsonObject();
107
				structureJsonArray.add(structureJson);
108
				structureJson.addProperty("name", structureName);
109
				structureJson.addProperty("count", statSummary.count());
110
				structureJson.addProperty("durationNanosTotal", statSummary.totalDuration().toNanos());
111
				structureJson.addProperty("durationNanosAvg", statSummary.totalDuration().toNanos() / statSummary.count());
112
				JsonObject percentiles = Util.make(new JsonObject(), self -> structureJson.add("durationNanosPercentiles", self));
113
				statSummary.percentilesNanos().forEach((percentile, v) -> percentiles.addProperty("p" + percentile, v));
114
				Function<StructureGenStat, JsonElement> structureGenStatJsonGenerator = structureGen -> {
115
					JsonObject result = new JsonObject();
116
					result.addProperty("durationNanos", structureGen.duration().toNanos());
117
					result.addProperty("chunkPosX", structureGen.chunkPos().x());
118
					result.addProperty("chunkPosZ", structureGen.chunkPos().z());
119
					result.addProperty("structureName", structureGen.structureName());
120
					result.addProperty("level", structureGen.level());
121
					result.addProperty("success", structureGen.success());
122
					return result;
123
				};
124
				root.add("fastest", structureGenStatJsonGenerator.apply(summary.fastest()));
125
				root.add("slowest", structureGenStatJsonGenerator.apply(summary.slowest()));
126
				root.add("secondSlowest", summary.secondSlowest() != null ? structureGenStatJsonGenerator.apply(summary.secondSlowest()) : JsonNull.INSTANCE);
127
			}
128
		});
129
		return root;
130
	}
131
132
	private JsonElement chunkGen(final List<Pair<ChunkStatus, TimedStatSummary<ChunkGenStat>>> chunkGenSummary) {
133
		JsonObject json = new JsonObject();
134
		if (chunkGenSummary.isEmpty()) {
135
			return json;
136
		}
137
138
		json.addProperty("durationNanosTotal", chunkGenSummary.stream().mapToDouble(it -> it.getSecond().totalDuration().toNanos()).sum());
139
		JsonArray chunkJsonArray = Util.make(new JsonArray(), self -> json.add("status", self));
140
141
		for (Pair<ChunkStatus, TimedStatSummary<ChunkGenStat>> summaryByStatus : chunkGenSummary) {
142
			TimedStatSummary<ChunkGenStat> chunkStat = summaryByStatus.getSecond();
143
			JsonObject chunkStatusJson = Util.make(new JsonObject(), chunkJsonArray::add);
144
			chunkStatusJson.addProperty("state", summaryByStatus.getFirst().toString());
145
			chunkStatusJson.addProperty("count", chunkStat.count());
146
			chunkStatusJson.addProperty("durationNanosTotal", chunkStat.totalDuration().toNanos());
147
			chunkStatusJson.addProperty("durationNanosAvg", chunkStat.totalDuration().toNanos() / chunkStat.count());
148
			JsonObject percentiles = Util.make(new JsonObject(), self -> chunkStatusJson.add("durationNanosPercentiles", self));
149
			chunkStat.percentilesNanos().forEach((percentile, value) -> percentiles.addProperty("p" + percentile, value));
150
			Function<ChunkGenStat, JsonElement> chunkGenStatJsonGenerator = chunk -> {
151
				JsonObject chunkGenStatJson = new JsonObject();
152
				chunkGenStatJson.addProperty("durationNanos", chunk.duration().toNanos());
153
				chunkGenStatJson.addProperty("level", chunk.level());
154
				chunkGenStatJson.addProperty("chunkPosX", chunk.chunkPos().x());
155
				chunkGenStatJson.addProperty("chunkPosZ", chunk.chunkPos().z());
156
				chunkGenStatJson.addProperty("worldPosX", chunk.worldPos().x());
157
				chunkGenStatJson.addProperty("worldPosZ", chunk.worldPos().z());
158
				return chunkGenStatJson;
159
			};
160
			chunkStatusJson.add("fastest", chunkGenStatJsonGenerator.apply(chunkStat.fastest()));
161
			chunkStatusJson.add("slowest", chunkGenStatJsonGenerator.apply(chunkStat.slowest()));
162
			chunkStatusJson.add("secondSlowest", chunkStat.secondSlowest() != null ? chunkGenStatJsonGenerator.apply(chunkStat.secondSlowest()) : JsonNull.INSTANCE);
163
		}
164
165
		return json;
166
	}
167
168
	private JsonElement threadAllocations(final ThreadAllocationStat.Summary threadAllocationSummary) {
169
		JsonArray threads = new JsonArray();
170
		threadAllocationSummary.allocationsPerSecondByThread().forEach((threadName, bytesPerSecond) -> threads.add(Util.make(new JsonObject(), json -> {
171
			json.addProperty("thread", threadName);
172
			json.addProperty("bytesPerSecond", bytesPerSecond);
173
		})));
174
		return threads;
175
	}
176
177
	private JsonElement serverTicks(final List<TickTimeStat> tickTimeStats) {
178
		if (tickTimeStats.isEmpty()) {
179
			return JsonNull.INSTANCE;
180
		}
181
182
		JsonObject json = new JsonObject();
183
		double[] tickTimesMs = tickTimeStats.stream().mapToDouble(tickTimeStat -> tickTimeStat.currentAverage().toNanos() / 1000000.0).toArray();
184
		DoubleSummaryStatistics summary = DoubleStream.of(tickTimesMs).summaryStatistics();
185
		json.addProperty("minMs", summary.getMin());
186
		json.addProperty("averageMs", summary.getAverage());
187
		json.addProperty("maxMs", summary.getMax());
188
		Map<Integer, Double> percentiles = Percentiles.evaluate(tickTimesMs);
189
		percentiles.forEach((percentile, value) -> json.addProperty("p" + percentile, value));
190
		return json;
191
	}
192
193
	private JsonElement fps(final List<FpsStat> fpsStats) {
194
		if (fpsStats.isEmpty()) {
195
			return JsonNull.INSTANCE;
196
		}
197
198
		JsonObject json = new JsonObject();
199
		int[] fps = fpsStats.stream().mapToInt(FpsStat::fps).toArray();
200
		IntSummaryStatistics summary = IntStream.of(fps).summaryStatistics();
201
		json.addProperty("minFps", summary.getMin());
202
		json.addProperty("averageFps", summary.getAverage());
203
		json.addProperty("maxFps", summary.getMax());
204
		Map<Integer, Double> percentiles = Percentiles.evaluate(fps);
205
		percentiles.forEach((percentile, value) -> json.addProperty("p" + percentile, value));
206
		return json;
207
	}
208
209
	private JsonElement fileIO(final JfrStatsResult jfrStats) {
210
		JsonObject json = new JsonObject();
211
		json.add("write", this.fileIoSummary(jfrStats.fileWrites()));
212
		json.add("read", this.fileIoSummary(jfrStats.fileReads()));
213
		json.add("chunksRead", this.ioSummary(jfrStats.readChunks(), JfrResultJsonSerializer::serializeChunkId));
214
		json.add("chunksWritten", this.ioSummary(jfrStats.writtenChunks(), JfrResultJsonSerializer::serializeChunkId));
215
		return json;
216
	}
217
218
	private JsonElement fileIoSummary(final FileIOStat.Summary io) {
219
		JsonObject json = new JsonObject();
220
		json.addProperty("totalBytes", io.totalBytes());
221
		json.addProperty("count", io.counts());
222
		json.addProperty("bytesPerSecond", io.bytesPerSecond());
223
		json.addProperty("countPerSecond", io.countsPerSecond());
224
		JsonArray topContributors = new JsonArray();
225
		json.add("topContributors", topContributors);
226
		io.topTenContributorsByTotalBytes().forEach(contributor -> {
227
			JsonObject contributorJson = new JsonObject();
228
			topContributors.add(contributorJson);
229
			contributorJson.addProperty("path", contributor.getFirst());
230
			contributorJson.addProperty("totalBytes", contributor.getSecond());
231
		});
232
		return json;
233
	}
234
235
	private JsonElement network(final JfrStatsResult jfrStats) {
236
		JsonObject json = new JsonObject();
237
		json.add("sent", this.ioSummary(jfrStats.sentPacketsSummary(), JfrResultJsonSerializer::serializePacketId));
238
		json.add("received", this.ioSummary(jfrStats.receivedPacketsSummary(), JfrResultJsonSerializer::serializePacketId));
239
		return json;
240
	}
241
242
	private <T> JsonElement ioSummary(final IoSummary<T> summary, final BiConsumer<T, JsonObject> elementWriter) {
243
		JsonObject json = new JsonObject();
244
		json.addProperty("totalBytes", summary.getTotalSize());
245
		json.addProperty("count", summary.getTotalCount());
246
		json.addProperty("bytesPerSecond", summary.getSizePerSecond());
247
		json.addProperty("countPerSecond", summary.getCountsPerSecond());
248
		JsonArray topContributors = new JsonArray();
249
		json.add("topContributors", topContributors);
250
		summary.largestSizeContributors().forEach(contributor -> {
251
			JsonObject contributorJson = new JsonObject();
252
			topContributors.add(contributorJson);
253
			T identifier = contributor.getFirst();
254
			IoSummary.CountAndSize countAndSize = contributor.getSecond();
255
			elementWriter.accept(identifier, contributorJson);
256
			contributorJson.addProperty("totalBytes", countAndSize.totalSize());
257
			contributorJson.addProperty("count", countAndSize.totalCount());
258
			contributorJson.addProperty("averageSize", countAndSize.averageSize());
259
		});
260
		return json;
261
	}
262
263
	private JsonElement cpu(final List<CpuLoadStat> cpuStats) {
264
		JsonObject json = new JsonObject();
265
		BiFunction<List<CpuLoadStat>, ToDoubleFunction<CpuLoadStat>, JsonObject> transformer = (cpuLoadStats, extractor) -> {
266
			JsonObject jsonGroup = new JsonObject();
267
			DoubleSummaryStatistics stats = cpuLoadStats.stream().mapToDouble(extractor).summaryStatistics();
268
			jsonGroup.addProperty("min", stats.getMin());
269
			jsonGroup.addProperty("average", stats.getAverage());
270
			jsonGroup.addProperty("max", stats.getMax());
271
			return jsonGroup;
272
		};
273
		json.add("jvm", transformer.apply(cpuStats, CpuLoadStat::jvm));
274
		json.add("userJvm", transformer.apply(cpuStats, CpuLoadStat::userJvm));
275
		json.add("system", transformer.apply(cpuStats, CpuLoadStat::system));
276
		return json;
277
	}
278
}
279

Decompiled Minecraft 26.3 source of JfrResultJsonSerializer (net/minecraft/util/profiling/jfr/serialize/JfrResultJsonSerializer.java) with Mojang official mappings, stored verbatim on the site.