FilledProfileResults .java

net.minecraft.util.profiling.FilledProfileResults (class): members: COUNTER_ENTRY_COMPARATOR, CounterCollector, endTimeTicks, tickDuration, endTimeNano.

net.minecraft.util.profilingclass307 linescommon9.7 KB

Implements

API Surface (14)

long getDuration()
long getMaxDuration()
long getCount()
Object2LongMap<String> getCounters()
List<ResultField> getTimes(String path)
long getStartTimeNano()
int getStartTimeTicks()
long getEndTimeNano()
int getEndTimeTicks()
boolean saveResults(final Path file)
String getProfilerResults(final long timespan, final int tickspan)
String getProfilerResults()
int getTickDuration()
void addValue(final Iterator<String> path, final long value)

Fields & Constants (11)

LOGGER
EMPTY
SPLITTER
COUNTER_ENTRY_COMPARATOR
entries
startTimeNano
startTimeTicks
endTimeNano
endTimeTicks
tickDuration
tickspan

String literals (17)

"root"
"unspecified"
"Version: "
"Time span: "
"Tick span: "
" ticks\n"
"// This is approximately "
" ticks per second. It should be "
" ticks per second\n\n"
"--- BEGIN PROFILE DUMP ---\n\n"
"--- END PROFILE DUMP ---\n\n"
"--- BEGIN COUNTER DUMP ---\n\n"
"--- END COUNTER DUMP ---\n\n"
"[[ EXCEPTION "
" total:"
" average: "
"-- Counter: "

Code Graph: Dependencies (6)

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

Full Source

FilledProfileResults.java307 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.profiling;
2
3
import com.google.common.base.Splitter;
4
import com.google.common.collect.Lists;
5
import com.google.common.collect.Maps;
6
import com.mojang.logging.LogUtils;
7
import it.unimi.dsi.fastutil.objects.Object2LongMap;
8
import it.unimi.dsi.fastutil.objects.Object2LongMaps;
9
import java.io.Writer;
10
import java.nio.charset.StandardCharsets;
11
import java.nio.file.Files;
12
import java.nio.file.Path;
13
import java.util.Collections;
14
import java.util.Comparator;
15
import java.util.Iterator;
16
import java.util.List;
17
import java.util.Locale;
18
import java.util.Map;
19
import java.util.Map.Entry;
20
import net.minecraft.ReportType;
21
import net.minecraft.SharedConstants;
22
import org.apache.commons.io.IOUtils;
23
import org.apache.commons.lang3.ObjectUtils;
24
import org.slf4j.Logger;
25
26
public class FilledProfileResults implements ProfileResults {
27
	private static final Logger LOGGER = LogUtils.getLogger();
28
	private static final ProfilerPathEntry EMPTY = new ProfilerPathEntry() {
29
		@Override
30
		public long getDuration() {
31
			return 0L;
32
		}
33
34
		@Override
35
		public long getMaxDuration() {
36
			return 0L;
37
		}
38
39
		@Override
40
		public long getCount() {
41
			return 0L;
42
		}
43
44
		@Override
45
		public Object2LongMap<String> getCounters() {
46
			return Object2LongMaps.emptyMap();
47
		}
48
	};
49
	private static final Splitter SPLITTER = Splitter.on('\u001e');
50
	private static final Comparator<Entry<String, FilledProfileResults.CounterCollector>> COUNTER_ENTRY_COMPARATOR = Entry.<String, FilledProfileResults.CounterCollector>comparingByValue(
51
			Comparator.comparingLong(c -> c.totalValue)
52
		)
53
		.reversed();
54
	private final Map<String, ? extends ProfilerPathEntry> entries;
55
	private final long startTimeNano;
56
	private final int startTimeTicks;
57
	private final long endTimeNano;
58
	private final int endTimeTicks;
59
	private final int tickDuration;
60
61
	public FilledProfileResults(
62
		final Map<String, ? extends ProfilerPathEntry> entries, final long startTimeNano, final int startTimeTicks, final long endTimeNano, final int endTimeTicks
63
	) {
64
		this.entries = entries;
65
		this.startTimeNano = startTimeNano;
66
		this.startTimeTicks = startTimeTicks;
67
		this.endTimeNano = endTimeNano;
68
		this.endTimeTicks = endTimeTicks;
69
		this.tickDuration = endTimeTicks - startTimeTicks;
70
	}
71
72
	private ProfilerPathEntry getEntry(final String path) {
73
		ProfilerPathEntry result = this.entries.get(path);
74
		return result != null ? result : EMPTY;
75
	}
76
77
	@Override
78
	public List<ResultField> getTimes(String path) {
79
		String rawPath = path;
80
		ProfilerPathEntry rootEntry = this.getEntry("root");
81
		long globalTime = rootEntry.getDuration();
82
		ProfilerPathEntry currentEntry = this.getEntry(path);
83
		long selfTime = currentEntry.getDuration();
84
		long selfCount = currentEntry.getCount();
85
		List<ResultField> result = Lists.newArrayList();
86
		if (!path.isEmpty()) {
87
			path = path + "\u001e";
88
		}
89
90
		long totalTime = 0L;
91
92
		for (String key : this.entries.keySet()) {
93
			if (isDirectChild(path, key)) {
94
				totalTime += this.getEntry(key).getDuration();
95
			}
96
		}
97
98
		float oldTime = (float)totalTime;
99
		if (totalTime < selfTime) {
100
			totalTime = selfTime;
101
		}
102
103
		if (globalTime < totalTime) {
104
			globalTime = totalTime;
105
		}
106
107
		for (String key : this.entries.keySet()) {
108
			if (isDirectChild(path, key)) {
109
				ProfilerPathEntry entry = this.getEntry(key);
110
				long time = entry.getDuration();
111
				double timePercentage = time * 100.0 / totalTime;
112
				double globalPercentage = time * 100.0 / globalTime;
113
				String name = key.substring(path.length());
114
				result.add(new ResultField(name, timePercentage, globalPercentage, entry.getCount()));
115
			}
116
		}
117
118
		if ((float)totalTime > oldTime) {
119
			result.add(new ResultField("unspecified", ((float)totalTime - oldTime) * 100.0 / totalTime, ((float)totalTime - oldTime) * 100.0 / globalTime, selfCount));
120
		}
121
122
		Collections.sort(result);
123
		result.add(0, new ResultField(rawPath, 100.0, totalTime * 100.0 / globalTime, selfCount));
124
		return result;
125
	}
126
127
	private static boolean isDirectChild(final String path, final String test) {
128
		return test.length() > path.length() && test.startsWith(path) && test.indexOf(30, path.length() + 1) < 0;
129
	}
130
131
	private Map<String, FilledProfileResults.CounterCollector> getCounterValues() {
132
		Map<String, FilledProfileResults.CounterCollector> result = Maps.newTreeMap();
133
		this.entries
134
			.forEach(
135
				(path, entry) -> {
136
					Object2LongMap<String> counters = entry.getCounters();
137
					if (!counters.isEmpty()) {
138
						List<String> pathSegments = SPLITTER.splitToList(path);
139
						counters.forEach(
140
							(counter, value) -> result.computeIfAbsent(counter, k -> new FilledProfileResults.CounterCollector()).addValue(pathSegments.iterator(), value)
141
						);
142
					}
143
				}
144
			);
145
		return result;
146
	}
147
148
	@Override
149
	public long getStartTimeNano() {
150
		return this.startTimeNano;
151
	}
152
153
	@Override
154
	public int getStartTimeTicks() {
155
		return this.startTimeTicks;
156
	}
157
158
	@Override
159
	public long getEndTimeNano() {
160
		return this.endTimeNano;
161
	}
162
163
	@Override
164
	public int getEndTimeTicks() {
165
		return this.endTimeTicks;
166
	}
167
168
	@Override
169
	public boolean saveResults(final Path file) {
170
		Writer writer = null;
171
172
		try {
173
			Files.createDirectories(file.getParent());
174
			writer = Files.newBufferedWriter(file, StandardCharsets.UTF_8);
175
			writer.write(this.getProfilerResults(this.getNanoDuration(), this.getTickDuration()));
176
			return true;
177
		} catch (Throwable t) {
178
			LOGGER.error("Could not save profiler results to {}", file, t);
179
			return false;
180
		} finally {
181
			IOUtils.closeQuietly(writer);
182
		}
183
	}
184
185
	protected String getProfilerResults(final long timespan, final int tickspan) {
186
		StringBuilder builder = new StringBuilder();
187
		ReportType.PROFILE.appendHeader(builder, List.of());
188
		builder.append("Version: ").append(SharedConstants.getCurrentVersion().id()).append('\n');
189
		builder.append("Time span: ").append(timespan / 1000000L).append(" ms\n");
190
		builder.append("Tick span: ").append(tickspan).append(" ticks\n");
191
		builder.append("// This is approximately ")
192
			.append(String.format(Locale.ROOT, "%.2f", tickspan / ((float)timespan / 1.0E9F)))
193
			.append(" ticks per second. It should be ")
194
			.append(20)
195
			.append(" ticks per second\n\n");
196
		builder.append("--- BEGIN PROFILE DUMP ---\n\n");
197
		this.appendProfilerResults(0, "root", builder);
198
		builder.append("--- END PROFILE DUMP ---\n\n");
199
		Map<String, FilledProfileResults.CounterCollector> counters = this.getCounterValues();
200
		if (!counters.isEmpty()) {
201
			builder.append("--- BEGIN COUNTER DUMP ---\n\n");
202
			this.appendCounters(counters, builder, tickspan);
203
			builder.append("--- END COUNTER DUMP ---\n\n");
204
		}
205
206
		return builder.toString();
207
	}
208
209
	@Override
210
	public String getProfilerResults() {
211
		StringBuilder builder = new StringBuilder();
212
		this.appendProfilerResults(0, "root", builder);
213
		return builder.toString();
214
	}
215
216
	private static StringBuilder indentLine(final StringBuilder builder, final int depth) {
217
		builder.append(String.format(Locale.ROOT, "[%02d] ", depth));
218
219
		for (int j = 0; j < depth; j++) {
220
			builder.append("|   ");
221
		}
222
223
		return builder;
224
	}
225
226
	private void appendProfilerResults(final int depth, final String path, final StringBuilder builder) {
227
		List<ResultField> results = this.getTimes(path);
228
		Object2LongMap<String> counters = ObjectUtils.firstNonNull(this.entries.get(path), EMPTY).getCounters();
229
		counters.forEach(
230
			(id, value) -> indentLine(builder, depth).append('#').append(id).append(' ').append(value).append('/').append(value / this.tickDuration).append('\n')
231
		);
232
		if (results.size() >= 3) {
233
			for (int i = 1; i < results.size(); i++) {
234
				ResultField result = results.get(i);
235
				indentLine(builder, depth)
236
					.append(result.name)
237
					.append('(')
238
					.append(result.count)
239
					.append('/')
240
					.append(String.format(Locale.ROOT, "%.0f", (float)result.count / this.tickDuration))
241
					.append(')')
242
					.append(" - ")
243
					.append(String.format(Locale.ROOT, "%.2f", result.percentage))
244
					.append("%/")
245
					.append(String.format(Locale.ROOT, "%.2f", result.globalPercentage))
246
					.append("%\n");
247
				if (!"unspecified".equals(result.name)) {
248
					try {
249
						this.appendProfilerResults(depth + 1, path + "\u001e" + result.name, builder);
250
					} catch (Exception e) {
251
						builder.append("[[ EXCEPTION ").append(e).append(" ]]");
252
					}
253
				}
254
			}
255
		}
256
	}
257
258
	private void appendCounterResults(
259
		final int depth, final String name, final FilledProfileResults.CounterCollector result, final int tickspan, final StringBuilder builder
260
	) {
261
		indentLine(builder, depth)
262
			.append(name)
263
			.append(" total:")
264
			.append(result.selfValue)
265
			.append('/')
266
			.append(result.totalValue)
267
			.append(" average: ")
268
			.append(result.selfValue / tickspan)
269
			.append('/')
270
			.append(result.totalValue / tickspan)
271
			.append('\n');
272
		result.children
273
			.entrySet()
274
			.stream()
275
			.sorted(COUNTER_ENTRY_COMPARATOR)
276
			.forEach(e -> this.appendCounterResults(depth + 1, e.getKey(), e.getValue(), tickspan, builder));
277
	}
278
279
	private void appendCounters(final Map<String, FilledProfileResults.CounterCollector> counters, final StringBuilder builder, final int tickspan) {
280
		counters.forEach((counter, counterRoot) -> {
281
			builder.append("-- Counter: ").append(counter).append(" --\n");
282
			this.appendCounterResults(0, "root", counterRoot.children.get("root"), tickspan, builder);
283
			builder.append("\n\n");
284
		});
285
	}
286
287
	@Override
288
	public int getTickDuration() {
289
		return this.tickDuration;
290
	}
291
292
	private static class CounterCollector {
293
		private long selfValue;
294
		private long totalValue;
295
		private final Map<String, FilledProfileResults.CounterCollector> children = Maps.newHashMap();
296
297
		public void addValue(final Iterator<String> path, final long value) {
298
			this.totalValue += value;
299
			if (!path.hasNext()) {
300
				this.selfValue += value;
301
			} else {
302
				this.children.computeIfAbsent(path.next(), k -> new FilledProfileResults.CounterCollector()).addValue(path, value);
303
			}
304
		}
305
	}
306
}
307

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