GcHeapStat .java

net.minecraft.util.profiling.jfr.stats.GcHeapStat (record): members: timestamp, heapUsed, timing, calculateAllocationRatePerSecond, Timing, Summary.

net.minecraft.util.profiling.jfr.statsrecord52 linescommon1.9 KB

API Surface (5)

record GcHeapStat(Instant timestamp, long heapUsed, GcHeapStat.Timing timing)
GcHeapStat from(final RecordedEvent event)
GcHeapStat.Summary summary(final Duration recordingDuration, final List<GcHeapStat> heapStats, final Duration gcTotalDuration, final int totalGCs)
record Summary(Duration duration, Duration gcTotalDuration, int totalGCs, double allocationRateBytesPerSecond)
float gcOverHead()

Fields & Constants (1)

gcTotalDuration

String literals (3)

"heapUsed"
"when"
"before gc"

Code Graph: Dependencies (1)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

GcHeapStat.java52 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.profiling.jfr.stats;
2
3
import java.time.Duration;
4
import java.time.Instant;
5
import java.util.List;
6
import java.util.Map;
7
import java.util.stream.Collectors;
8
import jdk.jfr.consumer.RecordedEvent;
9
10
public record GcHeapStat(Instant timestamp, long heapUsed, GcHeapStat.Timing timing) {
11
	public static GcHeapStat from(final RecordedEvent event) {
12
		return new GcHeapStat(
13
			event.getStartTime(),
14
			event.getLong("heapUsed"),
15
			event.getString("when").equalsIgnoreCase("before gc") ? GcHeapStat.Timing.BEFORE_GC : GcHeapStat.Timing.AFTER_GC
16
		);
17
	}
18
19
	public static GcHeapStat.Summary summary(
20
		final Duration recordingDuration, final List<GcHeapStat> heapStats, final Duration gcTotalDuration, final int totalGCs
21
	) {
22
		return new GcHeapStat.Summary(recordingDuration, gcTotalDuration, totalGCs, calculateAllocationRatePerSecond(heapStats));
23
	}
24
25
	private static double calculateAllocationRatePerSecond(final List<GcHeapStat> heapStats) {
26
		long totalAllocations = 0L;
27
		Map<GcHeapStat.Timing, List<GcHeapStat>> byTiming = heapStats.stream().collect(Collectors.groupingBy(it -> it.timing));
28
		List<GcHeapStat> beforeGcs = byTiming.get(GcHeapStat.Timing.BEFORE_GC);
29
		List<GcHeapStat> afterGcs = byTiming.get(GcHeapStat.Timing.AFTER_GC);
30
31
		for (int i = 1; i < beforeGcs.size(); i++) {
32
			GcHeapStat beforeGC = beforeGcs.get(i);
33
			GcHeapStat previousGC = afterGcs.get(i - 1);
34
			totalAllocations += beforeGC.heapUsed - previousGC.heapUsed;
35
		}
36
37
		Duration totalDuration = Duration.between(heapStats.get(1).timestamp, heapStats.get(heapStats.size() - 1).timestamp);
38
		return (double)totalAllocations / totalDuration.getSeconds();
39
	}
40
41
	public record Summary(Duration duration, Duration gcTotalDuration, int totalGCs, double allocationRateBytesPerSecond) {
42
		public float gcOverHead() {
43
			return (float)this.gcTotalDuration.toMillis() / (float)this.duration.toMillis();
44
		}
45
	}
46
47
	public enum Timing {
48
		BEFORE_GC,
49
		AFTER_GC;
50
	}
51
}
52

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