mirror of
https://github.com/PurpurMC/Purpur.git
synced 2026-02-17 16:37:43 +01:00
Add leaf's async chunk io patch back
This commit is contained in:
3687
patches/server/0053-Asynchronous-chunk-IO-and-loading.patch
Normal file
3687
patches/server/0053-Asynchronous-chunk-IO-and-loading.patch
Normal file
File diff suppressed because it is too large
Load Diff
291
patches/server/0054-Reduce-sync-loads.patch
Normal file
291
patches/server/0054-Reduce-sync-loads.patch
Normal file
@@ -0,0 +1,291 @@
|
||||
From d02b684c04499ecbdb0baade56dffc3922913f18 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] Reduce sync loads
|
||||
|
||||
This reduces calls to getChunkAt which would load chunks.
|
||||
|
||||
This patch also adds a tool to find calls which are doing this, however
|
||||
it must be enabled by setting the startup flag -Dpaper.debug-sync-loads=true
|
||||
|
||||
To get a debug log for sync loads, the command is /paper syncloadinfo
|
||||
---
|
||||
.../com/destroystokyo/paper/PaperCommand.java | 43 ++++++
|
||||
.../paper/io/SyncLoadFinder.java | 142 ++++++++++++++++++
|
||||
.../minecraft/server/ChunkProviderServer.java | 1 +
|
||||
src/main/java/net/minecraft/server/World.java | 6 +-
|
||||
4 files changed, 189 insertions(+), 3 deletions(-)
|
||||
create mode 100644 src/main/java/com/destroystokyo/paper/io/SyncLoadFinder.java
|
||||
|
||||
diff --git a/src/main/java/com/destroystokyo/paper/PaperCommand.java b/src/main/java/com/destroystokyo/paper/PaperCommand.java
|
||||
index 8db92edc36..c7ff5132e2 100644
|
||||
--- a/src/main/java/com/destroystokyo/paper/PaperCommand.java
|
||||
+++ b/src/main/java/com/destroystokyo/paper/PaperCommand.java
|
||||
@@ -1,9 +1,13 @@
|
||||
package com.destroystokyo.paper;
|
||||
|
||||
+import com.destroystokyo.paper.io.SyncLoadFinder;
|
||||
import com.google.common.base.Functions;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
+import com.google.gson.JsonObject;
|
||||
+import com.google.gson.internal.Streams;
|
||||
+import com.google.gson.stream.JsonWriter;
|
||||
import net.minecraft.server.*;
|
||||
import org.apache.commons.lang3.tuple.MutablePair;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
@@ -18,6 +22,9 @@ import org.bukkit.craftbukkit.CraftWorld;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.io.File;
|
||||
+import java.io.FileOutputStream;
|
||||
+import java.io.PrintStream;
|
||||
+import java.io.StringWriter;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.*;
|
||||
@@ -130,6 +137,9 @@ public class PaperCommand extends Command {
|
||||
case "chunkinfo":
|
||||
doChunkInfo(sender, args);
|
||||
break;
|
||||
+ case "syncloadinfo":
|
||||
+ this.doSyncLoadInfo(sender, args);
|
||||
+ break;
|
||||
case "ver":
|
||||
case "version":
|
||||
Command ver = org.bukkit.Bukkit.getServer().getCommandMap().getCommand("version");
|
||||
@@ -146,6 +156,39 @@ public class PaperCommand extends Command {
|
||||
return true;
|
||||
}
|
||||
|
||||
+ private void doSyncLoadInfo(CommandSender sender, String[] args) {
|
||||
+ if (!SyncLoadFinder.ENABLED) {
|
||||
+ sender.sendMessage(ChatColor.RED + "This command requires the server startup flag '-Dpaper.debug-sync-loads=true' to be set.");
|
||||
+ return;
|
||||
+ }
|
||||
+ File file = new File(new File(new File("."), "debug"),
|
||||
+ "sync-load-info" + DateTimeFormatter.ofPattern("yyyy-MM-dd_HH.mm.ss").format(LocalDateTime.now()) + ".txt");
|
||||
+ sender.sendMessage(ChatColor.GREEN + "Writing sync load info to " + file.toString());
|
||||
+
|
||||
+
|
||||
+ try {
|
||||
+ final JsonObject data = SyncLoadFinder.serialize();
|
||||
+
|
||||
+ StringWriter stringWriter = new StringWriter();
|
||||
+ JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
+ jsonWriter.setIndent(" ");
|
||||
+ jsonWriter.setLenient(false);
|
||||
+ Streams.write(data, jsonWriter);
|
||||
+
|
||||
+ String fileData = stringWriter.toString();
|
||||
+
|
||||
+ try (
|
||||
+ PrintStream out = new PrintStream(new FileOutputStream(file), false, "UTF-8")
|
||||
+ ) {
|
||||
+ out.print(fileData);
|
||||
+ }
|
||||
+ sender.sendMessage(ChatColor.GREEN + "Successfully written sync load information!");
|
||||
+ } catch (Throwable thr) {
|
||||
+ sender.sendMessage(ChatColor.RED + "Failed to write sync load information");
|
||||
+ thr.printStackTrace();
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
private void doChunkInfo(CommandSender sender, String[] args) {
|
||||
List<org.bukkit.World> worlds;
|
||||
if (args.length < 2 || args[1].equals("*")) {
|
||||
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
|
||||
index 0000000000..ad6c5ff0d5
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/com/destroystokyo/paper/io/SyncLoadFinder.java
|
||||
@@ -0,0 +1,142 @@
|
||||
+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.objects.Object2IntOpenHashMap;
|
||||
+import net.minecraft.server.World;
|
||||
+
|
||||
+import java.util.ArrayList;
|
||||
+import java.util.List;
|
||||
+import java.util.Map;
|
||||
+import java.util.WeakHashMap;
|
||||
+
|
||||
+public class SyncLoadFinder {
|
||||
+
|
||||
+ public static final boolean ENABLED = Boolean.getBoolean("paper.debug-sync-loads");
|
||||
+
|
||||
+ private static final WeakHashMap<World, Object2IntOpenHashMap<ThrowableWithEquals>> SYNC_LOADS = new WeakHashMap<>();
|
||||
+
|
||||
+ public static void logSyncLoad(final World world, final int chunkX, final int chunkZ) {
|
||||
+ if (!ENABLED) {
|
||||
+ return;
|
||||
+ }
|
||||
+
|
||||
+ final ThrowableWithEquals stacktrace = new ThrowableWithEquals(Thread.currentThread().getStackTrace());
|
||||
+
|
||||
+ SYNC_LOADS.compute(world, (final World keyInMap, Object2IntOpenHashMap<ThrowableWithEquals> map) -> {
|
||||
+ if (map == null) {
|
||||
+ map = new Object2IntOpenHashMap<>();
|
||||
+ }
|
||||
+
|
||||
+ map.computeInt(stacktrace, (ThrowableWithEquals keyInMap0, Integer valueInMap) -> {
|
||||
+ return valueInMap == null ? Integer.valueOf(1) : Integer.valueOf(valueInMap.intValue() + 1);
|
||||
+ });
|
||||
+
|
||||
+ return map;
|
||||
+ });
|
||||
+ }
|
||||
+
|
||||
+ public static JsonObject serialize() {
|
||||
+ final JsonObject ret = new JsonObject();
|
||||
+
|
||||
+ final JsonArray worldsData = new JsonArray();
|
||||
+
|
||||
+ for (final Map.Entry<World, Object2IntOpenHashMap<ThrowableWithEquals>> entry : SYNC_LOADS.entrySet()) {
|
||||
+ final World world = entry.getKey();
|
||||
+
|
||||
+ final JsonObject worldData = new JsonObject();
|
||||
+
|
||||
+ worldData.addProperty("name", world.getWorld().getName());
|
||||
+
|
||||
+ final List<Pair<ThrowableWithEquals, Integer>> data = new ArrayList<>();
|
||||
+
|
||||
+ entry.getValue().forEach((ThrowableWithEquals stacktrace, Integer times) -> {
|
||||
+ data.add(new Pair<>(stacktrace, times));
|
||||
+ });
|
||||
+
|
||||
+ data.sort((Pair<ThrowableWithEquals, Integer> pair1, Pair<ThrowableWithEquals, Integer> pair2) -> {
|
||||
+ return pair2.getSecond().compareTo(pair1.getSecond()); // reverse order
|
||||
+ });
|
||||
+
|
||||
+ final JsonArray stacktraces = new JsonArray();
|
||||
+
|
||||
+ for (Pair<ThrowableWithEquals, Integer> pair : data) {
|
||||
+ final JsonObject stacktrace = new JsonObject();
|
||||
+
|
||||
+ stacktrace.addProperty("times", pair.getSecond());
|
||||
+
|
||||
+ final JsonArray traces = new JsonArray();
|
||||
+
|
||||
+ for (StackTraceElement element : pair.getFirst().stacktrace) {
|
||||
+ traces.add(String.valueOf(element));
|
||||
+ }
|
||||
+
|
||||
+ stacktrace.add("stacktrace", traces);
|
||||
+
|
||||
+ 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) {
|
||||
+ 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;
|
||||
+ }
|
||||
+ }
|
||||
+}
|
||||
diff --git a/src/main/java/net/minecraft/server/ChunkProviderServer.java b/src/main/java/net/minecraft/server/ChunkProviderServer.java
|
||||
index e75e311376..7ade9a53b4 100644
|
||||
--- a/src/main/java/net/minecraft/server/ChunkProviderServer.java
|
||||
+++ b/src/main/java/net/minecraft/server/ChunkProviderServer.java
|
||||
@@ -321,6 +321,7 @@ public class ChunkProviderServer extends IChunkProvider {
|
||||
// Paper start - async chunk io // Paper start - async chunk loading
|
||||
this.world.asyncChunkTaskManager.raisePriority(x, z, com.destroystokyo.paper.io.PrioritizedTaskQueue.HIGHEST_PRIORITY);
|
||||
// Paper end
|
||||
+ com.destroystokyo.paper.io.SyncLoadFinder.logSyncLoad(this.world, x, z); // Paper - sync load info
|
||||
this.world.timings.chunkAwait.startTiming(); // Paper
|
||||
this.serverThreadQueue.awaitTasks(completablefuture::isDone);
|
||||
this.world.timings.chunkAwait.stopTiming(); // Paper
|
||||
diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java
|
||||
index 9f657b01fc..eb6929e2b0 100644
|
||||
--- a/src/main/java/net/minecraft/server/World.java
|
||||
+++ b/src/main/java/net/minecraft/server/World.java
|
||||
@@ -1252,7 +1252,7 @@ public abstract class World implements IIBlockAccess, GeneratorAccess, AutoClose
|
||||
|
||||
for (int i1 = i; i1 <= j; ++i1) {
|
||||
for (int j1 = k; j1 <= l; ++j1) {
|
||||
- Chunk chunk = this.getChunkProvider().getChunkAt(i1, j1, false);
|
||||
+ Chunk chunk = (Chunk)this.getChunkIfLoadedImmediately(i1, j1); // Paper
|
||||
|
||||
if (chunk != null) {
|
||||
chunk.a(entity, axisalignedbb, list, predicate);
|
||||
@@ -1272,7 +1272,7 @@ public abstract class World implements IIBlockAccess, GeneratorAccess, AutoClose
|
||||
|
||||
for (int i1 = i; i1 < j; ++i1) {
|
||||
for (int j1 = k; j1 < l; ++j1) {
|
||||
- Chunk chunk = this.getChunkProvider().getChunkAt(i1, j1, false);
|
||||
+ Chunk chunk = (Chunk)this.getChunkIfLoadedImmediately(i1, j1); // Paper
|
||||
|
||||
if (chunk != null) {
|
||||
chunk.a(entitytypes, axisalignedbb, list, predicate);
|
||||
@@ -1294,7 +1294,7 @@ public abstract class World implements IIBlockAccess, GeneratorAccess, AutoClose
|
||||
|
||||
for (int i1 = i; i1 < j; ++i1) {
|
||||
for (int j1 = k; j1 < l; ++j1) {
|
||||
- Chunk chunk = ichunkprovider.getChunkAt(i1, j1, false);
|
||||
+ Chunk chunk = (Chunk)this.getChunkIfLoadedImmediately(i1, j1); // Paper
|
||||
|
||||
if (chunk != null) {
|
||||
chunk.a(oclass, axisalignedbb, list, predicate);
|
||||
--
|
||||
2.20.1
|
||||
|
||||
Reference in New Issue
Block a user