| 1 | package net.minecraft.util.profiling;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.io.File;
|
| 5 | import java.util.function.LongSupplier;
|
| 6 | import net.minecraft.SharedConstants;
|
| 7 | import net.minecraft.util.Util;
|
| 8 | import org.jspecify.annotations.Nullable;
|
| 9 | import org.slf4j.Logger;
|
| 10 |
|
| 11 | public class SingleTickProfiler {
|
| 12 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 13 | private final LongSupplier realTime;
|
| 14 | private final long saveThreshold;
|
| 15 | private int tick;
|
| 16 | private final File location;
|
| 17 | private ProfileCollector profiler = InactiveProfiler.INSTANCE;
|
| 18 |
|
| 19 | public SingleTickProfiler(final LongSupplier realTime, final String location, final long saveThresholdNs) {
|
| 20 | this.realTime = realTime;
|
| 21 | this.location = new File("debug", location);
|
| 22 | this.saveThreshold = saveThresholdNs;
|
| 23 | }
|
| 24 |
|
| 25 | public ProfilerFiller startTick() {
|
| 26 | this.profiler = new ActiveProfiler(this.realTime, () -> this.tick, () -> true);
|
| 27 | this.tick++;
|
| 28 | return this.profiler;
|
| 29 | }
|
| 30 |
|
| 31 | public void endTick() {
|
| 32 | if (this.profiler != InactiveProfiler.INSTANCE) {
|
| 33 | ProfileResults results = this.profiler.getResults();
|
| 34 | this.profiler = InactiveProfiler.INSTANCE;
|
| 35 | if (results.getNanoDuration() >= this.saveThreshold) {
|
| 36 | File file = new File(this.location, "tick-results-" + Util.getFilenameFormattedDateTime() + ".txt");
|
| 37 | results.saveResults(file.toPath());
|
| 38 | LOGGER.info("Recorded long tick -- wrote info to: {}", file.getAbsolutePath());
|
| 39 | }
|
| 40 | }
|
| 41 | }
|
| 42 |
|
| 43 | public static @Nullable SingleTickProfiler createTickProfiler(final String name) {
|
| 44 | return SharedConstants.DEBUG_MONITOR_TICK_TIMES ? new SingleTickProfiler(Util.timeSource(), name, SharedConstants.MAXIMUM_TICK_TIME_NANOS) : null;
|
| 45 | }
|
| 46 |
|
| 47 | public static ProfilerFiller decorateFiller(final ProfilerFiller filler, final @Nullable SingleTickProfiler tickProfiler) {
|
| 48 | return tickProfiler != null ? ProfilerFiller.combine(tickProfiler.startTick(), filler) : filler;
|
| 49 | }
|
| 50 | }
|
| 51 |
|