379 lines
17 KiB
Diff
379 lines
17 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: peaches94 <peachescu94@gmail.com>
|
|
Date: Sat, 2 Jul 2022 00:35:56 -0500
|
|
Subject: [PATCH] Multithreaded Tracker
|
|
|
|
This patch was ported downstream from the Petal fork, and is derived from
|
|
the Airplane fork by Paul Sauve
|
|
|
|
Based off the Airplane multithreaded tracker, this patch properly handles
|
|
concurrent accesses everywhere, as well as being much simpler to maintain
|
|
|
|
Some things are too unsafe to run off the main thread so we don't attempt to do
|
|
that. This multithreaded tracker remains accurate, non-breaking and fast.
|
|
|
|
diff --git a/src/main/java/dev/etil/mirai/MiraiConfig.java b/src/main/java/dev/etil/mirai/MiraiConfig.java
|
|
index 8dffa2fadd615cfd59cc85630b4a1454c87e5aa9..4c8dd8b1a3355578f067d547ab1d0151314f7ddf 100644
|
|
--- a/src/main/java/dev/etil/mirai/MiraiConfig.java
|
|
+++ b/src/main/java/dev/etil/mirai/MiraiConfig.java
|
|
@@ -275,4 +275,16 @@ public class MiraiConfig {
|
|
}
|
|
}
|
|
|
|
+ public static boolean enableAsyncEntityTracker;
|
|
+ public static boolean enableAsyncEntityTrackerInitialized;
|
|
+ private static void asyncEntityTracker() {
|
|
+ boolean temp = getBoolean("enable-async-entity-tracker", true,
|
|
+ "Whether or not async entity tracking should be enabled.",
|
|
+ "You may encounter issues with NPCs.");
|
|
+ if (!enableAsyncEntityTrackerInitialized) {
|
|
+ enableAsyncEntityTrackerInitialized = true;
|
|
+ enableAsyncEntityTracker = temp;
|
|
+ }
|
|
+ }
|
|
+
|
|
}
|
|
\ No newline at end of file
|
|
diff --git a/src/main/java/dev/etil/mirai/tracker/MultithreadedTracker.java b/src/main/java/dev/etil/mirai/tracker/MultithreadedTracker.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..613bd104762755395e86101decaf1cb7dc74d2ad
|
|
--- /dev/null
|
|
+++ b/src/main/java/dev/etil/mirai/tracker/MultithreadedTracker.java
|
|
@@ -0,0 +1,154 @@
|
|
+package dev.etil.mirai.tracker;
|
|
+
|
|
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
|
+import io.papermc.paper.util.maplist.IteratorSafeOrderedReferenceSet;
|
|
+import io.papermc.paper.world.ChunkEntitySlices;
|
|
+import net.minecraft.server.MinecraftServer;
|
|
+import net.minecraft.server.level.ChunkMap;
|
|
+import net.minecraft.world.entity.Entity;
|
|
+import net.minecraft.world.level.chunk.LevelChunk;
|
|
+
|
|
+import java.util.concurrent.ConcurrentLinkedQueue;
|
|
+import java.util.concurrent.Executor;
|
|
+import java.util.concurrent.Executors;
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
+
|
|
+public class MultithreadedTracker {
|
|
+
|
|
+ private enum TrackerStage {
|
|
+ UPDATE_PLAYERS,
|
|
+ SEND_CHANGES
|
|
+ }
|
|
+
|
|
+ private static final int parallelism = Math.max(4, Runtime.getRuntime().availableProcessors());
|
|
+ private static final Executor trackerExecutor = Executors.newFixedThreadPool(parallelism, new ThreadFactoryBuilder()
|
|
+ .setNameFormat("mirai-tracker-%d")
|
|
+ .setPriority(Thread.NORM_PRIORITY - 2)
|
|
+ .build());
|
|
+
|
|
+ private final IteratorSafeOrderedReferenceSet<LevelChunk> entityTickingChunks;
|
|
+ private final AtomicInteger taskIndex = new AtomicInteger();
|
|
+
|
|
+ private final ConcurrentLinkedQueue<Runnable> mainThreadTasks;
|
|
+ private final AtomicInteger finishedTasks = new AtomicInteger();
|
|
+
|
|
+ public MultithreadedTracker(IteratorSafeOrderedReferenceSet<LevelChunk> entityTickingChunks, ConcurrentLinkedQueue<Runnable> mainThreadTasks) {
|
|
+ this.entityTickingChunks = entityTickingChunks;
|
|
+ this.mainThreadTasks = mainThreadTasks;
|
|
+ }
|
|
+
|
|
+ public void tick() {
|
|
+ int iterator = this.entityTickingChunks.createRawIterator();
|
|
+
|
|
+ if (iterator == -1) {
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ // start with updating players
|
|
+ try {
|
|
+ this.taskIndex.set(iterator);
|
|
+ this.finishedTasks.set(0);
|
|
+
|
|
+ for (int i = 0; i < parallelism; i++) {
|
|
+ trackerExecutor.execute(this::runUpdatePlayers);
|
|
+ }
|
|
+
|
|
+ while (this.taskIndex.get() < this.entityTickingChunks.getListSize()) {
|
|
+ this.runMainThreadTasks();
|
|
+ this.handleChunkUpdates(5); // assist
|
|
+ }
|
|
+
|
|
+ while (this.finishedTasks.get() != parallelism) {
|
|
+ this.runMainThreadTasks();
|
|
+ }
|
|
+
|
|
+ this.runMainThreadTasks(); // finish any remaining tasks
|
|
+ } finally {
|
|
+ this.entityTickingChunks.finishRawIterator();
|
|
+ }
|
|
+
|
|
+ // then send changes
|
|
+ iterator = this.entityTickingChunks.createRawIterator();
|
|
+
|
|
+ if (iterator == -1) {
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ try {
|
|
+ do {
|
|
+ LevelChunk chunk = this.entityTickingChunks.rawGet(iterator);
|
|
+
|
|
+ if (chunk != null) {
|
|
+ this.updateChunkEntities(chunk, TrackerStage.SEND_CHANGES);
|
|
+ }
|
|
+ } while (++iterator < this.entityTickingChunks.getListSize());
|
|
+ } finally {
|
|
+ this.entityTickingChunks.finishRawIterator();
|
|
+ }
|
|
+ }
|
|
+
|
|
+ private void runMainThreadTasks() {
|
|
+ try {
|
|
+ Runnable task;
|
|
+ while ((task = this.mainThreadTasks.poll()) != null) {
|
|
+ task.run();
|
|
+ }
|
|
+ } catch (Throwable throwable) {
|
|
+ MinecraftServer.LOGGER.warn("Tasks failed while ticking track queue", throwable);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ private void runUpdatePlayers() {
|
|
+ try {
|
|
+ while (handleChunkUpdates(10));
|
|
+ } finally {
|
|
+ this.finishedTasks.incrementAndGet();
|
|
+ }
|
|
+ }
|
|
+
|
|
+ private boolean handleChunkUpdates(int tasks) {
|
|
+ int index;
|
|
+ while ((index = this.taskIndex.getAndAdd(tasks)) < this.entityTickingChunks.getListSize()) {
|
|
+ for (int i = index; i < index + tasks && i < this.entityTickingChunks.getListSize(); i++) {
|
|
+ LevelChunk chunk = this.entityTickingChunks.rawGet(i);
|
|
+ if (chunk != null) {
|
|
+ try {
|
|
+ this.updateChunkEntities(chunk, TrackerStage.UPDATE_PLAYERS);
|
|
+ } catch (Throwable throwable) {
|
|
+ MinecraftServer.LOGGER.warn("Ticking tracker failed", throwable);
|
|
+ }
|
|
+
|
|
+ }
|
|
+ }
|
|
+
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ return false;
|
|
+ }
|
|
+
|
|
+ private void updateChunkEntities(LevelChunk chunk, TrackerStage trackerStage) {
|
|
+ final ChunkEntitySlices entitySlices = chunk.level.getEntityLookup().getChunk(chunk.locX, chunk.locZ);
|
|
+ if (entitySlices == null) {
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ final Entity[] rawEntities = entitySlices.entities.getRawData();
|
|
+ final ChunkMap chunkMap = chunk.level.chunkSource.chunkMap;
|
|
+
|
|
+ for (int i = 0; i < rawEntities.length; i++) {
|
|
+ Entity entity = rawEntities[i];
|
|
+ if (entity != null) {
|
|
+ ChunkMap.TrackedEntity entityTracker = chunkMap.entityMap.get(entity.getId());
|
|
+ if (entityTracker != null) {
|
|
+ if (trackerStage == TrackerStage.SEND_CHANGES) {
|
|
+ entityTracker.serverEntity.sendChanges();
|
|
+ } else if (trackerStage == TrackerStage.UPDATE_PLAYERS) {
|
|
+ entityTracker.updatePlayers(entityTracker.entity.getPlayersInTrackRange());
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+}
|
|
\ No newline at end of file
|
|
diff --git a/src/main/java/io/papermc/paper/util/maplist/IteratorSafeOrderedReferenceSet.java b/src/main/java/io/papermc/paper/util/maplist/IteratorSafeOrderedReferenceSet.java
|
|
index 0fd814f1d65c111266a2b20f86561839a4cef755..dc06747df171678c8531e1153c5fa9b80b70baed 100644
|
|
--- a/src/main/java/io/papermc/paper/util/maplist/IteratorSafeOrderedReferenceSet.java
|
|
+++ b/src/main/java/io/papermc/paper/util/maplist/IteratorSafeOrderedReferenceSet.java
|
|
@@ -15,7 +15,7 @@ public final class IteratorSafeOrderedReferenceSet<E> {
|
|
|
|
/* list impl */
|
|
protected E[] listElements;
|
|
- protected int listSize;
|
|
+ protected int listSize; public int getListSize() { return this.listSize; } // Mirai - expose listSize
|
|
|
|
protected final double maxFragFactor;
|
|
|
|
diff --git a/src/main/java/io/papermc/paper/world/ChunkEntitySlices.java b/src/main/java/io/papermc/paper/world/ChunkEntitySlices.java
|
|
index 209c4f0b25470bff7278c0a8dcd30576900b9933..603eb6ca629796cfd00c831a509fb71ad0fba360 100644
|
|
--- a/src/main/java/io/papermc/paper/world/ChunkEntitySlices.java
|
|
+++ b/src/main/java/io/papermc/paper/world/ChunkEntitySlices.java
|
|
@@ -35,7 +35,7 @@ public final class ChunkEntitySlices {
|
|
protected final EntityCollectionBySection allEntities;
|
|
protected final EntityCollectionBySection hardCollidingEntities;
|
|
protected final Reference2ObjectOpenHashMap<Class<? extends Entity>, EntityCollectionBySection> entitiesByClass;
|
|
- protected final EntityList entities = new EntityList();
|
|
+ public final EntityList entities = new EntityList();
|
|
|
|
public ChunkHolder.FullChunkStatus status;
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/level/ChunkMap.java b/src/main/java/net/minecraft/server/level/ChunkMap.java
|
|
index c01c22b6fda9e36a2336a992c760b813b71469ce..9f0fe4672abcfe417fd5f160ca497ad323434d5f 100644
|
|
--- a/src/main/java/net/minecraft/server/level/ChunkMap.java
|
|
+++ b/src/main/java/net/minecraft/server/level/ChunkMap.java
|
|
@@ -1237,8 +1237,36 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
|
|
entity.tracker = null; // Paper - We're no longer tracked
|
|
}
|
|
|
|
+ // Mirai start - multithreaded tracker
|
|
+ private @Nullable dev.etil.mirai.tracker.MultithreadedTracker multithreadedTracker;
|
|
+ private final java.util.concurrent.ConcurrentLinkedQueue<Runnable> trackerMainThreadTasks = new java.util.concurrent.ConcurrentLinkedQueue<>();
|
|
+ private boolean tracking = false;
|
|
+
|
|
+ public void runOnTrackerMainThread(final Runnable runnable) {
|
|
+ if (this.tracking) {
|
|
+ this.trackerMainThreadTasks.add(runnable);
|
|
+ } else {
|
|
+ runnable.run();
|
|
+ }
|
|
+ }
|
|
+
|
|
// Paper start - optimised tracker
|
|
private final void processTrackQueue() {
|
|
+ if (dev.etil.mirai.MiraiConfig.enableAsyncEntityTracker) {
|
|
+ if (this.multithreadedTracker == null) {
|
|
+ this.multithreadedTracker = new dev.etil.mirai.tracker.MultithreadedTracker(this.level.chunkSource.entityTickingChunks, this.trackerMainThreadTasks);
|
|
+ }
|
|
+
|
|
+ this.tracking = true;
|
|
+ try {
|
|
+ this.multithreadedTracker.tick();
|
|
+ } finally {
|
|
+ this.tracking = false;
|
|
+ }
|
|
+ return;
|
|
+ }
|
|
+ // Mirai end
|
|
+
|
|
this.level.timings.tracker1.startTiming();
|
|
try {
|
|
for (TrackedEntity tracker : this.entityMap.values()) {
|
|
@@ -1462,11 +1490,11 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
|
|
|
|
public class TrackedEntity {
|
|
|
|
- final ServerEntity serverEntity;
|
|
- final Entity entity;
|
|
+ public final ServerEntity serverEntity; // Mirai -> public
|
|
+ public final Entity entity; // Mirai -> public
|
|
private final int range;
|
|
SectionPos lastSectionPos;
|
|
- public final Set<ServerPlayerConnection> seenBy = new ReferenceOpenHashSet<>(); // Paper - optimise map impl
|
|
+ public final Set<ServerPlayerConnection> seenBy = it.unimi.dsi.fastutil.objects.ReferenceSets.synchronize(new ReferenceOpenHashSet<>()); // Paper - optimise map impl // Mirai - sync
|
|
|
|
public TrackedEntity(Entity entity, int i, int j, boolean flag) {
|
|
this.serverEntity = new ServerEntity(ChunkMap.this.level, entity, j, flag, this::broadcast, this.seenBy); // CraftBukkit
|
|
@@ -1478,7 +1506,7 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
|
|
// Paper start - use distance map to optimise tracker
|
|
com.destroystokyo.paper.util.misc.PooledLinkedHashSets.PooledObjectLinkedOpenHashSet<ServerPlayer> lastTrackerCandidates;
|
|
|
|
- final void updatePlayers(com.destroystokyo.paper.util.misc.PooledLinkedHashSets.PooledObjectLinkedOpenHashSet<ServerPlayer> newTrackerCandidates) {
|
|
+ public final void updatePlayers(com.destroystokyo.paper.util.misc.PooledLinkedHashSets.PooledObjectLinkedOpenHashSet<ServerPlayer> newTrackerCandidates) { // Mirai -> public
|
|
com.destroystokyo.paper.util.misc.PooledLinkedHashSets.PooledObjectLinkedOpenHashSet<ServerPlayer> oldTrackerCandidates = this.lastTrackerCandidates;
|
|
this.lastTrackerCandidates = newTrackerCandidates;
|
|
|
|
@@ -1550,7 +1578,7 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
|
|
}
|
|
|
|
public void removePlayer(ServerPlayer player) {
|
|
- org.spigotmc.AsyncCatcher.catchOp("player tracker clear"); // Spigot
|
|
+ //org.spigotmc.AsyncCatcher.catchOp("player tracker clear"); // Spigot // Mirai - we can remove async too
|
|
if (this.seenBy.remove(player.connection)) {
|
|
this.serverEntity.removePairing(player);
|
|
}
|
|
@@ -1558,7 +1586,7 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
|
|
}
|
|
|
|
public void updatePlayer(ServerPlayer player) {
|
|
- org.spigotmc.AsyncCatcher.catchOp("player tracker update"); // Spigot
|
|
+ //org.spigotmc.AsyncCatcher.catchOp("player tracker update"); // Spigot // Mirai - we can update async
|
|
if (player != this.entity) {
|
|
// Paper start - remove allocation of Vec3D here
|
|
// Vec3 vec3d = player.position().subtract(this.entity.position());
|
|
diff --git a/src/main/java/net/minecraft/server/level/ServerBossEvent.java b/src/main/java/net/minecraft/server/level/ServerBossEvent.java
|
|
index ca42c2642a729b90d22b968af7258f3aee72e14b..c4d6d5be09c788bbc062c50d8547538eb84530b4 100644
|
|
--- a/src/main/java/net/minecraft/server/level/ServerBossEvent.java
|
|
+++ b/src/main/java/net/minecraft/server/level/ServerBossEvent.java
|
|
@@ -13,7 +13,7 @@ import net.minecraft.util.Mth;
|
|
import net.minecraft.world.BossEvent;
|
|
|
|
public class ServerBossEvent extends BossEvent {
|
|
- private final Set<ServerPlayer> players = Sets.newHashSet();
|
|
+ private final Set<ServerPlayer> players = Sets.newConcurrentHashSet(); // Mirai - players can be removed in async tracking
|
|
private final Set<ServerPlayer> unmodifiablePlayers = Collections.unmodifiableSet(this.players);
|
|
public boolean visible = true;
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/level/ServerEntity.java b/src/main/java/net/minecraft/server/level/ServerEntity.java
|
|
index 3a0ff721e01e5bb2b2d05459019f8bba5f5d0737..52697f4907d63b975c2d5045f7d11bc76dd6b78d 100644
|
|
--- a/src/main/java/net/minecraft/server/level/ServerEntity.java
|
|
+++ b/src/main/java/net/minecraft/server/level/ServerEntity.java
|
|
@@ -266,14 +266,18 @@ public class ServerEntity {
|
|
|
|
public void removePairing(ServerPlayer player) {
|
|
this.entity.stopSeenByPlayer(player);
|
|
- player.connection.send(new ClientboundRemoveEntitiesPacket(new int[]{this.entity.getId()}));
|
|
+ // Mirai start - ensure main thread
|
|
+ ((ServerLevel) this.entity.level).chunkSource.chunkMap.runOnTrackerMainThread(() ->
|
|
+ player.connection.send(new ClientboundRemoveEntitiesPacket(new int[]{this.entity.getId()}))
|
|
+ );
|
|
+ // Mirai end
|
|
}
|
|
|
|
public void addPairing(ServerPlayer player) {
|
|
ServerGamePacketListenerImpl playerconnection = player.connection;
|
|
|
|
Objects.requireNonNull(player.connection);
|
|
- this.sendPairingData(playerconnection::send, player); // CraftBukkit - add player
|
|
+ ((ServerLevel) this.entity.level).chunkSource.chunkMap.runOnTrackerMainThread(() -> this.sendPairingData(playerconnection::send, player)); // CraftBukkit - add player // Mirai - main thread
|
|
this.entity.startSeenByPlayer(player);
|
|
}
|
|
|
|
@@ -381,19 +385,30 @@ public class ServerEntity {
|
|
SynchedEntityData datawatcher = this.entity.getEntityData();
|
|
|
|
if (datawatcher.isDirty()) {
|
|
- this.broadcastAndSend(new ClientboundSetEntityDataPacket(this.entity.getId(), datawatcher, false));
|
|
+ // Mirai start - sync
|
|
+ ((ServerLevel) this.entity.level).chunkSource.chunkMap.runOnTrackerMainThread(() ->
|
|
+ this.broadcastAndSend(new ClientboundSetEntityDataPacket(this.entity.getId(), datawatcher, false))
|
|
+ );
|
|
+ // Mirai end
|
|
}
|
|
|
|
if (this.entity instanceof LivingEntity) {
|
|
Set<AttributeInstance> set = ((LivingEntity) this.entity).getAttributes().getDirtyAttributes();
|
|
|
|
if (!set.isEmpty()) {
|
|
+ // Mirai start - sync
|
|
+ final var copy = Lists.newArrayList(set);
|
|
+ ((ServerLevel) this.entity.level).chunkSource.chunkMap.runOnTrackerMainThread(() -> {
|
|
+
|
|
// CraftBukkit start - Send scaled max health
|
|
if (this.entity instanceof ServerPlayer) {
|
|
- ((ServerPlayer) this.entity).getBukkitEntity().injectScaledMaxHealth(set, false);
|
|
+ ((ServerPlayer) this.entity).getBukkitEntity().injectScaledMaxHealth(copy, false);
|
|
}
|
|
// CraftBukkit end
|
|
- this.broadcastAndSend(new ClientboundUpdateAttributesPacket(this.entity.getId(), set));
|
|
+ this.broadcastAndSend(new ClientboundUpdateAttributesPacket(this.entity.getId(), copy));
|
|
+
|
|
+ });
|
|
+ // Mirai end
|
|
}
|
|
|
|
set.clear();
|