From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Aikar Date: Tue, 23 Oct 2018 23:14:38 -0400 Subject: [PATCH] Improve Server Thread Pool and Thread Priorities Use a simple executor since Fork join is a much more complex pool type and we are not using its capabilities. Set thread priorities so main thread has above normal priority over server threads Allow usage of a single thread executor by not using ForkJoin so single core CPU's. == AT == public net.minecraft.Util onThreadException(Ljava/lang/Thread;Ljava/lang/Throwable;)V diff --git a/src/main/java/io/papermc/paper/util/ServerWorkerThread.java b/src/main/java/io/papermc/paper/util/ServerWorkerThread.java new file mode 100644 index 0000000000000000000000000000000000000000..b60f59cf5cc8eb84a6055b7861857dece7f2501b --- /dev/null +++ b/src/main/java/io/papermc/paper/util/ServerWorkerThread.java @@ -0,0 +1,14 @@ +package io.papermc.paper.util; + +import java.util.concurrent.atomic.AtomicInteger; +import net.minecraft.Util; + +public class ServerWorkerThread extends Thread { + private static final AtomicInteger threadId = new AtomicInteger(1); + public ServerWorkerThread(Runnable target, String poolName, int prioritityModifier) { + super(target, "Worker-" + poolName + "-" + threadId.getAndIncrement()); + setPriority(Thread.NORM_PRIORITY+prioritityModifier); // Deprioritize over main + this.setDaemon(true); + this.setUncaughtExceptionHandler(Util::onThreadException); + } +} diff --git a/src/main/java/net/minecraft/Util.java b/src/main/java/net/minecraft/Util.java index 33ec55ed1f7d10a5235330ad082e7c7b54c42368..d5b37301d4e860c019105445001ed7a9a6f849bd 100644 --- a/src/main/java/net/minecraft/Util.java +++ b/src/main/java/net/minecraft/Util.java @@ -80,7 +80,7 @@ public class Util { private static final int DEFAULT_MAX_THREADS = 255; private static final String MAX_THREADS_SYSTEM_PROPERTY = "max.bg.threads"; private static final AtomicInteger WORKER_COUNT = new AtomicInteger(1); - private static final ExecutorService BACKGROUND_EXECUTOR = makeExecutor("Main"); + private static final ExecutorService BACKGROUND_EXECUTOR = makeExecutor("Main", -1); // Paper - add -1 priority // 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() { @@ -143,14 +143,18 @@ public class Util { return FILENAME_DATE_TIME_FORMATTER.format(ZonedDateTime.now()); } - private static ExecutorService makeExecutor(String name) { - int i = Mth.clamp(Runtime.getRuntime().availableProcessors() - 1, 1, getMaxThreads()); + private static ExecutorService makeExecutor(String s, int priorityModifier) { // Paper - add priority + // Paper start - use simpler thread pool that allows 1 thread + int i = Math.min(8, Math.max(Runtime.getRuntime().availableProcessors() - 2, 1)); + i = Integer.getInteger("Paper.WorkerThreadCount", i); ExecutorService executorService; + if (i <= 0) { executorService = MoreExecutors.newDirectExecutorService(); } else { - executorService = new ForkJoinPool(i, (forkJoinPool) -> { - ForkJoinWorkerThread forkJoinWorkerThread = new ForkJoinWorkerThread(forkJoinPool) { + 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)); + } + /* @Override protected void onTermination(Throwable throwable) { if (throwable != null) { @@ -166,6 +170,7 @@ public class Util { return forkJoinWorkerThread; }, Util::onThreadException, true); } + }*/ // Paper end return executorService; } diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java index 8c64c3c1e33cb289ac2eb1b55e1febf6920fd4c6..52d602fdaad361deb3b3e0ff2b99b7808e4a32cf 100644 --- a/src/main/java/net/minecraft/server/MinecraftServer.java +++ b/src/main/java/net/minecraft/server/MinecraftServer.java @@ -320,6 +320,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop