2018-09-09 19:29:22 +00:00
|
|
|
From c7b9d824fc6f8304e7d1e82fccaf1b0fc2a0233d Mon Sep 17 00:00:00 2001
|
2016-03-12 20:23:17 +00: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 17:40:46 +00: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 20:23:17 +00:00
|
|
|
|
2016-03-18 17:40:46 +00: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 20:23:17 +00:00
|
|
|
|
|
|
|
diff --git a/src/main/java/com/destroystokyo/paper/PaperConfig.java b/src/main/java/com/destroystokyo/paper/PaperConfig.java
|
2018-08-26 18:11:49 +00:00
|
|
|
index 0d68ffd75a..fd00c320ce 100644
|
2016-03-12 20:23:17 +00:00
|
|
|
--- a/src/main/java/com/destroystokyo/paper/PaperConfig.java
|
|
|
|
+++ b/src/main/java/com/destroystokyo/paper/PaperConfig.java
|
2018-07-24 00:24:44 +00:00
|
|
|
@@ -208,4 +208,10 @@ public class PaperConfig {
|
2016-06-04 22:26:11 +00:00
|
|
|
private static void chunkLoadThreads() {
|
2016-03-25 04:59:37 +00: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 20:23:17 +00: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-09-09 19:29:22 +00:00
|
|
|
index 2e9bd0949a..1dbcedbf94 100644
|
2016-03-12 20:23:17 +00:00
|
|
|
--- a/src/main/java/net/minecraft/server/ChunkRegionLoader.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/ChunkRegionLoader.java
|
2018-08-26 18:11:49 +00:00
|
|
|
@@ -22,6 +22,7 @@ import java.util.function.Consumer;
|
2018-07-15 01:53:17 +00:00
|
|
|
import java.util.function.Function;
|
|
|
|
import java.util.function.Predicate;
|
2016-05-12 02:07:46 +00:00
|
|
|
import javax.annotation.Nullable;
|
2016-03-12 20:23:17 +00:00
|
|
|
+import java.util.concurrent.ConcurrentLinkedQueue; // Paper
|
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2017-08-12 21:32:01 +00:00
|
|
|
// Spigot start
|
2018-09-09 19:29:22 +00:00
|
|
|
@@ -31,6 +32,26 @@ import org.spigotmc.SupplierUtils;
|
2016-03-12 20:23:17 +00:00
|
|
|
|
|
|
|
public class ChunkRegionLoader implements IChunkLoader, IAsyncChunkSaver {
|
|
|
|
|
2018-07-15 01:53:17 +00:00
|
|
|
+ // Paper start - Chunk queue improvements
|
|
|
|
+ private static class QueuedChunk {
|
|
|
|
+ public ChunkCoordIntPair coords;
|
|
|
|
+ public Supplier<NBTTagCompound> compoundSupplier;
|
2018-09-09 19:29:22 +00:00
|
|
|
+ public Runnable onSave;
|
|
|
|
+
|
|
|
|
+ public QueuedChunk(Runnable run) {
|
|
|
|
+ this.coords = null;
|
|
|
|
+ this.compoundSupplier = null;
|
|
|
|
+ this.onSave = run;
|
|
|
|
+ }
|
2018-07-15 01:53:17 +00:00
|
|
|
+
|
|
|
|
+ public QueuedChunk(ChunkCoordIntPair coords, Supplier<NBTTagCompound> compoundSupplier) {
|
|
|
|
+ this.coords = coords;
|
|
|
|
+ this.compoundSupplier = compoundSupplier;
|
|
|
|
+ }
|
|
|
|
+ }
|
2018-08-26 18:11:49 +00:00
|
|
|
+ final private ConcurrentLinkedQueue<QueuedChunk> queue = new ConcurrentLinkedQueue<>();
|
2018-07-15 01:53:17 +00:00
|
|
|
+ // Paper end
|
|
|
|
+
|
2016-03-12 20:23:17 +00:00
|
|
|
private static final Logger a = LogManager.getLogger();
|
2018-09-09 19:26:56 +00:00
|
|
|
private final Map<ChunkCoordIntPair, Supplier<NBTTagCompound>> b = java.util.Collections.synchronizedMap(Maps.newHashMap()); // CraftBukkit // Spigot
|
2018-07-15 01:53:17 +00:00
|
|
|
private final File c;
|
2018-09-09 19:29:22 +00:00
|
|
|
@@ -305,8 +326,8 @@ public class ChunkRegionLoader implements IChunkLoader, IAsyncChunkSaver {
|
2018-07-15 01:53:17 +00: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-09-09 19:29:22 +00:00
|
|
|
@@ -315,7 +336,10 @@ public class ChunkRegionLoader implements IChunkLoader, IAsyncChunkSaver {
|
2018-07-15 01:53:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-26 18:11:49 +00:00
|
|
|
protected void a(ChunkCoordIntPair chunkcoordintpair, Supplier<NBTTagCompound> nbttagcompound) { // Spigot
|
|
|
|
- this.b.put(chunkcoordintpair, nbttagcompound);
|
2018-09-09 19:29:22 +00:00
|
|
|
+ synchronized (this.b) { // Paper - synchronize while modifying the map
|
2018-08-26 18:11:49 +00:00
|
|
|
+ queue.add(new QueuedChunk(chunkcoordintpair, nbttagcompound)); // Paper - Chunk queue improvements
|
|
|
|
+ this.b.put(chunkcoordintpair, nbttagcompound);
|
|
|
|
+ }
|
2016-03-12 20:23:17 +00:00
|
|
|
FileIOThread.a().a(this);
|
|
|
|
}
|
2018-07-15 01:53:17 +00:00
|
|
|
|
2018-09-09 19:29:22 +00:00
|
|
|
@@ -325,20 +349,24 @@ public class ChunkRegionLoader implements IChunkLoader, IAsyncChunkSaver {
|
2017-08-11 11:02:53 +00:00
|
|
|
}
|
2016-03-12 20:23:17 +00:00
|
|
|
|
2018-08-26 18:11:49 +00:00
|
|
|
private boolean processSaveQueueEntry(boolean logCompletion) {
|
2018-09-09 19:26:56 +00:00
|
|
|
- synchronized (this.b) { // CraftBukkit
|
2018-08-26 18:11:49 +00:00
|
|
|
- Iterator iterator = this.b.entrySet().iterator();
|
|
|
|
- if (!iterator.hasNext()) {
|
2016-03-12 20:23:17 +00:00
|
|
|
+ // Paper start - Chunk queue improvements
|
|
|
|
+ QueuedChunk chunk = queue.poll();
|
|
|
|
+ if (chunk == null) {
|
2017-08-11 11:02:53 +00:00
|
|
|
+ // Paper - end
|
2018-08-26 18:11:49 +00:00
|
|
|
if (logCompletion) { // CraftBukkit
|
2018-07-15 01:53:17 +00:00
|
|
|
ChunkRegionLoader.a.info("ThreadedAnvilChunkStorage ({}): All chunks are saved", this.c.getName());
|
2018-08-26 18:11:49 +00:00
|
|
|
}
|
2016-03-12 20:23:17 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
} else {
|
2018-08-26 18:11:49 +00:00
|
|
|
- Entry entry = (Entry) iterator.next();
|
|
|
|
-
|
|
|
|
- iterator.remove();
|
|
|
|
- ChunkCoordIntPair chunkcoordintpair = (ChunkCoordIntPair) entry.getKey();
|
|
|
|
- Supplier<NBTTagCompound> nbttagcompound = (Supplier<NBTTagCompound>) entry.getValue(); // Spigot
|
2018-09-09 19:29:22 +00:00
|
|
|
+ // Paper start
|
|
|
|
+ if (chunk.onSave != null) {
|
|
|
|
+ chunk.onSave.run();
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ // Paper end
|
2016-03-12 20:23:17 +00:00
|
|
|
+ ChunkCoordIntPair chunkcoordintpair = chunk.coords; // Paper - Chunk queue improvements
|
2018-08-26 18:11:49 +00:00
|
|
|
+ Supplier<NBTTagCompound> nbttagcompound = chunk.compoundSupplier; // Spigot // Paper
|
2016-03-12 20:23:17 +00:00
|
|
|
|
2018-08-26 18:11:49 +00:00
|
|
|
if (nbttagcompound == null) {
|
|
|
|
return true;
|
2018-09-09 19:29:22 +00:00
|
|
|
@@ -347,6 +375,14 @@ public class ChunkRegionLoader implements IChunkLoader, IAsyncChunkSaver {
|
2018-08-26 18:11:49 +00:00
|
|
|
// CraftBukkit start
|
|
|
|
RegionFileCache.write(this.c, chunkcoordintpair.x, chunkcoordintpair.z, SupplierUtils.getIfExists(nbttagcompound)); // Spigot
|
2016-03-12 20:23:17 +00:00
|
|
|
|
2018-08-26 18:11:49 +00:00
|
|
|
+ // Paper start remove from map only if this was the latest version of the chunk
|
2018-09-09 19:29:22 +00:00
|
|
|
+ synchronized (this.b) {
|
2018-08-26 18:11:49 +00:00
|
|
|
+ // 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);
|
|
|
|
+ }
|
2018-07-15 01:53:17 +00:00
|
|
|
+ }
|
2018-08-26 18:11:49 +00:00
|
|
|
+ // Paper end
|
|
|
|
/*
|
|
|
|
NBTCompressedStreamTools.a(nbttagcompound, (DataOutput) dataoutputstream);
|
|
|
|
dataoutputstream.close();
|
2018-09-09 19:29:22 +00:00
|
|
|
@@ -362,7 +398,7 @@ public class ChunkRegionLoader implements IChunkLoader, IAsyncChunkSaver {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
- } // CraftBukkit
|
|
|
|
+ // } // CraftBukkit // Paper
|
|
|
|
}
|
|
|
|
|
|
|
|
private ChunkStatus.Type a(@Nullable NBTTagCompound nbttagcompound) {
|
2016-03-12 20:23:17 +00:00
|
|
|
diff --git a/src/main/java/net/minecraft/server/FileIOThread.java b/src/main/java/net/minecraft/server/FileIOThread.java
|
2018-08-31 22:56:57 +00:00
|
|
|
index a3aba244af..97917551a4 100644
|
2016-03-12 20:23:17 +00:00
|
|
|
--- a/src/main/java/net/minecraft/server/FileIOThread.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/FileIOThread.java
|
2018-08-31 22:56:57 +00:00
|
|
|
@@ -35,20 +35,21 @@ public class FileIOThread implements Runnable {
|
|
|
|
for(int i = 0; i < this.c.size(); ++i) {
|
|
|
|
IAsyncChunkSaver iasyncchunksaver = (IAsyncChunkSaver)this.c.get(i);
|
2018-08-27 02:09:10 +00:00
|
|
|
boolean flag;
|
2018-08-31 22:56:57 +00:00
|
|
|
- synchronized(iasyncchunksaver) {
|
|
|
|
+ //synchronized(iasyncchunksaver) { // Paper - remove synchronized
|
2018-08-27 02:09:10 +00:00
|
|
|
flag = iasyncchunksaver.a();
|
|
|
|
- }
|
|
|
|
+ //} // Paper
|
|
|
|
|
|
|
|
if (!flag) {
|
|
|
|
this.c.remove(i--);
|
2018-07-15 01:53:17 +00:00
|
|
|
++this.e;
|
2016-03-12 20:23:17 +00:00
|
|
|
}
|
|
|
|
|
2018-07-15 01:53:17 +00:00
|
|
|
+ if (com.destroystokyo.paper.PaperConfig.enableFileIOThreadSleep) { // Paper
|
|
|
|
try {
|
|
|
|
- Thread.sleep(this.f ? 0L : 10L);
|
|
|
|
+ Thread.sleep(this.f ? 0L : 1L); // Paper
|
2018-08-31 22:56:57 +00:00
|
|
|
} catch (InterruptedException interruptedexception1) {
|
|
|
|
interruptedexception1.printStackTrace();
|
2018-07-15 01:53:17 +00:00
|
|
|
- }
|
|
|
|
+ }} // Paper
|
2016-03-12 20:23:17 +00:00
|
|
|
}
|
|
|
|
|
2018-07-15 01:53:17 +00:00
|
|
|
if (this.c.isEmpty()) {
|
2016-03-12 20:23:17 +00:00
|
|
|
--
|
2018-07-04 07:55:24 +00:00
|
|
|
2.18.0
|
2016-03-12 20:23:17 +00:00
|
|
|
|