ThreadAllocationStat .java

net.minecraft.util.profiling.jfr.stats.ThreadAllocationStat (record): members: timestamp, threadName, totalBytes, UNKNOWN_THREAD, Summary, summary, LOGGER.

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

API Surface (4)

record ThreadAllocationStat(Instant timestamp, String threadName, long totalBytes)
ThreadAllocationStat from(final RecordedEvent event)
ThreadAllocationStat.Summary summary(final List<ThreadAllocationStat> stats)
record Summary(Map<String, Double> allocationsPerSecondByThread)

Fields & Constants (2)

LOGGER
UNKNOWN_THREAD

String literals (3)

"unknown"
"thread"
"allocated"

Code Graph: Dependencies (1)

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

Full Source

ThreadAllocationStat.java47 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.profiling.jfr.stats;
2
3
import com.google.common.base.MoreObjects;
4
import com.mojang.logging.LogUtils;
5
import java.time.Duration;
6
import java.time.Instant;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.TreeMap;
10
import java.util.stream.Collectors;
11
import jdk.jfr.consumer.RecordedEvent;
12
import jdk.jfr.consumer.RecordedThread;
13
import org.slf4j.Logger;
14
15
public record ThreadAllocationStat(Instant timestamp, String threadName, long totalBytes) {
16
	public static final Logger LOGGER = LogUtils.getLogger();
17
	private static final String UNKNOWN_THREAD = "unknown";
18
19
	public static ThreadAllocationStat from(final RecordedEvent event) {
20
		RecordedThread recoredThread = event.getThread("thread");
21
		String threadName = recoredThread == null ? "unknown" : MoreObjects.firstNonNull(recoredThread.getJavaName(), "unknown");
22
		return new ThreadAllocationStat(event.getStartTime(), threadName, event.getLong("allocated"));
23
	}
24
25
	public static ThreadAllocationStat.Summary summary(final List<ThreadAllocationStat> stats) {
26
		Map<String, Double> allocationsPerSecondByThread = new TreeMap<>();
27
		Map<String, List<ThreadAllocationStat>> byThread = stats.stream().collect(Collectors.groupingBy(it -> it.threadName));
28
		byThread.forEach((thread, threadStats) -> {
29
			if (threadStats.size() >= 2) {
30
				ThreadAllocationStat first = threadStats.getFirst();
31
				ThreadAllocationStat last = threadStats.getLast();
32
				long duration = Duration.between(first.timestamp, last.timestamp).getSeconds();
33
				if (duration <= 0L) {
34
					LOGGER.warn("Thread allocation stat timestamps are not in chronological order for thread {}, skipping it", thread);
35
				} else {
36
					long diff = last.totalBytes - first.totalBytes;
37
					allocationsPerSecondByThread.put(thread, (double)diff / duration);
38
				}
39
			}
40
		});
41
		return new ThreadAllocationStat.Summary(allocationsPerSecondByThread);
42
	}
43
44
	public record Summary(Map<String, Double> allocationsPerSecondByThread) {
45
	}
46
}
47

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