2021-06-16 02:50:38 +02:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
|
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
|
|
|
|
Date: Fri, 19 Jul 2019 03:29:14 -0700
|
|
|
|
Subject: [PATCH] Add debug for sync chunk loads
|
|
|
|
|
|
|
|
This patch adds a tool to find calls to getChunkAt which would load
|
|
|
|
chunks, however it must be enabled by setting the startup flag
|
|
|
|
-Dpaper.debug-sync-loads=true
|
|
|
|
|
2021-08-25 01:45:40 +02:00
|
|
|
- To get a debug log for sync loads, the command is
|
|
|
|
/paper syncloadinfo
|
|
|
|
- To clear clear the currently stored sync load info, use
|
|
|
|
/paper syncloadinfo clear
|
2021-06-16 02:50:38 +02:00
|
|
|
|
|
|
|
diff --git a/src/main/java/com/destroystokyo/paper/io/SyncLoadFinder.java b/src/main/java/com/destroystokyo/paper/io/SyncLoadFinder.java
|
|
|
|
new file mode 100644
|
2021-08-25 01:45:40 +02:00
|
|
|
index 0000000000000000000000000000000000000000..0bb4aaa546939b67a5d22865190f30478a9337c1
|
2021-06-16 02:50:38 +02:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/src/main/java/com/destroystokyo/paper/io/SyncLoadFinder.java
|
2021-08-25 01:45:40 +02:00
|
|
|
@@ -0,0 +1,175 @@
|
2021-06-16 02:50:38 +02:00
|
|
|
+package com.destroystokyo.paper.io;
|
|
|
|
+
|
|
|
|
+import com.google.gson.JsonArray;
|
|
|
|
+import com.google.gson.JsonObject;
|
|
|
|
+import com.mojang.datafixers.util.Pair;
|
|
|
|
+import it.unimi.dsi.fastutil.longs.Long2IntMap;
|
|
|
|
+import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap;
|
|
|
|
+import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
|
|
|
+
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.WeakHashMap;
|
|
|
|
+import net.minecraft.world.level.Level;
|
|
|
|
+
|
|
|
|
+public class SyncLoadFinder {
|
|
|
|
+
|
|
|
|
+ public static final boolean ENABLED = Boolean.getBoolean("paper.debug-sync-loads");
|
|
|
|
+
|
|
|
|
+ private static final WeakHashMap<Level, Object2ObjectOpenHashMap<ThrowableWithEquals, SyncLoadInformation>> SYNC_LOADS = new WeakHashMap<>();
|
|
|
|
+
|
|
|
|
+ private static final class SyncLoadInformation {
|
|
|
|
+
|
|
|
|
+ public int times;
|
|
|
|
+
|
|
|
|
+ public final Long2IntOpenHashMap coordinateTimes = new Long2IntOpenHashMap();
|
|
|
|
+ }
|
|
|
|
+
|
2021-08-25 01:45:40 +02:00
|
|
|
+ public static void clear() {
|
|
|
|
+ SYNC_LOADS.clear();
|
|
|
|
+ }
|
|
|
|
+
|
2021-06-16 02:50:38 +02:00
|
|
|
+ public static void logSyncLoad(final Level world, final int chunkX, final int chunkZ) {
|
|
|
|
+ if (!ENABLED) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ final ThrowableWithEquals stacktrace = new ThrowableWithEquals(Thread.currentThread().getStackTrace());
|
|
|
|
+
|
|
|
|
+ SYNC_LOADS.compute(world, (final Level keyInMap, Object2ObjectOpenHashMap<ThrowableWithEquals, SyncLoadInformation> map) -> {
|
|
|
|
+ if (map == null) {
|
|
|
|
+ map = new Object2ObjectOpenHashMap<>();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ map.compute(stacktrace, (ThrowableWithEquals keyInMap0, SyncLoadInformation valueInMap) -> {
|
|
|
|
+ if (valueInMap == null) {
|
|
|
|
+ valueInMap = new SyncLoadInformation();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ++valueInMap.times;
|
|
|
|
+
|
|
|
|
+ valueInMap.coordinateTimes.compute(IOUtil.getCoordinateKey(chunkX, chunkZ), (Long keyInMap1, Integer valueInMap1) -> {
|
|
|
|
+ return valueInMap1 == null ? Integer.valueOf(1) : Integer.valueOf(valueInMap1.intValue() + 1);
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ return valueInMap;
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ return map;
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static JsonObject serialize() {
|
|
|
|
+ final JsonObject ret = new JsonObject();
|
|
|
|
+
|
|
|
|
+ final JsonArray worldsData = new JsonArray();
|
|
|
|
+
|
|
|
|
+ for (final Map.Entry<Level, Object2ObjectOpenHashMap<ThrowableWithEquals, SyncLoadInformation>> entry : SYNC_LOADS.entrySet()) {
|
|
|
|
+ final Level world = entry.getKey();
|
|
|
|
+
|
|
|
|
+ final JsonObject worldData = new JsonObject();
|
|
|
|
+
|
|
|
|
+ worldData.addProperty("name", world.getWorld().getName());
|
|
|
|
+
|
|
|
|
+ final List<Pair<ThrowableWithEquals, SyncLoadInformation>> data = new ArrayList<>();
|
|
|
|
+
|
|
|
|
+ entry.getValue().forEach((ThrowableWithEquals stacktrace, SyncLoadInformation times) -> {
|
|
|
|
+ data.add(new Pair<>(stacktrace, times));
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ data.sort((Pair<ThrowableWithEquals, SyncLoadInformation> pair1, Pair<ThrowableWithEquals, SyncLoadInformation> pair2) -> {
|
|
|
|
+ return Integer.compare(pair2.getSecond().times, pair1.getSecond().times); // reverse order
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ final JsonArray stacktraces = new JsonArray();
|
|
|
|
+
|
|
|
|
+ for (Pair<ThrowableWithEquals, SyncLoadInformation> pair : data) {
|
|
|
|
+ final JsonObject stacktrace = new JsonObject();
|
|
|
|
+
|
|
|
|
+ stacktrace.addProperty("times", pair.getSecond().times);
|
|
|
|
+
|
|
|
|
+ final JsonArray traces = new JsonArray();
|
|
|
|
+
|
|
|
|
+ for (StackTraceElement element : pair.getFirst().stacktrace) {
|
|
|
|
+ traces.add(String.valueOf(element));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ stacktrace.add("stacktrace", traces);
|
|
|
|
+
|
|
|
|
+ final JsonArray coordinates = new JsonArray();
|
|
|
|
+
|
|
|
|
+ for (Long2IntMap.Entry coordinate : pair.getSecond().coordinateTimes.long2IntEntrySet()) {
|
|
|
|
+ final long key = coordinate.getLongKey();
|
|
|
|
+ final int times = coordinate.getIntValue();
|
|
|
|
+ coordinates.add("(" + IOUtil.getCoordinateX(key) + "," + IOUtil.getCoordinateZ(key) + "): " + times);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ stacktrace.add("coordinates", coordinates);
|
|
|
|
+
|
|
|
|
+ stacktraces.add(stacktrace);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ worldData.add("stacktraces", stacktraces);
|
|
|
|
+ worldsData.add(worldData);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ret.add("worlds", worldsData);
|
|
|
|
+
|
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ static final class ThrowableWithEquals {
|
|
|
|
+
|
|
|
|
+ private final StackTraceElement[] stacktrace;
|
|
|
|
+ private final int hash;
|
|
|
|
+
|
|
|
|
+ public ThrowableWithEquals(final StackTraceElement[] stacktrace) {
|
|
|
|
+ this.stacktrace = stacktrace;
|
|
|
|
+ this.hash = ThrowableWithEquals.hash(stacktrace);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static int hash(final StackTraceElement[] stacktrace) {
|
|
|
|
+ int hash = 0;
|
|
|
|
+
|
|
|
|
+ for (int i = 0; i < stacktrace.length; ++i) {
|
|
|
|
+ hash *= 31;
|
|
|
|
+ hash += stacktrace[i].hashCode();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return hash;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public int hashCode() {
|
|
|
|
+ return this.hash;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean equals(final Object obj) {
|
|
|
|
+ if (obj == null || obj.getClass() != this.getClass()) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ final ThrowableWithEquals other = (ThrowableWithEquals)obj;
|
|
|
|
+ final StackTraceElement[] otherStackTrace = other.stacktrace;
|
|
|
|
+
|
|
|
|
+ if (this.stacktrace.length != otherStackTrace.length || this.hash != other.hash) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (this == obj) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (int i = 0; i < this.stacktrace.length; ++i) {
|
|
|
|
+ if (!this.stacktrace[i].equals(otherStackTrace[i])) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
2022-07-09 01:01:42 +02:00
|
|
|
diff --git a/src/main/java/io/papermc/paper/command/PaperCommand.java b/src/main/java/io/papermc/paper/command/PaperCommand.java
|
2023-02-19 15:57:10 +01:00
|
|
|
index 32b84c59722970218a1515e21c6454d0ea4f781d..8f35943e71911b6d49b4cf210e56f5e11c5753e8 100644
|
2022-07-09 01:01:42 +02:00
|
|
|
--- a/src/main/java/io/papermc/paper/command/PaperCommand.java
|
|
|
|
+++ b/src/main/java/io/papermc/paper/command/PaperCommand.java
|
|
|
|
@@ -5,6 +5,7 @@ import io.papermc.paper.command.subcommands.EntityCommand;
|
|
|
|
import io.papermc.paper.command.subcommands.FixLightCommand;
|
|
|
|
import io.papermc.paper.command.subcommands.HeapDumpCommand;
|
|
|
|
import io.papermc.paper.command.subcommands.ReloadCommand;
|
|
|
|
+import io.papermc.paper.command.subcommands.SyncLoadInfoCommand;
|
|
|
|
import io.papermc.paper.command.subcommands.VersionCommand;
|
2023-02-19 15:57:10 +01:00
|
|
|
import io.papermc.paper.command.subcommands.DumpPluginsCommand;
|
2022-07-09 01:01:42 +02:00
|
|
|
import it.unimi.dsi.fastutil.Pair;
|
2023-02-19 15:57:10 +01:00
|
|
|
@@ -46,6 +47,7 @@ public final class PaperCommand extends Command {
|
|
|
|
commands.put(Set.of("dumpplugins"), new DumpPluginsCommand());
|
2022-07-09 01:01:42 +02:00
|
|
|
commands.put(Set.of("fixlight"), new FixLightCommand());
|
2022-09-26 10:02:51 +02:00
|
|
|
commands.put(Set.of("debug", "chunkinfo", "holderinfo"), new ChunkDebugCommand());
|
2022-07-09 01:01:42 +02:00
|
|
|
+ commands.put(Set.of("syncloadinfo"), new SyncLoadInfoCommand());
|
|
|
|
|
|
|
|
return commands.entrySet().stream()
|
|
|
|
.flatMap(entry -> entry.getKey().stream().map(s -> Map.entry(s, entry.getValue())))
|
|
|
|
diff --git a/src/main/java/io/papermc/paper/command/subcommands/SyncLoadInfoCommand.java b/src/main/java/io/papermc/paper/command/subcommands/SyncLoadInfoCommand.java
|
|
|
|
new file mode 100644
|
2022-11-19 20:10:13 +01:00
|
|
|
index 0000000000000000000000000000000000000000..a73e35e7c71c1337c2846010b3ccd6875cae640e
|
2022-07-09 01:01:42 +02:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/src/main/java/io/papermc/paper/command/subcommands/SyncLoadInfoCommand.java
|
2022-11-19 20:10:13 +01:00
|
|
|
@@ -0,0 +1,88 @@
|
2022-07-09 01:01:42 +02:00
|
|
|
+package io.papermc.paper.command.subcommands;
|
|
|
|
+
|
|
|
|
+import com.destroystokyo.paper.io.SyncLoadFinder;
|
|
|
|
+import com.google.gson.JsonObject;
|
|
|
|
+import com.google.gson.internal.Streams;
|
|
|
|
+import com.google.gson.stream.JsonWriter;
|
|
|
|
+import io.papermc.paper.command.CommandUtil;
|
|
|
|
+import io.papermc.paper.command.PaperSubcommand;
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.FileOutputStream;
|
|
|
|
+import java.io.PrintStream;
|
|
|
|
+import java.io.StringWriter;
|
2022-11-19 20:10:13 +01:00
|
|
|
+import java.nio.charset.StandardCharsets;
|
2022-07-09 01:01:42 +02:00
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
|
+import java.util.List;
|
2022-11-19 20:10:13 +01:00
|
|
|
+
|
|
|
|
+import net.kyori.adventure.text.event.ClickEvent;
|
|
|
|
+import net.kyori.adventure.text.event.HoverEvent;
|
|
|
|
+import net.minecraft.server.MinecraftServer;
|
2022-07-09 01:01:42 +02:00
|
|
|
+import org.bukkit.command.CommandSender;
|
|
|
|
+import org.checkerframework.checker.nullness.qual.NonNull;
|
|
|
|
+import org.checkerframework.framework.qual.DefaultQualifier;
|
|
|
|
+
|
|
|
|
+import static net.kyori.adventure.text.Component.text;
|
|
|
|
+import static net.kyori.adventure.text.format.NamedTextColor.GRAY;
|
|
|
|
+import static net.kyori.adventure.text.format.NamedTextColor.GREEN;
|
|
|
|
+import static net.kyori.adventure.text.format.NamedTextColor.RED;
|
2022-11-19 20:10:13 +01:00
|
|
|
+import static net.kyori.adventure.text.format.NamedTextColor.WHITE;
|
2022-07-09 01:01:42 +02:00
|
|
|
+
|
|
|
|
+@DefaultQualifier(NonNull.class)
|
|
|
|
+public final class SyncLoadInfoCommand implements PaperSubcommand {
|
|
|
|
+ @Override
|
|
|
|
+ public boolean execute(final CommandSender sender, final String subCommand, final String[] args) {
|
|
|
|
+ this.doSyncLoadInfo(sender, args);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public List<String> tabComplete(final CommandSender sender, final String subCommand, final String[] args) {
|
|
|
|
+ return CommandUtil.getListMatchingLast(sender, args, "clear");
|
|
|
|
+ }
|
|
|
|
+
|
2022-11-19 20:10:13 +01:00
|
|
|
+ private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH.mm.ss");
|
|
|
|
+
|
2022-07-09 01:01:42 +02:00
|
|
|
+ private void doSyncLoadInfo(final CommandSender sender, final String[] args) {
|
|
|
|
+ if (!SyncLoadFinder.ENABLED) {
|
2022-11-19 20:10:13 +01:00
|
|
|
+ String systemFlag = "-Dpaper.debug-sync-loads=true";
|
|
|
|
+ sender.sendMessage(text().color(RED).append(text("This command requires the server startup flag '")).append(
|
|
|
|
+ text(systemFlag, WHITE).clickEvent(ClickEvent.copyToClipboard(systemFlag))
|
|
|
|
+ .hoverEvent(HoverEvent.showText(text("Click to copy the system flag")))).append(
|
|
|
|
+ text("' to be set.")));
|
2022-07-09 01:01:42 +02:00
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (args.length > 0 && args[0].equals("clear")) {
|
|
|
|
+ SyncLoadFinder.clear();
|
|
|
|
+ sender.sendMessage(text("Sync load data cleared.", GRAY));
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ File file = new File(new File(new File("."), "debug"),
|
2022-11-19 20:10:13 +01:00
|
|
|
+ "sync-load-info" + FORMATTER.format(LocalDateTime.now()) + ".txt");
|
2022-07-09 01:01:42 +02:00
|
|
|
+ file.getParentFile().mkdirs();
|
|
|
|
+ sender.sendMessage(text("Writing sync load info to " + file, GREEN));
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ final JsonObject data = SyncLoadFinder.serialize();
|
|
|
|
+
|
|
|
|
+ StringWriter stringWriter = new StringWriter();
|
|
|
|
+ JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
|
|
|
+ jsonWriter.setIndent(" ");
|
|
|
|
+ jsonWriter.setLenient(false);
|
|
|
|
+ Streams.write(data, jsonWriter);
|
|
|
|
+
|
|
|
|
+ try (
|
2022-11-19 20:10:13 +01:00
|
|
|
+ PrintStream out = new PrintStream(new FileOutputStream(file), false, StandardCharsets.UTF_8)
|
2022-07-09 01:01:42 +02:00
|
|
|
+ ) {
|
2022-11-19 20:10:13 +01:00
|
|
|
+ out.print(stringWriter);
|
2022-07-09 01:01:42 +02:00
|
|
|
+ }
|
|
|
|
+ sender.sendMessage(text("Successfully written sync load information!", GREEN));
|
|
|
|
+ } catch (Throwable thr) {
|
2022-11-19 20:10:13 +01:00
|
|
|
+ sender.sendMessage(text("Failed to write sync load information! See the console for more info.", RED));
|
|
|
|
+ MinecraftServer.LOGGER.warn("Error occurred while dumping sync chunk load info", thr);
|
2022-07-09 01:01:42 +02:00
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
2021-06-16 02:50:38 +02:00
|
|
|
diff --git a/src/main/java/net/minecraft/server/level/ServerChunkCache.java b/src/main/java/net/minecraft/server/level/ServerChunkCache.java
|
2022-12-07 20:22:28 +01:00
|
|
|
index f838a921a08981bac0d0c0a68e334ba06d11cb18..665e088cb0b73f6a0c62f29c56da462bab7c927e 100644
|
2021-06-16 02:50:38 +02:00
|
|
|
--- a/src/main/java/net/minecraft/server/level/ServerChunkCache.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/level/ServerChunkCache.java
|
2022-12-07 20:22:28 +01:00
|
|
|
@@ -445,6 +445,7 @@ public class ServerChunkCache extends ChunkSource {
|
2022-09-26 10:02:51 +02:00
|
|
|
// Paper start - async chunk io/loading
|
|
|
|
io.papermc.paper.chunk.system.scheduling.ChunkTaskScheduler.pushChunkWait(this.level, x1, z1); // Paper - rewrite chunk system
|
2021-06-16 02:50:38 +02:00
|
|
|
// Paper end
|
|
|
|
+ com.destroystokyo.paper.io.SyncLoadFinder.logSyncLoad(this.level, x1, z1); // Paper - sync load info
|
|
|
|
this.level.timings.syncChunkLoad.startTiming(); // Paper
|
2021-11-24 10:01:27 +01:00
|
|
|
chunkproviderserver_b.managedBlock(completablefuture::isDone);
|
2022-09-26 10:02:51 +02:00
|
|
|
io.papermc.paper.chunk.system.scheduling.ChunkTaskScheduler.popChunkWait(); // Paper - async chunk debug // Paper - rewrite chunk system
|
2021-06-16 02:50:38 +02:00
|
|
|
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
|
2023-02-13 18:52:27 +01:00
|
|
|
index 76c388347ebbff2d50a975b40dbe93cc2760f6bb..52a514b657485246827bf9c153303b8b5229bf5b 100644
|
2021-06-16 02:50:38 +02:00
|
|
|
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
|
2022-12-07 20:22:28 +01:00
|
|
|
@@ -424,6 +424,12 @@ public class ServerLevel extends Level implements WorldGenLevel {
|
2022-09-26 10:02:51 +02:00
|
|
|
return this.entityLookup;
|
|
|
|
}
|
|
|
|
// Paper end - rewrite chunk system
|
2021-06-16 02:50:38 +02:00
|
|
|
+ // Paper start
|
|
|
|
+ @Override
|
|
|
|
+ public boolean hasChunk(int chunkX, int chunkZ) {
|
|
|
|
+ return this.getChunkSource().getChunkAtIfLoadedImmediately(chunkX, chunkZ) != null;
|
|
|
|
+ }
|
|
|
|
+ // Paper end
|
|
|
|
|
|
|
|
// Paper start - optimise getPlayerByUUID
|
|
|
|
@Nullable
|