2018-07-22 07:27:46 +02:00
From 9cb479ab95970eba850de078eaa105429a3024a7 Mon Sep 17 00:00:00 2001
2016-03-12 21:23:17 +01:00
From: Aikar <aikar@aikar.co>
Date: Fri, 4 Mar 2016 18:18:37 -0600
Subject: [PATCH] Chunk save queue improvements
For some unknown reason, Minecraft is sleeping 10ms between every single chunk being saved to disk.
Under high chunk load/unload activity (lots of movement / teleporting), this causes the chunk unload queue
to build up in size.
This has multiple impacts:
1) Performance of the unload queue itself - The save thread is pretty ineffecient for how it accesses it
By letting the queue get larger, checking and popping work off the queue can get less performant.
2) Performance of chunk loading - As with #1, chunk loads also have to check this queue when loading
chunk data so that it doesn't load stale data if new data is pending write to disk.
3) Memory Usage - The entire chunk has been serialized to NBT, and now sits in this queue. This leads to
elevated memory usage, and then the objects used in the serialization sit around longer than needed,
resulting in promotion to Old Generation instead of dying young.
2016-03-18 18:40:46 +01:00
To optimize this, we change the entire unload queue to be a proper queue. This improves the behavior of popping
the first queued chunk off, instead of abusing iterators like Mojang was doing.
2016-03-12 21:23:17 +01:00
2016-03-18 18:40:46 +01:00
This also improves reliability of chunk saving, as the previous hack job had a race condition that could
fail to save some chunks.
Then finally, Sleeping will by default be removed, but due to known issues with 1.9, a config option was added.
But if sleeps are to remain enabled, we at least lower the sleep interval so it doesn't have as much negative impact.
2016-03-12 21:23:17 +01:00
diff --git a/src/main/java/com/destroystokyo/paper/PaperConfig.java b/src/main/java/com/destroystokyo/paper/PaperConfig.java
2018-07-04 09:55:24 +02:00
index 36689db74..3898ad8fa 100644
2016-03-12 21:23:17 +01:00
--- a/src/main/java/com/destroystokyo/paper/PaperConfig.java
+++ b/src/main/java/com/destroystokyo/paper/PaperConfig.java
2017-04-22 08:16:45 +02:00
@@ -204,4 +204,10 @@ public class PaperConfig {
2016-06-05 00:26:11 +02:00
private static void chunkLoadThreads() {
2016-03-25 05:59:37 +01:00
minChunkLoadThreads = Math.min(6, getInt("settings.min-chunk-load-threads", 2)); // Keep people from doing stupid things with max of 6
2016-03-12 21:23:17 +01:00
}
+
+ public static boolean enableFileIOThreadSleep;
+ private static void enableFileIOThreadSleep() {
+ enableFileIOThreadSleep = getBoolean("settings.sleep-between-chunk-saves", false);
+ if (enableFileIOThreadSleep) Bukkit.getLogger().info("Enabled sleeping between chunk saves, beware of memory issues");
+ }
}
diff --git a/src/main/java/net/minecraft/server/ChunkRegionLoader.java b/src/main/java/net/minecraft/server/ChunkRegionLoader.java
2018-07-22 07:27:46 +02:00
index 5001fd11d..b247d5f07 100644
2016-03-12 21:23:17 +01:00
--- a/src/main/java/net/minecraft/server/ChunkRegionLoader.java
+++ b/src/main/java/net/minecraft/server/ChunkRegionLoader.java
2018-07-15 03:53:17 +02:00
@@ -25,6 +25,7 @@ import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
2016-05-12 04:07:46 +02:00
import javax.annotation.Nullable;
2016-03-12 21:23:17 +01:00
+import java.util.concurrent.ConcurrentLinkedQueue; // Paper
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2017-08-12 23:32:01 +02:00
// Spigot start
2018-07-15 03:53:17 +02:00
@@ -34,8 +35,21 @@ import org.spigotmc.SupplierUtils;
2016-03-12 21:23:17 +01:00
public class ChunkRegionLoader implements IChunkLoader, IAsyncChunkSaver {
2018-07-15 03:53:17 +02:00
+ // Paper start - Chunk queue improvements
+ private static class QueuedChunk {
+ public ChunkCoordIntPair coords;
+ public Supplier<NBTTagCompound> compoundSupplier;
+
+ public QueuedChunk(ChunkCoordIntPair coords, Supplier<NBTTagCompound> compoundSupplier) {
+ this.coords = coords;
+ this.compoundSupplier = compoundSupplier;
+ }
+ }
+ private ConcurrentLinkedQueue<QueuedChunk> queue = new ConcurrentLinkedQueue<>();
+ // Paper end
+
2016-03-12 21:23:17 +01:00
private static final Logger a = LogManager.getLogger();
2018-07-15 03:53:17 +02:00
- private final Object2ObjectMap<ChunkCoordIntPair, Supplier<NBTTagCompound>> b = Object2ObjectMaps.synchronize(new Object2ObjectLinkedOpenHashMap()); // Spigot
+ private final Object2ObjectMap<ChunkCoordIntPair, Supplier<NBTTagCompound>> b = new Object2ObjectLinkedOpenHashMap(); // Spigot // Paper - remove synchronized
private final File c;
private final DataFixer d;
private PersistentStructureLegacy e;
2018-07-22 07:27:46 +02:00
@@ -298,8 +312,8 @@ public class ChunkRegionLoader implements IChunkLoader, IAsyncChunkSaver {
2018-07-15 03:53:17 +02:00
}
};
}
-
- this.a(chunkcoordintpair, SupplierUtils.createUnivaluedSupplier(completion, unloaded && this.b.size() < SAVE_QUEUE_TARGET_SIZE));
+ this.a(chunkcoordintpair, SupplierUtils.createUnivaluedSupplier(completion, unloaded)); // Paper - Remove save queue target size
+ // Paper end
// Spigot end
} catch (Exception exception) {
ChunkRegionLoader.a.error("Failed to save chunk", exception);
2018-07-22 07:27:46 +02:00
@@ -308,18 +322,22 @@ public class ChunkRegionLoader implements IChunkLoader, IAsyncChunkSaver {
2018-07-15 03:53:17 +02:00
}
protected synchronized void a(ChunkCoordIntPair chunkcoordintpair, Supplier<NBTTagCompound> nbttagcompound) { // Spigot
+ queue.add(new QueuedChunk(chunkcoordintpair, nbttagcompound)); // Paper - Chunk queue improvements
this.b.put(chunkcoordintpair, nbttagcompound);
2016-03-12 21:23:17 +01:00
FileIOThread.a().a(this);
}
2018-07-15 03:53:17 +02:00
- public synchronized boolean a() {
+ public boolean a() { // Paper - remove synchronized
// CraftBukkit start
return this.processSaveQueueEntry(false);
2017-08-11 13:02:53 +02:00
}
2016-03-12 21:23:17 +01:00
2018-07-15 03:53:17 +02:00
- private synchronized boolean processSaveQueueEntry(boolean logCompletion) {
2017-08-12 23:32:01 +02:00
- Iterator<Map.Entry<ChunkCoordIntPair, Supplier<NBTTagCompound>>> iter = this.b.entrySet().iterator(); // Spigot
2016-08-28 01:41:58 +02:00
- if (!iter.hasNext()) {
2018-07-15 03:53:17 +02:00
+ private boolean processSaveQueueEntry(boolean logCompletion) { // Paper - dont synchronize during save
2017-08-11 13:02:53 +02:00
+ // CraftBukkit start
2016-03-12 21:23:17 +01:00
+ // Paper start - Chunk queue improvements
+ QueuedChunk chunk = queue.poll();
+ if (chunk == null) {
2017-08-11 13:02:53 +02:00
+ // Paper - end
if (logCompletion) {
// CraftBukkit end
2018-07-15 03:53:17 +02:00
ChunkRegionLoader.a.info("ThreadedAnvilChunkStorage ({}): All chunks are saved", this.c.getName());
2018-07-22 07:27:46 +02:00
@@ -327,17 +345,13 @@ public class ChunkRegionLoader implements IChunkLoader, IAsyncChunkSaver {
2016-03-12 21:23:17 +01:00
return false;
} else {
2016-08-28 01:41:58 +02:00
- // CraftBukkit start
2017-08-12 23:32:01 +02:00
- Map.Entry<ChunkCoordIntPair, Supplier<NBTTagCompound>> entry = iter.next(); // Spigot
2016-08-28 01:41:58 +02:00
- ChunkCoordIntPair chunkcoordintpair = entry.getKey();
2017-08-12 23:32:01 +02:00
- Supplier<NBTTagCompound> value = entry.getValue(); // Spigot
2016-08-28 01:41:58 +02:00
- // CraftBukkit end
2016-03-12 21:23:17 +01:00
+ ChunkCoordIntPair chunkcoordintpair = chunk.coords; // Paper - Chunk queue improvements
boolean flag;
try {
2018-07-15 03:53:17 +02:00
// NBTTagCompound nbttagcompound = (NBTTagCompound) this.b.get(chunkcoordintpair); // CraftBukkit
2017-08-12 23:32:01 +02:00
- NBTTagCompound nbttagcompound = SupplierUtils.getIfExists(value); // Spigot
+ NBTTagCompound nbttagcompound = SupplierUtils.getIfExists(chunk.compoundSupplier); // Spigot // Paper
2016-03-12 21:23:17 +01:00
if (nbttagcompound != null) {
2018-07-15 03:53:17 +02:00
try {
2018-07-22 07:27:46 +02:00
@@ -349,7 +363,14 @@ public class ChunkRegionLoader implements IChunkLoader, IAsyncChunkSaver {
2016-03-12 21:23:17 +01:00
flag = true;
} finally {
2017-08-12 23:32:01 +02:00
- this.b.remove(chunkcoordintpair, value); // CraftBukkit // Spigot
2018-07-15 03:53:17 +02:00
+ // Paper start - only synchronize here
+ synchronized (this) {
+ // This will not equal if a newer version is still pending - wait until newest is saved to remove
+ if (this.b.get(chunkcoordintpair) == chunk.compoundSupplier) {
+ this.b.remove(chunkcoordintpair);
+ }
+ }
+ // Paper start
2016-03-12 21:23:17 +01:00
}
return flag;
diff --git a/src/main/java/net/minecraft/server/FileIOThread.java b/src/main/java/net/minecraft/server/FileIOThread.java
2018-07-15 03:53:17 +02:00
index 34312667a..549fab9a5 100644
2016-03-12 21:23:17 +01:00
--- a/src/main/java/net/minecraft/server/FileIOThread.java
+++ b/src/main/java/net/minecraft/server/FileIOThread.java
2018-07-15 03:53:17 +02:00
@@ -43,11 +43,12 @@ public class FileIOThread implements Runnable {
++this.e;
2016-03-12 21:23:17 +01:00
}
2018-07-15 03:53:17 +02:00
+ if (com.destroystokyo.paper.PaperConfig.enableFileIOThreadSleep) { // Paper
try {
- Thread.sleep(this.f ? 0L : 10L);
+ Thread.sleep(this.f ? 0L : 1L); // Paper
} catch (InterruptedException interruptedexception) {
interruptedexception.printStackTrace();
- }
+ }} // Paper
2016-03-12 21:23:17 +01:00
}
2018-07-15 03:53:17 +02:00
if (this.c.isEmpty()) {
2016-03-12 21:23:17 +01:00
--
2018-07-04 09:55:24 +02:00
2.18.0
2016-03-12 21:23:17 +01:00