| 1 | package net.minecraft.util.profiling;
|
| 2 |
|
| 3 | import com.mojang.jtracy.Plot;
|
| 4 | import com.mojang.jtracy.TracyClient;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import java.lang.StackWalker.Option;
|
| 7 | import java.lang.StackWalker.StackFrame;
|
| 8 | import java.util.ArrayList;
|
| 9 | import java.util.HashMap;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Map;
|
| 12 | import java.util.Optional;
|
| 13 | import java.util.Set;
|
| 14 | import java.util.function.Supplier;
|
| 15 | import net.minecraft.SharedConstants;
|
| 16 | import net.minecraft.util.profiling.metrics.MetricCategory;
|
| 17 | import org.slf4j.Logger;
|
| 18 |
|
| 19 | public class TracyZoneFiller implements ProfilerFiller {
|
| 20 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 21 | private static final StackWalker STACK_WALKER = StackWalker.getInstance(Set.of(Option.RETAIN_CLASS_REFERENCE), 5);
|
| 22 | private final List<com.mojang.jtracy.Zone> activeZones = new ArrayList<>();
|
| 23 | private final Map<String, TracyZoneFiller.PlotAndValue> plots = new HashMap<>();
|
| 24 | private final String name = Thread.currentThread().getName();
|
| 25 |
|
| 26 | @Override
|
| 27 | public void startTick() {
|
| 28 | }
|
| 29 |
|
| 30 | @Override
|
| 31 | public void endTick() {
|
| 32 | for (TracyZoneFiller.PlotAndValue plotAndValue : this.plots.values()) {
|
| 33 | plotAndValue.set(0);
|
| 34 | }
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public void push(final String name) {
|
| 39 | String function = "";
|
| 40 | String file = "";
|
| 41 | int line = 0;
|
| 42 | if (SharedConstants.IS_RUNNING_IN_IDE) {
|
| 43 | Optional<StackFrame> result = STACK_WALKER.walk(
|
| 44 | s -> s.filter(framex -> framex.getDeclaringClass() != TracyZoneFiller.class && framex.getDeclaringClass() != ProfilerFiller.CombinedProfileFiller.class)
|
| 45 | .findFirst()
|
| 46 | );
|
| 47 | if (result.isPresent()) {
|
| 48 | StackFrame frame = result.get();
|
| 49 | function = frame.getMethodName();
|
| 50 | file = frame.getFileName();
|
| 51 | line = frame.getLineNumber();
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | com.mojang.jtracy.Zone zone = TracyClient.beginZone(name, function, file, line);
|
| 56 | this.activeZones.add(zone);
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public void push(final Supplier<String> name) {
|
| 61 | this.push(name.get());
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | public void pop() {
|
| 66 | if (this.activeZones.isEmpty()) {
|
| 67 | LOGGER.error("Tried to pop one too many times! Mismatched push() and pop()?");
|
| 68 | } else {
|
| 69 | com.mojang.jtracy.Zone zone = this.activeZones.removeLast();
|
| 70 | zone.close();
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public void popPush(final String name) {
|
| 76 | this.pop();
|
| 77 | this.push(name);
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public void popPush(final Supplier<String> name) {
|
| 82 | this.pop();
|
| 83 | this.push(name.get());
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | public void markForCharting(final MetricCategory category) {
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | public void incrementCounter(final String name, final int amount) {
|
| 92 | this.plots.computeIfAbsent(name, s -> new TracyZoneFiller.PlotAndValue(this.name + " " + name)).add(amount);
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | public void incrementCounter(final Supplier<String> name, final int amount) {
|
| 97 | this.incrementCounter(name.get(), amount);
|
| 98 | }
|
| 99 |
|
| 100 | private com.mojang.jtracy.Zone activeZone() {
|
| 101 | return this.activeZones.getLast();
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | public void addZoneText(final String text) {
|
| 106 | this.activeZone().addText(text);
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | public void addZoneValue(final long value) {
|
| 111 | this.activeZone().addValue(value);
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | public void setZoneColor(final int color) {
|
| 116 | this.activeZone().setColor(color);
|
| 117 | }
|
| 118 |
|
| 119 | private static final class PlotAndValue {
|
| 120 | private final Plot plot;
|
| 121 | private int value;
|
| 122 |
|
| 123 | private PlotAndValue(final String name) {
|
| 124 | this.plot = TracyClient.createPlot(name);
|
| 125 | this.value = 0;
|
| 126 | }
|
| 127 |
|
| 128 | public void set(final int value) {
|
| 129 | this.value = value;
|
| 130 | this.plot.setValue(value);
|
| 131 | }
|
| 132 |
|
| 133 | public void add(final int amount) {
|
| 134 | this.set(this.value + amount);
|
| 135 | }
|
| 136 | }
|
| 137 | }
|
| 138 |
|