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. diff --git a/src/main/java/net/minecraft/Util.java b/src/main/java/net/minecraft/Util.java index 3ed0bf0485266c0a2b459f5cc1940f85f74e3624..b9775bc4e601fe1be8e514222e56ae782a897395 100644 --- a/src/main/java/net/minecraft/Util.java +++ b/src/main/java/net/minecraft/Util.java @@ -68,8 +68,8 @@ 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 BOOTSTRAP_EXECUTOR = makeExecutor("Bootstrap"); - private static final ExecutorService BACKGROUND_EXECUTOR = makeExecutor("Main"); + private static final ExecutorService BOOTSTRAP_EXECUTOR = makeExecutor("Bootstrap", -2); // Paper - add -2 priority + private static final ExecutorService BACKGROUND_EXECUTOR = makeExecutor("Main", -1); // Paper - add -1 priority private static final ExecutorService IO_POOL = makeIoExecutor(); public static LongSupplier timeSource = System::nanoTime; public static final UUID NIL_UUID = new UUID(0L, 0L); @@ -105,14 +105,18 @@ public class Util { return Instant.now().toEpochMilli(); } - 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 net.minecraft.server.ServerWorkerThread(target, s, priorityModifier)); + } + /* @Override protected void onTermination(Throwable throwable) { if (throwable != null) { @@ -128,6 +132,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 3380aca80bdda5edf8809f6fe36eaf5ba40a68ae..98a45d058afe89b824ecfa5b9e75e351291f0a1e 100644 --- a/src/main/java/net/minecraft/server/MinecraftServer.java +++ b/src/main/java/net/minecraft/server/MinecraftServer.java @@ -319,6 +319,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop