ActiveProfiler.java

net.minecraft.util.profiling.ActiveProfiler

信息

  • 全限定名:net.minecraft.util.profiling.ActiveProfiler
  • 类型:public class
  • 包:net.minecraft.util.profiling
  • 源码路径:src/main/java/net/minecraft/util/profiling/ActiveProfiler.java
  • 起始行号:L26
  • 实现:ProfileCollector
  • 职责:

    TODO

字段/常量

  • WARNING_TIME_NANOS

    • 类型: long
    • 修饰符: private static final
    • 源码定位: L27
    • 说明:

      TODO

  • LOGGER

    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L28
    • 说明:

      TODO

  • paths

    • 类型: List<String>
    • 修饰符: private final
    • 源码定位: L29
    • 说明:

      TODO

  • startTimes

    • 类型: LongList
    • 修饰符: private final
    • 源码定位: L30
    • 说明:

      TODO

  • entries

    • 类型: Map<String,ActiveProfiler.PathEntry>
    • 修饰符: private final
    • 源码定位: L31
    • 说明:

      TODO

  • getTickTime

    • 类型: IntSupplier
    • 修饰符: private final
    • 源码定位: L32
    • 说明:

      TODO

  • getRealTime

    • 类型: LongSupplier
    • 修饰符: private final
    • 源码定位: L33
    • 说明:

      TODO

  • startTimeNano

    • 类型: long
    • 修饰符: private final
    • 源码定位: L34
    • 说明:

      TODO

  • startTimeTicks

    • 类型: int
    • 修饰符: private final
    • 源码定位: L35
    • 说明:

      TODO

  • path

    • 类型: String
    • 修饰符: private
    • 源码定位: L36
    • 说明:

      TODO

  • started

    • 类型: boolean
    • 修饰符: private
    • 源码定位: L37
    • 说明:

      TODO

  • currentEntry

    • 类型: ActiveProfiler.PathEntry
    • 修饰符: private
    • 源码定位: L38
    • 说明:

      TODO

  • suppressWarnings

    • 类型: BooleanSupplier
    • 修饰符: private final
    • 源码定位: L39
    • 说明:

      TODO

  • chartedPaths

    • 类型: Set<Pair<String,MetricCategory>>
    • 修饰符: private final
    • 源码定位: L40
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.util.profiling.ActiveProfiler.PathEntry
    • 类型: class
    • 修饰符: public static
    • 源码定位: L178
    • 说明:

      TODO

构造器

public ActiveProfiler(LongSupplier getRealTime, IntSupplier getTickTime, BooleanSupplier suppressWarnings) @ L42

  • 构造器名:ActiveProfiler
  • 源码定位:L42
  • 修饰符:public

参数:

  • getRealTime: LongSupplier
  • getTickTime: IntSupplier
  • suppressWarnings: BooleanSupplier

说明:

TODO

方法

下面的方法块按源码顺序生成。

public void startTick() @ L50

  • 方法名:startTick
  • 源码定位:L50
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

public void endTick() @ L62

  • 方法名:endTick
  • 源码定位:L62
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

public void push(String name) @ L78

  • 方法名:push
  • 源码定位:L78
  • 返回类型:void
  • 修饰符:public

参数:

  • name: String

说明:

TODO

public void push(Supplier<String> name) @ L94

  • 方法名:push
  • 源码定位:L94
  • 返回类型:void
  • 修饰符:public

参数:

  • name: Supplier

说明:

TODO

public void markForCharting(MetricCategory category) @ L99

  • 方法名:markForCharting
  • 源码定位:L99
  • 返回类型:void
  • 修饰符:public

参数:

  • category: MetricCategory

说明:

TODO

public void pop() @ L104

  • 方法名:pop
  • 源码定位:L104
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

public void popPush(String name) @ L133

  • 方法名:popPush
  • 源码定位:L133
  • 返回类型:void
  • 修饰符:public

参数:

  • name: String

说明:

TODO

public void popPush(Supplier<String> name) @ L139

  • 方法名:popPush
  • 源码定位:L139
  • 返回类型:void
  • 修饰符:public

参数:

  • name: Supplier

说明:

TODO

private ActiveProfiler.PathEntry getCurrentEntry() @ L145

  • 方法名:getCurrentEntry
  • 源码定位:L145
  • 返回类型:ActiveProfiler.PathEntry
  • 修饰符:private

参数:

说明:

TODO

public void incrementCounter(String name, int amount) @ L153

  • 方法名:incrementCounter
  • 源码定位:L153
  • 返回类型:void
  • 修饰符:public

参数:

  • name: String
  • amount: int

说明:

TODO

public void incrementCounter(Supplier<String> name, int amount) @ L158

  • 方法名:incrementCounter
  • 源码定位:L158
  • 返回类型:void
  • 修饰符:public

参数:

  • name: Supplier
  • amount: int

说明:

TODO

public ProfileResults getResults() @ L163

  • 方法名:getResults
  • 源码定位:L163
  • 返回类型:ProfileResults
  • 修饰符:public

参数:

说明:

TODO

public ActiveProfiler.PathEntry getEntry(String path) @ L168

  • 方法名:getEntry
  • 源码定位:L168
  • 返回类型:ActiveProfiler.PathEntry
  • 修饰符:public

参数:

  • path: String

说明:

TODO

public Set<Pair<String,MetricCategory>> getChartedPaths() @ L173

  • 方法名:getChartedPaths
  • 源码定位:L173
  • 返回类型:Set<Pair<String,MetricCategory>>
  • 修饰符:public

参数:

说明:

TODO

代码

public class ActiveProfiler implements ProfileCollector {
    private static final long WARNING_TIME_NANOS = Duration.ofMillis(100L).toNanos();
    private static final Logger LOGGER = LogUtils.getLogger();
    private final List<String> paths = Lists.newArrayList();
    private final LongList startTimes = new LongArrayList();
    private final Map<String, ActiveProfiler.PathEntry> entries = Maps.newHashMap();
    private final IntSupplier getTickTime;
    private final LongSupplier getRealTime;
    private final long startTimeNano;
    private final int startTimeTicks;
    private String path = "";
    private boolean started;
    private ActiveProfiler.@Nullable PathEntry currentEntry;
    private final BooleanSupplier suppressWarnings;
    private final Set<Pair<String, MetricCategory>> chartedPaths = new ObjectArraySet<>();
 
    public ActiveProfiler(LongSupplier getRealTime, IntSupplier getTickTime, BooleanSupplier suppressWarnings) {
        this.startTimeNano = getRealTime.getAsLong();
        this.getRealTime = getRealTime;
        this.startTimeTicks = getTickTime.getAsInt();
        this.getTickTime = getTickTime;
        this.suppressWarnings = suppressWarnings;
    }
 
    @Override
    public void startTick() {
        if (this.started) {
            LOGGER.error("Profiler tick already started - missing endTick()?");
        } else {
            this.started = true;
            this.path = "";
            this.paths.clear();
            this.push("root");
        }
    }
 
    @Override
    public void endTick() {
        if (!this.started) {
            LOGGER.error("Profiler tick already ended - missing startTick()?");
        } else {
            this.pop();
            this.started = false;
            if (!this.path.isEmpty()) {
                LOGGER.error(
                    "Profiler tick ended before path was fully popped (remainder: '{}'). Mismatched push/pop?",
                    LogUtils.defer(() -> ProfileResults.demanglePath(this.path))
                );
            }
        }
    }
 
    @Override
    public void push(String name) {
        if (!this.started) {
            LOGGER.error("Cannot push '{}' to profiler if profiler tick hasn't started - missing startTick()?", name);
        } else {
            if (!this.path.isEmpty()) {
                this.path = this.path + "\u001e";
            }
 
            this.path = this.path + name;
            this.paths.add(this.path);
            this.startTimes.add(Util.getNanos());
            this.currentEntry = null;
        }
    }
 
    @Override
    public void push(Supplier<String> name) {
        this.push(name.get());
    }
 
    @Override
    public void markForCharting(MetricCategory category) {
        this.chartedPaths.add(Pair.of(this.path, category));
    }
 
    @Override
    public void pop() {
        if (!this.started) {
            LOGGER.error("Cannot pop from profiler if profiler tick hasn't started - missing startTick()?");
        } else if (this.startTimes.isEmpty()) {
            LOGGER.error("Tried to pop one too many times! Mismatched push() and pop()?");
        } else {
            long endTime = Util.getNanos();
            long startTime = this.startTimes.removeLong(this.startTimes.size() - 1);
            this.paths.removeLast();
            long time = endTime - startTime;
            ActiveProfiler.PathEntry currentEntry = this.getCurrentEntry();
            currentEntry.accumulatedDuration += time;
            currentEntry.count++;
            currentEntry.maxDuration = Math.max(currentEntry.maxDuration, time);
            currentEntry.minDuration = Math.min(currentEntry.minDuration, time);
            if (time > WARNING_TIME_NANOS && !this.suppressWarnings.getAsBoolean()) {
                LOGGER.warn(
                    "Something's taking too long! '{}' took aprox {} ms",
                    LogUtils.defer(() -> ProfileResults.demanglePath(this.path)),
                    LogUtils.defer(() -> time / 1000000.0)
                );
            }
 
            this.path = this.paths.isEmpty() ? "" : this.paths.getLast();
            this.currentEntry = null;
        }
    }
 
    @Override
    public void popPush(String name) {
        this.pop();
        this.push(name);
    }
 
    @Override
    public void popPush(Supplier<String> name) {
        this.pop();
        this.push(name);
    }
 
    private ActiveProfiler.PathEntry getCurrentEntry() {
        if (this.currentEntry == null) {
            this.currentEntry = this.entries.computeIfAbsent(this.path, key -> new ActiveProfiler.PathEntry());
        }
 
        return this.currentEntry;
    }
 
    @Override
    public void incrementCounter(String name, int amount) {
        this.getCurrentEntry().counters.addTo(name, amount);
    }
 
    @Override
    public void incrementCounter(Supplier<String> name, int amount) {
        this.getCurrentEntry().counters.addTo(name.get(), amount);
    }
 
    @Override
    public ProfileResults getResults() {
        return new FilledProfileResults(this.entries, this.startTimeNano, this.startTimeTicks, this.getRealTime.getAsLong(), this.getTickTime.getAsInt());
    }
 
    @Override
    public ActiveProfiler.@Nullable PathEntry getEntry(String path) {
        return this.entries.get(path);
    }
 
    @Override
    public Set<Pair<String, MetricCategory>> getChartedPaths() {
        return this.chartedPaths;
    }
 
    public static class PathEntry implements ProfilerPathEntry {
        private long maxDuration = Long.MIN_VALUE;
        private long minDuration = Long.MAX_VALUE;
        private long accumulatedDuration;
        private long count;
        private final Object2LongOpenHashMap<String> counters = new Object2LongOpenHashMap<>();
 
        @Override
        public long getDuration() {
            return this.accumulatedDuration;
        }
 
        @Override
        public long getMaxDuration() {
            return this.maxDuration;
        }
 
        @Override
        public long getCount() {
            return this.count;
        }
 
        @Override
        public Object2LongMap<String> getCounters() {
            return Object2LongMaps.unmodifiable(this.counters);
        }
    }
}

引用的其他类