From 4a5ff5039c97dce6c77b063febf3cf286aa74306 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Fri, 29 Nov 2024 17:07:35 +0100 Subject: [PATCH] Clean up thread pool usage (#11681) Using an unbound LinkedBlockingQueue means you *have* to set core and max core thread pool size the same, as they will never go above the minimum pool size by just passing them through. So this fixes the async command executor pool to actually use 2 threads, and also cleans up other usage to be explicitly "fixed" thread pool sizes, and splits off one more in Minecraft's Util class --- .../server/Async-command-map-building.patch | 11 ++-- ...er-Thread-Pool-and-Thread-Priorities.patch | 2 +- patches/server/MC-Utils.patch | 22 ++++---- .../Separate-dimensiondata-executor.patch | 53 +++++++++++++++++++ 4 files changed, 66 insertions(+), 22 deletions(-) create mode 100644 patches/server/Separate-dimensiondata-executor.patch diff --git a/patches/server/Async-command-map-building.patch b/patches/server/Async-command-map-building.patch index d6df455d42..e92905e614 100644 --- a/patches/server/Async-command-map-building.patch +++ b/patches/server/Async-command-map-building.patch @@ -17,19 +17,14 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 // CraftBukkit start // Register Vanilla commands into builtRoot as before + // Paper start - Perf: Async command map building -+ COMMAND_SENDING_POOL.execute(() -> { -+ this.sendAsync(player); -+ }); ++ COMMAND_SENDING_POOL.execute(() -> this.sendAsync(player)); + } + -+ public static final java.util.concurrent.ThreadPoolExecutor COMMAND_SENDING_POOL = new java.util.concurrent.ThreadPoolExecutor( -+ 0, 2, 60L, java.util.concurrent.TimeUnit.SECONDS, -+ new java.util.concurrent.LinkedBlockingQueue<>(), ++ public static final java.util.concurrent.ExecutorService COMMAND_SENDING_POOL = java.util.concurrent.Executors.newFixedThreadPool(2, + new com.google.common.util.concurrent.ThreadFactoryBuilder() + .setNameFormat("Paper Async Command Builder Thread Pool - %1$d") + .setUncaughtExceptionHandler(new net.minecraft.DefaultUncaughtExceptionHandlerWithName(net.minecraft.server.MinecraftServer.LOGGER)) -+ .build(), -+ new java.util.concurrent.ThreadPoolExecutor.DiscardPolicy() ++ .build() + ); + + private void sendAsync(ServerPlayer player) { diff --git a/patches/server/Improve-Server-Thread-Pool-and-Thread-Priorities.patch b/patches/server/Improve-Server-Thread-Pool-and-Thread-Priorities.patch index 69b21180ee..2a199c2b06 100644 --- a/patches/server/Improve-Server-Thread-Pool-and-Thread-Priorities.patch +++ b/patches/server/Improve-Server-Thread-Pool-and-Thread-Priorities.patch @@ -78,7 +78,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 - AtomicInteger atomicInteger = new AtomicInteger(1); - executorService = new ForkJoinPool(i, pool -> { - final String string2 = "Worker-" + name + "-" + atomicInteger.getAndIncrement(); -+ executorService = new java.util.concurrent.ThreadPoolExecutor(i, i,0L, TimeUnit.MILLISECONDS, new java.util.concurrent.LinkedBlockingQueue<>(), target -> new io.papermc.paper.util.ServerWorkerThread(target, s, priorityModifier)); ++ executorService = Executors.newFixedThreadPool(i, target -> new io.papermc.paper.util.ServerWorkerThread(target, s, priorityModifier)); + } + /* final String string2 = "Worker-" + name + "-" + atomicInteger.getAndIncrement(); ForkJoinWorkerThread forkJoinWorkerThread = new ForkJoinWorkerThread(pool) { diff --git a/patches/server/MC-Utils.patch b/patches/server/MC-Utils.patch index d01fb2d06c..bf7d0b1b70 100644 --- a/patches/server/MC-Utils.patch +++ b/patches/server/MC-Utils.patch @@ -4861,9 +4861,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; -+import java.util.concurrent.LinkedBlockingQueue; -+import java.util.concurrent.ThreadPoolExecutor; -+import java.util.concurrent.TimeUnit; ++import java.util.concurrent.ExecutorService; ++import java.util.concurrent.Executors; +import java.util.function.BiConsumer; +import java.util.function.Consumer; +import java.util.function.Function; @@ -4888,13 +4887,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + run.run(); + } + }; -+ public static final ThreadPoolExecutor asyncExecutor = new ThreadPoolExecutor( -+ 2, 2, 60L, TimeUnit.SECONDS, -+ new LinkedBlockingQueue<>(), -+ new ThreadFactoryBuilder() -+ .setNameFormat("Paper Async Task Handler Thread - %1$d") -+ .setUncaughtExceptionHandler(new net.minecraft.DefaultUncaughtExceptionHandlerWithName(MinecraftServer.LOGGER)) -+ .build() ++ public static final ExecutorService ASYNC_EXECUTOR = Executors.newFixedThreadPool(2, new ThreadFactoryBuilder() ++ .setNameFormat("Paper Async Task Handler Thread - %1$d") ++ .setUncaughtExceptionHandler(new net.minecraft.DefaultUncaughtExceptionHandlerWithName(MinecraftServer.LOGGER)) ++ .build() + ); + + private MCUtil() { @@ -5029,7 +5025,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + } + + public static void scheduleAsyncTask(Runnable run) { -+ asyncExecutor.execute(run); ++ ASYNC_EXECUTOR.execute(run); + } + + public static ResourceKey toResourceKey( @@ -5223,8 +5219,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 MinecraftServer.LOGGER.error("Failed to unlock level {}", this.storageSource.getLevelId(), ioexception1); } // Spigot start -+ io.papermc.paper.util.MCUtil.asyncExecutor.shutdown(); // Paper -+ try { io.papermc.paper.util.MCUtil.asyncExecutor.awaitTermination(30, java.util.concurrent.TimeUnit.SECONDS); // Paper ++ io.papermc.paper.util.MCUtil.ASYNC_EXECUTOR.shutdown(); // Paper ++ try { io.papermc.paper.util.MCUtil.ASYNC_EXECUTOR.awaitTermination(30, java.util.concurrent.TimeUnit.SECONDS); // Paper + } catch (java.lang.InterruptedException ignored) {} // Paper if (org.spigotmc.SpigotConfig.saveUserCacheOnStopOnly) { MinecraftServer.LOGGER.info("Saving usercache.json"); diff --git a/patches/server/Separate-dimensiondata-executor.patch b/patches/server/Separate-dimensiondata-executor.patch new file mode 100644 index 0000000000..845818cfda --- /dev/null +++ b/patches/server/Separate-dimensiondata-executor.patch @@ -0,0 +1,53 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Nassim Jahnke +Date: Thu, 28 Nov 2024 10:35:58 +0100 +Subject: [PATCH] Separate dimensiondata executor + + +diff --git a/src/main/java/net/minecraft/Util.java b/src/main/java/net/minecraft/Util.java +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/net/minecraft/Util.java ++++ b/src/main/java/net/minecraft/Util.java +@@ -0,0 +0,0 @@ public class Util { + private static final String MAX_THREADS_SYSTEM_PROPERTY = "max.bg.threads"; + private static final TracingExecutor BACKGROUND_EXECUTOR = makeExecutor("Main", -1); // Paper - Perf: add priority + private static final TracingExecutor IO_POOL = makeIoExecutor("IO-Worker-", false); ++ public static final TracingExecutor DIMENSION_DATA_IO_POOL = makeExtraIoExecutor("Dimension-Data-IO-Worker-"); // Paper - Separate dimension data IO pool + private static final TracingExecutor DOWNLOAD_POOL = makeIoExecutor("Download-", true); + // Paper start - don't submit BLOCKING PROFILE LOOKUPS to the world gen thread + public static final ExecutorService PROFILE_EXECUTOR = Executors.newFixedThreadPool(2, new java.util.concurrent.ThreadFactory() { +@@ -0,0 +0,0 @@ public class Util { + })); + } + ++ // Paper start - Separate dimension data IO pool ++ private static TracingExecutor makeExtraIoExecutor(String namePrefix) { ++ AtomicInteger atomicInteger = new AtomicInteger(1); ++ return new TracingExecutor(Executors.newFixedThreadPool(4, runnable -> { ++ Thread thread = new Thread(runnable); ++ String string2 = namePrefix + atomicInteger.getAndIncrement(); ++ TracyClient.setThreadName(string2, namePrefix.hashCode()); ++ thread.setName(string2); ++ thread.setDaemon(false); ++ thread.setUncaughtExceptionHandler(Util::onThreadException); ++ return thread; ++ })); ++ } ++ // Paper end - Separate dimension data IO pool ++ + public static void throwAsRuntime(Throwable t) { + throw t instanceof RuntimeException ? (RuntimeException)t : new RuntimeException(t); + } +diff --git a/src/main/java/net/minecraft/world/level/storage/DimensionDataStorage.java b/src/main/java/net/minecraft/world/level/storage/DimensionDataStorage.java +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/net/minecraft/world/level/storage/DimensionDataStorage.java ++++ b/src/main/java/net/minecraft/world/level/storage/DimensionDataStorage.java +@@ -0,0 +0,0 @@ public class DimensionDataStorage implements AutoCloseable { + } catch (IOException var3) { + LOGGER.error("Could not save data to {}", path.getFileName(), var3); + } +- }, Util.ioPool()); ++ }, Util.DIMENSION_DATA_IO_POOL); // Paper - Separate dimension data IO pool + } + + public void saveAndJoin() {