Paper/patches/server/0397-Mid-Tick-Chunk-Tasks-Speed-up-processing-of-chunk-lo.patch

270 lines
14 KiB
Diff
Raw Normal View History

2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Thu, 9 Apr 2020 00:09:26 -0400
Subject: [PATCH] Mid Tick Chunk Tasks - Speed up processing of chunk loads and
generation
Credit to Spotted for the idea
A lot of the new chunk system requires constant back and forth the main thread
to handle priority scheduling and ensuring conflicting tasks do not run at the
same time.
The issue is, these queues are only checked at either:
A) Sync Chunk Loads
B) End of Tick while sleeping
This results in generating chunks sitting waiting for a full tick to
complete before it will even start the next unit of work to do.
Additionally, this also delays loading of chunks until this same timing.
We will now periodically poll the chunk task queues throughout the tick,
looking for work to do.
We do this in a fair method that considers all worlds, not just the one being
ticked, so that each world can get 1 task procesed each before the next pass.
In a view distance of 15, chunk loading performance was visually faster on the client.
Flying at high speed in spectator mode was able to keep up with chunk loading (as long as they are already generated)
diff --git a/src/main/java/co/aikar/timings/MinecraftTimings.java b/src/main/java/co/aikar/timings/MinecraftTimings.java
index b47b7dce26805badd422c1867733ff4bfd00e9f4..b9cdbf8acccfd6b207a0116f068168f3b8c8e17d 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/co/aikar/timings/MinecraftTimings.java
+++ b/src/main/java/co/aikar/timings/MinecraftTimings.java
2021-06-14 00:05:18 +02:00
@@ -16,6 +16,7 @@ import java.util.Map;
2021-06-11 14:02:28 +02:00
public final class MinecraftTimings {
public static final Timing serverOversleep = Timings.ofSafe("Server Oversleep");
+ public static final Timing midTickChunkTasks = Timings.ofSafe("Mid Tick Chunk Tasks");
public static final Timing playerListTimer = Timings.ofSafe("Player List");
public static final Timing commandFunctionsTimer = Timings.ofSafe("Command Functions");
public static final Timing connectionTimer = Timings.ofSafe("Connection Handler");
diff --git a/src/main/java/com/destroystokyo/paper/PaperConfig.java b/src/main/java/com/destroystokyo/paper/PaperConfig.java
2021-07-07 08:52:40 +02:00
index bfdf4b302860d56dec485af77c69d18db22dc6f4..5c67c51dd0a19357086d4ceb3ca724401e4d26b8 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/com/destroystokyo/paper/PaperConfig.java
+++ b/src/main/java/com/destroystokyo/paper/PaperConfig.java
@@ -410,4 +410,9 @@ public class PaperConfig {
log("Async Chunks: Enabled - Chunks will be loaded much faster, without lag.");
}
}
+
+ public static int midTickChunkTasks = 1000;
+ private static void midTickChunkTasks() {
+ midTickChunkTasks = getInt("settings.chunk-tasks-per-tick", midTickChunkTasks);
+ }
}
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
2021-07-07 08:52:40 +02:00
index ce438760cbc92c08c079d06a8b97eaeda1018491..0115ffe84356468ddc254d8d5bdd719bc5e7e3f8 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
2021-07-07 08:52:40 +02:00
@@ -1120,6 +1120,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
2021-06-11 14:02:28 +02:00
// Paper end
tickSection = curTime;
}
+ midTickChunksTasksRan = 0; // Paper
// Spigot end
2021-06-14 00:05:18 +02:00
if (this.debugCommandProfilerDelayStart) {
2021-07-07 08:52:40 +02:00
@@ -1194,7 +1195,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
2021-06-11 14:02:28 +02:00
}
- private boolean haveTime() {
+ public boolean haveTime() { // Paper
// CraftBukkit start
if (isOversleep) return canOversleep();// Paper - because of our changes, this logic is broken
return this.forceTicks || this.runningTask() || Util.getMillis() < (this.mayHaveDelayedTasks ? this.delayedTasksMaxNextTickTime : this.nextTickTime);
2021-07-07 08:52:40 +02:00
@@ -1224,6 +1225,23 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
2021-06-11 14:02:28 +02:00
});
}
+ // Paper start
+ public int midTickChunksTasksRan = 0;
+ private long midTickLastRan = 0;
+ public void midTickLoadChunks() {
+ if (!isSameThread() || System.nanoTime() - midTickLastRan < 1000000) {
+ // only check once per 0.25ms incase this code is called in a hot method
+ return;
+ }
+ try (co.aikar.timings.Timing ignored = co.aikar.timings.MinecraftTimings.midTickChunkTasks.startTiming()) {
+ for (ServerLevel value : this.getAllLevels()) {
+ value.getChunkSource().mainThreadProcessor.midTickLoadChunks();
+ }
+ midTickLastRan = System.nanoTime();
+ }
+ }
+ // Paper end
+
@Override
public TickTask wrapRunnable(Runnable runnable) {
return new TickTask(this.tickCount, runnable);
2021-07-07 08:52:40 +02:00
@@ -1313,6 +1331,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
2021-06-11 14:02:28 +02:00
// Paper start - move oversleep into full server tick
isOversleep = true;MinecraftTimings.serverOversleep.startTiming();
this.managedBlock(() -> {
+ midTickLoadChunks(); // will only do loads since we are still considered !canSleepForTick
return !this.canOversleep();
});
isOversleep = false;MinecraftTimings.serverOversleep.stopTiming();
2021-07-07 08:52:40 +02:00
@@ -1381,13 +1400,16 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
2021-06-11 14:02:28 +02:00
}
2021-06-14 00:05:18 +02:00
public void tickChildren(BooleanSupplier shouldKeepTicking) {
2021-06-11 14:02:28 +02:00
+ midTickLoadChunks(); // Paper
MinecraftTimings.bukkitSchedulerTimer.startTiming(); // Spigot // Paper
this.server.getScheduler().mainThreadHeartbeat(this.tickCount); // CraftBukkit
MinecraftTimings.bukkitSchedulerTimer.stopTiming(); // Spigot // Paper
+ midTickLoadChunks(); // Paper
this.profiler.push("commandFunctions");
MinecraftTimings.commandFunctionsTimer.startTiming(); // Spigot // Paper
this.getFunctions().tick();
MinecraftTimings.commandFunctionsTimer.stopTiming(); // Spigot // Paper
+ midTickLoadChunks(); // Paper
this.profiler.popPush("levels");
Iterator iterator = this.getAllLevels().iterator();
2021-07-07 08:52:40 +02:00
@@ -1398,7 +1420,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
2021-06-14 00:05:18 +02:00
this.processQueue.remove().run();
2021-06-11 14:02:28 +02:00
}
MinecraftTimings.processQueueTimer.stopTiming(); // Spigot
-
+ midTickLoadChunks(); // Paper
MinecraftTimings.timeUpdateTimer.startTiming(); // Spigot // Paper
// Send time updates to everyone, it will get the right time from the world the player is in.
// Paper start - optimize time updates
2021-07-07 08:52:40 +02:00
@@ -1440,9 +1462,11 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
2021-06-11 14:02:28 +02:00
this.profiler.push("tick");
try {
+ midTickLoadChunks(); // Paper
worldserver.timings.doTick.startTiming(); // Spigot
worldserver.tick(shouldKeepTicking);
worldserver.timings.doTick.stopTiming(); // Spigot
+ midTickLoadChunks(); // Paper
} catch (Throwable throwable) {
// Spigot Start
CrashReport crashreport;
diff --git a/src/main/java/net/minecraft/server/level/ServerChunkCache.java b/src/main/java/net/minecraft/server/level/ServerChunkCache.java
2021-07-07 08:52:40 +02:00
index a4c043d33b83003b440eb2baf1cc1bc7081019d3..902394e45ed6d34aa33ed8d3c74ddcaf0adbe83c 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/level/ServerChunkCache.java
+++ b/src/main/java/net/minecraft/server/level/ServerChunkCache.java
2021-07-07 08:52:40 +02:00
@@ -703,6 +703,7 @@ public class ServerChunkCache extends ChunkSource {
2021-06-11 14:02:28 +02:00
this.level.getProfiler().push("purge");
this.level.timings.doChunkMap.startTiming(); // Spigot
this.distanceManager.purgeStaleTickets();
+ this.level.getServer().midTickLoadChunks(); // Paper
this.runDistanceManagerUpdates();
this.level.timings.doChunkMap.stopTiming(); // Spigot
this.level.getProfiler().popPush("chunks");
2021-07-07 08:52:40 +02:00
@@ -712,6 +713,7 @@ public class ServerChunkCache extends ChunkSource {
2021-06-11 14:02:28 +02:00
this.level.timings.doChunkUnload.startTiming(); // Spigot
this.level.getProfiler().popPush("unload");
2021-06-14 00:05:18 +02:00
this.chunkMap.tick(booleansupplier);
2021-06-11 14:02:28 +02:00
+ this.level.getServer().midTickLoadChunks(); // Paper
this.level.timings.doChunkUnload.stopTiming(); // Spigot
this.level.getProfiler().pop();
this.clearCache();
2021-07-07 08:52:40 +02:00
@@ -766,7 +768,7 @@ public class ServerChunkCache extends ChunkSource {
2021-06-11 14:02:28 +02:00
};
// Paper end
this.level.timings.chunkTicks.startTiming(); // Paper
- this.chunkMap.forEachVisibleChunk((playerchunk) -> { // Paper - safe iterator incase chunk loads, also no wrapping
+ final int[] chunksTicked = {0}; this.chunkMap.forEachVisibleChunk((playerchunk) -> { // Paper - safe iterator incase chunk loads, also no wrapping
Optional<LevelChunk> optional = ((Either) playerchunk.getTickingChunkFuture().getNow(ChunkHolder.UNLOADED_LEVEL_CHUNK)).left();
if (optional.isPresent()) {
2021-07-07 08:52:40 +02:00
@@ -777,6 +779,7 @@ public class ServerChunkCache extends ChunkSource {
2021-06-14 00:05:18 +02:00
chunk.setInhabitedTime(chunk.getInhabitedTime() + j);
if (flag1 && (this.spawnEnemies || this.spawnFriendlies) && this.level.getWorldBorder().isWithinBounds(chunk.getPos()) && !this.chunkMap.isOutsideOfRange(chunkcoordintpair, true)) { // Spigot
NaturalSpawner.spawnForChunk(this.level, chunk, spawnercreature_d, this.spawnFriendlies, this.spawnEnemies, flag2);
2021-06-11 14:02:28 +02:00
+ if (chunksTicked[0]++ % 10 == 0) this.level.getServer().midTickLoadChunks(); // Paper
}
2021-06-14 00:05:18 +02:00
// this.level.timings.doTickTiles.startTiming(); // Spigot // Paper
2021-07-07 08:52:40 +02:00
@@ -794,7 +797,7 @@ public class ServerChunkCache extends ChunkSource {
}
this.level.getProfiler().popPush("broadcast");
- this.chunkMap.getChunks().forEach((playerchunk) -> { // Paper - no... just no...
+ this.chunkMap.forEachVisibleChunk((playerchunk) -> { // Paper - safe iterator incase chunk loads, also no wrapping
Optional<LevelChunk> optional = ((Either) playerchunk.getTickingChunkFuture().getNow(ChunkHolder.UNLOADED_LEVEL_CHUNK)).left(); // CraftBukkit - decompile error
Objects.requireNonNull(playerchunk);
@@ -958,6 +961,41 @@ public class ServerChunkCache extends ChunkSource {
2021-06-11 14:02:28 +02:00
super.doRunTask(task);
}
+ // Paper start
+ private long lastMidTickChunkTask = 0;
+ public boolean pollChunkLoadTasks() {
+ if (com.destroystokyo.paper.io.chunk.ChunkTaskManager.pollChunkWaitQueue() || ServerChunkCache.this.level.asyncChunkTaskManager.pollNextChunkTask()) {
+ try {
+ ServerChunkCache.this.runDistanceManagerUpdates();
+ } finally {
+ // from below: process pending Chunk loadCallback() and unloadCallback() after each run task
+ chunkMap.callbackExecutor.run();
+ }
+ return true;
+ }
+ return false;
+ }
+ public void midTickLoadChunks() {
2021-06-14 00:05:18 +02:00
+ net.minecraft.server.MinecraftServer server = ServerChunkCache.this.level.getServer();
2021-06-11 14:02:28 +02:00
+ // always try to load chunks, restrain generation/other updates only. don't count these towards tick count
+ //noinspection StatementWithEmptyBody
+ while (pollChunkLoadTasks()) {}
+
+ if (System.nanoTime() - lastMidTickChunkTask < 200000) {
+ return;
+ }
+
+ for (;server.midTickChunksTasksRan < com.destroystokyo.paper.PaperConfig.midTickChunkTasks && server.haveTime();) {
+ if (this.pollTask()) {
+ server.midTickChunksTasksRan++;
+ lastMidTickChunkTask = System.nanoTime();
+ } else {
+ break;
+ }
+ }
+ }
+ // Paper end
+
@Override
// CraftBukkit start - process pending Chunk loadCallback() and unloadCallback() after each run task
2021-06-14 00:05:18 +02:00
public boolean pollTask() {
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
2021-07-07 08:52:40 +02:00
index 4ab1b6b8fb2e320c5654f8d0399bd40b40e3e0cf..c05b5cdd26abcf90f4c44099d186938ccdd57608 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
2021-07-07 08:52:40 +02:00
@@ -583,6 +583,7 @@ public class ServerLevel extends net.minecraft.world.level.Level implements Worl
2021-06-11 14:02:28 +02:00
}
timings.scheduledBlocks.stopTiming(); // Paper
+ this.getServer().midTickLoadChunks(); // Paper
gameprofilerfiller.popPush("raid");
this.timings.raids.startTiming(); // Paper - timings
this.raids.tick();
@@ -595,6 +596,7 @@ public class ServerLevel extends net.minecraft.world.level.Level implements Worl
2021-06-11 14:02:28 +02:00
timings.doSounds.startTiming(); // Spigot
this.runBlockEvents();
timings.doSounds.stopTiming(); // Spigot
+ this.getServer().midTickLoadChunks(); // Paper
this.handlingTick = false;
2021-06-14 00:05:18 +02:00
gameprofilerfiller.pop();
2021-06-11 14:02:28 +02:00
boolean flag3 = true || !this.players.isEmpty() || !this.getForcedChunks().isEmpty(); // CraftBukkit - this prevents entity cleanup, other issues on servers with no players
@@ -641,10 +643,12 @@ public class ServerLevel extends net.minecraft.world.level.Level implements Worl
2021-06-11 14:02:28 +02:00
timings.entityTick.stopTiming(); // Spigot
timings.tickEntities.stopTiming(); // Spigot
2021-06-14 00:05:18 +02:00
gameprofilerfiller.pop();
2021-06-11 14:02:28 +02:00
+ this.getServer().midTickLoadChunks(); // Paper
this.tickBlockEntities();
}
2021-06-14 00:05:18 +02:00
gameprofilerfiller.push("entityManagement");
+ this.getServer().midTickLoadChunks(); // Paper
this.entityManager.tick();
gameprofilerfiller.pop();
}