| 1 | package net.minecraft.util.profiling.metrics;
|
| 2 |
|
| 3 | import io.netty.buffer.ByteBuf;
|
| 4 | import io.netty.buffer.ByteBufAllocator;
|
| 5 | import it.unimi.dsi.fastutil.ints.Int2DoubleMap;
|
| 6 | import it.unimi.dsi.fastutil.ints.Int2DoubleOpenHashMap;
|
| 7 | import java.util.Locale;
|
| 8 | import java.util.function.Consumer;
|
| 9 | import java.util.function.DoubleSupplier;
|
| 10 | import java.util.function.ToDoubleFunction;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | public class MetricSampler {
|
| 14 | private final String name;
|
| 15 | private final MetricSampler.SamplingPhase samplingPhase;
|
| 16 | private final MetricCategory category;
|
| 17 | private final DoubleSupplier sampler;
|
| 18 | private final ByteBuf ticks;
|
| 19 | private final ByteBuf values;
|
| 20 | private volatile boolean isRunning;
|
| 21 | private final @Nullable Runnable beforeTick;
|
| 22 | final MetricSampler.@Nullable ThresholdTest thresholdTest;
|
| 23 | private double currentValue;
|
| 24 |
|
| 25 | protected MetricSampler(
|
| 26 | final String name,
|
| 27 | final MetricSampler.SamplingPhase samplingPhase,
|
| 28 | final MetricCategory category,
|
| 29 | final DoubleSupplier sampler,
|
| 30 | final @Nullable Runnable beforeTick,
|
| 31 | final MetricSampler.@Nullable ThresholdTest thresholdTest
|
| 32 | ) {
|
| 33 | this.name = name;
|
| 34 | this.samplingPhase = samplingPhase;
|
| 35 | this.category = category;
|
| 36 | this.beforeTick = beforeTick;
|
| 37 | this.sampler = sampler;
|
| 38 | this.thresholdTest = thresholdTest;
|
| 39 | this.values = ByteBufAllocator.DEFAULT.buffer();
|
| 40 | this.ticks = ByteBufAllocator.DEFAULT.buffer();
|
| 41 | this.isRunning = true;
|
| 42 | }
|
| 43 |
|
| 44 | public static MetricSampler create(final String name, final MetricCategory category, final DoubleSupplier sampler) {
|
| 45 | return new MetricSampler(name, MetricSampler.SamplingPhase.END_TICK, category, sampler, null, null);
|
| 46 | }
|
| 47 |
|
| 48 | public static MetricSampler createExtractSampler(final String name, final MetricCategory category, final DoubleSupplier sampler) {
|
| 49 | return new MetricSampler(name, MetricSampler.SamplingPhase.EXTRACT, category, sampler, null, null);
|
| 50 | }
|
| 51 |
|
| 52 | public static <T> MetricSampler.MetricSamplerBuilder<T> builder(
|
| 53 | final String metricName, final MetricCategory category, final ToDoubleFunction<T> sampler, final T context
|
| 54 | ) {
|
| 55 | if (sampler == null) {
|
| 56 | throw new IllegalStateException();
|
| 57 | } else {
|
| 58 | return new MetricSampler.MetricSamplerBuilder<>(metricName, category, sampler, context);
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | public void onStartTick() {
|
| 63 | if (!this.isRunning) {
|
| 64 | throw new IllegalStateException("Not running");
|
| 65 | }
|
| 66 |
|
| 67 | if (this.beforeTick != null) {
|
| 68 | this.beforeTick.run();
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | public void onEndTick(final int currentTick) {
|
| 73 | this.verifyRunning();
|
| 74 | this.currentValue = this.sampler.getAsDouble();
|
| 75 | this.values.writeDouble(this.currentValue);
|
| 76 | this.ticks.writeInt(currentTick);
|
| 77 | }
|
| 78 |
|
| 79 | public void onFinished() {
|
| 80 | this.verifyRunning();
|
| 81 | this.values.release();
|
| 82 | this.ticks.release();
|
| 83 | this.isRunning = false;
|
| 84 | }
|
| 85 |
|
| 86 | private void verifyRunning() {
|
| 87 | if (!this.isRunning) {
|
| 88 | throw new IllegalStateException(String.format(Locale.ROOT, "Sampler for metric %s not started!", this.name));
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | public DoubleSupplier getSampler() {
|
| 93 | return this.sampler;
|
| 94 | }
|
| 95 |
|
| 96 | public String getName() {
|
| 97 | return this.name;
|
| 98 | }
|
| 99 |
|
| 100 | public MetricSampler.SamplingPhase samplingPhase() {
|
| 101 | return this.samplingPhase;
|
| 102 | }
|
| 103 |
|
| 104 | public MetricCategory getCategory() {
|
| 105 | return this.category;
|
| 106 | }
|
| 107 |
|
| 108 | public MetricSampler.SamplerResult result() {
|
| 109 | Int2DoubleMap result = new Int2DoubleOpenHashMap();
|
| 110 | int firstTick = Integer.MIN_VALUE;
|
| 111 | int lastTick = Integer.MIN_VALUE;
|
| 112 |
|
| 113 | while (this.values.isReadable(8)) {
|
| 114 | int currentTick = this.ticks.readInt();
|
| 115 | if (firstTick == Integer.MIN_VALUE) {
|
| 116 | firstTick = currentTick;
|
| 117 | }
|
| 118 |
|
| 119 | result.put(currentTick, this.values.readDouble());
|
| 120 | lastTick = currentTick;
|
| 121 | }
|
| 122 |
|
| 123 | return new MetricSampler.SamplerResult(firstTick, lastTick, result);
|
| 124 | }
|
| 125 |
|
| 126 | public boolean triggersThreshold() {
|
| 127 | return this.thresholdTest != null && this.thresholdTest.test(this.currentValue);
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | public boolean equals(final Object o) {
|
| 132 | if (this == o) {
|
| 133 | return true;
|
| 134 | } else if (o != null && this.getClass() == o.getClass()) {
|
| 135 | MetricSampler that = (MetricSampler)o;
|
| 136 | return this.name.equals(that.name) && this.category.equals(that.category);
|
| 137 | } else {
|
| 138 | return false;
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | @Override
|
| 143 | public int hashCode() {
|
| 144 | return this.name.hashCode();
|
| 145 | }
|
| 146 |
|
| 147 | public static class MetricSamplerBuilder<T> {
|
| 148 | private final String name;
|
| 149 | private final MetricCategory category;
|
| 150 | private final DoubleSupplier sampler;
|
| 151 | private final T context;
|
| 152 | private MetricSampler.SamplingPhase samplingPhase = MetricSampler.SamplingPhase.END_TICK;
|
| 153 | private @Nullable Runnable beforeTick;
|
| 154 | private MetricSampler.@Nullable ThresholdTest thresholdTest;
|
| 155 |
|
| 156 | public MetricSamplerBuilder(final String name, final MetricCategory category, final ToDoubleFunction<T> sampler, final T context) {
|
| 157 | this.name = name;
|
| 158 | this.category = category;
|
| 159 | this.sampler = () -> sampler.applyAsDouble(context);
|
| 160 | this.context = context;
|
| 161 | }
|
| 162 |
|
| 163 | public MetricSampler.MetricSamplerBuilder<T> withBeforeTick(final Consumer<T> beforeTick) {
|
| 164 | this.beforeTick = () -> beforeTick.accept(this.context);
|
| 165 | return this;
|
| 166 | }
|
| 167 |
|
| 168 | public MetricSampler.MetricSamplerBuilder<T> withThresholdAlert(final MetricSampler.ThresholdTest thresholdTest) {
|
| 169 | this.thresholdTest = thresholdTest;
|
| 170 | return this;
|
| 171 | }
|
| 172 |
|
| 173 | public MetricSampler.MetricSamplerBuilder<T> withSamplingPhase(final MetricSampler.SamplingPhase samplingPhase) {
|
| 174 | this.samplingPhase = samplingPhase;
|
| 175 | return this;
|
| 176 | }
|
| 177 |
|
| 178 | public MetricSampler build() {
|
| 179 | return new MetricSampler(this.name, this.samplingPhase, this.category, this.sampler, this.beforeTick, this.thresholdTest);
|
| 180 | }
|
| 181 | }
|
| 182 |
|
| 183 | public static class SamplerResult {
|
| 184 | private final Int2DoubleMap recording;
|
| 185 | private final int firstTick;
|
| 186 | private final int lastTick;
|
| 187 |
|
| 188 | public SamplerResult(final int firstTick, final int lastTick, final Int2DoubleMap recording) {
|
| 189 | this.firstTick = firstTick;
|
| 190 | this.lastTick = lastTick;
|
| 191 | this.recording = recording;
|
| 192 | }
|
| 193 |
|
| 194 | public double valueAtTick(final int tick) {
|
| 195 | return this.recording.get(tick);
|
| 196 | }
|
| 197 |
|
| 198 | public int getFirstTick() {
|
| 199 | return this.firstTick;
|
| 200 | }
|
| 201 |
|
| 202 | public int getLastTick() {
|
| 203 | return this.lastTick;
|
| 204 | }
|
| 205 | }
|
| 206 |
|
| 207 | public enum SamplingPhase {
|
| 208 | EXTRACT,
|
| 209 | END_TICK;
|
| 210 | }
|
| 211 |
|
| 212 | public interface ThresholdTest {
|
| 213 | boolean test(final double value);
|
| 214 | }
|
| 215 |
|
| 216 | public static class ValueIncreasedByPercentage implements MetricSampler.ThresholdTest {
|
| 217 | private final float percentageIncreaseThreshold;
|
| 218 | private double previousValue = Double.MIN_VALUE;
|
| 219 |
|
| 220 | public ValueIncreasedByPercentage(final float percentageIncreaseThreshold) {
|
| 221 | this.percentageIncreaseThreshold = percentageIncreaseThreshold;
|
| 222 | }
|
| 223 |
|
| 224 | @Override
|
| 225 | public boolean test(final double value) {
|
| 226 | boolean result;
|
| 227 | if (this.previousValue != Double.MIN_VALUE && !(value <= this.previousValue)) {
|
| 228 | result = (value - this.previousValue) / this.previousValue >= this.percentageIncreaseThreshold;
|
| 229 | } else {
|
| 230 | result = false;
|
| 231 | }
|
| 232 |
|
| 233 | this.previousValue = value;
|
| 234 | return result;
|
| 235 | }
|
| 236 | }
|
| 237 | }
|
| 238 |
|