ActiveProfiler .java

net.minecraft.util.profiling.ActiveProfiler (class): members: WARNING_TIME_NANOS, chartedPaths, currentEntry, getRealTime, getTickTime, startTimes, paths.

net.minecraft.util.profilingclass205 linescommon6.1 KB

Implements

API Surface (16)

void startTick()
void endTick()
void push(final String name)
void push(final Supplier<String> name)
void markForCharting(final MetricCategory category)
void pop()
void popPush(final String name)
void popPush(final Supplier<String> name)
void incrementCounter(final String name, final int amount)
void incrementCounter(final Supplier<String> name, final int amount)
ProfileResults getResults()
Set<Pair<String, MetricCategory>> getChartedPaths()
long getDuration()
long getMaxDuration()
long getCount()
Object2LongMap<String> getCounters()

Fields & Constants (14)

WARNING_TIME_NANOS
LOGGER
paths
startTimes
entries
getTickTime
getRealTime
startTimeNano
startTimeTicks
path
started
currentEntry
suppressWarnings
chartedPaths

String literals (1)

"root"

Code Graph: Dependencies (5)

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

Full Source

ActiveProfiler.java205 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.profiling;
2
3
import com.google.common.collect.Lists;
4
import com.google.common.collect.Maps;
5
import com.mojang.datafixers.util.Pair;
6
import com.mojang.logging.LogUtils;
7
import it.unimi.dsi.fastutil.longs.LongArrayList;
8
import it.unimi.dsi.fastutil.longs.LongList;
9
import it.unimi.dsi.fastutil.objects.Object2LongMap;
10
import it.unimi.dsi.fastutil.objects.Object2LongMaps;
11
import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap;
12
import it.unimi.dsi.fastutil.objects.ObjectArraySet;
13
import java.time.Duration;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.Set;
17
import java.util.function.BooleanSupplier;
18
import java.util.function.IntSupplier;
19
import java.util.function.LongSupplier;
20
import java.util.function.Supplier;
21
import net.minecraft.util.Util;
22
import net.minecraft.util.profiling.metrics.MetricCategory;
23
import org.jspecify.annotations.Nullable;
24
import org.slf4j.Logger;
25
26
public class ActiveProfiler implements ProfileCollector {
27
	private static final long WARNING_TIME_NANOS = Duration.ofMillis(100L).toNanos();
28
	private static final Logger LOGGER = LogUtils.getLogger();
29
	private final List<String> paths = Lists.newArrayList();
30
	private final LongList startTimes = new LongArrayList();
31
	private final Map<String, ActiveProfiler.PathEntry> entries = Maps.newHashMap();
32
	private final IntSupplier getTickTime;
33
	private final LongSupplier getRealTime;
34
	private final long startTimeNano;
35
	private final int startTimeTicks;
36
	private String path = "";
37
	private boolean started;
38
	private ActiveProfiler.@Nullable PathEntry currentEntry;
39
	private final BooleanSupplier suppressWarnings;
40
	private final Set<Pair<String, MetricCategory>> chartedPaths = new ObjectArraySet<>();
41
42
	public ActiveProfiler(final LongSupplier getRealTime, final IntSupplier getTickTime, final BooleanSupplier suppressWarnings) {
43
		this.startTimeNano = getRealTime.getAsLong();
44
		this.getRealTime = getRealTime;
45
		this.startTimeTicks = getTickTime.getAsInt();
46
		this.getTickTime = getTickTime;
47
		this.suppressWarnings = suppressWarnings;
48
	}
49
50
	@Override
51
	public void startTick() {
52
		if (this.started) {
53
			LOGGER.error("Profiler tick already started - missing endTick()?");
54
		} else {
55
			this.started = true;
56
			this.path = "";
57
			this.paths.clear();
58
			this.push("root");
59
		}
60
	}
61
62
	@Override
63
	public void endTick() {
64
		if (!this.started) {
65
			LOGGER.error("Profiler tick already ended - missing startTick()?");
66
		} else {
67
			this.pop();
68
			this.started = false;
69
			if (!this.path.isEmpty()) {
70
				LOGGER.error(
71
					"Profiler tick ended before path was fully popped (remainder: '{}'). Mismatched push/pop?", LogUtils.defer(() -> ProfileResults.demanglePath(this.path))
72
				);
73
			}
74
		}
75
	}
76
77
	@Override
78
	public void push(final String name) {
79
		if (!this.started) {
80
			LOGGER.error("Cannot push '{}' to profiler if profiler tick hasn't started - missing startTick()?", name);
81
		} else {
82
			if (!this.path.isEmpty()) {
83
				this.path = this.path + "\u001e";
84
			}
85
86
			this.path = this.path + name;
87
			this.paths.add(this.path);
88
			this.startTimes.add(Util.getNanos());
89
			this.currentEntry = null;
90
		}
91
	}
92
93
	@Override
94
	public void push(final Supplier<String> name) {
95
		this.push(name.get());
96
	}
97
98
	@Override
99
	public void markForCharting(final MetricCategory category) {
100
		this.chartedPaths.add(Pair.of(this.path, category));
101
	}
102
103
	@Override
104
	public void pop() {
105
		if (!this.started) {
106
			LOGGER.error("Cannot pop from profiler if profiler tick hasn't started - missing startTick()?");
107
		} else if (this.startTimes.isEmpty()) {
108
			LOGGER.error("Tried to pop one too many times! Mismatched push() and pop()?");
109
		} else {
110
			long endTime = Util.getNanos();
111
			long startTime = this.startTimes.removeLong(this.startTimes.size() - 1);
112
			this.paths.removeLast();
113
			long time = endTime - startTime;
114
			ActiveProfiler.PathEntry currentEntry = this.getCurrentEntry();
115
			currentEntry.accumulatedDuration += time;
116
			currentEntry.count++;
117
			currentEntry.maxDuration = Math.max(currentEntry.maxDuration, time);
118
			currentEntry.minDuration = Math.min(currentEntry.minDuration, time);
119
			if (time > WARNING_TIME_NANOS && !this.suppressWarnings.getAsBoolean()) {
120
				LOGGER.warn(
121
					"Something's taking too long! '{}' took approximately {} ms",
122
					LogUtils.defer(() -> ProfileResults.demanglePath(this.path)),
123
					LogUtils.defer(() -> time / 1000000.0)
124
				);
125
			}
126
127
			this.path = this.paths.isEmpty() ? "" : this.paths.getLast();
128
			this.currentEntry = null;
129
		}
130
	}
131
132
	@Override
133
	public void popPush(final String name) {
134
		this.pop();
135
		this.push(name);
136
	}
137
138
	@Override
139
	public void popPush(final Supplier<String> name) {
140
		this.pop();
141
		this.push(name);
142
	}
143
144
	private ActiveProfiler.PathEntry getCurrentEntry() {
145
		if (this.currentEntry == null) {
146
			this.currentEntry = this.entries.computeIfAbsent(this.path, key -> new ActiveProfiler.PathEntry());
147
		}
148
149
		return this.currentEntry;
150
	}
151
152
	@Override
153
	public void incrementCounter(final String name, final int amount) {
154
		this.getCurrentEntry().counters.addTo(name, amount);
155
	}
156
157
	@Override
158
	public void incrementCounter(final Supplier<String> name, final int amount) {
159
		this.getCurrentEntry().counters.addTo(name.get(), amount);
160
	}
161
162
	@Override
163
	public ProfileResults getResults() {
164
		return new FilledProfileResults(this.entries, this.startTimeNano, this.startTimeTicks, this.getRealTime.getAsLong(), this.getTickTime.getAsInt());
165
	}
166
167
	@Override
168
	public ActiveProfiler.@Nullable PathEntry getEntry(final String path) {
169
		return this.entries.get(path);
170
	}
171
172
	@Override
173
	public Set<Pair<String, MetricCategory>> getChartedPaths() {
174
		return this.chartedPaths;
175
	}
176
177
	public static class PathEntry implements ProfilerPathEntry {
178
		private long maxDuration = Long.MIN_VALUE;
179
		private long minDuration = Long.MAX_VALUE;
180
		private long accumulatedDuration;
181
		private long count;
182
		private final Object2LongOpenHashMap<String> counters = new Object2LongOpenHashMap<>();
183
184
		@Override
185
		public long getDuration() {
186
			return this.accumulatedDuration;
187
		}
188
189
		@Override
190
		public long getMaxDuration() {
191
			return this.maxDuration;
192
		}
193
194
		@Override
195
		public long getCount() {
196
			return this.count;
197
		}
198
199
		@Override
200
		public Object2LongMap<String> getCounters() {
201
			return Object2LongMaps.unmodifiable(this.counters);
202
		}
203
	}
204
}
205

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