| 1 | package net.minecraft.server.packs.resources;
|
| 2 |
|
| 3 | import com.google.common.base.Stopwatch;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import java.util.List;
|
| 6 | import java.util.concurrent.CompletableFuture;
|
| 7 | import java.util.concurrent.Executor;
|
| 8 | import java.util.concurrent.TimeUnit;
|
| 9 | import java.util.concurrent.atomic.AtomicLong;
|
| 10 | import net.minecraft.util.Unit;
|
| 11 | import net.minecraft.util.Util;
|
| 12 | import net.minecraft.util.profiling.Profiler;
|
| 13 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 14 | import org.slf4j.Logger;
|
| 15 |
|
| 16 | public class ProfiledReloadInstance extends SimpleReloadInstance<ProfiledReloadInstance.State> {
|
| 17 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 18 | private final Stopwatch total = Stopwatch.createUnstarted();
|
| 19 |
|
| 20 | public static ReloadInstance of(
|
| 21 | final ResourceManager resourceManager,
|
| 22 | final List<PreparableReloadListener> listeners,
|
| 23 | final Executor taskExecutor,
|
| 24 | final Executor mainThreadExecutor,
|
| 25 | final CompletableFuture<Unit> initialTask
|
| 26 | ) {
|
| 27 | ProfiledReloadInstance result = new ProfiledReloadInstance(listeners);
|
| 28 | result.startTasks(
|
| 29 | taskExecutor,
|
| 30 | mainThreadExecutor,
|
| 31 | resourceManager,
|
| 32 | listeners,
|
| 33 | (currentReload, previousStep, listener, parentTaskExecutor, parentReloadExecutor) -> {
|
| 34 | AtomicLong preparationNanos = new AtomicLong();
|
| 35 | AtomicLong preparationCount = new AtomicLong();
|
| 36 | AtomicLong reloadNanos = new AtomicLong();
|
| 37 | AtomicLong reloadCount = new AtomicLong();
|
| 38 | CompletableFuture<Void> reload = listener.reload(
|
| 39 | currentReload,
|
| 40 | profiledExecutor(parentTaskExecutor, preparationNanos, preparationCount, listener.getName()),
|
| 41 | previousStep,
|
| 42 | profiledExecutor(parentReloadExecutor, reloadNanos, reloadCount, listener.getName())
|
| 43 | );
|
| 44 | return reload.thenApplyAsync(v -> {
|
| 45 | LOGGER.debug("Finished reloading {}", listener.getName());
|
| 46 | return new ProfiledReloadInstance.State(listener.getName(), preparationNanos, preparationCount, reloadNanos, reloadCount);
|
| 47 | }, mainThreadExecutor);
|
| 48 | },
|
| 49 | initialTask
|
| 50 | );
|
| 51 | return result;
|
| 52 | }
|
| 53 |
|
| 54 | private ProfiledReloadInstance(final List<PreparableReloadListener> listeners) {
|
| 55 | super(listeners);
|
| 56 | this.total.start();
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | protected CompletableFuture<List<ProfiledReloadInstance.State>> prepareTasks(
|
| 61 | final Executor taskExecutor,
|
| 62 | final Executor mainThreadExecutor,
|
| 63 | final ResourceManager resourceManager,
|
| 64 | final List<PreparableReloadListener> listeners,
|
| 65 | final SimpleReloadInstance.StateFactory<ProfiledReloadInstance.State> stateFactory,
|
| 66 | final CompletableFuture<?> initialTask
|
| 67 | ) {
|
| 68 | return super.prepareTasks(taskExecutor, mainThreadExecutor, resourceManager, listeners, stateFactory, initialTask)
|
| 69 | .thenApplyAsync(this::finish, mainThreadExecutor);
|
| 70 | }
|
| 71 |
|
| 72 | private static Executor profiledExecutor(final Executor executor, final AtomicLong accumulatedNanos, final AtomicLong taskCount, final String name) {
|
| 73 | return r -> executor.execute(() -> {
|
| 74 | ProfilerFiller profiler = Profiler.get();
|
| 75 | profiler.push(name);
|
| 76 | long nanos = Util.getNanos();
|
| 77 | r.run();
|
| 78 | accumulatedNanos.addAndGet(Util.getNanos() - nanos);
|
| 79 | taskCount.incrementAndGet();
|
| 80 | profiler.pop();
|
| 81 | });
|
| 82 | }
|
| 83 |
|
| 84 | private List<ProfiledReloadInstance.State> finish(final List<ProfiledReloadInstance.State> result) {
|
| 85 | this.total.stop();
|
| 86 | long blockingTime = 0L;
|
| 87 | LOGGER.info("Resource reload finished after {} ms", this.total.elapsed(TimeUnit.MILLISECONDS));
|
| 88 |
|
| 89 | for (ProfiledReloadInstance.State state : result) {
|
| 90 | long prepTime = TimeUnit.NANOSECONDS.toMillis(state.preparationNanos.get());
|
| 91 | long prepCount = state.preparationCount.get();
|
| 92 | long reloadTime = TimeUnit.NANOSECONDS.toMillis(state.reloadNanos.get());
|
| 93 | long reloadCount = state.reloadCount.get();
|
| 94 | long totalTime = prepTime + reloadTime;
|
| 95 | long totalCount = prepCount + reloadCount;
|
| 96 | String name = state.name;
|
| 97 | LOGGER.info(
|
| 98 | "{} took approximately {} tasks/{} ms ({} tasks/{} ms preparing, {} tasks/{} ms applying)",
|
| 99 | name,
|
| 100 | totalCount,
|
| 101 | totalTime,
|
| 102 | prepCount,
|
| 103 | prepTime,
|
| 104 | reloadCount,
|
| 105 | reloadTime
|
| 106 | );
|
| 107 | blockingTime += reloadTime;
|
| 108 | }
|
| 109 |
|
| 110 | LOGGER.info("Total blocking time: {} ms", blockingTime);
|
| 111 | return result;
|
| 112 | }
|
| 113 |
|
| 114 | public record State(String name, AtomicLong preparationNanos, AtomicLong preparationCount, AtomicLong reloadNanos, AtomicLong reloadCount) {
|
| 115 | }
|
| 116 | }
|
| 117 |
|